add chpass command

This commit is contained in:
Ted Unangst 2019-10-12 19:21:29 -04:00
parent 438a8bbcc8
commit dd8400b66c
6 changed files with 70 additions and 14 deletions

View File

@ -2,6 +2,8 @@ changelog
-- next -- next
+ Add chpass command.
+ Improved honker management. + Improved honker management.
+ Better markdown output. + Better markdown output.

View File

@ -95,6 +95,11 @@ New users can be added with the
command. command.
This is discouraged. This is discouraged.
.Dl ./honk adduser .Dl ./honk adduser
.Pp
Passwords may be reset with the
.Ic chpass
command.
.Dl ./honk chpass username
.Ss MAINTENANCE .Ss MAINTENANCE
The database may grow large over time. The database may grow large over time.
The The

2
go.mod
View File

@ -7,5 +7,5 @@ require (
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 golang.org/x/net v0.0.0-20190620200207-3b0461eec859
humungus.tedunangst.com/r/go-sqlite3 v1.1.3 humungus.tedunangst.com/r/go-sqlite3 v1.1.3
humungus.tedunangst.com/r/webs v0.6.9 humungus.tedunangst.com/r/webs v0.6.10
) )

6
go.sum
View File

@ -21,7 +21,5 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
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=
humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI= humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI=
humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M=
humungus.tedunangst.com/r/webs v0.6.8 h1:0Xn6+iHZO8F4R3jhkql0qBR8nh5v9D59xG2vWQJzoa8= humungus.tedunangst.com/r/webs v0.6.10 h1:Qe4QW/7us8szAwL68zoIihfkaqsy/7Ys00bfdSPH5/g=
humungus.tedunangst.com/r/webs v0.6.8/go.mod h1:Ho+nmafD/aUWF7LnH+Yl2/b0ob7f2pCkXm4onteWvLE= humungus.tedunangst.com/r/webs v0.6.10/go.mod h1:Ho+nmafD/aUWF7LnH+Yl2/b0ob7f2pCkXm4onteWvLE=
humungus.tedunangst.com/r/webs v0.6.9 h1:bW2rBRZTBgov2yNUCvQ09dAK9oLfQ9A84QkMRLwaXZM=
humungus.tedunangst.com/r/webs v0.6.9/go.mod h1:Ho+nmafD/aUWF7LnH+Yl2/b0ob7f2pCkXm4onteWvLE=

View File

@ -174,6 +174,8 @@ func main() {
switch cmd { switch cmd {
case "adduser": case "adduser":
adduser() adduser()
case "chpass":
chpass()
case "cleanup": case "cleanup":
arg := "30" arg := "30"
if len(os.Args) > 2 { if len(os.Args) > 2 {

67
util.go
View File

@ -49,6 +49,7 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
_ "humungus.tedunangst.com/r/go-sqlite3" _ "humungus.tedunangst.com/r/go-sqlite3"
"humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/httpsig"
"humungus.tedunangst.com/r/webs/login"
) )
var savedassetparams = make(map[string]string) var savedassetparams = make(map[string]string)
@ -221,6 +222,62 @@ func adduser() {
os.Exit(0) os.Exit(0)
} }
func chpass() {
if len(os.Args) < 3 {
fmt.Printf("need a username\n")
os.Exit(1)
}
user, err := butwhatabout(os.Args[2])
if err != nil {
log.Fatal(err)
}
defer func() {
os.Exit(1)
}()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
<-c
C.termecho(1)
fmt.Printf("\n")
os.Exit(1)
}()
db := opendatabase()
login.Init(db)
r := bufio.NewReader(os.Stdin)
pass, err := askpassword(r)
if err != nil {
log.Print(err)
return
}
err = login.SetPassword(user.ID, pass)
if err != nil {
log.Print(err)
return
}
fmt.Printf("done\n")
os.Exit(0)
}
func askpassword(r *bufio.Reader) (string, error) {
C.termecho(0)
fmt.Printf("password: ")
pass, err := r.ReadString('\n')
C.termecho(1)
fmt.Printf("\n")
if err != nil {
return "", err
}
pass = pass[:len(pass)-1]
if len(pass) < 6 {
return "", fmt.Errorf("that's way too short")
}
return pass, nil
}
func createuser(db *sql.DB, r *bufio.Reader) error { func createuser(db *sql.DB, r *bufio.Reader) error {
fmt.Printf("username: ") fmt.Printf("username: ")
name, err := r.ReadString('\n') name, err := r.ReadString('\n')
@ -231,18 +288,10 @@ func createuser(db *sql.DB, r *bufio.Reader) error {
if len(name) < 1 { if len(name) < 1 {
return fmt.Errorf("that's way too short") return fmt.Errorf("that's way too short")
} }
C.termecho(0) pass, err := askpassword(r)
fmt.Printf("password: ")
pass, err := r.ReadString('\n')
C.termecho(1)
fmt.Printf("\n")
if err != nil { if err != nil {
return err return err
} }
pass = pass[:len(pass)-1]
if len(pass) < 6 {
return fmt.Errorf("that's way too short")
}
hash, err := bcrypt.GenerateFromPassword([]byte(pass), 12) hash, err := bcrypt.GenerateFromPassword([]byte(pass), 12)
if err != nil { if err != nil {
return err return err