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