]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/routing.cgi
ddns: Import latest upstream patches for ddns-013
[people/pmueller/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'}) {
6be114f0 121 # Validate inputs
1574f255
MT
122 if (!&General::validipandmask($settings{'IP'})){
123 $errormessage = $Lang::tr{'invalid ip'}." / ".$Lang::tr{'invalid netmask'};
6be114f0
AM
124 }else{
125 #set networkip if not already correctly defined
126 my($ip,$cidr) = split(/\//,$settings{'IP'});
127 $cidr = &General::iporsubtocidr($cidr);
128 my $netip=&General::getnetworkip($ip,$cidr);
129 $settings{'IP'} = "$netip/$cidr";
a5b0d8bf
CS
130 }
131
860ad8cb 132 if ($settings{'IP'} =~ /^0\.0\.0\.0/){
a5b0d8bf
CS
133 $errormessage = $Lang::tr{'invalid ip'}." - 0.0.0.0";
134 }
135
136 if( !&General::validip($settings{'GATEWAY'}) ) {
137 $errormessage = $Lang::tr{'invalid ip'}. " - ".$Lang::tr{'gateway ip'};
138 }
139
88e64c23
MT
140 # Escape input in REMARK field
141 $settings{'REMARK'} = &Header::escape($settings{'REMARK'});
142
29f238b2
AM
143 #set networkip if not already correctly defined
144 my($ip,$cidr) = split(/\//,$settings{'IP'});
145 my $netip=&General::getnetworkip($ip,$cidr);
146 $settings{'IP'} = "$netip/$cidr";
147
148 #Check for already existing routing entry
149 foreach my $line (@current) {
150 chomp($line); # remove newline
151 my @temp=split(/\,/,$line);
152 $temp[2] ='' unless defined $temp[2]; # not always populated
153 $temp[3] ='' unless defined $temp[2]; # not always populated
154 #Same ip already used?
1e656e8a 155 if($temp[1] eq $settings{'IP'} && $settings{'KEY1'} eq ''){
29f238b2
AM
156 $errormessage = $Lang::tr{'ccd err irouteexist'};
157 last;
158 }
159 #Is the network part of an internal network?
51141b15 160 $errormessage .= &General::check_net_internal_exact($settings{'IP'});
29f238b2
AM
161 last;
162 }
163
a5b0d8bf
CS
164 unless ($errormessage) {
165 if ($settings{'KEY1'} eq '') { #add or edit ?
166 unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'GATEWAY'},$settings{'REMARK'}\n");
167 &General::log($Lang::tr{'routing config added'});
168 } else {
169 @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'GATEWAY'},$settings{'REMARK'}\n";
170 $settings{'KEY1'} = ''; # End edit mode
171 &General::log($Lang::tr{'routing config changed'});
172 }
173
174 # Write changes to config file.
175 &SortDataFile; # sort newly added/modified entry
176 &BuildConfiguration; # then re-build routing
177
178 #map ($settings{$_}='' ,@nosaved); # Clear fields
179 }
180}
181
182if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
183 #move out new line
184 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
185 chomp($line);
186 my @temp = split(/\,/, $line);
187 $settings{'EN'}=$temp[0]; # Prepare the screen for editing
188 $settings{'IP'}=$temp[1];
189 $settings{'GATEWAY'}=$temp[2];
190 $settings{'REMARK'}=$temp[3];
191 &BuildConfiguration;
192}
193
194if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
195 splice (@current,$settings{'KEY1'},1); # Delete line
196 open(FILE, ">$datafile") or die 'route datafile error';
197 print FILE @current;
198 close(FILE);
199 $settings{'KEY1'} = ''; # End remove mode
200 &General::log($Lang::tr{'route config changed'});
201
202 &BuildConfiguration; # then re-build conf which use new data
203}
204
205## Check if sorting is asked
206# If same column clicked, reverse the sort.
207if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
208 my $newsort=$ENV{'QUERY_STRING'};
209 my $actual=$settings{'SORT_GATEWAYLIST'};
210 #Reverse actual sort ?
211 if ($actual =~ $newsort) {
212 my $Rev='';
213 if ($actual !~ 'Rev') {
214 $Rev='Rev';
215 }
216 $newsort.=$Rev;
217 }
218 $settings{'SORT_GATEWAYLIST'}=$newsort;
219 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
220 &General::writehash($setting, \%settings);
221 &SortDataFile;
222 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
223 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
224}
225
226if ($settings{'ACTION'} eq '' ) { # First launch from GUI
227 # Place here default value when nothing is initialized
228 $settings{'EN'} = 'on';
229 $settings{'GATEWAY'} = '';
230 $settings{'IP'} = '';
231}
232
233&Header::openpage($Lang::tr{'routing table entries'}, 1, '');
234&Header::openbigbox('100%', 'left', '', $errormessage);
235my %checked=(); # Checkbox manipulations
236
237if ($errormessage) {
238 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
239 print "<font class='base'>$errormessage&nbsp;</font>";
240 &Header::closebox();
241}
242
243#
244
245$checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ? '' : "checked='checked'";
246
247my $buttontext = $Lang::tr{'add'};
248if ($settings{'KEY1'} ne '') {
249 $buttontext = $Lang::tr{'update'};
250 &Header::openbox('100%', 'left', $Lang::tr{'Edit an existing route'});
251} else {
252 &Header::openbox('100%', 'left', $Lang::tr{'Add a route'});
253}
254
255#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
256print <<END
257<form method='post' action='$ENV{'SCRIPT_NAME'}'>
258<input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
259<table width='100%'>
260<tr>
261 <td class='base'>$Lang::tr{'host ip'} / $Lang::tr{'network'}:&nbsp;</td>
262 <td><input type='text' name='IP' value='$settings{'IP'}' size='25'/></td>
263</tr><tr>
264 <td class='base'>$Lang::tr{'gateway'}:&nbsp;</td>
265 <td><input type='text' name='GATEWAY' value='$settings{'GATEWAY'}' size='25'/></td>
266 <td class='base'>$Lang::tr{'enabled'}</td>
267 <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td>
268</tr>
269</tr>
270 <td class='base'>$Lang::tr{'remark'}:&nbsp;</td>
271 <td><input type='text' name='REMARK' value='$settings{'REMARK'}' size='25'/></td>
272</tr>
273</table>
b84989af 274<br>
a5b0d8bf
CS
275<table width='100%'>
276<tr>
b84989af 277 <td width='50%' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
a5b0d8bf
CS
278</tr>
279</table>
280</form>
281END
282;
283&Header::closebox();
284
285&Header::openbox('100%', 'left', $Lang::tr{'routing table'});
286print <<END
623758aa
AM
287
288<table width='100%' class='tbl'>
a5b0d8bf 289<tr>
623758aa
AM
290 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'} / $Lang::tr{'network'}</b></a></th>
291 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?GATEWAY'><b>$Lang::tr{'gateway'}</b></a></th>
292 <th width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?REMARK'><b>$Lang::tr{'remark'}</b></a></th>
293 <th width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
a5b0d8bf
CS
294</tr>
295END
296;
297
298#
299# Print each line of @current list
300#
301
302my $key = 0;
623758aa 303my $col="";
a5b0d8bf
CS
304foreach my $line (@current) {
305 chomp($line); # remove newline
306 my @temp=split(/\,/,$line);
307 $temp[2] ='' unless defined $temp[2]; # not always populated
308 $temp[3] ='' unless defined $temp[2]; # not always populated
309
310 #Choose icon for checkbox
311 my $gif = '';
312 my $gdesc = '';
313 if ($temp[0] ne '' ) {
314 $gif = 'on.gif';
315 $gdesc = $Lang::tr{'click to disable'};
316 } else {
317 $gif = 'off.gif';
318 $gdesc = $Lang::tr{'click to enable'};
319 }
320
321 #Colorize each line
322 if ($settings{'KEY1'} eq $key) {
323 print "<tr bgcolor='${Header::colouryellow}'>";
324 } elsif ($key % 2) {
623758aa
AM
325 print "<tr>";
326 $col="bgcolor='$color{'color20'}'";
a5b0d8bf 327 } else {
623758aa
AM
328 print "<tr>";
329 $col="bgcolor='$color{'color22'}'";
a5b0d8bf
CS
330 }
331 print <<END
623758aa
AM
332<td align='center' $col>$temp[1]</td>
333<td align='center' $col>$temp[2]</td>
334<td align='center' $col>$temp[3]</td>
335<td align='center' $col>
a5b0d8bf
CS
336<form method='post' action='$ENV{'SCRIPT_NAME'}'>
337<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
338<input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
339<input type='hidden' name='KEY1' value='$key' />
340</form>
341</td>
342
623758aa 343<td align='center' $col>
a5b0d8bf
CS
344<form method='post' action='$ENV{'SCRIPT_NAME'}'>
345<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
346<input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
347<input type='hidden' name='KEY1' value='$key' />
348</form>
349</td>
350
623758aa 351<td align='center' $col>
a5b0d8bf
CS
352<form method='post' action='$ENV{'SCRIPT_NAME'}'>
353<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
354<input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
355<input type='hidden' name='KEY1' value='$key' />
356</form>
357</td>
358</tr>
359END
360;
361 $key++;
362}
363print "</table>";
364
365# If table contains entries, print 'Key to action icons'
366if ($key) {
367print <<END
368<table>
369<tr>
370 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
371 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
372 <td class='base'>$Lang::tr{'click to disable'}</td>
373 <td>&nbsp;&nbsp;</td>
374 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
375 <td class='base'>$Lang::tr{'click to enable'}</td>
376 <td>&nbsp;&nbsp;</td>
377 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
378 <td class='base'>$Lang::tr{'edit'}</td>
379 <td>&nbsp;&nbsp;</td>
380 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
381 <td class='base'>$Lang::tr{'remove'}</td>
382</tr>
383</table>
384END
385;
386}
387
388&Header::closebox();
389
390my $output = `/sbin/ip route show table static`;
391$output = &Header::cleanhtml($output,"y");
392
393if ( $output != "" ) {
394 &Header::openbox('100%', 'left', $Lang::tr{'routing table entries'});
395 print "<pre>$output</pre>\n";
396 &Header::closebox();
397}
398
399&Header::closebigbox();
400&Header::closepage();
401
402## Ouf it's the end !
403
404# Sort the "current" array according to choices
405sub SortDataFile
406{
407 our %entries = ();
408
409 # Sort pair of record received in $a $b special vars.
410 # When IP is specified use numeric sort else alpha.
411 # If sortname ends with 'Rev', do reverse sort.
412 #
413 sub fixedleasesort {
414 my $qs=''; # The sort field specified minus 'Rev'
415 if (rindex ($settings{'SORT_GATEWAYLIST'},'Rev') != -1) {
416 $qs=substr ($settings{'SORT_GATEWAYLIST'},0,length($settings{'SORT_GATEWAYLIST'})-3);
417 if ($qs eq 'IP') {
418 my @a = split(/\./,$entries{$a}->{$qs});
419 my @b = split(/\./,$entries{$b}->{$qs});
420 ($b[0]<=>$a[0]) ||
421 ($b[1]<=>$a[1]) ||
422 ($b[2]<=>$a[2]) ||
423 ($b[3]<=>$a[3]);
424 } else {
425 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
426 }
427 } else { #not reverse
428 $qs=$settings{'SORT_GATEWAYLIST'};
429 if ($qs eq 'IP') {
430 my @a = split(/\./,$entries{$a}->{$qs});
431 my @b = split(/\./,$entries{$b}->{$qs});
432 ($a[0]<=>$b[0]) ||
433 ($a[1]<=>$b[1]) ||
434 ($a[2]<=>$b[2]) ||
435 ($a[3]<=>$b[3]);
436 } else {
437 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
438 }
439 }
440 }
441
442 #Use an associative array (%entries)
443 my $key = 0;
444 foreach my $line (@current) {
445 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
446 my @temp = ( '','','', '');
447 @temp = split (',',$line);
448
449 # Build a pair 'Field Name',value for each of the data dataline.
450 # Each SORTABLE field must have is pair.
451 # Other data fields (non sortable) can be grouped in one
452
453 my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'GATEWAY',$temp[2],'REMARK',$temp[3]);
454 my $record = {}; # create a reference to empty hash
455 %{$record} = @record; # populate that hash with @record
456 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
457 }
458
459 open(FILE, ">$datafile") or die 'routing datafile error';
460
461 # Each field value is printed , with the newline ! Don't forget separator and order of them.
462 foreach my $entry (sort fixedleasesort keys %entries) {
463 print FILE "$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{GATEWAY},$entries{$entry}->{REMARK}\n";
464 }
465
466 close(FILE);
467 # Reload sorted @current
468 open (FILE, "$datafile");
469 @current = <FILE>;
470 close (FILE);
471}
472
473#
474# Build the configuration file
475#
476sub BuildConfiguration {
477 system '/usr/local/bin/rebuildroutes';
ff1ed674 478}