Mercurial > go > multipass
annotate file/file.go @ 10:1246b4b9028b
Add tool to remove passwords.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 25 Oct 2015 11:47:20 -0400 |
parents | e58bfc7fc207 |
children | da6c493cf08a |
rev | line source |
---|---|
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
1 // Package file handles I/O for single password files. |
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 // A password file contains multiple passwords for a single user. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
4 // It starts with a banner that indicates the version of the file, |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 // then has entries in the format specified by auth.Entry. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 package file |
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 import ( |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 "bufio" |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 "errors" |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 "os" |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 "os/user" |
10 | 14 "path" |
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 "syscall" |
10 | 16 "time" |
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
17 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
18 "golang.org/x/sys/unix" |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 "pfish.zone/go/multipass/auth" |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 ) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
21 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
22 const ( |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
23 // the Banner acts as a version indicator |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
24 Banner = "# Multipass v0.1 password file" |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 ) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 var ( |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
28 // Raised when |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
29 ErrorBadFile = errors.New("multipass/file: Invalid file format") |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 ) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 type ShadowFile struct { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 name string |
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 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 // ForUser gets the given user's ShadowFile. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
37 func ForUser(username string) (*ShadowFile, error) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
38 u, err := user.Lookup(username) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
39 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
40 return nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
41 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
42 return New(path.Join(u.HomeDir, MultipassFile)), nil |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
43 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
44 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 // ForMe gets the current user's ShadowFile. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 func ForMe() (*ShadowFile, error) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
47 u, err := user.Current() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
49 return nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 return New(path.Join(u.HomeDir, MultipassFile)), nil |
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 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 // New creates a ShadowFile for reading at the given path. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 // If a file needs to be created, uses the given GID to create it. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 func New(name string) *ShadowFile { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 f := new(ShadowFile) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 f.name = name |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
59 return f |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
60 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 func (f *ShadowFile) newFilename() string { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 return f.name + ".new" |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 func (f *ShadowFile) open() (*os.File, *bufio.Scanner, error) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 file, err := os.Open(f.name) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 return nil, nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
71 scanner := bufio.NewScanner(file) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
72 if !scanner.Scan() { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
73 file.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 return nil, nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 if scanner.Text() != Banner { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
77 file.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
78 return nil, nil, ErrorBadFile |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
79 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
80 return file, scanner, nil |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
81 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
82 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
83 func (f *ShadowFile) Authenticate(password string) (bool, error) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
84 file, scanner, err := f.open() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
85 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
86 return false, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
87 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
88 defer file.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
89 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
90 for scanner.Scan() { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
91 entry, err := auth.EntryFromShadow(scanner.Text()) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
92 // Skip invalid lines. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
93 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
94 continue |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 if entry.Authenticate(password) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 return true, nil |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 return false, nil |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
101 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
102 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
103 func (f *ShadowFile) Add(entry *auth.Entry) error { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
104 handle, err := f.openWrite() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
105 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
106 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
107 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 err = handle.write(entry) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
110 handle.bail() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
111 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
113 for handle.next() { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
114 if entry, err := handle.entry(); err == nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
115 // If we get an invalid entry, just skip it. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
116 if err := handle.write(entry); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
117 handle.bail() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
118 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
119 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
120 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
121 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
122 return handle.finalize() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
123 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
124 |
10 | 125 func (f *ShadowFile) AllEntries() ([]*auth.Entry, error) { |
126 file, scanner, err := f.open() | |
127 if err != nil { | |
128 return nil, err | |
129 } | |
130 defer file.Close() | |
131 | |
132 var entries []*auth.Entry | |
133 | |
134 for scanner.Scan() { | |
135 entry, err := auth.EntryFromShadow(scanner.Text()) | |
136 // Skip invalid lines. | |
137 if err != nil { | |
138 continue | |
139 } | |
140 entries = append(entries, entry) | |
141 } | |
142 return entries, nil | |
143 } | |
144 | |
145 func (f *ShadowFile) Remove(id uint64) error { | |
146 handle, err := f.openWrite() | |
147 if err != nil { | |
148 return err | |
149 } | |
150 for handle.next() { | |
151 if entry, err := handle.entry(); err == nil { | |
152 // If we get an invalid entry, just skip it. | |
153 if entry.ID() != id { | |
154 if err := handle.write(entry); err != nil { | |
155 handle.bail() | |
156 return err | |
157 } | |
158 } | |
159 } | |
160 } | |
161 return handle.finalize() | |
162 } | |
163 | |
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
164 func (f *ShadowFile) openWrite() (*writeHandle, error) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
165 return openWriteHandle(f.newFilename(), f.name) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
166 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
167 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
168 type writeHandle struct { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
169 // We write to a file handle pointing to tempName. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
170 tempName string |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
171 tempFile *os.File |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
172 writer *bufio.Writer |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
173 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
174 // It will eventually move to fileName, which is our source file. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
175 fileName string |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
176 // If inFile is nil, we're creating a new multipass file. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
177 inFile *os.File |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
178 scanner *bufio.Scanner |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
179 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
180 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
181 func openWriteHandle(tempName, fileName string) (*writeHandle, error) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
182 h := new(writeHandle) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
183 h.tempName = tempName |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
184 h.fileName = fileName |
10 | 185 // Open the output file, but only if it doesn't exist. |
9
e58bfc7fc207
Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
186 // This prevents race conditions. |
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
187 oldUmask := unix.Umask(077) |
10 | 188 var tempFile *os.File |
189 delay := time.Nanosecond | |
190 for tempFile == nil { | |
191 tempTempFile, err := os.OpenFile(tempName, os.O_CREATE|os.O_EXCL|os.O_WRONLY|os.O_SYNC, 0600) | |
192 tempFile = tempTempFile | |
193 if err != nil { | |
194 perr := err.(*os.PathError) | |
195 errno, ok := perr.Err.(syscall.Errno) | |
196 if !ok { | |
197 return nil, err | |
198 } | |
199 if errno != syscall.EEXIST || delay > time.Minute { | |
200 return nil, err | |
201 } | |
202 time.Sleep(delay) | |
203 delay *= 2 | |
204 } | |
205 } | |
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
206 unix.Umask(oldUmask) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
207 h.tempFile = tempFile |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
208 // Open the input file. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
209 inFile, err := os.Open(fileName) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
210 if err == nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
211 // Prepare to read in the input file, if it exists. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
212 h.inFile = inFile |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
213 h.scanner = bufio.NewScanner(h.inFile) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
214 if !h.scanner.Scan() { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
215 return nil, ErrorBadFile |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
216 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
217 if h.scanner.Text() != Banner { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
218 return nil, ErrorBadFile |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
219 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
220 // Change the owner and group of the new file to that of the old. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
221 inStat, err := h.inFile.Stat() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
222 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
223 h.bail() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
224 return nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
225 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
226 inStatUnix := inStat.Sys().(*syscall.Stat_t) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
227 if err := h.tempFile.Chown(int(inStatUnix.Uid), int(inStatUnix.Gid)); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
228 h.bail() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
229 return nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
230 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
231 if err := h.tempFile.Chmod(inStat.Mode()); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
232 h.bail() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
233 return nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
234 } |
9
e58bfc7fc207
Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
235 } else { |
e58bfc7fc207
Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
236 // TODO(pfish): Restrict ACL to only multipass authenticators. |
e58bfc7fc207
Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
237 if err := h.tempFile.Chmod(0644); err != nil { |
e58bfc7fc207
Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
238 h.bail() |
e58bfc7fc207
Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
239 return nil, err |
e58bfc7fc207
Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
240 } |
0
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
241 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
242 h.writer = bufio.NewWriter(h.tempFile) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
243 if _, err := h.writer.WriteString(Banner + "\n"); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
244 return nil, err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
245 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
246 return h, nil |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
247 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
248 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
249 func (h *writeHandle) bail() { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
250 h.tempFile.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
251 h.inFile.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
252 os.Remove(h.tempName) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
253 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
254 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
255 func (h *writeHandle) next() bool { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
256 // If the scanner is nil, then we have no input file and therefore no next. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
257 return h.scanner != nil && h.scanner.Scan() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
258 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
259 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
260 func (h *writeHandle) entry() (*auth.Entry, error) { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
261 return auth.EntryFromShadow(h.scanner.Text()) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
262 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
263 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
264 func (h *writeHandle) write(entry *auth.Entry) error { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
265 if _, err := h.writer.WriteString(entry.Encode()); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
266 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
267 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
268 _, err := h.writer.WriteString("\n") |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
269 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
270 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
271 |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
272 func (h *writeHandle) finalize() error { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
273 h.inFile.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
274 // Make absolutely completely sure we're synced. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
275 if err := h.writer.Flush(); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
276 h.tempFile.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
277 os.Remove(h.tempName) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
278 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
279 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
280 if err := h.tempFile.Sync(); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
281 h.tempFile.Close() |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
282 os.Remove(h.tempName) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
283 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
284 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
285 // Close the file. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
286 if err := h.tempFile.Close(); err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
287 os.Remove(h.tempName) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
288 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
289 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
290 // And atomically write it over the new one. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
291 err := os.Rename(h.tempName, h.fileName) |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
292 if err != nil { |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
293 return err |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
294 } |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
295 // If we get here, everything succeeded, and only in this case |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
296 // will the multipass shadow file have been updated. |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
297 return nil |
c18bc7b9d1d9
Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
298 } |