comparison file/file.go @ 18:00d30c67b56d

Put all the library stuff into multipass/file.
author Paul Fisher <paul@pfish.zone>
date Sun, 01 Nov 2015 12:16:51 -0500
parents 9b4ec6b5c23e
children ef2ef22ca4b1
comparison
equal deleted inserted replaced
17:342f63116bfd 18:00d30c67b56d
1 // Package file handles I/O for single password files. 1 // Package file handles I/O for single password files.
2 // 2 //
3 // A password file contains multiple passwords for a single user. 3 // A password file contains multiple passwords for a single user.
4 // It starts with a banner that indicates the version of the file, 4 // It starts with a banner that indicates the version of the file,
5 // then has entries in the format specified by auth.Entry. 5 // then has entries in the format specified by Entry.
6 6
7 package file 7 package file
8 8
9 import ( 9 import (
10 "bufio" 10 "bufio"
12 "os" 12 "os"
13 "syscall" 13 "syscall"
14 "time" 14 "time"
15 15
16 "golang.org/x/sys/unix" 16 "golang.org/x/sys/unix"
17 "pfish.zone/go/multipass/auth"
18 ) 17 )
19 18
20 const ( 19 const (
21 // the Banner acts as a version indicator 20 // the Banner acts as a version indicator
22 Banner = "# Multipass v0.1 password file" 21 Banner = "# Multipass v0.1 password file"
71 return false, err 70 return false, err
72 } 71 }
73 defer file.Close() 72 defer file.Close()
74 73
75 for scanner.Scan() { 74 for scanner.Scan() {
76 entry, err := auth.EntryFromShadow(scanner.Text()) 75 entry, err := EntryFromShadow(scanner.Text())
77 // Skip invalid lines. 76 // Skip invalid lines.
78 if err != nil { 77 if err != nil {
79 continue 78 continue
80 } 79 }
81 if entry.Authenticate(password) { 80 if entry.Authenticate(password) {
83 } 82 }
84 } 83 }
85 return false, nil 84 return false, nil
86 } 85 }
87 86
88 func (f *ShadowFile) Add(entry *auth.Entry) error { 87 func (f *ShadowFile) Add(entry *Entry) error {
89 handle, err := f.openWrite() 88 handle, err := f.openWrite()
90 if err != nil { 89 if err != nil {
91 return err 90 return err
92 } 91 }
93 err = handle.write(entry) 92 err = handle.write(entry)
105 } 104 }
106 } 105 }
107 return handle.finalize() 106 return handle.finalize()
108 } 107 }
109 108
110 func (f *ShadowFile) AllEntries() ([]*auth.Entry, error) { 109 func (f *ShadowFile) AllEntries() ([]*Entry, error) {
111 file, scanner, err := f.open() 110 file, scanner, err := f.open()
112 if err != nil { 111 if err != nil {
113 return nil, err 112 return nil, err
114 } 113 }
115 defer file.Close() 114 defer file.Close()
116 115
117 var entries []*auth.Entry 116 var entries []*Entry
118 117
119 for scanner.Scan() { 118 for scanner.Scan() {
120 entry, err := auth.EntryFromShadow(scanner.Text()) 119 entry, err := EntryFromShadow(scanner.Text())
121 // Skip invalid lines. 120 // Skip invalid lines.
122 if err != nil { 121 if err != nil {
123 continue 122 continue
124 } 123 }
125 entries = append(entries, entry) 124 entries = append(entries, entry)
240 func (h *writeHandle) next() bool { 239 func (h *writeHandle) next() bool {
241 // If the scanner is nil, then we have no input file and therefore no next. 240 // If the scanner is nil, then we have no input file and therefore no next.
242 return h.scanner != nil && h.scanner.Scan() 241 return h.scanner != nil && h.scanner.Scan()
243 } 242 }
244 243
245 func (h *writeHandle) entry() (*auth.Entry, error) { 244 func (h *writeHandle) entry() (*Entry, error) {
246 return auth.EntryFromShadow(h.scanner.Text()) 245 return EntryFromShadow(h.scanner.Text())
247 } 246 }
248 247
249 func (h *writeHandle) write(entry *auth.Entry) error { 248 func (h *writeHandle) write(entry *Entry) error {
250 if _, err := h.writer.WriteString(entry.Encode()); err != nil { 249 if _, err := h.writer.WriteString(entry.Encode()); err != nil {
251 return err 250 return err
252 } 251 }
253 _, err := h.writer.WriteString("\n") 252 _, err := h.writer.WriteString("\n")
254 return err 253 return err