Mercurial > go > multipass
diff mp-checkpassword/checkpassword.go @ 19:58fe867c9ecf
Reorganize commands to more standard go layout.
- Unify multipass user commands under one 'multipass' binary
- Move multipass checkpassword command to mp-checkpassword.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 01 Nov 2015 12:42:02 -0500 |
parents | cmds/multipass-checkpassword/checkpassword.go@342f63116bfd |
children |
line wrap: on
line diff
--- /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()) +}