]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/aliases.cgi
firewall: Apply destination NAT rules for the firewall itself, too.
[people/teissler/ipfire-2.x.git] / html / cgi-bin / aliases.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 # this cgi is base on IPCop CGI - aliases.cgi
23 #
24
25 # to fully troubleshot your code, uncomment diagnostics, Carp and cluck lines
26 #use diagnostics; # need to add the file /usr/lib/perl5/5.8.x/pods/perldiag.pod before to work
27 # next look at /var/log/httpd/error_log , http://www.perl.com/pub/a/2002/05/07/mod_perl.html may help
28 #use warnings;
29 use strict;
30 #use Carp ();
31 #local $SIG{__WARN__} = \&Carp::cluck;
32
33 require '/var/ipfire/general-functions.pl'; # replace /var/ipcop with /var/ipcop in case of manual install
34 require "${General::swroot}/lang.pl";
35 require "${General::swroot}/header.pl";
36
37 my $configfwdfw = "${General::swroot}/firewall/config";
38 my $configinput = "${General::swroot}/firewall/input";
39 my $configoutgoing = "${General::swroot}/firewall/outgoing";
40 my %input=();
41 my %forward=();
42 my %output=();
43
44 #workaround to suppress a warning when a variable is used only once
45 my @dummy = ( ${Header::colouryellow} );
46 @dummy = ( ${Header::table1colour} );
47 @dummy = ( ${Header::table2colour} );
48 undef (@dummy);
49
50 # Files used
51 my $setting = "${General::swroot}/ethernet/settings";
52 our $datafile = "${General::swroot}/ethernet/aliases";
53
54
55 our %settings=();
56 #Settings1
57
58 #Settings2 for editing the multi-line list
59 #Must not be saved !
60 $settings{'IP'} = '';
61 $settings{'ENABLED'} = 'off'; # Every check box must be set to off
62 $settings{'NAME'} = '';
63 my @nosaved=('IP','ENABLED','NAME'); # List here ALL setting2 fields. Mandatory
64
65 $settings{'ACTION'} = ''; # add/edit/remove
66 $settings{'KEY1'} = ''; # point record for ACTION
67
68 #Define each field that can be used to sort columns
69 my $sortstring='^IP|^NAME';
70 my $errormessage = '';
71 my $warnmessage = '';
72
73 &Header::showhttpheaders();
74
75 # Read needed Ipcop netsettings
76 my %netsettings=();
77 $netsettings{'SORT_ALIASES'} = 'NAME'; # default sort
78 &General::readhash($setting, \%netsettings);
79
80 #Get GUI values
81 &Header::getcgihash(\%settings);
82
83 # Load multiline data
84 our @current = ();
85 if (open(FILE, "$datafile")) {
86 @current = <FILE>;
87 close (FILE);
88 }
89
90 #
91 # Check Settings1 first because they are needed before working on @current
92 #
93 # Remove if no Setting1 needed
94 #
95 if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
96
97 #
98 #Validate static Settings1 here
99 #
100 unless ($errormessage) { # Everything is ok, save settings
101 #map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
102 #&General::writehash($setting, \%settings); # Save good settings
103 #$settings{'ACTION'} = $Lang::tr{'save'}; # Recreate 'ACTION'
104 #map ($settings{$_}= '',(@nosaved,'KEY1')); # and reinit var to empty
105
106 # Rebuild configuration file if needed
107 &BuildConfiguration;
108 }
109
110 ERROR: # Leave the faulty field untouched
111 } else {
112 #&General::readhash($setting, \%settings); # Get saved settings and reset to good if needed
113 }
114
115 ## Now manipulate the multi-line list with Settings2
116 # Basic actions are:
117 # toggle the check box
118 # add/update a new line
119 # begin editing a line
120 # remove a line
121
122
123 # Toggle enable/disable field. Field is in second position
124 if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
125 #move out new line
126 chomp(@current[$settings{'KEY1'}]);
127 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
128 $temp[1] = $temp[1] eq 'on' ? 'off' : 'on'; # Toggle the field
129 $temp[2] = '' if ( $temp[2] eq '' );
130 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
131 $settings{'KEY1'} = ''; # End edit mode
132
133 &General::log($Lang::tr{'ip alias changed'});
134
135 #Save current
136 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
137 print FILE @current;
138 close(FILE);
139
140 # Rebuild configuration file
141 &BuildConfiguration;
142 }
143
144 if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
145 # Validate inputs
146 if (! &General::validip($settings{'IP'})) {$errormessage = "invalid ip"};
147 $settings{'NAME'} = &Header::cleanhtml($settings{'NAME'});
148
149 # Make sure we haven't duplicated an alias or RED
150 my $spacer='';
151 if ($settings{'IP'} eq $netsettings{'RED_ADDRESS'}) {
152 $errormessage = $Lang::tr{'duplicate ip'} . ' (RED)';
153 $spacer=" & ";
154 }
155 #Check if we have an emtpy name
156 if (!$settings{'NAME'}){
157 $errormessage=$Lang::tr{'fwhost err name1'};
158 }elsif(! &General::validfqdn($settings{'NAME'}) && ! &General::validhostname($settings{'NAME'})){
159 $errormessage=$Lang::tr{'invalid hostname'};
160 }
161 my $idx=0;
162 foreach my $line (@current) {
163 chomp ($line);
164 my @temp = split (/\,/, $line);
165 if ( ($settings{'KEY1'} eq '')||(($settings{'KEY1'} ne '') && ($settings{'KEY1'} != $idx))) { # update
166 if ($temp[0] eq $settings{'IP'}) {
167 $errormessage .= $spacer.$Lang::tr{'duplicate ip'};
168 $spacer=" & ";
169 }
170 if ($temp[2] eq $settings{'NAME'} && $temp[2] ne '') {
171 $errormessage .= $spacer.$Lang::tr{'duplicate name'};
172 $spacer=" & ";
173 }
174 }
175 $idx++;
176 }
177 #Update firewallrules if aliasname is changed
178 if ($settings{'OLDNAME'} ne $settings {'NAME'}){
179 &General::readhasharray("$configfwdfw", \%forward);
180 &General::readhasharray("$configinput", \%input);
181 &General::readhasharray("$configoutgoing", \%output);
182 #Check FORWARD
183 foreach my $forwardkey (sort keys %forward){
184 if ($forward{$forwardkey}[29] eq $settings{'OLDNAME'}){
185 $forward{$forwardkey}[29] = $settings {'NAME'};
186 }
187 }
188 &General::writehasharray($configfwdfw, \%forward);
189 #Check INPUT
190 foreach my $inputkey (sort keys %input){
191 if ($input{$inputkey}[6] eq $settings{'OLDNAME'}){
192 $input{$inputkey}[6] = $settings {'NAME'};
193 }
194 }
195 &General::writehasharray($configinput, \%input);
196 #Check OUTPUT
197 foreach my $outputkey (sort keys %output){
198 if ($output{$outputkey}[4] eq $settings{'OLDNAME'}){
199 $output{$outputkey}[4] = $settings {'NAME'};
200 }
201 }
202 &General::writehasharray($configoutgoing, \%output);
203 &General::firewall_config_changed;
204 }
205 #If Alias IP has changed, set firewall_config_changed
206 if($settings{'OLDIP'} ne $settings{'IP'} && $settings{'OLDIP'}){
207 &General::firewall_config_changed;
208 }
209 unless ($errormessage) {
210 if ($settings{'KEY1'} eq '') { #add or edit ?
211 unshift (@current, "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n");
212 &General::log($Lang::tr{'ip alias added'});
213 } else {
214 @current[$settings{'KEY1'}] = "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n";
215 $settings{'KEY1'} = ''; # End edit mode
216 &General::log($Lang::tr{'ip alias changed'});
217 }
218
219 # Write changes to config file.
220 &SortDataFile; # sort newly added/modified entry
221
222 &BuildConfiguration; # then re-build conf which use new data
223
224 ##
225 ## if entering data line is repetitive, choose here to not erase fields between each addition
226 ##
227 map ($settings{$_}='' ,@nosaved); # Clear fields
228 }
229 }
230
231 if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
232 #move out new line
233 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
234 chomp($line);
235 my @temp = split(/\,/, $line);
236
237 ##
238 ## move data fields to Setting2 for edition
239 ##
240 $settings{'IP'}=$temp[0]; # Prepare the screen for editing
241 $settings{'ENABLED'}=$temp[1];
242 $settings{'NAME'}=$temp[2];
243 }
244
245 if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
246 splice (@current,$settings{'KEY1'},1); # Delete line
247 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
248 print FILE @current;
249 close(FILE);
250 $settings{'KEY1'} = ''; # End remove mode
251 &General::log($Lang::tr{'ip alias removed'});
252
253 &BuildConfiguration; # then re-build conf which use new data
254 }
255
256
257
258 ## Check if sorting is asked
259 # If same column clicked, reverse the sort.
260 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
261 my $newsort=$ENV{'QUERY_STRING'};
262 my $actual=$netsettings{'SORT_ALIASES'};
263 #Reverse actual sort ?
264 if ($actual =~ $newsort) {
265 my $Rev='';
266 if ($actual !~ 'Rev') {
267 $Rev='Rev';
268 }
269 $newsort.=$Rev;
270 }
271 $netsettings{'SORT_ALIASES'}=$newsort;
272 &General::writehash($setting, \%netsettings);
273 &SortDataFile;
274 $settings{'ACTION'} = 'SORT'; # Recreate 'ACTION'
275 }
276
277 # Default initial value
278 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
279 $settings{'ENABLED'} ='on';
280 }
281
282 &Header::openpage($Lang::tr{'external aliases configuration'}, 1, '');
283 &Header::openbigbox('100%', 'left', '', $errormessage);
284 my %checked =(); # Checkbox manipulations
285
286 if ($errormessage) {
287 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
288 print "$errormessage&nbsp;";
289 &Header::closebox();
290 }
291 unless (( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ ) && ($netsettings{'RED_TYPE'} eq 'STATIC'))
292 {
293 &Header::openbox('100%', 'left', $Lang::tr{'capswarning'});
294 print <<END
295 <table style='width:100%;'>
296 <tr>
297 <td class='boldbase' style='color:${Header::colourred};'><b>$Lang::tr{'aliases not active'}</b></td>
298 </tr>
299 </table>
300 END
301 ;
302 &Header::closebox();
303 }
304
305 #
306 # Second check box is for editing the list
307 #
308 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq 'on') ? "checked='checked'" : '' ;
309
310 my $buttontext = $Lang::tr{'add'};
311 if ($settings{'KEY1'} ne '') {
312 $buttontext = $Lang::tr{'update'};
313 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing alias'});
314 } else {
315 &Header::openbox('100%', 'left', $Lang::tr{'add new alias'});
316 }
317
318 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
319 print <<END
320 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
321 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
322 <input type='hidden' name='OLDNAME' value='$settings{'NAME'}' />
323 <input type='hidden' name='OLDIP' value='$settings{'IP'}' />
324 <table style='width:100%;'>
325 <tr>
326 <td class='base' style='color:${Header::colourred};'>$Lang::tr{'name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
327 <td><input type='text' name='NAME' value='$settings{'NAME'}' size='32' /></td>
328 <td class='base' style='text-align:right; color:${Header::colourred};'>$Lang::tr{'alias ip'}:&nbsp;</td>
329 <td><input type='text' name='IP' value='$settings{'IP'}' size='16' /></td>
330 <td class='base' style='text-align:right;'>$Lang::tr{'enabled'}&nbsp;</td>
331 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
332 </tr>
333 </table>
334 <br>
335 <hr />
336 <table style='width:100%;'>
337 <tr>
338 <td><img src='/blob.gif' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
339 <td style='text-align:right;'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
340 </tr>
341 </table>
342 </form>
343 END
344 ;
345 &Header::closebox();
346
347 # Add visual indicators to column headings to show sort order - EO
348 my $sortarrow1 = '';
349 my $sortarrow2 = '';
350
351 if ($netsettings{'SORT_ALIASES'} eq 'NAMERev') {
352 $sortarrow1 = $Header::sortdn;
353 } elsif ($netsettings{'SORT_ALIASES'} eq 'NAME') {
354 $sortarrow1 = $Header::sortup;
355 } elsif ($netsettings{'SORT_ALIASES'} eq 'IPRev') {
356 $sortarrow2 = $Header::sortdn;
357 } else {
358 $sortarrow2 = $Header::sortup;
359 }
360
361 #
362 # Third box shows the list, in columns
363 #
364 # Columns headers may content a link. In this case it must be named in $sortstring
365 #
366 &Header::openbox('100%', 'left', $Lang::tr{'current aliases'});
367 print <<END
368 <table class='tbl' style='width:100%;'>
369 <tr>
370 <th style='width:55%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</th>
371 <th style='width:45%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</th>
372 <th colspan='3' class='boldbase' style='width:5%; text-align:center;'><b>$Lang::tr{'action'}</b></th>
373 </tr>
374 END
375 ;
376
377 #
378 # Print each line of @current list
379 #
380 # each data line is splitted into @temp.
381 #
382
383 my $key = 0;
384 my $col="";
385 foreach my $line (@current) {
386 chomp($line);
387 my @temp = split(/\,/,$line);
388
389 #Choose icon for checkbox
390 my $gif = '';
391 my $gdesc = '';
392 if ($temp[1] eq "on") {
393 $gif = 'on.gif';
394 $gdesc = $Lang::tr{'click to disable'};
395 } else {
396 $gif = 'off.gif';
397 $gdesc = $Lang::tr{'click to enable'};
398 }
399
400 #Colorize each line
401 if ($settings{'KEY1'} eq $key) {
402 $col="background-color:${Header::colouryellow};";
403 } elsif ($key % 2) {
404 $col="background-color:${Header::table2colour};";
405 } else {
406 $col="background-color:${Header::table1colour};";
407 }
408 print "<tr style='$col'>";
409
410 print <<END
411 <td style='text-align:center; $col'>$temp[2]</td>
412 <td style='text-align:center; $col'>$temp[0]</td>
413
414 <td style='text-align:center; $col'>
415 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
416 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
417 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
418 <input type='hidden' name='KEY1' value='$key' />
419 </form>
420 </td>
421
422 <td style='text-align:center; $col'>
423 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
424 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
425 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
426 <input type='hidden' name='KEY1' value='$key' />
427 </form>
428 </td>
429
430 <td style='text-align:center; $col'>
431 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
432 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
433 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
434 <input type='hidden' name='KEY1' value='$key' />
435 </form>
436 </td>
437 </tr>
438 END
439 ;
440 $key++;
441 }
442 print "</table>";
443
444 # If table contains entries, print 'Key to action icons'
445 if ($key) {
446 print <<END
447 <table>
448 <tr>
449 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
450 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
451 <td class='base'>$Lang::tr{'click to disable'}</td>
452 <td>&nbsp;&nbsp;</td>
453 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
454 <td class='base'>$Lang::tr{'click to enable'}</td>
455 <td>&nbsp;&nbsp;</td>
456 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
457 <td class='base'>$Lang::tr{'edit'}</td>
458 <td>&nbsp;&nbsp;</td>
459 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
460 <td class='base'>$Lang::tr{'remove'}</td>
461 </tr>
462 </table>
463 END
464 ;
465 }
466
467 &Header::closebox();
468 &Header::closebigbox();
469 &Header::closepage();
470
471 ## Ouf it's the end !
472
473
474
475 # Sort the "current" array according to choices
476 sub SortDataFile
477 {
478 our %entries = ();
479
480 # Sort pair of record received in $a $b special vars.
481 # When IP is specified use numeric sort else alpha.
482 # If sortname ends with 'Rev', do reverse sort.
483 #
484 sub fixedleasesort {
485 my $qs=''; # The sort field specified minus 'Rev'
486 if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
487 $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
488 if ($qs eq 'IP') {
489 my @a = split(/\./,$entries{$a}->{$qs});
490 my @b = split(/\./,$entries{$b}->{$qs});
491 ($b[0]<=>$a[0]) ||
492 ($b[1]<=>$a[1]) ||
493 ($b[2]<=>$a[2]) ||
494 ($b[3]<=>$a[3]);
495 } else {
496 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
497 }
498 } else { #not reverse
499 $qs=$netsettings{'SORT_ALIASES'};
500 if ($qs eq 'IP') {
501 my @a = split(/\./,$entries{$a}->{$qs});
502 my @b = split(/\./,$entries{$b}->{$qs});
503 ($a[0]<=>$b[0]) ||
504 ($a[1]<=>$b[1]) ||
505 ($a[2]<=>$b[2]) ||
506 ($a[3]<=>$b[3]);
507 } else {
508 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
509 }
510 }
511 }
512
513 #Use an associative array (%entries)
514 my $key = 0;
515 foreach my $line (@current) {
516 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
517 my @temp = split (',',$line);
518
519 # Build a pair 'Field Name',value for each of the data dataline.
520 # Each SORTABLE field must have is pair.
521 # Other data fields (non sortable) can be grouped in one
522
523 # Exemple
524 # F1,F2,F3,F4,F5 only F1 F2 for sorting
525 # my @record = ('KEY',$key++,
526 # 'F1',$temp[0],
527 # 'F2',$temp[1],
528 # 'DATA',join(',',@temp[2..4]) ); #group remainning values, with separator (,)
529
530 # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
531
532
533 my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);
534 my $record = {}; # create a reference to empty hash
535 %{$record} = @record; # populate that hash with @record
536 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
537 }
538
539 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
540
541 # Each field value is printed , with the newline ! Don't forget separator and order of them.
542 foreach my $entry (sort fixedleasesort keys %entries) {
543 print FILE "$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";
544 }
545
546 close(FILE);
547 # Reload sorted @current
548 open (FILE, "$datafile");
549 @current = <FILE>;
550 close (FILE);
551 }
552
553 #
554 # Build the configuration file for application aliases
555 #
556 sub BuildConfiguration {
557 # Restart service associated with this
558 system '/usr/local/bin/setaliases';
559 }
560