comparison multipass-checkpassword.go @ 1:faf4aad86fc9

Make checkpassword work; fix minor bug in add.
author Paul Fisher <paul@pfish.zone>
date Sat, 24 Oct 2015 21:48:38 -0400
parents c18bc7b9d1d9
children ec4ded022025
comparison
equal deleted inserted replaced
0:c18bc7b9d1d9 1:faf4aad86fc9
1 package main 1 package main
2 2
3 import ( 3 import (
4 "bufio" 4 "bufio"
5 "fmt"
5 "os" 6 "os"
6 "os/user" 7 "os/user"
7 "syscall" 8 "syscall"
8 9
9 "pfish.zone/go/multipass/file" 10 "pfish.zone/go/multipass/file"
13 InternalError = 111 14 InternalError = 111
14 Failed = 1 15 Failed = 1
15 ) 16 )
16 17
17 func main() { 18 func main() {
18 //infile = os.NewFile(3, "") 19 infile = os.NewFile(3, "")
19 reader := bufio.NewReader(os.Stdin) 20 reader := bufio.NewReader(os.Stdin)
20 username, err := reader.ReadString('\n') 21 username, err := reader.ReadString('\n')
21 if err != nil { 22 if err != nil {
22 os.Exit(InternalError) 23 os.Exit(InternalError)
23 } 24 }
25 username = username[:len(username)-1]
24 pass, err := reader.ReadString('\n') 26 pass, err := reader.ReadString('\n')
25 if err != nil { 27 if err != nil {
26 os.Exit(InternalError) 28 os.Exit(InternalError)
27 } 29 }
30 pass = pass[:len(pass)-1]
31 infile.Close()
28 passfile, err := file.ForUser(username) 32 passfile, err := file.ForUser(username)
29 if err != nil { 33 if err != nil {
30 os.Exit(Failed) 34 os.Exit(Failed)
31 } 35 }
32 success, _ := passfile.Authenticate(pass) 36 success, _ := passfile.Authenticate(pass)
33 if !success { 37 if !success {
34 os.Exit(Failed) 38 os.Exit(Failed)
35 } 39 }
36 user, err := user.Lookup(username) 40 user, err := user.Lookup(username)
37 err = os.Setenv("USER", user.HomeDir)
38 if err != nil { 41 if err != nil {
39 os.Exit(Failed) 42 os.Exit(Failed)
40 } 43 }
41 os.Setenv("userdb_uid", user.Uid) 44 os.Setenv("userdb_uid", user.Uid)
42 os.Setenv("userdb_gid", user.Gid) 45 os.Setenv("userdb_gid", user.Gid)
43 os.Setenv("EXTRA", "userdb_uid userdb_gid") 46 os.Setenv("EXTRA", "userdb_uid userdb_gid")
44 environ := []string{ 47 environ := []string{
48 "USER=" + user.Username
45 "userdb_uid=" + user.Uid, 49 "userdb_uid=" + user.Uid,
46 "userdb_gid=" + user.Gid, 50 "userdb_gid=" + user.Gid,
51 "HOME=" + user.HomeDir,
47 "EXTRA=userdb_uid userdb_gid", 52 "EXTRA=userdb_uid userdb_gid",
48 } 53 }
49 syscall.Exec(os.Args[1], os.Args[1:], environ) 54 syscall.Exec(os.Args[1], os.Args[1:], environ)
50 } 55 }