]>
git.ipfire.org Git - 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
10 require '/var/ipfire/general-functions.pl';
11 require '/var/ipfire/connscheduler/lib.pl';
14 # seems to be necessary
15 my $sleep_after_profile = 5;
17 my ($second, $minute, $hour, $day, $month ,$year, $weekday) = localtime(time);
18 # correction for weekday, I am used to weeks starting with Monday (= 0) ;-)
19 $weekday = ($weekday + 6) % 7;
20 # get the closest thing possible
21 $minute = int($minute / 5) * 5;
24 if ( $ARGV[0] eq 'hangup' )
28 elsif ( $ARGV[0] eq 'dial' )
32 elsif ( $ARGV[0] eq 'reconnect' )
36 elsif ( $ARGV[0] eq 'profile' )
40 elsif ( $ARGV[0] eq 'timer' )
44 elsif ( $ARGV[0] eq 'test' )
50 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
58 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
59 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
60 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
61 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
65 unless ( -e
"${General::swroot}/red/active" )
67 &General
::log("ConnSched already disconnected");
71 &General
::log("ConnSched disconnect");
72 unless ( system('/etc/rc.d/rc.red', 'stop') == 0 )
74 &General
::log("ConnSched disconnect failed: $?");
78 # now wait for active triggerfile and ppp daemon to disappear
80 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipcop.pid' )
89 if ( -e
"${General::swroot}/red/active" )
91 &General
::log("ConnSched already connected");
95 &General
::log("ConnSched connect");
96 unless ( system('/etc/rc.d/rc.red', 'start') == 0 )
98 &General
::log("ConnSched connect failed: $?");
102 # wait maximum 60 seconds for active triggerfile
104 until ( -e
"${General::swroot}/red/active" || $counter == 0 )
114 &hangup
() if ( -e
"${General::swroot}/red/active" );
124 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles
) )
126 &General
::log("ConnSched invalid profile: $profile");
130 unless ( -e
"${General::swroot}/ppp/settings-$profile" )
132 &General
::log("ConnSched profile file does not exist: $profile");
136 if ( -e
"${General::swroot}/red/active" )
138 # remember to restart red after changing profile
143 &General
::log("ConnSched select profile: $profile");
145 # Method to change Profile from pppsetup.cgi
146 unlink("${General::swroot}/ppp/settings");
147 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
148 system ("/bin/touch", "${General::swroot}/ppp/updatesettings");
150 if ( $restart_red == 1 )
152 ## FIXME: do we need to do this ?
153 sleep($sleep_after_profile);
162 for my $i ( 0 .. $#CONNSCHED::config
)
164 next if ( $CONNSCHED::config
[$i]{'ACTIVE'} ne 'on' );
166 my $action_hour = substr($CONNSCHED::config
[$i]{'TIME'},0,2);
167 my $action_minute = substr($CONNSCHED::config
[$i]{'TIME'},3,2);
169 next if ( $action_hour != $hour );
170 next if ( $action_minute != $minute );
172 if ( $CONNSCHED::config
[$i]{'DAYSTYPE'} eq 'days' )
174 my @temp = split(/-/,$CONNSCHED::config
[$i]{'DAYS'},2);
176 my $daystart = substr($temp[0], 0, -1);
177 my $dayend = substr($temp[1], 1);
179 next if ( ($day < $daystart) || ($day > $dayend) );
183 next if ( index($CONNSCHED::config
[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays
[$weekday]) == -1 );
187 if ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reconnect' )
191 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'dial' )
195 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'hangup' )
199 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'select profile' )
201 &profile
($CONNSCHED::config
[$i]{'PROFILENR'});
203 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reboot' )
205 &General
::log("ConnSched reboot");
206 system ("/usr/local/bin/ipfirereboot", "boot");
208 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'shutdown' )
210 &General
::log("ConnSched shutdown");
211 system ("/usr/local/bin/ipfirereboot", "down");
213 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstart' )
215 &General
::log("ConnSched ipsecstart");
216 system ("/usr/local/bin/ipsecctrl", "S");
218 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstop' )
220 &General
::log("ConnSched ipsecstop");
221 system ("/usr/local/bin/ipsecctrl", "D");
225 # okay ? an event we don't know about