2020-03-11 18:01:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-20 21:09:11 +00:00
|
|
|
"bufio"
|
2021-10-18 17:15:15 +00:00
|
|
|
"context"
|
2020-03-11 18:01:03 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-04-19 12:11:25 +00:00
|
|
|
"io"
|
2020-03-11 18:01:03 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2022-05-09 10:34:43 +00:00
|
|
|
|
|
|
|
"git.sr.ht/~emersion/soju/config"
|
|
|
|
"git.sr.ht/~emersion/soju/database"
|
2020-03-11 18:01:03 +00:00
|
|
|
)
|
|
|
|
|
2023-01-20 14:49:57 +00:00
|
|
|
const usage = `usage: sojudb [-config path] <action> [options...]
|
2020-03-11 18:01:03 +00:00
|
|
|
|
2020-06-06 23:32:50 +00:00
|
|
|
create-user <username> [-admin] Create a new user
|
|
|
|
change-password <username> Change password for a user
|
|
|
|
help Show this help message
|
2020-03-11 18:01:03 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprintf(flag.CommandLine.Output(), usage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var configPath string
|
2023-02-06 12:16:55 +00:00
|
|
|
flag.StringVar(&configPath, "config", config.DefaultPath, "path to configuration file")
|
2020-03-11 18:01:03 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
var cfg *config.Server
|
|
|
|
if configPath != "" {
|
|
|
|
var err error
|
|
|
|
cfg, err = config.Load(configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to load config file: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cfg = config.Defaults()
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:50:34 +00:00
|
|
|
db, err := database.Open(cfg.DB.Driver, cfg.DB.Source)
|
2020-03-11 18:01:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to open database: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-11-17 10:54:28 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-03-11 18:01:03 +00:00
|
|
|
switch cmd := flag.Arg(0); cmd {
|
|
|
|
case "create-user":
|
|
|
|
username := flag.Arg(1)
|
|
|
|
if username == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-06-06 23:32:50 +00:00
|
|
|
fs := flag.NewFlagSet("", flag.ExitOnError)
|
|
|
|
admin := fs.Bool("admin", false, "make the new user admin")
|
|
|
|
fs.Parse(flag.Args()[2:])
|
|
|
|
|
2020-04-20 21:09:11 +00:00
|
|
|
password, err := readPassword()
|
2020-03-11 18:01:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to read password: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to hash password: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
user := database.User{
|
2020-03-11 18:01:03 +00:00
|
|
|
Username: username,
|
|
|
|
Password: string(hashed),
|
2020-06-06 23:32:50 +00:00
|
|
|
Admin: *admin,
|
2023-01-26 17:33:55 +00:00
|
|
|
Enabled: true,
|
2020-03-11 18:01:03 +00:00
|
|
|
}
|
2021-11-17 10:54:28 +00:00
|
|
|
if err := db.StoreUser(ctx, &user); err != nil {
|
2020-03-11 18:01:03 +00:00
|
|
|
log.Fatalf("failed to create user: %v", err)
|
|
|
|
}
|
2020-04-08 10:59:50 +00:00
|
|
|
case "change-password":
|
|
|
|
username := flag.Arg(1)
|
|
|
|
if username == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-11-17 10:54:28 +00:00
|
|
|
user, err := db.GetUser(ctx, username)
|
2020-11-25 20:08:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get user: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:09:11 +00:00
|
|
|
password, err := readPassword()
|
2020-04-08 10:59:50 +00:00
|
|
|
if err != nil {
|
2020-04-20 21:09:11 +00:00
|
|
|
log.Fatalf("failed to read password: %v", err)
|
2020-04-08 10:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to hash password: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 08:21:49 +00:00
|
|
|
user.Password = string(hashed)
|
2021-11-17 10:54:28 +00:00
|
|
|
if err := db.StoreUser(ctx, user); err != nil {
|
2020-04-08 10:59:50 +00:00
|
|
|
log.Fatalf("failed to update password: %v", err)
|
|
|
|
}
|
2020-03-11 18:01:03 +00:00
|
|
|
default:
|
|
|
|
flag.Usage()
|
|
|
|
if cmd != "help" {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 21:09:11 +00:00
|
|
|
|
|
|
|
func readPassword() ([]byte, error) {
|
|
|
|
var password []byte
|
|
|
|
var err error
|
|
|
|
fd := int(os.Stdin.Fd())
|
|
|
|
|
|
|
|
if terminal.IsTerminal(fd) {
|
|
|
|
fmt.Printf("Password: ")
|
|
|
|
password, err = terminal.ReadPassword(int(os.Stdin.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "Warning: Reading password from stdin.\n")
|
2021-04-19 12:11:25 +00:00
|
|
|
// TODO: the buffering messes up repeated calls to readPassword
|
2020-04-20 21:09:11 +00:00
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
2020-06-06 23:20:56 +00:00
|
|
|
if !scanner.Scan() {
|
|
|
|
if err := scanner.Err(); err != nil {
|
2021-04-19 12:11:25 +00:00
|
|
|
return nil, err
|
2020-06-06 23:20:56 +00:00
|
|
|
}
|
2021-04-19 12:11:25 +00:00
|
|
|
return nil, io.ErrUnexpectedEOF
|
2020-06-06 23:20:56 +00:00
|
|
|
}
|
2020-04-20 21:09:11 +00:00
|
|
|
password = scanner.Bytes()
|
|
|
|
|
|
|
|
if len(password) == 0 {
|
|
|
|
return nil, fmt.Errorf("zero length password")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return password, nil
|
|
|
|
}
|