]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - html/cgi-bin/aliases.cgi
aliases.cgi: Mark name field as mandatory.
[people/teissler/ipfire-2.x.git] / html / cgi-bin / aliases.cgi
index 040eb2999a2123e36239f87d5154a396ca606be9..7c3ba91ae02fc3a522ba86aa0aac1a0a993f4a15 100644 (file)
-#!/usr/bin/perl\r
-#\r
-# IPCop CGI's - aliases.cgi\r
-#\r
-# This code is distributed under the terms of the GPL\r
-#\r
-# (c) Steve Bootes 2002/04/13 - Manage IP Aliases\r
-#\r
-# $Id: aliases.cgi,v 1.5.2.14 2006/01/13 20:14:48 eoberlander Exp $\r
-\r
-\r
-# to fully troubleshot your code, uncomment diagnostics, Carp and cluck lines\r
-#use diagnostics; # need to add the file /usr/lib/perl5/5.8.x/pods/perldiag.pod before to work\r
-# next look at /var/log/httpd/error_log , http://www.perl.com/pub/a/2002/05/07/mod_perl.html may help\r
-use warnings;\r
-use strict;\r
-#use Carp ();\r
-#local $SIG{__WARN__} = \&Carp::cluck;\r
-\r
-require 'CONFIG_ROOT/general-functions.pl';    # replace CONFIG_ROOT with /var/ipcop in case of manual install\r
-require "${General::swroot}/lang.pl";\r
-require "${General::swroot}/header.pl";\r
-\r
-#workaround to suppress a warning when a variable is used only once\r
-my @dummy = ( ${Header::colouryellow} );\r
-   @dummy = ( ${Header::table1colour} );\r
-   @dummy = ( ${Header::table2colour} );\r
-undef (@dummy);\r
-\r
-# Files used\r
-my $setting = "${General::swroot}/ethernet/settings";\r
-our $datafile = "${General::swroot}/ethernet/aliases";\r
-\r
-\r
-our %settings=();\r
-#Settings1\r
-\r
-#Settings2 for editing the multi-line list\r
-#Must not be saved !\r
-$settings{'IP'} = '';\r
-$settings{'ENABLED'} = 'off';          # Every check box must be set to off\r
-$settings{'NAME'} = '';\r
-my @nosaved=('IP','ENABLED','NAME');   # List here ALL setting2 fields. Mandatory\r
-    \r
-$settings{'ACTION'} = '';              # add/edit/remove\r
-$settings{'KEY1'} = '';                        # point record for ACTION\r
-\r
-#Define each field that can be used to sort columns\r
-my $sortstring='^IP|^NAME';\r
-my $errormessage = '';\r
-my $warnmessage = '';\r
-\r
-&Header::showhttpheaders();\r
-\r
-# Read needed Ipcop netsettings\r
-my %netsettings=();\r
-$netsettings{'SORT_ALIASES'} = 'NAME';         # default sort\r
-&General::readhash($setting, \%netsettings);\r
-\r
-#Get GUI values\r
-&Header::getcgihash(\%settings);\r
-\r
-# Load multiline data\r
-our @current = ();\r
-if (open(FILE, "$datafile")) {\r
-    @current = <FILE>;\r
-    close (FILE);\r
-}\r
-\r
-#\r
-# Check Settings1 first because they are needed before working on @current\r
-#\r
-# Remove if no Setting1 needed\r
-#\r
-if ($settings{'ACTION'} eq $Lang::tr{'save'}) {\r
-    \r
-    #\r
-    #Validate static Settings1 here\r
-    #\r
-    \r
-    unless ($errormessage) {                                   # Everything is ok, save settings\r
-       #map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved \r
-       #&General::writehash($setting, \%settings);             # Save good settings\r
-       #$settings{'ACTION'} = $Lang::tr{'save'};               # Recreate  'ACTION'\r
-       #map ($settings{$_}= '',(@nosaved,'KEY1'));             # and reinit var to empty\r
-       \r
-       # Rebuild configuration file if needed\r
-       &BuildConfiguration;\r
-    }\r
-\r
-    ERROR:                                             # Leave the faulty field untouched\r
-} else {\r
-    #&General::readhash($setting, \%settings);         # Get saved settings and reset to good if needed\r
-}\r
-\r
-## Now manipulate the multi-line list with Settings2\r
-# Basic actions are:\r
-#      toggle the check box\r
-#      add/update a new line\r
-#      begin editing a line\r
-#      remove a line\r
-\r
-\r
-# Toggle enable/disable field.  Field is in second position\r
-if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {\r
-    #move out new line\r
-    chomp(@current[$settings{'KEY1'}]);\r
-    my @temp = split(/\,/,@current[$settings{'KEY1'}]);\r
-    $temp[1] = $temp[1] eq 'on' ? '' : 'on';           # Toggle the field\r
-    @current[$settings{'KEY1'}] = join (',',@temp)."\n";\r
-    $settings{'KEY1'} = '';                            # End edit mode\r
-    \r
-    &General::log($Lang::tr{'ip alias changed'});\r
-    \r
-    #Save current\r
-    open(FILE, ">$datafile") or die 'Unable to open aliases file.';\r
-    print FILE @current;\r
-    close(FILE);\r
-       \r
-    # Rebuild configuration file\r
-    &BuildConfiguration;\r
-}\r
-\r
-if ($settings{'ACTION'} eq $Lang::tr{'add'}) {\r
-    # Validate inputs\r
-    if (! &General::validip($settings{'IP'})) {$errormessage = "invalid ip"};\r
-    $settings{'NAME'} = &Header::cleanhtml($settings{'NAME'});\r
-\r
-    # Make sure we haven't duplicated an alias or RED\r
-    my $spacer='';\r
-    if ($settings{'IP'} eq $netsettings{'RED_ADDRESS'}) {\r
-       $errormessage = $Lang::tr{'duplicate ip'} . ' (RED)';\r
-        $spacer=" & ";\r
-    }\r
-    my $idx=0;\r
-    foreach my $line (@current) {\r
-        chomp ($line);\r
-        my @temp = split (/\,/, $line);\r
-        if ( ($settings{'KEY1'} eq '')||(($settings{'KEY1'} ne '') && ($settings{'KEY1'} != $idx))) { # update\r
-           if ($temp[0] eq $settings{'IP'}) {\r
-               $errormessage .= $spacer.$Lang::tr{'duplicate ip'};\r
-               $spacer=" & ";\r
-           }\r
-           if ($temp[2] eq $settings{'NAME'} && $temp[2] ne '') {\r
-               $errormessage .= $spacer.$Lang::tr{'duplicate name'};\r
-               $spacer=" & ";\r
-               }\r
-       }\r
-       $idx++;\r
-    }\r
-    unless ($errormessage) {\r
-       if ($settings{'KEY1'} eq '') { #add or edit ?\r
-           unshift (@current, "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n");\r
-           &General::log($Lang::tr{'ip alias added'});\r
-       } else {\r
-           @current[$settings{'KEY1'}] = "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n";\r
-           $settings{'KEY1'} = '';       # End edit mode\r
-           &General::log($Lang::tr{'ip alias changed'});\r
-       }\r
-\r
-       # Write changes to config file.\r
-       &SortDataFile;                          # sort newly added/modified entry\r
-\r
-       &BuildConfiguration;                    # then re-build conf which use new data\r
-       \r
-##\r
-## if entering data line is repetitive, choose here to not erase fields between each addition\r
-##\r
-       map ($settings{$_}='' ,@nosaved);       # Clear fields\r
-    }\r
-}\r
-\r
-if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {\r
-    #move out new line\r
-    my $line = @current[$settings{'KEY1'}];    # KEY1 is the index in current\r
-    chomp($line);\r
-    my @temp = split(/\,/, $line);\r
-\r
-##\r
-## move data fields to Setting2 for edition\r
-##\r
-    $settings{'IP'}=$temp[0];                  # Prepare the screen for editing\r
-    $settings{'ENABLED'}=$temp[1];\r
-    $settings{'NAME'}=$temp[2];\r
-}\r
-\r
-if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {\r
-    splice (@current,$settings{'KEY1'},1);             # Delete line \r
-    open(FILE, ">$datafile") or die 'Unable to open aliases file.';\r
-    print FILE @current;\r
-    close(FILE);\r
-    $settings{'KEY1'} = '';                            # End remove mode\r
-    &General::log($Lang::tr{'ip alias removed'});\r
-\r
-    &BuildConfiguration;                               # then re-build conf which use new data\r
-}\r
-\r
-\r
-\r
-##  Check if sorting is asked\r
-# If same column clicked, reverse the sort.\r
-if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {\r
-    my $newsort=$ENV{'QUERY_STRING'};\r
-    my $actual=$netsettings{'SORT_ALIASES'};\r
-    #Reverse actual sort ?\r
-    if ($actual =~ $newsort) {\r
-       my $Rev='';\r
-       if ($actual !~ 'Rev') {\r
-           $Rev='Rev';\r
-       }\r
-       $newsort.=$Rev;\r
-    }\r
-    $netsettings{'SORT_ALIASES'}=$newsort;\r
-    &General::writehash($setting, \%netsettings);\r
-    &SortDataFile;\r
-    $settings{'ACTION'} = 'SORT';                      # Recreate  'ACTION'\r
-}\r
-\r
-# Default initial value\r
-if ($settings{'ACTION'} eq '' ) { # First launch from GUI\r
-    $settings{'ENABLED'} ='on';\r
-}\r
-    \r
-&Header::openpage($Lang::tr{'external aliases configuration'}, 1, '');\r
-&Header::openbigbox('100%', 'left', '', $errormessage);\r
-my %checked =();     # Checkbox manipulations\r
-\r
-if ($errormessage) {\r
-    &Header::openbox('100%', 'left', $Lang::tr{'error messages'});\r
-    print "<font class='base'>$errormessage&nbsp;</font>";\r
-    &Header::closebox();\r
-}\r
-unless (( $netsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/ ) && ($netsettings{'RED_TYPE'} eq 'STATIC'))\r
-{\r
-    &Header::openbox('100%', 'left', $Lang::tr{'capswarning'});\r
-    print <<END\r
-    <table width='100%'>\r
-    <tr>\r
-    <td width='100%' class='boldbase' align='center'><font color='${Header::colourred}'><b>$Lang::tr{'aliases not active'}</b></font></td>\r
-    </tr>\r
-    </table>\r
-END\r
-;\r
-    &Header::closebox();\r
-}\r
-                                                                       \r
-#\r
-# Second check box is for editing the list\r
-#\r
-$checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq '') ? '' :  "checked='checked'";\r
-\r
-my $buttontext = $Lang::tr{'add'};\r
-if ($settings{'KEY1'} ne '') {\r
-    $buttontext = $Lang::tr{'update'};\r
-    &Header::openbox('100%', 'left', $Lang::tr{'edit an existing alias'});\r
-} else {\r
-    &Header::openbox('100%', 'left', $Lang::tr{'add new alias'});\r
-}\r
-\r
-#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'\r
-print <<END\r
-<form method='post' action='$ENV{'SCRIPT_NAME'}'>\r
-<input type='hidden' name='KEY1' value='$settings{'KEY1'}' />\r
-<table width='100%'>\r
-<tr>\r
-<td class='base'><font color='${Header::colourred}'>$Lang::tr{'name'}:&nbsp;<img src='/blob.gif' alt='*' /></font></td>\r
-<td><input type='text' name='NAME' value='$settings{'NAME'}' size='32' /></td>\r
-<td class='base' align='right'><font color='${Header::colourred}'>$Lang::tr{'alias ip'}:&nbsp;</font></td>\r
-<td><input type='text' name='IP' value='$settings{'IP'}' size='16' /></td>\r
-<td class='base' align='right'>$Lang::tr{'enabled'}&nbsp;</td>\r
-<td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>\r
-</tr>\r
-</table>\r
-<hr />\r
-<table width='100%'>\r
-<tr>\r
-    <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>\r
-    <td width='50%' align='center'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>\r
-</tr>\r
-</table>\r
-</form>\r
-END\r
-;\r
-&Header::closebox();\r
-\r
-#\r
-# Third box shows the list, in columns\r
-#\r
-# Columns headers may content a link. In this case it must be named in $sortstring\r
-#\r
-&Header::openbox('100%', 'left', $Lang::tr{'current aliases'});\r
-print <<END\r
-<table width='100%'>\r
-<tr>\r
-    <td width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a></td>\r
-    <td width='40%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a></td>\r
-    <td width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>\r
-</tr>\r
-END\r
-;\r
-\r
-#\r
-# Print each line of @current list\r
-#\r
-# each data line is splitted into @temp.\r
-#\r
-\r
-my $key = 0;\r
-foreach my $line (@current) {\r
-    chomp($line);\r
-    my @temp = split(/\,/,$line);\r
-\r
-    #Choose icon for checkbox\r
-    my $gif = '';\r
-    my $gdesc = '';\r
-    if ($temp[1] eq "on") {\r
-       $gif = 'on.gif';\r
-       $gdesc = $Lang::tr{'click to disable'};\r
-    } else {\r
-       $gif = 'off.gif';\r
-       $gdesc = $Lang::tr{'click to enable'}; \r
-    }\r
-\r
-    #Colorize each line\r
-    if ($settings{'KEY1'} eq $key) {\r
-       print "<tr bgcolor='${Header::colouryellow}'>";\r
-    } elsif ($key % 2) {\r
-       print "<tr bgcolor='${Header::table2colour}'>";\r
-    } else {\r
-       print "<tr bgcolor='${Header::table1colour}'>"; \r
-    }\r
-\r
-    print <<END\r
-<td align='center'>$temp[2]</td>\r
-<td align='center'>$temp[0]</td>\r
-\r
-<td align='center'>\r
-<form method='post' action='$ENV{'SCRIPT_NAME'}'>\r
-<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />\r
-<input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />\r
-<input type='hidden' name='KEY1' value='$key' />\r
-</form>\r
-</td>\r
-\r
-<td align='center'>\r
-<form method='post' action='$ENV{'SCRIPT_NAME'}'>\r
-<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />\r
-<input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />\r
-<input type='hidden' name='KEY1' value='$key' />\r
-</form>\r
-</td>\r
-\r
-<td align='center'>\r
-<form method='post' action='$ENV{'SCRIPT_NAME'}'>\r
-<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />\r
-<input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />\r
-<input type='hidden' name='KEY1' value='$key' />\r
-</form>\r
-</td>\r
-</tr>\r
-END\r
-;\r
-    $key++;\r
-}\r
-print "</table>";\r
-\r
-# If table contains entries, print 'Key to action icons'\r
-if ($key) {\r
-print <<END\r
-<table>\r
-<tr>\r
-    <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>\r
-    <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>\r
-    <td class='base'>$Lang::tr{'click to disable'}</td>\r
-    <td>&nbsp;&nbsp;</td>\r
-    <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>\r
-    <td class='base'>$Lang::tr{'click to enable'}</td>\r
-    <td>&nbsp;&nbsp;</td>\r
-    <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>\r
-    <td class='base'>$Lang::tr{'edit'}</td>\r
-    <td>&nbsp;&nbsp;</td>\r
-    <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>\r
-    <td class='base'>$Lang::tr{'remove'}</td>\r
-</tr>\r
-</table>\r
-END\r
-;\r
-}\r
-\r
-&Header::closebox();\r
-&Header::closebigbox();\r
-&Header::closepage();\r
-\r
-## Ouf it's the end !\r
-\r
-\r
-\r
-# Sort the "current" array according to choices\r
-sub SortDataFile\r
-{\r
-    our %entries = ();\r
-    \r
-    # Sort pair of record received in $a $b special vars.\r
-    # When IP is specified use numeric sort else alpha.\r
-    # If sortname ends with 'Rev', do reverse sort.\r
-    #\r
-    sub fixedleasesort {\r
-       my $qs='';             # The sort field specified minus 'Rev'\r
-       if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {\r
-           $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);\r
-           if ($qs eq 'IP') {\r
-               my @a = split(/\./,$entries{$a}->{$qs});\r
-               my @b = split(/\./,$entries{$b}->{$qs});\r
-               ($b[0]<=>$a[0]) ||\r
-               ($b[1]<=>$a[1]) ||\r
-               ($b[2]<=>$a[2]) ||\r
-               ($b[3]<=>$a[3]);\r
-           } else {\r
-               $entries{$b}->{$qs} cmp $entries{$a}->{$qs};\r
-           }\r
-       } else { #not reverse\r
-           $qs=$netsettings{'SORT_ALIASES'};\r
-           if ($qs eq 'IP') {\r
-               my @a = split(/\./,$entries{$a}->{$qs});\r
-               my @b = split(/\./,$entries{$b}->{$qs});\r
-               ($a[0]<=>$b[0]) ||\r
-               ($a[1]<=>$b[1]) ||\r
-               ($a[2]<=>$b[2]) ||\r
-               ($a[3]<=>$b[3]);\r
-           } else {\r
-               $entries{$a}->{$qs} cmp $entries{$b}->{$qs};\r
-           }\r
-       }\r
-    }\r
-\r
-    #Use an associative array (%entries)\r
-    my $key = 0;\r
-    foreach my $line (@current) {\r
-       chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)\r
-       my @temp = split (',',$line);\r
-       \r
-       # Build a pair 'Field Name',value for each of the data dataline.\r
-       # Each SORTABLE field must have is pair.\r
-       # Other data fields (non sortable) can be grouped in one\r
-       \r
-       # Exemple\r
-       # F1,F2,F3,F4,F5       only F1 F2 for sorting\r
-       # my @record = ('KEY',$key++,\r
-       #               'F1',$temp[0],\r
-       #               'F2',$temp[1],\r
-       #               'DATA',join(',',@temp[2..4])    );  #group remainning values, with separator (,)\r
-       \r
-       # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.\r
-       \r
-       \r
-       my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);\r
-       my $record = {};                                # create a reference to empty hash\r
-       %{$record} = @record;                           # populate that hash with @record\r
-       $entries{$record->{KEY}} = $record;             # add this to a hash of hashes\r
-    }\r
-    \r
-    open(FILE, ">$datafile") or die 'Unable to open aliases file.';\r
-\r
-    # Each field value is printed , with the newline ! Don't forget separator and order of them.\r
-    foreach my $entry (sort fixedleasesort keys %entries) {\r
-       print FILE "$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";\r
-    }\r
-\r
-    close(FILE);\r
-    # Reload sorted  @current\r
-    open (FILE, "$datafile");\r
-    @current = <FILE>;\r
-    close (FILE);\r
-}\r
-\r
-#                                                  \r
-# Build the configuration file for application aliases\r
-#\r
-sub BuildConfiguration {\r
-    # Restart service associated with this\r
-    system '/usr/local/bin/setaliases';\r
-}\r
+#!/usr/bin/perl
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+#
+# this cgi is base on IPCop CGI - aliases.cgi
+#
+
+# to fully troubleshot your code, uncomment diagnostics, Carp and cluck lines
+#use diagnostics; # need to add the file /usr/lib/perl5/5.8.x/pods/perldiag.pod before to work
+# next look at /var/log/httpd/error_log , http://www.perl.com/pub/a/2002/05/07/mod_perl.html may help
+#use warnings;
+use strict;
+#use Carp ();
+#local $SIG{__WARN__} = \&Carp::cluck;
+
+require '/var/ipfire/general-functions.pl';    # replace /var/ipcop with /var/ipcop in case of manual install
+require "${General::swroot}/lang.pl";
+require "${General::swroot}/header.pl";
+
+my $configfwdfw                = "${General::swroot}/firewall/config";
+my $configinput                = "${General::swroot}/firewall/input";
+my $configoutgoing     = "${General::swroot}/firewall/outgoing";
+my %input=();
+my %forward=();
+my %output=();
+
+#workaround to suppress a warning when a variable is used only once
+my @dummy = ( ${Header::colouryellow} );
+   @dummy = ( ${Header::table1colour} );
+   @dummy = ( ${Header::table2colour} );
+undef (@dummy);
+
+# Files used
+my $setting = "${General::swroot}/ethernet/settings";
+our $datafile = "${General::swroot}/ethernet/aliases";
+
+
+our %settings=();
+#Settings1
+
+#Settings2 for editing the multi-line list
+#Must not be saved !
+$settings{'IP'} = '';
+$settings{'ENABLED'} = 'off';          # Every check box must be set to off
+$settings{'NAME'} = '';
+my @nosaved=('IP','ENABLED','NAME');   # List here ALL setting2 fields. Mandatory
+    
+$settings{'ACTION'} = '';              # add/edit/remove
+$settings{'KEY1'} = '';                        # point record for ACTION
+
+#Define each field that can be used to sort columns
+my $sortstring='^IP|^NAME';
+my $errormessage = '';
+my $warnmessage = '';
+
+&Header::showhttpheaders();
+
+# Read needed Ipcop netsettings
+my %netsettings=();
+$netsettings{'SORT_ALIASES'} = 'NAME';         # default sort
+&General::readhash($setting, \%netsettings);
+
+#Get GUI values
+&Header::getcgihash(\%settings);
+
+# Load multiline data
+our @current = ();
+if (open(FILE, "$datafile")) {
+    @current = <FILE>;
+    close (FILE);
+}
+
+#
+# Check Settings1 first because they are needed before working on @current
+#
+# Remove if no Setting1 needed
+#
+if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
+    
+    #
+    #Validate static Settings1 here
+    #
+    unless ($errormessage) {                                   # Everything is ok, save settings
+       #map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved 
+       #&General::writehash($setting, \%settings);             # Save good settings
+       #$settings{'ACTION'} = $Lang::tr{'save'};               # Recreate  'ACTION'
+       #map ($settings{$_}= '',(@nosaved,'KEY1'));             # and reinit var to empty
+       
+       # Rebuild configuration file if needed
+       &BuildConfiguration;
+    }
+
+    ERROR:                                             # Leave the faulty field untouched
+} else {
+    #&General::readhash($setting, \%settings);         # Get saved settings and reset to good if needed
+}
+
+## Now manipulate the multi-line list with Settings2
+# Basic actions are:
+#      toggle the check box
+#      add/update a new line
+#      begin editing a line
+#      remove a line
+
+
+# Toggle enable/disable field.  Field is in second position
+if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
+    #move out new line
+    chomp(@current[$settings{'KEY1'}]);
+    my @temp = split(/\,/,@current[$settings{'KEY1'}]);
+    $temp[1] = $temp[1] eq 'on' ? 'off' : 'on';                # Toggle the field
+    $temp[2] = '' if ( $temp[2] eq '' );
+    @current[$settings{'KEY1'}] = join (',',@temp)."\n";
+    $settings{'KEY1'} = '';                            # End edit mode
+    
+    &General::log($Lang::tr{'ip alias changed'});
+    
+    #Save current
+    open(FILE, ">$datafile") or die 'Unable to open aliases file.';
+    print FILE @current;
+    close(FILE);
+       
+    # Rebuild configuration file
+    &BuildConfiguration;
+}
+
+if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
+    # Validate inputs
+    if (! &General::validip($settings{'IP'})) {$errormessage = "invalid ip"};
+    $settings{'NAME'} = &Header::cleanhtml($settings{'NAME'});
+
+    # Make sure we haven't duplicated an alias or RED
+    my $spacer='';
+    if ($settings{'IP'} eq $netsettings{'RED_ADDRESS'}) {
+       $errormessage = $Lang::tr{'duplicate ip'} . ' (RED)';
+        $spacer=" & ";
+    }
+    #Check if we have an emtpy name
+    if (!$settings{'NAME'}){
+               $errormessage=$Lang::tr{'fwhost err name1'};
+       }elsif(! &General::validfqdn($settings{'NAME'}) && ! &General::validhostname($settings{'NAME'})){
+               $errormessage=$Lang::tr{'invalid hostname'};
+       }
+    my $idx=0;
+    foreach my $line (@current) {
+        chomp ($line);
+        my @temp = split (/\,/, $line);
+        if ( ($settings{'KEY1'} eq '')||(($settings{'KEY1'} ne '') && ($settings{'KEY1'} != $idx))) { # update
+           if ($temp[0] eq $settings{'IP'}) {
+               $errormessage .= $spacer.$Lang::tr{'duplicate ip'};
+               $spacer=" & ";
+           }
+           if ($temp[2] eq $settings{'NAME'} && $temp[2] ne '') {
+               $errormessage .= $spacer.$Lang::tr{'duplicate name'};
+               $spacer=" & ";
+               }
+       }
+       $idx++;
+    }
+       #Update firewallrules if aliasname is changed
+       if ($settings{'OLDNAME'} ne $settings {'NAME'}){
+               &General::readhasharray("$configfwdfw", \%forward);
+               &General::readhasharray("$configinput", \%input);
+               &General::readhasharray("$configoutgoing", \%output);
+               #Check FORWARD
+               foreach my $forwardkey (sort keys %forward){
+                       if ($forward{$forwardkey}[29] eq $settings{'OLDNAME'}){
+                               $forward{$forwardkey}[29] = $settings {'NAME'};
+                       }
+               }
+               &General::writehasharray($configfwdfw, \%forward);
+               #Check INPUT
+               foreach my $inputkey (sort keys %input){
+                       if ($input{$inputkey}[6] eq $settings{'OLDNAME'}){
+                               $input{$inputkey}[6] = $settings {'NAME'};
+                       }
+               }
+               &General::writehasharray($configinput, \%input);
+               #Check OUTPUT
+               foreach my $outputkey (sort keys %output){
+                       if ($output{$outputkey}[4] eq $settings{'OLDNAME'}){
+                               $output{$outputkey}[4] = $settings {'NAME'};
+                       }
+               }
+               &General::writehasharray($configoutgoing, \%output);
+               &General::firewall_config_changed;
+       }
+       #If Alias IP has changed, set firewall_config_changed
+       if($settings{'OLDIP'} ne $settings{'IP'} && $settings{'OLDIP'}){
+               &General::firewall_config_changed;
+       }
+    unless ($errormessage) {
+       if ($settings{'KEY1'} eq '') { #add or edit ?
+           unshift (@current, "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n");
+           &General::log($Lang::tr{'ip alias added'});
+       } else {
+           @current[$settings{'KEY1'}] = "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'}\n";
+           $settings{'KEY1'} = '';       # End edit mode
+           &General::log($Lang::tr{'ip alias changed'});
+       }
+
+       # Write changes to config file.
+       &SortDataFile;                          # sort newly added/modified entry
+
+       &BuildConfiguration;                    # then re-build conf which use new data
+       
+##
+## if entering data line is repetitive, choose here to not erase fields between each addition
+##
+       map ($settings{$_}='' ,@nosaved);       # Clear fields
+    }
+}
+
+if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
+    #move out new line
+    my $line = @current[$settings{'KEY1'}];    # KEY1 is the index in current
+    chomp($line);
+    my @temp = split(/\,/, $line);
+
+##
+## move data fields to Setting2 for edition
+##
+    $settings{'IP'}=$temp[0];                  # Prepare the screen for editing
+    $settings{'ENABLED'}=$temp[1];
+    $settings{'NAME'}=$temp[2];
+}
+
+if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
+    splice (@current,$settings{'KEY1'},1);             # Delete line 
+    open(FILE, ">$datafile") or die 'Unable to open aliases file.';
+    print FILE @current;
+    close(FILE);
+    $settings{'KEY1'} = '';                            # End remove mode
+    &General::log($Lang::tr{'ip alias removed'});
+
+    &BuildConfiguration;                               # then re-build conf which use new data
+}
+
+
+
+##  Check if sorting is asked
+# If same column clicked, reverse the sort.
+if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
+    my $newsort=$ENV{'QUERY_STRING'};
+    my $actual=$netsettings{'SORT_ALIASES'};
+    #Reverse actual sort ?
+    if ($actual =~ $newsort) {
+       my $Rev='';
+       if ($actual !~ 'Rev') {
+           $Rev='Rev';
+       }
+       $newsort.=$Rev;
+    }
+    $netsettings{'SORT_ALIASES'}=$newsort;
+    &General::writehash($setting, \%netsettings);
+    &SortDataFile;
+    $settings{'ACTION'} = 'SORT';                      # Recreate  'ACTION'
+}
+
+# Default initial value
+if ($settings{'ACTION'} eq '' ) { # First launch from GUI
+    $settings{'ENABLED'} ='on';
+}
+    
+&Header::openpage($Lang::tr{'external aliases configuration'}, 1, '');
+&Header::openbigbox('100%', 'left', '', $errormessage);
+my %checked =();     # Checkbox manipulations
+
+if ($errormessage) {
+    &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
+    print "$errormessage&nbsp;";
+    &Header::closebox();
+}
+unless (( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ ) && ($netsettings{'RED_TYPE'} eq 'STATIC'))
+{
+    &Header::openbox('100%', 'left', $Lang::tr{'capswarning'});
+    print <<END
+    <table style='width:100%;'>
+    <tr>
+    <td class='boldbase' style='color:${Header::colourred};'><b>$Lang::tr{'aliases not active'}</b></td>
+    </tr>
+    </table>
+END
+;
+    &Header::closebox();
+}
+                                                                       
+#
+# Second check box is for editing the list
+#
+$checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq 'on') ? "checked='checked'" : '' ;
+
+my $buttontext = $Lang::tr{'add'};
+if ($settings{'KEY1'} ne '') {
+    $buttontext = $Lang::tr{'update'};
+    &Header::openbox('100%', 'left', $Lang::tr{'edit an existing alias'});
+} else {
+    &Header::openbox('100%', 'left', $Lang::tr{'add new alias'});
+}
+
+#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
+print <<END
+<form method='post' action='$ENV{'SCRIPT_NAME'}'>
+<input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
+<input type='hidden' name='OLDNAME' value='$settings{'NAME'}' />
+<input type='hidden' name='OLDIP' value='$settings{'IP'}' />
+<table style='width:100%;'>
+<tr>
+<td class='base' style='color:${Header::colourred};'>$Lang::tr{'name'}:</td>
+<td><input type='text' name='NAME' value='$settings{'NAME'}' size='32' /></td>
+<td class='base' style='text-align:right; color:${Header::colourred};'>$Lang::tr{'alias ip'}:&nbsp;</td>
+<td><input type='text' name='IP' value='$settings{'IP'}' size='16' /></td>
+<td class='base' style='text-align:right;'>$Lang::tr{'enabled'}&nbsp;</td>
+<td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
+</tr>
+</table>
+<br>
+<hr />
+<table style='width:100%;'>
+<tr>
+    <td style='text-align:right;'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
+</tr>
+</table>
+</form>
+END
+;
+&Header::closebox();
+
+# Add visual indicators to column headings to show sort order - EO
+my $sortarrow1 = '';
+my $sortarrow2 = '';
+
+if ($netsettings{'SORT_ALIASES'} eq 'NAMERev') {
+       $sortarrow1 = $Header::sortdn;
+} elsif ($netsettings{'SORT_ALIASES'} eq 'NAME') {
+       $sortarrow1 = $Header::sortup;
+} elsif ($netsettings{'SORT_ALIASES'} eq 'IPRev') {
+       $sortarrow2 = $Header::sortdn;
+} else {
+       $sortarrow2 = $Header::sortup;
+}
+
+#
+# Third box shows the list, in columns
+#
+# Columns headers may content a link. In this case it must be named in $sortstring
+#
+&Header::openbox('100%', 'left', $Lang::tr{'current aliases'});
+print <<END
+<table class='tbl' style='width:100%;'>
+<tr>
+    <th style='width:55%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</th>
+    <th style='width:45%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</th>
+    <th colspan='3' class='boldbase' style='width:5%; text-align:center;'><b>$Lang::tr{'action'}</b></th>
+</tr>
+END
+;
+
+#
+# Print each line of @current list
+#
+# each data line is splitted into @temp.
+#
+
+my $key = 0;
+my $col="";
+foreach my $line (@current) {
+    chomp($line);
+    my @temp = split(/\,/,$line);
+
+    #Choose icon for checkbox
+    my $gif = '';
+    my $gdesc = '';
+    if ($temp[1] eq "on") {
+       $gif = 'on.gif';
+       $gdesc = $Lang::tr{'click to disable'};
+    } else {
+       $gif = 'off.gif';
+       $gdesc = $Lang::tr{'click to enable'}; 
+    }
+
+    #Colorize each line
+    if ($settings{'KEY1'} eq $key) {
+        $col="background-color:${Header::colouryellow};";
+    } elsif ($key % 2) {
+        $col="background-color:${Header::table2colour};";
+    } else {
+        $col="background-color:${Header::table1colour};";
+    }
+    print "<tr style='$col'>";
+
+    print <<END
+<td style='text-align:center; $col'>$temp[2]</td>
+<td style='text-align:center; $col'>$temp[0]</td>
+
+<td style='text-align:center; $col'>
+<form method='post' action='$ENV{'SCRIPT_NAME'}'>
+<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
+<input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
+<input type='hidden' name='KEY1' value='$key' />
+</form>
+</td>
+
+<td style='text-align:center; $col'>
+<form method='post' action='$ENV{'SCRIPT_NAME'}'>
+<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
+<input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
+<input type='hidden' name='KEY1' value='$key' />
+</form>
+</td>
+
+<td style='text-align:center; $col'>
+<form method='post' action='$ENV{'SCRIPT_NAME'}'>
+<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
+<input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
+<input type='hidden' name='KEY1' value='$key' />
+</form>
+</td>
+</tr>
+END
+;
+    $key++;
+}
+print "</table>";
+
+# If table contains entries, print 'Key to action icons'
+if ($key) {
+print <<END
+<table>
+<tr>
+    <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
+    <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
+    <td class='base'>$Lang::tr{'click to disable'}</td>
+    <td>&nbsp;&nbsp;</td>
+    <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
+    <td class='base'>$Lang::tr{'click to enable'}</td>
+    <td>&nbsp;&nbsp;</td>
+    <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
+    <td class='base'>$Lang::tr{'edit'}</td>
+    <td>&nbsp;&nbsp;</td>
+    <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
+    <td class='base'>$Lang::tr{'remove'}</td>
+</tr>
+</table>
+END
+;
+}
+
+&Header::closebox();
+&Header::closebigbox();
+&Header::closepage();
+
+## Ouf it's the end !
+
+
+
+# Sort the "current" array according to choices
+sub SortDataFile
+{
+    our %entries = ();
+    
+    # Sort pair of record received in $a $b special vars.
+    # When IP is specified use numeric sort else alpha.
+    # If sortname ends with 'Rev', do reverse sort.
+    #
+    sub fixedleasesort {
+       my $qs='';             # The sort field specified minus 'Rev'
+       if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
+           $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
+           if ($qs eq 'IP') {
+               my @a = split(/\./,$entries{$a}->{$qs});
+               my @b = split(/\./,$entries{$b}->{$qs});
+               ($b[0]<=>$a[0]) ||
+               ($b[1]<=>$a[1]) ||
+               ($b[2]<=>$a[2]) ||
+               ($b[3]<=>$a[3]);
+           } else {
+               $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
+           }
+       } else { #not reverse
+           $qs=$netsettings{'SORT_ALIASES'};
+           if ($qs eq 'IP') {
+               my @a = split(/\./,$entries{$a}->{$qs});
+               my @b = split(/\./,$entries{$b}->{$qs});
+               ($a[0]<=>$b[0]) ||
+               ($a[1]<=>$b[1]) ||
+               ($a[2]<=>$b[2]) ||
+               ($a[3]<=>$b[3]);
+           } else {
+               $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
+           }
+       }
+    }
+
+    #Use an associative array (%entries)
+    my $key = 0;
+    foreach my $line (@current) {
+       chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
+       my @temp = split (',',$line);
+       
+       # Build a pair 'Field Name',value for each of the data dataline.
+       # Each SORTABLE field must have is pair.
+       # Other data fields (non sortable) can be grouped in one
+       
+       # Exemple
+       # F1,F2,F3,F4,F5       only F1 F2 for sorting
+       # my @record = ('KEY',$key++,
+       #               'F1',$temp[0],
+       #               'F2',$temp[1],
+       #               'DATA',join(',',@temp[2..4])    );  #group remainning values, with separator (,)
+       
+       # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
+       
+       
+       my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2]);
+       my $record = {};                                # create a reference to empty hash
+       %{$record} = @record;                           # populate that hash with @record
+       $entries{$record->{KEY}} = $record;             # add this to a hash of hashes
+    }
+    
+    open(FILE, ">$datafile") or die 'Unable to open aliases file.';
+
+    # Each field value is printed , with the newline ! Don't forget separator and order of them.
+    foreach my $entry (sort fixedleasesort keys %entries) {
+       print FILE "$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME}\n";
+    }
+
+    close(FILE);
+    # Reload sorted  @current
+    open (FILE, "$datafile");
+    @current = <FILE>;
+    close (FILE);
+}
+
+#                                                  
+# Build the configuration file for application aliases
+#
+sub BuildConfiguration {
+    # Restart service associated with this
+    system '/usr/local/bin/setaliases';
+}
+