]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/cfgroot/general-functions.pl
Merge remote-tracking branch 'stevee/proxy-squidclamav' into next
[ipfire-2.x.git] / config / cfgroot / general-functions.pl
index c14f9903fcc28a1cb1ed502e586fe74a4e7ffc2a..41643d8d7451c25aebda2475600b0f9c170845a4 100644 (file)
@@ -144,6 +144,10 @@ sub age
        my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
                $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
        my $now = time;
+       my $timestring = '';
+       my $dset = 0;           # Day is set, when > 0
+       my $hset = 0;           # Hour is set, when > 0
+       my $mset = 0;           # Minute is set, when > 0
 
        my $totalsecs = $now - $mtime;
        my $days = int($totalsecs / 86400);
@@ -153,7 +157,50 @@ sub age
        my $mins = $totalmins % 60;
        my $secs = $totalsecs % 60;
 
-       return "${days}d ${hours}h ${mins}m ${secs}s";
+       if      ($days > 1) { 
+               ${timestring} .= ${days}.' '.$Lang::tr{'days'}.', ';
+               $dset = 1; 
+       }
+       elsif   ($days == 1) { 
+               ${timestring} .= ${days}.' '.$Lang::tr{'day'}.', ';
+               $dset = 1; 
+       }
+
+       if      (($hours > 1) && !($dset)) { 
+               ${timestring} .= ${hours}.' '.$Lang::tr{'hours'}.', ';
+               $hset = 1;
+       }
+       elsif   (($hours == 1) && !($dset)) { 
+               ${timestring} .= ${hours}.' '.$Lang::tr{'hour'}.', ';
+               $hset = 1;
+       }
+       elsif ($dset) {
+               ${timestring} .= ${hours}.' '.$Lang::tr{'age shour'}.', ';
+               $hset = 1;
+       }
+
+       if      ((($mins > 1) || ($mins == 0)) && !($dset || $hset)) { 
+               ${timestring} .= ${mins}.' '.$Lang::tr{'minutes'}.', ';
+               $mset = 1;
+       }
+       elsif   (($mins == 1) && !($dset || $hset)) { 
+               ${timestring} .= ${mins}.' '.$Lang::tr{'minute'}.', ';
+               $mset = 1;
+       }
+       else {
+               ${timestring} .= ${mins}.' '.$Lang::tr{'age sminute'}.', '; 
+               $mset = 1;
+       }
+
+       if      ((($secs > 1) || ($secs == 0)) && !($dset || $hset || $mset)) { 
+               ${timestring} .= ${secs}.' '.$Lang::tr{'age seconds'};
+       }
+       elsif   (($secs == 1) && !($dset || $hset || $mset)) { 
+               ${timestring} .= $secs.' '.$Lang::tr{'age second'};
+       }
+       else    { ${timestring} .= $secs.' '.$Lang::tr{'age ssecond'}; }
+
+       return ${timestring};
 }
 
 sub validip
