Mercurial > go > multipass
annotate multipass-checkpassword.go @ 5:882f913f4e89
Replace '\0' with correct 0 value.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sat, 24 Oct 2015 23:19:20 -0400 |
| parents | b8545eea86b4 |
| children | 10613f0f4737 |
| rev | line source |
|---|---|
|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
1 package main |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
2 |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
3 import ( |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
4 "bufio" |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 "os" |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 "os/user" |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 "syscall" |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
9 "pfish.zone/go/multipass/file" |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 ) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 const ( |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 InternalError = 111 |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
14 Failed = 1 |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 ) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
16 |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
17 func main() { |
|
3
ec4ded022025
Fix checkpassword to actually build.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
18 infile := os.NewFile(3, "") |
|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 reader := bufio.NewReader(os.Stdin) |
|
5
882f913f4e89
Replace '\0' with correct 0 value.
Paul Fisher <paul@pfish.zone>
parents:
4
diff
changeset
|
20 username, err := reader.ReadString(0) |
|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
21 if err != nil { |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
22 os.Exit(InternalError) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
23 } |
|
1
faf4aad86fc9
Make checkpassword work; fix minor bug in add.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
24 username = username[:len(username)-1] |
|
5
882f913f4e89
Replace '\0' with correct 0 value.
Paul Fisher <paul@pfish.zone>
parents:
4
diff
changeset
|
25 pass, err := reader.ReadString(0) |
|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 if err != nil { |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 os.Exit(InternalError) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
28 } |
|
1
faf4aad86fc9
Make checkpassword work; fix minor bug in add.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
29 pass = pass[:len(pass)-1] |
|
faf4aad86fc9
Make checkpassword work; fix minor bug in add.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
30 infile.Close() |
|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 passfile, err := file.ForUser(username) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 if err != nil { |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 os.Exit(Failed) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
34 } |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
35 success, _ := passfile.Authenticate(pass) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 if !success { |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
37 os.Exit(Failed) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
38 } |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
39 user, err := user.Lookup(username) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
40 if err != nil { |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
41 os.Exit(Failed) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
42 } |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
43 os.Setenv("userdb_uid", user.Uid) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
44 os.Setenv("userdb_gid", user.Gid) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 os.Setenv("EXTRA", "userdb_uid userdb_gid") |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 environ := []string{ |
|
3
ec4ded022025
Fix checkpassword to actually build.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
47 "USER=" + user.Username, |
|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 "userdb_uid=" + user.Uid, |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
49 "userdb_gid=" + user.Gid, |
|
1
faf4aad86fc9
Make checkpassword work; fix minor bug in add.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
50 "HOME=" + user.HomeDir, |
|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 "EXTRA=userdb_uid userdb_gid", |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
52 } |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 syscall.Exec(os.Args[1], os.Args[1:], environ) |
|
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 } |
