1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-24 17:45:58 +01:00

Read password interactively.

This commit is contained in:
Juliusz Chroboczek 2024-10-25 15:54:54 +02:00
parent 340f726834
commit fdac5f5bde
3 changed files with 41 additions and 18 deletions

View file

@ -20,6 +20,7 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
"golang.org/x/term"
"github.com/jech/galene/group" "github.com/jech/galene/group"
) )
@ -240,14 +241,15 @@ func setUsage(cmd *flag.FlagSet, cmdname string, format string, args ...any) {
} }
func hashPasswordCmd(cmdname string, args []string) { func hashPasswordCmd(cmdname string, args []string) {
var algorithm string var password, algorithm string
var iterations, cost, length, saltlen int var iterations, cost, length, saltlen int
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, setUsage(cmd, cmdname,
"%v [option...] %v [option...] password...\n", "%v [option...] %v [option...]\n",
os.Args[0], cmdname, os.Args[0], cmdname,
) )
cmd.StringVar(&password, "password", "", "new `password`")
cmd.StringVar(&algorithm, "type", "pbkdf2", cmd.StringVar(&algorithm, "type", "pbkdf2",
"password `type`") "password `type`")
cmd.IntVar(&iterations, "iterations", 4096, cmd.IntVar(&iterations, "iterations", 4096,
@ -258,14 +260,23 @@ func hashPasswordCmd(cmdname string, args []string) {
cmd.IntVar(&saltlen, "salt", 8, "salt `length` (pbkdf2)") cmd.IntVar(&saltlen, "salt", 8, "salt `length` (pbkdf2)")
cmd.Parse(args) cmd.Parse(args)
if cmd.NArg() == 0 { if cmd.NArg() != 0 {
cmd.Usage() cmd.Usage()
os.Exit(1) os.Exit(1)
} }
for _, pw := range cmd.Args() { if algorithm != "wildcard" && password == "" {
fmt.Fprint(os.Stdin, "New password: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
if err != nil {
log.Fatalf("ReadPassword: %v", err)
}
password = string(pw)
}
p, err := makePassword( p, err := makePassword(
pw, algorithm, iterations, length, saltlen, cost, password, algorithm, iterations, length, saltlen, cost,
) )
if err != nil { if err != nil {
log.Fatalf("Make password: %v", err) log.Fatalf("Make password: %v", err)
@ -276,7 +287,6 @@ func hashPasswordCmd(cmdname string, args []string) {
log.Fatalf("Encode: %v", err) log.Fatalf("Encode: %v", err)
} }
} }
}
func setAuthorization(req *http.Request) { func setAuthorization(req *http.Request) {
if adminToken != "" { if adminToken != "" {
@ -382,17 +392,18 @@ func deleteValue(url string) error {
func setPasswordCmd(cmdname string, args []string) { func setPasswordCmd(cmdname string, args []string) {
var groupname, username string var groupname, username string
var wildcard bool var wildcard bool
var algorithm string var password, algorithm string
var iterations, cost, length, saltlen int var iterations, cost, length, saltlen int
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, setUsage(cmd, cmdname,
"%v [option...] %v [option...] password\n", "%v [option...] %v [option...]\n",
os.Args[0], cmdname, os.Args[0], cmdname,
) )
cmd.StringVar(&groupname, "group", "", "group `name`") cmd.StringVar(&groupname, "group", "", "group `name`")
cmd.StringVar(&username, "user", "", "user `name`") cmd.StringVar(&username, "user", "", "user `name`")
cmd.BoolVar(&wildcard, "wildcard", false, "set wildcard user's password") cmd.BoolVar(&wildcard, "wildcard", false, "set wildcard user's password")
cmd.StringVar(&password, "password", "", "new `password`")
cmd.StringVar(&algorithm, "type", "pbkdf2", cmd.StringVar(&algorithm, "type", "pbkdf2",
"password `type`") "password `type`")
cmd.IntVar(&iterations, "iterations", 4096, cmd.IntVar(&iterations, "iterations", 4096,
@ -403,7 +414,7 @@ func setPasswordCmd(cmdname string, args []string) {
cmd.IntVar(&saltlen, "salt", 8, "salt `length` (pbkdf2)") cmd.IntVar(&saltlen, "salt", 8, "salt `length` (pbkdf2)")
cmd.Parse(args) cmd.Parse(args)
if cmd.NArg() != 1 { if cmd.NArg() != 0 {
cmd.Usage() cmd.Usage()
os.Exit(1) os.Exit(1)
} }
@ -421,9 +432,18 @@ func setPasswordCmd(cmdname string, args []string) {
os.Exit(1) os.Exit(1)
} }
if algorithm != "wildcard" && password == "" {
fmt.Fprint(os.Stdin, "New password: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
if err != nil {
log.Fatalf("ReadPassword: %v", err)
}
password = string(pw)
}
pw, err := makePassword( pw, err := makePassword(
cmd.Args()[0], password, algorithm, iterations, length, saltlen, cost,
algorithm, iterations, length, saltlen, cost,
) )
if err != nil { if err != nil {
log.Fatalf("Make password: %v", err) log.Fatalf("Make password: %v", err)

1
go.mod
View file

@ -16,6 +16,7 @@ require (
github.com/pion/webrtc/v3 v3.3.1 github.com/pion/webrtc/v3 v3.3.1
golang.org/x/crypto v0.27.0 golang.org/x/crypto v0.27.0
golang.org/x/sys v0.25.0 golang.org/x/sys v0.25.0
golang.org/x/term v0.24.0
) )
require ( require (

2
go.sum
View file

@ -196,6 +196,8 @@ golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=