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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
-- acf model for /etc/dhcp/*
-- Copyright(c) 2007 A. Brodmann - Licensed under terms of GPL2
module (..., package.seeall)
--- get additional libraries
require("validator")
local subnet = {}
local cfgdir = "/etc/dhcp/"
--- the tokenizer functions - must be dislocated into a library later
tokenizer = {}
tokenizer.new = function( str, delim )
local token = {}
token.value = str;
token.delim = delim;
token.pos = 1
return token
end
tokenizer.pos = function( value, substr, pos )
local retval = pos
local done = false
while not done and retval <= #value do
if string.sub( value, retval, retval ) == substr then
done = true
else
retval = retval + 1
end
end
return retval
end
tokenizer.next = function( token )
if token.pos > #token.value then
return token, nil
end
local strpos = tokenizer.pos( token.value, token.delim, token.pos )
retval = string.sub(token.value, token.pos, strpos-1)
if retval == token.delim then
retval = ""
token.pos = token.pos + 1
else
token.pos = strpos + 1
end
return token, retval
end
---
dep_check = function ()
icode = 0
retval = {}
lpos = require "posix"
ptr, msg, code = lpos.access("/etc/dhcp")
if ptr == nil then
table.insert(retval, "/etc/dhcp")
icode = icode + 1
end
ptr, msg, code = lpos.access("/usr/sbin/dhcpd")
if ptr == nil then
table.insert(retval, "/usr/sbin/dhcpd")
icode = icode + 1
end
ptr, msg, code = lpos.access("/etc/init.d/dhcpd")
if ptr == nil then
table.insert(retval, "/etc/init.d/dhcpd")
icode = icode + 1
end
if icode == 0 then
retval = nil
end
return retval
end
config_generate = function()
msg = ""
tmpfilename = os.tmpname()
-- create tmp config file
local tmpfile = io.open( tmpfilename, "w+" )
-- get, validate and write global settings to tmp config file
settings = read_settings()
s_msg, s_fields = validate_settings( settings )
if #s_msg > 0 then
tmpfile:close()
os.remove( tmpfilename )
msg = "Configuration Generation Failed!\n\n" ..
"Reason: Error in Global Settings\n"
return msg
end
tmpfile:write( "authoritative;\n" )
tmpfile:write( "ddns-update-style none;\n\n" )
tmpfile:write( "option local-wpad-server code 252 = text;\n\n" )
config_generate_extconfig( tmpfile, "preconfig" )
if #settings.domainname.value > 0 then
tmpfile:write( "option domain-name \"" .. settings.domainname.value .. "\";\n" )
end
tmpfile:write( "default-lease-time " .. settings.defleasetime.value .. ";\n" )
tmpfile:write( "max-lease-time " .. settings.maxleasetime.value .. ";\n\n" )
-- get, validate and write subnet configurations to tmp config file
tmpfile:write( "###### SUBNET CONFIG BEGIN ######\n\n" )
subnets = get_subnets()
local numnetworks = 0
for k,v in ipairs(subnets) do
numnetworks = numnetworks + 1
net = subnet_read( v )
sn_msg, sn_fields = validate_network( net )
if #sn_msg > 0 then
tmpfile:close()
os.remove( tmpfilename )
msg = "Configuration Generation Failed!\n\n" ..
"Reason: Error in Subnet '" .. v .. "'\n"
return msg
end
tmpfile:write( "# " .. net.name.value .. "\n" )
tmpfile:write( "subnet " .. net.subnet.value .. " netmask " .. net.netmask.value .. " {\n" )
if #net.defleasetime.value > 0 then
tmpfile:write( " default-lease-time " .. net.defleasetime.value .. ";\n" )
end
if #net.maxleasetime.value > 0 then
tmpfile:write( " max-lease-time " .. net.maxleasetime.value .. ";\n" )
end
tmpfile:write( " option routers " .. net.gateway.value .. ";\n" )
dnssrvrs = ""
if #net.dnssrv1.value > 0 then
dnssrvrs = net.dnssrv1.value
end
if #net.dnssrv2.value > 0 then
if #dnssrvrs > 0 then
dnssrvrs = dnssrvrs .. ", " .. net.dnssrv2.value
else
dnssrvrs = net.dnssrv2.value
end
end
if #dnssrvrs > 0 then
tmpfile:write( " option domain-name-servers " .. dnssrvrs .. ";\n" )
end
if #net.domainname.value > 0 then
tmpfile:write( " option domain-name \"" .. net.domainname.value .. "\";\n" )
end
if #net.wpad.value > 0 then
tmpfile:write( " option local-wpad-server \"" .. net.wpad.value .. "\\n\";\n" )
end
spec2_msg = generate_pool( tmpfile, tmpfilename, net )
if #spec2_msg > 0 then
tmpfile:close()
os.remove( tmpfilename )
msg = "Configuration Generation Failed!\n\n" .. spec2_msg
return msg
end
--- generate advanced part / drop in
advancedfile = io.open( cfgdir .. net.name.value .. ".advanced", "r" )
if advancedfile ~= nil then
nxtline = advancedfile:read( "*l" )
while nxtline ~= nil do
tmpfile:write( " " .. nxtline .. "\n" )
nxtline = advancedfile:read( "*l" )
end
advancedfile:close()
end
---
tmpfile:write( "}\n\n" )
end
tmpfile:write( "###### SUBNET CONFIG END ######\n\n" )
if numnetworks <= 0 then
tmpfile:close()
os.remove( tmpfilename )
msg = "Configuration Generation Failed!\n\n" ..
"Reason: No Subnets defined!\n"
return msg
end
msg = generate_hosts( tmpfile )
if #msg > 0 then
tmpfile:close()
os.remove( tmpfilename )
return msg
end
config_generate_extconfig( tmpfile, "postconfig" )
tmpfile:close()
os.rename( tmpfilename, "/etc/dhcp/dhcpd.conf" )
return "Configuration Generation Successful!\n"
end
config_generate_extconfig = function( outfile, postfix )
infile = io.open( cfgdir .. "dhcpd." .. postfix, "r" )
if infile ~= nil then
data = infile:read( "*a" )
if data ~= nil then
outfile:write( "\n" .. data .. "\n" )
end
infile:close()
end
end
generate_pool = function( tmpfile, tmpfilename, net )
if not validator.is_ipv4( net.leaserangestart.value ) or
not validator.is_ipv4( net.leaserangeend.value ) then
if net.unknownclients.value == "allow" then
msg = "Reason: permitted unknown clients but failed to define lease range!\n"
return msg
end
return ""
end
--- pool header
tmpfile:write( " pool {\n" )
if net.unknownclients.value == "allow" then
tmpfile:write( " allow known-clients;\n" )
tmpfile:write( " allow unknown-clients;\n" )
else
tmpfile:write( " allow known-clients;\n" )
tmpfile:write( " deny unknown-clients;\n" )
end
tmpfile:write( " range " .. net.leaserangestart.value .. " " .. net.leaserangeend.value .. ";\n" )
tmpfile:write( " }\n" )
return ""
end
generate_hosts = function( outfile )
retval = ""
outfile:write( "\n####### STATIC HOSTS BEGIN ######\n\n" )
snets = get_subnets()
for k,v in ipairs(snets) do
msg = generate_hosts_persubnet( outfile, v )
if #msg > 0 then
return msg
end
end
retval = generate_hosts_dynamic( outfile )
return retval
end
generate_hosts_persubnet = function( outfile, netname )
retval = ""
hostsfile = io.open( cfgdir .. netname .. ".static", "r" )
if hostsfile ~= nil then
hostsdata = hostsfile:read( "*a" )
if hostsdata ~= nil then
outfile:write( "# " .. netname .. "\n" )
outfile:write( "group {\n" )
local done = false
hosttoken = tokenizer.new( hostsdata, "\n" )
while not done do
hosttoken, nexthost = tokenizer.next( hosttoken )
if nexthost ~= nil then
if string.sub( nexthost, 1, 1) ~= "#" then
spectoken = tokenizer.new( nexthost, ";" )
spectoken, hostname = tokenizer.next( spectoken )
spectoken, ip = tokenizer.next( spectoken )
spectoken, mac = tokenizer.next( spectoken )
spectoken, comment = tokenizer.next( spectoken )
outfile:write(" host " .. hostname .. " {\n")
outfile:write(" hardware ethernet " .. mac .. ";\n")
outfile:write(" fixed-address " .. ip .. ";\n")
outfile:write(" }\n")
end
else
done = true
end
end
outfile:write( "}\n\n" )
outfile:write( "###### STATIC HOSTS END ######\n\n" )
else
retval = "Configuration Generation Failed: Failed to read data from subnet static hosts file for " .. netname
end
hostsfile:close()
end
return retval
end
generate_hosts_dynamic = function( outfile )
retval = ""
hostsfile = io.open( cfgdir .. "dhcpd.dynamic", "r" )
if hostsfile ~= nil then
hostsdata = hostsfile:read( "*a" )
if hostsdata ~= nil then
outfile:write( "group {\n" )
local done = false
hosttoken = tokenizer.new( hostsdata, "\n" )
while not done do
hosttoken, nexthost = tokenizer.next( hosttoken )
if nexthost ~= nil then
if string.sub( nexthost, 1, 1) ~= "#" then
spectoken = tokenizer.new( nexthost, ";" )
spectoken, hostname = tokenizer.next( spectoken )
spectoken, mac = tokenizer.next( spectoken )
spectoken, comment = tokenizer.next( spectoken )
outfile:write(" host " .. hostname .. " {\n")
outfile:write(" hardware ethernet " .. mac .. ";\n")
outfile:write(" }\n")
end
else
done = true
end
end
outfile:write( "}\n" )
else
retval = "Configuration Generation Failed: Failed to read data from dynamic hosts file!"
end
end
return retval
end
generate_hosts_old = function( tmpfile, tmpfilename, net )
--- generate dynamic hosts
dynamichostsfile = io.open( cfgdir .. net.name.value .. ".dynamic", "r" )
if dynamichostsfile ~= nil then
dynamichosts = dynamichostsfile:read( "*a" )
if dynamichosts == nil then
dynamichostsfile:close()
msg = "Configuration Generation Failed!\n\n" ..
"Reason: failed to read dynamic hosts file for '" .. net.name.value .. "'"
return msg
end
msg = validate_dynamichosts( dynamichosts )
--- loop through all hosts
done = false
hosttoken = tokenizer.new( dynamichosts, "\n" )
while not done do
hosttoken, nexthost = tokenizer.next( hosttoken )
if nexthost ~= nil then
if string.sub( nexthost, 1, 1) ~= "#" then
spectoken = tokenizer.new( nexthost, ";" )
spectoken, hostname = tokenizer.next( spectoken )
spectoken, mac = tokenizer.next( spectoken )
spectoken, comment = tokenizer.next( spectoken )
tmpfile:write(" host " .. hostname .. " {\n")
tmpfile:write(" hardware ethernet " .. mac .. ";\n")
tmpfile:write(" }\n")
end
else
done = true
end
end
dynamichostsfile:close()
end
---
return ""
end
subnet_delete = function( name )
local msg = ""
local filename = cfgdir .. name
os.remove( filename .. ".subnet" )
os.remove( filename .. ".static" )
os.remove( filename .. ".dynamic" )
return msg
end
advglobal_read = function()
preconfig = ""
postconfig = ""
dynamic = ""
file = io.open( cfgdir .. "dhcpd.preconfig", "r" )
if file ~= nil then
preconfig = file:read( "*a" )
if preconfig == nil then
preconfig = ""
end
file:close()
end
file = io.open( cfgdir .. "dhcpd.postconfig", "r" )
if file ~= nil then
postconfig = file:read( "*a" )
if postconfig == nil then
postconfig = ""
end
file:close()
end
file = io.open( cfgdir .. "dhcpd.dynamic", "r" )
if file ~= nil then
dynamic = file:read( "*a" )
if dynamic == nil then
dynamic = ""
end
file:close()
end
return cfe({ preconfig = preconfig, postconfig = postconfig, dynamic = dynamic })
end
advglobal_update = function( preconfig, postconfig, dynamic )
file = io.open( cfgdir .. "dhcpd.preconfig", "wb+" )
if file ~= nil then
file:write( preconfig )
file:close()
end
file = io.open( cfgdir .. "dhcpd.postconfig", "wb+" )
if file ~= nil then
file:write( postconfig )
file:close()
end
file = io.open( cfgdir .. "dhcpd.dynamic", "wb+" )
if file ~= nil then
file:write( dynamic )
file:close()
end
return cfe({ preconfig = preconfig, postconfig = postconfig, dynamic = dynamic })
end
subnet_read = function( name )
local filename = cfgdir .. name .. ".subnet"
local net = create_new_net( name, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil )
for line in io.lines(filename) do
if (string.sub(line, 1, 15) == "def-lease-time:") then
net.defleasetime.value = string.sub(line, 17)
elseif (string.sub(line, 1, 15) == "max-lease-time:") then
net.maxleasetime.value = string.sub(line, 17)
elseif (string.sub(line, 1, 8) == "gateway:") then
net.gateway.value = string.sub(line, 10)
elseif (string.sub(line, 1, 12) == "domain-name:") then
net.domainname.value = string.sub(line, 14)
elseif (string.sub(line, 1, 10) == "dns-srv-1:") then
net.dnssrv1.value = string.sub(line, 12)
elseif (string.sub(line, 1, 10) == "dns-srv-2:") then
net.dnssrv2.value = string.sub(line, 12)
elseif (string.sub(line, 1, 7) == "subnet:") then
net.subnet.value = string.sub(line, 9)
elseif (string.sub(line, 1, 8) == "netmask:") then
net.netmask.value = string.sub(line, 10)
elseif (string.sub(line, 1, 18) == "lease-range-start:") then
net.leaserangestart.value = string.sub(line, 20)
elseif (string.sub(line, 1, 16) == "lease-range-end:") then
net.leaserangeend.value = string.sub(line, 18)
elseif (string.sub(line, 1, 5) == "wpad:") then
net.wpad.value = string.sub(line, 7)
elseif (string.sub(line, 1, 16) == "unknown-clients:") then
net.unknownclients.value = string.sub(line, 18)
end
end
if net.unknownclients.value ~= "allow" then
net.unknownclients.value = "deny"
end
net.statichosts.value = subnet_get_spechosts( name, "static" )
net.advanced.value = subnet_get_spechosts( name, "advanced" )
return net
end
subnet_get_spechosts = function( name, suffix )
local retval = ""
local filename = cfgdir .. name .. "." .. suffix
if file_exists( filename ) then
local file = io.open( filename, "r" )
if file ~= nil then
msg = file:read( "*a" )
if msg ~= nil then
retval = msg
end
file:close()
end
end
return retval
end
subnet_update_statichosts = function( name, statichosts )
local msg = "";
local filename = cfgdir .. name .. ".static"
file, errmsg = io.open( filename, "wb+" )
if file == nil then
msg = "Error: Failed to open " .. filename .. "(" .. errmsg .. ")!"
else
file:write( statichosts )
file:close()
end
return msg
end
subnet_update_advanced = function( name, advanced )
local msg = "";
local filename = cfgdir .. name .. ".advanced"
file, errmsg = io.open( filename, "wb+" )
if file == nil then
msg = "Error: Failed to open " .. filename .. "(" .. errmsg .. ")!"
else
file:write( advanced )
file:close()
end
return msg
end
read_settings = function()
local filename = cfgdir .. "globalsettings.conf"
local settings = create_new_settings( nil, nil, nil )
if file_exists( filename ) then
for line in io.lines(filename) do
if (string.sub(line, 1, 15) == "def-lease-time:") then
settings.defleasetime.value = string.sub(line, 17)
elseif (string.sub(line, 1, 15) == "max-lease-time:") then
settings.maxleasetime.value = string.sub(line, 17)
elseif (string.sub(line, 1, 12) == "domain-name:") then
settings.domainname.value = string.sub(line, 14)
end
end
end
return settings
end
subnet_write = function( net )
msg, fields = validate_network( net )
if #msg > 0 then
return cfe({ msg = msg, fields = fields }), net
end
local filename = cfgdir .. net.name.value .. ".subnet"
local file = io.open( filename, "w+" )
file:write( "def-lease-time: " .. net.defleasetime.value .. "\n" )
file:write( "max-lease-time: " .. net.maxleasetime.value .. "\n" )
file:write( "gateway: " .. net.gateway.value .. "\n" )
file:write( "domain-name: " .. net.domainname.value .. "\n" )
file:write( "dns-srv-1: " .. net.dnssrv1.value .. "\n" )
file:write( "dns-srv-2: " .. net.dnssrv2.value .. "\n" )
file:write( "subnet: " .. net.subnet.value .. "\n" )
file:write( "netmask: " .. net.netmask.value .. "\n" )
file:write( "lease-range-start: " .. net.leaserangestart.value .. "\n" )
file:write( "lease-range-end: " .. net.leaserangeend.value .. "\n" )
file:write( "wpad: " .. net.wpad.value .. "\n" )
file:write( "unknown-clients: " .. net.unknownclients.value .. "\n" )
file:close()
spec_msg = validate_statichosts( net.statichosts.value )
if #spec_msg == 0 then
spec_msg = subnet_update_statichosts( net.name.value, net.statichosts.value )
if #spec_msg > 0 then
msg = spec_msg
table.insert( fields, "statichosts" )
end
else
msg = spec_msg
table.insert( fields, "statichosts" )
end
spec_msg = subnet_update_advanced( net.name.value, net.advanced.value )
if #spec_msg > 0 then
msg = spec_msg
table.insert( fields, "advanced" )
end
return cfe({ msg = msg, fields = {}}), net
end
validate_statichosts = function( statichosts )
local line = 1
local msg = ""
local done = false
hosttoken = tokenizer.new( statichosts, "\n")
while not done do
hosttoken, nexthost = tokenizer.next( hosttoken )
if nexthost ~= nil then
if string.sub(nexthost, 1, 1) ~= "#" then
fieldtoken = tokenizer.new( nexthost, ";")
fieldtoken, hostname = tokenizer.next( fieldtoken )
fieldtoken, ip = tokenizer.next( fieldtoken )
fieldtoken, mac = tokenizer.next( fieldtoken )
fieldtoken, comment = tokenizer.next( fieldtoken )
if hostname == nil then
msg = msg .. "Static Hosts: hostname missing on line " .. line .. "!\n"
else
if not is_valid_hostname( hostname ) then
msg = msg .. "Static Hosts: Invalid hostname on line " .. line .. "!\n"
end
end
if ip == nil then
msg = msg .. "Static Hosts: ip missing on line " .. line .. "!\n"
else
if not validator.is_ipv4( ip ) then
msg = msg .. "Static Hosts: Invalid ip on line " .. line .. "!\n"
end
end
if mac == nil then
msg = msg .. "Static Hosts: mac missing on line " .. line .. "!\n"
else
if not validator.is_mac( mac ) then
msg = msg .. "Static Hosts: Invalid mac on line " .. line .. "!\n"
end
end
end
line = line + 1
else
done = true
end
end
return msg
end
validate_dynamichosts = function( dynamichosts )
local line = 1
local msg = ""
local done = false
hosttoken = tokenizer.new( dynamichosts, "\n")
while not done do
hosttoken, nexthost = tokenizer.next( hosttoken )
if nexthost ~= nil then
if string.sub(nexthost, 1, 1) ~= "#" then
fieldtoken = tokenizer.new( nexthost, ";")
fieldtoken, hostname = tokenizer.next( fieldtoken )
fieldtoken, mac = tokenizer.next( fieldtoken )
fieldtoken, comment = tokenizer.next( fieldtoken )
if hostname == nil then
msg = msg .. "Dynamic Hosts: hostname missing on line " .. line .. "!\n"
else
if not is_valid_hostname( hostname ) then
msg = msg .. "Dynamic Hosts: Invalid hostname on line " .. line .. "!\n"
end
end
if mac == nil then
msg = msg .. "Dynamic Hosts: mac missing on line " .. line .. "!\n"
else
if not validator.is_mac( mac ) then
msg = msg .. "Dynamic Hosts: Invalid mac on line " .. line .. "!\n"
end
end
end
line = line + 1
else
done = true
end
end
return msg
end
update_settings = function ( settings )
msg, fields = validate_settings ( settings )
if #msg > 0 then
return cfe({ msg = msg, fields = fields }), settings
end
local filename = cfgdir .. "globalsettings.conf"
local file = io.open( filename, "w+" )
file:write( "def-lease-time: " .. settings.defleasetime.value .. "\n" )
file:write( "max-lease-time: " .. settings.maxleasetime.value .. "\n" )
file:write( "domain-name: " .. settings.domainname.value .. "\n" )
return cfe({ msg = "", fields = {}}), settings
end
subnet_create = function( net )
if file_exists( cfgdir .. net.name.value .. ".subnet" ) then
return cfe({ msg = "This subnet already exists!", fields = {}}), net
end
retcode, net = subnet_write( net )
return retcode, net
end
_tonumber = function( value )
ret = tonumber( value )
if (ret == nil) then
ret = 0
end
return ret
end
validate_network = function( net )
fields = {}
msg = ""
if #net.name.value < 3 then
table.insert(fields, "name")
msg = msg .. "Minimum network name length is 3 characters!\n"
end
if not is_valid_netname( net.name.value ) then
table.insert( fields, "name" )
msg = msg .. "Invalid network name: allowed characters are: 'a..z', '0..9', '-'\n"
end
if net.name.value == "<new>" then
table.insert(fields, "name")
msg = msg .. "<new> is not a valid network name!\n"
end
if #net.defleasetime.value > 0 then
if not validator.is_integer_in_range(_tonumber(net.defleasetime.value), 1800, 86400) then
table.insert(fields, "defleasetime")
msg = msg .. "Default-Lease-Time must be: 1800 < x < 86400\n"
end
end
if #net.maxleasetime.value > 0 then
if not validator.is_integer_in_range(_tonumber(net.maxleasetime.value), 1800, 86400) then
table.insert(fields, "maxleasetime")
msg = msg .. "Maximum-Lease-Time must be: 1800 < x < 86400\n"
end
end
if not validator.is_ipv4(net.gateway.value) then
table.insert(fields, "gateway")
msg = msg .. "Gateway: invalid IPv4 address!\n"
end
if #net.dnssrv1.value > 0 then
if not validator.is_ipv4(net.dnssrv1.value) then
table.insert(fields, "dnssrv1")
msg = msg .. "DNS Server 1: invalid IPv4 address!\n"
end
end
if not validator.is_ipv4(net.dnssrv2.value) then
if #net.dnssrv2.value > 0 then
table.insert(fields, "dnssrv2")
msg = msg .. "DNS Server 2: invalid IPv4 address!\n"
end
end
if not validator.is_ipv4(net.subnet.value) then
table.insert(fields, "subnet")
msg = msg .. "Subnet: invalid IPv4 address!\n"
end
if not validator.is_ipv4(net.netmask.value) then
table.insert(fields, "netmask")
msg = msg .. "Netmask: invalid IPv4 address!\n"
end
if #net.leaserangestart.value > 0 then
if not validator.is_ipv4(net.leaserangestart.value) then
table.insert(fields, "leaserangestart")
msg = msg .. "Lease-Range-Start: invalid IPv4 address!\n"
end
end
if #net.leaserangeend.value > 0 then
if not validator.is_ipv4(net.leaserangeend.value) then
table.insert(fields, "leaserangeend")
msg = msg .. "Lease-Range-End: invalid IPv4 address!\n"
end
end
return msg, fields
end
file_exists = function( filename )
retval = false
lpos = require "posix"
ptr, msg, code = lpos.access( filename )
if ptr ~= nil then
retval = true
end
return retval
end
read_file = function ( filename )
local contents = ""
local line = ""
local file = io.open( filename, "r" )
if file ~= nil then
line = file:read( "*l" )
while line ~= nil do
contents = contents .. "\n" .. line
line = file:read( "*l" )
end
file:close()
else
contents = "\n Error: File not found!\n\n"
end
return contents
end
is_running = function( process )
local retval = false
local file = io.popen("pidof " .. process)
if file ~= nil then
local line = file:read( "*l" )
file:close()
if #line > 0 then
retval = true
end
end
return retval
end
get_dhcpd_version = function()
local retval = "dhcpd"
local file = io.popen("/usr/sbin/dhcpd --version 2>&1")
if file ~= nil then
local line = file:read( "*a" )
if #line > 0 then
retval = line
end
file:close()
end
return retval
end
service_control = function ( command )
local retval = ""
local line = ""
local file = io.popen( "/etc/init.d/dhcpd " .. command .. " 2>&1" )
if file ~= nil then
line = file:read( "*l" )
while line ~= nil do
retval = retval .. "\n" .. line
line = file:read( "*l" )
end
file:close()
end
return retval
end
function nonil( value )
local retval = ""
if value ~= nil then
retval = value
end
return retval
end
get_subnets = function ()
local retval = {}
lpos = require "posix"
files = lpos.dir( "/etc/dhcp" )
for k,v in ipairs(files) do
if string.sub(v, -7) == ".subnet" then
table.insert(retval, string.sub(v, 1, -8))
end
end
return retval
end
create_new_net = function( name, defleasetime, maxleasetime, gateway, domainname, dnssrv1, dnssrv2, subnet, netmask, leaserangestart, leaserangeend, wpad, statichosts, unknownclients, dynamichosts, advanced, useadvanced )
net = { name = { label="Name", value=nonil(name), type="message" },
defleasetime = { label="Default Lease Time", value=nonil(defleasetime), type="text" },
maxleasetime = { label="Maximum Lease Time", value=nonil(maxleasetime), type="text" },
gateway = { label="Gateway", value=nonil(gateway), type="text" },
domainname = { label="Domainname", value=nonil(domainname), type="text" },
dnssrv1 = { label="DNS Server 1", value=nonil(dnssrv1), type="text" },
dnssrv2 = { label="DNS Server 2", value=nonil(dnssrv2), type="text" },
subnet = { label="Subnet", value=nonil(subnet), type="text" },
netmask = { label="Netmask", value=nonil(netmask), type="text" },
leaserangestart = { label="Lease Range Start", value=nonil(leaserangestart), type="text" },
leaserangeend = { label="Lease Range End", value=nonil(leaserangeend), type="text" },
wpad = { label="Web Proxy Auto Discovery", value=nonil(wpad), type="text" },
statichosts = { label="Static Hosts", value=nonil(statichosts), type="text" },
dynamichosts = { label="Dynamic Hosts", value=nonil(dynamichosts), type="text" },
unknownclients = { label="Unknown Clients", value=nonil(unknownclients), type="text" },
advanced = { label="Advanced", value=nonil(advanced), type="text" },
useadvanced = { label="Use Advanced", value=nonil(useadvanced), type="text" }
}
if net.unknownclients.value ~= "allow" then
net.unknownclients.value = "deny"
end
return net
end
create_new_settings = function( defleasetime, maxleasetime, domainname )
settings = { domainname = { label="Domainname", type="text", value=nonil(domainname) },
defleasetime = { label="Default Lease Time", type="text", value=nonil(defleasetime) },
maxleasetime = { label="Maximum Lease Time", type="text", value=nonil(maxleasetime) }
}
return settings
end
validate_settings = function ( settings )
msg = ""
fields = {}
if not validator.is_integer_in_range(_tonumber(settings.defleasetime.value), 1800, 86400) then
msg = msg .. "Default Lease Time: Out of range 1800 < x < 86400 or not integer\n"
table.insert( fields, "defleasetime" )
end
if not validator.is_integer_in_range(_tonumber(settings.maxleasetime.value), 1800, 86400) then
msg = msg .. "Maximum Lease Time: Out of range 1800 < x < 86400 or not integer\n"
table.insert( fields, "maxleasetime" )
end
if not is_valid_hostname( settings.domainname.value ) then
if #settings.domainname.value > 0 then
msg = msg .. "Invalid domainname: valid chars are 'a..z', '0..9', '.', '-'\n"
table.insert( fields, "domainname" )
end
end
return msg, fields
end
is_valid_hostname = function ( hostname )
local retval = true
name = string.lower( hostname )
lap = 1
while lap <= #name do
chr = string.sub(name, lap, lap)
if (chr >= "a" and chr <= "z") or
(chr >= "0" and chr <= "9") or
(chr == ".") or (chr == "-") then
else
retval = false
end
lap = lap + 1
end
return retval
end
is_valid_netname = function ( netname )
local retval = true
name = string.lower( netname )
lap = 1
while lap <= #name do
chr = string.sub( name, lap, lap )
if (chr >= "a" and chr <= "z") or
(chr >= "0" and chr <= "9") or
(chr == "-") then
else
retval = false
end
lap = lap + 1
end
return retval
end
|