]> git.ipfire.org Git - ipfire-2.x.git/blame - src/scripts/connscheduler
Upgraded some packs
[ipfire-2.x.git] / src / scripts / connscheduler
CommitLineData
4e565351
MT
1#!/usr/bin/perl
2#
3# IPFire Connection Scheduler (F)Cron Job
4#
5# This code is distributed under the terms of the GPL
0e199a22
MT
6# The original code is taken from weizen_42.
7# See /home/httpd/cgi-bin/connscheduler.cgi
4e565351
MT
8#
9
10use strict;
11
12require '/var/ipfire/general-functions.pl';
13require '/var/ipfire/connscheduler/lib.pl';
14
4e565351
MT
15# seems to be necessary
16my $sleep_after_profile = 5;
17
18my ($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;
23
24
25if ( $ARGV[0] eq 'hangup' )
26{
27 &hangup();
28}
29elsif ( $ARGV[0] eq 'dial' )
30{
31 &dial();
32}
33elsif ( $ARGV[0] eq 'reconnect' )
34{
35 &reconnect();
36}
37elsif ( $ARGV[0] eq 'profile' )
38{
39 &profile($ARGV[1]);
40}
41elsif ( $ARGV[0] eq 'timer' )
42{
43 &timer();
44}
45elsif ( $ARGV[0] eq 'test' )
46{
47 &test();
48}
49else
50{
51 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
52}
53
54exit 0;
55
56
57# __ _ _
58# / _| | | (_)
59# | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
60# | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
61# | | | |_| | | | | (__| |_| | (_) | | | \__ \
62# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
63#
64sub hangup
65{
66 unless ( -e "${General::swroot}/red/active" )
67 {
68 &General::log("ConnSched already disconnected");
69 return;
70 }
71
72 &General::log("ConnSched disconnect");
10b0c9a0 73 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
4e565351
MT
74 {
75 &General::log("ConnSched disconnect failed: $?");
76 return;
77 }
78
79 # now wait for active triggerfile and ppp daemon to disappear
ed35052a
AF
80 # wait maximum 60 seconds
81 my $counter = 60;
4e565351 82 sleep 1;
ed35052a 83 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
4e565351
MT
84 {
85 sleep 1;
ed35052a 86 $counter--;
4e565351
MT
87 }
88}
89
90
91sub dial
92{
93 if ( -e "${General::swroot}/red/active" )
94 {
95 &General::log("ConnSched already connected");
96 return;
97 }
98
99 &General::log("ConnSched connect");
10b0c9a0 100 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
4e565351
MT
101 {
102 &General::log("ConnSched connect failed: $?");
103 return;
104 }
105
106 # wait maximum 60 seconds for active triggerfile
107 my $counter = 60;
108 until ( -e "${General::swroot}/red/active" || $counter == 0 )
109 {
110 sleep 1;
111 $counter--;
112 }
113}
114
115
116sub reconnect
117{
118 &hangup() if ( -e "${General::swroot}/red/active" );
ed35052a
AF
119 # now wait for active triggerfile and ppp daemon to disappear
120 # wait maximum 60 seconds
121 my $counter = 60;
122 sleep 1;
123 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
124 {
125 sleep 1;
126 $counter--;
127 }
4e565351
MT
128 &dial();
129}
130
131
132sub profile
133{
134 my $profile = shift;
135 my $restart_red = 0;
136
137 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles) )
138 {
139 &General::log("ConnSched invalid profile: $profile");
140 return;
141 }
142
143 unless ( -e "${General::swroot}/ppp/settings-$profile" )
144 {
145 &General::log("ConnSched profile file does not exist: $profile");
146 return;
147 }
148
149 if ( -e "${General::swroot}/red/active" )
150 {
151 # remember to restart red after changing profile
152 $restart_red = 1;
153 &hangup();
154 }
155
156 &General::log("ConnSched select profile: $profile");
157
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");
24249567 161 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
4e565351
MT
162
163 if ( $restart_red == 1 )
164 {
165 ## FIXME: do we need to do this ?
166 sleep($sleep_after_profile);
167 &dial();
168 }
169}
170
171
172# fcronjob entry
173sub timer
174{
175 for my $i ( 0 .. $#CONNSCHED::config )
176 {
177 next if ( $CONNSCHED::config[$i]{'ACTIVE'} ne 'on' );
178
179 my $action_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2);
180 my $action_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2);
181
182 next if ( $action_hour != $hour );
183 next if ( $action_minute != $minute );
184
185 if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' )
186 {
187 my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2);
188
189 my $daystart = substr($temp[0], 0, -1);
190 my $dayend = substr($temp[1], 1);
191
192 next if ( ($day < $daystart) || ($day > $dayend) );
193 }
194 else
195 {
196 next if ( index($CONNSCHED::config[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays[$weekday]) == -1 );
197 }
198
199
200 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' )
201 {
202 &reconnect()
203 }
204 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'dial' )
205 {
206 &dial();
207 }
208 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'hangup' )
209 {
210 &hangup();
211 }
212 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' )
213 {
214 &profile($CONNSCHED::config[$i]{'PROFILENR'});
215 }
216 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'reboot' )
217 {
218 &General::log("ConnSched reboot");
219 system ("/usr/local/bin/ipfirereboot", "boot");
220 }
221 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'shutdown' )
222 {
223 &General::log("ConnSched shutdown");
224 system ("/usr/local/bin/ipfirereboot", "down");
225 }
957fb958
MT
226 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstart' )
227 {
228 &General::log("ConnSched ipsecstart");
229 system ("/usr/local/bin/ipsecctrl", "S");
230 }
231 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstop' )
232 {
233 &General::log("ConnSched ipsecstop");
234 system ("/usr/local/bin/ipsecctrl", "D");
235 }
236 else
237 {
238 # okay ? an event we don't know about
239 }
4e565351
MT
240 }
241}