]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/routing.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2011 IPFire Team <info@ipfire.org> #
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/routing"; #(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);
47 $settings{'EN'} = ''; # reuse for dummy field in position zero
49 $settings{'GATEWAY'} = '';
50 $settings{'REMARK'} = '';
51 my @nosaved=('EN','IP','GATEWAY','REMARK'); # List here ALL setting2 fields. Mandatory
53 $settings{'ACTION'} = ''; # add/edit/remove
54 $settings{'KEY1'} = ''; # point record for ACTION
56 #Define each field that can be used to sort columns
57 my $sortstring='^IP|^GATEWAY|^REMARK';
58 $settings{'SORT_GATEWAYLIST'} = 'GATEWAY';
59 my $errormessage = '';
62 &Header
::showhttpheaders
();
65 &Header
::getcgihash
(\
%settings);
69 #&Header::openbox('100%', 'left', 'DEBUG');
71 #foreach my $line (sort keys %settings) {
72 #print "$line = $settings{$line}<br />\n";
75 #print " Count: $debugCount\n";
82 if (open(FILE
, "$datafile")) {
87 ## Settings1 Box not used...
88 &General
::readhash
("${General::swroot}/main/settings", \
%settings);
91 ## Now manipulate the multi-line list with Settings2
93 # toggle the check box
94 # add/update a new line
95 # begin editing a line
99 # Toggle enable/disable field. Field is in second position
100 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
102 chomp(@current[$settings{'KEY1'}]);
103 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
105 $temp[0] = $temp[0] ne '' ?
'' : 'on'; # Toggle the field
106 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
107 $settings{'KEY1'} = ''; # End edit mode
109 &General
::log($Lang::tr
{'routing config changed'});
112 open(FILE
, ">$datafile") or die 'routing datafile error';
116 # Rebuild configuration file
120 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
121 # Convert subnet masks to CIDR notation.
122 $settings{'IP'} = &General
::iporsubtocidr
($settings{'IP'});
125 if (( !&General
::validip
($settings{'IP'})) and ( !&General
::validipandmask
($settings{'IP'}))){
126 $errormessage = $Lang::tr
{'invalid ip'}." / ".$Lang::tr
{'invalid netmask'};
129 if ($settings{'IP'} =~ /^0\.0\.0\.0/){
130 $errormessage = $Lang::tr
{'invalid ip'}." - 0.0.0.0";
133 if( !&General
::validip
($settings{'GATEWAY'}) ) {
134 $errormessage = $Lang::tr
{'invalid ip'}. " - ".$Lang::tr
{'gateway ip'};
137 unless ($errormessage) {
138 if ($settings{'KEY1'} eq '') { #add or edit ?
139 unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'GATEWAY'},$settings{'REMARK'}\n");
140 &General
::log($Lang::tr
{'routing config added'});
142 @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'GATEWAY'},$settings{'REMARK'}\n";
143 $settings{'KEY1'} = ''; # End edit mode
144 &General
::log($Lang::tr
{'routing config changed'});
147 # Write changes to config file.
148 &SortDataFile
; # sort newly added/modified entry
149 &BuildConfiguration
; # then re-build routing
151 #map ($settings{$_}='' ,@nosaved); # Clear fields
155 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
157 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
159 my @temp = split(/\,/, $line);
160 $settings{'EN'}=$temp[0]; # Prepare the screen for editing
161 $settings{'IP'}=$temp[1];
162 $settings{'GATEWAY'}=$temp[2];
163 $settings{'REMARK'}=$temp[3];
167 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
168 splice (@current,$settings{'KEY1'},1); # Delete line
169 open(FILE
, ">$datafile") or die 'route datafile error';
172 $settings{'KEY1'} = ''; # End remove mode
173 &General
::log($Lang::tr
{'route config changed'});
175 &BuildConfiguration
; # then re-build conf which use new data
178 ## Check if sorting is asked
179 # If same column clicked, reverse the sort.
180 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
181 my $newsort=$ENV{'QUERY_STRING'};
182 my $actual=$settings{'SORT_GATEWAYLIST'};
183 #Reverse actual sort ?
184 if ($actual =~ $newsort) {
186 if ($actual !~ 'Rev') {
191 $settings{'SORT_GATEWAYLIST'}=$newsort;
192 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
193 &General
::writehash
($setting, \
%settings);
195 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
196 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
199 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
200 # Place here default value when nothing is initialized
201 $settings{'EN'} = 'on';
202 $settings{'GATEWAY'} = '';
203 $settings{'IP'} = '';
206 &Header
::openpage
($Lang::tr
{'routing table entries'}, 1, '');
207 &Header
::openbigbox
('100%', 'left', '', $errormessage);
208 my %checked=(); # Checkbox manipulations
211 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
212 print "<font class='base'>$errormessage </font>";
218 $checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ?
'' : "checked='checked'";
220 my $buttontext = $Lang::tr
{'add'};
221 if ($settings{'KEY1'} ne '') {
222 $buttontext = $Lang::tr
{'update'};
223 &Header
::openbox
('100%', 'left', $Lang::tr
{'Edit an existing route'});
225 &Header
::openbox
('100%', 'left', $Lang::tr
{'Add a route'});
228 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
230 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
231 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
234 <td class='base'>$Lang::tr{'host ip'} / $Lang::tr{'network'}: </td>
235 <td><input type='text' name='IP' value='$settings{'IP'}' size='25'/></td>
237 <td class='base'>$Lang::tr{'gateway'}: </td>
238 <td><input type='text' name='GATEWAY' value='$settings{'GATEWAY'}' size='25'/></td>
239 <td class='base'>$Lang::tr{'enabled'}</td>
240 <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td>
243 <td class='base'>$Lang::tr{'remark'}: </td>
244 <td><input type='text' name='REMARK' value='$settings{'REMARK'}' size='25'/></td>
250 <td width='50%' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
258 &Header
::openbox
('100%', 'left', $Lang::tr
{'routing table'});
261 <table width='100%' class='tbl'>
263 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'} / $Lang::tr{'network'}</b></a></th>
264 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?GATEWAY'><b>$Lang::tr{'gateway'}</b></a></th>
265 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?REMARK'><b>$Lang::tr{'remark'}</b></a></th>
266 <th width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
272 # Print each line of @current list
277 foreach my $line (@current) {
278 chomp($line); # remove newline
279 my @temp=split(/\,/,$line);
280 $temp[2] ='' unless defined $temp[2]; # not always populated
281 $temp[3] ='' unless defined $temp[2]; # not always populated
283 #Choose icon for checkbox
286 if ($temp[0] ne '' ) {
288 $gdesc = $Lang::tr
{'click to disable'};
291 $gdesc = $Lang::tr
{'click to enable'};
295 if ($settings{'KEY1'} eq $key) {
296 print "<tr bgcolor='${Header::colouryellow}'>";
299 $col="bgcolor='$color{'color20'}'";
302 $col="bgcolor='$color{'color22'}'";
305 <td align='center' $col>$temp[1]</td>
306 <td align='center' $col>$temp[2]</td>
307 <td align='center' $col>$temp[3]</td>
308 <td align='center' $col>
309 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
310 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
311 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
312 <input type='hidden' name='KEY1' value='$key' />
316 <td align='center' $col>
317 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
318 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
319 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
320 <input type='hidden' name='KEY1' value='$key' />
324 <td align='center' $col>
325 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
326 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
327 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
328 <input type='hidden' name='KEY1' value='$key' />
338 # If table contains entries, print 'Key to action icons'
343 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
344 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
345 <td class='base'>$Lang::tr{'click to disable'}</td>
346 <td> </td>
347 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
348 <td class='base'>$Lang::tr{'click to enable'}</td>
349 <td> </td>
350 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
351 <td class='base'>$Lang::tr{'edit'}</td>
352 <td> </td>
353 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
354 <td class='base'>$Lang::tr{'remove'}</td>
363 my $output = `/sbin/ip route show table static`;
364 $output = &Header
::cleanhtml
($output,"y");
366 if ( $output != "" ) {
367 &Header
::openbox
('100%', 'left', $Lang::tr
{'routing table entries'});
368 print "<pre>$output</pre>\n";
372 &Header
::closebigbox
();
373 &Header
::closepage
();
375 ## Ouf it's the end !
377 # Sort the "current" array according to choices
382 # Sort pair of record received in $a $b special vars.
383 # When IP is specified use numeric sort else alpha.
384 # If sortname ends with 'Rev', do reverse sort.
387 my $qs=''; # The sort field specified minus 'Rev'
388 if (rindex ($settings{'SORT_GATEWAYLIST'},'Rev') != -1) {
389 $qs=substr ($settings{'SORT_GATEWAYLIST'},0,length($settings{'SORT_GATEWAYLIST'})-3);
391 my @a = split(/\./,$entries{$a}->{$qs});
392 my @b = split(/\./,$entries{$b}->{$qs});
398 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
400 } else { #not reverse
401 $qs=$settings{'SORT_GATEWAYLIST'};
403 my @a = split(/\./,$entries{$a}->{$qs});
404 my @b = split(/\./,$entries{$b}->{$qs});
410 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
415 #Use an associative array (%entries)
417 foreach my $line (@current) {
418 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
419 my @temp = ( '','','', '');
420 @temp = split (',',$line);
422 # Build a pair 'Field Name',value for each of the data dataline.
423 # Each SORTABLE field must have is pair.
424 # Other data fields (non sortable) can be grouped in one
426 my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'GATEWAY',$temp[2],'REMARK',$temp[3]);
427 my $record = {}; # create a reference to empty hash
428 %{$record} = @record; # populate that hash with @record
429 $entries{$record->{KEY
}} = $record; # add this to a hash of hashes
432 open(FILE
, ">$datafile") or die 'routing datafile error';
434 # Each field value is printed , with the newline ! Don't forget separator and order of them.
435 foreach my $entry (sort fixedleasesort
keys %entries) {
436 print FILE
"$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{GATEWAY},$entries{$entry}->{REMARK}\n";
440 # Reload sorted @current
441 open (FILE
, "$datafile");
447 # Build the configuration file
449 sub BuildConfiguration
{
450 system '/usr/local/bin/rebuildroutes';