Forward Firewall: Fix ruletimes. Now the timevalues which are entered in the gui...
authorAlexander Marx <amarx@ipfire.org>
Fri, 8 Mar 2013 07:38:41 +0000 (08:38 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Aug 2013 12:11:55 +0000 (14:11 +0200)
Wenn rule.pl is called, the script calculates the difference to UTC time and sets the iptables times accordingly.

With this approach there's no need to save if the times are created in summertime or wintertime.

config/forwardfw/rules.pl
html/cgi-bin/forwardfw.cgi

index d4006846d589dcf99bd1994c051df0e7d38228e5..9332c9bec3e5a03f1816d6aab4acfe6041af3ca6 100755 (executable)
@@ -25,6 +25,7 @@
 ###############################################################################
 
 use strict;
+use Time::Local;
 no warnings 'uninitialized';
 
 # enable only the following on debugging purpose
@@ -200,6 +201,10 @@ sub buildrules
 
                        #get time if defined
                        if($$hash{$key}[18] eq 'ON'){
+                               my ($time1,$time2,$daylight);
+                               my $daylight=$$hash{$key}[28];
+                               $time1=&get_time($$hash{$key}[26],$daylight);
+                               $time2=&get_time($$hash{$key}[27],$daylight);
                                if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
                                if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
                                if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
@@ -208,8 +213,9 @@ sub buildrules
                                if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
                                if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
                                $TIME=join(",",@timeframe);
-                               $TIMEFROM="--timestart $$hash{$key}[26] ";
-                               $TIMETILL="--timestop $$hash{$key}[27] ";
+                               
+                               $TIMEFROM="--timestart $time1 ";
+                               $TIMETILL="--timestop $time2 ";
                                $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
                        }
                        if ($MODE eq '1'){      
@@ -287,6 +293,36 @@ sub buildrules
                undef $TIMETILL;
        }
 }
+sub get_time
+{
+       my $val=shift;
+       my $val1=shift;
+       my $time;
+       my $minutes;
+       my $ruletime;
+       $minutes = &utcmin($val);
+       $ruletime = $minutes + &time_get_utc($val);
+       if ($ruletime < 0){$ruletime +=1440;}
+       if ($ruletime > 1440){$ruletime -=1440;}
+       $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60;
+       return $time;
+}
+sub time_get_utc
+{
+       # Calculates the UTCtime from a given time
+       my $val=shift;
+       my @localtime=localtime(time);
+       my @gmtime=gmtime(time);
+       my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60);
+       return $diff;
+}
+sub utcmin
+{
+       my $ruletime=shift;
+       my ($hrs,$min) = split(":",$ruletime);
+       my $newtime = $hrs*60+$min;
+       return $newtime;
+}
 sub p2pblock
 {
        my $P2PSTRING;
index 1caa34869cbdd5edb89f38b73004f6f7d16e2927..1f3d6a9a600230c9cd9dfa4a761a1010283e5d25 100755 (executable)
@@ -74,7 +74,7 @@ my %ipsecsettings=();
 my %aliases=();
 my %optionsfw=();
 
-my $VERSION='0.9.8.3';
+my $VERSION='0.9.8.4';
 my $color;
 my $confignet          = "${General::swroot}/fwhosts/customnetworks";
 my $confighost         = "${General::swroot}/fwhosts/customhosts";
@@ -143,12 +143,6 @@ if ($fwdfwsettings{'ACTION'} eq 'saverule')
        if(     $fwdfwsettings{$fwdfwsettings{'grp1'}} eq 'IPFire' && $fwdfwsettings{'grp2'} eq 'ipfire'){
                $errormessage.=$Lang::tr{'fwdfw err same'};
        }
-       #Konvert timeframe if defined
-       if ($fwdfwsettings{'TIME'} eq 'ON'){
-               $fwdfwsettings{'TIME_FROM'} = &timeconvert($fwdfwsettings{'TIME_FROM'},'2utc');
-               $fwdfwsettings{'TIME_TO'}   = &timeconvert($fwdfwsettings{'TIME_TO'},'2utc');
-               print "NACHHER: $fwdfwsettings{'TIME_FROM'} - $fwdfwsettings{'TIME_TO'}<br>";
-       }
        #DMZ-Part
        if ($fwdfwsettings{$fwdfwsettings{'grp1'}} eq 'ORANGE' || $checkorange eq 'on'){
                $fwdfwsettings{'config'}=$configdmz;
@@ -1391,11 +1385,10 @@ sub newrule
        $checked{'TIME_FRI'}{$fwdfwsettings{'TIME_FRI'}}                = 'CHECKED';
        $checked{'TIME_SAT'}{$fwdfwsettings{'TIME_SAT'}}                = 'CHECKED';
        $checked{'TIME_SUN'}{$fwdfwsettings{'TIME_SUN'}}                = 'CHECKED';
-       $fwdfwsettings{'TIME_FROM'} = &timeconvert($fwdfwsettings{'TIME_FROM'},'');
-       $fwdfwsettings{'TIME_TO'} = &timeconvert($fwdfwsettings{'TIME_TO'},'');
        $selected{'TIME_FROM'}{$fwdfwsettings{'TIME_FROM'}}             = 'selected';
        $selected{'TIME_TO'}{$fwdfwsettings{'TIME_TO'}}                 = 'selected';
        $selected{'ipfire'}{$fwdfwsettings{$fwdfwsettings{'grp2'}}} ='selected';
+       
        #check if update and get values
        if($fwdfwsettings{'updatefwrule'} eq 'on' || $fwdfwsettings{'copyfwrule'} eq 'on' && !$errormessage){
                &General::readhasharray("$config", \%hash);
@@ -1485,8 +1478,10 @@ sub newrule
                }       
        }
        &Header::openbox('100%', 'left', $Lang::tr{'fwdfw addrule'});
-       $fwdfwsettings{'TIME_FROM'} = &timeconvert($fwdfwsettings{'TIME_FROM'},'');
-       $fwdfwsettings{'TIME_TO'} = &timeconvert($fwdfwsettings{'TIME_TO'},'');
+       if ($fwdfwsettings{'TIME'} eq 'ON'){    
+               $fwdfwsettings{'TIME_FROM'} = &timeconvert($fwdfwsettings{'TIME_FROM'},'');
+               $fwdfwsettings{'TIME_TO'} = &timeconvert($fwdfwsettings{'TIME_TO'},'');
+       }
 print <<END;
        <form method="post">
        <table border='0'>
@@ -1998,49 +1993,6 @@ sub saverule
                }
        }
 }
