]>
git.ipfire.org Git - people/mfischer/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 # Kill connectd if running to prevent redial
67 system('/bin/killall', 'connectd');
69 unless ( -e
"${General::swroot}/red/active" )
71 &General
::log("ConnSched already disconnected");
75 &General
::log("ConnSched disconnect");
76 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
78 &General
::log("ConnSched disconnect failed: $?");
82 # now wait for active triggerfile and ppp daemon to disappear
83 # wait maximum 60 seconds
86 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
96 if ( -e
"${General::swroot}/red/active" )
98 &General
::log("ConnSched already connected");
102 &General
::log("ConnSched connect");
103 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
105 &General
::log("ConnSched connect failed: $?");
109 # wait maximum 60 seconds for active triggerfile
111 until ( -e
"${General::swroot}/red/active" || $counter == 0 )
121 &hangup
() if ( -e
"${General::swroot}/red/active" );
122 # now wait for active triggerfile and ppp daemon to disappear
123 # wait maximum 60 seconds
126 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
140 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles
) )
142 &General
::log("ConnSched invalid profile: $profile");
146 unless ( -e
"${General::swroot}/ppp/settings-$profile" )
148 &General
::log("ConnSched profile file does not exist: $profile");
152 if ( -e
"${General::swroot}/red/active" )
154 # remember to restart red after changing profile
159 &General
::log("ConnSched select profile: $profile");
161 # Method to change Profile from pppsetup.cgi
162 unlink("${General::swroot}/ppp/settings");
163 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
164 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
166 if ( $restart_red == 1 )
168 ## FIXME: do we need to do this ?
169 sleep($sleep_after_profile);
178 for my $i ( 0 .. $#CONNSCHED::config
)
180 next if ( $CONNSCHED::config
[$i]{'ACTIVE'} ne 'on' );
182 my $action_hour = substr($CONNSCHED::config
[$i]{'TIME'},0,2);
183 my $action_minute = substr($CONNSCHED::config
[$i]{'TIME'},3,2);
185 next if ( $action_hour != $hour );
186 next if ( $action_minute != $minute );
188 if ( $CONNSCHED::config
[$i]{'DAYSTYPE'} eq 'days' )
190 my @temp = split(/-/,$CONNSCHED::config
[$i]{'DAYS'},2);
192 my $daystart = substr($temp[0], 0, -1);
193 my $dayend = substr($temp[1], 1);
195 next if ( ($day < $daystart) || ($day > $dayend) );
199 next if ( index($CONNSCHED::config
[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays
[$weekday]) == -1 );
203 if ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reconnect' )
207 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'dial' )
211 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'hangup' )
215 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'select profile' )
217 &profile
($CONNSCHED::config
[$i]{'PROFILENR'});
219 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reboot' )
221 &General
::log("ConnSched reboot");
222 system ("/usr/local/bin/ipfirereboot", "boot");
224 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'shutdown' )
226 &General
::log("ConnSched shutdown");
227 system ("/usr/local/bin/ipfirereboot", "down");
229 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstart' )
231 &General
::log("ConnSched ipsecstart");
232 system ("/usr/local/bin/ipsecctrl", "S");
234 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstop' )
236 &General
::log("ConnSched ipsecstop");
237 system ("/usr/local/bin/ipsecctrl", "D");
241 # okay ? an event we don't know about