]>
git.ipfire.org Git - people/pmueller/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;
25 if ( $ARGV[0] eq 'hangup' )
29 elsif ( $ARGV[0] eq 'dial' )
33 elsif ( $ARGV[0] eq 'reconnect' )
37 elsif ( $ARGV[0] eq 'profile' )
41 elsif ( $ARGV[0] eq 'timer' )
45 elsif ( $ARGV[0] eq 'test' )
51 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
59 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
60 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
61 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
62 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
66 unless ( -e
"${General::swroot}/red/active" )
68 &General
::log("ConnSched already disconnected");
72 &General
::log("ConnSched disconnect");
73 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
75 &General
::log("ConnSched disconnect failed: $?");
79 # now wait for active triggerfile and ppp daemon to disappear
80 # wait maximum 60 seconds
83 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
93 if ( -e
"${General::swroot}/red/active" )
95 &General
::log("ConnSched already connected");
99 &General
::log("ConnSched connect");
100 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
102 &General
::log("ConnSched connect failed: $?");
106 # wait maximum 60 seconds for active triggerfile
108 until ( -e
"${General::swroot}/red/active" || $counter == 0 )
118 &hangup
() if ( -e
"${General::swroot}/red/active" );
119 # now wait for active triggerfile and ppp daemon to disappear
120 # wait maximum 60 seconds
123 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
137 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles
) )
139 &General
::log("ConnSched invalid profile: $profile");
143 unless ( -e
"${General::swroot}/ppp/settings-$profile" )
145 &General
::log("ConnSched profile file does not exist: $profile");
149 if ( -e
"${General::swroot}/red/active" )
151 # remember to restart red after changing profile
156 &General
::log("ConnSched select profile: $profile");
158 # Method to change Profile from pppsetup.cgi
159 unlink("${General::swroot}/ppp/settings");
160 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
161 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
163 if ( $restart_red == 1 )
165 ## FIXME: do we need to do this ?
166 sleep($sleep_after_profile);
175 for my $i ( 0 .. $#CONNSCHED::config
)
177 next if ( $CONNSCHED::config
[$i]{'ACTIVE'} ne 'on' );
179 my $action_hour = substr($CONNSCHED::config
[$i]{'TIME'},0,2);
180 my $action_minute = substr($CONNSCHED::config
[$i]{'TIME'},3,2);
182 next if ( $action_hour != $hour );
183 next if ( $action_minute != $minute );
185 if ( $CONNSCHED::config
[$i]{'DAYSTYPE'} eq 'days' )
187 my @temp = split(/-/,$CONNSCHED::config
[$i]{'DAYS'},2);
189 my $daystart = substr($temp[0], 0, -1);
190 my $dayend = substr($temp[1], 1);
192 next if ( ($day < $daystart) || ($day > $dayend) );
196 next if ( index($CONNSCHED::config
[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays
[$weekday]) == -1 );
200 if ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reconnect' )
204 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'dial' )
208 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'hangup' )
212 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'select profile' )
214 &profile
($CONNSCHED::config
[$i]{'PROFILENR'});
216 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reboot' )
218 &General
::log("ConnSched reboot");
219 system ("/usr/local/bin/ipfirereboot", "boot");
221 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'shutdown' )
223 &General
::log("ConnSched shutdown");
224 system ("/usr/local/bin/ipfirereboot", "down");
226 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstart' )
228 &General
::log("ConnSched ipsecstart");
229 system ("/usr/local/bin/ipsecctrl", "S");
231 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstop' )
233 &General
::log("ConnSched ipsecstop");
234 system ("/usr/local/bin/ipsecctrl", "D");
238 # okay ? an event we don't know about