]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/routing.cgi
Merge remote-tracking branch 'ms/dhcp-rfc2136-broken-down' into next
[ipfire-2.x.git] / html / cgi-bin / routing.cgi
CommitLineData
a5b0d8bf
CS
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007-2011 IPFire Team <info@ipfire.org> #
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
22use strict;
23
24# enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31
32#workaround to suppress a warning when a variable is used only once
33my @dummy = ( ${Header::colouryellow} );
34undef (@dummy);
35
36# Files used
37my $setting = "${General::swroot}/main/settings";
38our $datafile = "${General::swroot}/main/routing"; #(our: used in subroutine)
39
40my %color = ();
41my %mainsettings = ();
42&General::readhash("${General::swroot}/main/settings", \%mainsettings);
43&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
44
45our %settings = ();
46
47$settings{'EN'} = ''; # reuse for dummy field in position zero
48$settings{'IP'} = '';
49$settings{'GATEWAY'} = '';
50$settings{'REMARK'} = '';
51my @nosaved=('EN','IP','GATEWAY','REMARK'); # List here ALL setting2 fields. Mandatory
52
53$settings{'ACTION'} = ''; # add/edit/remove
54$settings{'KEY1'} = ''; # point record for ACTION
55
56#Define each field that can be used to sort columns
57my $sortstring='^IP|^GATEWAY|^REMARK';
58$settings{'SORT_GATEWAYLIST'} = 'GATEWAY';
59my $errormessage = '';
60my $warnmessage = '';
61
62&Header::showhttpheaders();
63
64#Get GUI values
65&Header::getcgihash(\%settings);
66
67###############
68# DEBUG DEBUG
69#&Header::openbox('100%', 'left', 'DEBUG');
70#my $debugCount = 0;
71#foreach my $line (sort keys %settings) {
72#print "$line = $settings{$line}<br />\n";
73# $debugCount++;
74#}
75#print "&nbsp;Count: $debugCount\n";
76#&Header::closebox();
77# DEBUG DEBUG
78###############
79
80# Load multiline data
81our @current = ();
82if (open(FILE, "$datafile")) {
83 @current = <FILE>;
84 close (FILE);
85}
86
87## Settings1 Box not used...
88&General::readhash("${General::swroot}/main/settings", \%settings);
89
90
91## Now manipulate the multi-line list with Settings2
92# Basic actions are:
93# toggle the check box
94# add/update a new line
95# begin editing a line
96# remove a line
97
98
99# Toggle enable/disable field. Field is in second position
100if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
101 #move out new line
102 chomp(@current[$settings{'KEY1'}]);
103 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
104
105 $temp[0] = $temp[0] ne '' ? '' : 'on'; # Toggle the field
106 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
107 $settings{'KEY1'} = ''; # End edit mode
108
109 &General::log($Lang::tr{'routing config changed'});
110
111 #Save current
112 open(FILE, ">$datafile") or die 'routing datafile error';
113 print FILE @current;
114 close(FILE);
115
116 # Rebuild configuration file
117 &BuildConfiguration;
118}
119
120if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
121
6be114f0
AM
122 # Validate inputs
123 if (!&General::validipandmask($settings{'IP'}))){
a5b0d8bf 124 $errormessage = $Lang::tr{'invalid ip'}." / ".$Lang::tr{'invalid netmask'};
6be114f0
AM
125 }else{
126 #set networkip if not already correctly defined
127 my($ip,$cidr) = split(/\//,$settings{'IP'});
128 $cidr = &General::iporsubtocidr($cidr);
129 my $netip=&General::getnetworkip($ip,$cidr);
130 $settings{'IP'} = "$netip/$cidr";
a5b0d8bf
CS
131 }
132
860ad8cb 133 if ($settings{'IP'} =~ /^0\.0\.0\.0/){
a5b0d8bf
CS
134 $errormessage = $Lang::tr{'invalid ip'}." - 0.0.0.0";
135 }
136
137 if( !&General::validip($settings{'GATEWAY'}) ) {
138 $errormessage = $Lang::tr{'invalid ip'}. " - ".$Lang::tr{'gateway ip'};
139 }
140
29f238b2
AM
141 #set networkip if not already correctly defined
142 my($ip,$cidr) = split(/\//,$settings{'IP'});
143 my $netip=&General::getnetworkip($ip,$cidr);
144 $settings{'IP'} = "$netip/$cidr";
145
146 #Check for already existing routing entry
147 foreach my $line (@current) {
148 chomp($line); # remove newline
149 my @temp=split(/\,/,$line);
150 $temp[2] ='' unless defined $temp[2]; # not always populated
151 $temp[3] ='' unless defined $temp[2]; # not always populated
152 #Same ip already used?
153 if($temp[1] eq $settings{'IP'}){
154 $errormessage = $Lang::tr{'ccd err irouteexist'};
155 last;
156 }
157 #Is the network part of an internal network?
158 $errormessage .= &General::check_net_internal($settings{'IP'});
159 last;
160 }
161
a5b0d8bf
CS
162 unless ($errormessage) {
163 if ($settings{'KEY1'} eq '') { #add or edit ?
164 unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'GATEWAY'},$settings{'REMARK'}\n");
165 &General::log($Lang::tr{'routing config added'});
166 } else {
167 @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'GATEWAY'},$settings{'REMARK'}\n";
168 $settings{'KEY1'} = ''; # End edit mode
169 &General::log($Lang::tr{'routing config changed'});
170 }
171
172 # Write changes to config file.
173 &SortDataFile; # sort newly added/modified entry
174 &BuildConfiguration; # then re-build routing
175
176 #map ($settings{$_}='' ,@nosaved); # Clear fields
177 }
178}
179
180if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
181 #move out new line
182 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
183 chomp($line);
184 my @temp = split(/\,/, $line);
185 $settings{'EN'}=$temp[0]; # Prepare the screen for editing
186 $settings{'IP'}=$temp[1];
187 $settings{'GATEWAY'}=$temp[2];
188 $settings{'REMARK'}=$temp[3];
189 &BuildConfiguration;
190}
191
192if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
193 splice (@current,$settings{'KEY1'},1); # Delete line
194 open(FILE, ">$datafile") or die 'route datafile error';
195 print FILE @current;
196 close(FILE);
197 $settings{'KEY1'} = ''; # End remove mode
198 &General::log($Lang::tr{'route config changed'});
199
200 &BuildConfiguration; # then re-build conf which use new data
201}
202
203## Check if sorting is asked
204# If same column clicked, reverse the sort.
205if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
206 my $newsort=$ENV{'QUERY_STRING'};
207 my $actual=$settings{'SORT_GATEWAYLIST'};
208 #Reverse actual sort ?
209 if ($actual =~ $newsort) {
210 my $Rev='';
211 if ($actual !~ 'Rev') {
212 $Rev='Rev';
213 }
214 $newsort.=$Rev;
215 }
216 $settings{'SORT_GATEWAYLIST'}=$newsort;
217 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
218 &General::writehash($setting, \%settings);
219 &SortDataFile;
220 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
221 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
222}
223
224if ($settings{'ACTION'} eq '' ) { # First launch from GUI
225 # Place here default value when nothing is initialized
226 $settings{'EN'} = 'on';
227 $settings{'GATEWAY'} = '';
228 $settings{'IP'} = '';
229}
230
231&Header::openpage($Lang::tr{'routing table entries'}, 1, '');
232&Header::openbigbox('100%', 'left', '', $errormessage);
233my %checked=(); # Checkbox manipulations
234
235if ($errormessage) {
236 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
237 print "<font class='base'>$errormessage&nbsp;</font>";
238 &Header::closebox();
239}
240
241#
242
243$checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ? '' : "checked='checked'";
244
245my $buttontext = $Lang::tr{'add'};
246if ($settings{'KEY1'} ne '') {
247 $buttontext = $Lang::tr{'update'};
248 &Header::openbox('100%', 'left', $Lang::tr{'Edit an existing route'});
249} else {
250 &Header::openbox('100%', 'left', $Lang::tr{'Add a route'});
251}
252
253#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
254print <<END
255<form method='post' action='$ENV{'SCRIPT_NAME'}'>
256<input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
257<table width='100%'>
258<tr>
259 <td class='base'>$Lang::tr{'host ip'} / $Lang::tr{'network'}:&nbsp;</td>
260 <td><input type='text' name='IP' value='$settings{'IP'}' size='25'/></td>
261</tr><tr>
262 <td class='base'>$Lang::tr{'gateway'}:&nbsp;</td>
263 <td><input type='text' name='GATEWAY' value='$settings{'GATEWAY'}' size='25'/></td>
264 <td class='base'>$Lang::tr{'enabled'}</td>
265 <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td>
266</tr>
267</tr>
268 <td class='base'>$Lang::tr{'remark'}:&nbsp;</td>
269 <td><input type='text' name='REMARK' value='$settings{'REMARK'}' size='25'/></td>
270</tr>
271</table>
b84989af 272<br>
a5b0d8bf
CS
273<table width='100%'>
274<tr>
b84989af 275 <td width='50%' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
a5b0d8bf
CS
276</tr>
277</table>
278</form>
279END
280;
281&Header::closebox();
282
283&Header::openbox('100%', 'left', $Lang::tr{'routing table'});
284print <<END
623758aa
AM
285
286<table width='100%' class='tbl'>
a5b0d8bf 287<tr>
623758aa
AM
288 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'} / $Lang::tr{'network'}</b></a></th>
289 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?GATEWAY'><b>$Lang::tr{'gateway'}</b></a></th>
290 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?REMARK'><b>$Lang::tr{'remark'}</b></a></th>
291 <th width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
a5b0d8bf
CS
292</tr>
293END
294;
295
296#
297# Print each line of @current list
298#
299
300my $key = 0;
623758aa 301my $col="";
a5b0d8bf
CS
302foreach my $line (@current) {
303 chomp($line); # remove newline
304 my @temp=split(/\,/,$line);
305 $temp[2] ='' unless defined $temp[2]; # not always populated
306 $temp[3] ='' unless defined $temp[2]; # not always populated
307
308 #Choose icon for checkbox
309 my $gif = '';
310 my $gdesc = '';
311 if ($temp[0] ne '' ) {
312 $gif = 'on.gif';
313 $gdesc = $Lang::tr{'click to disable'};
314 } else {
315 $gif = 'off.gif';
316 $gdesc = $Lang::tr{'click to enable'};
317 }
318
319 #Colorize each line
320 if ($settings{'KEY1'} eq $key) {
321 print "<tr bgcolor='${Header::colouryellow}'>";
322 } elsif ($key % 2) {
623758aa
AM
323 print "<tr>";
324 $col="bgcolor='$color{'color20'}'";
a5b0d8bf 325 } else {
623758aa
AM
326 print "<tr>";
327 $col="bgcolor='$color{'color22'}'";
a5b0d8bf
CS
328 }
329 print <<END
623758aa
AM
330<td align='center' $col>$temp[1]</td>
331<td align='center' $col>$temp[2]</td>
332<td align='center' $col>$temp[3]</td>
333<td align='center' $col>
a5b0d8bf
CS
334<form method='post' action='$ENV{'SCRIPT_NAME'}'>
335<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
336<input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
337<input type='hidden' name='KEY1' value='$key' />
338</form>
339</td>
340
623758aa 341<td align='center' $col>
a5b0d8bf
CS
342<form method='post' action='$ENV{'SCRIPT_NAME'}'>
343<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
344<input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
345<input type='hidden' name='KEY1' value='$key' />
346</form>
347</td>
348
623758aa 349<td align='center' $col>
a5b0d8bf
CS
350<form method='post' action='$ENV{'SCRIPT_NAME'}'>
351<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
352<input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
353<input type='hidden' name='KEY1' value='$key' />
354</form>
355</td>
356</tr>
357END
358;
359 $key++;
360}
361print "</table>";
362
363# If table contains entries, print 'Key to action icons'
364if ($key) {
365print <<END
366<table>
367<tr>
368 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
369 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
370 <td class='base'>$Lang::tr{'click to disable'}</td>
371 <td>&nbsp;&nbsp;</td>
372 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
373 <td class='base'>$Lang::tr{'click to enable'}</td>
374 <td>&nbsp;&nbsp;</td>
375 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
376 <td class='base'>$Lang::tr{'edit'}</td>
377 <td>&nbsp;&nbsp;</td>
378 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
379 <td class='base'>$Lang::tr{'remove'}</td>
380</tr>
381</table>
382END
383;
384}
385
386&Header::closebox();
387
388my $output = `/sbin/ip route show table static`;
389$output = &Header::cleanhtml($output,"y");
390
391if ( $output != "" ) {
392 &Header::openbox('100%', 'left', $Lang::tr{'routing table entries'});
393 print "<pre>$output</pre>\n";
394 &Header::closebox();
395}
396
397&Header::closebigbox();
398&Header::closepage();
399
400## Ouf it's the end !
401
402# Sort the "current" array according to choices
403sub SortDataFile
404{
405 our %entries = ();
406
407 # Sort pair of record received in $a $b special vars.
408 # When IP is specified use numeric sort else alpha.
409 # If sortname ends with 'Rev', do reverse sort.
410 #
411 sub fixedleasesort {
412 my $qs=''; # The sort field specified minus 'Rev'
413 if (rindex ($settings{'SORT_GATEWAYLIST'},'Rev') != -1) {
414 $qs=substr ($settings{'SORT_GATEWAYLIST'},0,length($settings{'SORT_GATEWAYLIST'})-3);
415 if ($qs eq 'IP') {
416 my @a = split(/\./,$entries{$a}->{$qs});
417 my @b = split(/\./,$entries{$b}->{$qs});
418 ($b[0]<=>$a[0]) ||
419 ($b[1]<=>$a[1]) ||
420 ($b[2]<=>$a[2]) ||
421 ($b[3]<=>$a[3]);
422 } else {
423 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
424 }
425 } else { #not reverse
426 $qs=$settings{'SORT_GATEWAYLIST'};
427 if ($qs eq 'IP') {
428 my @a = split(/\./,$entries{$a}->{$qs});
429 my @b = split(/\./,$entries{$b}->{$qs});
430 ($a[0]<=>$b[0]) ||
431 ($a[1]<=>$b[1]) ||
432 ($a[2]<=>$b[2]) ||
433 ($a[3]<=>$b[3]);
434 } else {
435 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
436 }
437 }
438 }
439
440 #Use an associative array (%entries)
441 my $key = 0;
442 foreach my $line (@current) {
443 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
444 my @temp = ( '','','', '');
445 @temp = split (',',$line);
446
447 # Build a pair 'Field Name',value for each of the data dataline.
448 # Each SORTABLE field must have is pair.
449 # Other data fields (non sortable) can be grouped in one
450
451 my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'GATEWAY',$temp[2],'REMARK',$temp[3]);
452 my $record = {}; # create a reference to empty hash
453 %{$record} = @record; # populate that hash with @record
454 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
455 }
456
457 open(FILE, ">$datafile") or die 'routing datafile error';
458
459 # Each field value is printed , with the newline ! Don't forget separator and order of them.
460 foreach my $entry (sort fixedleasesort keys %entries) {
461 print FILE "$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{GATEWAY},$entries{$entry}->{REMARK}\n";
462 }
463
464 close(FILE);
465 # Reload sorted @current
466 open (FILE, "$datafile");
467 @current = <FILE>;
468 close (FILE);
469}
470
471#
472# Build the configuration file
473#
474sub BuildConfiguration {
475 system '/usr/local/bin/rebuildroutes';
ff1ed674 476}