]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/aliases.cgi
Add missing patches for compat-wireless.
[people/teissler/ipfire-2.x.git] / html / cgi-bin / aliases.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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 # this cgi is base on IPCop CGI - aliases.cgi
23 #
24
25 # to fully troubleshot your code, uncomment diagnostics, Carp and cluck lines
26 #use diagnostics; # need to add the file /usr/lib/perl5/5.8.x/pods/perldiag.pod before to work
27 # next look at /var/log/httpd/error_log , http://www.perl.com/pub/a/2002/05/07/mod_perl.html may help
28 #use warnings;
29 use strict;
30 #use Carp ();
31 #local $SIG{__WARN__} = \&Carp::cluck;
32
33 require '/var/ipfire/general-functions.pl'; # replace /var/ipcop with /var/ipcop in case of manual install
34 require "${General::swroot}/lang.pl";
35 require "${General::swroot}/header.pl";
36
37 #workaround to suppress a warning when a variable is used only once
38 my @dummy = ( ${Header::colouryellow} );
39 @dummy = ( ${Header::table1colour} );
40 @dummy = ( ${Header::table2colour} );
41 undef (@dummy);
42
43 # Files used
44 my $setting = "${General::swroot}/ethernet/settings";
45 our $datafile = "${General::swroot}/ethernet/aliases";
46
47
48 our %settings=();
49 #Settings1
50
51 #Settings2 for editing the multi-line list
52 #Must not be saved !
53 $settings{'IP'} = '';
54 $settings{'ENABLED'} = 'off'; # Every check box must be set to off
55 $settings{'NAME'} = '';
56 my @nosaved=('IP','ENABLED','NAME'); # List here ALL setting2 fields. Mandatory
57
58 $settings{'ACTION'} = ''; # add/edit/remove
59 $settings{'KEY1'} = ''; # point record for ACTION
60
61 #Define each field that can be used to sort columns
62 my $sortstring='^IP|^NAME';
63 my $errormessage = '';
64 my $warnmessage = '';
65
66 &Header::showhttpheaders();
67
68 # Read needed Ipcop netsettings
69 my %netsettings=();
70 $netsettings{'SORT_ALIASES'} = 'NAME'; # default sort
71 &General::readhash($setting, \%netsettings);
72
73 #Get GUI values
74 &Header::getcgihash(\%settings);
75
76 # Load multiline data
77 our @current = ();
78 if (open(FILE, "$datafile")) {
79 @current = <FILE>;
80 close (FILE);
81 }
82
83 #
84 # Check Settings1 first because they are needed before working on @current
85 #
86 # Remove if no Setting1 needed
87 #
88 if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
89
90 #
91 #Validate static Settings1 here
92 #
93
94 unless ($errormessage) { # Everything is ok, save settings
95 #map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
96 #&General::writehash($setting, \%settings); # Save good settings
97 #$settings{'ACTION'} = $Lang::tr{'save'}; # Recreate 'ACTION'
98 #map ($settings{$_}= '',(@nosaved,'KEY1')); # and reinit var to empty
99
100 # Rebuild configuration file if needed
101 &BuildConfiguration;
102 }
103
104 ERROR: # Leave the faulty field untouched
105 } else {
106 #&General::readhash($setting, \%settings); # Get saved settings and reset to good if needed
107 }
108
109 ## Now manipulate the multi-line list with Settings2
110 # Basic actions are:
111 # toggle the check box
112 # add/update a new line
113 # begin editing a line
114 # remove a line
115
116
117 # Toggle enable/disable field. Field is in second position
118 if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
119 #move out new line
120 chomp(@current[$settings{'KEY1'}]);
121 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
122 $temp[1] = $temp[1] eq 'on' ? 'off' : 'on'; # Toggle the field
123 $temp[2] = '' if ( $temp[2] eq '' );
124 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
125 $settings{'KEY1'} = ''; # End edit mode
126
127 &General::log($Lang::tr{'ip alias changed'});
128
129 #Save current
130 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
131 print FILE @current;
132 close(FILE);
133
134 # Rebuild configuration file
135 &BuildConfiguration;
136 }
137
138 if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
139 # Validate inputs
140 if (! &General::validip($settings{'IP'})) {$errormessage = "invalid ip"};
141 $settings{'NAME'} = &Header::cleanhtml($settings{'NAME'});
142
143 # Make sure we haven't duplicated an alias or RED
144 my $spacer='';
145 if ($settings{'IP'} eq $netsettings{'RED_ADDRESS'}) {
146 $errormessage = $Lang::tr{'duplicate ip'} . ' (RED)';
147 $spacer=" & ";
148 }
149 my $idx=0;
150 foreach my $line (@current) {
151 chomp ($line);
152 my @temp = split (/\,/, $line);
153 if ( ($settings{'KEY1'} eq '')||(($settings{'KEY1'} ne '') && ($settings{'KEY1'} != $idx))) { # update
154 if ($temp[0] eq $settings{'IP'}) {
155 $errormessage .= $spacer.$Lang::tr{'duplicate ip'};
156 $spacer=" & ";
157 }
158 if ($temp[2] eq $settings{'NAME'} && $temp[2] ne '') {
159 $errormessage .= $spacer.$Lang::tr{'duplicate name'};
160 $spacer=" & ";
161 }
162 }
163 $idx++;
164 }
165 unless ($errormessage) {
166 if ($settings{'KEY1'} eq '') { #add or edit ?
167 unshift (@current, "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n");
168 &General::log($Lang::tr{'ip alias added'});
169 } else {
170 @current[$settings{'KEY1'}] = "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n";
171 $settings{'KEY1'} = ''; # End edit mode
172 &General::log($Lang::tr{'ip alias changed'});
173 }
174
175 # Write changes to config file.
176 &SortDataFile; # sort newly added/modified entry
177
178 &BuildConfiguration; # then re-build conf which use new data
179
180 ##
181 ## if entering data line is repetitive, choose here to not erase fields between each addition
182 ##
183 map ($settings{$_}='' ,@nosaved); # Clear fields
184 }
185 }
186
187 if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
188 #move out new line
189 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
190 chomp($line);
191 my @temp = split(/\,/, $line);
192
193 ##
194 ## move data fields to Setting2 for edition
195 ##
196 $settings{'IP'}=$temp[0]; # Prepare the screen for editing
197 $settings{'ENABLED'}=$temp[1];
198 $settings{'NAME'}=$temp[2];
199 }
200
201 if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
202 splice (@current,$settings{'KEY1'},1); # Delete line
203 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
204 print FILE @current;
205 close(FILE);
206 $settings{'KEY1'} = ''; # End remove mode
207 &General::log($Lang::tr{'ip alias removed'});
208
209 &BuildConfiguration; # then re-build conf which use new data
210 }
211
212
213
214 ## Check if sorting is asked
215 # If same column clicked, reverse the sort.
216 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
217 my $newsort=$ENV{'QUERY_STRING'};
218 my $actual=$netsettings{'SORT_ALIASES'};
219 #Reverse actual sort ?
220 if ($actual =~ $newsort) {
221 my $Rev='';
222 if ($actual !~ 'Rev') {
223 $Rev='Rev';
224 }
225 $newsort.=$Rev;
226 }
227 $netsettings{'SORT_ALIASES'}=$newsort;
228 &General::writehash($setting, \%netsettings);
229 &SortDataFile;
230 $settings{'ACTION'} = 'SORT'; # Recreate 'ACTION'
231 }
232
233 # Default initial value
234 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
235 $settings{'ENABLED'} ='on';
236 }
237
238 &Header::openpage($Lang::tr{'external aliases configuration'}, 1, '');
239 &Header::openbigbox('100%', 'left', '', $errormessage);
240 my %checked =(); # Checkbox manipulations
241
242 if ($errormessage) {
243 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
244 print "<font class='base'>$errormessage&nbsp;</font>";
245 &Header::closebox();
246 }
247 unless (( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ ) && ($netsettings{'RED_TYPE'} eq 'STATIC'))
248 {
249 &Header::openbox('100%', 'left', $Lang::tr{'capswarning'});
250 print <<END
251 <table width='100%'>
252 <tr>
253 <td width='100%' class='boldbase' align='center'><font color='${Header::colourred}'><b>$Lang::tr{'aliases not active'}</b></font></td>
254 </tr>
255 </table>
256 END
257 ;
258 &Header::closebox();
259 }
260
261 #
262 # Second check box is for editing the list
263 #
264 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq 'on') ? "checked='checked'" : '' ;
265
266 my $buttontext = $Lang::tr{'add'};
267 if ($settings{'KEY1'} ne '') {
268 $buttontext = $Lang::tr{'update'};
269 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing alias'});
270 } else {
271 &Header::openbox('100%', 'left', $Lang::tr{'add new alias'});
272 }
273
274 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
275 print <<END
276 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
277 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
278 <table width='100%'>
279 <tr>
280 <td class='base'><font color='${Header::colourred}'>$Lang::tr{'name'}:&nbsp;<img src='/blob.gif' alt='*' /></font></td>
281 <td><input type='text' name='NAME' value='$settings{'NAME'}' size='32' /></td>
282 <td class='base' align='right'><font color='${Header::colourred}'>$Lang::tr{'alias ip'}:&nbsp;</font></td>
283 <td><input type='text' name='IP' value='$settings{'IP'}' size='16' /></td>
284 <td class='base' align='right'>$Lang::tr{'enabled'}&nbsp;</td>
285 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
286 </tr>
287 </table>
288 <hr />
289 <table width='100%'>
290 <tr>
291 <td class='base' width='55%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
292 <td width='40%' align='center'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
293 <td width='5%' align='right'>
294 </td>
295 </tr>
296 </table>
297 </form>
298 END
299 ;
300 &Header::closebox();
301
302 # Add visual indicators to column headings to show sort order - EO
303 my $sortarrow1 = '';
304 my $sortarrow2 = '';
305
306 if ($netsettings{'SORT_ALIASES'} eq 'NAMERev') {
307 $sortarrow1 = $Header::sortdn;
308 } elsif ($netsettings{'SORT_ALIASES'} eq 'NAME') {
309 $sortarrow1 = $Header::sortup;
310 } elsif ($netsettings{'SORT_ALIASES'} eq 'IPRev') {
311 $sortarrow2 = $Header::sortdn;
312 } else {
313 $sortarrow2 = $Header::sortup;
314 }
315
316 #
317 # Third box shows the list, in columns
318 #
319 # Columns headers may content a link. In this case it must be named in $sortstring
320 #
321 &Header::openbox('100%', 'left', $Lang::tr{'current aliases'});
322 print <<END
323 <table width='100%'>
324 <tr>
325 <td width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</td>
326 <td width='45%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</td>
327 <td width='5%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>
328 </tr>
329 END
330 ;
331
332 #
333 # Print each line of @current list
334 #
335 # each data line is splitted into @temp.
336 #
337
338 my $key = 0;
339 foreach my $line (@current) {
340 chomp($line);
341 my @temp = split(/\,/,$line);
342
343 #Choose icon for checkbox
344 my $gif = '';
345 my $gdesc = '';
346 if ($temp[1] eq "on") {
347 $gif = 'on.gif';
348 $gdesc = $Lang::tr{'click to disable'};
349 } else {
350 $gif = 'off.gif';
351 $gdesc = $Lang::tr{'click to enable'};
352 }
353
354 #Colorize each line
355 if ($settings{'KEY1'} eq $key) {
356 print "<tr bgcolor='${Header::colouryellow}'>";
357 } elsif ($key % 2) {
358 print "<tr bgcolor='${Header::table2colour}'>";
359 } else {
360 print "<tr bgcolor='${Header::table1colour}'>";
361 }
362
363 print <<END
364 <td align='center'>$temp[2]</td>
365 <td align='center'>$temp[0]</td>
366
367 <td align='center'>
368 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
369 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
370 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
371 <input type='hidden' name='KEY1' value='$key' />
372 </form>
373 </td>
374
375 <td align='center'>
376 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
377 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
378 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
379 <input type='hidden' name='KEY1' value='$key' />
380 </form>
381 </td>
382
383 <td align='center'>
384 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
385 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
386 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
387 <input type='hidden' name='KEY1' value='$key' />
388 </form>
389 </td>
390 </tr>
391 END
392 ;
393 $key++;
394 }
395 print "</table>";
396
397 # If table contains entries, print 'Key to action icons'
398 if ($key) {
399 print <<END
400 <table>
401 <tr>
402 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
403 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
404 <td class='base'>$Lang::tr{'click to disable'}</td>
405 <td>&nbsp;&nbsp;</td>
406 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
407 <td class='base'>$Lang::tr{'click to enable'}</td>
408 <td>&nbsp;&nbsp;</td>
409 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
410 <td class='base'>$Lang::tr{'edit'}</td>
411 <td>&nbsp;&nbsp;</td>
412 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
413 <td class='base'>$Lang::tr{'remove'}</td>
414 </tr>
415 </table>
416 END
417 ;
418 }
419
420 &Header::closebox();
421 &Header::closebigbox();
422 &Header::closepage();
423
424 ## Ouf it's the end !
425
426
427
428 # Sort the "current" array according to choices
429 sub SortDataFile
430 {
431 our %entries = ();
432
433 # Sort pair of record received in $a $b special vars.
434 # When IP is specified use numeric sort else alpha.
435 # If sortname ends with 'Rev', do reverse sort.
436 #
437 sub fixedleasesort {
438 my $qs=''; # The sort field specified minus 'Rev'
439 if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
440 $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
441 if ($qs eq 'IP') {
442 my @a = split(/\./,$entries{$a}->{$qs});
443 my @b = split(/\./,$entries{$b}->{$qs});
444 ($b[0]<=>$a[0]) ||
445 ($b[1]<=>$a[1]) ||
446 ($b[2]<=>$a[2]) ||
447 ($b[3]<=>$a[3]);
448 } else {
449 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
450 }
451 } else { #not reverse
452 $qs=$netsettings{'SORT_ALIASES'};
453 if ($qs eq 'IP') {
454 my @a = split(/\./,$entries{$a}->{$qs});
455 my @b = split(/\./,$entries{$b}->{$qs});
456 ($a[0]<=>$b[0]) ||
457 ($a[1]<=>$b[1]) ||
458 ($a[2]<=>$b[2]) ||
459 ($a[3]<=>$b[3]);
460 } else {
461 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
462 }
463 }
464 }
465
466 #Use an associative array (%entries)
467 my $key = 0;
468 foreach my $line (@current) {
469 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
470 my @temp = split (',',$line);
471
472 # Build a pair 'Field Name',value for each of the data dataline.
473 # Each SORTABLE field must have is pair.
474 # Other data fields (non sortable) can be grouped in one
475
476 # Exemple
477 # F1,F2,F3,F4,F5 only F1 F2 for sorting
478 # my @record = ('KEY',$key++,
479 # 'F1',$temp[0],
480 # 'F2',$temp[1],
481 # 'DATA',join(',',@temp[2..4]) ); #group remainning values, with separator (,)
482
483 # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
484
485
486 my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);
487 my $record = {}; # create a reference to empty hash
488 %{$record} = @record; # populate that hash with @record
489 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
490 }
491
492 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
493
494 # Each field value is printed , with the newline ! Don't forget separator and order of them.
495 foreach my $entry (sort fixedleasesort keys %entries) {
496 print FILE "$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";
497 }
498
499 close(FILE);
500 # Reload sorted @current
501 open (FILE, "$datafile");
502 @current = <FILE>;
503 close (FILE);
504 }
505
506 #
507 # Build the configuration file for application aliases
508 #
509 sub BuildConfiguration {
510 # Restart service associated with this
511 system '/usr/local/bin/setaliases';
512 }