]> git.ipfire.org Git - ipfire-2.x.git/blame - src/scripts/connscheduler
Fixed connscheduler reconnecting even if not pppoe.
[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
b68117f5
CS
24my $swroot = "/var/ipfire";
25my %ethernetsettings;
26&readhash("${swroot}/ethernet/settings", \%ethernetsettings);
4e565351
MT
27
28if ( $ARGV[0] eq 'hangup' )
29{
30 &hangup();
31}
32elsif ( $ARGV[0] eq 'dial' )
33{
34 &dial();
35}
36elsif ( $ARGV[0] eq 'reconnect' )
37{
38 &reconnect();
39}
40elsif ( $ARGV[0] eq 'profile' )
41{
42 &profile($ARGV[1]);
43}
44elsif ( $ARGV[0] eq 'timer' )
45{
46 &timer();
47}
48elsif ( $ARGV[0] eq 'test' )
49{
50 &test();
51}
52else
53{
54 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
55}
56
57exit 0;
58
59
60# __ _ _
61# / _| | | (_)
62# | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
63# | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
64# | | | |_| | | | | (__| |_| | (_) | | | \__ \
65# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
66#
67sub hangup
68{
f8036c87
AF
69 # Kill connectd if running to prevent redial
70 system('/bin/killall', 'connectd');
71
4e565351
MT
72 unless ( -e "${General::swroot}/red/active" )
73 {
74 &General::log("ConnSched already disconnected");
75 return;
76 }
77
78 &General::log("ConnSched disconnect");
10b0c9a0 79 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
4e565351
MT
80 {
81 &General::log("ConnSched disconnect failed: $?");
82 return;
83 }
84
ca530b2d 85 # now wait for active triggerfile and ppp daemon to disappear
ed35052a
AF
86 # wait maximum 60 seconds
87 my $counter = 60;
4e565351 88 sleep 1;
ca530b2d 89 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
4e565351
MT
90 {
91 sleep 1;
ed35052a 92 $counter--;
4e565351
MT
93 }
94}
95
96
97sub dial
98{
99 if ( -e "${General::swroot}/red/active" )
100 {
101 &General::log("ConnSched already connected");
102 return;
103 }
104
105 &General::log("ConnSched connect");
10b0c9a0 106 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
4e565351
MT
107 {
108 &General::log("ConnSched connect failed: $?");
109 return;
110 }
111
112 # wait maximum 60 seconds for active triggerfile
113 my $counter = 60;
114 until ( -e "${General::swroot}/red/active" || $counter == 0 )
115 {
116 sleep 1;
117 $counter--;
118 }
119}
120
121
122sub reconnect
b68117f5
CS
123{
124 if ( $ethernetsettings{'RED_TYPE'} ne 'PPPOE' ){
125 &General::log("ConnSched skipped because not running pppoe");
126 exit;
127 }
4e565351 128 &hangup() if ( -e "${General::swroot}/red/active" );
ed35052a
AF
129 # now wait for active triggerfile and ppp daemon to disappear
130 # wait maximum 60 seconds
131 my $counter = 60;
132 sleep 1;
ca530b2d 133 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
ed35052a
AF
134 {
135 sleep 1;
136 $counter--;
137 }
4e565351
MT
138 &dial();
139}
140
141
142sub profile
143{
144 my $profile = shift;
145 my $restart_red = 0;
146
147 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles) )
148 {
149 &General::log("ConnSched invalid profile: $profile");
150 return;
151 }
152
153 unless ( -e "${General::swroot}/ppp/settings-$profile" )
154 {
155 &General::log("ConnSched profile file does not exist: $profile");
156 return;
157 }
158
159 if ( -e "${General::swroot}/red/active" )
160 {
161 # remember to restart red after changing profile
162 $restart_red = 1;
163 &hangup();
164 }
165
166 &General::log("ConnSched select profile: $profile");
167
168 # Method to change Profile from pppsetup.cgi
169 unlink("${General::swroot}/ppp/settings");
170 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
24249567 171 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
4e565351
MT
172
173 if ( $restart_red == 1 )
174 {
175 ## FIXME: do we need to do this ?
176 sleep($sleep_after_profile);
177 &dial();
ca530b2d 178 }
4e565351
MT
179}
180
181
182# fcronjob entry
183sub timer
184{
185 for my $i ( 0 .. $#CONNSCHED::config )
186 {
187 next if ( $CONNSCHED::config[$i]{'ACTIVE'} ne 'on' );
188
189 my $action_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2);
190 my $action_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2);
191
192 next if ( $action_hour != $hour );
193 next if ( $action_minute != $minute );
194
195 if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' )
196 {
197 my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2);
198
199 my $daystart = substr($temp[0], 0, -1);
200 my $dayend = substr($temp[1], 1);
201
202 next if ( ($day < $daystart) || ($day > $dayend) );
203 }
204 else
205 {
206 next if ( index($CONNSCHED::config[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays[$weekday]) == -1 );
207 }
208
209
ca530b2d 210 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' )
4e565351
MT
211 {
212 &reconnect()
213 }
214 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'dial' )
215 {
216 &dial();
217 }
218 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'hangup' )
219 {
220 &hangup();
221 }
222 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' )
223 {
224 &profile($CONNSCHED::config[$i]{'PROFILENR'});
225 }
226 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'reboot' )
227 {
228 &General::log("ConnSched reboot");
229 system ("/usr/local/bin/ipfirereboot", "boot");
230 }
231 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'shutdown' )
232 {
233 &General::log("ConnSched shutdown");
234 system ("/usr/local/bin/ipfirereboot", "down");
235 }
957fb958
MT
236 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstart' )
237 {
238 &General::log("ConnSched ipsecstart");
239 system ("/usr/local/bin/ipsecctrl", "S");
240 }
241 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstop' )
242 {
243 &General::log("ConnSched ipsecstop");
244 system ("/usr/local/bin/ipsecctrl", "D");
245 }
246 else
247 {
248 # okay ? an event we don't know about
249 }
4e565351
MT
250 }
251}
b68117f5
CS
252
253sub readhash
254{
255 my $filename = $_[0];
256 my $hash = $_[1];
257 my ($var, $val);
258
259 if (-e $filename)
260 {
261 open(FILE, $filename) or die "Unable to read file $filename";
262 while (<FILE>)
263 {
264 chop;
265 ($var, $val) = split /=/, $_, 2;
266 if ($var)
267 {
268 $val =~ s/^\'//g;
269 $val =~ s/\'$//g;
270
271 # Untaint variables read from hash
272 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
273 $val =~ /([\w\W]*)/; $val = $1;
274 $hash->{$var} = $val;
275 }
276 }
277 close FILE;
278 }
279}