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