]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/aliases.cgi
Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into next
[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 <br>
289 <hr />
290 <table width='100%'>
291 <tr>
292 <td class='base' width='55%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
293 <td width='40%' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
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%' class='tbl'>
324 <tr>
325 <th width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</th>
326 <th width='45%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</th>
327 <th width='5%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
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 my $col="";
340 foreach my $line (@current) {
341 chomp($line);
342 my @temp = split(/\,/,$line);
343
344 #Choose icon for checkbox
345 my $gif = '';
346 my $gdesc = '';
347 if ($temp[1] eq "on") {
348 $gif = 'on.gif';
349 $gdesc = $Lang::tr{'click to disable'};
350 } else {
351 $gif = 'off.gif';
352 $gdesc = $Lang::tr{'click to enable'};
353 }
354
355 #Colorize each line
356 if ($settings{'KEY1'} eq $key) {
357 print "<tr>";
358 $col="bgcolor='${Header::colouryellow}'";
359 } elsif ($key % 2) {
360 print "<tr>";
361 $col="bgcolor='${Header::table2colour}'";
362 } else {
363 print "<tr>";
364 $col="bgcolor='${Header::table1colour}'";
365 }
366
367 print <<END
368 <td align='center' $col>$temp[2]</td>
369 <td align='center' $col>$temp[0]</td>
370
371 <td align='center' $col>
372 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
373 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
374 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
375 <input type='hidden' name='KEY1' value='$key' />
376 </form>
377 </td>
378
379 <td align='center' $col>
380 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
381 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
382 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
383 <input type='hidden' name='KEY1' value='$key' />
384 </form>
385 </td>
386
387 <td align='center' $col>
388 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
389 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
390 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
391 <input type='hidden' name='KEY1' value='$key' />
392 </form>
393 </td>
394 </tr>
395 END
396 ;
397 $key++;
398 }
399 print "</table>";
400
401 # If table contains entries, print 'Key to action icons'
402 if ($key) {
403 print <<END
404 <table>
405 <tr>
406 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
407 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
408 <td class='base'>$Lang::tr{'click to disable'}</td>
409 <td>&nbsp;&nbsp;</td>
410 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
411 <td class='base'>$Lang::tr{'click to enable'}</td>
412 <td>&nbsp;&nbsp;</td>
413 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
414 <td class='base'>$Lang::tr{'edit'}</td>
415 <td>&nbsp;&nbsp;</td>
416 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
417 <td class='base'>$Lang::tr{'remove'}</td>
418 </tr>
419 </table>
420 END
421 ;
422 }
423
424 &Header::closebox();
425 &Header::closebigbox();
426 &Header::closepage();
427
428 ## Ouf it's the end !
429
430
431
432 # Sort the "current" array according to choices
433 sub SortDataFile
434 {
435 our %entries = ();
436
437 # Sort pair of record received in $a $b special vars.
438 # When IP is specified use numeric sort else alpha.
439 # If sortname ends with 'Rev', do reverse sort.
440 #
441 sub fixedleasesort {
442 my $qs=''; # The sort field specified minus 'Rev'
443 if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
444 $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
445 if ($qs eq 'IP') {
446 my @a = split(/\./,$entries{$a}->{$qs});
447 my @b = split(/\./,$entries{$b}->{$qs});
448 ($b[0]<=>$a[0]) ||
449 ($b[1]<=>$a[1]) ||
450 ($b[2]<=>$a[2]) ||
451 ($b[3]<=>$a[3]);
452 } else {
453 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
454 }
455 } else { #not reverse
456 $qs=$netsettings{'SORT_ALIASES'};
457 if ($qs eq 'IP') {
458 my @a = split(/\./,$entries{$a}->{$qs});
459 my @b = split(/\./,$entries{$b}->{$qs});
460 ($a[0]<=>$b[0]) ||
461 ($a[1]<=>$b[1]) ||
462 ($a[2]<=>$b[2]) ||
463 ($a[3]<=>$b[3]);
464 } else {
465 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
466 }
467 }
468 }
469
470 #Use an associative array (%entries)
471 my $key = 0;
472 foreach my $line (@current) {
473 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
474 my @temp = split (',',$line);
475
476 # Build a pair 'Field Name',value for each of the data dataline.
477 # Each SORTABLE field must have is pair.
478 # Other data fields (non sortable) can be grouped in one
479
480 # Exemple
481 # F1,F2,F3,F4,F5 only F1 F2 for sorting
482 # my @record = ('KEY',$key++,
483 # 'F1',$temp[0],
484 # 'F2',$temp[1],
485 # 'DATA',join(',',@temp[2..4]) ); #group remainning values, with separator (,)
486
487 # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
488
489
490 my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);
491 my $record = {}; # create a reference to empty hash
492 %{$record} = @record; # populate that hash with @record
493 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
494 }
495
496 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
497
498 # Each field value is printed , with the newline ! Don't forget separator and order of them.
499 foreach my $entry (sort fixedleasesort keys %entries) {
500 print FILE "$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";
501 }
502
503 close(FILE);
504 # Reload sorted @current
505 open (FILE, "$datafile");
506 @current = <FILE>;
507 close (FILE);
508 }
509
510 #
511 # Build the configuration file for application aliases
512 #
513 sub BuildConfiguration {
514 # Restart service associated with this
515 system '/usr/local/bin/setaliases';
516 }