]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/dhcp.cgi
Merge remote-tracking branch 'dweismueller/vdr_vnsiserver5_new' into next
[ipfire-2.x.git] / html / cgi-bin / dhcp.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 #workaround to suppress a warning when a variable is used only once
32 my @dummy = ( ${Header::colouryellow} );
33 undef (@dummy);
34
35 our %dhcpsettings=();
36 our %netsettings=();
37 my %mainsettings=();
38 my %timesettings=();
39 my $setting = "${General::swroot}/dhcp/settings";
40 our $filename1 = "${General::swroot}/dhcp/advoptions"; # Field separator is TAB in this file (comma is standart)
41 # because we need commas in the some data
42 our $filename2 = "${General::swroot}/dhcp/fixleases";
43 our $filename3 = "${General::swroot}/dhcp/advoptions-list"; # Describe the allowed syntax for dhcp options
44 my $errormessage = '';
45 my $warnNTPmessage = '';
46 my @nosaved=();
47 my %color = ();
48
49 #Basic syntax allowed for new Option definition. Not implemented: RECORDS & array of RECORDS
50 our $OptionTypes = 'boolean|((un)?signed )?integer (8|16|32)|ip-address|text|string|encapsulate \w+|array of ip-address';
51
52 &Header::showhttpheaders();
53 our @ITFs=('GREEN');
54 if (&Header::blue_used()){push(@ITFs,'BLUE');}
55
56 #Settings1 for the first screen box
57 foreach my $itf (@ITFs) {
58 $dhcpsettings{"ENABLE_${itf}"} = 'off';
59 $dhcpsettings{"ENABLEBOOTP_${itf}"} = 'off';
60 $dhcpsettings{"START_ADDR_${itf}"} = '';
61 $dhcpsettings{"END_ADDR_${itf}"} = '';
62 $dhcpsettings{"DOMAIN_NAME_${itf}"} = '';
63 $dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} = '';
64 $dhcpsettings{"MAX_LEASE_TIME_${itf}"} = '';
65 $dhcpsettings{"WINS1_${itf}"} = '';
66 $dhcpsettings{"WINS2_${itf}"} = '';
67 $dhcpsettings{"DNS1_${itf}"} = '';
68 $dhcpsettings{"DNS2_${itf}"} = '';
69 $dhcpsettings{"NTP1_${itf}"} = '';
70 $dhcpsettings{"NTP2_${itf}"} = '';
71 $dhcpsettings{"NEXT_${itf}"} = '';
72 $dhcpsettings{"FILE_${itf}"} = '';
73 }
74
75 $dhcpsettings{'SORT_FLEASELIST'} = 'FIPADDR';
76 $dhcpsettings{'SORT_LEASELIST'} = 'IPADDR';
77
78 #Settings2 for editing the multi-line list
79 #Must not be saved with writehash !
80 $dhcpsettings{'FIX_MAC'} = '';
81 $dhcpsettings{'FIX_ADDR'} = '';
82 $dhcpsettings{'FIX_ENABLED'} = 'off';
83 $dhcpsettings{'FIX_NEXTADDR'} = '';
84 $dhcpsettings{'FIX_FILENAME'} = '';
85 $dhcpsettings{'FIX_ROOTPATH'} = '';
86 $dhcpsettings{'FIX_REMARK'} = '';
87 $dhcpsettings{'ACTION'} = '';
88 $dhcpsettings{'KEY1'} = '';
89 $dhcpsettings{'KEY2'} = '';
90 @nosaved=('FIX_MAC','FIX_ADDR','FIX_ENABLED','FIX_NEXTADDR',
91 'FIX_FILENAME','FIX_ROOTPATH','FIX_REMARK');
92
93 $dhcpsettings{'ADVOPT_ENABLED'} = '';
94 $dhcpsettings{'ADVOPT_NAME'} = '';
95 $dhcpsettings{'ADVOPT_DATA'} = '';
96 unshift (@nosaved,'ADVOPT_ENABLED','ADVOPT_NAME','ADVOPT_DATA');
97 foreach my $itf (@ITFs) {
98 $dhcpsettings{"ADVOPT_SCOPE_${itf}"} = 'off';
99 unshift (@nosaved, "ADVOPT_SCOPE_${itf}");
100 }
101
102 # Read Ipcop settings
103 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
104 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
105 &General::readhash("${General::swroot}/time/settings", \%timesettings);
106 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
107
108 #Get GUI values
109 &Header::getcgihash(\%dhcpsettings);
110
111 open(FILE, "$filename1") or die 'Unable to open dhcp advanced options file.';
112 our @current1 = <FILE>;
113 close(FILE);
114 # Extract OptionDefinition
115 foreach my $line (@current1) {
116 #chomp($line); # remove newline #don't know why, but this remove newline in @current1 .... !
117 my @temp = split(/\t/,$line);
118 AddNewOptionDefinition ($temp[1] . ' ' . $temp[2]);
119 }
120
121 open(FILE, "$filename2") or die 'Unable to open fixed leases file.';
122 our @current2 = <FILE>;
123 close(FILE);
124
125 # Check Settings1 first because they are needed by &buildconf
126 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'save'}) {
127 foreach my $itf (@ITFs) {
128 if ($dhcpsettings{"ENABLE_${itf}"} eq 'on' ) {
129 # "Start" is defined, need "End" and vice versa
130 if ($dhcpsettings{"START_ADDR_${itf}"}) {
131 if (!(&General::validip($dhcpsettings{"START_ADDR_${itf}"}))) {
132 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid start address'};
133 goto ERROR;
134 }
135 if (!$dhcpsettings{"END_ADDR_${itf}"}) {
136 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid end address'};
137 goto ERROR;
138 }
139 if (! &General::IpInSubnet ( $dhcpsettings{"START_ADDR_${itf}"},
140 $netsettings{"${itf}_NETADDRESS"},
141 $netsettings{"${itf}_NETMASK"})) {
142 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid start address'};
143 goto ERROR;
144 }
145 }
146
147 if ($dhcpsettings{"END_ADDR_${itf}"}) {
148 if (!(&General::validip($dhcpsettings{"END_ADDR_${itf}"}))) {
149 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid end address'};
150 goto ERROR;
151 }
152 if (!$dhcpsettings{"START_ADDR_${itf}"}) {
153 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid start address'};
154 goto ERROR;
155 }
156 if (! &General::IpInSubnet ( $dhcpsettings{"END_ADDR_${itf}"},
157 $netsettings{"${itf}_NETADDRESS"},
158 $netsettings{"${itf}_NETMASK"})) {
159 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid end address'};
160 goto ERROR;
161 }
162 #swap if necessary! (support 255.255.0.0 range, I doubt we need more) GE
163 my @startoct = split (/\./, $dhcpsettings{"START_ADDR_${itf}"});
164 my @endoct = split (/\./, $dhcpsettings{"END_ADDR_${itf}"});
165 if ( $endoct[2]*256+$endoct[3] < $startoct[2]*256+$startoct[3] ) {
166 ($dhcpsettings{"START_ADDR_${itf}"},$dhcpsettings{"END_ADDR_${itf}"}) =
167 ($dhcpsettings{"END_ADDR_${itf}"},$dhcpsettings{"START_ADDR_${itf}"});
168 }
169 }
170
171 if (!($dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} =~ /^\d+$/)) {
172 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid default lease time'} . $dhcpsettings{'DEFAULT_LEASE_TIME_${itf}'};
173 goto ERROR;
174 }
175
176 if (!($dhcpsettings{"MAX_LEASE_TIME_${itf}"} =~ /^\d+$/)) {
177 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid max lease time'} . $dhcpsettings{'MAX_LEASE_TIME_${itf}'};
178 goto ERROR;
179 }
180
181 if ($dhcpsettings{"DNS1_${itf}"}) {
182 if (!(&General::validip($dhcpsettings{"DNS1_${itf}"}))) {
183 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid primary dns'};
184 goto ERROR;
185 }
186 }
187 if ($dhcpsettings{"DNS2_${itf}"}) {
188 if (!(&General::validip($dhcpsettings{"DNS2_${itf}"}))) {
189 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid secondary dns'};
190 goto ERROR;
191 }
192 if (! $dhcpsettings{"DNS1_${itf}"}) {
193 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'cannot specify secondary dns without specifying primary'};
194 goto ERROR;
195 }
196 }
197
198 if ($dhcpsettings{"WINS1_${itf}"}) {
199 if (!(&General::validip($dhcpsettings{"WINS1_${itf}"}))) {
200 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid wins address'};
201 goto ERROR;
202 }
203 }
204 if ($dhcpsettings{"WINS2_${itf}"}) {
205 if (!(&General::validip($dhcpsettings{"WINS2_${itf}"}))) {
206 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid wins address'};
207 goto ERROR;
208 }
209 if (! $dhcpsettings{"WINS1_${itf}"} ) {
210 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'cannot specify secondary wins without specifying primary'};
211 goto ERROR;
212 }
213 }
214 if ($dhcpsettings{"NEXT_${itf}"}) {
215 if (!(&General::validip($dhcpsettings{"NEXT_${itf}"}))) {
216 $errormessage = "next-server on ${itf}: " . $Lang::tr{'invalid ip'};
217 goto ERROR;
218 }
219 }
220 if ($dhcpsettings{"NTP1_${itf}"}) {
221 if (!(&General::validip($dhcpsettings{"NTP1_${itf}"}))) {
222 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid primary ntp'};
223 goto ERROR;
224 }
225 if ($dhcpsettings{"NTP1_${itf}"} eq $netsettings{"${itf}_ADDRESS"} && ($timesettings{'ENABLECLNTP'} ne 'on')) {
226 $warnNTPmessage = "DHCP on ${itf}: " . $Lang::tr{'local ntp server specified but not enabled'};
227 #goto ERROR;
228 }
229 }
230 if ($dhcpsettings{"NTP2_${itf}"}) {
231 if (!(&General::validip($dhcpsettings{"NTP2_${itf}"}))) {
232 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid secondary ntp'};
233 goto ERROR;
234 }
235 if ($dhcpsettings{"NTP2_${itf}"} eq $netsettings{"${itf}_ADDRESS"} && ($timesettings{'ENABLECLNTP'} ne 'on')) {
236 $warnNTPmessage = "DHCP on ${itf}: " . $Lang::tr{'local ntp server specified but not enabled'};
237 #goto ERROR;
238 }
239 if (! $dhcpsettings{"NTP1_${itf}"}) {
240 $errormessage = "DHCP on ${itf}: " . $Lang::tr{'cannot specify secondary ntp without specifying primary'};
241 goto ERROR;
242 }
243 }
244 } # enabled
245 }#loop interface verify
246
247 map (delete ($dhcpsettings{$_}) ,@nosaved,'ACTION','KEY1','KEY2'); # Must not be saved
248 &General::writehash($setting, \%dhcpsettings); # Save good settings
249 $dhcpsettings{'ACTION'} = $Lang::tr{'save'}; # create an 'ACTION'
250 map ($dhcpsettings{$_} = '',@nosaved,'KEY1','KEY2'); # and reinit vars to empty
251 &buildconf;
252 ERROR: # Leave the faulty field untouched
253 } else {
254 &General::readhash($setting, \%dhcpsettings); # Get saved settings and reset to good if needed
255 }
256
257 ## Sorting of fixed leases
258 if ($ENV{'QUERY_STRING'} =~ /^FETHER|^FIPADDR/ ) {
259 my $newsort=$ENV{'QUERY_STRING'};
260 my $act=$dhcpsettings{'SORT_FLEASELIST'};
261 #Reverse actual sort ?
262 if ($act =~ $newsort) {
263 my $Rev='';
264 if ($act !~ 'Rev') {
265 $Rev='Rev';
266 }
267 $newsort.=$Rev;
268 }
269 $dhcpsettings{'SORT_FLEASELIST'}=$newsort;
270 map (delete ($dhcpsettings{$_}) ,@nosaved,'ACTION','KEY1','KEY2'); # Must never be saved
271 &General::writehash($setting, \%dhcpsettings);
272 &sortcurrent2;
273 $dhcpsettings{'ACTION'} = 'SORT'; # create an 'ACTION'
274 map ($dhcpsettings{$_} = '',@nosaved,'KEY1','KEY2');# and reinit vars to empty
275 }
276
277 #Sorting of allocated leases
278 &Header::CheckSortOrder;
279
280
281 ## Now manipulate the two multi-line list with Settings2.
282 # '1' suffix is for ADVANCED OPTIONS
283 # '2' suffix is for FIXED LEASES
284
285 # Toggle enable/disable field on specified options.
286
287 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'toggle enable disable'}.'1') {
288 #move out new line
289 chomp(@current1[$dhcpsettings{'KEY1'}]);
290 my @temp = split(/\t/,@current1[$dhcpsettings{'KEY1'}]); #use TAB separator !
291 $temp[0] = $temp[0] eq 'on' ? '' : 'on'; # Toggle the field
292 @current1[$dhcpsettings{'KEY1'}] = join ("\t",@temp)."\n";
293 $dhcpsettings{'KEY1'} = ''; # End edit mode
294 &General::log($Lang::tr{'dhcp advopt modified'});
295 open(FILE, ">$filename1") or die 'Unable to open dhcp advanced options file.';
296 print FILE @current1;
297 close(FILE);
298
299 #Write changes to dhcpd.conf.
300 &buildconf;
301 }
302
303
304
305 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'add'}.'1' &&
306 $dhcpsettings{'SUBMIT'} ne $Lang::tr{'dhcp advopt help'}) {
307 $dhcpsettings{'ADVOPT_NAME'} =~ s/[^ \w-]//g; # prevent execution of code by removing everything except letters/space
308 $dhcpsettings{'ADVOPT_DATA'} =~ s/`//g; # back tik ` ? not allowed !
309
310 if ($dhcpsettings{'ADVOPT_DATA'} eq '') {
311 $errormessage=$Lang::tr{'dhcp advopt blank value'};
312 }
313
314 # Test for a new option definition string (join field name & data)
315 if (ExistNewOptionDefinition ($dhcpsettings{'ADVOPT_NAME'} . ' ' . $dhcpsettings{'ADVOPT_DATA'})) {
316 #only edit permitted if option definition exists
317 $errormessage = $Lang::tr{'dhcp advopt definition exists'} if ($dhcpsettings{'KEY1'} eq '');
318 $dhcpsettings{'ADVOPT_ENABLED'} = 'on'; # force active
319 map ($dhcpsettings{"ADVOPT_SCOPE_$_"} = 'off', @ITFs); # force global
320 } elsif (AddNewOptionDefinition ($dhcpsettings{'ADVOPT_NAME'} . ' ' . $dhcpsettings{'ADVOPT_DATA'})) {
321 #was a new option definition
322 $dhcpsettings{'ADVOPT_ENABLED'} = 'on'; # force active
323 map ($dhcpsettings{"ADVOPT_SCOPE_$_"} = 'off', @ITFs); # force global
324 } elsif (ValidNewOption ($dhcpsettings{'ADVOPT_NAME'} . ' ' . $dhcpsettings{'ADVOPT_DATA'})) {
325 #was a new option
326 } elsif (! `grep "\$option $dhcpsettings{'ADVOPT_NAME'} " $filename3`) {
327 $errormessage=$Lang::tr{'dhcp advopt unknown'}.': '.$dhcpsettings{'ADVOPT_NAME'};
328 }
329
330 unless ($errormessage) {
331
332 my $scope = '';
333 foreach my $itf (@ITFs) { # buils "RED,GREEN,ORANGE,... based on selection
334 $scope .= $dhcpsettings{"ADVOPT_SCOPE_${itf}"} eq 'on' ? "\t$itf" : "\toff" ;
335 }
336 if ($dhcpsettings{'KEY1'} eq '') { #add or edit ? TAB separator !
337 unshift (@current1, "$dhcpsettings{'ADVOPT_ENABLED'}\t$dhcpsettings{'ADVOPT_NAME'}\t$dhcpsettings{'ADVOPT_DATA'}$scope\n");
338 &General::log($Lang::tr{'dhcp advopt added'});
339 } else {
340 @current1[$dhcpsettings{'KEY1'}] = "$dhcpsettings{'ADVOPT_ENABLED'}\t$dhcpsettings{'ADVOPT_NAME'}\t$dhcpsettings{'ADVOPT_DATA'}$scope\n";
341 $dhcpsettings{'KEY1'} = ''; # End edit mode
342 &General::log($Lang::tr{'dhcp advopt modified'});
343 }
344
345 #Write changes to dhcpd.conf.
346 &sortcurrent1; # sort newly added/modified entry
347 &buildconf; # before calling buildconf which use fixed lease file !
348 }
349 }
350
351 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'edit'}.'1') {
352 #move out new line
353 my $line = @current1[$dhcpsettings{'KEY1'}];
354 chomp($line);
355 my @temp = split(/\t/, $line);
356 $dhcpsettings{'ADVOPT_ENABLED'}=$temp[0];
357 $dhcpsettings{'ADVOPT_NAME'}=$temp[1];
358 $dhcpsettings{'ADVOPT_DATA'}=$temp[2];
359
360 # read next fields which are the name (color) of an interface if this interface is scoped
361 for (my $key=0; $key<@ITFs; $key++) {
362 my $itf = $temp[3+$key];
363 if ($itf ne 'off') # Only is an interface name is read
364 {
365 $dhcpsettings{"ADVOPT_SCOPE_${itf}"} = 'on';
366 }
367 }
368 }
369
370 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'remove'}.'1') {
371 splice (@current1,$dhcpsettings{'KEY1'},1);
372 open(FILE, ">$filename1") or die 'Unable to open dhcp advanced options file.';
373 print FILE @current1;
374 close(FILE);
375 $dhcpsettings{'KEY1'} = ''; # End remove mode
376 &General::log($Lang::tr{'dhcp advopt removed'});
377 #Write changes to dhcpd.conf.
378 &buildconf;
379 }
380 #end KEY1
381
382
383 # Toggle enable/disable field on specified lease.
384 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'toggle enable disable'}.'2') {
385 #move out new line
386 chomp(@current2[$dhcpsettings{'KEY2'}]);
387 my @temp = split(/\,/,@current2[$dhcpsettings{'KEY2'}]);
388 $temp[2] = $temp[2] eq 'on' ? '' : 'on'; # Toggle the field
389 @current2[$dhcpsettings{'KEY2'}] = join (',',@temp)."\n";
390 $dhcpsettings{'KEY2'} = ''; # End edit mode
391 &General::log($Lang::tr{'fixed ip lease modified'});
392 open(FILE, ">$filename2") or die 'Unable to open fixed leases file.';
393 print FILE @current2;
394 close(FILE);
395
396 #Write changes to dhcpd.conf.
397 &buildconf;
398 }
399
400 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'add'}.'2') {
401 $dhcpsettings{'FIX_MAC'} =~ tr/-/:/;
402 unless(&General::validip($dhcpsettings{'FIX_ADDR'})) { $errormessage = $Lang::tr{'invalid fixed ip address'}; }
403 unless(&General::validmac($dhcpsettings{'FIX_MAC'})) { $errormessage = $Lang::tr{'invalid fixed mac address'}; }
404 if ($dhcpsettings{'FIX_NEXTADDR'}) {
405 unless(&General::validip($dhcpsettings{'FIX_NEXTADDR'})) { $errormessage = $Lang::tr{'invalid fixed ip address'}; }
406 }
407
408 my $key = 0;
409 CHECK:foreach my $line (@current2) {
410 my @temp = split(/\,/,$line);
411 if($dhcpsettings{'KEY2'} ne $key) {
412 # same MAC is OK on different subnets. This test is not complete because
413 # if ip are not inside a known subnet, I don't warn.
414 # Also it may be needed to put duplicate fixed lease in their right subnet definition..
415 foreach my $itf (@ITFs) {
416 my $scoped = &General::IpInSubnet($dhcpsettings{'FIX_ADDR'},
417 $netsettings{"${itf}_NETADDRESS"},
418 $netsettings{"${itf}_NETMASK"}) &&
419 $dhcpsettings{"ENABLE_${itf}"} eq 'on';
420 if ( $scoped &&
421 (lc($dhcpsettings{'FIX_MAC'}) eq lc($temp[0])) &&
422 &General::IpInSubnet($temp[1],
423 $netsettings{"${itf}_NETADDRESS"},
424 $netsettings{"${itf}_NETMASK"})) {
425 $errormessage = "$Lang::tr{'mac address in use'} $dhcpsettings{'FIX_MAC'}";
426 last CHECK;
427 }
428 }
429 }
430 $key++;
431 }
432
433 unless ($errormessage) {
434 $dhcpsettings{'FIX_REMARK'} = &Header::cleanhtml($dhcpsettings{'FIX_REMARK'});
435 $dhcpsettings{'FIX_NEXTADDR'} = &Header::cleanhtml($dhcpsettings{'FIX_NEXTADDR'});
436 $dhcpsettings{'FIX_FILENAME'} = &Header::cleanhtml($dhcpsettings{'FIX_FILENAME'});
437 $dhcpsettings{'FIX_ROOTPATH'} = &Header::cleanhtml($dhcpsettings{'FIX_ROOTPATH'});
438 if ($dhcpsettings{'KEY2'} eq '') { #add or edit ?
439 unshift (@current2, "$dhcpsettings{'FIX_MAC'},$dhcpsettings{'FIX_ADDR'},$dhcpsettings{'FIX_ENABLED'},$dhcpsettings{'FIX_NEXTADDR'},$dhcpsettings{'FIX_FILENAME'},$dhcpsettings{'FIX_ROOTPATH'},$dhcpsettings{'FIX_REMARK'}\n");
440 &General::log($Lang::tr{'fixed ip lease added'});
441 } else {
442 @current2[$dhcpsettings{'KEY2'}] = "$dhcpsettings{'FIX_MAC'},$dhcpsettings{'FIX_ADDR'},$dhcpsettings{'FIX_ENABLED'},$dhcpsettings{'FIX_NEXTADDR'},$dhcpsettings{'FIX_FILENAME'},$dhcpsettings{'FIX_ROOTPATH'},$dhcpsettings{'FIX_REMARK'}\n";
443 $dhcpsettings{'KEY2'} = ''; # End edit mode
444 &General::log($Lang::tr{'fixed ip lease modified'});
445 }
446
447 #Write changes to dhcpd.conf.
448 &sortcurrent2; # sort newly added/modified entry
449 &buildconf; # before calling buildconf which use fixed lease file !
450 }
451 }
452
453 if ($dhcpsettings{'ACTION_ALL'} eq '+') {
454 my $news = 0;
455 foreach (keys %dhcpsettings) {
456 if (/^(\d+\.\d+\.\d+\.\d+)-([0-9a-fA-F:]+)$/) { # checked names are index of the line
457 my $ip=$1;
458 my $mac=$2;
459 if (!grep (/$2/,@current2)) {
460 unshift (@current2, "$mac,$ip,on,,,,imported\n");
461 $news++;
462 }
463 }
464 }
465 if ($news) {
466 #Write changes to dhcpd.conf.
467 $warnNTPmessage = $Lang::tr{'fixed ip lease added'}."($news)";
468 &General::log($warnNTPmessage);
469 &sortcurrent2; # sort newly added/modified entry
470 &buildconf; # before calling buildconf which use fixed lease file !
471 }
472 }
473
474 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'edit'}.'2') {
475 #move out new line
476 my $line = @current2[$dhcpsettings{'KEY2'}];
477 chomp($line);
478 my @temp = split(/\,/, $line);
479 $dhcpsettings{'FIX_MAC'}=$temp[0];
480 $dhcpsettings{'FIX_ADDR'}=$temp[1];
481 $dhcpsettings{'FIX_ENABLED'}=$temp[2];
482 $dhcpsettings{'FIX_NEXTADDR'}=$temp[3];
483 $dhcpsettings{'FIX_FILENAME'}=$temp[4];
484 $dhcpsettings{'FIX_ROOTPATH'}=$temp[5];
485 $dhcpsettings{'FIX_REMARK'}=$temp[6];
486 }
487
488 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'remove'}.'2') {
489 splice (@current2,$dhcpsettings{'KEY2'},1);
490 open(FILE, ">$filename2") or die 'Unable to open fixed lease file.';
491 print FILE @current2;
492 close(FILE);
493 $dhcpsettings{'KEY2'} = ''; # End remove mode
494 &General::log($Lang::tr{'fixed ip lease removed'});
495 #Write changes to dhcpd.conf.
496 &buildconf;
497 }
498 #end KEY2 defined
499
500
501
502
503 if ($dhcpsettings{'ACTION'} eq '' ) { # First launch from GUI
504
505 # Set default DHCP values only if blank and disabled
506 foreach my $itf (@ITFs) {
507 if ($dhcpsettings{"ENABLE_${itf}"} ne 'on' ) {
508 $dhcpsettings{"DNS1_${itf}"} = $netsettings{"${itf}_ADDRESS"};
509 $dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} = '60';
510 $dhcpsettings{"MAX_LEASE_TIME_${itf}"} = '120';
511 $dhcpsettings{"DOMAIN_NAME_${itf}"} = $mainsettings{'DOMAINNAME'};
512 }
513 }
514 $dhcpsettings{'FIX_ENABLED'} = 'on';
515 }
516
517 &Header::openpage($Lang::tr{'dhcp configuration'}, 1, '');
518 &Header::openbigbox('100%', 'left', '', $errormessage);
519
520 if ($errormessage) {
521 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
522 print "<font class='base'>$errormessage&nbsp;</font>\n";
523 &Header::closebox();
524 }
525 if ($warnNTPmessage) {
526 $warnNTPmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnNTPmessage";
527 }
528
529 &Header::openbox('100%', 'left', 'DHCP');
530 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
531
532 foreach my $itf (@ITFs) {
533 my %checked=();
534 $checked{'ENABLE'}{'on'} = ( $dhcpsettings{"ENABLE_${itf}"} ne 'on') ? '' : "checked='checked'";
535 $checked{'ENABLEBOOTP'}{'on'} = ( $dhcpsettings{"ENABLEBOOTP_${itf}"} ne 'on') ? '' : "checked='checked'";
536
537 if ($netsettings{"${itf}_DEV"} ne '' ) { # Show only defined interface
538 my $lc_itf=lc($itf);
539 print <<END
540 <table width='100%'>
541 <tr>
542 <td width='25%' class='boldbase'><b><font color='${lc_itf}'>$Lang::tr{"$lc_itf interface"}</font></b></td>
543 <td class='base'>$Lang::tr{'enabled'}
544 <input type='checkbox' name='ENABLE_${itf}' $checked{'ENABLE'}{'on'} /></td>
545 <td width='25%' class='base'>$Lang::tr{'ip address'}<br />$Lang::tr{'netmask'}:</td><td><b>$netsettings{"${itf}_ADDRESS"}<br />$netsettings{"${itf}_NETMASK"}</b></td>
546 </tr><tr>
547 <td width='25%' class='base'>$Lang::tr{'start address'}</td>
548 <td width='25%'><input type='text' name='START_ADDR_${itf}' value='$dhcpsettings{"START_ADDR_${itf}"}' /></td>
549 <td width='25%' class='base'>$Lang::tr{'end address'}</td>
550 <td width='25%'><input type='text' name='END_ADDR_${itf}' value='$dhcpsettings{"END_ADDR_${itf}"}' /></td>
551 </tr><tr>
552 <td class='base'>$Lang::tr{'default lease time'}</td>
553 <td><input type='text' name='DEFAULT_LEASE_TIME_${itf}' value='$dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"}' /></td>
554 <td class='base'>$Lang::tr{'max lease time'}</td>
555 <td><input type='text' name='MAX_LEASE_TIME_${itf}' value='$dhcpsettings{"MAX_LEASE_TIME_${itf}"}' /></td>
556 </tr><tr>
557 <td class='base'>$Lang::tr{'domain name suffix'}&nbsp;<img src='/blob.gif' alt='*' /></td>
558 <td><input type='text' name='DOMAIN_NAME_${itf}' value='$dhcpsettings{"DOMAIN_NAME_${itf}"}' /></td>
559 <td>$Lang::tr{'dhcp allow bootp'}:</td>
560 <td><input type='checkbox' name='ENABLEBOOTP_${itf}' $checked{'ENABLEBOOTP'}{'on'} /></td>
561 </tr><tr>
562 <td class='base'>$Lang::tr{'primary dns'}</td>
563 <td><input type='text' name='DNS1_${itf}' value='$dhcpsettings{"DNS1_${itf}"}' /></td>
564 <td class='base'>$Lang::tr{'secondary dns'}&nbsp;<img src='/blob.gif' alt='*' /></td>
565 <td><input type='text' name='DNS2_${itf}' value='$dhcpsettings{"DNS2_${itf}"}' /></td>
566 </tr><tr>
567 <td class='base'>$Lang::tr{'primary ntp server'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
568 <td><input type='text' name='NTP1_${itf}' value='$dhcpsettings{"NTP1_${itf}"}' /></td>
569 <td class='base'>$Lang::tr{'secondary ntp server'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
570 <td><input type='text' name='NTP2_${itf}' value='$dhcpsettings{"NTP2_${itf}"}' /></td>
571 </tr><tr>
572 <td class='base'>$Lang::tr{'primary wins server address'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
573 <td><input type='text' name='WINS1_${itf}' value='$dhcpsettings{"WINS1_${itf}"}' /></td>
574 <td class='base'>$Lang::tr{'secondary wins server address'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
575 <td><input type='text' name='WINS2_${itf}' value='$dhcpsettings{"WINS2_${itf}"}' /></td>
576 </tr><tr>
577 <td class='base'>next-server:&nbsp;<img src='/blob.gif' alt='*' /></td>
578 <td><input type='text' name='NEXT_${itf}' value='$dhcpsettings{"NEXT_${itf}"}' /></td>
579 <td class='base'>filename:&nbsp;<img src='/blob.gif' alt='*' /></td>
580 <td><input type='text' name='FILE_${itf}' value='$dhcpsettings{"FILE_${itf}"}' /></td>
581 </tr>
582 </table>
583 <hr />
584 END
585 ;
586 }# Show only defined interface
587 }#foreach itf
588 print <<END
589 <table width='100%'>
590 <tr>
591 <td class='base' width='25%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
592 <td class='base' width='30%'>$warnNTPmessage</td>
593 <td width='40%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
594 </tr>
595 </table>
596 </form>
597 END
598 ;
599
600 &Header::closebox();
601
602 &Header::openbox('100%', 'left', $Lang::tr{'dhcp advopt list'});
603 # DHCP Advanced options settings
604 my %checked=();
605 $checked{'ADVOPT_ENABLED'}{'on'} = ($dhcpsettings{'ADVOPT_ENABLED'} ne 'on') ? '' : "checked='checked'";
606
607 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>";
608 my $buttontext = $Lang::tr{'add'};
609 if ($dhcpsettings{'KEY1'} ne '') {
610 $buttontext = $Lang::tr{'update'};
611 print "<tr><td class='boldbase'><b>$Lang::tr{'dhcp advopt edit'}</b></td></tr>";
612 } else {
613 print "<tr><td class='boldbase'><b>$Lang::tr{'dhcp advopt add'}</b></td></tr>"
614 }
615
616 #search if the 'option' is in the list and print the syntax model
617 my $opt = `grep "\$option $dhcpsettings{'ADVOPT_NAME'} " $filename3`;
618 if ($opt ne '') {
619 $opt =~ s/option $dhcpsettings{'ADVOPT_NAME'}/Syntax:/; # "option xyz abc" => "syntax: abc"
620 $opt =~ s/;//;
621 $opt = "<tr><td></td><td></td><td colspan='2'>$opt</td></tr>";
622 }
623 print <<END
624 <tr>
625 <td class='base'>$Lang::tr{'dhcp advopt name'}:</td>
626 <td><input type='text' name='ADVOPT_NAME' value='$dhcpsettings{'ADVOPT_NAME'}' size='18' /></td>
627 <td class='base'>$Lang::tr{'dhcp advopt value'}:</td>
628 <td><input type='text' name='ADVOPT_DATA' value='$dhcpsettings{'ADVOPT_DATA'}' size='40' /></td>
629 </tr>$opt<tr>
630 <td class='base'>$Lang::tr{'enabled'}</td><td><input type='checkbox' name='ADVOPT_ENABLED' $checked{'ADVOPT_ENABLED'}{'on'} /></td>
631 <td class='base'>$Lang::tr{'dhcp advopt scope'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
632 <td>
633 END
634 ;
635
636 # Put a checkbox for each interface. Checkbox visible disabled if interface is disabled
637 foreach my $itf (@ITFs) {
638 my $lc_itf=lc($itf);
639 $checked{'ADVOPT_SCOPE_${itf}'}{'on'} = $dhcpsettings{"ADVOPT_SCOPE_${itf}"} ne 'on' ? '' : "checked='checked'";
640 print "$Lang::tr{\"${lc_itf}\"} <input type='checkbox' name='ADVOPT_SCOPE_${itf}' $checked{'ADVOPT_SCOPE_${itf}'}{'on'} ";
641 print $dhcpsettings{"ENABLE_${itf}"} eq 'on' ? "/>" : "disabled='disabled' />";
642 print "&nbsp; &nbsp;";
643 }
644
645 print <<END
646 </td>
647 </tr>
648 </table>
649 <hr />
650 <table width='100%'>
651 <tr>
652 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'dhcp advopt scope help'}</td>
653 <td width='50%' align='right'>
654 <input type='hidden' name='ACTION' value='$Lang::tr{'add'}1' />
655 <input type='submit' name='SUBMIT' value='$buttontext' />
656 <input type='submit' name='SUBMIT' value='$Lang::tr{'dhcp advopt help'}' />
657 <input type='hidden' name='KEY1' value='$dhcpsettings{'KEY1'}' />
658 </td>
659 </tr>
660 </table>
661 </form>
662 END
663 ;
664 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
665
666 # print help taken from the file describing options
667 if ($dhcpsettings{'SUBMIT'} eq $Lang::tr{'dhcp advopt help'}) {
668 print "<hr />";
669 print "<table width='100%'>";
670 print "<tr><td width='30%'><b>$Lang::tr{'dhcp advopt name'}</b></td><td width='70%'><b>$Lang::tr{'dhcp advopt value'}</b></td>";
671 open(FILE, "$filename3");
672 my @current3 = <FILE>;
673 close(FILE);
674 foreach my $line (@current3) {
675 $line =~ /option ([a-z0-9-]+) (.*);/;
676 print "<tr><td>$1</td><td>$2</td></tr>\n";
677 }
678 print "<tr><td colspan='2'><hr /></td></tr>\n";
679 print '<tr><td>string type</td><td>"quoted string" or 00:01:FF...</td></tr>';
680 print '<tr><td>ip-address type </td><td>10.0.0.1 | www.dot.com</td></tr>';
681 print '<tr><td>int,uint types</td><td>numbers</td></tr>';
682 print '<tr><td>flag type</td><td>on | off</td></tr>';
683 print '</table>';
684 print "<hr />";
685 print "<table width='100%'>";
686 print "<tr><td width='30%'><b>$Lang::tr{'dhcp advopt custom definition'}</b></td><td width='70%'><b>$Lang::tr{'dhcp advopt value'}</b></td>";
687 print "<tr><td>any-name </td><td> code NNN=$OptionTypes</td></tr>";
688 print '<tr><td>a-string</td><td>code 100=string</td></tr>';
689 print '<tr><td>a-number</td><td>code 101=signed integer 8</td></tr>';
690 print '<tr><td>wpad</td><td>code 252=text</td></tr>';
691 print '<tr><td>wpad</td><td>"http://www.server.fr/path-to/proxy.pac"</td></tr>';
692 print '</table>';
693
694 }
695
696 print <<END
697 <hr />
698 <table width='100%'>
699 <tr>
700 <td width='30%' class='boldbase' align='center'><b>$Lang::tr{'dhcp advopt name'}</b></td>
701 <td width='50%' class='boldbase' align='center'><b>$Lang::tr{'dhcp advopt value'}</b></td>
702 <td width='20%' class='boldbase' align='center'><b>$Lang::tr{'dhcp advopt scope'}</b></td>
703 <td colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>
704 </tr>
705 END
706 ;
707 my $key = 0;
708 foreach my $line (@current1) {
709 my $gif = '';
710 my $gdesc = '';
711 chomp($line); # remove newline
712 my @temp = split(/\t/,$line);
713
714 if ($temp[0] eq "on") {
715 $gif = 'on.gif';
716 $gdesc = $Lang::tr{'click to disable'};
717 } else {
718 $gif = 'off.gif';
719 $gdesc = $Lang::tr{'click to enable'};
720 }
721
722 if ($dhcpsettings{'KEY1'} eq $key) {
723 print "<tr bgcolor='${Header::colouryellow}'>";
724 } elsif ($key % 2) {
725 print "<tr bgcolor='$color{'color22'}'>";
726 } else {
727 print "<tr bgcolor='$color{'color20'}'>";
728 }
729
730 print <<END
731 <td align='center'>$temp[1]</td>
732 <td align='center'>$temp[2]</td>
733 <td align='center'>
734 END
735 ;
736 # Prepare a global flag to make easy reading
737 my $global = '';
738 my $disabledTogle = '';
739 my $disabledEditRemove = '';
740 if ( ExistNewOptionDefinition ($temp[1] . ' ' . $temp[2]) ) {
741 $global = $Lang::tr{'dhcp advopt definition'};
742 $disabledTogle = "disabled='disabled'";
743 # Search if it is a used NewOptionDefinition to also disable edit & delete
744 $disabledEditRemove = "disabled='disabled'" if (IsUsedNewOptionDefinition ($temp[1], $temp[2]));
745 } else {
746 $global = $Lang::tr{'dhcp advopt scope global'};
747 }
748
749
750 # Print each checked interface
751 for (my $key=0; $key<@ITFs; $key++) {
752 my $itf = $temp[3+$key];
753 if ($itf ne 'off') { # Only if an interface name is read
754 print "$itf";
755 $global=''; # fall to local scope !
756 }
757 }
758 print <<END
759 $global</td>
760 <td align='center'>
761 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
762 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}1' />
763 <input $disabledTogle type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
764 <input type='hidden' name='KEY1' value='$key' />
765 </form>
766 </td>
767
768 <td align='center'>
769 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
770 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}1' />
771 <input $disabledEditRemove type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
772 <input type='hidden' name='KEY1' value='$key' />
773 </form>
774 </td>
775
776 <td align='center'>
777 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
778 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}1' />
779 <input $disabledEditRemove type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
780 <input type='hidden' name='KEY1' value='$key' />
781 </form>
782 </td>
783 </tr>
784 END
785 ;
786 $key++;
787 }
788
789 print "</table>";
790
791 # If there are dhcp options, print Key to action icons
792 if ($key) {
793 print <<END
794 <table>
795 <tr>
796 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
797 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
798 <td class='base'>$Lang::tr{'click to disable'}</td>
799 <td>&nbsp;&nbsp;</td>
800 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
801 <td class='base'>$Lang::tr{'click to enable'}</td>
802 <td>&nbsp;&nbsp;</td>
803 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
804 <td class='base'>$Lang::tr{'edit'}</td>
805 <td>&nbsp;&nbsp;</td>
806 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
807 <td class='base'>$Lang::tr{'remove'}</td>
808 </tr>
809 </table>
810 END
811 ;
812 }
813 &Header::closebox();
814
815 &Header::openbox('100%', 'left', $Lang::tr{'current fixed leases'});
816 # Fixed leases screens
817 $checked{'FIX_ENABLED'}{'on'} = ($dhcpsettings{'FIX_ENABLED'} ne 'on') ? '' : "checked='checked'";
818
819 $buttontext = $Lang::tr{'add'};
820 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>";
821
822 if ($dhcpsettings{'KEY2'} ne '') {
823 $buttontext = $Lang::tr{'update'};
824 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'edit an existing lease'}</b></td></tr>";
825 } else {
826 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'add new lease'}</b></td></tr>"
827 }
828 print <<END
829 <tr>
830 <td class='base'>$Lang::tr{'mac address'}:</td>
831 <td><input type='text' name='FIX_MAC' value='$dhcpsettings{'FIX_MAC'}' size='18' /></td>
832 <td class='base'>$Lang::tr{'ip address'}:</td>
833 <td><input type='text' name='FIX_ADDR' value='$dhcpsettings{'FIX_ADDR'}' size='18' /></td>
834 <td class='base'>$Lang::tr{'remark'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
835 <td><input type='text' name='FIX_REMARK' value='$dhcpsettings{'FIX_REMARK'}' size='18' /></td>
836 </tr><tr>
837 <td class='base'>$Lang::tr{'enabled'}</td><td><input type='checkbox' name='FIX_ENABLED' $checked{'FIX_ENABLED'}{'on'} /></td>
838 </tr><tr>
839 <td colspan = '3'><b>$Lang::tr{'dhcp bootp pxe data'}</b></td>
840 </tr><tr>
841 <td class='base'>next-server:&nbsp;<img src='/blob.gif' alt='*' /></td>
842 <td><input type='text' name='FIX_NEXTADDR' value='$dhcpsettings{'FIX_NEXTADDR'}' size='18' /></td>
843 <td class='base'>filename:&nbsp;<img src='/blob.gif' alt='*' /></td>
844 <td><input type='text' name='FIX_FILENAME' value='$dhcpsettings{'FIX_FILENAME'}' size='18' /></td>
845 <td class='base'>root path:&nbsp;<img src='/blob.gif' alt='*' /></td>
846 <td><input type='text' name='FIX_ROOTPATH' value='$dhcpsettings{'FIX_ROOTPATH'}' size='18' /></td>
847 </tr>
848 </table>
849 <hr />
850 <table width='100%'>
851 <tr>
852 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
853 <td width='50%' align='right'>
854 <input type='hidden' name='ACTION' value='$Lang::tr{'add'}2' />
855 <input type='submit' name='SUBMIT' value='$buttontext' />
856 <input type='hidden' name='KEY2' value='$dhcpsettings{'KEY2'}' /></td>
857 </tr>
858 </table>
859 </form>
860 END
861 ;
862 #Edited line number (KEY2) passed until cleared by 'save' or 'remove' or 'new sort order'
863
864 print <<END
865 <hr />
866 <table width='100%' class='tbl'>
867 <tr>
868 <th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?FETHER'><b>$Lang::tr{'mac address'}</b></a></th>
869 <th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?FIPADDR'><b>$Lang::tr{'ip address'}</b></a></th>
870 <th width='15%' align='center'><b>$Lang::tr{'remark'}</b></th>
871 <th width='15%' class='boldbase' align='center'><b>next-server</b></th>
872 <th width='15%' class='boldbase' align='center'><b>filename</b></th>
873 <th width='15%' class='boldbase' align='center'><b>root path</b></th>
874 <th colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
875 </tr>
876 END
877 ;
878 my $ipdup = 0;
879 my %ipinuse = ();
880 my %macdupl = (); # Duplicate MACs have to be on different subnets
881 my %ipoutside = ();
882
883 # mark duplicate ip or duplicate MAC
884 foreach my $line (@current2) {
885 my @temp = split(/\,/,$line);
886 $macdupl{$temp[0]} += 1;
887 if ($macdupl{$temp[0]} > 1) {
888 $ipdup = 1; # Flag up duplicates for use later
889 }
890 $ipinuse{$temp[1]} += 1;
891 if ($ipinuse{$temp[1]} > 1) {
892 $ipdup = 1; # Flag up duplicates for use later
893 }
894 # Mark IP addresses outwith known subnets
895 $ipoutside{$temp[1]} = 1;
896 foreach my $itf (@ITFs) {
897 if ( &General::IpInSubnet($temp[1],
898 $netsettings{"${itf}_NETADDRESS"},
899 $netsettings{"${itf}_NETMASK"})) {
900 $ipoutside{$temp[1]} = 0;
901 }
902 }
903 }
904
905 $key = 0;
906 my $col="";
907 foreach my $line (@current2) {
908 my $gif = '';
909 my $gdesc = '';
910 chomp($line); # remove newline
911 my @temp = split(/\,/,$line);
912
913 if ($temp[2] eq "on") {
914 $gif = 'on.gif';
915 $gdesc = $Lang::tr{'click to disable'};
916 } else {
917 $gif = 'off.gif';
918 $gdesc = $Lang::tr{'click to enable'};
919 }
920
921 if ($dhcpsettings{'KEY2'} eq $key) {
922 print "<tr>";
923 $col="bgcolor='${Header::colouryellow}'";
924 } elsif ($key % 2) {
925 print "<tr>";
926 $col="bgcolor='$color{'color20'}'";
927 } else {
928 print "<tr>";
929 $col="bgcolor='$color{'color22'}'";
930 }
931 my $TAG0 = '';
932 my $TAG1 = '';
933 my $TAG2 = '';
934 my $TAG3 = '';
935 my $TAG4 = '';
936 if ($ipinuse{$temp[1]} > 1) {
937 $TAG0 = '<b>';
938 $TAG1 = '</b>';
939 }
940 if ($macdupl{$temp[0]} > 1) {
941 $TAG2 = '<b>';
942 $TAG3 = '</b>';
943 }
944 if ($ipoutside{$temp[1]} > 0) {
945 $TAG4 = "bgcolor='orange'" if ($dhcpsettings{'KEY2'} ne $key);
946 }
947
948 print <<END
949 <td align='center' $col>$TAG2$temp[0]$TAG3</td>
950 <td align='center' $col $TAG4>$TAG0$temp[1]$TAG1</td>
951 <td align='center' $col>$temp[6]&nbsp;</td>
952 <td align='center' $col>$temp[3]&nbsp;</td>
953 <td align='center' $col>$temp[4]&nbsp;</td>
954 <td align='center' $col>$temp[5]&nbsp;</td>
955
956 <td align='center' $col>
957 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
958 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}2' />
959 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
960 <input type='hidden' name='KEY2' value='$key' />
961 </form>
962 </td>
963
964 <td align='center' $col>
965 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
966 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}2' />
967 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
968 <input type='hidden' name='KEY2' value='$key' />
969 </form>
970 </td>
971
972 <td align='center' $col>
973 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
974 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}2' />
975 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
976 <input type='hidden' name='KEY2' value='$key' />
977 </form>
978 </td>
979 </tr>
980 END
981 ;
982 $key++;
983 }
984 print "</table>";
985
986 # If the fixed lease file contains entries, print Key to action icons
987 if ($key) {
988 my $dup = $ipdup ? "<td class='base'>$Lang::tr{'duplicate ip bold'}</td>" :'';
989 print <<END
990 <table>
991 <tr>
992 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
993 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
994 <td class='base'>$Lang::tr{'click to disable'}</td>
995 <td>&nbsp;&nbsp;</td>
996 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
997 <td class='base'>$Lang::tr{'click to enable'}</td>
998 <td>&nbsp;&nbsp;</td>
999 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
1000 <td class='base'>$Lang::tr{'edit'}</td>
1001 <td>&nbsp;&nbsp;</td>
1002 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
1003 <td class='base'>$Lang::tr{'remove'}</td>
1004 </tr>
1005 <tr>
1006 <td>&nbsp;</td>
1007 <td bgcolor='orange'>&nbsp;</td>
1008 <td class='base'>$Lang::tr{'ip address outside subnets'}</td>
1009 <td>&nbsp;</td>
1010 <td>&nbsp;</td>
1011 $dup
1012 </tr>
1013 </table>
1014 END
1015 ;
1016 }
1017
1018 &Header::closebox();
1019
1020 foreach my $itf (@ITFs) {
1021 if ($dhcpsettings{"ENABLE_${itf}"} eq 'on') {
1022 # display leases with a list of actions to do with the global select checkbox.
1023 &Header::PrintActualLeases("+"); # "+" => create fixed leases from nodeaddress
1024 last; #Print one time only for all interfaces
1025 };
1026 }
1027
1028 &Header::closebigbox();
1029 &Header::closepage();
1030
1031 ## Ouf it's the end !
1032
1033 sub sortcurrent1 # by now, do not sort, just write
1034 {
1035 open(FILE, ">$filename1") or die 'Unable to open dhcp advanced options file.';
1036 print FILE @current1;
1037 close(FILE);
1038 }
1039
1040
1041 # Sort the "current2" array according to choices
1042 sub sortcurrent2
1043 {
1044 our %entries = ();
1045
1046 sub fixedleasesort {
1047 my $qs='';
1048 if (rindex ($dhcpsettings{'SORT_FLEASELIST'},'Rev') != -1) {
1049 $qs=substr ($dhcpsettings{'SORT_FLEASELIST'},0,length($dhcpsettings{'SORT_FLEASELIST'})-3);
1050 if ($qs eq 'FIPADDR') {
1051 my @a = split(/\./,$entries{$a}->{$qs});
1052 my @b = split(/\./,$entries{$b}->{$qs});
1053 ($b[0]<=>$a[0]) ||
1054 ($b[1]<=>$a[1]) ||
1055 ($b[2]<=>$a[2]) ||
1056 ($b[3]<=>$a[3]);
1057 } else {
1058 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
1059 }
1060 } else { #not reverse
1061 $qs=$dhcpsettings{'SORT_FLEASELIST'};
1062 if ($qs eq 'FIPADDR') {
1063 my @a = split(/\./,$entries{$a}->{$qs});
1064 my @b = split(/\./,$entries{$b}->{$qs});
1065 ($a[0]<=>$b[0]) ||
1066 ($a[1]<=>$b[1]) ||
1067 ($a[2]<=>$b[2]) ||
1068 ($a[3]<=>$b[3]);
1069 } else {
1070 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
1071 }
1072 }
1073 }
1074
1075 #Use an associative array (%entries)
1076 foreach my $line (@current2) {
1077 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
1078 my @temp = split (',',$line);
1079 my @record = ('FETHER',$temp[0],'FIPADDR',$temp[1],'DATA',join(',',@temp[2..6]));
1080 my $record = {}; # create a reference to empty hash
1081 %{$record} = @record; # populate that hash with @record
1082 # use combination of ether & IP as key to allow duplicates in either but not both
1083 $entries{$record->{FETHER} . $record->{FIPADDR}} = $record; # add this to a hash of hashes
1084 }
1085
1086 open(FILE, ">$filename2") or die 'Unable to open fixed lease file.';
1087 foreach my $entry ( sort fixedleasesort keys %entries) {
1088 print FILE "$entries{$entry}->{FETHER},$entries{$entry}->{FIPADDR},$entries{$entry}->{DATA}\n";
1089 }
1090 close(FILE);
1091
1092 # Reload sorted @current2
1093 open (FILE, "$filename2");
1094 @current2 = <FILE>;
1095 close (FILE);
1096 undef (%entries); #This array is reused latter. Clear it.
1097 }
1098
1099 # Build the configuration file mixing settings, fixed leases and advanced options
1100 sub buildconf {
1101 open(FILE, ">/${General::swroot}/dhcp/dhcpd.conf") or die "Unable to write dhcpd.conf file";
1102 flock(FILE, 2);
1103
1104 # Global settings
1105 print FILE "ddns-update-style none;\n";
1106 print FILE "deny bootp; #default\n";
1107 print FILE "authoritative;\n";
1108
1109 # Write first new option definition
1110 foreach my $line (@current1) {
1111 chomp($line); # remove newline
1112 my @temp = split(/\t/,$line);
1113 if (ExistNewOptionDefinition ($temp[1] . ' ' . $temp[2])) {
1114 print FILE "option $temp[1] $temp[2];\n";
1115 }
1116 }
1117 # Write other global options
1118 foreach my $line (@current1) {
1119 chomp($line); # remove newline
1120 my @temp = split(/\t/,$line);
1121
1122 if ($temp[0] eq 'on' && !ExistNewOptionDefinition ($temp[1] . ' ' . $temp[2])){ # active & !definition
1123 my $global=1;
1124 for (my $key=0; $key<@ITFs; $key++) {
1125 my $itf = $temp[3+$key];
1126 if ($itf ne 'off') # Only if an interface name is read
1127 {
1128 $global=0;
1129 }
1130 }
1131 if ($global) {
1132 print FILE "option $temp[1] $temp[2];\n";
1133 }
1134 }# on
1135 }# foreach line
1136
1137 #Subnet range definition
1138 foreach my $itf (@ITFs) {
1139 my $lc_itf=lc($itf);
1140 if ($dhcpsettings{"ENABLE_${itf}"} eq 'on' ){
1141 print FILE "\nsubnet " . $netsettings{"${itf}_NETADDRESS"} . " netmask ". $netsettings{"${itf}_NETMASK"} . " #$itf\n";
1142 print FILE "{\n";
1143 print FILE "\trange " . $dhcpsettings{"START_ADDR_${itf}"} . ' ' . $dhcpsettings{"END_ADDR_${itf}"}.";\n" if ($dhcpsettings{"START_ADDR_${itf}"});
1144 print FILE "\toption subnet-mask " . $netsettings{"${itf}_NETMASK"} . ";\n";
1145 print FILE "\toption domain-name \"" . $dhcpsettings{"DOMAIN_NAME_${itf}"} . "\";\n";
1146 print FILE "\toption routers " . $netsettings{"${itf}_ADDRESS"} . ";\n";
1147 print FILE "\toption domain-name-servers " . $dhcpsettings{"DNS1_${itf}"} if ($dhcpsettings{"DNS1_${itf}"});
1148 print FILE ", " . $dhcpsettings{"DNS2_${itf}"} if ($dhcpsettings{"DNS2_${itf}"});
1149 print FILE ";\n" if ($dhcpsettings{"DNS1_${itf}"});
1150 print FILE "\toption ntp-servers " . $dhcpsettings{"NTP1_${itf}"} if ($dhcpsettings{"NTP1_${itf}"});
1151 print FILE ", " . $dhcpsettings{"NTP2_${itf}"} if ($dhcpsettings{"NTP2_${itf}"});
1152 print FILE ";\n" if ($dhcpsettings{"NTP1_${itf}"});
1153 print FILE "\toption netbios-name-servers " . $dhcpsettings{"WINS1_${itf}"} if ($dhcpsettings{"WINS1_${itf}"});
1154 print FILE ", " . $dhcpsettings{"WINS2_${itf}"} if ($dhcpsettings{"WINS2_${itf}"});
1155 print FILE ";\n" if ($dhcpsettings{"WINS1_${itf}"});
1156 print FILE "\tnext-server " . $dhcpsettings{"NEXT_${itf}"} . ";\n" if ($dhcpsettings{"NEXT_${itf}"});
1157 print FILE "\tfilename \"" . $dhcpsettings{"FILE_${itf}"} . "\";\n" if ($dhcpsettings{"FILE_${itf}"});
1158 print FILE "\tdefault-lease-time " . ($dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} * 60). ";\n";
1159 print FILE "\tmax-lease-time " . ($dhcpsettings{"MAX_LEASE_TIME_${itf}"} * 60) . ";\n";
1160 print FILE "\tallow bootp;\n" if ($dhcpsettings{"ENABLEBOOTP_${itf}"} eq 'on');
1161
1162
1163
1164 # Write scoped options
1165 foreach my $line (@current1) {
1166 chomp($line); # remove newline
1167 my @temp = split(/\t/,$line); # Use TAB separator !
1168
1169 if ($temp[0] eq 'on'){
1170 for (my $key=0; $key<@ITFs; $key++) {
1171 if ($itf eq $temp[3+$key]) # Only is an interface name is read
1172 {
1173 print FILE "\toption $temp[1] $temp[2];\n";
1174 }
1175 }
1176 }# on
1177 }# foreach line
1178 print FILE "} #$itf\n";
1179
1180 system ('/usr/bin/touch', "${General::swroot}/dhcp/enable_${lc_itf}");
1181 &General::log("DHCP on ${itf}: " . $Lang::tr{'dhcp server enabled'})
1182 } else {
1183 unlink "${General::swroot}/dhcp/enable_${lc_itf}";
1184 &General::log("DHCP on ${itf}: " . $Lang::tr{'dhcp server disabled'})
1185 }
1186 }
1187
1188 #write fixed leases if any. Does not handle duplicates to write them elsewhere than the global scope.
1189 my $key = 0;
1190 foreach my $line (@current2) {
1191 chomp($line);
1192 my @temp = split(/\,/,$line);
1193 if ($temp[2] eq "on") {
1194 print FILE "\nhost fix$key # $temp[6]\n";
1195 print FILE "{\n";
1196 print FILE "\thardware ethernet $temp[0];\n";
1197 print FILE "\tfixed-address $temp[1];\n";
1198 print FILE "\tnext-server $temp[3];\n" if ($temp[3]);
1199 print FILE "\tfilename \"$temp[4]\";\n" if ($temp[4]);
1200 print FILE "\toption root-path \"$temp[5]\";\n" if ($temp[5]);
1201 print FILE "}\n";
1202 $key++;
1203 }
1204 }
1205 print FILE "include \"${General::swroot}/dhcp/dhcpd.conf.local\";\n";
1206 close FILE;
1207 if ( $dhcpsettings{"ENABLE_GREEN"} eq 'on' || $dhcpsettings{"ENABLE_BLUE"} eq 'on' ) {system '/usr/local/bin/dhcpctrl enable >/dev/null 2>&1';}
1208 else {system '/usr/local/bin/dhcpctrl disable >/dev/null 2>&1';}
1209 system '/usr/local/bin/dhcpctrl restart >/dev/null 2>&1';
1210 }
1211
1212 #
1213 # Receive a string and if it match model for a new option,
1214 # add it to the list %newOptions
1215 #
1216 my %NewOptions = ();
1217
1218 sub AddNewOptionDefinition {
1219 my ($line) = @_;
1220 if ( $line =~ /^([-\w]+)( code \d+=($OptionTypes))/ ) {
1221 $NewOptions{$1} = $2;
1222 #&General::log ("new:<$1><$2>");
1223 return 1;
1224 }
1225 return 0;
1226 }
1227
1228 #
1229 # Check existence of definition for a new option
1230 #
1231 sub ExistNewOptionDefinition {
1232 my ($line) = @_;
1233
1234 if ( $line =~ /^([-\w]+)( code \d+=($OptionTypes))/ ) {
1235 return defined $NewOptions{$1};
1236 }
1237 return 0;
1238 }
1239
1240 #
1241 # Check if it is a new option (definition must exist)
1242 # "code=" test eliminate a false response when definition exists
1243 # but this string is a definition with bad $OptionTypes.
1244 sub ValidNewOption {
1245 my ($line) = @_;
1246 if ($line =~ /^([-\w]+) (.*)/ ) {
1247 return defined ( $NewOptions{$1} ) && $2 !~ /code=/;
1248 }
1249 return 0;
1250 }
1251
1252 #
1253 # Check if the new option $opt is used, except the definition of itself!
1254 #
1255 sub IsUsedNewOptionDefinition {
1256 my ($opt,$val) = @_;
1257
1258 foreach my $line (@current1) {
1259 #chomp($line); # remove newline #don't know why, but this remove newline in @current1 .... !
1260 my @temp = split(/\t/,$line);
1261 # if we find something "opt value" & value != "code nnn=" it's ok.
1262 return 1 if ( ($opt eq $temp[1]) && ($temp[2] !~ /code \d+=/) );
1263 }
1264 return 0;
1265 }