main.go 3.68 KB
Newer Older
1 2 3
package main

import (
4 5 6 7 8 9
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"runtime"

10
	"dfss"
11
	dapi "dfss/dfssd/api"
12
	"dfss/dfssp/authority"
13
	"dfss/dfssp/server"
14
	"dfss/net"
15 16 17
)

var (
18 19 20
	verbose                                                  bool
	path, country, org, unit, cn, port, address, dbURI, demo string
	keySize, rootValidity, certValidity                      int
21 22 23 24 25 26
)

func init() {

	flag.BoolVar(&verbose, "v", false, "Print verbose messages")

27
	flag.StringVar(&demo, "d", "", "Demonstrator address and port (empty string disables debug)")
28 29 30
	flag.StringVar(&port, "p", "9000", "Default port listening")
	flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening")

Loïck Bonniot's avatar
Loïck Bonniot committed
31
	flag.StringVar(&path, "path", ".", "Path for the platform's private key and root certificate")
Caro Axel's avatar
Caro Axel committed
32 33 34 35 36 37
	flag.StringVar(&country, "country", "France", "Country for the root certificate")
	flag.StringVar(&org, "org", "DFSS", "Organization for the root certificate")
	flag.StringVar(&unit, "unit", "INSA Rennes", "Organizational unit for the root certificate")
	flag.StringVar(&cn, "cn", "dfssp", "Common name for the root certificate")

	flag.IntVar(&keySize, "keySize", 512, "Encoding size for the private key")
Loïck Bonniot's avatar
Loïck Bonniot committed
38 39
	flag.IntVar(&rootValidity, "rootValidity", 365, "Root certificate's validity duration (days)")
	flag.IntVar(&certValidity, "certValidity", 365, "Validity duration for the certificates generated by this platform (days)")
Caro Axel's avatar
Caro Axel committed
40

41
	flag.StringVar(&dbURI, "db", "mongodb://localhost/dfss", "Name of the environment variable containing the server url in standard MongoDB format")
Caro Axel's avatar
Caro Axel committed
42

43 44 45 46 47
	flag.Usage = func() {
		fmt.Println("DFSS platform v" + dfss.Version)
		fmt.Println("Users and contracts manager")

		fmt.Println("\nUsage:")
48
		fmt.Println("  dfssp [flags] command")
49 50

		fmt.Println("\nThe commands are:")
Loïck Bonniot's avatar
Loïck Bonniot committed
51
		fmt.Println("  init     [cn, country, keySize, org, path, unit, rootValidity]")
Caro Axel's avatar
Caro Axel committed
52
		fmt.Println("           create and save the platform's private key and root certificate")
53 54 55
		fmt.Println("  ttp      [cn, country, keySize, org, path, unit, certValidity]")
		fmt.Println("           create and save the TTP's private key and certificate")
		fmt.Println("  start    [path, db, a, p, certValidity]")
Caro Axel's avatar
Caro Axel committed
56
		fmt.Println("           start the platform after loading its private key and root certificate")
57 58 59 60 61 62 63 64 65 66 67 68 69
		fmt.Println("  help     print this help")
		fmt.Println("  version  print dfss client version")

		fmt.Println("\nFlags:")
		flag.PrintDefaults()

		fmt.Println()
	}
}

func main() {
	flag.Parse()
	command := flag.Arg(0)
70
	dapi.Configure(demo != "", demo, "platform")
71 72 73 74

	switch command {
	case "version":
		fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
Caro Axel's avatar
Caro Axel committed
75
	case "init":
76
		err := authority.Initialize(keySize, rootValidity, country, org, unit, cn, path, nil, nil)
Caro Axel's avatar
Caro Axel committed
77
		if err != nil {
78
			fmt.Fprintln(os.Stderr, "An error occured during the initialization operation:", err)
79
			os.Exit(1)
Caro Axel's avatar
Caro Axel committed
80
		}
81 82 83 84 85 86 87 88 89 90 91 92
		dapi.DLog("Private key and root certificate generated")
	case "ttp":
		pid, err := authority.Start(path)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Bad root CA or key; please use the `init` command before the `ttp` one.\n", err)
		}
		ttpPath := filepath.Join(path, "ttp")
		err = authority.Initialize(keySize, certValidity, country, org, unit, cn, ttpPath, pid.RootCA, pid.Pkey)
		if err != nil {
			fmt.Fprintln(os.Stderr, "An error occured during TTP credentials generation:", err)
		}
		dapi.DLog("Private key and certificate generated for TTP")
Caro Axel's avatar
Caro Axel committed
93
	case "start":
Loïck Bonniot's avatar
Loïck Bonniot committed
94
		srv := server.GetServer(path, dbURI, certValidity, verbose)
95
		fmt.Println("Listening on " + address + ":" + port)
96
		dapi.DLog("Platform server started on " + address + ":" + port)
97
		err := net.Listen(address+":"+port, srv)
98
		if err != nil {
99
			fmt.Fprintln(os.Stderr, err)
Caro Axel's avatar
Caro Axel committed
100
		}
101 102 103
	default:
		flag.Usage()
	}
104 105

	dapi.DClose()
106
}