]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/scripts/connscheduler
Changes for serial console support
[people/pmueller/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{
f8036c87
AF
66 # Kill connectd if running to prevent redial
67 system('/bin/killall', 'connectd');
68
4e565351
MT
69 unless ( -e "${General::swroot}/red/active" )
70 {
71 &General::log("ConnSched already disconnected");
72 return;
73 }
74
75 &General::log("ConnSched disconnect");
10b0c9a0 76 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
4e565351
MT
77 {
78 &General::log("ConnSched disconnect failed: $?");
79 return;
80 }
81
67c532f5 82 # now wait for active triggerfile and ppp daemon to disappear
ed35052a
AF
83 # wait maximum 60 seconds
84 my $counter = 60;
4e565351 85 sleep 1;
67c532f5 86 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
4e565351
MT
87 {
88 sleep 1;
ed35052a 89 $counter--;
4e565351
MT
90 }
91}
92
93
94sub dial
95{
96 if ( -e "${General::swroot}/red/active" )
97 {
98 &General::log("ConnSched already connected");
99 return;
100 }
101
102 &General::log("ConnSched connect");
10b0c9a0 103 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
4e565351
MT
104 {
105 &General::log("ConnSched connect failed: $?");
106 return;
107 }
108
109 # wait maximum 60 seconds for active triggerfile
110 my $counter = 60;
111 until ( -e "${General::swroot}/red/active" || $counter == 0 )
112 {
113 sleep 1;
114 $counter--;
115 }
116}
117
118
119sub reconnect
120{
121 &hangup() if ( -e "${General::swroot}/red/active" );
ed35052a
AF
122 # now wait for active triggerfile and ppp daemon to disappear
123 # wait maximum 60 seconds
124 my $counter = 60;
125 sleep 1;
67c532f5 126 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
ed35052a
AF
127 {
128 sleep 1;
129 $counter--;
130 }
67c532f5 131 /etc/init.d/collectd stop
4e565351 132 &dial();
67c532f5 133 /etc/init.d/collectd start
4e565351
MT
134}
135
136
137sub profile
138{
139 my $profile = shift;
140 my $restart_red = 0;
141
142 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles) )
143 {
144 &General::log("ConnSched invalid profile: $profile");
145 return;
146 }
147
148 unless ( -e "${General::swroot}/ppp/settings-$profile" )
149 {
150 &General::log("ConnSched profile file does not exist: $profile");
151 return;
152 }
153
154 if ( -e "${General::swroot}/red/active" )
155 {
156 # remember to restart red after changing profile
157 $restart_red = 1;
158 &hangup();
159 }
160
161 &General::log("ConnSched select profile: $profile");
162
163 # Method to change Profile from pppsetup.cgi
164 unlink("${General::swroot}/ppp/settings");
165 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
24249567 166 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
4e565351
MT
167
168 if ( $restart_red == 1 )
169 {
170 ## FIXME: do we need to do this ?
171 sleep($sleep_after_profile);
172 &dial();
67c532f5 173 }
4e565351
MT
174}
175
176
177# fcronjob entry
178sub timer
179{
180 for my $i ( 0 .. $#CONNSCHED::config )
181 {
182 next if ( $CONNSCHED::config[$i]{'ACTIVE'} ne 'on' );
183
184 my $action_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2);
185 my $action_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2);
186
187 next if ( $action_hour != $hour );
188 next if ( $action_minute != $minute );
189
190 if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' )
191 {
192 my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2);
193
194 my $daystart = substr($temp[0], 0, -1);
195 my $dayend = substr($temp[1], 1);
196
197 next if ( ($day < $daystart) || ($day > $dayend) );
198 }
199 else
200 {
201 next if ( index($CONNSCHED::config[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays[$weekday]) == -1 );
202 }
203
204
67c532f5 205 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' )
4e565351
MT
206 {
207 &reconnect()
208 }
209 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'dial' )
210 {
211 &dial();
212 }
213 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'hangup' )
214 {
215 &hangup();
216 }
217 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' )
218 {
219 &profile($CONNSCHED::config[$i]{'PROFILENR'});
220 }
221 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'reboot' )
222 {
223 &General::log("ConnSched reboot");
224 system ("/usr/local/bin/ipfirereboot", "boot");
225 }
226 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'shutdown' )
227 {
228 &General::log("ConnSched shutdown");
229 system ("/usr/local/bin/ipfirereboot", "down");
230 }
957fb958
MT
231 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstart' )
232 {
233 &General::log("ConnSched ipsecstart");
234 system ("/usr/local/bin/ipsecctrl", "S");
235 }
236 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstop' )
237 {
238 &General::log("ConnSched ipsecstop");
239 system ("/usr/local/bin/ipsecctrl", "D");
240 }
241 else
242 {
243 # okay ? an event we don't know about
244 }
4e565351
MT
245 }
246}