# HG changeset patch # User Paul Fisher # Date 1446399722 18000 # Node ID 58fe867c9ecf67af30677b04e953b4441a5929fc # Parent 00d30c67b56d7bbf160534c688ec4cf5f2897f55 Reorganize commands to more standard go layout. - Unify multipass user commands under one 'multipass' binary - Move multipass checkpassword command to mp-checkpassword. diff -r 00d30c67b56d -r 58fe867c9ecf add.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/add.go Sun Nov 01 12:42:02 2015 -0500 @@ -0,0 +1,35 @@ +// multipass-add allows a user to add an entry to their multipass database. + +package main + +import ( + "bufio" + "fmt" + "os" + + "pfish.zone/go/multipass/file" +) + +func add(passfile *file.ShadowFile, input *bufio.Reader) int { + reader := bufio.NewReader(os.Stdin) + fmt.Print("Describe password: ") + text, err := reader.ReadString('\n') + if err != nil { + fmt.Println(err.Error()) + return 1 + } + text = text[:len(text)-1] + entry, password, err := file.NewEntry(text) + if err != nil { + fmt.Println(err.Error()) + return 1 + } + err = passfile.Add(entry) + if err != nil { + fmt.Println("Couldn't create a password:") + fmt.Println(err.Error()) + return 1 + } + fmt.Printf("New password: %s\n", password) + return 0 +} diff -r 00d30c67b56d -r 58fe867c9ecf cmds/multipass-add/add.go --- a/cmds/multipass-add/add.go Sun Nov 01 12:16:51 2015 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -// multipass-add allows a user to add an entry to their multipass database. - -package main - -import ( - "bufio" - "fmt" - "os" - - "pfish.zone/go/multipass/file" -) - -func main() { - passfile, err := file.ForMe() - if err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - reader := bufio.NewReader(os.Stdin) - fmt.Print("Describe password: ") - text, err := reader.ReadString('\n') - if err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - text = text[:len(text)-1] - entry, password, err := file.NewEntry(text) - if err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - err = passfile.Add(entry) - if err != nil { - fmt.Println("Couldn't create a password:") - fmt.Println(err.Error()) - os.Exit(1) - } - fmt.Printf("New password: %s\n", password) -} diff -r 00d30c67b56d -r 58fe867c9ecf cmds/multipass-checkpassword/checkpassword.go --- a/cmds/multipass-checkpassword/checkpassword.go Sun Nov 01 12:16:51 2015 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -package main - -import ( - "bufio" - "os" - "os/user" - "syscall" - - "pfish.zone/go/multipass/file" -) - -const ( - InternalError = 111 - Failed = 1 -) - -func main() { - infile := os.NewFile(3, "") - reader := bufio.NewReader(infile) - username, err := reader.ReadString(0) - if err != nil { - os.Exit(InternalError) - } - username = username[:len(username)-1] - pass, err := reader.ReadString(0) - if err != nil { - os.Exit(InternalError) - } - pass = pass[:len(pass)-1] - infile.Close() - passfile, err := file.ForUser(username) - if err != nil { - os.Exit(Failed) - } - success, _ := passfile.Authenticate(pass) - if !success { - os.Exit(Failed) - } - user, err := user.Lookup(username) - if err != nil { - os.Exit(Failed) - } - os.Setenv("USER", user.Username) - os.Setenv("HOME", user.HomeDir) - os.Setenv("userdb_uid", user.Uid) - os.Setenv("userdb_gid", user.Gid) - os.Setenv("EXTRA", "userdb_uid userdb_gid") - syscall.Exec(os.Args[1], os.Args[1:], os.Environ()) -} diff -r 00d30c67b56d -r 58fe867c9ecf cmds/multipass-remove/remove.go --- a/cmds/multipass-remove/remove.go Sun Nov 01 12:16:51 2015 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -// multipass-remove allows a user to remove an entry from their multipass database. - -package main - -import ( - "bufio" - "fmt" - "os" - "strconv" - - "pfish.zone/go/multipass/file" -) - -func main() { - passfile, err := file.ForMe() - if err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - fmt.Println("Choose a password to remove:") - reader := bufio.NewReader(os.Stdin) - entries, err := passfile.AllEntries() - for i, entry := range entries { - fmt.Printf("%2d: %s\n", i+1, entry.Description()) - } - fmt.Print("Enter the number to remove: ") - text, err := reader.ReadString('\n') - if err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - picked, err := strconv.Atoi(text[:len(text)-1]) - if err != nil { - fmt.Println("Not a valid number") - os.Exit(1) - } - picked -= 1 - if picked < 0 || len(entries) <= picked { - fmt.Println("Not a valid selection") - os.Exit(1) - } - - if err := passfile.Remove(entries[picked].ID()); err != nil { - fmt.Printf("Couldn't remove password: %s\n", err.Error()) - os.Exit(1) - } - fmt.Println("Removed password entry.") -} diff -r 00d30c67b56d -r 58fe867c9ecf mp-checkpassword/checkpassword.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mp-checkpassword/checkpassword.go Sun Nov 01 12:42:02 2015 -0500 @@ -0,0 +1,49 @@ +package main + +import ( + "bufio" + "os" + "os/user" + "syscall" + + "pfish.zone/go/multipass/file" +) + +const ( + InternalError = 111 + Failed = 1 +) + +func main() { + infile := os.NewFile(3, "") + reader := bufio.NewReader(infile) + username, err := reader.ReadString(0) + if err != nil { + os.Exit(InternalError) + } + username = username[:len(username)-1] + pass, err := reader.ReadString(0) + if err != nil { + os.Exit(InternalError) + } + pass = pass[:len(pass)-1] + infile.Close() + passfile, err := file.ForUser(username) + if err != nil { + os.Exit(Failed) + } + success, _ := passfile.Authenticate(pass) + if !success { + os.Exit(Failed) + } + user, err := user.Lookup(username) + if err != nil { + os.Exit(Failed) + } + os.Setenv("USER", user.Username) + os.Setenv("HOME", user.HomeDir) + os.Setenv("userdb_uid", user.Uid) + os.Setenv("userdb_gid", user.Gid) + os.Setenv("EXTRA", "userdb_uid userdb_gid") + syscall.Exec(os.Args[1], os.Args[1:], os.Environ()) +} diff -r 00d30c67b56d -r 58fe867c9ecf multipass.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/multipass.go Sun Nov 01 12:42:02 2015 -0500 @@ -0,0 +1,63 @@ +// multipass is a command for users to manage their multipass password database. +// +// See the Usage message for more details. +package main + +import ( + "bufio" + "fmt" + "os" + + "pfish.zone/go/multipass/file" +) + +const Usage = ` +multipass is a command for users to manage their multipass password databases. + +Usage: + multipass command + +The commands are: + add + remove + +multipass add: + adds a new password to the user's multipass database. + Prompts the user for a description, and adds it. + +multipass remove: + removes a password from the user's multipass database. + Lists the user's password, and removes the one they choose. +` + +var commands map[string]func(*file.ShadowFile, *bufio.Reader) int + +func init() { + commands = make(map[string]func(*file.ShadowFile, *bufio.Reader) int) + commands["add"] = add + commands["remove"] = remove +} + +func main() { + if len(os.Args) != 2 { + usage() + } + + cmd, ok := commands[os.Args[1]] + if !ok { + usage() + } + + passfile, err := file.ForMe() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + input := bufio.NewReader(os.Stdin) + os.Exit(cmd(passfile, input)) +} + +func usage() { + fmt.Print(Usage) + os.Exit(2) +} diff -r 00d30c67b56d -r 58fe867c9ecf remove.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/remove.go Sun Nov 01 12:42:02 2015 -0500 @@ -0,0 +1,44 @@ +// multipass-remove allows a user to remove an entry from their multipass database. + +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + + "pfish.zone/go/multipass/file" +) + +func remove(passfile *file.ShadowFile, input *bufio.Reader) int { + fmt.Println("Choose a password to remove:") + reader := bufio.NewReader(os.Stdin) + entries, err := passfile.AllEntries() + for i, entry := range entries { + fmt.Printf("%2d: %s\n", i+1, entry.Description()) + } + fmt.Print("Enter the number to remove: ") + text, err := reader.ReadString('\n') + if err != nil { + fmt.Println(err.Error()) + return 1 + } + picked, err := strconv.Atoi(text[:len(text)-1]) + if err != nil { + fmt.Println("Not a valid number") + return 1 + } + picked -= 1 + if picked < 0 || len(entries) <= picked { + fmt.Println("Not a valid selection") + return 1 + } + + if err := passfile.Remove(entries[picked].ID()); err != nil { + fmt.Printf("Couldn't remove password: %s\n", err.Error()) + return 1 + } + fmt.Println("Removed password entry.") + return 0 +}