]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/connscheduler
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
24 require '/var/ipfire/general-functions.pl';
25 require '/var/ipfire/connscheduler/lib.pl';
27 # seems to be necessary
28 my $sleep_after_profile = 5;
30 my ($second, $minute, $hour, $day, $month ,$year, $weekday) = localtime(time);
31 # correction for weekday, I am used to weeks starting with Monday (= 0) ;-)
32 $weekday = ($weekday + 6) % 7;
33 # get the closest thing possible
34 $minute = int($minute / 5) * 5;
37 if ( $ARGV[0] eq 'hangup' )
41 elsif ( $ARGV[0] eq 'dial' )
45 elsif ( $ARGV[0] eq 'reconnect' )
49 elsif ( $ARGV[0] eq 'profile' )
53 elsif ( $ARGV[0] eq 'timer' )
57 elsif ( $ARGV[0] eq 'test' )
63 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
71 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
72 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
73 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
74 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
78 # Kill connectd if running to prevent redial
79 system('/bin/killall', 'connectd');
81 unless ( -e
"${General::swroot}/red/active" )
83 &General
::log("ConnSched already disconnected");
87 &General
::log("ConnSched disconnect");
88 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
90 &General
::log("ConnSched disconnect failed: $?");
94 # now wait for active triggerfile and ppp daemon to disappear
95 # wait maximum 60 seconds
98 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
108 if ( -e
"${General::swroot}/red/active" )
110 &General
::log("ConnSched already connected");
114 &General
::log("ConnSched connect");
115 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
117 &General
::log("ConnSched connect failed: $?");
121 # wait maximum 60 seconds for active triggerfile
123 until ( -e
"${General::swroot}/red/active" || $counter == 0 )
133 &hangup
() if ( -e
"${General::swroot}/red/active" );
134 # now wait for active triggerfile and ppp daemon to disappear
135 # wait maximum 60 seconds
138 while ( -e
"${General::swroot}/red/active" || -e
'/var/run/ppp-ipfire.pid' || $counter == 0 )
152 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles
) )
154 &General
::log("ConnSched invalid profile: $profile");
158 unless ( -e
"${General::swroot}/ppp/settings-$profile" )
160 &General
::log("ConnSched profile file does not exist: $profile");
164 if ( -e
"${General::swroot}/red/active" )
166 # remember to restart red after changing profile
171 &General
::log("ConnSched select profile: $profile");
173 # Method to change Profile from pppsetup.cgi
174 unlink("${General::swroot}/ppp/settings");
175 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
176 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
178 if ( $restart_red == 1 )
180 ## FIXME: do we need to do this ?
181 sleep($sleep_after_profile);
190 for my $i ( 0 .. $#CONNSCHED::config
)
192 next if ( $CONNSCHED::config
[$i]{'ACTIVE'} ne 'on' );
194 my $action_hour = substr($CONNSCHED::config
[$i]{'TIME'},0,2);
195 my $action_minute = substr($CONNSCHED::config
[$i]{'TIME'},3,2);
197 next if ( $action_hour != $hour );
198 next if ( $action_minute != $minute );
200 if ( $CONNSCHED::config
[$i]{'DAYSTYPE'} eq 'days' )
202 my @temp = split(/-/,$CONNSCHED::config
[$i]{'DAYS'},2);
204 my $daystart = substr($temp[0], 0, -1);
205 my $dayend = substr($temp[1], 1);
207 next if ( ($day < $daystart) || ($day > $dayend) );
211 next if ( index($CONNSCHED::config
[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays
[$weekday]) == -1 );
215 if ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reconnect' )
219 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'dial' )
223 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'hangup' )
227 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'select profile' )
229 &profile
($CONNSCHED::config
[$i]{'PROFILENR'});
231 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'reboot' )
233 &General
::log("ConnSched reboot");
234 system ("/usr/local/bin/ipfirereboot", "boot");
236 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'shutdown' )
238 &General
::log("ConnSched shutdown");
239 system ("/usr/local/bin/ipfirereboot", "down");
241 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstart' )
243 &General
::log("ConnSched ipsecstart");
244 system ("/usr/local/bin/ipsecctrl", "S");
246 elsif ( $CONNSCHED::config
[$i]{'ACTION'} eq 'ipsecstop' )
248 &General
::log("ConnSched ipsecstop");
249 system ("/usr/local/bin/ipsecctrl", "D");
253 # okay ? an event we don't know about