ceb411601e55efbfb4f2a7fe2a11742b8917dbe6
[people/pmueller/ipfire-2.x.git] / src / scripts / connscheduler
1 #!/usr/bin/perl
2 #
3 # IPFire Connection Scheduler (F)Cron Job
4 #
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
8 #
9
10 use strict;
11
12 require '/var/ipfire/general-functions.pl';
13 require '/var/ipfire/connscheduler/lib.pl';
14
15 # seems to be necessary
16 my $sleep_after_profile = 5;
17
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;
23
24 my $swroot = "/var/ipfire";
25 my %ethernetsettings;
26 &readhash("${swroot}/ethernet/settings", \%ethernetsettings);
27
28 if ( $ARGV[0] eq 'hangup' )
29 {
30 &hangup();
31 }
32 elsif ( $ARGV[0] eq 'dial' )
33 {
34 &dial();
35 }
36 elsif ( $ARGV[0] eq 'reconnect' )
37 {
38 &reconnect();
39 }
40 elsif ( $ARGV[0] eq 'profile' )
41 {
42 &profile($ARGV[1]);
43 }
44 elsif ( $ARGV[0] eq 'timer' )
45 {
46 &timer();
47 }
48 elsif ( $ARGV[0] eq 'test' )
49 {
50 &test();
51 }
52 else
53 {
54 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
55 }
56
57 exit 0;
58
59
60 # __ _ _
61 # / _| | | (_)
62 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
63 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
64 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
65 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
66 #
67 sub hangup
68 {
69 # Kill connectd if running to prevent redial
70 system('/bin/killall', 'connectd');
71
72 unless ( -e "${General::swroot}/red/active" )
73 {
74 &General::log("ConnSched already disconnected");
75 return;
76 }
77
78 &General::log("ConnSched disconnect");
79 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
80 {
81 &General::log("ConnSched disconnect failed: $?");
82 return;
83 }
84
85 # now wait for active triggerfile and ppp daemon to disappear
86 # wait maximum 60 seconds
87 my $counter = 60;
88 sleep 1;
89 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
90 {
91 sleep 1;
92 $counter--;
93 }
94 }
95
96
97 sub 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");
106 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
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
122 sub reconnect
123 {
124 if ( $ethernetsettings{'RED_TYPE'} ne 'PPPOE' ){
125 &General::log("ConnSched skipped because not running pppoe");
126 exit;
127 }
128 &hangup() if ( -e "${General::swroot}/red/active" );
129 # now wait for active triggerfile and ppp daemon to disappear
130 # wait maximum 60 seconds
131 my $counter = 60;
132 sleep 1;
133 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
134 {
135 sleep 1;
136 $counter--;
137 }
138 &dial();
139 }
140
141
142 sub 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");
171 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
172
173 if ( $restart_red == 1 )
174 {
175 ## FIXME: do we need to do this ?
176 sleep($sleep_after_profile);
177 &dial();
178 }
179 }
180
181
182 # fcronjob entry
183 sub 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
210 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' )
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 }
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 }
250 }
251 }
252
253 sub 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 }