]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/hosts.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2019 IPFire Team <info@ipfire.org> #
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";
32 #workaround to suppress a warning when a variable is used only once
33 my @dummy = ( ${Header
::colouryellow
} );
37 my $setting = "${General::swroot}/main/settings";
38 our $datafile = "${General::swroot}/main/hosts"; #(our: used in subroutine)
41 my %mainsettings = ();
42 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
43 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
49 #Settings2 for editing the multi-line list
51 $settings{'EN'} = ''; # reuse for dummy field in position zero
53 $settings{'HOST'} = '';
54 $settings{'DOM'} = '';
55 $settings{'PTR'} = '';
56 my @nosaved=('EN','IP','HOST','DOM','PTR'); # List here ALL setting2 fields. Mandatory
58 $settings{'ACTION'} = ''; # add/edit/remove
59 $settings{'KEY1'} = ''; # point record for ACTION
61 #Define each field that can be used to sort columns
62 my $sortstring='^IP|^HOST|^DOM';
63 $settings{'SORT_HOSTSLIST'} = 'HOST';
64 my $errormessage = '';
67 &Header
::showhttpheaders
();
70 &Header
::getcgihash
(\
%settings);
74 if (open(FILE
, "$datafile")) {
79 ## Settings1 Box not used...
80 &General
::readhash
("${General::swroot}/main/settings", \
%settings);
82 # Set PTR to off if filed was not received
83 if ($settings{'PTR'} eq '') {
84 $settings{'PTR'} = 'off';
87 ## Now manipulate the multi-line list with Settings2
89 # toggle the check box
90 # add/update a new line
91 # begin editing a line
95 # Toggle enable/disable field. Field is in second position
96 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
98 chomp(@current[$settings{'KEY1'}]);
99 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
101 $temp[0] = $temp[0] ne '' ?
'' : 'on'; # Toggle the field
102 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
103 $settings{'KEY1'} = ''; # End edit mode
105 &General
::log($Lang::tr
{'hosts config changed'});
108 open(FILE
, ">$datafile") or die 'hosts datafile error';
112 # Rebuild configuration file
116 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
118 unless(&General
::validip
($settings{'IP'})) {
119 $errormessage = $Lang::tr
{'invalid fixed ip address'};
122 unless(&General
::validhostname
($settings{'HOST'})) {
123 $errormessage = $Lang::tr
{'invalid hostname'};
126 if ($settings{'DOM'} && ! &General
::validdomainname
($settings{'DOM'})) {
127 $errormessage = $Lang::tr
{'invalid domain name'};
130 unless ($errormessage) {
131 if ($settings{'KEY1'} eq '') { #add or edit ?
132 unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'},$settings{'PTR'}\n");
133 &General
::log($Lang::tr
{'hosts config added'});
135 @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'},$settings{'PTR'}\n";
136 $settings{'KEY1'} = ''; # End edit mode
137 &General
::log($Lang::tr
{'hosts config changed'});
140 # Write changes to config file.
141 &SortDataFile
; # sort newly added/modified entry
142 &BuildConfiguration
; # then re-build new host
144 #map ($settings{$_}='' ,@nosaved); # Clear fields
148 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
150 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
152 my @temp = split(/\,/, $line);
153 $settings{'EN'}=$temp[0]; # Prepare the screen for editing
154 $settings{'IP'}=$temp[1];
155 $settings{'HOST'}=$temp[2];
156 $settings{'DOM'}=$temp[3];
157 if ($temp[4] eq '') {
158 $settings{'PTR'} = 'on';
160 $settings{'PTR'}=$temp[4];
164 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
165 splice (@current,$settings{'KEY1'},1); # Delete line
166 open(FILE
, ">$datafile") or die 'hosts datafile error';
169 $settings{'KEY1'} = ''; # End remove mode
170 &General
::log($Lang::tr
{'hosts config changed'});
172 &BuildConfiguration
; # then re-build conf which use new data
177 ## Check if sorting is asked
178 # If same column clicked, reverse the sort.
179 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
180 my $newsort=$ENV{'QUERY_STRING'};
181 my $actual=$settings{'SORT_HOSTSLIST'};
182 #Reverse actual sort ?
183 if ($actual =~ $newsort) {
185 if ($actual !~ 'Rev') {
190 $settings{'SORT_HOSTSLIST'}=$newsort;
191 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
192 &General
::writehash
($setting, \
%settings);
194 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
195 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
198 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
199 # Place here default value when nothing is initialized
200 $settings{'EN'} = 'on';
201 $settings{'DOM'} = $settings{'DOMAINNAME'};
202 $settings{'PTR'} = 'on';
205 &Header
::openpage
($Lang::tr
{'hostname'}, 1, '');
206 &Header
::openbigbox
('100%', 'left', '', $errormessage);
207 my %checked=(); # Checkbox manipulations
210 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
211 print "<font class='base'>$errormessage </font>";
216 # Remove if no Setting1 needed
219 # $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage";
221 #&Header::openbox('100%', 'left', $Lang::tr{'settings'});
222 #print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
224 #<table width='100%'>
226 # <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td>
233 #<table width='100%'>
236 # <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'required field'}</td>-->
237 # <td class='base' width='25%'>$warnmessage</td>
238 # <td width='50%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' disabled='disabled' /></td>
244 #&Header::closebox(); # end of Settings1
248 # Second check box is for editing the list
250 $checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ?
'' : "checked='checked'";
251 $checked{'PTR'}{'on'} = ($settings{'PTR'} eq 'off' ) ?
'' : "checked='checked'";
253 my $buttontext = $Lang::tr
{'add'};
254 if ($settings{'KEY1'} ne '') {
255 $buttontext = $Lang::tr
{'update'};
256 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
258 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
261 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
263 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
264 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
267 <td class='base'>$Lang::tr{'host ip'}: <img src='/blob.gif' alt='*' /></td>
268 <td><input type='text' name='IP' value='$settings{'IP'}' /></td>
269 <td class='base'>$Lang::tr{'hostname'}: <img src='/blob.gif' alt='*' /></td>
270 <td><input type='text' name='HOST' value='$settings{'HOST'}' /></td>
273 <td class='base'>$Lang::tr{'domain name'}:</td>
274 <td><input type='text' name='DOM' value='$settings{'DOM'}' /></td>
275 <td class='base'>$Lang::tr{'generate ptr'}:</td>
276 <td><input type='checkbox' name='PTR' $checked{'PTR'}{'on'} /></td>
281 <td class='base'>$Lang::tr{'enabled'}</td>
282 <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td>
289 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'required field'}</td>
290 <td width='50%' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
299 # Third box shows the list, in columns
301 # Columns headers may content a link. In this case it must be named in $sortstring
303 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
305 <table width='100%' class='tbl'>
307 <th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'}</b></a></th>
308 <th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOST'><b>$Lang::tr{'hostname'}</b></a></th>
309 <th width='40%' align='center'><a href='$ENV{'SCRIPT_NAME'}?DOM'><b>$Lang::tr{'domain name'}</b></a></th>
310 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'ptr'}</b></th>
311 <th width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
317 # Print each line of @current list
321 foreach my $line (@current) {
322 chomp($line); # remove newline
323 my @temp=split(/\,/,$line);
324 $temp[3] ='' unless defined $temp[3]; # not always populated
326 #Choose icon for checkbox
329 if ($temp[0] ne '' ) {
331 $gdesc = $Lang::tr
{'click to disable'};
334 $gdesc = $Lang::tr
{'click to enable'};
337 if ($temp[4] eq '' || $temp[4] eq 'on') {
338 $temp[4] = $Lang::tr
{'yes'};
340 $temp[4] = $Lang::tr
{'no'};
344 if ($settings{'KEY1'} eq $key) {
345 print "<tr bgcolor='${Header::colouryellow}'>";
348 $col="bgcolor='$color{'color20'}'";
351 $col="bgcolor='$color{'color22'}'";
354 <td align='center' $col>$temp[1]</td>
355 <td align='center' $col>$temp[2]</td>
356 <td align='center' $col>$temp[3]</td>
357 <td align='center' $col>$temp[4]</td>
358 <td align='center' $col>
359 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
360 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
361 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
362 <input type='hidden' name='KEY1' value='$key' />
366 <td align='center' $col>
367 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
368 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
369 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
370 <input type='hidden' name='KEY1' value='$key' />
374 <td align='center' $col>
375 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
376 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
377 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
378 <input type='hidden' name='KEY1' value='$key' />
388 # If table contains entries, print 'Key to action icons'
393 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
394 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
395 <td class='base'>$Lang::tr{'click to disable'}</td>
396 <td> </td>
397 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
398 <td class='base'>$Lang::tr{'click to enable'}</td>
399 <td> </td>
400 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
401 <td class='base'>$Lang::tr{'edit'}</td>
402 <td> </td>
403 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
404 <td class='base'>$Lang::tr{'remove'}</td>
412 &Header
::closebigbox
();
413 &Header
::closepage
();
415 ## Ouf it's the end !
417 # Sort the "current" array according to choices
422 # Sort pair of record received in $a $b special vars.
423 # When IP is specified use numeric sort else alpha.
424 # If sortname ends with 'Rev', do reverse sort.
427 my $qs=''; # The sort field specified minus 'Rev'
428 if (rindex ($settings{'SORT_HOSTSLIST'},'Rev') != -1) {
429 $qs=substr ($settings{'SORT_HOSTSLIST'},0,length($settings{'SORT_HOSTSLIST'})-3);
431 my @a = split(/\./,$entries{$a}->{$qs});
432 my @b = split(/\./,$entries{$b}->{$qs});
438 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
440 } else { #not reverse
441 $qs=$settings{'SORT_HOSTSLIST'};
443 my @a = split(/\./,$entries{$a}->{$qs});
444 my @b = split(/\./,$entries{$b}->{$qs});
450 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
455 #Use an associative array (%entries)
457 foreach my $line (@current) {
458 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
459 my @temp = ( '','','','','');
460 @temp = split (',',$line);
462 # Build a pair 'Field Name',value for each of the data dataline.
463 # Each SORTABLE field must have is pair.
464 # Other data fields (non sortable) can be grouped in one
466 my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'HOST',$temp[2],'DOM',$temp[3],'PTR',$temp[4]);
467 my $record = {}; # create a reference to empty hash
468 %{$record} = @record; # populate that hash with @record
469 $entries{$record->{KEY
}} = $record; # add this to a hash of hashes
472 open(FILE
, ">$datafile") or die 'hosts datafile error';
474 # Each field value is printed , with the newline ! Don't forget separator and order of them.
475 foreach my $entry (sort fixedleasesort
keys %entries) {
476 print FILE
"$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{HOST},$entries{$entry}->{DOM},$entries{$entry}->{PTR}\n";
480 # Reload sorted @current
481 open (FILE
, "$datafile");
487 # Build the configuration file
489 sub BuildConfiguration
{
490 system '/usr/local/bin/rebuildhosts';
491 system '/usr/local/bin/unboundctrl restart &>/dev/null';