register.go 3.01 KB
Newer Older
1 2 3
package main

import (
Loïck Bonniot's avatar
Loïck Bonniot committed
4
	"bufio"
5 6
	"errors"
	"fmt"
Loïck Bonniot's avatar
Loïck Bonniot committed
7 8 9 10
	"os"
	osuser "os/user"
	"strconv"
	"strings"
11

Loïck Bonniot's avatar
Loïck Bonniot committed
12
	"dfss/dfssc/user"
13 14 15
	"golang.org/x/crypto/ssh/terminal"
)

Loïck Bonniot's avatar
Loïck Bonniot committed
16
func registerUser(_ []string) {
Loïck Bonniot's avatar
Loïck Bonniot committed
17 18 19 20 21
	fmt.Println("Registering a new user")
	// Initialize variables
	var country, mail, organization, unit, passphrase string
	var bits int

22
	name := "Jon Doe"
Loïck Bonniot's avatar
Loïck Bonniot committed
23
	u, err := osuser.Current()
24 25
	if err == nil {
		name = u.Name
Loïck Bonniot's avatar
Loïck Bonniot committed
26 27 28 29
	}

	// Get all the necessary parameters
	readStringParam("Mail", "", &mail)
30 31 32
	readStringParam("Country", "FR", &country)
	readStringParam("Organization", name, &organization)
	readStringParam("Organizational unit", name, &unit)
Loïck Bonniot's avatar
Loïck Bonniot committed
33 34 35 36
	readIntParam("Length of the key (2048 or 4096)", "2048", &bits)
	err = readPassword(&passphrase, true)
	if err != nil {
		fmt.Println("An error occurred:", err.Error())
37
		os.Exit(1)
Loïck Bonniot's avatar
Loïck Bonniot committed
38 39 40 41 42 43 44
		return
	}

	recapUser(mail, country, organization, unit)
	err = user.Register(fca, fcert, fkey, addrPort, passphrase, country, organization, unit, mail, bits)
	if err != nil {
		fmt.Println("An error occurred:", err.Error())
45
		os.Exit(2)
Loïck Bonniot's avatar
Loïck Bonniot committed
46 47 48
	}
}

49 50 51
// We need to use ONLY ONE reader: buffio buffers some data (= consumes from stdin)
var reader *bufio.Reader

52 53
// Get a string parameter from standard input
func readStringParam(message, def string, ptr *string) {
Loïck Bonniot's avatar
Loïck Bonniot committed
54 55 56 57 58 59
	fmt.Print(message)
	if len(def) > 0 {
		fmt.Printf(" [%s]", def)
	}
	fmt.Print(": ")

60 61 62
	if reader == nil {
		reader = bufio.NewReader(os.Stdin)
	}
Loïck Bonniot's avatar
Loïck Bonniot committed
63 64 65 66 67 68 69 70
	value, _ := reader.ReadString('\n')

	// Trim newline symbols
	value = strings.TrimRight(value, "\n")
	value = strings.TrimRight(value, "\r")

	*ptr = value
	if value == "" {
71 72
		*ptr = def
	}
Loïck Bonniot's avatar
Loïck Bonniot committed
73

74 75
}

Loïck Bonniot's avatar
Loïck Bonniot committed
76 77 78 79 80 81 82 83
func readIntParam(message, def string, ptr *int) {
	var str string
	readStringParam(message, def, &str)
	value, err := strconv.Atoi(str)
	if err != nil {
		*ptr = 0
	} else {
		*ptr = value
84 85 86 87
	}
}

// Get the password from standard input
Loïck Bonniot's avatar
Loïck Bonniot committed
88
func readPassword(ptr *string, needConfirm bool) error {
89 90 91 92 93 94 95 96 97

	if !terminal.IsTerminal(0) {
		fmt.Println("+------------------------- WARNING --------------------------+")
		fmt.Println("| This is not a UNIX terminal, your password will be visible |")
		fmt.Println("+------------------------- WARNING --------------------------+")
		readStringParam("Enter your passphrase", "", ptr)
		return nil
	}

98 99 100 101 102
	oldState, err := terminal.MakeRaw(0)
	if err != nil {
		return err
	}

Loïck Bonniot's avatar
Loïck Bonniot committed
103
	fmt.Print("Enter your passphrase: ")
104
	passphrase, err := terminal.ReadPassword(0)
Loïck Bonniot's avatar
Loïck Bonniot committed
105
	fmt.Println()
106 107 108 109
	if err != nil {
		return err
	}

Loïck Bonniot's avatar
Loïck Bonniot committed
110 111 112 113 114 115 116
	if needConfirm {
		fmt.Print("Confirm your passphrase: ")
		confirm, err := terminal.ReadPassword(0)
		fmt.Println()
		if err != nil {
			return err
		}
117

Loïck Bonniot's avatar
Loïck Bonniot committed
118 119 120
		if fmt.Sprintf("%s", passphrase) != fmt.Sprintf("%s", confirm) {
			return errors.New("Password do not match")
		}
121 122 123 124 125 126 127 128
	}

	*ptr = fmt.Sprintf("%s", passphrase)
	_ = terminal.Restore(0, oldState)

	return nil
}

Loïck Bonniot's avatar
Loïck Bonniot committed
129 130 131 132 133 134
func recapUser(mail, country, organization, unit string) {
	fmt.Println("Summary of the new user:")
	fmt.Println("  Common Name:", mail)
	fmt.Println("  Country:", country)
	fmt.Println("  Organization:", organization)
	fmt.Println("  Organizational unit:", unit)
135
}