git » chasquid » commit 92d16a0

Add a new "chasquid-userdb" command line tool

author Alberto Bertogli
2016-07-22 00:49:45 UTC
committer Alberto Bertogli
2016-08-01 21:56:57 UTC
parent 7eba9bb4f74d863dbfbb4799e4800b4010bb777e

Add a new "chasquid-userdb" command line tool

This patch adds a "chasquid-userdb" command line tool to check and add users
to chasquid's userdb files.

It's not pretty or very friendly, as it's meant to be used for testing for
now.

.gitignore +2 -1
cmd/chasquid-userdb/chasquid-userdb.go +96 -0

diff --git a/.gitignore b/.gitignore
index c1345c2..b6643a6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,8 +7,9 @@
 # excluded.
 !.gitignore
 
-# The binary.
+# The binaries.
 chasquid
+chasquid-userdb
 
 # Exclude any .pem files, to prevent accidentally including test keys and
 # certificates.
diff --git a/cmd/chasquid-userdb/chasquid-userdb.go b/cmd/chasquid-userdb/chasquid-userdb.go
new file mode 100644
index 0000000..cb12424
--- /dev/null
+++ b/cmd/chasquid-userdb/chasquid-userdb.go
@@ -0,0 +1,96 @@
+package main
+
+import (
+	"bytes"
+	"flag"
+	"fmt"
+	"os"
+	"syscall"
+
+	"golang.org/x/crypto/ssh/terminal"
+
+	"blitiri.com.ar/go/chasquid/internal/userdb"
+)
+
+var (
+	dbFname  = flag.String("database", "", "database file")
+	adduser  = flag.String("add_user", "", "user to add")
+	password = flag.String("password", "",
+		"password for the user to add (will prompt if missing)")
+	disableChecks = flag.Bool("dangerously_disable_checks", false,
+		"disable security checks - DANGEROUS, use for testing only")
+)
+
+func main() {
+	flag.Parse()
+
+	if *dbFname == "" {
+		fmt.Printf("database name missing, forgot --database?\n")
+		os.Exit(1)
+	}
+
+	db, ws, err := userdb.Load(*dbFname)
+	if err != nil {
+		fmt.Printf("error loading database: %v\n", err)
+		os.Exit(1)
+	}
+
+	for _, w := range ws {
+		fmt.Printf("warning: %v\n", w)
+	}
+
+	if *adduser == "" {
+		fmt.Printf("database loaded\n")
+		if len(ws) == 0 {
+			os.Exit(0)
+		} else {
+			os.Exit(1)
+		}
+	}
+
+	if *password == "" {
+		fmt.Printf("Password: ")
+		p1, err := terminal.ReadPassword(syscall.Stdin)
+		fmt.Printf("\n")
+		if err != nil {
+			fmt.Printf("error reading password: %v\n", err)
+			os.Exit(1)
+		}
+
+		fmt.Printf("Confirm password: ")
+		p2, err := terminal.ReadPassword(syscall.Stdin)
+		fmt.Printf("\n")
+		if err != nil {
+			fmt.Printf("error reading password: %v\n", err)
+			os.Exit(1)
+		}
+
+		if !bytes.Equal(p1, p2) {
+			fmt.Printf("passwords don't match\n")
+			os.Exit(1)
+		}
+
+		*password = string(p1)
+	}
+
+	if !*disableChecks {
+		if len(*password) < 8 {
+			fmt.Printf("password is too short\n")
+			os.Exit(1)
+		}
+	}
+
+	err = db.AddUser(*adduser, *password)
+	if err != nil {
+		fmt.Printf("error adding user: %v\n", err)
+		os.Exit(1)
+	}
+
+	err = db.Write()
+	if err != nil {
+		fmt.Printf("error writing database: %v\n", err)
+		os.Exit(1)
+	}
+
+	fmt.Printf("added user\n")
+}