-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathfiles.go
438 lines (353 loc) · 11.1 KB
/
files.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package files
import (
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
. "github.com/iegomez/mosquitto-go-auth/backends/constants"
"github.com/iegomez/mosquitto-go-auth/backends/topics"
"github.com/iegomez/mosquitto-go-auth/hashing"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const (
read = "read"
write = "write"
readwrite = "readwrite"
subscribe = "subscribe"
deny = "deny"
)
var permissions = map[string]byte{
read: MOSQ_ACL_READ,
write: MOSQ_ACL_WRITE,
readwrite: MOSQ_ACL_READWRITE,
subscribe: MOSQ_ACL_SUBSCRIBE,
deny: MOSQ_ACL_DENY,
}
// StaticFileUer keeps a user password and acl records.
type staticFileUser struct {
password string
aclRecords []aclRecord
}
// aclRecord holds a topic and access privileges.
type aclRecord struct {
topic string
acc byte //None 0x00, Read 0x01, Write 0x02, ReadWrite: Read | Write : 0x03, Subscribe 0x04, Deny 0x11
}
// Checker holds paths to static files, list of file users and general (no user or pattern) acl records.
type Checker struct {
sync.Mutex
pwPath string
aclPath string
checkACLs bool
checkUsers bool
users map[string]*staticFileUser //users keeps a registry of username/staticFileUser pairs, holding a user's password and Acl records.
aclRecords []aclRecord
staticFilesOnly bool
hasher hashing.HashComparer
signals chan os.Signal
}
// NewCheckers initializes a static files checker.
func NewChecker(backends, passwordPath, aclPath string, logLevel log.Level, hasher hashing.HashComparer) (*Checker, error) {
log.SetLevel(logLevel)
var checker = &Checker{
pwPath: passwordPath,
aclPath: aclPath,
checkACLs: true,
users: make(map[string]*staticFileUser),
aclRecords: make([]aclRecord, 0),
staticFilesOnly: true,
hasher: hasher,
signals: make(chan os.Signal, 1),
checkUsers: true,
}
if checker.pwPath == "" {
checker.checkUsers = false
log.Infoln("[StaticFiles] passwords won't be checked")
}
if checker.aclPath == "" {
checker.checkACLs = false
log.Infoln("[StaticFiles] acls won't be checked")
}
if len(strings.Split(strings.Replace(backends, " ", "", -1), ",")) > 1 {
checker.staticFilesOnly = false
}
err := checker.loadStaticFiles()
if err != nil {
return nil, err
}
go checker.watchSignals()
return checker, nil
}
func (o *Checker) watchSignals() {
signal.Notify(o.signals, syscall.SIGHUP)
for {
select {
case sig := <-o.signals:
if sig == syscall.SIGHUP {
log.Debugln("[StaticFiles] got SIGHUP, reloading static files")
o.loadStaticFiles()
}
}
}
}
func (o *Checker) loadStaticFiles() error {
o.Lock()
defer o.Unlock()
if o.checkUsers {
count, err := o.readPasswords()
if err != nil {
return errors.Errorf("read passwords: %s", err)
}
log.Debugf("got %d users from passwords file", count)
}
if o.checkACLs {
count, err := o.readAcls()
if err != nil {
return errors.Errorf("read acls: %s", err)
}
log.Debugf("got %d lines from acl file", count)
}
return nil
}
// ReadPasswords reads passwords file and populates static file users. Returns amount of users seen and possile error.
func (o *Checker) readPasswords() (int, error) {
usersCount := 0
file, err := os.Open(o.pwPath)
if err != nil {
return usersCount, fmt.Errorf("[StaticFiles] error: couldn't open passwords file: %s", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
index := 0
for scanner.Scan() {
index++
text := scanner.Text()
if checkCommentOrEmpty(text) {
continue
}
lineArr := strings.Split(text, ":")
if len(lineArr) != 2 {
log.Errorf("Read passwords error: line %d is not well formatted", index)
continue
}
var fileUser *staticFileUser
var ok bool
fileUser, ok = o.users[lineArr[0]]
if ok {
fileUser.password = lineArr[1]
} else {
usersCount++
fileUser = &staticFileUser{
password: lineArr[1],
aclRecords: make([]aclRecord, 0),
}
o.users[lineArr[0]] = fileUser
}
}
return usersCount, nil
}
// readAcls reads the Acl file and associates them to existing users. It omits any non existing users.
func (o *Checker) readAcls() (int, error) {
linesCount := 0
currentUser := ""
userExists := false
userSeen := false
file, err := os.Open(o.aclPath)
if err != nil {
return linesCount, errors.Errorf("StaticFiles backend error: couldn't open acl file: %s", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
index := 0
for scanner.Scan() {
index++
text := scanner.Text()
if checkCommentOrEmpty(text) {
continue
}
line := strings.TrimSpace(text)
lineArr := strings.Fields(line)
prefix := lineArr[0]
if prefix == "user" {
// Flag that a user has been seen so no topic coming after is addigned to general ones.
userSeen = true
// Since there may be more than one consecutive space in the username, we have to remove the prefix and trim to get the username.
username, err := removeAndTrim(prefix, line, index)
if err != nil {
return 0, err
}
_, ok := o.users[username]
if !ok {
if o.checkUsers {
log.Warnf("user %s doesn't exist, skipping acls", username)
// Flag username to skip topics later.
userExists = false
continue
}
o.users[username] = &staticFileUser{
password: "",
aclRecords: make([]aclRecord, 0),
}
}
userExists = true
currentUser = username
} else if prefix == "topic" || prefix == "pattern" {
var aclRecord = aclRecord{
topic: "",
acc: MOSQ_ACL_NONE,
}
/* If len is 2, then we assume ReadWrite privileges.
Notice that Mosquitto docs prevent whitespaces in the topic when there's no explicit access given:
"The access type is controlled using "read", "write", "readwrite" or "deny". This parameter is optional (unless <topic> includes a space character)"
https://mosquitto.org/man/mosquitto-conf-5.html
When access is given, then the topic may contain whitespaces.
Nevertheless, there may be white spaces between topic/pattern and the permission or the topic itself.
Fields captures the case in which there's only topic/pattern and the given topic because it trims extra spaces between them.
*/
if len(lineArr) == 2 {
aclRecord.topic = lineArr[1]
aclRecord.acc = MOSQ_ACL_READWRITE
} else {
// There may be more than one space between topic/pattern and the permission, as well as between the latter and the topic itself.
// Hence, we remove the prefix, trim the line and split on white space to get the permission.
line, err = removeAndTrim(prefix, line, index)
if err != nil {
return 0, err
}
lineArr = strings.Split(line, " ")
permission := lineArr[0]
// Again, there may be more than one space between the permission and the topic, so we'll trim what's left after removing it and that'll be the topic.
topic, err := removeAndTrim(permission, line, index)
if err != nil {
return 0, err
}
switch permission {
case read, write, readwrite, subscribe, deny:
aclRecord.acc = permissions[permission]
default:
return 0, errors.Errorf("StaticFiles backend error: wrong acl format at line %d", index)
}
aclRecord.topic = topic
}
if prefix == "topic" {
if currentUser != "" {
// Skip topic when user was not found.
if !userExists {
continue
}
fUser, ok := o.users[currentUser]
if !ok {
return 0, errors.Errorf("StaticFiles backend error: user does not exist for acl at line %d", index)
}
fUser.aclRecords = append(fUser.aclRecords, aclRecord)
} else {
// Only append to general topics when no user has been processed.
if !userSeen {
o.aclRecords = append(o.aclRecords, aclRecord)
}
}
} else {
o.aclRecords = append(o.aclRecords, aclRecord)
}
linesCount++
} else {
return 0, errors.Errorf("StaticFiles backend error: wrong acl format at line %d", index)
}
}
return linesCount, nil
}
func removeAndTrim(prefix, line string, index int) (string, error) {
if len(line)-len(prefix) < 1 {
return "", errors.Errorf("StaticFiles backend error: wrong acl format at line %d", index)
}
newLine := strings.TrimSpace(line[len(prefix):])
return newLine, nil
}
func checkCommentOrEmpty(line string) bool {
if len(strings.Replace(line, " ", "", -1)) == 0 || line[0:1] == "#" {
return true
}
return false
}
func (o *Checker) Users() map[string]*staticFileUser {
return o.users
}
// GetUser checks that user exists and password is correct.
func (o *Checker) GetUser(username, password, clientid string) (bool, error) {
fileUser, ok := o.users[username]
if !ok {
return false, nil
}
if o.hasher.Compare(password, fileUser.password) {
return true, nil
}
log.Warnf("wrong password for user %s", username)
return false, nil
}
// GetSuperuser returns false as there are no files superusers.
func (o *Checker) GetSuperuser(username string) (bool, error) {
return false, nil
}
// CheckAcl checks that the topic may be read/written by the given user/clientid.
func (o *Checker) CheckAcl(username, topic, clientid string, acc int32) (bool, error) {
// If there are no acls and StaticFiles is the only backend, all access is allowed.
// If there are other backends, then we can't blindly grant access.
if !o.checkACLs {
return o.staticFilesOnly, nil
}
fileUser, ok := o.users[username]
// Check if the topic was explicitly denied and refuse to authorize if so.
if ok {
for _, aclRecord := range fileUser.aclRecords {
match := topics.Match(aclRecord.topic, topic)
if match {
if aclRecord.acc == MOSQ_ACL_DENY {
return false, nil
}
}
}
}
for _, aclRecord := range o.aclRecords {
aclTopic := strings.Replace(aclRecord.topic, "%c", clientid, -1)
aclTopic = strings.Replace(aclTopic, "%u", username, -1)
match := topics.Match(aclTopic, topic)
if match {
if aclRecord.acc == MOSQ_ACL_DENY {
return false, nil
}
}
}
// No denials, check against user's acls and common ones. If not authorized, check against pattern acls.
if ok {
for _, aclRecord := range fileUser.aclRecords {
match := topics.Match(aclRecord.topic, topic)
if match {
if acc == int32(aclRecord.acc) || int32(aclRecord.acc) == MOSQ_ACL_READWRITE || (acc == MOSQ_ACL_SUBSCRIBE && topic != "#" && (int32(aclRecord.acc) == MOSQ_ACL_READ || int32(aclRecord.acc) == MOSQ_ACL_SUBSCRIBE)) {
return true, nil
}
}
}
}
for _, aclRecord := range o.aclRecords {
// Replace all occurrences of %c for clientid and %u for username
aclTopic := strings.Replace(aclRecord.topic, "%c", clientid, -1)
aclTopic = strings.Replace(aclTopic, "%u", username, -1)
match := topics.Match(aclTopic, topic)
if match {
if acc == int32(aclRecord.acc) || int32(aclRecord.acc) == MOSQ_ACL_READWRITE || (acc == MOSQ_ACL_SUBSCRIBE && topic != "#" && (int32(aclRecord.acc) == MOSQ_ACL_READ || int32(aclRecord.acc) == MOSQ_ACL_SUBSCRIBE)) {
return true, nil
}
}
}
return false, nil
}
// Halt does nothing for static files as there's no cleanup needed.
func (o *Checker) Halt() {
// NO-OP
}