]> 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 "$errormessage&nbsp;";
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 style='width:100%;'>
252 <tr>
253 <td class='boldbase' style='color:${Header::colourred};'><b>$Lang::tr{'aliases not active'}</b></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 style='width:100%;'>
279 <tr>
280 <td class='base' style='color:${Header::colourred};'>$Lang::tr{'name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
281 <td><input type='text' name='NAME' value='$settings{'NAME'}' size='32' /></td>
282 <td class='base' style='text-align:right; color:${Header::colourred};'>$Lang::tr{'alias ip'}:&nbsp;</td>
283 <td><input type='text' name='IP' value='$settings{'IP'}' size='16' /></td>
284 <td class='base' style='text-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 style='width:100%;'>
291 <tr>
292 <td><img src='/blob.gif' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
293 <td style='text-align:right;'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
294 </tr>
295 </table>
296 </form>
297 END
298 ;
299 &Header::closebox();
300
301 # Add visual indicators to column headings to show sort order - EO
302 my $sortarrow1 = '';
303 my $sortarrow2 = '';
304
305 if ($netsettings{'SORT_ALIASES'} eq 'NAMERev') {
306 $sortarrow1 = $Header::sortdn;
307 } elsif ($netsettings{'SORT_ALIASES'} eq 'NAME') {
308 $sortarrow1 = $Header::sortup;
309 } elsif ($netsettings{'SORT_ALIASES'} eq 'IPRev') {
310 $sortarrow2 = $Header::sortdn;
311 } else {
312 $sortarrow2 = $Header::sortup;
313 }
314
315 #
316 # Third box shows the list, in columns
317 #
318 # Columns headers may content a link. In this case it must be named in $sortstring
319 #
320 &Header::openbox('100%', 'left', $Lang::tr{'current aliases'});
321 print <<END
322 <table class='tbl' style='width:100%;'>
323 <tr>
324 <th style='width:55%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</th>
325 <th style='width:45%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</th>
326 <th colspan='3' class='boldbase' style='width:5%; text-align:center;'><b>$Lang::tr{'action'}</b></th>
327 </tr>
328 END
329 ;
330
331 #
332 # Print each line of @current list
333 #
334 # each data line is splitted into @temp.
335 #
336
337 my $key = 0;
338 my $col="";
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 $col="background-color:${Header::colouryellow};";
357 } elsif ($key % 2) {
358 $col="background-color:${Header::table2colour};";
359 } else {
360 $col="background-color:${Header::table1colour};";
361 }
362 print "<tr style='$col'>";
363
364 print <<END
365 <td style='text-align:center; $col'>$temp[2]</td>
366 <td style='text-align:center; $col'>$temp[0]</td>
367
368 <td style='text-align:center; $col'>
369 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
370 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
371 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
372 <input type='hidden' name='KEY1' value='$key' />
373 </form>
374 </td>
375
376 <td style='text-align:center; $col'>
377 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
378 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
379 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
380 <input type='hidden' name='KEY1' value='$key' />
381 </form>
382 </td>
383
384 <td style='text-align:center; $col'>
385 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
386 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
387 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
388 <input type='hidden' name='KEY1' value='$key' />
389 </form>
390 </td>
391 </tr>
392 END
393 ;
394 $key++;
395 }
396 print "</table>";
397
398 # If table contains entries, print 'Key to action icons'
399 if ($key) {
400 print <<END
401 <table>
402 <tr>
403 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
404 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
405 <td class='base'>$Lang::tr{'click to disable'}</td>
406 <td>&nbsp;&nbsp;</td>
407 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
408 <td class='base'>$Lang::tr{'click to enable'}</td>
409 <td>&nbsp;&nbsp;</td>
410 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
411 <td class='base'>$Lang::tr{'edit'}</td>
412 <td>&nbsp;&nbsp;</td>
413 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
414 <td class='base'>$Lang::tr{'remove'}</td>
415 </tr>
416 </table>
417 END
418 ;
419 }
420
421 &Header::closebox();
422 &Header::closebigbox();
423 &Header::closepage();
424
425 ## Ouf it's the end !
426
427
428
429 # Sort the "current" array according to choices
430 sub SortDataFile
431 {
432 our %entries = ();
433
434 # Sort pair of record received in $a $b special vars.
435 # When IP is specified use numeric sort else alpha.
436 # If sortname ends with 'Rev', do reverse sort.
437 #
438 sub fixedleasesort {
439 my $qs=''; # The sort field specified minus 'Rev'
440 if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
441 $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
442 if ($qs eq 'IP') {
443 my @a = split(/\./,$entries{$a}->{$qs});
444 my @b = split(/\./,$entries{$b}->{$qs});
445 ($b[0]<=>$a[0]) ||
446 ($b[1]<=>$a[1]) ||
447 ($b[2]<=>$a[2]) ||
448 ($b[3]<=>$a[3]);
449 } else {
450 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
451 }
452 } else { #not reverse
453 $qs=$netsettings{'SORT_ALIASES'};
454 if ($qs eq 'IP') {
455 my @a = split(/\./,$entries{$a}->{$qs});
456 my @b = split(/\./,$entries{$b}->{$qs});
457 ($a[0]<=>$b[0]) ||
458 ($a[1]<=>$b[1]) ||
459 ($a[2]<=>$b[2]) ||
460 ($a[3]<=>$b[3]);
461 } else {
462 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
463 }
464 }
465 }
466
467 #Use an associative array (%entries)
468 my $key = 0;
469 foreach my $line (@current) {
470 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
471 my @temp = split (',',$line);
472
473 # Build a pair 'Field Name',value for each of the data dataline.
474 # Each SORTABLE field must have is pair.
475 # Other data fields (non sortable) can be grouped in one
476
477 # Exemple
478 # F1,F2,F3,F4,F5 only F1 F2 for sorting
479 # my @record = ('KEY',$key++,
480 # 'F1',$temp[0],
481 # 'F2',$temp[1],
482 # 'DATA',join(',',@temp[2..4]) ); #group remainning values, with separator (,)
483
484 # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
485
486
487 my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);
488 my $record = {}; # create a reference to empty hash
489 %{$record} = @record; # populate that hash with @record
490 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
491 }
492
493 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
494
495 # Each field value is printed , with the newline ! Don't forget separator and order of them.
496 foreach my $entry (sort fixedleasesort keys %entries) {
497 print FILE "$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";
498 }
499
500 close(FILE);
501 # Reload sorted @current
502 open (FILE, "$datafile");
503 @current = <FILE>;
504 close (FILE);
505 }
506
507 #
508 # Build the configuration file for application aliases
509 #
510 sub BuildConfiguration {
511 # Restart service associated with this
512 system '/usr/local/bin/setaliases';
513 }
514