-
Notifications
You must be signed in to change notification settings - Fork 165
/
files.go
356 lines (284 loc) · 9.09 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
package backends
import (
"bufio"
"fmt"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
"github.com/iegomez/mosquitto-go-auth/common"
)
// saltSize defines the salt size
const saltSize = 16
// HashIterations defines the number of hash iterations.
var HashIterations = 100000
//FileUer keeps a user password and acl records.
type FileUser 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
}
//FileBE holds paths to files, list of file users and general (no user or pattern) acl records.
type Files struct {
PasswordPath string
AclPath string
SaltEncoding string
CheckAcls bool
Users map[string]*FileUser //Users keeps a registry of username/FileUser pairs, holding a user's password and Acl records.
AclRecords []AclRecord
}
//NewFiles initializes a files backend.
func NewFiles(authOpts map[string]string, logLevel log.Level) (Files, error) {
log.SetLevel(logLevel)
var files = Files{
PasswordPath: "",
AclPath: "",
CheckAcls: false,
Users: make(map[string]*FileUser),
AclRecords: make([]AclRecord, 0),
}
if passwordPath, ok := authOpts["password_path"]; ok {
files.PasswordPath = passwordPath
} else {
return files, errors.New("Files backend error: no password path given")
}
if saltEncoding, ok := authOpts["salt_encoding"]; ok {
files.SaltEncoding = saltEncoding
} else {
files.SaltEncoding = "base64"
}
if aclPath, ok := authOpts["acl_path"]; ok {
files.AclPath = aclPath
files.CheckAcls = true
} else {
files.CheckAcls = false
log.Info("Acls won't be checked")
}
//Now initialize FileUsers by reading from password and acl files.
uCount, err := files.readPasswords()
if err != nil {
return files, errors.Errorf("read passwords: %s", err)
} else {
log.Debugf("got %d users from passwords file", uCount)
}
//Only read acls if path was given.
if files.CheckAcls {
aclCount, err := files.readAcls()
if err != nil {
return files, errors.Errorf("read acls: %s", err)
} else {
log.Infof("got %d lines from acl file", aclCount)
}
}
return files, nil
}
//ReadPasswords read file and populates FileUsers. Return amount of users seen and possile error.
func (o Files) readPasswords() (int, error) {
usersCount := 0
file, err := os.Open(o.PasswordPath)
if err != nil {
return usersCount, fmt.Errorf("Files backend error: couldn't open passwords file: %s", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
index := 0
//Read line by line
for scanner.Scan() {
index++
//Check comment or empty line to skip them.
if checkCommentOrEmpty(scanner.Text()) {
continue
}
lineArr := strings.Split(scanner.Text(), ":")
if len(lineArr) != 2 {
log.Errorf("Read passwords error: line %d is not well formatted", index)
continue
}
//Create user if it doesn't exist and save password; override password if user existed.
var fileUser *FileUser
var ok bool
fileUser, ok = o.Users[lineArr[0]]
if ok {
fileUser.Password = lineArr[1]
} else {
usersCount++
fileUser = &FileUser{
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 *Files) readAcls() (int, error) {
linesCount := 0
//Set currentUser as empty string
currentUser := ""
file, err := os.Open(o.AclPath)
if err != nil {
return linesCount, errors.Errorf("Files 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++
line := scanner.Text()
//Check comment or empty line to skip them.
if checkCommentOrEmpty(scanner.Text()) {
continue
}
//If we see a user line, change the current user.
if strings.Contains(line, "user") {
//Try to get username
lineArr := strings.Fields(line)
//Check format
if len(lineArr) == 2 && lineArr[0] == "user" {
_, ok := o.Users[lineArr[1]]
//Check that user exists
if !ok {
return 0, errors.Errorf("Files backend error: user %s does not exist for acl at line %d", lineArr[1], index)
}
currentUser = lineArr[1]
} else {
return 0, errors.Errorf("Files backend error: wrong acl format at line %d", index)
}
} else if strings.Contains(line, "topic") {
//Split and check for read, write or empty (readwwrite) privileges.
lineArr := strings.Fields(line)
if (len(lineArr) == 2 || len(lineArr) == 3) && lineArr[0] == "topic" {
var aclRecord = AclRecord{
Topic: "",
Acc: MOSQ_ACL_NONE,
}
//If len is 2, then we assume ReadWrite privileges.
if len(lineArr) == 2 {
aclRecord.Topic = lineArr[1]
aclRecord.Acc = MOSQ_ACL_READWRITE
} else {
aclRecord.Topic = lineArr[2]
if lineArr[1] == "read" {
aclRecord.Acc = MOSQ_ACL_READ
} else if lineArr[1] == "write" {
aclRecord.Acc = MOSQ_ACL_WRITE
} else if lineArr[1] == "readwrite" {
aclRecord.Acc = MOSQ_ACL_READWRITE
} else if lineArr[1] == "subscribe" {
aclRecord.Acc = MOSQ_ACL_SUBSCRIBE
} else {
return 0, errors.Errorf("Files backend error: wrong acl format at line %d", index)
}
}
//Append to user or general depending on currentUser.
if currentUser != "" {
fUser, ok := o.Users[currentUser]
if !ok {
return 0, errors.Errorf("Files backend error: user %s does not exist for acl at line %d", lineArr[1], index)
}
fUser.AclRecords = append(fUser.AclRecords, aclRecord)
} else {
o.AclRecords = append(o.AclRecords, aclRecord)
}
linesCount++
} else {
return 0, errors.Errorf("Files backend error: wrong acl format at line %d", index)
}
} else if strings.Contains(line, "pattern") {
//Split and check for read, write or empty (readwwrite) privileges.
lineArr := strings.Fields(line)
if (len(lineArr) == 2 || len(lineArr) == 3) && lineArr[0] == "pattern" {
var aclRecord = AclRecord{
Topic: "",
Acc: MOSQ_ACL_NONE,
}
//If len is 2, then we assume ReadWrite privileges.
if len(lineArr) == 2 {
aclRecord.Topic = lineArr[1]
aclRecord.Acc = MOSQ_ACL_READWRITE
} else {
aclRecord.Topic = lineArr[2]
if lineArr[1] == "read" {
aclRecord.Acc = MOSQ_ACL_READ
} else if lineArr[1] == "write" {
aclRecord.Acc = MOSQ_ACL_WRITE
} else if lineArr[1] == "readwrite" {
aclRecord.Acc = MOSQ_ACL_READWRITE
} else if lineArr[1] == "subscribe" {
aclRecord.Acc = MOSQ_ACL_SUBSCRIBE
} else {
return 0, errors.Errorf("Files backend error: wrong acl format at line %d", index)
}
}
//Append to general acls.
o.AclRecords = append(o.AclRecords, aclRecord)
linesCount++
} else {
return 0, errors.Errorf("Files backend error: wrong acl format at line %d", index)
}
}
}
return linesCount, nil
}
func checkCommentOrEmpty(line string) bool {
if len(strings.Replace(line, " ", "", -1)) == 0 || line[0:1] == "#" {
return true
}
return false
}
//GetUser checks that user exists and password is correct.
func (o Files) GetUser(username, password, clientid string) bool {
fileUser, ok := o.Users[username]
if !ok {
return false
}
if common.HashCompare(password, fileUser.Password, o.SaltEncoding) {
return true
}
log.Warnf("wrong password for user %s", username)
return false
}
//GetSuperuser returns false for files backend.
func (o Files) GetSuperuser(username string) bool {
return false
}
//CheckAcl checks that the topic may be read/written by the given user/clientid.
func (o Files) CheckAcl(username, topic, clientid string, acc int32) bool {
//If there are no acls, all access is allowed.
if !o.CheckAcls {
return true
}
fileUser, ok := o.Users[username]
//If user exists, check against his acls and common ones. If not, check against common acls only.
if ok {
for _, aclRecord := range fileUser.AclRecords {
if common.TopicsMatch(aclRecord.Topic, topic) && (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
}
}
}
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)
if common.TopicsMatch(aclTopic, topic) && (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
}
}
return false
}
//GetName returns the backend's name
func (o Files) GetName() string {
return "Files"
}
//Halt does nothing for files as there's no cleanup needed.
func (o Files) Halt() {
//Do nothing
}