1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-16 14:27:01 +00:00

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.
This commit is contained in:
Alberto Bertogli
2016-07-22 01:49:45 +01:00
parent 7eba9bb4f7
commit 92d16a0ca9
2 changed files with 98 additions and 1 deletions

3
.gitignore vendored
View File

@@ -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.

View File

@@ -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")
}