annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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, "")
6
10613f0f4737 Read from fd3, not stdin.
Paul Fisher <paul@pfish.zone>
parents: 5
diff changeset
19 reader := bufio.NewReader(infile)
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 }
8
4db389f948c9 Preserve the entire environment when password checking.
Paul Fisher <paul@pfish.zone>
parents: 6
diff changeset
43 os.Setenv("USER", user.Username)
4db389f948c9 Preserve the entire environment when password checking.
Paul Fisher <paul@pfish.zone>
parents: 6
diff changeset
44 os.Setenv("HOME", user.HomeDir)
0
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 os.Setenv("userdb_uid", user.Uid)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 os.Setenv("userdb_gid", user.Gid)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 os.Setenv("EXTRA", "userdb_uid userdb_gid")
8
4db389f948c9 Preserve the entire environment when password checking.
Paul Fisher <paul@pfish.zone>
parents: 6
diff changeset
48 syscall.Exec(os.Args[1], os.Args[1:], os.Environ())
0
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 }