]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/hosts.cgi
Merge remote-tracking branch 'origin/master' into next
[ipfire-2.x.git] / html / cgi-bin / hosts.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 #workaround to suppress a warning when a variable is used only once
33 my @dummy = ( ${Header::colouryellow} );
34 undef (@dummy);
35
36 # Files used
37 my $setting = "${General::swroot}/main/settings";
38 our $datafile = "${General::swroot}/main/hosts"; #(our: used in subroutine)
39
40 my %color = ();
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);
44
45 our %settings = ();
46 #Settings1
47 # removed
48
49 #Settings2 for editing the multi-line list
50 #Must not be saved !
51 $settings{'EN'} = ''; # reuse for dummy field in position zero
52 $settings{'IP'} = '';
53 $settings{'HOST'} = '';
54 $settings{'DOM'} = '';
55 my @nosaved=('EN','IP','HOST','DOM'); # List here ALL setting2 fields. Mandatory
56
57 $settings{'ACTION'} = ''; # add/edit/remove
58 $settings{'KEY1'} = ''; # point record for ACTION
59
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 = '';
64 my $warnmessage = '';
65
66 &Header::showhttpheaders();
67
68 #Get GUI values
69 &Header::getcgihash(\%settings);
70
71 # Load multiline data
72 our @current = ();
73 if (open(FILE, "$datafile")) {
74 @current = <FILE>;
75 close (FILE);
76 }
77
78 ## Settings1 Box not used...
79 &General::readhash("${General::swroot}/main/settings", \%settings);
80
81
82 ## Now manipulate the multi-line list with Settings2
83 # Basic actions are:
84 # toggle the check box
85 # add/update a new line
86 # begin editing a line
87 # remove a line
88
89
90 # Toggle enable/disable field. Field is in second position
91 if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
92 #move out new line
93 chomp(@current[$settings{'KEY1'}]);
94 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
95
96 $temp[0] = $temp[0] ne '' ? '' : 'on'; # Toggle the field
97 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
98 $settings{'KEY1'} = ''; # End edit mode
99
100 &General::log($Lang::tr{'hosts config changed'});
101
102 #Save current
103 open(FILE, ">$datafile") or die 'hosts datafile error';
104 print FILE @current;
105 close(FILE);
106
107 # Rebuild configuration file
108 &BuildConfiguration;
109 }
110
111 if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
112 # Validate inputs
113 unless(&General::validip($settings{'IP'})) {
114 $errormessage = $Lang::tr{'invalid fixed ip address'};
115 }
116
117 unless(&General::validhostname($settings{'HOST'})) {
118 $errormessage = $Lang::tr{'invalid hostname'};
119 }
120
121 if ($settings{'DOM'} && ! &General::validdomainname($settings{'DOM'})) {
122 $errormessage = $Lang::tr{'invalid domain name'};
123 }
124
125
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'});
130 } else {
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'});
134 }
135
136 # Write changes to config file.
137 &SortDataFile; # sort newly added/modified entry
138 &BuildConfiguration; # then re-build new host
139
140 #map ($settings{$_}='' ,@nosaved); # Clear fields
141 }
142 }
143
144 if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
145 #move out new line
146 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
147 chomp($line);
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];
153 }
154
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';
158 print FILE @current;
159 close(FILE);
160 $settings{'KEY1'} = ''; # End remove mode
161 &General::log($Lang::tr{'hosts config changed'});
162
163 &BuildConfiguration; # then re-build conf which use new data
164 }
165
166
167
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) {
175 my $Rev='';
176 if ($actual !~ 'Rev') {
177 $Rev='Rev';
178 }
179 $newsort.=$Rev;
180 }
181 $settings{'SORT_HOSTSLIST'}=$newsort;
182 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
183 &General::writehash($setting, \%settings);
184 &SortDataFile;
185 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
186 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
187 }
188
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'};
193 }
194
195 &Header::openpage($Lang::tr{'hostname'}, 1, '');
196 &Header::openbigbox('100%', 'left', '', $errormessage);
197 my %checked=(); # Checkbox manipulations
198
199 if ($errormessage) {
200 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
201 print "<font class='base'>$errormessage&nbsp;</font>";
202 &Header::closebox();
203 }
204
205 #
206 # Remove if no Setting1 needed
207 #
208 #if ($warnmessage) {
209 # $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage";
210 #}
211 #&Header::openbox('100%', 'left', $Lang::tr{'settings'});
212 #print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
213 #print <<END
214 #<table width='100%'>
215 #<tr>
216 # <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td>
217 #</table>
218 #
219 #END
220 #;
221 #
222 #print <<END
223 #<table width='100%'>
224 #<hr />
225 #<tr>
226 # <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' />&nbsp;$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>
229 #</tr>
230 #</table>
231 #</form>
232 #END
233 #;
234 #&Header::closebox(); # end of Settings1
235
236
237 #
238 # Second check box is for editing the list
239 #
240 $checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ? '' : "checked='checked'";
241
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'});
246 } else {
247 &Header::openbox('100%', 'left', $Lang::tr{'add a host'});
248 }
249
250 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
251 print <<END
252 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
253 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
254 <table width='100%'>
255 <tr>
256 <td class='base'>$Lang::tr{'host ip'}:&nbsp;</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>
260 </tr><tr>
261 <td class='base'>$Lang::tr{'domain name'}:&nbsp;<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>
265 </tr>
266 </table>
267 <br>
268 <hr />
269 <table width='100%'>
270 <tr>
271 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$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>
273 </tr>
274 </table>
275 </form>
276 END
277 ;
278 &Header::closebox();
279
280 #
281 # Third box shows the list, in columns
282 #
283 # Columns headers may content a link. In this case it must be named in $sortstring
284 #
285 &Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
286 print <<END
287 <table width='100%' class='tbl'>
288 <tr>
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>
293 </tr>
294 END
295 ;
296
297 #
298 # Print each line of @current list
299 #
300 my $col="";
301 my $key = 0;
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
306
307 #Choose icon for checkbox
308 my $gif = '';
309 my $gdesc = '';
310 if ($temp[0] ne '' ) {
311 $gif = 'on.gif';
312 $gdesc = $Lang::tr{'click to disable'};
313 } else {
314 $gif = 'off.gif';
315 $gdesc = $Lang::tr{'click to enable'};
316 }
317
318 #Colorize each line
319 if ($settings{'KEY1'} eq $key) {
320 print "<tr bgcolor='${Header::colouryellow}'>";
321 } elsif ($key % 2) {
322 print "<tr>";
323 $col="bgcolor='$color{'color20'}'";
324 } else {
325 print "<tr>";
326 $col="bgcolor='$color{'color22'}'";
327 }
328 print <<END
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' />
337 </form>
338 </td>
339
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' />
345 </form>
346 </td>
347
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' />
353 </form>
354 </td>
355 </tr>
356 END
357 ;
358 $key++;
359 }
360 print "</table>";
361
362 # If table contains entries, print 'Key to action icons'
363 if ($key) {
364 print <<END
365 <table>
366 <tr>
367 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</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>&nbsp;&nbsp;</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>&nbsp;&nbsp;</td>
374 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
375 <td class='base'>$Lang::tr{'edit'}</td>
376 <td>&nbsp;&nbsp;</td>
377 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
378 <td class='base'>$Lang::tr{'remove'}</td>
379 </tr>
380 </table>
381 END
382 ;
383 }
384
385 &Header::closebox();
386 &Header::closebigbox();
387 &Header::closepage();
388
389 ## Ouf it's the end !
390
391 # Sort the "current" array according to choices
392 sub SortDataFile
393 {
394 our %entries = ();
395
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.
399 #
400 sub fixedleasesort {
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);
404 if ($qs eq 'IP') {
405 my @a = split(/\./,$entries{$a}->{$qs});
406 my @b = split(/\./,$entries{$b}->{$qs});
407 ($b[0]<=>$a[0]) ||
408 ($b[1]<=>$a[1]) ||
409 ($b[2]<=>$a[2]) ||
410 ($b[3]<=>$a[3]);
411 } else {
412 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
413 }
414 } else { #not reverse
415 $qs=$settings{'SORT_HOSTSLIST'};
416 if ($qs eq 'IP') {
417 my @a = split(/\./,$entries{$a}->{$qs});
418 my @b = split(/\./,$entries{$b}->{$qs});
419 ($a[0]<=>$b[0]) ||
420 ($a[1]<=>$b[1]) ||
421 ($a[2]<=>$b[2]) ||
422 ($a[3]<=>$b[3]);
423 } else {
424 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
425 }
426 }
427 }
428
429 #Use an associative array (%entries)
430 my $key = 0;
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);
435
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
439
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
444 }
445
446 open(FILE, ">$datafile") or die 'hosts datafile error';
447
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";
451 }
452
453 close(FILE);
454 # Reload sorted @current
455 open (FILE, "$datafile");
456 @current = <FILE>;
457 close (FILE);
458 }
459
460 #
461 # Build the configuration file
462 #
463 sub BuildConfiguration {
464 system '/usr/local/bin/rebuildhosts';
465 }