@@ -401,7 +448,6 @@ sub validipandmask
 
 sub checksubnets
 {
-       
        my %ccdconfhash=();                     
        my @ccdconf=();                         
        my $ccdname=$_[0];                      
@@ -409,20 +455,16 @@ sub checksubnets
        my $errormessage;
        my ($ip,$cidr)=split(/\//,$ccdnet);
        $cidr=&iporsubtocidr($cidr);
-       
-       
        #get OVPN-Subnet (dynamic range)
        my %ovpnconf=();
        &readhash("${General::swroot}/ovpn/settings", \%ovpnconf);
        my ($ovpnip,$ovpncidr)= split (/\//,$ovpnconf{'DOVPN_SUBNET'});
        $ovpncidr=&iporsubtocidr($ovpncidr);
-       
        #check if we try to use same network as ovpn server
        if ("$ip/$cidr" eq "$ovpnip/$ovpncidr") {
                        $errormessage=$errormessage.$Lang::tr{'ccd err isovpnnet'}."<br>";
                        return $errormessage;
        }
-               
        #check if we use a network-name/subnet that already exists
        &readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
        foreach my $key (keys %ccdconfhash) {
@@ -438,33 +480,22 @@ sub checksubnets
                        $errormessage=$errormessage.$Lang::tr{'ccd err issubnet'}."<br>";
                        return $errormessage;
                }
-                       
        }
-       #check if we use a name which is already used by ovpn
-       
-       
-       
-       
-       
        #check if we use a ipsec right network which is already defined
        my %ipsecconf=();
        &General::readhasharray("${General::swroot}/vpn/config", \%ipsecconf);
        foreach my $key (keys %ipsecconf){
                if ($ipsecconf{$key}[11] ne ''){
-                       #$errormessage="DRIN!";
-                       #return $errormessage;
-                       
                        my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
                        $ipsecsub=&iporsubtodec($ipsecsub);
-                       
-                       if ( &IpInSubnet ($ip,$ipsecip,$ipsecsub) ){
-                               $errormessage=$Lang::tr{'ccd err isipsecnet'}." Name:  $ipsecconf{$key}[2]";
-                               return $errormessage;
+                       if($ipsecconf{$key}[1] ne $ccdname){
+                               if ( &IpInSubnet ($ip,$ipsecip,$ipsecsub) ){
+                                       $errormessage=$Lang::tr{'ccd err isipsecnet'}." Name:  $ipsecconf{$key}[1]";
+                                       return $errormessage;
+                               }
                        }
                }
        }
-       
-               
        #check if we use one of ipfire's networks (green,orange,blue)
        my %ownnet=();
        &readhash("${General::swroot}/ethernet/settings", \%ownnet);
@@ -472,9 +503,6 @@ sub checksubnets
        if (($ownnet{'ORANGE_NETADDRESS'}       ne '' && $ownnet{'ORANGE_NETADDRESS'}   ne '0.0.0.0') && &IpInSubnet($ownnet{'ORANGE_NETADDRESS'},$ip,&iporsubtodec($cidr))){ $errormessage=$Lang::tr{'ccd err orange'};return $errormessage;}
        if (($ownnet{'BLUE_NETADDRESS'}         ne '' && $ownnet{'BLUE_NETADDRESS'}     ne '0.0.0.0') && &IpInSubnet($ownnet{'BLUE_NETADDRESS'},$ip,&iporsubtodec($cidr))){ $errormessage=$Lang::tr{'ccd err blue'};return $errormessage;}
        if (($ownnet{'RED_NETADDRESS'}          ne '' && $ownnet{'RED_NETADDRESS'}              ne '0.0.0.0') && &IpInSubnet($ownnet{'RED_NETADDRESS'},$ip,&iporsubtodec($cidr))){ $errormessage=$Lang::tr{'ccd err red'};return $errormessage;}
-       
-       
-
 }
 
 
@@ -829,13 +857,14 @@ sub FetchPublicIp {
         my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
         Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
     }
-    my ($out, $response) = Net::SSLeay::get_http(  'checkip.dyndns.org',
+    my $user_agent = &MakeUserAgent();
+    my ($out, $response) = Net::SSLeay::get_http(  'checkip4.dns.lightningwirelabs.com',
                                                    80,
                                                    "/",
-                                                   Net::SSLeay::make_headers('User-Agent' => 'IPFire' )
+                                                   Net::SSLeay::make_headers('User-Agent' => $user_agent )
                                                );
     if ($response =~ m%HTTP/1\.. 200 OK%) {
-       $out =~ /Current IP Address: (\d+.\d+.\d+.\d+)/;
+       $out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
        return $1;
     }
     return '';
@@ -891,9 +920,11 @@ sub GetDyndnsRedIP {
     close(IP);
     chomp $ip;
 
+    # 100.64.0.0/10 is reserved for dual-stack lite (http://tools.ietf.org/html/rfc6598).
     if (&General::IpInSubnet ($ip,'10.0.0.0','255.0.0.0') ||
         &General::IpInSubnet ($ip,'172.16.0.0.','255.240.0.0') ||
-        &General::IpInSubnet ($ip,'192.168.0.0','255.255.0.0'))
+        &General::IpInSubnet ($ip,'192.168.0.0','255.255.0.0') ||
+        &General::IpInSubnet ($ip,'100.64.0.0', '255.192.0.0'))
     {
        if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
            my $RealIP = &General::FetchPublicIp;
@@ -952,4 +983,74 @@ sub GetIcmpDescription ($) {
     'Experimental');
     if ($index>41) {return 'unknown'} else {return @icmp_description[$index]};
 }
+
+sub GetCoreUpdateVersion() {
+       my $core_update;
+
+       open(FILE, "/opt/pakfire/db/core/mine");
+       while (<FILE>) {
+               $core_update = $_;
+               last;
+       }
+       close(FILE);
+
+       return $core_update;
+}
+
+sub MakeUserAgent() {
+       my $user_agent = "IPFire/$General::version";
+
+       my $core_update = &GetCoreUpdateVersion();
+       if ($core_update ne "") {
+               $user_agent .= "/$core_update";
+       }
+
+       return $user_agent;
+}
+
+sub RedIsWireless() {
+       # This function checks if a network device is a wireless device.
+
+       my %settings = ();
+       &readhash("${General::swroot}/ethernet/settings", \%settings);
+
+       # Find the name of the network device.
+       my $device = $settings{'RED_DEV'};
+
+       # Exit, if no device is configured.
+       return 0 if ($device eq "");
+
+       # Return 1 if the device is a wireless one.
+       my $path = "/sys/class/net/$device/wireless";
+       if (-d $path) {
+               return 1;
+       }
+
+       # Otherwise return zero.
+       return 0;
+}
+
+# Function to read a file with UTF-8 charset.
+sub read_file_utf8 ($) {
+       my ($file) = @_;
+
+       open my $in, '<:encoding(UTF-8)', $file or die "Could not open '$file' for reading $!";
+       local $/ = undef;
+       my $all = <$in>;
+       close $in;
+
+       return $all;
+}
+
+# Function to write a file with UTF-8 charset.
+sub write_file_utf8 ($) {
+       my ($file, $content) = @_;
+
+       open my $out, '>:encoding(UTF-8)', $file or die "Could not open '$file' for writing $!";;           
+       print $out $content;
+       close $out;
+
+       return; 
+}
+
 1;