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