]>
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
81 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' )
90 if ( -e
"${General::swroot}/red/active" )
92 &General
::log("ConnSched already connected");
96 &General
::log("ConnSched connect");
97 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
99 &General
::log("ConnSched connect failed: $?");
103 # wait maximum 60 seconds for active triggerfile
105 until ( -e
"${General::swroot}/red/active" || $counter == 0 )
115 &hangup
() if ( -e
"${General::swroot}/red/active" );
125 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles
) )
127 &General
::log("ConnSched invalid profile: $profile");
131 unless ( -e
"${General::swroot}/ppp/settings-$profile" )
133 &General
::log("ConnSched profile file does not exist: $profile");
137 if ( -e
"${General::swroot}/red/active" )
139 # remember to restart red after changing profile
144 &General
::log("ConnSched select profile: $profile");
146 # Method to change Profile from pppsetup.cgi
147 unlink("${General::swroot}/ppp/settings");
148 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
149 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
151 if ( $restart_red == 1 )
153 ## FIXME: do we need to do this ?
154 sleep($sleep_after_profile);
163 for my $i ( 0 .. $#CONNSCHED::config
)
165 next if ( $CONNSCHED::config
[$i]{'ACTIVE'} ne 'on' );
167 my $action_hour = substr($CONNSCHED::config
[$i]{'TIME'},0,2);
168 my $action_minute = substr($CONNSCHED::config
[$i]{'TIME'},3,2);
170 next if ( $action_hour != $hour );
171 next if ( $action_minute != $minute );
173 if ( $CONNSCHED::config
[$i]{'DAYSTYPE'} eq 'days' )
175 my @temp = split(/-/,$CONNSCHED::config
[$i]{'DAYS'},2);
177 my $daystart = substr($temp[0], 0, -1);
178 my $dayend = substr($temp[1], 1);
180 next if ( ($day < $daystart) || ($day > $dayend) );
184 next if ( index($CONNSCHED::config
[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays
[$weekday]) == -1 );
188 if ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reconnect' )
192 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'dial' )
196 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'hangup' )
200 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'select profile' )
202 &profile
($CONNSCHED::config
[$i]{'PROFILENR'});
204 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reboot' )
206 &General
::log("ConnSched reboot");
207 system ("/usr/local/bin/ipfirereboot", "boot");
209 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'shutdown' )
211 &General
::log("ConnSched shutdown");
212 system ("/usr/local/bin/ipfirereboot", "down");
214 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstart' )
216 &General
::log("ConnSched ipsecstart");
217 system ("/usr/local/bin/ipsecctrl", "S");
219 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstop' )
221 &General
::log("ConnSched ipsecstop");
222 system ("/usr/local/bin/ipsecctrl", "D");
226 # okay ? an event we don't know about