2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
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
} );
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 = '';
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';
52 &Header
::showhttpheaders
();
54 if (&Header
::blue_used
()){push(@ITFs,'BLUE');}
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}"} = '';
75 $dhcpsettings{'SORT_FLEASELIST'} = 'FIPADDR';
76 $dhcpsettings{'SORT_LEASELIST'} = 'IPADDR';
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');
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}");
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);
109 &Header
::getcgihash
(\
%dhcpsettings);
111 open(FILE
, "$filename1") or die 'Unable to open dhcp advanced options file.';
112 our @current1 = <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]);
121 open(FILE
, "$filename2") or die 'Unable to open fixed leases file.';
122 our @current2 = <FILE
>;
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'};
135 if (!$dhcpsettings{"END_ADDR_${itf}"}) {
136 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid end address'};
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'};
147 if ($dhcpsettings{"END_ADDR_${itf}"}) {
148 if (!(&General
::validip
($dhcpsettings{"END_ADDR_${itf}"}))) {
149 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid end address'};
152 if (!$dhcpsettings{"START_ADDR_${itf}"}) {
153 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid start address'};
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'};
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}"});
171 if (!($dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} =~ /^\d+$/)) {
172 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid default lease time'} . $dhcpsettings{'DEFAULT_LEASE_TIME_${itf}'};
176 if (!($dhcpsettings{"MAX_LEASE_TIME_${itf}"} =~ /^\d+$/)) {
177 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid max lease time'} . $dhcpsettings{'MAX_LEASE_TIME_${itf}'};
181 if ($dhcpsettings{"DNS1_${itf}"}) {
182 if (!(&General
::validip
($dhcpsettings{"DNS1_${itf}"}))) {
183 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid primary dns'};
187 if ($dhcpsettings{"DNS2_${itf}"}) {
188 if (!(&General
::validip
($dhcpsettings{"DNS2_${itf}"}))) {
189 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid secondary dns'};
192 if (! $dhcpsettings{"DNS1_${itf}"}) {
193 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'cannot specify secondary dns without specifying primary'};
198 if ($dhcpsettings{"WINS1_${itf}"}) {
199 if (!(&General
::validip
($dhcpsettings{"WINS1_${itf}"}))) {
200 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid wins address'};
204 if ($dhcpsettings{"WINS2_${itf}"}) {
205 if (!(&General
::validip
($dhcpsettings{"WINS2_${itf}"}))) {
206 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid wins address'};
209 if (! $dhcpsettings{"WINS1_${itf}"} ) {
210 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'cannot specify secondary wins without specifying primary'};
214 if ($dhcpsettings{"NEXT_${itf}"}) {
215 if (!(&General
::validip
($dhcpsettings{"NEXT_${itf}"}))) {
216 $errormessage = "next-server on ${itf}: " . $Lang::tr
{'invalid ip'};
220 if ($dhcpsettings{"NTP1_${itf}"}) {
221 if (!(&General
::validip
($dhcpsettings{"NTP1_${itf}"}))) {
222 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid primary ntp'};
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'};
230 if ($dhcpsettings{"NTP2_${itf}"}) {
231 if (!(&General
::validip
($dhcpsettings{"NTP2_${itf}"}))) {
232 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'invalid secondary ntp'};
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'};
239 if (! $dhcpsettings{"NTP1_${itf}"}) {
240 $errormessage = "DHCP on ${itf}: " . $Lang::tr
{'cannot specify secondary ntp without specifying primary'};
245 }#loop interface verify
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
252 ERROR
: # Leave the faulty field untouched
254 &General
::readhash
($setting, \
%dhcpsettings); # Get saved settings and reset to good if needed
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) {
269 $dhcpsettings{'SORT_FLEASELIST'}=$newsort;
270 map (delete ($dhcpsettings{$_}) ,@nosaved,'ACTION','KEY1','KEY2'); # Must never be saved
271 &General
::writehash
($setting, \
%dhcpsettings);
273 $dhcpsettings{'ACTION'} = 'SORT'; # create an 'ACTION'
274 map ($dhcpsettings{$_} = '',@nosaved,'KEY1','KEY2');# and reinit vars to empty
277 #Sorting of allocated leases
278 &Header
::CheckSortOrder
;
281 ## Now manipulate the two multi-line list with Settings2.
282 # '1' suffix is for ADVANCED OPTIONS
283 # '2' suffix is for FIXED LEASES
285 # Toggle enable/disable field on specified options.
287 if ($dhcpsettings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}.'1') {
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;
299 #Write changes to dhcpd.conf.
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 !
310 if ($dhcpsettings{'ADVOPT_DATA'} eq '') {
311 $errormessage=$Lang::tr
{'dhcp advopt blank value'};
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'})) {
326 } elsif (! `grep "\$option $dhcpsettings{'ADVOPT_NAME'} " $filename3`) {
327 $errormessage=$Lang::tr
{'dhcp advopt unknown'}.': '.$dhcpsettings{'ADVOPT_NAME'};
330 unless ($errormessage) {
333 foreach my $itf (@ITFs) { # buils "RED,GREEN,ORANGE,... based on selection
334 $scope .= $dhcpsettings{"ADVOPT_SCOPE_${itf}"} eq 'on' ?
"\t$itf" : "\toff" ;
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'});
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'});
345 #Write changes to dhcpd.conf.
346 &sortcurrent1
; # sort newly added/modified entry
347 &buildconf
; # before calling buildconf which use fixed lease file !
351 if ($dhcpsettings{'ACTION'} eq $Lang::tr
{'edit'}.'1') {
353 my $line = @current1[$dhcpsettings{'KEY1'}];
355 my @temp = split(/\t/, $line);
356 $dhcpsettings{'ADVOPT_ENABLED'}=$temp[0];
357 $dhcpsettings{'ADVOPT_NAME'}=$temp[1];
358 $dhcpsettings{'ADVOPT_DATA'}=$temp[2];
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
365 $dhcpsettings{"ADVOPT_SCOPE_${itf}"} = 'on';
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;
375 $dhcpsettings{'KEY1'} = ''; # End remove mode
376 &General
::log($Lang::tr
{'dhcp advopt removed'});
377 #Write changes to dhcpd.conf.
383 # Toggle enable/disable field on specified lease.
384 if ($dhcpsettings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}.'2') {
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;
396 #Write changes to dhcpd.conf.
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'}; }
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';
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'}";
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'});
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'});
447 #Write changes to dhcpd.conf.
448 &sortcurrent2
; # sort newly added/modified entry
449 &buildconf
; # before calling buildconf which use fixed lease file !
453 if ($dhcpsettings{'ACTION_ALL'} eq '+') {
455 foreach (keys %dhcpsettings) {
456 if (/^(\d+\.\d+\.\d+\.\d+)-([0-9a-fA-F:]+)$/) { # checked names are index of the line
459 if (!grep (/$2/,@current2)) {
460 unshift (@current2, "$mac,$ip,on,,,,imported\n");
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 !
474 if ($dhcpsettings{'ACTION'} eq $Lang::tr
{'edit'}.'2') {
476 my $line = @current2[$dhcpsettings{'KEY2'}];
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];
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;
493 $dhcpsettings{'KEY2'} = ''; # End remove mode
494 &General
::log($Lang::tr
{'fixed ip lease removed'});
495 #Write changes to dhcpd.conf.
503 if ($dhcpsettings{'ACTION'} eq '' ) { # First launch from GUI
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'};
514 $dhcpsettings{'FIX_ENABLED'} = 'on';
517 &Header
::openpage
($Lang::tr
{'dhcp configuration'}, 1, '');
518 &Header
::openbigbox
('100%', 'left', '', $errormessage);
521 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
522 print "<font class='base'>$errormessage </font>\n";
525 if ($warnNTPmessage) {
526 $warnNTPmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnNTPmessage";
529 &Header
::openbox
('100%', 'left', 'DHCP');
530 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
532 foreach my $itf (@ITFs) {
534 $checked{'ENABLE'}{'on'} = ( $dhcpsettings{"ENABLE_${itf}"} ne 'on') ?
'' : "checked='checked'";
535 $checked{'ENABLEBOOTP'}{'on'} = ( $dhcpsettings{"ENABLEBOOTP_${itf}"} ne 'on') ?
'' : "checked='checked'";
537 if ($netsettings{"${itf}_DEV"} ne '' ) { # Show only defined interface
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>
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>
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>
557 <td class='base'>$Lang::tr{'domain name suffix'} <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>
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'} <img src='/blob.gif' alt='*' /></td>
565 <td><input type='text' name='DNS2_${itf}' value='$dhcpsettings{"DNS2_${itf}"}' /></td>
567 <td class='base'>$Lang::tr{'primary ntp server'}: <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'}: <img src='/blob.gif' alt='*' /></td>
570 <td><input type='text' name='NTP2_${itf}' value='$dhcpsettings{"NTP2_${itf}"}' /></td>
572 <td class='base'>$Lang::tr{'primary wins server address'}: <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'}: <img src='/blob.gif' alt='*' /></td>
575 <td><input type='text' name='WINS2_${itf}' value='$dhcpsettings{"WINS2_${itf}"}' /></td>
577 <td class='base'>next-server: <img src='/blob.gif' alt='*' /></td>
578 <td><input type='text' name='NEXT_${itf}' value='$dhcpsettings{"NEXT_${itf}"}' /></td>
579 <td class='base'>filename: <img src='/blob.gif' alt='*' /></td>
580 <td><input type='text' name='FILE_${itf}' value='$dhcpsettings{"FILE_${itf}"}' /></td>
586 }# Show only defined interface
591 <td class='base' width='25%'><img src='/blob.gif' align='top' alt='*' /> $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>
602 &Header
::openbox
('100%', 'left', $Lang::tr
{'dhcp advopt list'});
603 # DHCP Advanced options settings
605 $checked{'ADVOPT_ENABLED'}{'on'} = ($dhcpsettings{'ADVOPT_ENABLED'} ne 'on') ?
'' : "checked='checked'";
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>";
613 print "<tr><td class='boldbase'><b>$Lang::tr{'dhcp advopt add'}</b></td></tr>"
616 #search if the 'option' is in the list and print the syntax model
617 my $opt = `grep "\$option $dhcpsettings{'ADVOPT_NAME'} " $filename3`;
619 $opt =~ s/option $dhcpsettings{'ADVOPT_NAME'}/Syntax:/; # "option xyz abc" => "syntax: abc"
621 $opt = "<tr><td></td><td></td><td colspan='2'>$opt</td></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>
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'}: <img src='/blob.gif' alt='*' /></td>
636 # Put a checkbox for each interface. Checkbox visible disabled if interface is disabled
637 foreach my $itf (@ITFs) {
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 " ";
652 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' /> $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'}' />
664 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
666 # print help taken from the file describing options
667 if ($dhcpsettings{'SUBMIT'} eq $Lang::tr
{'dhcp advopt help'}) {
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
>;
674 foreach my $line (@current3) {
675 $line =~ /option ([a-z0-9-]+) (.*);/;
676 print "<tr><td>$1</td><td>$2</td></tr>\n";
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>';
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>';
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>
708 foreach my $line (@current1) {
711 chomp($line); # remove newline
712 my @temp = split(/\t/,$line);
714 if ($temp[0] eq "on") {
716 $gdesc = $Lang::tr
{'click to disable'};
719 $gdesc = $Lang::tr
{'click to enable'};
722 if ($dhcpsettings{'KEY1'} eq $key) {
723 print "<tr bgcolor='${Header::colouryellow}'>";
725 print "<tr bgcolor='$color{'color22'}'>";
727 print "<tr bgcolor='$color{'color20'}'>";
731 <td align='center'>$temp[1]</td>
732 <td align='center'>$temp[2]</td>
736 # Prepare a global flag to make easy reading
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]));
746 $global = $Lang::tr
{'dhcp advopt scope global'};
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
755 $global=''; # fall to local scope !
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' />
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' />
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' />
791 # If there are dhcp options, print Key to action icons
796 <td class='boldbase'> <b>$Lang::tr{'legend'}: </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> </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> </td>
803 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
804 <td class='base'>$Lang::tr{'edit'}</td>
805 <td> </td>
806 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
807 <td class='base'>$Lang::tr{'remove'}</td>
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'";
819 $buttontext = $Lang::tr
{'add'};
820 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>";
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>";
826 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'add new lease'}</b></td></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'}: <img src='/blob.gif' alt='*' /></td>
835 <td><input type='text' name='FIX_REMARK' value='$dhcpsettings{'FIX_REMARK'}' size='18' /></td>
837 <td class='base'>$Lang::tr{'enabled'}</td><td><input type='checkbox' name='FIX_ENABLED' $checked{'FIX_ENABLED'}{'on'} /></td>
839 <td colspan = '3'><b>$Lang::tr{'dhcp bootp pxe data'}</b></td>
841 <td class='base'>next-server: <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: <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: <img src='/blob.gif' alt='*' /></td>
846 <td><input type='text' name='FIX_ROOTPATH' value='$dhcpsettings{'FIX_ROOTPATH'}' size='18' /></td>
852 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' /> $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>
862 #Edited line number (KEY2) passed until cleared by 'save' or 'remove' or 'new sort order'
866 <table width='100%' class='tbl'>
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>
880 my %macdupl = (); # Duplicate MACs have to be on different subnets
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
890 $ipinuse{$temp[1]} += 1;
891 if ($ipinuse{$temp[1]} > 1) {
892 $ipdup = 1; # Flag up duplicates for use later
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;
907 foreach my $line (@current2) {
910 chomp($line); # remove newline
911 my @temp = split(/\,/,$line);
913 if ($temp[2] eq "on") {
915 $gdesc = $Lang::tr
{'click to disable'};
918 $gdesc = $Lang::tr
{'click to enable'};
921 if ($dhcpsettings{'KEY2'} eq $key) {
923 $col="bgcolor='${Header::colouryellow}'";
926 $col="bgcolor='$color{'color20'}'";
929 $col="bgcolor='$color{'color22'}'";
936 if ($ipinuse{$temp[1]} > 1) {
940 if ($macdupl{$temp[0]} > 1) {
944 if ($ipoutside{$temp[1]} > 0) {
945 $TAG4 = "bgcolor='orange'" if ($dhcpsettings{'KEY2'} ne $key);
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] </td>
952 <td align='center' $col>$temp[3] </td>
953 <td align='center' $col>$temp[4] </td>
954 <td align='center' $col>$temp[5] </td>
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' />
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' />
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' />
986 # If the fixed lease file contains entries, print Key to action icons
988 my $dup = $ipdup ?
"<td class='base'>$Lang::tr{'duplicate ip bold'}</td>" :'';
992 <td class='boldbase'> <b>$Lang::tr{'legend'}: </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> </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> </td>
999 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
1000 <td class='base'>$Lang::tr{'edit'}</td>
1001 <td> </td>
1002 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
1003 <td class='base'>$Lang::tr{'remove'}</td>
1007 <td bgcolor='orange'> </td>
1008 <td class='base'>$Lang::tr{'ip address outside subnets'}</td>
1018 &Header
::closebox
();
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
1028 &Header
::closebigbox
();
1029 &Header
::closepage
();
1031 ## Ouf it's the end !
1033 sub sortcurrent1
# by now, do not sort, just write
1035 open(FILE
, ">$filename1") or die 'Unable to open dhcp advanced options file.';
1036 print FILE
@current1;
1041 # Sort the "current2" array according to choices
1046 sub fixedleasesort
{
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});
1058 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
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});
1070 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
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
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";
1092 # Reload sorted @current2
1093 open (FILE
, "$filename2");
1096 undef (%entries); #This array is reused latter. Clear it.
1099 # Build the configuration file mixing settings, fixed leases and advanced options
1101 open(FILE
, ">/${General::swroot}/dhcp/dhcpd.conf") or die "Unable to write dhcpd.conf file";
1105 print FILE
"ddns-update-style none;\n";
1106 print FILE
"deny bootp; #default\n";
1107 print FILE
"authoritative;\n";
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";
1117 # Write other global options
1118 foreach my $line (@current1) {
1119 chomp($line); # remove newline
1120 my @temp = split(/\t/,$line);
1122 if ($temp[0] eq 'on' && !ExistNewOptionDefinition
($temp[1] . ' ' . $temp[2])){ # active & !definition
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
1132 print FILE
"option $temp[1] $temp[2];\n";
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";
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');
1164 # Write scoped options
1165 foreach my $line (@current1) {
1166 chomp($line); # remove newline
1167 my @temp = split(/\t/,$line); # Use TAB separator !
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
1173 print FILE
"\toption $temp[1] $temp[2];\n";
1178 print FILE
"} #$itf\n";
1180 system ('/usr/bin/touch', "${General::swroot}/dhcp/enable_${lc_itf}");
1181 &General
::log("DHCP on ${itf}: " . $Lang::tr
{'dhcp server enabled'})
1183 unlink "${General::swroot}/dhcp/enable_${lc_itf}";
1184 &General
::log("DHCP on ${itf}: " . $Lang::tr
{'dhcp server disabled'})
1188 #write fixed leases if any. Does not handle duplicates to write them elsewhere than the global scope.
1190 foreach my $line (@current2) {
1192 my @temp = split(/\,/,$line);
1193 if ($temp[2] eq "on") {
1194 print FILE
"\nhost fix$key # $temp[6]\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]);
1205 print FILE
"include \"${General::swroot}/dhcp/dhcpd.conf.local\";\n";
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';
1213 # Receive a string and if it match model for a new option,
1214 # add it to the list %newOptions
1216 my %NewOptions = ();
1218 sub AddNewOptionDefinition
{
1220 if ( $line =~ /^([-\w]+)( code \d+=($OptionTypes))/ ) {
1221 $NewOptions{$1} = $2;
1222 #&General::log ("new:<$1><$2>");
1229 # Check existence of definition for a new option
1231 sub ExistNewOptionDefinition
{
1234 if ( $line =~ /^([-\w]+)( code \d+=($OptionTypes))/ ) {
1235 return defined $NewOptions{$1};
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
{
1246 if ($line =~ /^([-\w]+) (.*)/ ) {
1247 return defined ( $NewOptions{$1} ) && $2 !~ /code=/;
1253 # Check if the new option $opt is used, except the definition of itself!
1255 sub IsUsedNewOptionDefinition
{
1256 my ($opt,$val) = @_;
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+=/) );