]>
git.ipfire.org Git - people/teissler/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 #workaround to suppress a warning when a variable is used only once
38 my @dummy = ( ${Header
::colouryellow
} );
39 @dummy = ( ${Header
::table1colour
} );
40 @dummy = ( ${Header
::table2colour
} );
44 my $setting = "${General::swroot}/ethernet/settings";
45 our $datafile = "${General::swroot}/ethernet/aliases";
51 #Settings2 for editing the multi-line list
54 $settings{'ENABLED'} = 'off'; # Every check box must be set to off
55 $settings{'NAME'} = '';
56 my @nosaved=('IP','ENABLED','NAME'); # 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|^NAME';
63 my $errormessage = '';
66 &Header
::showhttpheaders
();
68 # Read needed Ipcop netsettings
70 $netsettings{'SORT_ALIASES'} = 'NAME'; # default sort
71 &General
::readhash
($setting, \
%netsettings);
74 &Header
::getcgihash
(\
%settings);
78 if (open(FILE
, "$datafile")) {
84 # Check Settings1 first because they are needed before working on @current
86 # Remove if no Setting1 needed
88 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
91 #Validate static Settings1 here
94 unless ($errormessage) { # Everything is ok, save settings
95 #map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
96 #&General::writehash($setting, \%settings); # Save good settings
97 #$settings{'ACTION'} = $Lang::tr{'save'}; # Recreate 'ACTION'
98 #map ($settings{$_}= '',(@nosaved,'KEY1')); # and reinit var to empty
100 # Rebuild configuration file if needed
104 ERROR
: # Leave the faulty field untouched
106 #&General::readhash($setting, \%settings); # Get saved settings and reset to good if needed
109 ## Now manipulate the multi-line list with Settings2
111 # toggle the check box
112 # add/update a new line
113 # begin editing a line
117 # Toggle enable/disable field. Field is in second position
118 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
120 chomp(@current[$settings{'KEY1'}]);
121 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
122 $temp[1] = $temp[1] eq 'on' ?
'off' : 'on'; # Toggle the field
123 $temp[2] = '' if ( $temp[2] eq '' );
124 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
125 $settings{'KEY1'} = ''; # End edit mode
127 &General
::log($Lang::tr
{'ip alias changed'});
130 open(FILE
, ">$datafile") or die 'Unable to open aliases file.';
134 # Rebuild configuration file
138 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
140 if (! &General
::validip
($settings{'IP'})) {$errormessage = "invalid ip"};
141 $settings{'NAME'} = &Header
::cleanhtml
($settings{'NAME'});
143 # Make sure we haven't duplicated an alias or RED
145 if ($settings{'IP'} eq $netsettings{'RED_ADDRESS'}) {
146 $errormessage = $Lang::tr
{'duplicate ip'} . ' (RED)';
150 foreach my $line (@current) {
152 my @temp = split (/\,/, $line);
153 if ( ($settings{'KEY1'} eq '')||(($settings{'KEY1'} ne '') && ($settings{'KEY1'} != $idx))) { # update
154 if ($temp[0] eq $settings{'IP'}) {
155 $errormessage .= $spacer.$Lang::tr
{'duplicate ip'};
158 if ($temp[2] eq $settings{'NAME'} && $temp[2] ne '') {
159 $errormessage .= $spacer.$Lang::tr
{'duplicate name'};
165 unless ($errormessage) {
166 if ($settings{'KEY1'} eq '') { #add or edit ?
167 unshift (@current, "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n");
168 &General
::log($Lang::tr
{'ip alias added'});
170 @current[$settings{'KEY1'}] = "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n";
171 $settings{'KEY1'} = ''; # End edit mode
172 &General
::log($Lang::tr
{'ip alias changed'});
175 # Write changes to config file.
176 &SortDataFile
; # sort newly added/modified entry
178 &BuildConfiguration
; # then re-build conf which use new data
181 ## if entering data line is repetitive, choose here to not erase fields between each addition
183 map ($settings{$_}='' ,@nosaved); # Clear fields
187 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
189 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
191 my @temp = split(/\,/, $line);
194 ## move data fields to Setting2 for edition
196 $settings{'IP'}=$temp[0]; # Prepare the screen for editing
197 $settings{'ENABLED'}=$temp[1];
198 $settings{'NAME'}=$temp[2];
201 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
202 splice (@current,$settings{'KEY1'},1); # Delete line
203 open(FILE
, ">$datafile") or die 'Unable to open aliases file.';
206 $settings{'KEY1'} = ''; # End remove mode
207 &General
::log($Lang::tr
{'ip alias removed'});
209 &BuildConfiguration
; # then re-build conf which use new data
214 ## Check if sorting is asked
215 # If same column clicked, reverse the sort.
216 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
217 my $newsort=$ENV{'QUERY_STRING'};
218 my $actual=$netsettings{'SORT_ALIASES'};
219 #Reverse actual sort ?
220 if ($actual =~ $newsort) {
222 if ($actual !~ 'Rev') {
227 $netsettings{'SORT_ALIASES'}=$newsort;
228 &General
::writehash
($setting, \
%netsettings);
230 $settings{'ACTION'} = 'SORT'; # Recreate 'ACTION'
233 # Default initial value
234 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
235 $settings{'ENABLED'} ='on';
238 &Header
::openpage
($Lang::tr
{'external aliases configuration'}, 1, '');
239 &Header
::openbigbox
('100%', 'left', '', $errormessage);
240 my %checked =(); # Checkbox manipulations
243 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
244 print "<font class='base'>$errormessage </font>";
247 unless (( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ ) && ($netsettings{'RED_TYPE'} eq 'STATIC'))
249 &Header
::openbox
('100%', 'left', $Lang::tr
{'capswarning'});
253 <td width='100%' class='boldbase' align='center'><font color='${Header::colourred}'><b>$Lang::tr{'aliases not active'}</b></font></td>
262 # Second check box is for editing the list
264 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq 'on') ?
"checked='checked'" : '' ;
266 my $buttontext = $Lang::tr
{'add'};
267 if ($settings{'KEY1'} ne '') {
268 $buttontext = $Lang::tr
{'update'};
269 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing alias'});
271 &Header
::openbox
('100%', 'left', $Lang::tr
{'add new alias'});
274 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
276 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
277 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
280 <td class='base'><font color='${Header::colourred}'>$Lang::tr{'name'}: <img src='/blob.gif' alt='*' /></font></td>
281 <td><input type='text' name='NAME' value='$settings{'NAME'}' size='32' /></td>
282 <td class='base' align='right'><font color='${Header::colourred}'>$Lang::tr{'alias ip'}: </font></td>
283 <td><input type='text' name='IP' value='$settings{'IP'}' size='16' /></td>
284 <td class='base' align='right'>$Lang::tr{'enabled'} </td>
285 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
291 <td class='base' width='55%'><img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'this field may be blank'}</td>
292 <td width='40%' align='center'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
293 <td width='5%' align='right'>
302 # Add visual indicators to column headings to show sort order - EO
306 if ($netsettings{'SORT_ALIASES'} eq 'NAMERev') {
307 $sortarrow1 = $Header::sortdn
;
308 } elsif ($netsettings{'SORT_ALIASES'} eq 'NAME') {
309 $sortarrow1 = $Header::sortup
;
310 } elsif ($netsettings{'SORT_ALIASES'} eq 'IPRev') {
311 $sortarrow2 = $Header::sortdn
;
313 $sortarrow2 = $Header::sortup
;
317 # Third box shows the list, in columns
319 # Columns headers may content a link. In this case it must be named in $sortstring
321 &Header
::openbox
('100%', 'left', $Lang::tr
{'current aliases'});
325 <td width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</td>
326 <td width='45%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</td>
327 <td width='5%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>
333 # Print each line of @current list
335 # each data line is splitted into @temp.
339 foreach my $line (@current) {
341 my @temp = split(/\,/,$line);
343 #Choose icon for checkbox
346 if ($temp[1] eq "on") {
348 $gdesc = $Lang::tr
{'click to disable'};
351 $gdesc = $Lang::tr
{'click to enable'};
355 if ($settings{'KEY1'} eq $key) {
356 print "<tr bgcolor='${Header::colouryellow}'>";
358 print "<tr bgcolor='${Header::table2colour}'>";
360 print "<tr bgcolor='${Header::table1colour}'>";
364 <td align='center'>$temp[2]</td>
365 <td align='center'>$temp[0]</td>
368 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
369 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
370 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
371 <input type='hidden' name='KEY1' value='$key' />
376 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
377 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
378 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
379 <input type='hidden' name='KEY1' value='$key' />
384 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
385 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
386 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
387 <input type='hidden' name='KEY1' value='$key' />
397 # If table contains entries, print 'Key to action icons'
402 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
403 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
404 <td class='base'>$Lang::tr{'click to disable'}</td>
405 <td> </td>
406 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
407 <td class='base'>$Lang::tr{'click to enable'}</td>
408 <td> </td>
409 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
410 <td class='base'>$Lang::tr{'edit'}</td>
411 <td> </td>
412 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
413 <td class='base'>$Lang::tr{'remove'}</td>
421 &Header
::closebigbox
();
422 &Header
::closepage
();
424 ## Ouf it's the end !
428 # Sort the "current" array according to choices
433 # Sort pair of record received in $a $b special vars.
434 # When IP is specified use numeric sort else alpha.
435 # If sortname ends with 'Rev', do reverse sort.
438 my $qs=''; # The sort field specified minus 'Rev'
439 if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
440 $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
442 my @a = split(/\./,$entries{$a}->{$qs});
443 my @b = split(/\./,$entries{$b}->{$qs});
449 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
451 } else { #not reverse
452 $qs=$netsettings{'SORT_ALIASES'};
454 my @a = split(/\./,$entries{$a}->{$qs});
455 my @b = split(/\./,$entries{$b}->{$qs});
461 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
466 #Use an associative array (%entries)
468 foreach my $line (@current) {
469 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
470 my @temp = split (',',$line);
472 # Build a pair 'Field Name',value for each of the data dataline.
473 # Each SORTABLE field must have is pair.
474 # Other data fields (non sortable) can be grouped in one
477 # F1,F2,F3,F4,F5 only F1 F2 for sorting
478 # my @record = ('KEY',$key++,
481 # 'DATA',join(',',@temp[2..4]) ); #group remainning values, with separator (,)
483 # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
486 my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);
487 my $record = {}; # create a reference to empty hash
488 %{$record} = @record; # populate that hash with @record
489 $entries{$record->{KEY
}} = $record; # add this to a hash of hashes
492 open(FILE
, ">$datafile") or die 'Unable to open aliases file.';
494 # Each field value is printed , with the newline ! Don't forget separator and order of them.
495 foreach my $entry (sort fixedleasesort
keys %entries) {
496 print FILE
"$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";
500 # Reload sorted @current
501 open (FILE
, "$datafile");
507 # Build the configuration file for application aliases
509 sub BuildConfiguration
{
510 # Restart service associated with this
511 system '/usr/local/bin/setaliases';