impexp.go 1.85 KB
Newer Older
1 2 3 4 5 6 7 8 9
package main

import (
	"dfss/dfssc/user"
	"fmt"
	"os"
)

// export the certificate and private key of the user
Loïck Bonniot's avatar
Loïck Bonniot committed
10 11
func exportConf(args []string) {
	confFile := args[0]
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	fmt.Println("Export user configuration")
	var keyPassphrase, confPassphrase string

	config, err := user.NewConfig(fkey, fcert)
	if err != nil {
		_, _ = os.Stderr.WriteString(fmt.Sprintf("Couldn't open the files: %s", err))
		os.Exit(1)
		return
	}

	err = readPassphrases(&keyPassphrase, &confPassphrase, true)
	if err != nil {
		_, _ = os.Stderr.WriteString(fmt.Sprintf("An error occurred: %s", err))
		os.Exit(1)
		return
	}

	err = config.SaveConfigToFile(confFile, confPassphrase, keyPassphrase)
	if err != nil {
		_, _ = os.Stderr.WriteString(fmt.Sprintf("Couldn't save the configuration on the disk: %s", err))
		os.Exit(1)
		return
	}

}

// Read two passphrases for the configuration
func readPassphrases(keyPassphrase, confPassphrase *string, second bool) error {
	fmt.Println("Enter the passphrase of the configuration")
	err := readPassword(confPassphrase, second)
	if err != nil {
		return err
	}

	fmt.Println("Enter the passphrase of your current key (if any)")
47
	return readPassword(keyPassphrase, false)
48 49 50
}

// import the configuration
Loïck Bonniot's avatar
Loïck Bonniot committed
51 52
func importConf(args []string) {
	confFile := args[0]
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	var keyPassphrase, confPassphrase string
	err := readPassphrases(&keyPassphrase, &confPassphrase, false)
	if err != nil {
		_, _ = os.Stderr.WriteString(fmt.Sprintf("An error occurred: %s", err))
		os.Exit(1)
		return
	}

	config, err := user.DecodeConfiguration(confFile, keyPassphrase, confPassphrase)
	if err != nil {
		_, _ = os.Stderr.WriteString(fmt.Sprintf("Couldnd't decrypt the configuration: %s", err))
		os.Exit(1)
		return
	}

	err = config.SaveUserInformations()
	if err != nil {
		_, _ = os.Stderr.WriteString(fmt.Sprintf("Couldn't save the certificate and private key: %s", err))
		os.Exit(1)
		return
	}
}