]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - config/cfgroot/general-functions.pl
Firewall: Fixed portfw-converter (rules where not converted correctly) And Standard...
[people/teissler/ipfire-2.x.git] / config / cfgroot / general-functions.pl
index b1b47abcb0e9985cbbbfff4004458c99df2618dc..9d9ee5d900cd9f6833caa87c501405f6b53f7427 100644 (file)
@@ -39,6 +39,96 @@ sub log
        $logmessage = $1;
        system('logger', '-t', $tag, $logmessage);
 }
+sub setup_default_networks
+{
+       my %netsettings=();
+       my $defaultNetworks = shift;
+       
+       &readhash("/var/ipfire/ethernet/settings", \%netsettings);
+       
+       # Get current defined networks (Red, Green, Blue, Orange)
+       $defaultNetworks->{$Lang::tr{'fwhost any'}}{'IPT'} = "0.0.0.0/0.0.0.0";
+       $defaultNetworks->{$Lang::tr{'fwhost any'}}{'NAME'} = "ALL";
+               
+       $defaultNetworks->{$Lang::tr{'green'}}{'IPT'} = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
+       $defaultNetworks->{$Lang::tr{'green'}}{'NET'} = "$netsettings{'GREEN_ADDRESS'}";
+       $defaultNetworks->{$Lang::tr{'green'}}{'NAME'} = "GREEN";
+
+       if ($netsettings{'RED_DEV'} ne ''){
+               $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'IPT'} = "$netsettings{'RED_NETADDRESS'}/$netsettings{'RED_NETMASK'}";
+               $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'NET'} = "$netsettings{'RED_ADDRESS'}";
+               $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'NAME'} = "RED";
+       }
+       if ($netsettings{'ORANGE_DEV'} ne ''){
+               $defaultNetworks->{$Lang::tr{'orange'}}{'IPT'} = "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
+               $defaultNetworks->{$Lang::tr{'orange'}}{'NET'} = "$netsettings{'ORANGE_ADDRESS'}";
+               $defaultNetworks->{$Lang::tr{'orange'}}{'NAME'} = "ORANGE";
+       }
+
+       if ($netsettings{'BLUE_DEV'} ne ''){
+               $defaultNetworks->{$Lang::tr{'blue'}}{'IPT'} = "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
+               $defaultNetworks->{$Lang::tr{'blue'}}{'NET'} = "$netsettings{'BLUE_ADDRESS'}";
+               $defaultNetworks->{$Lang::tr{'blue'}}{'NAME'} = "BLUE";
+       }
+       
+       #IPFire himself
+       $defaultNetworks->{'IPFire'}{'NAME'} = "IPFire";
+
+       # OpenVPN
+       if(-e "${General::swroot}/ovpn/settings")
+       {
+               my %ovpnSettings = ();
+               &readhash("${General::swroot}/ovpn/settings", \%ovpnSettings);
+
+               # OpenVPN on Red?
+               if(defined($ovpnSettings{'DOVPN_SUBNET'}))
+               {
+                       my ($ip,$sub) = split(/\//,$ovpnSettings{'DOVPN_SUBNET'});
+                       $sub=&General::iporsubtocidr($sub);
+                       my @tempovpnsubnet = split("\/", $ovpnSettings{'DOVPN_SUBNET'});
+                       $defaultNetworks->{'OpenVPN ' ."($ip/$sub)"}{'ADR'} = $tempovpnsubnet[0];
+                       $defaultNetworks->{'OpenVPN ' ."($ip/$sub)"}{'NAME'} = "OpenVPN-Dyn";
+               }
+       } # end OpenVPN
+       # IPsec RW NET
+       if(-e "${General::swroot}/vpn/settings")
+       {
+               my %ipsecsettings = ();
+               &readhash("${General::swroot}/vpn/settings", \%ipsecsettings);
+               if($ipsecsettings{'RW_NET'} ne '')
+               {
+                       my ($ip,$sub) = split(/\//,$ipsecsettings{'RW_NET'});
+                       $sub=&General::iporsubtocidr($sub);
+                       my @tempipsecsubnet = split("\/", $ipsecsettings{'RW_NET'});
+                       $defaultNetworks->{'IPsec RW (' .$ip."/".$sub.")"}{'ADR'} = $tempipsecsubnet[0];
+                       $defaultNetworks->{'IPsec RW (' .$ip."/".$sub.")"}{'NAME'} = "IPsec RW";
+                       $defaultNetworks->{'IPsec RW (' .$ip."/".$sub.")"}{'NET'} = &getnextip($ip);
+               }
+       }
+}
+sub get_aliases
+{
+       
+       my $defaultNetworks = shift;
+       open(FILE, "${General::swroot}/ethernet/aliases") or die 'Unable to open aliases file.';
+       my @current = <FILE>;
+       close(FILE);
+       my $ctr = 0;
+       foreach my $line (@current)
+       {
+               if ($line ne ''){
+                       chomp($line);
+                       my @temp = split(/\,/,$line);
+                       if ($temp[2] eq '') {
+                               $temp[2] = "Alias $ctr : $temp[0]";
+                       }
+                       $defaultNetworks->{$temp[2]}{'IPT'} = "$temp[0]";
+                       $defaultNetworks->{$temp[2]}{'NET'} = "$temp[0]";
+                       
+                       $ctr++;
+               }
+       }
+}
 
 sub readhash
 {
@@ -920,9 +1010,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;
@@ -1006,4 +1098,68 @@ sub MakeUserAgent() {
        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; 
+}
+
+my $FIREWALL_RELOAD_INDICATOR = "${General::swroot}/firewall/reread";
+
+sub firewall_config_changed() {
+       open FILE, ">$FIREWALL_RELOAD_INDICATOR" or die "Could not open $FIREWALL_RELOAD_INDICATOR";
+       close FILE;
+}
+
+sub firewall_needs_reload() {
+       if (-e "$FIREWALL_RELOAD_INDICATOR") {
+               return 1;
+       }
+
+       return 0;
+}
+
+sub firewall_reload() {
+       system("/usr/local/bin/firewallctrl");
+}
+
 1;