1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-17 14:37:02 +00:00
Files
go-chasquid-smtp/cmd/chasquid-userdb/chasquid-userdb.go
Alberto Bertogli 92d16a0ca9 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.
2016-08-01 22:56:57 +01:00

97 lines
1.8 KiB
Go

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