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