annotate file/file.go @ 13:da6c493cf08a

Move Unix-specific user auth files into unix_users.go.
author Paul Fisher <paul@pfish.zone>
date Thu, 29 Oct 2015 21:29:27 -0400
parents 1246b4b9028b
children 9b4ec6b5c23e
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 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 "syscall"
10
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
14 "time"
0
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 "golang.org/x/sys/unix"
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 "pfish.zone/go/multipass/auth"
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 )
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 const (
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 // the Banner acts as a version indicator
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 Banner = "# Multipass v0.1 password file"
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 )
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 var (
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 // Raised when
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 ErrorBadFile = errors.New("multipass/file: Invalid file format")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 )
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 type ShadowFile struct {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 name string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 // 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
35 // 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
36 func New(name string) *ShadowFile {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 f := new(ShadowFile)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 f.name = name
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 return f
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 }
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 func (f *ShadowFile) newFilename() string {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 return f.name + ".new"
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
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 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
47 file, err := os.Open(f.name)
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, 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 scanner := bufio.NewScanner(file)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 if !scanner.Scan() {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 file.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 return nil, nil, err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
56 if scanner.Text() != Banner {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 file.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 return nil, nil, ErrorBadFile
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 return file, scanner, nil
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
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 func (f *ShadowFile) Authenticate(password string) (bool, error) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 file, scanner, err := f.open()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 return false, err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 defer file.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 for scanner.Scan() {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 entry, err := auth.EntryFromShadow(scanner.Text())
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 // Skip invalid lines.
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 continue
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 entry.Authenticate(password) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 return true, nil
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
78 }
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 false, 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) Add(entry *auth.Entry) error {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 handle, err := f.openWrite()
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 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 err = handle.write(entry)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 handle.bail()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
91 return err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 for handle.next() {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 if entry, err := handle.entry(); err == nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
95 // 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
96 if err := handle.write(entry); err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
97 handle.bail()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
98 return err
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 }
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 return handle.finalize()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
103 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
104
10
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
105 func (f *ShadowFile) AllEntries() ([]*auth.Entry, error) {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
106 file, scanner, err := f.open()
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
107 if err != nil {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
108 return nil, err
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
109 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
110 defer file.Close()
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
111
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
112 var entries []*auth.Entry
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
113
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
114 for scanner.Scan() {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
115 entry, err := auth.EntryFromShadow(scanner.Text())
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
116 // Skip invalid lines.
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
117 if err != nil {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
118 continue
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
119 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
120 entries = append(entries, entry)
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
121 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
122 return entries, nil
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
123 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
124
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
125 func (f *ShadowFile) Remove(id uint64) error {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
126 handle, err := f.openWrite()
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
127 if err != nil {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
128 return err
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
129 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
130 for handle.next() {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
131 if entry, err := handle.entry(); err == nil {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
132 // If we get an invalid entry, just skip it.
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
133 if entry.ID() != id {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
134 if err := handle.write(entry); err != nil {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
135 handle.bail()
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
136 return err
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
137 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
138 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
139 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
140 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
141 return handle.finalize()
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
142 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
143
0
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
144 func (f *ShadowFile) openWrite() (*writeHandle, error) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
145 return openWriteHandle(f.newFilename(), f.name)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
146 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
147
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
148 type writeHandle struct {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
149 // 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
150 tempName string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
151 tempFile *os.File
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
152 writer *bufio.Writer
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
153
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
154 // 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
155 fileName string
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
156 // 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
157 inFile *os.File
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
158 scanner *bufio.Scanner
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
159 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
160
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
161 func openWriteHandle(tempName, fileName string) (*writeHandle, error) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
162 h := new(writeHandle)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
163 h.tempName = tempName
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
164 h.fileName = fileName
10
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
165 // 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
166 // This prevents race conditions.
0
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
167 oldUmask := unix.Umask(077)
10
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
168 var tempFile *os.File
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
169 delay := time.Nanosecond
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
170 for tempFile == nil {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
171 tempTempFile, err := os.OpenFile(tempName, os.O_CREATE|os.O_EXCL|os.O_WRONLY|os.O_SYNC, 0600)
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
172 tempFile = tempTempFile
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
173 if err != nil {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
174 perr := err.(*os.PathError)
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
175 errno, ok := perr.Err.(syscall.Errno)
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
176 if !ok {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
177 return nil, err
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
178 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
179 if errno != syscall.EEXIST || delay > time.Minute {
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
180 return nil, err
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
181 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
182 time.Sleep(delay)
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
183 delay *= 2
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
184 }
1246b4b9028b Add tool to remove passwords.
Paul Fisher <paul@pfish.zone>
parents: 9
diff changeset
185 }
0
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
186 unix.Umask(oldUmask)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
187 h.tempFile = tempFile
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
188 // Open the input file.
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
189 inFile, err := os.Open(fileName)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
190 if err == nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
191 // 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
192 h.inFile = inFile
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
193 h.scanner = bufio.NewScanner(h.inFile)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
194 if !h.scanner.Scan() {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
195 return nil, ErrorBadFile
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
196 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
197 if h.scanner.Text() != Banner {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
198 return nil, ErrorBadFile
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
199 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
200 // 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
201 inStat, err := h.inFile.Stat()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
202 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
203 h.bail()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
204 return nil, err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
205 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
206 inStatUnix := inStat.Sys().(*syscall.Stat_t)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
207 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
208 h.bail()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
209 return nil, err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
210 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
211 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
212 h.bail()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
213 return nil, err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
214 }
9
e58bfc7fc207 Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
215 } else {
e58bfc7fc207 Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
216 // TODO(pfish): Restrict ACL to only multipass authenticators.
e58bfc7fc207 Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
217 if err := h.tempFile.Chmod(0644); err != nil {
e58bfc7fc207 Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
218 h.bail()
e58bfc7fc207 Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
219 return nil, err
e58bfc7fc207 Make multipass files default all-readable.
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
220 }
0
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
221 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
222 h.writer = bufio.NewWriter(h.tempFile)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
223 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
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 return h, nil
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
227 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
228
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
229 func (h *writeHandle) bail() {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
230 h.tempFile.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
231 h.inFile.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
232 os.Remove(h.tempName)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
233 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
234
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
235 func (h *writeHandle) next() bool {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
236 // 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
237 return h.scanner != nil && h.scanner.Scan()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
238 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
239
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
240 func (h *writeHandle) entry() (*auth.Entry, error) {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
241 return auth.EntryFromShadow(h.scanner.Text())
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
242 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
243
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
244 func (h *writeHandle) write(entry *auth.Entry) error {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
245 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
246 return err
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 _, err := h.writer.WriteString("\n")
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
249 return err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
250 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
251
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
252 func (h *writeHandle) finalize() error {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
253 h.inFile.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
254 // Make absolutely completely sure we're synced.
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
255 if err := h.writer.Flush(); err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
256 h.tempFile.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
257 os.Remove(h.tempName)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
258 return err
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 if err := h.tempFile.Sync(); err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
261 h.tempFile.Close()
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
262 os.Remove(h.tempName)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
263 return err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
264 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
265 // Close the file.
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
266 if err := h.tempFile.Close(); err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
267 os.Remove(h.tempName)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
268 return err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
269 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
270 // And atomically write it over the new one.
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
271 err := os.Rename(h.tempName, h.fileName)
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
272 if err != nil {
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
273 return err
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
274 }
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
275 // 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
276 // will the multipass shadow file have been updated.
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
277 return nil
c18bc7b9d1d9 Basic binaries. checkpassword doesn't yet work.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
278 }