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
|
local mymodule = {}
-- Load libraries
modelfunctions = require("modelfunctions")
fs = require("acf.fs")
format = require("acf.format")
validator = require("acf.validator")
authenticator = require("authenticator")
roles = require("roles")
posix = require("posix")
-- Set variables
local allzonefiles = {}
local cachedzonefiles = {}
local cached_user
local packagename = "nsd"
local processname = "nsd"
local configfile = "/etc/nsd/nsd.conf"
local configdir = "/etc/nsd/"
local descr = {
prefix={
['.']="Name server for your domain (NS + A + SOA)",
['&']="Delegate subdomain (NS + A)",
['=']="Host and reverse record (A + PTR)",
['+']="Host record (A, no PTR)",
['@']="Mail exchanger (MX)",
["'"]="Text record (TXT)",
['^']="Reverse record (PTR)",
['C']="Canonical name (CNAME)",
['Z']="SOA record (SOA)",
[':']="Generic record",
['%']="Client location",
['S']="Service location (SRV)",
['N']="Naming authority pointer (NAPTR)",
},
fieldlabels={
['.']={"Domain", "IP address", "Name server", "Time to live", "Timestamp", "Location", },
['&']={"Domain", "IP address", "Name server", "Time to live", "Timestamp", "Location", },
['=']={"Host", "IP address", "Time to live", "Timestamp", "Location", },
['+']={"Host", "IP address", "Time to live", "Timestamp", "Location", },
['@']={"Domain", "IP address", "Mail exchanger", "Distance", "Time to live", "Timestamp", "Location", },
['\'']={"Domain", "Text Record", "Time to live", "Timestamp", "Location", },
['^']={"PTR", "Domain name", "Time to live", "Timestamp", "Location", },
['C']={"Domain", "Canonical name", "Time to live", "Timestamp", "Location", },
['Z']={"Domain", "Primary name server", "Contact address", "Serial number", "Refresh time", "Retry time", "Expire time", "Minimum time", "Time to live", "Timestamp", "Location",},
[':']={"Domain", "Record type", "Record data", "Time to live", "Timestamp", "Location", },
['%']={"Location", "IP prefix", },
['S']={"Domain Service", "IP address", "Server", "Port", "Priority", "Weight", "Time to live", "Timestamp", },
['N']={"Domain", "Order", "Preference", "Flags", "Service", "Regular expression", "Replacement", "Time to live", "Timestamp", },
},
}
-- ################################################################################
-- LOCAL FUNCTIONS
-- Function to recursively inserts all file details in a dir into an array
local function recursedir(path, filearray)
filearray = filearray or {}
local k,v
for k,v in pairs(posix.dir(path) or {}) do
-- Ignore files that begins with a '.'
if not string.match(v, "^%.") then
local f = path .. v
local details = posix.stat(f)
-- If subfolder exists, list files in this subfolder
if details.type == "directory" then
recursedir(f.."/", filearray)
elseif details.type == "regular" then
details.filename = f
table.insert(filearray, details)
end
end
end
return filearray
end
local is_valid_filename = function(filename)
local dirname = posix.dirname(filename or "").."/"
return validator.is_valid_filename(filename) and string.match(dirname, "^"..format.escapemagiccharacters(configdir)) and not string.match(dirname, "%.%.")
end
local function getallowedlist(self, userid)
local allowedlist = {}
local auth = authenticator.get_subauth(self)
local entry = auth.read_entry(self, authenticator.usertable, self.conf.prefix..self.conf.controller, userid) or ""
for x in string.gmatch(entry, "([^,]+),?") do allowedlist[#allowedlist + 1] = x end
-- also check to see if there are allowed files for this user's roles
local userinfo = authenticator.get_userinfo(self, userid)
-- add in the guest role
userinfo.roles[#userinfo.roles + 1] = roles.guest_role
for i,role in ipairs(userinfo.roles) do
local entry = auth.read_entry(self, authenticator.roletable, self.conf.prefix..self.conf.controller, role) or ""
for x in string.gmatch(entry, "([^,]+),?") do allowedlist[#allowedlist + 1] = x end
end
return allowedlist
end
-- Feed the allzonefiles table with list of all zonefiles in the config
local function listzonefiles()
if #allzonefiles > 0 then
return allzonefiles
end
-- Parse the config
-- TODO - handle include statements
-- TODO - should properly parse the whole file, not just look for lines
local file = io.open(configfile)
local zonesdir
local zonefiles = {}
for line in file:lines() do
if string.find(line, "^%s*zonesdir:") then
zonesdir = string.match(line, "^%s*zonesdir:%s*\"(.*)\"")
elseif string.find(line, "^%s*zonefile:") then
local z = string.match(line, "^%s*zonefile:%s*\"(.*)\"")
zonefiles[#zonefiles+1] = z
end
end
file:close()
if not zonesdir or zonesdir == "" then
zonesdir = configdir
elseif not string.find(zonesdir, "/$") then
zonesdir = zonesdir.."/"
end
for i,z in ipairs(zonefiles) do
allzonefiles[#allzonefiles+1] = zonesdir..z
end
return allzonefiles
end
-- Feed the cachedzonefiles table with list of all zonefiles that are available and allowed
-- Default to allowing all files if no userid or allowed list
local function searchforzonefiles(self, userid)
if #cachedzonefiles > 0 and cached_user == userid then
return cachedzonefiles
end
allzonefiles = listzonefiles(configdir)
local allowedlist = getallowedlist(self, userid)
if allowedlist and #allowedlist > 0 then
local reverseallowed = {}
for x,name in ipairs(allowedlist) do reverseallowed[name] = x end
for k,v in pairs(allzonefiles) do
if reverseallowed[v] then
table.insert(cachedzonefiles, v)
end
end
else
cachedzonefiles = allzonefiles
end
cached_user = userid
table.sort(cachedzonefiles)
return cachedzonefiles
end
local function is_valid_zonefile(path)
for k,v in pairs(cachedzonefiles) do
if (v == path) then
return true
end
end
return false, "Not a valid filename!"
end
-- ################################################################################
-- PUBLIC FUNCTIONS
function mymodule.get_startstop(self, clientdata)
return modelfunctions.get_startstop(processname)
end
function mymodule.startstop_service(self, startstop, action)
return modelfunctions.startstop_service(startstop, action)
end
function mymodule.getstatus()
return modelfunctions.getstatus(processname, packagename, "NSD Status")
end
function mymodule.list_files(self)
local filearray = recursedir(configdir)
table.sort(filearray, function(a,b) return a.filename < b.filename end)
return cfe({ type="structure", value=filearray, label="List of NSD files" })
end
function mymodule.get_file(self, clientdata)
local retval = cfe({ type="group", value={ filename=cfe({}) } })
self.handle_clientdata(retval, clientdata)
return modelfunctions.getfiledetails(retval.value.filename.value, is_valid_filename)
end
function mymodule.update_file(self, filedetails)
return modelfunctions.setfiledetails(self, filedetails, is_valid_filename)
end
function mymodule.get_new_file(self, clientdata)
local retval = cfe({ type="group", value={}, label="NSD File" })
retval.value.filename = cfe({ label="File Name", descr="Must be in "..configdir })
self.handle_clientdata(retval, clientdata)
return retval
end
function mymodule.create_file(self, filedetails)
local success = true
local path = string.match(filedetails.value.filename.value, "^%s*(.*%S)%s*$") or ""
if not string.find(path, "/") then
path = configdir..path
end
if not is_valid_filename(path) then
success = false
filedetails.value.filename.errtxt = "Invalid filename"
else
if not fs.is_dir(configdir) then fs.create_directory(configdir) end
if posix.stat(path) then
success = false
filedetails.value.filename.errtxt = "Filename already exists"
end
end
if success then
fs.create_file(path)
else
filedetails.errtxt = "Failed to Create File"
end
return filedetails
end
function mymodule.get_delete_file(self, clientdata)
local retval = cfe({ type="group", value={}, label="Delete NSD File" })
retval.value.filename = cfe({ label="File Name" })
self.handle_clientdata(retval, clientdata)
return retval
end
function mymodule.delete_file(self, delfile)
delfile.errtxt = "Failed to delete NSD File - invalid filename"
for i,file in ipairs(mymodule.list_files().value) do
if delfile.value.filename.value == file.filename then
delfile.errtxt = nil
os.remove(delfile.value.filename.value)
break
end
end
return delfile
end
function mymodule.getzonefilelist(self, clientdata, userid)
cachedzonefiles = searchforzonefiles(self, userid)
local listed_files = {}
for i,name in pairs(cachedzonefiles) do
local filedetails = posix.stat(name) or {}
filedetails.filename = name
table.insert(listed_files, filedetails)
end
return cfe({ type="structure", value=listed_files, label="Zone files" })
end
function mymodule.get_zonefile(self, clientdata, userid)
cachedzonefiles = searchforzonefiles(self, userid)
local retval = cfe({ type="group", value={ filename=cfe({}) } })
self.handle_clientdata(retval, clientdata)
return modelfunctions.getfiledetails(retval.value.filename.value, is_valid_zonefile)
end
function mymodule.set_zonefile (self, filedetails, userid)
cachedzonefiles = searchforzonefiles(self, userid)
return modelfunctions.setfiledetails(self, filedetails, is_valid_zonefile)
end
function mymodule.getpermissionslist(self)
local auth = authenticator.get_subauth(self)
local users = authenticator.list_users(self)
local userlist = {}
for i,user in ipairs(users) do
local allowedlist = {}
local entry = auth.read_entry(self, authenticator.usertable, self.conf.prefix..self.conf.controller, user) or ""
for x in string.gmatch(entry, "([^,]+),?") do allowedlist[#allowedlist + 1] = x end
userlist[#userlist + 1] = {id=user, allowed=allowedlist}
end
-- Need to check for roles as well as users
local rolelist = {}
local rols = roles.list_all_roles(self)
for i,role in ipairs(rols) do
local allowedlist = {}
local entry = auth.read_entry(self, authenticator.roletable, self.conf.prefix..self.conf.controller, role) or ""
for x in string.gmatch(entry, "([^,]+),?") do allowedlist[#allowedlist + 1] = x end
rolelist[#rolelist + 1] = {id=role, allowed=allowedlist}
end
table.sort(userlist, function(a,b) return a.id < b.id end)
return cfe({ type="structure", value={user=userlist, role=rolelist}, label="NSD Permissions" })
end
local function validateuserpermissions(self, userpermissions)
local success = true
local userinfo = authenticator.get_userinfo(self, userpermissions.value.userid.value)
if not userinfo then
userpermissions.value.userid.errtxt = "Invalid user"
success = false
end
success = modelfunctions.validatemulti(userpermissions.value.allowed) and success
return success, userpermissions
end
local function validaterolepermissions(self, rolepermissions)
local success = false
rolepermissions.value.role.errtxt = "Invalid role"
local rols = roles.list_all_roles(self)
for i,role in ipairs(rols) do
if rolepermissions.value.role.value == role then
rolepermissions.value.role.errtxt = nil
success = true
break
end
end
success = modelfunctions.validatemulti(rolepermissions.value.allowed) and success
return success, rolepermissions
end
function mymodule.getuserpermissions(self, clientdata)
local retval = cfe({ type="group", value={}, label="NSD User Permissions" })
retval.value.userid = cfe({ label="User Name", seq=0 })
self.handle_clientdata(retval, clientdata)
local allzonefiles = listzonefiles(configdir)
table.sort(allzonefiles)
retval.value.allowed = cfe({ type="multi", value={}, label="NSD Permissions", option=allzonefiles, descr="If no permissions are defined, then all are allowed", seq=1 })
if #allzonefiles == 0 then
retval.value.allowed.errtxt = "No zones defined"
end
local userid = retval.value.userid.value
if userid ~= "" then
retval.value.userid.readonly = true
local auth = authenticator.get_subauth(self)
local entry = auth.read_entry(self, authenticator.usertable, self.conf.prefix..self.conf.controller, userid) or ""
for x in string.gmatch(entry, "([^,]+),?") do retval.value.allowed.value[#retval.value.allowed.value + 1] = x end
validateuserpermissions(self, retval)
end
return retval
end
function mymodule.setuserpermissions(self, userpermissions)
local success, userpermissions = validateuserpermissions(self, userpermissions)
if success then
local auth = authenticator.get_subauth(self)
auth.write_entry(self, authenticator.usertable, self.conf.prefix..self.conf.controller, userpermissions.value.userid.value, table.concat(userpermissions.value.allowed.value, ","))
else
userpermissions.errtxt = "Failed to set user permissions"
end
return userpermissions
end
function mymodule.getrolepermissions(self, clientdata)
local retval = cfe({ type="group", value={}, label="NSD Role Permissions" })
retval.value.role = cfe({ label="Role", seq=0 })
self.handle_clientdata(retval, clientdata)
local allzonefiles = listzonefiles(configdir)
table.sort(allzonefiles)
retval.value.allowed = cfe({ type="multi", value={}, label="NSD Permissions", option=allzonefiles, descr="If no permissions are defined, then all are allowed", seq=1 })
if #allzonefiles == 0 then
retval.value.allowed.errtxt = "No zones defined"
end
local role = retval.value.role.value
if role ~= "" then
retval.value.role.readonly = true
local auth = authenticator.get_subauth(self)
local entry = auth.read_entry(self, authenticator.roletable, self.conf.prefix..self.conf.controller, role) or ""
for x in string.gmatch(entry, "([^,]+),?") do retval.value.allowed.value[#retval.value.allowed.value + 1] = x end
validaterolepermissions(self, retval)
end
return retval
end
function mymodule.setrolepermissions(self, rolepermissions)
local success, rolepermissions = validaterolepermissions(self, rolepermissions)
if success then
local auth = authenticator.get_subauth(self)
auth.write_entry(self, authenticator.roletable, self.conf.prefix..self.conf.controller, rolepermissions.value.role.value, table.concat(rolepermissions.value.allowed.value, ","))
else
rolepermissions.errtxt = "Failed to set role permissions"
end
return rolepermissions
end
return mymodule
|