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
|
--[[ ACF Logon/Logoff authenticator that uses plaintext files
Copyright (c) 2007 Nathan Angelacos
GPL2 license
The password file is in the format:
userid:password:username:role1[,role2...]
]]--
module (..., package.seeall)
local sess = require ("session")
local pvt={}
pvt.parse_authfile = function(filename)
local row = {}
-- open our password file
local f = io.open (filename)
if f then
local m = (f:read("*all") or "" ).. "\n"
f:close()
for l in string.gmatch(m, "(%C*)\n") do
local userid, password, username, roles =
string.match(l, "([^:]*):([^:]*):([^:]*):(.*)")
local r = {}
roles=roles or ""
for x in string.gmatch(roles, "([^,]%w+),?") do
table.insert (r, x )
end
local a = {}
a.userid = userid
a.password = password
a.username = username
a.roles = r
table.insert (row, a)
end
return row
else
return nil
end
end
pvt.get_id = function(userid, authstruct)
if authstruct ~= nil then
for x = 1,#authstruct do
if authstruct[x].userid == userid then
return authstruct[x]
end
end
end
return nil
end
--- public methods
-- This function returns true or false, and
-- if false: the reason for failure
authenticate = function ( self, userid, password )
password = password or ""
userid = userid or ""
local t = pvt.parse_authfile(self.conf.confdir .. "/passwd")
if t == false then
return false, "password file is missing"
else
if userid ~= nil then
local id = pvt.get_id (userid, t)
if id == false or id == nil then
return false, "Userid not found"
end
if id.password ~= password then
return false, "Invalid password"
end
else
return false
end
return true
end
end
pvt.permission_to_change = function()
--FIXME: Check if user is allowed to change things
return true
end
pvt.weak_password = function(password)
-- If password is too short, return false
if (#password < 4) then
return true, "Password is too short!"
end
if (tonumber(password)) then
return true, "Password can't contain only numbers!"
end
return false
end
pvt.availablefields = function (field)
-- This is a list of fileds in the /passwd file that we are allowed to use.
-- Could be used to check that right variable-name is used.
local availablefileds = {
['userid']=true,
['password']=true,
['username']=true,
['roles']=true,
}
return availablefileds[field]
end
-- validate the settings (ignore password if it's nil)
local validate_settings = function (self, userid, username, password, password_confirm, roles)
local errormessage = {}
-- Set errormessages when entering invalid values
if (#userid == 0) then errormessage.userid = "You need to enter a valid userid!" end
if password then
if (#password == 0) then
errormessage.password = "Password cannot be blank!"
elseif (password ~= password_confirm) then
errormessage.password_confirm = "You entered wrong password/confirmation"
else
local weak_password_result, weak_password_errormessage = pvt.weak_password(password)
if (weak_password_result) then errormessage.password = weak_password_errormessage end
end
end
local reverseroles = {}
for x,role in pairs(list_roles(self)) do
reverseroles[role] = x
end
for x,role in pairs(roles) do
if reverseroles[role] == nil then
errormessage.roles = "Invalid role"
break
end
end
-- Return false if any errormessages are set
for k,v in pairs(errormessage) do
return false, errormessage
end
return true, errormessage
end
-- This function returns the username and roles
-- or false on an error
get_userinfo = function ( self, userid )
local t = pvt.parse_authfile(self.conf.confdir .. "/passwd")
if t == false then
return nil
else
return pvt.get_id (userid, t)
end
end
get_userinfo_roles = function (self, userid)
local t = pvt.parse_authfile(self.conf.confdir .. "/passwd")
if t == false then
return nil
else
temp = pvt.get_id (userid, t)
return temp.roles
end
end
list_users = function (self)
local output = {}
local t = pvt.parse_authfile(self.conf.confdir .. "/passwd")
if t == false then
return nil
else
for k,v in pairs(t) do
table.insert(output,v.userid)
end
return output
end
end
list_roles = function (self)
local output = {"CREATE","UPDATE","DELETE","READ"}
return output
end
change_settings = function (self, userid, parameter, value)
local errormessage = {}
local passwd_path = self.conf.confdir .. "/passwd"
-- We start by checking if user is allowed to do changes
if not (pvt.permission_to_change) then
errormessage.permissions = "No permission to change!"
end
-- Check if userid already used
if (parameter == "userid") and (userid ~= value) then
for k,v in pairs(list_users(self)) do
if (v == value) then
errormessage.userid = "This userid already exists!"
end
end
end
-- Check if user entered available commands
if not (userid) or not (parameter) or not (pvt.availablefields(parameter)) then
errormessage.userid = "You need to enter valid userid, parameter and value!"
end
-- Check if password is weak
if (parameter == "password") then
local weak_password_result, weak_password_errormessage = pvt.weak_password(value)
if (weak_password_result) then
errormessage.password = weak_password_errormessage
end
end
-- Return false if some errormessages is set
for k,v in pairs(errormessage) do
return false, errormessage
end
-- If the parameter is password, then scramble the password
if (parameter == "password") then
value = fs.md5sum_string(value)
end
local passwdfilecontent = fs.read_file_as_array(passwd_path)
local changes
for k,v in pairs(passwdfilecontent) do
if ( string.match(v, "^".. userid .. ":") ) then
changes = {}
-- Get current values
changes.userid, changes.password, changes.username, changes.roles =
string.match(v, "([^:]*):([^:]*):([^:]*):(.*)")
-- Actually change the value (remove all ':')
changes[parameter] = string.gsub(value, ":", "")
-- Update the table with the new values
passwdfilecontent[k] = changes.userid .. ":" .. changes.password .. ":".. changes.username .. ":" .. changes.roles
end
end
--Write changes to file
fs.write_file(passwd_path, table.concat(passwdfilecontent,"\n"))
return true
end
-- For an existing user, change the settings that are non-nil
change_settings = function (self, userid, username, password, password_confirm, roles)
local result = true
local errormessage = {}
-- Get the current user info
local userinfo = get_userinfo(self, userid)
if userinfo == nil then
errormessage.userid = "This userid does not exist!"
result = false
end
local change = username or password or password_confirm or roles
if change then
-- Validate the inputs
if (result == true) then
-- Use the current settings if new ones are nil, except for password
result, errormessage = validate_settings(self, userid, username or userinfo.username, password, password_confirm, roles or userinfo.roles)
end
-- Update all the fields
if (result == true) then
userinfo.username = username or userinfo.username
if password then
userinfo.password = fs.md5sum_string(password)
end
userinfo.roles = roles or userinfo.roles
-- write the updated user
delete_user(self, userid)
-- Set path to passwordfile
local passwd_path = self.conf.confdir .. "/passwd"
-- Write the newline into the file
fs.write_line_file(passwd_path, userid .. ":" .. userinfo.password .. ":" .. userinfo.username .. ":" .. table.concat(userinfo.roles,",") )
end
end
return result, errormessage
end
new_settings = function (self, userid, username, password, password_confirm, roles)
local result = true
local errormessage = {}
-- make sure to check all fields
userid = userid or ""
username = username or ""
password = password or ""
password_confirm = password_confirm or ""
roles = roles or {}
-- Check if userid already used
for k,v in pairs(list_users(self)) do
if (v == userid) then
errormessage.userid = "This userid already exists!"
result = false
end
end
-- validate the settings
if (result == true) then
result, errormessage = validate_settings(self, userid, username, password, password_confirm, roles)
end
-- write the new user
if (result == true) then
-- Set path to passwordfile
local passwd_path = self.conf.confdir .. "/passwd"
-- Write the newline into the file
fs.write_line_file(passwd_path, userid .. ":" .. fs.md5sum_string(password) .. ":" .. username .. ":" .. table.concat(roles,",") )
end
return result, errormessage
end
delete_user = function (self, userid)
local result = false
local errormessage = {userid="User not found"}
local passwd_path = self.conf.confdir .. "/passwd"
local passwdfilecontent = fs.read_file_as_array(passwd_path)
local output = {}
for k,v in pairs(passwdfilecontent) do
if not ( string.match(v, "^".. userid .. ":") ) then
table.insert(output, v)
else
result = true
errormessage = {}
end
end
--Save the updated table
if result == true then
fs.write_file(passwd_path, table.concat(output,"\n"))
end
return result, errormessage
end
|