]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/cfgroot/general-functions.pl
Import VPN changes by the Special Interest Group.
[ipfire-2.x.git] / config / cfgroot / general-functions.pl
index 57939d8bd8b16594b567e0537b2f34d735caf271..567f2e104ec5cb02bae2e588e0b18472a7ab7957 100644 (file)
@@ -18,6 +18,7 @@ use strict;
 use Socket;
 use IO::Socket;
 use Net::SSLeay;
+use Net::IPv4Addr;
 
 $|=1; # line buffering
 
@@ -37,7 +38,7 @@ sub log
        my $logmessage = $_[0];
        $logmessage =~ /([\w\W]*)/;
        $logmessage = $1;
-       system('/usr/bin/logger', '-t', $tag, $logmessage);
+       system('logger', '-t', $tag, $logmessage);
 }
 
 sub readhash
@@ -63,8 +64,13 @@ sub readhash
                        $val =~ s/\'$//g;
 
                        # Untaint variables read from hash
-                       $var =~ /([A-Za-z0-9_-]*)/;        $var = $1;
-                       $val =~ /([\w\W]*)/; $val = $1;
+                       # trim space from begin and end
+                       $var =~ s/^\s+//;
+                       $var =~ s/\s+$//;
+                       $var =~ /([A-Za-z0-9_-]*)/;
+                       $var = $1;
+                       $val =~ /([\w\W]*)/;
+                       $val = $1;
                        $hash->{$var} = $val;
                }
        }
@@ -83,6 +89,7 @@ sub writehash
        flock FILE, 2;
        foreach $var (keys %$hash) 
        {
+               if ( $var eq "__CGI__"){next;}
                $val = $hash->{$var};
                # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
                # location of the mouse are submitted as well, this was being written to the settings file causing
@@ -97,6 +104,41 @@ sub writehash
        close FILE;
 }
 
+sub writehashpart
+{
+       # This function replaces the given hash in the original hash by keeping the old
+       # content and just replacing the new content
+
+       my $filename = $_[0];
+       my $newhash = $_[1];
+       my %oldhash;
+       my ($var, $val);
+
+       readhash("${filename}", \%oldhash);
+
+       foreach $var (keys %$newhash){
+               $oldhash{$var}=$newhash->{$var};
+       }
+
+       # write cgi vars to the file.
+       open(FILE, ">${filename}") or die "Unable to write file $filename";
+       flock FILE, 2;
+       foreach $var (keys %oldhash) 
+       {
+               if ( $var eq "__CGI__"){next;}
+               $val = $oldhash{$var};
+               # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
+               # location of the mouse are submitted as well, this was being written to the settings file causing
+               # some serious grief! This skips the variable.x and variable.y
+               if (!($var =~ /(.x|.y)$/)) {
+                       if ($val =~ / /) {
+                               $val = "\'$val\'"; }
+                       if (!($var =~ /^ACTION/)) {
+                               print FILE "${var}=${val}\n"; }
+               }
+       }
+       close FILE;
+}
 
 sub age
 {
@@ -198,6 +240,21 @@ sub validport
        return 0;
 }
 
+sub validproxyport
+{
+       $_ = $_[0];
+
+       if (!/^\d+$/) {
+               return 0; }
+       if (/^0./) {
+               return 0; }
+       if ($_ == 53 || $_ == 222 || $_ == 444 || $_ == 81 ) {
+               return 0; }
+       elsif ($_ >= 1 && $_ <= 65535) {
+               return 1; }
+       return 0;
+}
+
 sub validmac
 {
        my $checkmac = $_[0];
@@ -351,6 +408,20 @@ sub NextIP
                             );
 }
 
+sub ipcidr
+{
+       my ($ip,$cidr) = &Net::IPv4Addr::ipv4_parse(shift);
+       return "$ip\/$cidr";
+}
+
+sub ipcidr2msk
+{
+       my ($ip,$cidr) = &Net::IPv4Addr::ipv4_parse(shift);
+       my $netmask = &Net::IPv4Addr::ipv4_cidr2msk($cidr);
+       return "$ip\/$netmask";
+}
+
+
 sub validemail {
     my $mail = shift;
     return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ );