/
store.go
368 lines (319 loc) · 9.65 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// Copyright (c) 2016 whawty contributors (see AUTHORS file)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of whawty.auth nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Package store implements a simple storage backend for whawty.auth password
// hash files. The schema of the whawty.auth password store can be found in the
// doc directory.
// If the environment contains the variable WHAWTY_AUTH_DEBUG logging will be enabled.
// By default whawty.auth doesn't log anything.
package store
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
var (
wl = log.New(ioutil.Discard, "[whawty.auth]\t", log.LstdFlags)
userNameRe = regexp.MustCompile("^[A-Za-z0-9][-_.@A-Za-z0-9]*$")
errNoSupportedHash = errors.New("No admin with supported password hash found")
)
const (
adminExt string = ".admin"
userExt string = ".user"
tmpDir string = ".tmp"
)
func init() {
if _, exists := os.LookupEnv("WHAWTY_AUTH_DEBUG"); exists {
wl.SetOutput(os.Stderr)
}
}
// Dir represents a directory containing a whawty.auth password hash store. Use NewDir to create it.
type Dir struct {
BaseDir string
Default uint
Params map[uint]Hasher
}
// NewDir creates a new whawty.auth store using BaseDir as base directory.
func NewDir(BaseDir string) (d *Dir) {
d = &Dir{}
d.BaseDir = filepath.Clean(BaseDir)
d.Default = 0
d.Params = make(map[uint]Hasher)
return
}
// NewDirFromConfig creates a new whawty.auth store from yaml config file.
func NewDirFromConfig(configfile string) (d *Dir, err error) {
d = &Dir{}
d.Params = make(map[uint]Hasher)
err = d.fromConfig(configfile)
return
}
func openDir(path string) (*os.File, error) {
dir, err := os.Open(path)
if err != nil {
return nil, err
}
i, err := dir.Stat()
if err != nil {
dir.Close()
return nil, err
}
if !i.IsDir() {
dir.Close()
return nil, fmt.Errorf("Error: '%s' is not a directory", path)
}
return dir, nil
}
// getTempFile provides a new, empty file in the base's .tmp directory,
// suitable for atomic file updates (by create/write/rename)
func (d *Dir) getTempFile() (tmp *os.File, err error) {
tmpDir := filepath.Join(d.BaseDir, tmpDir)
if err := os.MkdirAll(tmpDir, 0700); err != nil {
return nil, err
}
return ioutil.TempFile(tmpDir, "")
}
func isDirEmpty(dir *os.File) bool {
entries, _ := dir.ReadDir(2)
if len(entries) == 0 {
return true
}
if len(entries) == 1 && entries[0].IsDir() && entries[0].Name() == ".tmp" {
return true
}
return false
}
func checkUserFile(filename string) (valid bool, user string, isAdmin bool, err error) {
switch filepath.Ext(filename) {
case adminExt:
user = strings.TrimSuffix(filename, adminExt)
isAdmin = true
case userExt:
user = strings.TrimSuffix(filename, userExt)
default:
err = fmt.Errorf("file '%s' has invalid extension", filename)
return
}
if userNameRe.MatchString(user) {
valid = true
}
return
}
// Init initializes the store by creating a password file for an admin user.
func (d *Dir) Init(admin, password string) error {
dir, err := openDir(d.BaseDir)
if err != nil {
return err
}
defer dir.Close()
if empty := isDirEmpty(dir); !empty {
return fmt.Errorf("Error: '%s' is not empty", d.BaseDir)
}
return d.AddUser(admin, password, true)
}
// Check tests if the directory is a valid whawty.auth base directory.
func (d *Dir) Check() error {
dir, err := openDir(d.BaseDir)
if err != nil {
return err
}
defer dir.Close()
names, err := dir.Readdirnames(0)
if err != nil && err != io.EOF {
return err
}
result := errNoSupportedHash
for _, name := range names {
// Skip the '.tmp' directory
if name == tmpDir {
continue
}
valid, user, isAdmin, err := checkUserFile(name)
if err != nil {
return err
}
if !valid {
wl.Printf("ignoring file for invalid username: '%s'", user)
}
if isAdmin {
if exists, _ := fileExists(filepath.Join(dir.Name(), user) + userExt); exists {
return fmt.Errorf("both '%s' and '%s' exist", name, user+userExt)
}
} else {
if exists, _ := fileExists(filepath.Join(dir.Name(), user) + adminExt); exists {
return fmt.Errorf("both '%s' and '%s' exist", name, user+adminExt)
}
continue
}
if isFormatSupported(filepath.Join(dir.Name(), name), d) == nil {
result = nil
}
}
return result
}
// AddUser adds user to the store. It is an error if the user already exists.
func (d *Dir) AddUser(user, password string, isAdmin bool) (err error) {
if !userNameRe.MatchString(user) {
return fmt.Errorf("username '%s' is invalid", user)
}
return NewUserHash(d, user).Add(password, isAdmin)
}
// UpdateUser changes the password of user. It is an error if the user does
// not exist.
func (d *Dir) UpdateUser(user, password string) (err error) {
return NewUserHash(d, user).Update(password)
}
// SetAdmin changes the admin status of user. It is an error if the user does
// not exist.
func (d *Dir) SetAdmin(user string, adminState bool) (err error) {
return NewUserHash(d, user).SetAdmin(adminState)
}
// RemoveUser removes user from the store.
func (d *Dir) RemoveUser(user string) {
NewUserHash(d, user).Remove()
}
// User holds basic information about a specific user. This is used as the
// value type for UserList.
type User struct {
IsAdmin bool `json:"admin"`
LastChanged time.Time `json:"lastchanged"`
}
// UserList is the return value of List(). The key of the map is the username.
type UserList map[string]User
// List returns a list of all supported users in the store.
func (d *Dir) List() (UserList, error) {
dir, err := openDir(d.BaseDir)
if err != nil {
return nil, err
}
defer dir.Close()
list := make(UserList)
for {
last := false
names, err := dir.Readdirnames(3)
if err != nil {
if err == io.EOF {
last = true
} else {
return list, err
}
}
for _, name := range names {
// Skip the '.tmp' directory
if name == tmpDir {
continue
}
valid, user, isAdmin, err := checkUserFile(name)
if err != nil {
return list, err
}
if !valid {
wl.Printf("ignoring file for invalid username: '%s'", user)
continue
}
ok, _, lastchanged, _, _ := isFormatSupportedFull(filepath.Join(dir.Name(), name), d)
if !ok {
wl.Printf("ignoring file with unsupported hash format for username: '%s'", user)
continue
}
list[user] = User{isAdmin, lastchanged}
}
if last {
break
}
}
return list, err
}
// UserFull holds additional information about a specific user. This is used as the
// value type for UserListFull.
type UserFull struct {
IsAdmin bool `json:"admin"`
LastChanged time.Time `json:"lastchanged"`
IsValid bool `json:"valid"`
IsSupported bool `json:"supported"`
FormatID string `json:"formatid"`
ParamID uint `json:"paramid"`
}
// UserListFull is the return value of ListFull(). The key of the map is the username.
type UserListFull map[string]UserFull
// ListFull returns a list of all users in the store. This includes users with
// unsupported hash formats.
func (d *Dir) ListFull() (UserListFull, error) {
dir, err := openDir(d.BaseDir)
if err != nil {
return nil, err
}
defer dir.Close()
list := make(UserListFull)
for {
last := false
names, err := dir.Readdirnames(3)
if err != nil {
if err == io.EOF {
last = true
} else {
return list, err
}
}
for _, name := range names {
// Skip the '.tmp' directory
if name == tmpDir {
continue
}
var user UserFull
var err error
var username string
if user.IsValid, username, user.IsAdmin, err = checkUserFile(name); err != nil {
return list, err
}
user.IsSupported, user.FormatID, user.LastChanged, user.ParamID, _ = isFormatSupportedFull(filepath.Join(dir.Name(), name), d)
list[username] = user
}
if last {
break
}
}
return list, err
}
// Exists checks if user exists. It also returns whether user is an admin.
func (d *Dir) Exists(user string) (exists bool, isAdmin bool, err error) {
return NewUserHash(d, user).Exists()
}
// Authenticate checks if user and password are a valid combination. It also returns
// whether user is an admin, the password is upgradeable and when the password was last changed.
func (d *Dir) Authenticate(user, password string) (isAuthenticated, isAdmin, upgradeable bool, lastchange time.Time, err error) {
return NewUserHash(d, user).Authenticate(password)
}