dhcp: Create local configuration file.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / dhcp.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
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
ac1cfefa
MT
22use strict;
23
24# enable only the following on debugging purpose
cb5e9c6c
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
ac1cfefa 27
986e08d9 28require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31#workaround to suppress a warning when a variable is used only once
32my @dummy = ( ${Header::colouryellow} );
33undef (@dummy);
34
35our %dhcpsettings=();
36our %netsettings=();
37my %mainsettings=();
38my %timesettings=();
39my $setting = "${General::swroot}/dhcp/settings";
40our $filename1 = "${General::swroot}/dhcp/advoptions"; # Field separator is TAB in this file (comma is standart)
d1883e28 41 # because we need commas in the some data
ac1cfefa 42our $filename2 = "${General::swroot}/dhcp/fixleases";
d1883e28 43our $filename3 = "${General::swroot}/dhcp/advoptions-list"; # Describe the allowed syntax for dhcp options
ac1cfefa
MT
44my $errormessage = '';
45my $warnNTPmessage = '';
46my @nosaved=();
fe6cda92 47my %color = ();
ac1cfefa
MT
48
49#Basic syntax allowed for new Option definition. Not implemented: RECORDS & array of RECORDS
50our $OptionTypes = 'boolean|((un)?signed )?integer (8|16|32)|ip-address|text|string|encapsulate \w+|array of ip-address';
51
52&Header::showhttpheaders();
541d93f0
CS
53our @ITFs=('GREEN');
54if (&Header::blue_used()){push(@ITFs,'BLUE');}
ac1cfefa
MT
55
56#Settings1 for the first screen box
57foreach 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}"} = '';
d1883e28
MT
71 $dhcpsettings{"NEXT_${itf}"} = '';
72 $dhcpsettings{"FILE_${itf}"} = '';
ac1cfefa
MT
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'} = '';
96unshift (@nosaved,'ADVOPT_ENABLED','ADVOPT_NAME','ADVOPT_DATA');
97foreach 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);
fe6cda92 106&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
ac1cfefa
MT
107
108#Get GUI values
109&Header::getcgihash(\%dhcpsettings);
110
111open(FILE, "$filename1") or die 'Unable to open dhcp advanced options file.';
112our @current1 = <FILE>;
113close(FILE);
114# Extract OptionDefinition
115foreach 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
121open(FILE, "$filename2") or die 'Unable to open fixed leases file.';
122our @current2 = <FILE>;
123close(FILE);
124
125# Check Settings1 first because they are needed by &buildconf
126if ($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 }
d1883e28
MT
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 }
ac1cfefa
MT
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
258if ($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
287if ($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
305if ($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
351if ($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
370if ($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.
384if ($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
400if ($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
453if ($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
474if ($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
488if ($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
503if ($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
520if ($errormessage) {
521 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
522 print "<font class='base'>$errormessage&nbsp;</font>\n";
523 &Header::closebox();
524}
525if ($warnNTPmessage) {
526 $warnNTPmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnNTPmessage";
527}
528
529&Header::openbox('100%', 'left', 'DHCP');
530print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
531
532foreach 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);
539print <<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>
9c100957 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>
ac1cfefa 546</tr><tr>
95a392d5 547 <td width='25%' class='base'>$Lang::tr{'start address'}</td>
ac1cfefa 548 <td width='25%'><input type='text' name='START_ADDR_${itf}' value='$dhcpsettings{"START_ADDR_${itf}"}' /></td>
95a392d5 549 <td width='25%' class='base'>$Lang::tr{'end address'}</td>
ac1cfefa
MT
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>
d1883e28
MT
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>
ac1cfefa
MT
581</tr>
582</table>
583<hr />
584END
585;
586 }# Show only defined interface
587}#foreach itf
588print <<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='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
39a7cc11 594 <td width='5%' align='right'>&nbsp;</td>
ac1cfefa
MT
595</tr>
596</table>
597</form>
598END
599;
600
601&Header::closebox();
602
603&Header::openbox('100%', 'left', $Lang::tr{'dhcp advopt list'});
604# DHCP Advanced options settings
605my %checked=();
606$checked{'ADVOPT_ENABLED'}{'on'} = ($dhcpsettings{'ADVOPT_ENABLED'} ne 'on') ? '' : "checked='checked'";
607
608print "<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>";
609my $buttontext = $Lang::tr{'add'};
610if ($dhcpsettings{'KEY1'} ne '') {
611 $buttontext = $Lang::tr{'update'};
612 print "<tr><td class='boldbase'><b>$Lang::tr{'dhcp advopt edit'}</b></td></tr>";
613} else {
614 print "<tr><td class='boldbase'><b>$Lang::tr{'dhcp advopt add'}</b></td></tr>"
615}
616
617#search if the 'option' is in the list and print the syntax model
618my $opt = `grep "\$option $dhcpsettings{'ADVOPT_NAME'} " $filename3`;
619if ($opt ne '') {
620 $opt =~ s/option $dhcpsettings{'ADVOPT_NAME'}/Syntax:/; # "option xyz abc" => "syntax: abc"
621 $opt =~ s/;//;
622 $opt = "<tr><td></td><td></td><td colspan='2'>$opt</td></tr>";
623}
624print <<END
625<tr>
626 <td class='base'>$Lang::tr{'dhcp advopt name'}:</td>
627 <td><input type='text' name='ADVOPT_NAME' value='$dhcpsettings{'ADVOPT_NAME'}' size='18' /></td>
628 <td class='base'>$Lang::tr{'dhcp advopt value'}:</td>
629 <td><input type='text' name='ADVOPT_DATA' value='$dhcpsettings{'ADVOPT_DATA'}' size='40' /></td>
630</tr>$opt<tr>
631 <td class='base'>$Lang::tr{'enabled'}</td><td><input type='checkbox' name='ADVOPT_ENABLED' $checked{'ADVOPT_ENABLED'}{'on'} /></td>
632 <td class='base'>$Lang::tr{'dhcp advopt scope'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
633 <td>
634END
635;
636
637# Put a checkbox for each interface. Checkbox visible disabled if interface is disabled
638foreach my $itf (@ITFs) {
639 my $lc_itf=lc($itf);
640 $checked{'ADVOPT_SCOPE_${itf}'}{'on'} = $dhcpsettings{"ADVOPT_SCOPE_${itf}"} ne 'on' ? '' : "checked='checked'";
641 print "$Lang::tr{\"${lc_itf}\"} <input type='checkbox' name='ADVOPT_SCOPE_${itf}' $checked{'ADVOPT_SCOPE_${itf}'}{'on'} ";
642 print $dhcpsettings{"ENABLE_${itf}"} eq 'on' ? "/>" : "disabled='disabled' />";
643 print "&nbsp; &nbsp;";
644}
645
646print <<END
647 </td>
648</tr>
649</table>
650<hr />
651<table width='100%'>
652<tr>
653 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'dhcp advopt scope help'}</td>
654 <td width='50%' align='center'>
655 <input type='hidden' name='ACTION' value='$Lang::tr{'add'}1' />
656 <input type='submit' name='SUBMIT' value='$buttontext' />
657 <input type='submit' name='SUBMIT' value='$Lang::tr{'dhcp advopt help'}' />
658 <input type='hidden' name='KEY1' value='$dhcpsettings{'KEY1'}' />
659 </td>
660</tr>
661</table>
662</form>
663END
664;
665#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
666
667# print help taken from the file describing options
668if ($dhcpsettings{'SUBMIT'} eq $Lang::tr{'dhcp advopt help'}) {
669 print "<hr />";
670 print "<table width='100%'>";
671 print "<tr><td width='30%'><b>$Lang::tr{'dhcp advopt name'}</b></td><td width='70%'><b>$Lang::tr{'dhcp advopt value'}</b></td>";
672 open(FILE, "$filename3");
673 my @current3 = <FILE>;
674 close(FILE);
675 foreach my $line (@current3) {
676 $line =~ /option ([a-z0-9-]+) (.*);/;
677 print "<tr><td>$1</td><td>$2</td></tr>\n";
678 }
679 print "<tr><td colspan='2'><hr /></td></tr>\n";
680 print '<tr><td>string type</td><td>"quoted string" or 00:01:FF...</td></tr>';
681 print '<tr><td>ip-address type </td><td>10.0.0.1 | www.dot.com</td></tr>';
682 print '<tr><td>int,uint types</td><td>numbers</td></tr>';
683 print '<tr><td>flag type</td><td>on | off</td></tr>';
684 print '</table>';
685 print "<hr />";
686 print "<table width='100%'>";
687 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>";
688 print "<tr><td>any-name </td><td> code NNN=$OptionTypes</td></tr>";
689 print '<tr><td>a-string</td><td>code 100=string</td></tr>';
690 print '<tr><td>a-number</td><td>code 101=signed integer 8</td></tr>';
691 print '<tr><td>wpad</td><td>code 252=text</td></tr>';
692 print '<tr><td>wpad</td><td>"http://www.server.fr/path-to/proxy.pac"</td></tr>';
693 print '</table>';
694
695}
696
697print <<END
698<hr />
699<table width='100%'>
700<tr>
701 <td width='30%' class='boldbase' align='center'><b>$Lang::tr{'dhcp advopt name'}</b></td>
702 <td width='50%' class='boldbase' align='center'><b>$Lang::tr{'dhcp advopt value'}</b></td>
703 <td width='20%' class='boldbase' align='center'><b>$Lang::tr{'dhcp advopt scope'}</b></td>
704 <td colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>
705</tr>
706END
707;
708my $key = 0;
709foreach my $line (@current1) {
710 my $gif = '';
711 my $gdesc = '';
712 chomp($line); # remove newline
713 my @temp = split(/\t/,$line);
714
715 if ($temp[0] eq "on") {
716 $gif = 'on.gif';
717 $gdesc = $Lang::tr{'click to disable'};
718 } else {
719 $gif = 'off.gif';
720 $gdesc = $Lang::tr{'click to enable'};
721 }
722
723 if ($dhcpsettings{'KEY1'} eq $key) {
724 print "<tr bgcolor='${Header::colouryellow}'>";
725 } elsif ($key % 2) {
fe6cda92 726 print "<tr bgcolor='$color{'color22'}'>";
ac1cfefa 727 } else {
fe6cda92 728 print "<tr bgcolor='$color{'color20'}'>";
ac1cfefa
MT
729 }
730
731 print <<END
732<td align='center'>$temp[1]</td>
733<td align='center'>$temp[2]</td>
734<td align='center'>
735END
736;
737 # Prepare a global flag to make easy reading
738 my $global = '';
739 my $disabledTogle = '';
740 my $disabledEditRemove = '';
741 if ( ExistNewOptionDefinition ($temp[1] . ' ' . $temp[2]) ) {
742 $global = $Lang::tr{'dhcp advopt definition'};
743 $disabledTogle = "disabled='disabled'";
744 # Search if it is a used NewOptionDefinition to also disable edit & delete
745 $disabledEditRemove = "disabled='disabled'" if (IsUsedNewOptionDefinition ($temp[1], $temp[2]));
746 } else {
747 $global = $Lang::tr{'dhcp advopt scope global'};
748 }
749
750
751 # Print each checked interface
752 for (my $key=0; $key<@ITFs; $key++) {
753 my $itf = $temp[3+$key];
754 if ($itf ne 'off') { # Only if an interface name is read
755 print "$itf";
756 $global=''; # fall to local scope !
757 }
758 }
759 print <<END
760$global</td>
761<td align='center'>
762<form method='post' action='$ENV{'SCRIPT_NAME'}'>
763<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}1' />
764<input $disabledTogle type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
765<input type='hidden' name='KEY1' value='$key' />
766</form>
767</td>
768
769<td align='center'>
770<form method='post' action='$ENV{'SCRIPT_NAME'}'>
771<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}1' />
772<input $disabledEditRemove type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
773<input type='hidden' name='KEY1' value='$key' />
774</form>
775</td>
776
777<td align='center'>
778<form method='post' action='$ENV{'SCRIPT_NAME'}'>
779<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}1' />
780<input $disabledEditRemove type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
781<input type='hidden' name='KEY1' value='$key' />
782</form>
783</td>
784</tr>
785END
786;
787 $key++;
788}
789
790print "</table>";
791
792# If there are dhcp options, print Key to action icons
793if ($key) {
794print <<END
795<table>
796<tr>
797 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
798 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
799 <td class='base'>$Lang::tr{'click to disable'}</td>
800 <td>&nbsp;&nbsp;</td>
801 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
802 <td class='base'>$Lang::tr{'click to enable'}</td>
803 <td>&nbsp;&nbsp;</td>
804 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
805 <td class='base'>$Lang::tr{'edit'}</td>
806 <td>&nbsp;&nbsp;</td>
807 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
808 <td class='base'>$Lang::tr{'remove'}</td>
809</tr>
810</table>
811END
812;
813}
814&Header::closebox();
815
816&Header::openbox('100%', 'left', $Lang::tr{'current fixed leases'});
817# Fixed leases screens
818$checked{'FIX_ENABLED'}{'on'} = ($dhcpsettings{'FIX_ENABLED'} ne 'on') ? '' : "checked='checked'";
819
820$buttontext = $Lang::tr{'add'};
821print "<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>";
822
823if ($dhcpsettings{'KEY2'} ne '') {
824 $buttontext = $Lang::tr{'update'};
825 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'edit an existing lease'}</b></td></tr>";
826} else {
827 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'add new lease'}</b></td></tr>"
828}
829print <<END
830<tr>
831 <td class='base'>$Lang::tr{'mac address'}:</td>
832 <td><input type='text' name='FIX_MAC' value='$dhcpsettings{'FIX_MAC'}' size='18' /></td>
833 <td class='base'>$Lang::tr{'ip address'}:</td>
834 <td><input type='text' name='FIX_ADDR' value='$dhcpsettings{'FIX_ADDR'}' size='18' /></td>
835 <td class='base'>$Lang::tr{'remark'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
836 <td><input type='text' name='FIX_REMARK' value='$dhcpsettings{'FIX_REMARK'}' size='18' /></td>
837</tr><tr>
838 <td class='base'>$Lang::tr{'enabled'}</td><td><input type='checkbox' name='FIX_ENABLED' $checked{'FIX_ENABLED'}{'on'} /></td>
839</tr><tr>
840 <td colspan = '3'><b>$Lang::tr{'dhcp bootp pxe data'}</b></td>
841</tr><tr>
842 <td class='base'>next-server:&nbsp;<img src='/blob.gif' alt='*' /></td>
843 <td><input type='text' name='FIX_NEXTADDR' value='$dhcpsettings{'FIX_NEXTADDR'}' size='18' /></td>
844 <td class='base'>filename:&nbsp;<img src='/blob.gif' alt='*' /></td>
845 <td><input type='text' name='FIX_FILENAME' value='$dhcpsettings{'FIX_FILENAME'}' size='18' /></td>
846 <td class='base'>root path:&nbsp;<img src='/blob.gif' alt='*' /></td>
847 <td><input type='text' name='FIX_ROOTPATH' value='$dhcpsettings{'FIX_ROOTPATH'}' size='18' /></td>
848</tr>
849</table>
850<hr />
851<table width='100%'>
852<tr>
853 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
854 <td width='50%' align='center'>
855 <input type='hidden' name='ACTION' value='$Lang::tr{'add'}2' />
856 <input type='submit' name='SUBMIT' value='$buttontext' />
857 <input type='hidden' name='KEY2' value='$dhcpsettings{'KEY2'}' /></td>
858</tr>
859</table>
860</form>
861END
862;
863#Edited line number (KEY2) passed until cleared by 'save' or 'remove' or 'new sort order'
864
865print <<END
866<hr />
867<table width='100%'>
868<tr>
869 <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?FETHER'><b>$Lang::tr{'mac address'}</b></a></td>
870 <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?FIPADDR'><b>$Lang::tr{'ip address'}</b></a></td>
871 <td width='15%' align='center'><b>$Lang::tr{'remark'}</b></td>
872 <td width='15%' class='boldbase' align='center'><b>next-server</b></td>
873 <td width='15%' class='boldbase' align='center'><b>filename</b></td>
874 <td width='15%' class='boldbase' align='center'><b>root path</b></td>
875 <td colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>
876</tr>
877END
878;
879my $ipdup = 0;
880my %ipinuse = ();
881my %macdupl = (); # Duplicate MACs have to be on different subnets
882my %ipoutside = ();
883
884# mark duplicate ip or duplicate MAC
885foreach my $line (@current2) {
886 my @temp = split(/\,/,$line);
887 $macdupl{$temp[0]} += 1;
888 if ($macdupl{$temp[0]} > 1) {
889 $ipdup = 1; # Flag up duplicates for use later
890 }
891 $ipinuse{$temp[1]} += 1;
892 if ($ipinuse{$temp[1]} > 1) {
893 $ipdup = 1; # Flag up duplicates for use later
894 }
895 # Mark IP addresses outwith known subnets
896 $ipoutside{$temp[1]} = 1;
897 foreach my $itf (@ITFs) {
898 if ( &General::IpInSubnet($temp[1],
899 $netsettings{"${itf}_NETADDRESS"},
900 $netsettings{"${itf}_NETMASK"})) {
901 $ipoutside{$temp[1]} = 0;
902 }
903 }
904}
905
906$key = 0;
907foreach 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 bgcolor='${Header::colouryellow}'>";
923 } elsif ($key % 2) {
fe6cda92 924 print "<tr bgcolor='$color{'color22'}'>";
ac1cfefa 925 } else {
fe6cda92 926 print "<tr bgcolor='$color{'color20'}'>";
ac1cfefa
MT
927 }
928 my $TAG0 = '';
929 my $TAG1 = '';
930 my $TAG2 = '';
931 my $TAG3 = '';
932 my $TAG4 = '';
933 if ($ipinuse{$temp[1]} > 1) {
934 $TAG0 = '<b>';
935 $TAG1 = '</b>';
936 }
937 if ($macdupl{$temp[0]} > 1) {
938 $TAG2 = '<b>';
939 $TAG3 = '</b>';
940 }
941 if ($ipoutside{$temp[1]} > 0) {
942 $TAG4 = "bgcolor='orange'" if ($dhcpsettings{'KEY2'} ne $key);
943 }
944
945 print <<END
946<td align='center'>$TAG2$temp[0]$TAG3</td>
947<td align='center' $TAG4>$TAG0$temp[1]$TAG1</td>
948<td align='center'>$temp[6]&nbsp;</td>
949<td align='center'>$temp[3]&nbsp;</td>
950<td align='center'>$temp[4]&nbsp;</td>
951<td align='center'>$temp[5]&nbsp;</td>
952
953<td align='center'>
954<form method='post' action='$ENV{'SCRIPT_NAME'}'>
955<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}2' />
956<input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
957<input type='hidden' name='KEY2' value='$key' />
958</form>
959</td>
960
961<td align='center'>
962<form method='post' action='$ENV{'SCRIPT_NAME'}'>
963<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}2' />
964<input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
965<input type='hidden' name='KEY2' value='$key' />
966</form>
967</td>
968
969<td align='center'>
970<form method='post' action='$ENV{'SCRIPT_NAME'}'>
971<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}2' />
972<input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
973<input type='hidden' name='KEY2' value='$key' />
974</form>
975</td>
976</tr>
977END
978;
979 $key++;
980}
981print "</table>";
982
983# If the fixed lease file contains entries, print Key to action icons
984if ($key) {
985my $dup = $ipdup ? "<td class='base'>$Lang::tr{'duplicate ip bold'}</td>" :'';
986print <<END
987<table>
988<tr>
989 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
990 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
991 <td class='base'>$Lang::tr{'click to disable'}</td>
992 <td>&nbsp;&nbsp;</td>
993 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
994 <td class='base'>$Lang::tr{'click to enable'}</td>
995 <td>&nbsp;&nbsp;</td>
996 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
997 <td class='base'>$Lang::tr{'edit'}</td>
998 <td>&nbsp;&nbsp;</td>
999 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
1000 <td class='base'>$Lang::tr{'remove'}</td>
1001</tr>
1002<tr>
1003 <td>&nbsp;</td>
1004 <td bgcolor='orange'>&nbsp;</td>
1005 <td class='base'>$Lang::tr{'ip address outside subnets'}</td>
1006 <td>&nbsp;</td>
1007 <td>&nbsp;</td>
1008 $dup
1009</tr>
1010</table>
1011END
1012;
1013}
1014
1015&Header::closebox();
1016
ac1cfefa
MT
1017foreach my $itf (@ITFs) {
1018 if ($dhcpsettings{"ENABLE_${itf}"} eq 'on') {
1019 # display leases with a list of actions to do with the global select checkbox.
1020 &Header::PrintActualLeases("+"); # "+" => create fixed leases from nodeaddress
1021 last; #Print one time only for all interfaces
1022 };
1023}
1024
1025&Header::closebigbox();
1026&Header::closepage();
1027
1028## Ouf it's the end !
1029
1030sub sortcurrent1 # by now, do not sort, just write
1031{
1032 open(FILE, ">$filename1") or die 'Unable to open dhcp advanced options file.';
1033 print FILE @current1;
1034 close(FILE);
1035}
1036
1037
1038# Sort the "current2" array according to choices
1039sub sortcurrent2
1040{
1041 our %entries = ();
1042
1043 sub fixedleasesort {
1044 my $qs='';
1045 if (rindex ($dhcpsettings{'SORT_FLEASELIST'},'Rev') != -1) {
1046 $qs=substr ($dhcpsettings{'SORT_FLEASELIST'},0,length($dhcpsettings{'SORT_FLEASELIST'})-3);
1047 if ($qs eq 'FIPADDR') {
1048 my @a = split(/\./,$entries{$a}->{$qs});
1049 my @b = split(/\./,$entries{$b}->{$qs});
1050 ($b[0]<=>$a[0]) ||
1051 ($b[1]<=>$a[1]) ||
1052 ($b[2]<=>$a[2]) ||
1053 ($b[3]<=>$a[3]);
1054 } else {
1055 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
1056 }
1057 } else { #not reverse
1058 $qs=$dhcpsettings{'SORT_FLEASELIST'};
1059 if ($qs eq 'FIPADDR') {
1060 my @a = split(/\./,$entries{$a}->{$qs});
1061 my @b = split(/\./,$entries{$b}->{$qs});
1062 ($a[0]<=>$b[0]) ||
1063 ($a[1]<=>$b[1]) ||
1064 ($a[2]<=>$b[2]) ||
1065 ($a[3]<=>$b[3]);
1066 } else {
1067 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
1068 }
1069 }
1070 }
1071
1072 #Use an associative array (%entries)
1073 foreach my $line (@current2) {
1074 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
1075 my @temp = split (',',$line);
1076 my @record = ('FETHER',$temp[0],'FIPADDR',$temp[1],'DATA',join(',',@temp[2..6]));
1077 my $record = {}; # create a reference to empty hash
1078 %{$record} = @record; # populate that hash with @record
1079 # use combination of ether & IP as key to allow duplicates in either but not both
1080 $entries{$record->{FETHER} . $record->{FIPADDR}} = $record; # add this to a hash of hashes
1081 }
1082
1083 open(FILE, ">$filename2") or die 'Unable to open fixed lease file.';
1084 foreach my $entry ( sort fixedleasesort keys %entries) {
1085 print FILE "$entries{$entry}->{FETHER},$entries{$entry}->{FIPADDR},$entries{$entry}->{DATA}\n";
1086 }
1087 close(FILE);
1088
1089 # Reload sorted @current2
1090 open (FILE, "$filename2");
1091 @current2 = <FILE>;
1092 close (FILE);
1093 undef (%entries); #This array is reused latter. Clear it.
1094}
1095
1096# Build the configuration file mixing settings, fixed leases and advanced options
1097sub buildconf {
1098 open(FILE, ">/${General::swroot}/dhcp/dhcpd.conf") or die "Unable to write dhcpd.conf file";
1099 flock(FILE, 2);
1100
1101 # Global settings
1102 print FILE "ddns-update-style none;\n";
1103 print FILE "deny bootp; #default\n";
1104 print FILE "authoritative;\n";
1105
1106 # Write first new option definition
1107 foreach my $line (@current1) {
1108 chomp($line); # remove newline
1109 my @temp = split(/\t/,$line);
1110 if (ExistNewOptionDefinition ($temp[1] . ' ' . $temp[2])) {
1111 print FILE "option $temp[1] $temp[2];\n";
1112 }
1113 }
1114 # Write other global options
1115 foreach my $line (@current1) {
1116 chomp($line); # remove newline
1117 my @temp = split(/\t/,$line);
1118
1119 if ($temp[0] eq 'on' && !ExistNewOptionDefinition ($temp[1] . ' ' . $temp[2])){ # active & !definition
1120 my $global=1;
1121 for (my $key=0; $key<@ITFs; $key++) {
1122 my $itf = $temp[3+$key];
1123 if ($itf ne 'off') # Only if an interface name is read
1124 {
1125 $global=0;
1126 }
1127 }
1128 if ($global) {
1129 print FILE "option $temp[1] $temp[2];\n";
1130 }
1131 }# on
1132 }# foreach line
1133
1134 #Subnet range definition
1135 foreach my $itf (@ITFs) {
1136 my $lc_itf=lc($itf);
1137 if ($dhcpsettings{"ENABLE_${itf}"} eq 'on' ){
1138 print FILE "\nsubnet " . $netsettings{"${itf}_NETADDRESS"} . " netmask ". $netsettings{"${itf}_NETMASK"} . " #$itf\n";
1139 print FILE "{\n";
1140 print FILE "\trange " . $dhcpsettings{"START_ADDR_${itf}"} . ' ' . $dhcpsettings{"END_ADDR_${itf}"}.";\n" if ($dhcpsettings{"START_ADDR_${itf}"});
1141 print FILE "\toption subnet-mask " . $netsettings{"${itf}_NETMASK"} . ";\n";
1142 print FILE "\toption domain-name \"" . $dhcpsettings{"DOMAIN_NAME_${itf}"} . "\";\n";
1143 print FILE "\toption routers " . $netsettings{"${itf}_ADDRESS"} . ";\n";
1144 print FILE "\toption domain-name-servers " . $dhcpsettings{"DNS1_${itf}"} if ($dhcpsettings{"DNS1_${itf}"});
1145 print FILE ", " . $dhcpsettings{"DNS2_${itf}"} if ($dhcpsettings{"DNS2_${itf}"});
1146 print FILE ";\n" if ($dhcpsettings{"DNS1_${itf}"});
1147 print FILE "\toption ntp-servers " . $dhcpsettings{"NTP1_${itf}"} if ($dhcpsettings{"NTP1_${itf}"});
1148 print FILE ", " . $dhcpsettings{"NTP2_${itf}"} if ($dhcpsettings{"NTP2_${itf}"});
1149 print FILE ";\n" if ($dhcpsettings{"NTP1_${itf}"});
1150 print FILE "\toption netbios-name-servers " . $dhcpsettings{"WINS1_${itf}"} if ($dhcpsettings{"WINS1_${itf}"});
1151 print FILE ", " . $dhcpsettings{"WINS2_${itf}"} if ($dhcpsettings{"WINS2_${itf}"});
1152 print FILE ";\n" if ($dhcpsettings{"WINS1_${itf}"});
d1883e28
MT
1153 print FILE "\tnext-server " . $dhcpsettings{"NEXT_${itf}"} . ";\n" if ($dhcpsettings{"NEXT_${itf}"});
1154 print FILE "\tfilename \"" . $dhcpsettings{"FILE_${itf}"} . "\";\n" if ($dhcpsettings{"FILE_${itf}"});
ac1cfefa
MT
1155 print FILE "\tdefault-lease-time " . ($dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} * 60). ";\n";
1156 print FILE "\tmax-lease-time " . ($dhcpsettings{"MAX_LEASE_TIME_${itf}"} * 60) . ";\n";
1157 print FILE "\tallow bootp;\n" if ($dhcpsettings{"ENABLEBOOTP_${itf}"} eq 'on');
1158
1159
1160
1161 # Write scoped options
1162 foreach my $line (@current1) {
1163 chomp($line); # remove newline
1164 my @temp = split(/\t/,$line); # Use TAB separator !
1165
1166 if ($temp[0] eq 'on'){
1167 for (my $key=0; $key<@ITFs; $key++) {
1168 if ($itf eq $temp[3+$key]) # Only is an interface name is read
1169 {
1170 print FILE "\toption $temp[1] $temp[2];\n";
1171 }
1172 }
1173 }# on
1174 }# foreach line
1175 print FILE "} #$itf\n";
1176
9833e7d8 1177 system ('/usr/bin/touch', "${General::swroot}/dhcp/enable_${lc_itf}");
ac1cfefa
MT
1178 &General::log("DHCP on ${itf}: " . $Lang::tr{'dhcp server enabled'})
1179 } else {
1180 unlink "${General::swroot}/dhcp/enable_${lc_itf}";
1181 &General::log("DHCP on ${itf}: " . $Lang::tr{'dhcp server disabled'})
1182 }
1183 }
1184
1185 #write fixed leases if any. Does not handle duplicates to write them elsewhere than the global scope.
1186 my $key = 0;
1187 foreach my $line (@current2) {
1188 chomp($line);
1189 my @temp = split(/\,/,$line);
1190 if ($temp[2] eq "on") {
1191 print FILE "\nhost fix$key # $temp[6]\n";
1192 print FILE "{\n";
1193 print FILE "\thardware ethernet $temp[0];\n";
1194 print FILE "\tfixed-address $temp[1];\n";
1195 print FILE "\tnext-server $temp[3];\n" if ($temp[3]);
1196 print FILE "\tfilename \"$temp[4]\";\n" if ($temp[4]);
1197 print FILE "\toption root-path \"$temp[5]\";\n" if ($temp[5]);
1198 print FILE "}\n";
1199 $key++;
1200 }
1201 }
3bd3aa72 1202 print FILE "include ${General::swroot}/dhcp/dhcpd.conf.local\n";
ac1cfefa 1203 close FILE;
32ab16de
CS
1204 if ( $dhcpsettings{"ENABLE_GREEN"} eq 'on' || $dhcpsettings{"ENABLE_BLUE"} eq 'on' ) {system '/usr/local/bin/dhcpctrl enable >/dev/null 2>&1';}
1205 else {system '/usr/local/bin/dhcpctrl disable >/dev/null 2>&1';}
6bdd769d 1206 system '/usr/local/bin/dhcpctrl restart >/dev/null 2>&1';
ac1cfefa
MT
1207}
1208
1209#
1210# Receive a string and if it match model for a new option,
1211# add it to the list %newOptions
1212#
1213my %NewOptions = ();
1214
1215sub AddNewOptionDefinition {
1216 my ($line) = @_;
1217 if ( $line =~ /^([-\w]+)( code \d+=($OptionTypes))/ ) {
1218 $NewOptions{$1} = $2;
1219 #&General::log ("new:<$1><$2>");
1220 return 1;
1221 }
1222 return 0;
1223}
1224
1225#
1226# Check existence of definition for a new option
1227#
1228sub ExistNewOptionDefinition {
1229 my ($line) = @_;
1230
1231 if ( $line =~ /^([-\w]+)( code \d+=($OptionTypes))/ ) {
1232 return defined $NewOptions{$1};
1233 }
1234 return 0;
1235}
1236
1237#
1238# Check if it is a new option (definition must exist)
1239# "code=" test eliminate a false response when definition exists
1240# but this string is a definition with bad $OptionTypes.
1241sub ValidNewOption {
1242 my ($line) = @_;
1243 if ($line =~ /^([-\w]+) (.*)/ ) {
1244 return defined ( $NewOptions{$1} ) && $2 !~ /code=/;
1245 }
1246 return 0;
1247}
1248
1249#
1250# Check if the new option $opt is used, except the definition of itself!
1251#
1252sub IsUsedNewOptionDefinition {
1253 my ($opt,$val) = @_;
1254
1255 foreach my $line (@current1) {
1256 #chomp($line); # remove newline #don't know why, but this remove newline in @current1 .... !
1257 my @temp = split(/\t/,$line);
1258 # if we find something "opt value" & value != "code nnn=" it's ok.
1259 return 1 if ( ($opt eq $temp[1]) && ($temp[2] !~ /code \d+=/) );
1260 }
1261 return 0;
1262}