]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/hosts.cgi
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";
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 my @nosaved=('EN','IP','HOST','DOM'); # List here ALL setting2 fields. Mandatory
57 $settings{'ACTION'} = ''; # add/edit/remove
58 $settings{'KEY1'} = ''; # point record for ACTION
60 #Define each field that can be used to sort columns
61 my $sortstring='^IP|^HOST|^DOM';
62 $settings{'SORT_HOSTSLIST'} = 'HOST';
63 my $errormessage = '';
66 &Header
::showhttpheaders
();
69 &Header
::getcgihash
(\
%settings);
73 if (open(FILE
, "$datafile")) {
78 ## Settings1 Box not used...
79 &General
::readhash
("${General::swroot}/main/settings", \
%settings);
82 ## Now manipulate the multi-line list with Settings2
84 # toggle the check box
85 # add/update a new line
86 # begin editing a line
90 # Toggle enable/disable field. Field is in second position
91 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
93 chomp(@current[$settings{'KEY1'}]);
94 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
96 $temp[0] = $temp[0] ne '' ?
'' : 'on'; # Toggle the field
97 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
98 $settings{'KEY1'} = ''; # End edit mode
100 &General
::log($Lang::tr
{'hosts config changed'});
103 open(FILE
, ">$datafile") or die 'hosts datafile error';
107 # Rebuild configuration file
111 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
113 unless(&General
::validip
($settings{'IP'})) {
114 $errormessage = $Lang::tr
{'invalid fixed ip address'};
117 unless(&General
::validhostname
($settings{'HOST'})) {
118 $errormessage = $Lang::tr
{'invalid hostname'};
121 if ($settings{'DOM'} && ! &General
::validdomainname
($settings{'DOM'})) {
122 $errormessage = $Lang::tr
{'invalid domain name'};
126 unless ($errormessage) {
127 if ($settings{'KEY1'} eq '') { #add or edit ?
128 unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n");
129 &General
::log($Lang::tr
{'hosts config added'});
131 @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n";
132 $settings{'KEY1'} = ''; # End edit mode
133 &General
::log($Lang::tr
{'hosts config changed'});
136 # Write changes to config file.
137 &SortDataFile
; # sort newly added/modified entry
138 &BuildConfiguration
; # then re-build new host
140 #map ($settings{$_}='' ,@nosaved); # Clear fields
144 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
146 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
148 my @temp = split(/\,/, $line);
149 $settings{'EN'}=$temp[0]; # Prepare the screen for editing
150 $settings{'IP'}=$temp[1];
151 $settings{'HOST'}=$temp[2];
152 $settings{'DOM'}=$temp[3];
155 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
156 splice (@current,$settings{'KEY1'},1); # Delete line
157 open(FILE
, ">$datafile") or die 'hosts datafile error';
160 $settings{'KEY1'} = ''; # End remove mode
161 &General
::log($Lang::tr
{'hosts config changed'});
163 &BuildConfiguration
; # then re-build conf which use new data
168 ## Check if sorting is asked
169 # If same column clicked, reverse the sort.
170 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
171 my $newsort=$ENV{'QUERY_STRING'};
172 my $actual=$settings{'SORT_HOSTSLIST'};
173 #Reverse actual sort ?
174 if ($actual =~ $newsort) {
176 if ($actual !~ 'Rev') {
181 $settings{'SORT_HOSTSLIST'}=$newsort;
182 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
183 &General
::writehash
($setting, \
%settings);
185 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
186 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
189 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
190 # Place here default value when nothing is initialized
191 $settings{'EN'} = 'on';
192 $settings{'DOM'} = $settings{'DOMAINNAME'};
195 &Header
::openpage
($Lang::tr
{'hostname'}, 1, '');
196 &Header
::openbigbox
('100%', 'left', '', $errormessage);
197 my %checked=(); # Checkbox manipulations
200 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
201 print "<font class='base'>$errormessage </font>";
206 # Remove if no Setting1 needed
209 # $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage";
211 #&Header::openbox('100%', 'left', $Lang::tr{'settings'});
212 #print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
214 #<table width='100%'>
216 # <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td>
223 #<table width='100%'>
226 # <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'this field may be blank'}</td>-->
227 # <td class='base' width='25%'>$warnmessage</td>
228 # <td width='50%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' disabled='disabled' /></td>
234 #&Header::closebox(); # end of Settings1
238 # Second check box is for editing the list
240 $checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ?
'' : "checked='checked'";
242 my $buttontext = $Lang::tr
{'add'};
243 if ($settings{'KEY1'} ne '') {
244 $buttontext = $Lang::tr
{'update'};
245 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
247 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
250 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
252 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
253 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
256 <td class='base'>$Lang::tr{'host ip'}: </td>
257 <td><input type='text' name='IP' value='$settings{'IP'}' /></td>
258 <td class='base'>$Lang::tr{'hostname'}:</td>
259 <td><input type='text' name='HOST' value='$settings{'HOST'}' /></td>
261 <td class='base'>$Lang::tr{'domain name'}: <img src='/blob.gif' alt='*' /></td>
262 <td><input type='text' name='DOM' value='$settings{'DOM'}' /></td>
263 <td class='base'>$Lang::tr{'enabled'}</td>
264 <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td>
271 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'this field may be blank'}</td>
272 <td width='50%' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
281 # Third box shows the list, in columns
283 # Columns headers may content a link. In this case it must be named in $sortstring
285 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
287 <table width='100%' class='tbl'>
289 <th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'}</b></a></th>
290 <th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOST'><b>$Lang::tr{'hostname'}</b></a></th>
291 <th width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?DOM'><b>$Lang::tr{'domain name'}</b></a></th>
292 <th width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
298 # Print each line of @current list
302 foreach my $line (@current) {
303 chomp($line); # remove newline
304 my @temp=split(/\,/,$line);
305 $temp[3] ='' unless defined $temp[3]; # not always populated
307 #Choose icon for checkbox
310 if ($temp[0] ne '' ) {
312 $gdesc = $Lang::tr
{'click to disable'};
315 $gdesc = $Lang::tr
{'click to enable'};
319 if ($settings{'KEY1'} eq $key) {
320 print "<tr bgcolor='${Header::colouryellow}'>";
323 $col="bgcolor='$color{'color20'}'";
326 $col="bgcolor='$color{'color22'}'";
329 <td align='center' $col>$temp[1]</td>
330 <td align='center' $col>$temp[2]</td>
331 <td align='center' $col>$temp[3]</td>
332 <td align='center' $col>
333 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
334 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
335 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
336 <input type='hidden' name='KEY1' value='$key' />
340 <td align='center' $col>
341 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
342 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
343 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
344 <input type='hidden' name='KEY1' value='$key' />
348 <td align='center' $col>
349 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
350 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
351 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
352 <input type='hidden' name='KEY1' value='$key' />
362 # If table contains entries, print 'Key to action icons'
367 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
368 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
369 <td class='base'>$Lang::tr{'click to disable'}</td>
370 <td> </td>
371 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
372 <td class='base'>$Lang::tr{'click to enable'}</td>
373 <td> </td>
374 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
375 <td class='base'>$Lang::tr{'edit'}</td>
376 <td> </td>
377 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
378 <td class='base'>$Lang::tr{'remove'}</td>
386 &Header
::closebigbox
();
387 &Header
::closepage
();
389 ## Ouf it's the end !
391 # Sort the "current" array according to choices
396 # Sort pair of record received in $a $b special vars.
397 # When IP is specified use numeric sort else alpha.
398 # If sortname ends with 'Rev', do reverse sort.
401 my $qs=''; # The sort field specified minus 'Rev'
402 if (rindex ($settings{'SORT_HOSTSLIST'},'Rev') != -1) {
403 $qs=substr ($settings{'SORT_HOSTSLIST'},0,length($settings{'SORT_HOSTSLIST'})-3);
405 my @a = split(/\./,$entries{$a}->{$qs});
406 my @b = split(/\./,$entries{$b}->{$qs});
412 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
414 } else { #not reverse
415 $qs=$settings{'SORT_HOSTSLIST'};
417 my @a = split(/\./,$entries{$a}->{$qs});
418 my @b = split(/\./,$entries{$b}->{$qs});
424 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
429 #Use an associative array (%entries)
431 foreach my $line (@current) {
432 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
433 my @temp = ( '','','', '');
434 @temp = split (',',$line);
436 # Build a pair 'Field Name',value for each of the data dataline.
437 # Each SORTABLE field must have is pair.
438 # Other data fields (non sortable) can be grouped in one
440 my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'HOST',$temp[2],'DOM',$temp[3]);
441 my $record = {}; # create a reference to empty hash
442 %{$record} = @record; # populate that hash with @record
443 $entries{$record->{KEY
}} = $record; # add this to a hash of hashes
446 open(FILE
, ">$datafile") or die 'hosts datafile error';
448 # Each field value is printed , with the newline ! Don't forget separator and order of them.
449 foreach my $entry (sort fixedleasesort
keys %entries) {
450 print FILE
"$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{HOST},$entries{$entry}->{DOM}\n";
454 # Reload sorted @current
455 open (FILE
, "$datafile");
461 # Build the configuration file
463 sub BuildConfiguration
{
464 system '/usr/local/bin/rebuildhosts';