summaryrefslogtreecommitdiffstats
path: root/dhcp-model.lua
blob: 247b6c32eaa0c033449a8179251f7364e3dbc399 (plain)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
-- acf model for /etc/dhcp/*
-- Copyright(c) 2007 A. Brodmann - Licensed under terms of GPL2
module (..., package.seeall)

--- get additional libraries
require("modelfunctions")
require("validator")

local subnet = {}
local configfile = "/etc/dhcp/dhcpd.conf"
local confdfile = "/etc/conf.d/dhcpd"
local filelist = {configfile, confdfile}
local processname = "dhcpd"
local packagename = "dhcp"
local leasefile = "/var/lib/dhcp/dhcpd.leases"
local config

-- ################################################################################
-- LOCAL FUNCTIONS

local replacemagiccharacters = function(str)
	return string.gsub(str, "[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
end

local replaceentry = function(file, configentry, newstring)
	if newstring then
		return string.gsub(file, string.gsub(replacemagiccharacters(table.concat(configentry, "\n")), "\n", "%%s+"), replacemagiccharacters(newstring), 1)
	else
		return string.gsub(file, "[^\n%S]*"..string.gsub(replacemagiccharacters(table.concat(configentry, "\n")), "\n", "%%s+").."%s*;%s*\n?", "", 1)
	end
end

local parseconfigfile = function(file)
	-- first, remove all comments
	file = file or ""
	lines = {}
	for line in string.gmatch(file, "([^\n]*)\n?") do
		lines[#lines+1] = string.gsub(line, "#.*$", "")
	end
	file = table.concat(lines, " ")
	
	-- each line either ends with ';' or with '{'...'}'
	-- build an array with one entry per statement, each entry having an array of elements, and possibly a subarray
	local config = {}
	local stack = {config}
	local entry
	for word in string.gmatch(file, "(%S+)") do
		if word == "{" then
			if not entry then
				entry = {}
				table.insert(stack[#stack], entry)
			end
			entry.sub = {}
			stack[#stack+1] = entry.sub
			entry = nil
		elseif word == "}" then
			stack[#stack] = nil
		else
			if not entry then
				entry = {}
				table.insert(stack[#stack], entry)
				entry[1] = word
			else
				entry[#entry+1] = word
			end
			if string.find(word, ";$") then
				entry[#entry] = string.sub(word, 1, -2)
				entry = nil
			end
		end
	end
	return config
end

local validate_host = function( host )
	local success = true
	if host.value.host.value == "" or string.find(host.value.host.value, "[^%w.-]") then
		host.value.host.errtxt = "Invalid host name"
		success = false
	end
	if not validator.is_mac(host.value.mac.value) then
		host.value.mac.errtxt = "Invalid mac address"
		success = false
	end
	if host.value.addresses.value ~= "" then
		for address in string.gmatch(host.value.addresses.value, "([^,%s]+),?%s*") do
			if string.find(address, "[^%w.-]") then
				host.value.addresses.errtxt = "Invalid domain name / IPv4 address"
				success = false
			end
		end
	end
	return success, host
end

local validate_subnet = function( net )
	local success = true
	if net.value.subnet.value == "" or string.find(net.value.subnet.value, "[^%w.-]") then
		net.value.subnet.errtxt = "Invalid domain name / IPv4 address"
		success = false
	end
	if not validator.is_ipv4(net.value.netmask.value) then
		net.value.netmask.errtxt = "Invalid IPv4 address"
		success = false
	end
	if net.value.defleasetime.value ~= "" and not validator.is_integer_in_range(net.value.defleasetime.value, 1800, 86400) then
		net.value.defleasetime.errtxt = "Lease time must be: 1800 < x < 86400"
		success = false
	end
	if net.value.maxleasetime.value ~= "" and not validator.is_integer_in_range(net.value.maxleasetime.value, 1800, 86400) then
		net.value.maxleasetime.errtxt = "Lease time must be: 1800 < x < 86400"
		success = false
	end
	if net.value.routers.value ~= "" then
		for router in string.gmatch(net.value.routers.value, "([^,%s]+),?%s*") do
			if string.find(router, "[^%w.-]") then
				net.value.routers.errtxt = "Invalid domain name / IPv4 address"
				success = false
				break
			end
		end
	end
	if string.find(net.value.domainname.value, "[^%w.-]") then
		net.value.domainname.errtxt = "Invalid domain name"
		success = false
	end
	if net.value.domainnameservers.value ~= "" then
		for server in string.gmatch(net.value.domainnameservers.value, "([^,%s]+),?%s*") do
			if string.find(server, "[^%w.-]") then
				net.value.domainnameservers.errtxt = "Invalid domain name / IPv4 address"
				success = false
			end
		end
	end
	if net.value.leaserangestart.value ~= "" and not validator.is_ipv4(net.value.leaserangestart.value) then
		net.value.leaserangestart.errtxt = "Invalid IPv4 address"
		success = false
	end
	if net.value.leaserangeend.value ~= "" then
		if not validator.is_ipv4(net.value.leaserangeend.value) then
			net.value.leaserangeend.errtxt = "Invalid IPv4 address"
			success = false
		elseif net.value.leaserangestart.value == "" then
			net.value.leaserangeend.errtxt = "Cannot define range end without range start"
			success = false
		end
	end
	success = modelfunctions.validateselect(net.value.unknownclients) and success

	return success, net
end

local validate_settings = function ( settings )
	local success = modelfunctions.validateselect(settings.value.dnsupdatestyle)
	if settings.value.defleasetime.value ~= "" and not validator.is_integer_in_range(settings.value.defleasetime.value, 1800, 86400) then
		settings.value.defleasetime.errtxt = "Out of range 1800 < x < 86400 or not integer"
		success = false
	end
	if settings.value.maxleasetime.value ~= "" and not validator.is_integer_in_range(settings.value.maxleasetime.value, 1800, 86400) then
		settings.value.maxleasetime.errtxt = "Out of range 1800 < x < 86400 or not integer"
		success = false
	end
	if string.find(settings.value.domainname.value, "[^%w.-]") then
		settings.value.domainname.errtxt = "Invalid domain name"
		success = false
	end
	if settings.value.domainnameservers.value ~= "" then
		for server in string.gmatch(settings.value.domainnameservers.value, "([^,%s]+),?%s*") do
			if string.find(server, "[^%w.-]") then
				settings.value.domainnameservers.errtxt = "Invalid domain name / IPv4 address"
				success = false
			end
		end
	end

	return success, settings
end

-- Give it the string and the position of the { and it will find the corresponding }
local find_section_end = function(file, section_start)
	local i = section_start+1
	local indent = 1
	while indent > 0 and i <= #file do
		local char = string.sub(file, i, i)
		if char == "}" then indent = indent-1
		elseif char == "{" then indent = indent+1
		elseif char == "#" then i = string.find(file, "\n", i)
		elseif char == "'" then i = string.find(file, "'", i)
		elseif char == '"' then i = string.find(file, '"', i)
		end
		i=i+1;
	end
	return i-1
end

local host_write = function(host)
	local file = fs.read_file(configfile) or ""
	config = config or parseconfigfile(file)

	-- First, add the host line if necessary
	local hostline = "host "..host.value.host.value
	local found = false
	for i,value in ipairs(config or {}) do
		if value[1] == "host" and value[2] == host.value.host.value then
			found = true
		end
	end
	if not found then file = file.."\n"..hostline.." {\n}" end

	-- Now, find the host section
	local host_start = select(2, string.find(file, replacemagiccharacters(hostline).."%s*{"))
	local host_end = find_section_end(file, host_start) - 1
	host_start = string.find(file, "\n", host_start) + 1
	local hostcontent = string.sub(file, host_start, host_end)

	-- Update the host data
	host.value.mac.replace = "hardware ethernet "..host.value.mac.value
	if host.value.addresses.value ~= "" then
		host.value.addresses.replace = "fixed-address "..host.value.addresses.value
	end

	local hostconfig = parseconfigfile(hostcontent)
	for i,value in ipairs(hostconfig or {}) do
		if value[1] == "hardware" and value[2] == "ethernet" then
			hostcontent = replaceentry(hostcontent, value, host.value.mac.replace)
			host.value.mac.replace = nil
		elseif value[1] == "fixed-address" then
			hostcontent = replaceentry(hostcontent, value, host.value.addresses.replace)
			host.value.addresses.replace = nil
		end
	end

	-- add in new lines at the top if they didn't exist
	local newlines = {}
	newlines[#newlines+1] = host.value.mac.replace
	host.value.mac.replace = nil
	newlines[#newlines+1] = host.value.addresses.replace
	host.value.addresses.replace = nil
	if #newlines > 0 then
		for i,line in ipairs(newlines) do newlines[i] = "  "..line end
		newlines[#newlines+1] = hostcontent
		hostcontent = table.concat(newlines, ";\n")
	end

	-- The host is updated, put it into the file
	file = string.sub(file, 1, host_start-1) .. hostcontent .. string.sub(file, host_end+1, -1)

	-- Finally, write out the new file
	fs.write_file(configfile, string.gsub(file, "\n*$", ""))
	config = nil
end

local subnet_write = function(net)
	local file = fs.read_file(configfile) or ""
	config = config or parseconfigfile(file)

	-- First, add or update the submet line
	local subnetline = "subnet "..net.value.subnet.value.." netmask "..net.value.netmask.value
	local found = false
	for i,value in ipairs(config or {}) do
		if value[1] == "subnet" and value[2] == net.value.subnet.value then
			file = replaceentry(file, value, subnetline)
			found = true
		end
	end
	if not found then file = file.."\n"..subnetline.." {\n}" end

	-- Now, find the subnet section
	local subnet_start = select(2, string.find(file, replacemagiccharacters(subnetline).."%s*{"))
	local subnet_end = find_section_end(file, subnet_start) - 1
	subnet_start = string.find(file, "\n", subnet_start) + 1
	local subnetcontent = string.sub(file, subnet_start, subnet_end)

	-- Update the subnet data
	if net.value.defleasetime.value ~= "" then
		net.value.defleasetime.replace = "default-lease-time "..net.value.defleasetime.value
	end
	if net.value.maxleasetime.value ~= "" then
		net.value.maxleasetime.replace = "max-lease-time "..net.value.maxleasetime.value
	end
	if net.value.routers.value ~= "" then
		net.value.routers.replace = "option routers "..net.value.routers.value
	end
	if net.value.domainname.value ~= "" then
		net.value.domainname.replace = 'option domain-name "'..net.value.domainname.value..'"'
	end
	if net.value.domainnameservers.value ~= "" then
		net.value.domainnameservers.replace = "option domain-name-servers "..net.value.domainnameservers.value
	end
	if net.value.leaserangestart.value ~= "" then
		net.value.leaserangestart.replace = "range "..net.value.leaserangestart.value
	end
	if net.value.leaserangeend.value ~= "" then
		net.value.leaserangestart.replace = net.value.leaserangestart.replace.." "..net.value.leaserangeend.value
	end
	if net.value.unknownclients.value ~= "" then
		net.value.unknownclients.replace = net.value.unknownclients.value.." unknown-clients"
	end

	local subnetconfig = parseconfigfile(subnetcontent)
	for i,value in ipairs(subnetconfig or {}) do
		if value[1] == "default-lease-time" then
			subnetcontent = replaceentry(subnetcontent, value, net.value.defleasetime.replace)
			net.value.defleasetime.replace = nil
		elseif value[1] == "max-lease-time" then
			subnetcontent = replaceentry(subnetcontent, value, net.value.maxleasetime.replace)
			net.value.maxleasetime.replace = nil
		elseif value[1] == "option" then
			if value[2] == "routers" then
				subnetcontent = replaceentry(subnetcontent, value, net.value.routers.replace)
				net.value.routers.replace = nil
			elseif value[2] == "domain-name" then
				subnetcontent = replaceentry(subnetcontent, value, net.value.domainname.replace)
				net.value.domainname.replace = nil
			elseif value[2] == "domain-name-servers" then
				subnetcontent = replaceentry(subnetcontent, value, net.value.domainnameservers.replace)
				net.value.domainnameservers.replace = nil
			end
		elseif value[1] == "range" then
			-- We need to steal the dynamic-bootp status
			if value[2] == "dynamic-bootp" and net.value.leaserangestart.replace then
				net.value.leaserangestart.replace = string.gsub(net.value.leaserangestart.replace, "range", "%1 "..value[2])
			end
			-- Need to use a pool if unknownclients defined
			if net.value.unknownclients.replace then
				subnetcontent = replaceentry(subnetcontent, value)
			else
				subnetcontent = replaceentry(subnetcontent, value, net.value.leaserangestart.replace)
				net.value.leaserangestart.replace = nil
			end
		-- We only support one pool per subnet
		elseif value[1] == "pool" then
			for x,y in ipairs(value.sub or {}) do
				if y[2] == "unknown-clients" then
					subnetcontent = replaceentry(subnetcontent, y, net.value.unknownclients.replace)
					net.value.unknownclients.replace = nil
				elseif y[1] == "range" then
					-- We need to steal the dynamic-bootp status
					if y[2] == "dynamic-bootp" and net.value.leaserangestart.replace then
						net.value.leaserangestart.replace = string.gsub(net.value.leaserangestart.replace, "range", "%1 "..y[2])
					end
					subnetcontent = replaceentry(subnetcontent, y, net.value.leaserangestart.replace)
					net.value.leaserangestart.replace = nil
				end
			end
			if net.value.leaserangestart.replace then
				subnetcontent = string.gsub(subnetcontent, "(pool%s*{%s*\n)", "%1"..replacemagiccharacters(net.value.leaserangestart.replace)..";\n")
				net.value.leaserangestart.replace = nil
			end
			if net.value.unknownclients.replace then
				subnetcontent = string.gsub(subnetcontent, "(pool%s*{%s*\n)", "%1"..replacemagiccharacters(net.value.unknownclients.replace)..";\n")
				net.value.unknownclients.replace = nil
			end
		end
	end

	-- add in new lines at the top if they didn't exist
	local newlines = {}
	newlines[#newlines+1] = net.value.defleasetime.replace
	net.value.defleasetime.replace = nil
	newlines[#newlines+1] = net.value.maxleasetime.replace
	net.value.maxleasetime.replace = nil
	newlines[#newlines+1] = net.value.routers.replace
	net.value.routers.replace = nil
	newlines[#newlines+1] = net.value.domainname.replace
	net.value.domainname.replace = nil
	newlines[#newlines+1] = net.value.domainnameservers.replace
	net.value.domainnameservers.replace = nil
	if net.value.leaserangestart.replace and not net.value.unknownclients.replace then
		newlines[#newlines+1] = net.value.leaserangestart.replace
		net.value.leaserangestart.replace = nil
	end
	if #newlines > 0 then
		for i,line in ipairs(newlines) do newlines[i] = "  "..line end
		newlines[#newlines+1] = subnetcontent
		subnetcontent = table.concat(newlines, ";\n")
	end
	if net.value.unknownclients.replace then
		local temp = "  pool {\n    "..net.value.unknownclients.replace..";\n"
		net.value.unknownclients.replace = nil
		if net.value.leaserangestart.replace then
			temp = temp .. "    " .. net.value.leaserangestart.replace .. ";\n"
			net.value.leaserangestart.replace = nil
		end
		subnetcontent = subnetcontent .. temp .. "  }\n"
	end

	-- The subnet is updated, put it into the file
	file = string.sub(file, 1, subnet_start-1) .. subnetcontent .. string.sub(file, subnet_end+1, -1)

	-- Finally, write out the new file
	fs.write_file(configfile, string.gsub(file, "\n*$", ""))
	config = nil
end

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service(action)
	return modelfunctions.startstop_service(processname, action)
end


function getstatus ()
	return modelfunctions.getstatus(processname, packagename, "DHCP Status")
end

create_new_host = function()
	host = { 
	        host         = cfe({ label="Host Name" }),
	        mac          = cfe({ label="MAC Address" }),
	        addresses    = cfe({ label="Fixed Addresses", descr="Comma-separated addresses" }),
	      }
	      
	return cfe({ type="group", value=host, label="Host" })
end

host_read = function( name )
	config = config or parseconfigfile(fs.read_file(configfile) or "")
	local host = create_new_host()
	host.value.host.value = name

	for j,k in ipairs(config) do
		if k[1] == "host" and k[2] == name then
			for i,value in ipairs(k.sub or {}) do
				if value[1] == "hardware" and value[2] == "ethernet" then
					host.value.mac.value = value[3] or ""
				elseif value[1] == "fixed-address" then
					host.value.addresses.value = table.concat(value, " ", 2)
				end
			end
			break
		end
	end

	return host
end

host_update = function( host )
	local success, host = validate_host( host )
	if not host.value.host.errtxt then
		local previous_success = success
		success = false
		host.value.host.errtxt = "This host does not exist"
		local hosts = get_hosts()
		for i,ht in ipairs(hosts.value) do
			if ht == host.value.host.value then
				success = previous_success
				host.value.host.errtxt = nil
				break
			end
		end
	end
	if success then
		host_write(host)
	else
		host.errtxt = "Failed to update host"
	end
	
	return host
end

host_create = function( host )
	local success, host = validate_host(host)
	if not host.value.host.errtxt then
		local hosts = get_hosts()
		for i,ht in ipairs(hosts.value) do
			if ht == host.value.host.value then
				success = false
				host.value.host.errtxt = "This host already exists"
				break
			end
		end
	end
	if success then
		host_write(host)
	else
		host.errtxt = "Failed to create host"
	end

	return host
end

host_delete = function(name)
	local file = fs.read_file(configfile) or ""
	config = config or parseconfigfile(file)
	local cmdresult = cfe({ value="Failed to delete host - not found", label="Delete host result" })
	local hosts = get_hosts()
	for i,host in ipairs(hosts.value) do
		if host == name then
			local start, endd = string.find(file, "host%s*"..replacemagiccharacters(name).."[^{]*{")
			endd = find_section_end(file, endd)
			endd = string.find(file, "\n", endd)
			file = string.sub(file, 1, start-1) .. string.sub(file, endd+1, -1)
			fs.write_file(configfile, string.gsub(file, "\n*$", ""))
			config = nil
			cmdresult.value = "Host Deleted"
		end
	end

	return cmdresult
end

get_hosts = function ()
	config = config or parseconfigfile(fs.read_file(configfile) or "")
	local retval = {}
	for i,entry in ipairs(config) do
		if string.lower(entry[1] or "") == "host" then
			table.insert(retval, entry[2])
		end
	end

	return cfe({ type="list", value=retval, label="Host list" })
end

create_new_subnet = function()
	net = { 
	        subnet       = cfe({ label="Subnet" }),
	        netmask      = cfe({ label="Netmask" }),
	        defleasetime = cfe({ label="Default Lease Time" }),
	        maxleasetime = cfe({ label="Maximum Lease Time" }),
	        routers      = cfe({ label="Routers", descr="Comma-separated addresses" }),
	        domainname   = cfe({ label="Domainname" }),
		domainnameservers = cfe({ label="Domain Name Servers", descr="Comma-separated addresses" }),
	        --wpad         = cfe({ label="Web Proxy Auto Discovery" }),
	        leaserangestart = cfe({ label="Lease Range Start" }),
	        leaserangeend = cfe({ label="Lease Range End" }),
	        unknownclients = cfe({ type="select", label="Unknown Clients", option={"", "allow", "deny"} }),
	      }
	      
	return cfe({ type="group", value=net, label="Subnet" })
end

subnet_read = function( name )
	config = config or parseconfigfile(fs.read_file(configfile) or "")
	local net = create_new_subnet()
	net.value.subnet.value = name
	local pools = 0
	local ranges = 0

	for j,k in ipairs(config) do
		if k[1] == "subnet" and k[2] == name then
			net.value.netmask.value = k[4] or ""
			for i,value in ipairs(k.sub or {}) do
				if value[1] == "default-lease-time" then
					net.value.defleasetime.value = value[2] or ""
				elseif value[1] == "max-lease-time" then
					net.value.maxleasetime.value = value[2] or ""
				elseif value[1] == "option" then
					if value[2] == "routers" then
						net.value.routers.value = table.concat(value, " ", 3)
					elseif value[2] == "domain-name" then
						net.value.domainname.value = string.sub(value[3] or "", 2, -2)
					elseif value[2] == "domain-name-servers" then
						net.value.domainnameservers.value = table.concat(value, " ", 3)
					--elseif value[2] == "local-wpad-server" then
					--	net.value.wpad.value = string.sub(value[3] or "", 2, -2)
					end
				elseif value[1] == "range" then
					ranges = ranges + 1
					if value[2] == "dynamic-bootp" then
						net.value.leaserangestart.value = value[3] or ""
						net.value.leaserangeend.value = value[4] or ""
					else
						net.value.leaserangestart.value = value[2] or ""
						net.value.leaserangeend.value = value[3] or ""
					end
				-- We only support one pool per subnet
				elseif value[1] == "pool" then
					pools = pools + 1
					for x,y in ipairs(value.sub or {}) do
						if y[2] == "unknown-clients" then
							net.value.unknownclients.value = y[1]
						elseif y[1] == "range" then
							ranges = ranges + 1
							if y[2] == "dynamic-bootp" then
								net.value.leaserangestart.value = y[3] or ""
								net.value.leaserangeend.value = y[4] or ""
							else
								net.value.leaserangestart.value = y[2] or ""
								net.value.leaserangeend.value = y[3] or ""
							end
						end
					end
				end
			end
			break
		end
	end
	
	if pools > 1 or ranges > 1 then
		net.value.subnet.errtxt = "Warning! This subnet contains multiple pool/range definitions.  This is not supported by ACF.  Saving may break functionality!"
	end

	return net
end

subnet_update = function( net )
	local success, net = validate_subnet( net )
	if not net.value.subnet.errtxt then
		local previous_success = success
		success = false
		net.value.subnet.errtxt = "This subnet does not exist"
		local subnets = get_subnets()
		for i,subnet in ipairs(subnets.value) do
			if subnet == net.value.subnet.value then
				success = previous_success
				net.value.subnet.errtxt = nil
				break
			end
		end
	end
	if success then
		subnet_write(net)
	else
		net.errtxt = "Failed to update subnet"
	end
	
	return net
end

subnet_create = function( net )
	local success, net = validate_subnet(net)
	if not net.value.subnet.errtxt then
		local subnets = get_subnets()
		for i,subnet in ipairs(subnets.value) do
			if subnet == net.value.subnet.value then
				success = false
				net.value.subnet.errtxt = "This subnet already exists"
				break
			end
		end
	end
	if success then
		subnet_write(net)
	else
		net.errtxt = "Failed to create subnet"
	end

	return net
end

subnet_delete = function(name)
	local file = fs.read_file(configfile) or ""
	config = config or parseconfigfile(file)
	local cmdresult = cfe({ value="Failed to delete subnet - not found", label="Delete subnet result" })
	local subnets = get_subnets()
	for i,subnet in ipairs(subnets.value) do
		if subnet == name then
			local start, endd = string.find(file, "subnet%s*"..replacemagiccharacters(name).."%s*netmask[^{]*{")
			endd = find_section_end(file, endd)
			endd = string.find(file, "\n", endd)
			file = string.sub(file, 1, start-1) .. string.sub(file, endd+1, -1)
			fs.write_file(configfile, string.gsub(file, "\n*$", ""))
			config = nil
			cmdresult.value = "Subnet Deleted"
		end
	end

	return cmdresult
end

get_subnets = function ()
	config = config or parseconfigfile(fs.read_file(configfile) or "")
	local retval = {}
	for i,entry in ipairs(config) do
		if string.lower(entry[1] or "") == "subnet" then
			table.insert(retval, entry[2])
		end
	end

	return cfe({ type="list", value=retval, label="Subnet list" })
end

read_settings = function()
	config = config or parseconfigfile(fs.read_file(configfile) or "")
	local settings = {}
	settings.domainname = cfe({ label="Domain Name" })
	settings.domainnameservers = cfe({ label="Domain Name Servers", descr="Comma-separated addresses" })
	settings.dnsupdatestyle = cfe({ type="select", label="DNS Update Style", option={"none", "ad-hoc", "interim"} })
	settings.defleasetime = cfe({ label="Default Lease Time" })
	settings.maxleasetime = cfe({ label="Maximum Lease Time" })
	for i,value in ipairs(config) do
		if value[1] == "option" then
			if value[2] == "domain-name" then
				settings.domainname.value = string.sub(value[3] or "", 2, -2)
			elseif value[2] == "domain-name-servers" then
				settings.domainnameservers.value = table.concat(value, " ", 3)
			end
		elseif value[1] == "default-lease-time" then
			settings.defleasetime.value = value[2] or ""
		elseif value[1] == "max-lease-time" then
			settings.maxleasetime.value = value[2] or ""
		elseif value[1] == "ddns-update-style" then
			settings.dnsupdatestyle.value = value[2] or ""
		end
	end
	
	return cfe({ type="group", value=settings, label = "Global settings" })
end

update_settings = function ( settings )
	success, settings = validate_settings(settings)
	if success then
		local file = fs.read_file(configfile) or ""
		config = config or parseconfigfile(file)

		-- set up the lines we want to enter
		if settings.value.domainname.value ~= "" then
			settings.value.domainname.replace = 'option domain-name "'..settings.value.domainname.value..'"'
		end
		if settings.value.domainnameservers.value ~= "" then
			settings.value.domainnameservers.replace = "option domain-name-servers "..settings.value.domainnameservers.value
		end
		settings.value.dnsupdatestyle.replace = "ddns-update-style "..settings.value.dnsupdatestyle.value
		if settings.value.defleasetime.value ~= "" then
			settings.value.defleasetime.replace = "default-lease-time "..settings.value.defleasetime.value
		end
		if settings.value.maxleasetime.value ~= "" then
			settings.value.maxleasetime.replace = "max-lease-time "..settings.value.maxleasetime.value
		end

		-- replace existing lines
		for i,value in ipairs(config) do
			if value[1] == "option" then
				if value[2] == "domain-name" then
					file = replaceentry(file, value, settings.value.domainname.replace)
					settings.value.domainname.replace = nil
				elseif value[2] == "domain-name-servers" then
					file = replaceentry(file, value, settings.value.domainnameservers.replace)
					settings.value.domainnameservers.replace = nil
				end
			elseif value[1] == "ddns-update-style" then
				file = replaceentry(file, value, settings.value.dnsupdatestyle.replace)
				settings.value.dnsupdatestyle.replace = nil
			elseif value[1] == "default-lease-time" then
				file = replaceentry(file, value, settings.value.defleasetime.replace)
				settings.value.defleasetime.replace = nil
			elseif value[1] == "max-lease-time" then
				file = replaceentry(file, value, settings.value.maxleasetime.replace)
				settings.value.maxleasetime.replace = nil
			end
		end

		-- add in new lines at the top if they didn't exist
		local newlines = {}
		newlines[#newlines+1] = settings.value.domainname.replace
		settings.value.domainname.replace = nil
		newlines[#newlines+1] = settings.value.domainnameservers.replace
		settings.value.domainnameservers.replace = nil
		newlines[#newlines+1] = settings.value.dnsupdatestyle.replace
		settings.value.dnsupdatestyle.replace = nil
		newlines[#newlines+1] = settings.value.defleasetime.replace
		settings.value.defleasetime.replace = nil
		newlines[#newlines+1] = settings.value.maxleasetime.replace
		settings.value.maxleasetime.replace = nil
		if #newlines > 0 then
			newlines[#newlines+1] = file
			file = table.concat(newlines, ";\n")
		end
		fs.write_file(configfile, string.gsub(file, "\n*$", ""))
		config = nil
	else
		settings.errtxt = "Failed to update global settings"
	end

	return settings
end

listconfigfiles = function()
	local listed_files = {}
	for i,name in ipairs(filelist) do
		local filedetails = fs.stat(name) or {}
		table.insert ( listed_files , {filename=name, mtime=filedetails.mtime or "---", filesize=filedetails.size or "0"} )
	end
	table.sort(listed_files, function (a,b) return (a.filename < b.filename) end )

	return cfe({ type="list", value=listed_files, label="DHCP File List" })
end

getconfigfile = function(filename)
	return modelfunctions.getfiledetails(filename, filelist)
end

setconfigfile = function(filedetails)
	return modelfunctions.setfiledetails(filedetails, filelist)
end

getleases = function()
	return modelfunctions.getfiledetails(leasefile)
end