]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - src/scripts/connscheduler
3 # IPFire Connection Scheduler (F)Cron Job
5 # This code is distributed under the terms of the GPL
6 # The original code is taken from weizen_42.
7 # See /home/httpd/cgi-bin/connscheduler.cgi
12 require '/var/ipfire/general-functions.pl';
13 require '/var/ipfire/connscheduler/lib.pl';
15 # seems to be necessary
16 my $sleep_after_profile = 5;
18 my ($second, $minute, $hour, $day, $month ,$year, $weekday) = localtime(time);
19 # correction for weekday, I am used to weeks starting with Monday (= 0) ;-)
20 $weekday = ($weekday + 6) % 7;
21 # get the closest thing possible
22 $minute = int($minute / 5) * 5;
24 my $swroot = "/var/ipfire";
26 &readhash
("${swroot}/ethernet/settings", \
%ethernetsettings);
28 if ( $ARGV[0] eq 'hangup' )
32 elsif ( $ARGV[0] eq 'dial' )
36 elsif ( $ARGV[0] eq 'reconnect' )
40 elsif ( $ARGV[0] eq 'profile' )
44 elsif ( $ARGV[0] eq 'timer' )
48 elsif ( $ARGV[0] eq 'test' )
54 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
62 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
63 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
64 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
65 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
69 # Kill connectd if running to prevent redial
70 system('/bin/killall', 'connectd');
72 unless ( -e
"${General::swroot}/red/active" )
74 &General
::log("ConnSched already disconnected");
78 &General
::log("ConnSched disconnect");
79 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
81 &General
::log("ConnSched disconnect failed: $?");
85 # now wait for active triggerfile and ppp daemon to disappear
86 # wait maximum 60 seconds
89 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
99 if ( -e
"${General::swroot}/red/active" )
101 &General
::log("ConnSched already connected");
105 &General
::log("ConnSched connect");
106 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
108 &General
::log("ConnSched connect failed: $?");
112 # wait maximum 60 seconds for active triggerfile
114 until ( -e
"${General::swroot}/red/active" || $counter == 0 )
124 if ( $ethernetsettings{'RED_TYPE'} ne 'PPPOE' ){
125 &General
::log("ConnSched skipped because not running pppoe");
128 &hangup
() if ( -e
"${General::swroot}/red/active" );
129 # now wait for active triggerfile and ppp daemon to disappear
130 # wait maximum 60 seconds
133 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
147 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles
) )
149 &General
::log("ConnSched invalid profile: $profile");
153 unless ( -e
"${General::swroot}/ppp/settings-$profile" )
155 &General
::log("ConnSched profile file does not exist: $profile");
159 if ( -e
"${General::swroot}/red/active" )
161 # remember to restart red after changing profile
166 &General
::log("ConnSched select profile: $profile");
168 # Method to change Profile from pppsetup.cgi
169 unlink("${General::swroot}/ppp/settings");
170 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
171 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
173 if ( $restart_red == 1 )
175 ## FIXME: do we need to do this ?
176 sleep($sleep_after_profile);
185 for my $i ( 0 .. $#CONNSCHED::config
)
187 next if ( $CONNSCHED::config
[$i]{'ACTIVE'} ne 'on' );
189 my $action_hour = substr($CONNSCHED::config
[$i]{'TIME'},0,2);
190 my $action_minute = substr($CONNSCHED::config
[$i]{'TIME'},3,2);
192 next if ( $action_hour != $hour );
193 next if ( $action_minute != $minute );
195 if ( $CONNSCHED::config
[$i]{'DAYSTYPE'} eq 'days' )
197 my @temp = split(/-/,$CONNSCHED::config
[$i]{'DAYS'},2);
199 my $daystart = substr($temp[0], 0, -1);
200 my $dayend = substr($temp[1], 1);
202 next if ( ($day < $daystart) || ($day > $dayend) );
206 next if ( index($CONNSCHED::config
[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays
[$weekday]) == -1 );
210 if ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reconnect' )
214 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'dial' )
218 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'hangup' )
222 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'select profile' )
224 &profile
($CONNSCHED::config
[$i]{'PROFILENR'});
226 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reboot' )
228 &General
::log("ConnSched reboot");
229 system ("/usr/local/bin/ipfirereboot", "boot");
231 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'shutdown' )
233 &General
::log("ConnSched shutdown");
234 system ("/usr/local/bin/ipfirereboot", "down");
236 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstart' )
238 &General
::log("ConnSched ipsecstart");
239 system ("/usr/local/bin/ipsecctrl", "S");
241 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstop' )
243 &General
::log("ConnSched ipsecstop");
244 system ("/usr/local/bin/ipsecctrl", "D");
248 # okay ? an event we don't know about
255 my $filename = $_[0];
261 open(FILE
, $filename) or die "Unable to read file $filename";
265 ($var, $val) = split /=/, $_, 2;
271 # Untaint variables read from hash
272 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
273 $val =~ /([\w\W]*)/; $val = $1;
274 $hash->{$var} = $val;