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