-hkexpasswd: now can add new users

-Removed unused disallowedCmdList (field 4) from CSV
This commit is contained in:
Russ Magee 2018-09-07 20:56:42 -07:00
parent 075ca7521c
commit 8bca54ed7b
1 changed files with 17 additions and 10 deletions

View File

@ -16,10 +16,9 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/user"
"github.com/jameskeane/bcrypt"
hkexsh "blitter.com/go/hkexsh" hkexsh "blitter.com/go/hkexsh"
"github.com/jameskeane/bcrypt"
) )
func main() { func main() {
@ -38,12 +37,13 @@ func main() {
os.Exit(1) os.Exit(1)
} }
u, err := user.Lookup(userName) //u, err := user.Lookup(userName)
if err != nil { //if err != nil {
log.Printf("Invalid user %s\n", userName) // log.Printf("Invalid user %s\n", userName)
log.Fatal(err) // log.Fatal(err)
} //}
uname = u.Username //uname = u.Username
uname = userName
fmt.Printf("New Password:") fmt.Printf("New Password:")
ab, err := hkexsh.ReadPassword(int(os.Stdin.Fd())) ab, err := hkexsh.ReadPassword(int(os.Stdin.Fd()))
@ -92,15 +92,18 @@ func main() {
r.Comma = ':' r.Comma = ':'
r.Comment = '#' r.Comment = '#'
r.FieldsPerRecord = 4 // username:salt:authCookie:disallowedCmdList (a,b,...) r.FieldsPerRecord = 3 // username:salt:authCookie [TODO:disallowedCmdList (a,b,...)]
records, err := r.ReadAll() records, err := r.ReadAll()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
recFound := false
for i, _ := range records { for i, _ := range records {
//fmt.Println(records[i]) //fmt.Println(records[i])
if records[i][0] == uname { if records[i][0] == uname {
recFound = true
records[i][1] = salt records[i][1] = salt
records[i][2] = hash records[i][2] = hash
} }
@ -109,6 +112,10 @@ func main() {
// records[i][0] = "#" + records[i][0] // records[i][0] = "#" + records[i][0]
//} //}
} }
if !recFound {
newRec := []string{uname, salt, hash}
records = append(records, newRec)
}
outFile, err := ioutil.TempFile("", "hkexsh-passwd") outFile, err := ioutil.TempFile("", "hkexsh-passwd")
if err != nil { if err != nil {
@ -117,7 +124,7 @@ func main() {
w := csv.NewWriter(outFile) w := csv.NewWriter(outFile)
w.Comma = ':' w.Comma = ':'
//w.FieldsPerRecord = 4 // username:salt:authCookie:disallowedCmdList (a,b,...) //w.FieldsPerRecord = 4 // username:salt:authCookie:disallowedCmdList (a,b,...)
w.Write([]string{"#username", "salt", "authCookie", "disallowedCmdList"}) w.Write([]string{"#username", "salt", "authCookie"/*, "disallowedCmdList"*/})
w.WriteAll(records) w.WriteAll(records)
if err = w.Error(); err != nil { if err = w.Error(); err != nil {
log.Fatal(err) log.Fatal(err)