]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/aliases.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 ###############################################################################
22 # this cgi is base on IPCop CGI - aliases.cgi
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
31 #local $SIG{__WARN__} = \&Carp::cluck;
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";
37 my $configfwdfw = "${General::swroot}/firewall/config";
38 my $configinput = "${General::swroot}/firewall/input";
39 my $configoutgoing = "${General::swroot}/firewall/outgoing";
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
} );
51 my $setting = "${General::swroot}/ethernet/settings";
52 our $datafile = "${General::swroot}/ethernet/aliases";
58 #Settings2 for editing the multi-line list
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
65 $settings{'ACTION'} = ''; # add/edit/remove
66 $settings{'KEY1'} = ''; # point record for ACTION
68 #Define each field that can be used to sort columns
69 my $sortstring='^IP|^NAME';
70 my $errormessage = '';
73 &Header
::showhttpheaders
();
75 # Read needed Ipcop netsettings
77 $netsettings{'SORT_ALIASES'} = 'NAME'; # default sort
78 &General
::readhash
($setting, \
%netsettings);
81 &Header
::getcgihash
(\
%settings);
85 if (open(FILE
, "$datafile")) {
91 # Check Settings1 first because they are needed before working on @current
93 # Remove if no Setting1 needed
95 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
98 #Validate static Settings1 here
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
106 # Rebuild configuration file if needed
110 ERROR
: # Leave the faulty field untouched
112 #&General::readhash($setting, \%settings); # Get saved settings and reset to good if needed
115 ## Now manipulate the multi-line list with Settings2
117 # toggle the check box
118 # add/update a new line
119 # begin editing a line
123 # Toggle enable/disable field. Field is in second position
124 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
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
133 &General
::log($Lang::tr
{'ip alias changed'});
136 open(FILE
, ">$datafile") or die 'Unable to open aliases file.';
140 # Rebuild configuration file
144 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
146 if (! &General
::validip
($settings{'IP'})) {$errormessage = "invalid ip"};
147 $settings{'NAME'} = &Header
::cleanhtml
($settings{'NAME'});
149 # Make sure we haven't duplicated an alias or RED
151 if ($settings{'IP'} eq $netsettings{'RED_ADDRESS'}) {
152 $errormessage = $Lang::tr
{'duplicate ip'} . ' (RED)';
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'};
162 foreach my $line (@current) {
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'};
170 if ($temp[2] eq $settings{'NAME'} && $temp[2] ne '') {
171 $errormessage .= $spacer.$Lang::tr
{'duplicate name'};
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);
183 foreach my $forwardkey (sort keys %forward){
184 if ($forward{$forwardkey}[29] eq $settings{'OLDNAME'}){
185 $forward{$forwardkey}[29] = $settings {'NAME'};
188 &General
::writehasharray
($configfwdfw, \
%forward);
190 foreach my $inputkey (sort keys %input){
191 if ($input{$inputkey}[6] eq $settings{'OLDNAME'}){
192 $input{$inputkey}[6] = $settings {'NAME'};
195 &General
::writehasharray
($configinput, \
%input);
197 foreach my $outputkey (sort keys %output){
198 if ($output{$outputkey}[4] eq $settings{'OLDNAME'}){
199 $output{$outputkey}[4] = $settings {'NAME'};
202 &General
::writehasharray
($configoutgoing, \
%output);
203 &General
::firewall_config_changed
;
205 #If Alias IP has changed, set firewall_config_changed
206 if($settings{'OLDIP'} ne $settings{'IP'} && $settings{'OLDIP'}){
207 &General
::firewall_config_changed
;
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'});
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'});
219 # Write changes to config file.
220 &SortDataFile
; # sort newly added/modified entry
222 &BuildConfiguration
; # then re-build conf which use new data
225 ## if entering data line is repetitive, choose here to not erase fields between each addition
227 map ($settings{$_}='' ,@nosaved); # Clear fields
231 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
233 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
235 my @temp = split(/\,/, $line);
238 ## move data fields to Setting2 for edition
240 $settings{'IP'}=$temp[0]; # Prepare the screen for editing
241 $settings{'ENABLED'}=$temp[1];
242 $settings{'NAME'}=$temp[2];
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.';
250 $settings{'KEY1'} = ''; # End remove mode
251 &General
::log($Lang::tr
{'ip alias removed'});
253 &BuildConfiguration
; # then re-build conf which use new data
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) {
266 if ($actual !~ 'Rev') {
271 $netsettings{'SORT_ALIASES'}=$newsort;
272 &General
::writehash
($setting, \
%netsettings);
274 $settings{'ACTION'} = 'SORT'; # Recreate 'ACTION'
277 # Default initial value
278 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
279 $settings{'ENABLED'} ='on';
282 &Header
::openpage
($Lang::tr
{'external aliases configuration'}, 1, '');
283 &Header
::openbigbox
('100%', 'left', '', $errormessage);
284 my %checked =(); # Checkbox manipulations
287 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
288 print "$errormessage ";
291 unless (( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ ) && ($netsettings{'RED_TYPE'} eq 'STATIC'))
293 &Header
::openbox
('100%', 'left', $Lang::tr
{'capswarning'});
295 <table style='width:100%;'>
297 <td class='boldbase' style='color:${Header::colourred};'><b>$Lang::tr{'aliases not active'}</b></td>
306 # Second check box is for editing the list
308 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq 'on') ?
"checked='checked'" : '' ;
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'});
315 &Header
::openbox
('100%', 'left', $Lang::tr
{'add new alias'});
318 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
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%;'>
326 <td class='base' style='color:${Header::colourred};'>$Lang::tr{'name'}:</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'}: </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'} </td>
331 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
336 <table style='width:100%;'>
338 <td style='text-align:right;'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
346 # Add visual indicators to column headings to show sort order - EO
350 if ($netsettings{'SORT_ALIASES'} eq 'NAMERev') {
351 $sortarrow1 = $Header::sortdn
;
352 } elsif ($netsettings{'SORT_ALIASES'} eq 'NAME') {
353 $sortarrow1 = $Header::sortup
;
354 } elsif ($netsettings{'SORT_ALIASES'} eq 'IPRev') {
355 $sortarrow2 = $Header::sortdn
;
357 $sortarrow2 = $Header::sortup
;
361 # Third box shows the list, in columns
363 # Columns headers may content a link. In this case it must be named in $sortstring
365 &Header
::openbox
('100%', 'left', $Lang::tr
{'current aliases'});
367 <table class='tbl' style='width:100%;'>
369 <th style='width:55%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</th>
370 <th style='width:45%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</th>
371 <th colspan='3' class='boldbase' style='width:5%; text-align:center;'><b>$Lang::tr{'action'}</b></th>
377 # Print each line of @current list
379 # each data line is splitted into @temp.
384 foreach my $line (@current) {
386 my @temp = split(/\,/,$line);
388 #Choose icon for checkbox
391 if ($temp[1] eq "on") {
393 $gdesc = $Lang::tr
{'click to disable'};
396 $gdesc = $Lang::tr
{'click to enable'};
400 if ($settings{'KEY1'} eq $key) {
401 $col="background-color:${Header::colouryellow};";
403 $col="background-color:${Header::table2colour};";
405 $col="background-color:${Header::table1colour};";
407 print "<tr style='$col'>";
410 <td style='text-align:center; $col'>$temp[2]</td>
411 <td style='text-align:center; $col'>$temp[0]</td>
413 <td style='text-align:center; $col'>
414 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
415 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
416 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
417 <input type='hidden' name='KEY1' value='$key' />
421 <td style='text-align:center; $col'>
422 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
423 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
424 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
425 <input type='hidden' name='KEY1' value='$key' />
429 <td style='text-align:center; $col'>
430 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
431 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
432 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
433 <input type='hidden' name='KEY1' value='$key' />
443 # If table contains entries, print 'Key to action icons'
448 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
449 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
450 <td class='base'>$Lang::tr{'click to disable'}</td>
451 <td> </td>
452 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
453 <td class='base'>$Lang::tr{'click to enable'}</td>
454 <td> </td>
455 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
456 <td class='base'>$Lang::tr{'edit'}</td>
457 <td> </td>
458 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
459 <td class='base'>$Lang::tr{'remove'}</td>
467 &Header
::closebigbox
();
468 &Header
::closepage
();
470 ## Ouf it's the end !
474 # Sort the "current" array according to choices
479 # Sort pair of record received in $a $b special vars.
480 # When IP is specified use numeric sort else alpha.
481 # If sortname ends with 'Rev', do reverse sort.
484 my $qs=''; # The sort field specified minus 'Rev'
485 if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
486 $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
488 my @a = split(/\./,$entries{$a}->{$qs});
489 my @b = split(/\./,$entries{$b}->{$qs});
495 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
497 } else { #not reverse
498 $qs=$netsettings{'SORT_ALIASES'};
500 my @a = split(/\./,$entries{$a}->{$qs});
501 my @b = split(/\./,$entries{$b}->{$qs});
507 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
512 #Use an associative array (%entries)
514 foreach my $line (@current) {
515 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
516 my @temp = split (',',$line);
518 # Build a pair 'Field Name',value for each of the data dataline.
519 # Each SORTABLE field must have is pair.
520 # Other data fields (non sortable) can be grouped in one
523 # F1,F2,F3,F4,F5 only F1 F2 for sorting
524 # my @record = ('KEY',$key++,
527 # 'DATA',join(',',@temp[2..4]) ); #group remainning values, with separator (,)
529 # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
532 my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);
533 my $record = {}; # create a reference to empty hash
534 %{$record} = @record; # populate that hash with @record
535 $entries{$record->{KEY
}} = $record; # add this to a hash of hashes
538 open(FILE
, ">$datafile") or die 'Unable to open aliases file.';
540 # Each field value is printed , with the newline ! Don't forget separator and order of them.
541 foreach my $entry (sort fixedleasesort
keys %entries) {
542 print FILE
"$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";
546 # Reload sorted @current
547 open (FILE
, "$datafile");
553 # Build the configuration file for application aliases
555 sub BuildConfiguration
{
556 # Restart service associated with this
557 system '/usr/local/bin/setaliases';