]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | use strict; | |
11 | ||
12 | require '/var/ipfire/general-functions.pl'; | |
13 | require '/var/ipfire/connscheduler/lib.pl'; | |
14 | ||
4e565351 MT |
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 | ||
25 | if ( $ARGV[0] eq 'hangup' ) | |
26 | { | |
27 | &hangup(); | |
28 | } | |
29 | elsif ( $ARGV[0] eq 'dial' ) | |
30 | { | |
31 | &dial(); | |
32 | } | |
33 | elsif ( $ARGV[0] eq 'reconnect' ) | |
34 | { | |
35 | &reconnect(); | |
36 | } | |
37 | elsif ( $ARGV[0] eq 'profile' ) | |
38 | { | |
39 | &profile($ARGV[1]); | |
40 | } | |
41 | elsif ( $ARGV[0] eq 'timer' ) | |
42 | { | |
43 | &timer(); | |
44 | } | |
45 | elsif ( $ARGV[0] eq 'test' ) | |
46 | { | |
47 | &test(); | |
48 | } | |
49 | else | |
50 | { | |
51 | print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n"; | |
52 | } | |
53 | ||
54 | exit 0; | |
55 | ||
56 | ||
57 | # __ _ _ | |
58 | # / _| | | (_) | |
59 | # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___ | |
60 | # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __| | |
61 | # | | | |_| | | | | (__| |_| | (_) | | | \__ \ | |
62 | # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ | |
63 | # | |
64 | sub 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 | ||
ca530b2d | 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; |
ca530b2d | 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 | ||
94 | sub 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 | ||
119 | sub reconnect | |
be40a5b1 | 120 | { |
4e565351 | 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; | |
ca530b2d | 126 | while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 ) |
ed35052a AF |
127 | { |
128 | sleep 1; | |
129 | $counter--; | |
130 | } | |
4e565351 MT |
131 | &dial(); |
132 | } | |
133 | ||
134 | ||
135 | sub profile | |
136 | { | |
137 | my $profile = shift; | |
138 | my $restart_red = 0; | |
139 | ||
140 | unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles) ) | |
141 | { | |
142 | &General::log("ConnSched invalid profile: $profile"); | |
143 | return; | |
144 | } | |
145 | ||
146 | unless ( -e "${General::swroot}/ppp/settings-$profile" ) | |
147 | { | |
148 | &General::log("ConnSched profile file does not exist: $profile"); | |
149 | return; | |
150 | } | |
151 | ||
152 | if ( -e "${General::swroot}/red/active" ) | |
153 | { | |
154 | # remember to restart red after changing profile | |
155 | $restart_red = 1; | |
156 | &hangup(); | |
157 | } | |
158 | ||
159 | &General::log("ConnSched select profile: $profile"); | |
160 | ||
161 | # Method to change Profile from pppsetup.cgi | |
162 | unlink("${General::swroot}/ppp/settings"); | |
163 | link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings"); | |
24249567 | 164 | system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings"); |
4e565351 MT |
165 | |
166 | if ( $restart_red == 1 ) | |
167 | { | |
168 | ## FIXME: do we need to do this ? | |
169 | sleep($sleep_after_profile); | |
170 | &dial(); | |
ca530b2d | 171 | } |
4e565351 MT |
172 | } |
173 | ||
174 | ||
175 | # fcronjob entry | |
176 | sub timer | |
177 | { | |
178 | for my $i ( 0 .. $#CONNSCHED::config ) | |
179 | { | |
180 | next if ( $CONNSCHED::config[$i]{'ACTIVE'} ne 'on' ); | |
181 | ||
182 | my $action_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2); | |
183 | my $action_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2); | |
184 | ||
185 | next if ( $action_hour != $hour ); | |
186 | next if ( $action_minute != $minute ); | |
187 | ||
188 | if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' ) | |
189 | { | |
190 | my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2); | |
191 | ||
192 | my $daystart = substr($temp[0], 0, -1); | |
193 | my $dayend = substr($temp[1], 1); | |
194 | ||
195 | next if ( ($day < $daystart) || ($day > $dayend) ); | |
196 | } | |
197 | else | |
198 | { | |
199 | next if ( index($CONNSCHED::config[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays[$weekday]) == -1 ); | |
200 | } | |
201 | ||
202 | ||
ca530b2d | 203 | if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' ) |
4e565351 MT |
204 | { |
205 | &reconnect() | |
206 | } | |
207 | elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'dial' ) | |
208 | { | |
209 | &dial(); | |
210 | } | |
211 | elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'hangup' ) | |
212 | { | |
213 | &hangup(); | |
214 | } | |
215 | elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' ) | |
216 | { | |
217 | &profile($CONNSCHED::config[$i]{'PROFILENR'}); | |
218 | } | |
219 | elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'reboot' ) | |
220 | { | |
221 | &General::log("ConnSched reboot"); | |
222 | system ("/usr/local/bin/ipfirereboot", "boot"); | |
223 | } | |
224 | elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'shutdown' ) | |
225 | { | |
226 | &General::log("ConnSched shutdown"); | |
227 | system ("/usr/local/bin/ipfirereboot", "down"); | |
228 | } | |
957fb958 MT |
229 | elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstart' ) |
230 | { | |
231 | &General::log("ConnSched ipsecstart"); | |
232 | system ("/usr/local/bin/ipsecctrl", "S"); | |
233 | } | |
234 | elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstop' ) | |
235 | { | |
236 | &General::log("ConnSched ipsecstop"); | |
237 | system ("/usr/local/bin/ipsecctrl", "D"); | |
238 | } | |
239 | else | |
240 | { | |
241 | # okay ? an event we don't know about | |
242 | } | |
4e565351 MT |
243 | } |
244 | } |