-sub timeconvert
-{
-       my $orgtime=shift;
-       my $type=shift;
-       my $newtime;
-       my ($hour,$min) = split (":", $orgtime);
-       my @locar = localtime(time);
-       my @gmtar = gmtime();
-       my $oldtime = $hour*60 + $min;
-       my $gmt = $gmtar[2]*60 + $gmtar[1];
-       my $loc = $locar[2]*60 + $locar[1];
-       my $diff;
-       my $newtime;
-       if ($gmt > $loc)
-       {
-               $diff = $gmt - $loc;
-               if ($type eq '2utc')
-               {
-                       print"umrechnen nach UTC<br>";
-                       $newtime = $oldtime + $diff;
-               }else{
-                       $newtime = $oldtime - $diff;
-               }
-       }else{
-               $diff = $loc - $gmt;
-               if ($type eq '2utc')
-               {
-                       $newtime = $oldtime - $diff;
-               }else{
-                       $newtime = $oldtime + $diff;
-               }
-       }
-       if ($newtime < 0 ){
-               $newtime += 1440;
-       }
-       if ($newtime >= 1440){
-               $newtime -= 1440;
-       }
-       my $newhour =sprintf"%02d", $newtime/60;
-       my $newmin  = sprintf"%02d",$newtime % 60;
-       $newtime = "$newhour:$newmin";
-       return $newtime;
-}
 sub validremark
 {
        # Checks a hostname against RFC1035
@@ -2221,10 +2173,7 @@ END
                                if($$hash{$key}[25] ne ''){push (@days,$Lang::tr{'fwdfw wd_sun'});}
                                my $weekdays=join(",",@days);
                                if (@days){
-                                       my $from = &timeconvert($$hash{$key}[26],'2loc');
-                                       my $to = &timeconvert($$hash{$key}[27],'2loc');;
-                                       
-                                       print"<td align='center' width='100'>$weekdays &nbsp $from - $to</td>";
+                                       print"<td align='center' width='100'>$weekdays &nbsp $$hash{$key}[26] - $$hash{$key}[27] </td>";
                                }
                        }else{
                                        print"<td align='center'>24/7</td>";