]> git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/connscheduler
Merge branch 'next'
[ipfire-2.x.git] / src / scripts / connscheduler
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 require '/var/ipfire/general-functions.pl';
25 require '/var/ipfire/connscheduler/lib.pl';
26
27 # seems to be necessary
28 my $sleep_after_profile = 5;
29
30 my ($second, $minute, $hour, $day, $month ,$year, $weekday) = localtime(time);
31 # correction for weekday, I am used to weeks starting with Monday (= 0) ;-)
32 $weekday = ($weekday + 6) % 7;
33 # get the closest thing possible
34 $minute = int($minute / 5) * 5;
35
36
37 if ( $ARGV[0] eq 'hangup' )
38 {
39 &hangup();
40 }
41 elsif ( $ARGV[0] eq 'dial' )
42 {
43 &dial();
44 }
45 elsif ( $ARGV[0] eq 'reconnect' )
46 {
47 &reconnect();
48 }
49 elsif ( $ARGV[0] eq 'profile' )
50 {
51 &profile($ARGV[1]);
52 }
53 elsif ( $ARGV[0] eq 'timer' )
54 {
55 &timer();
56 }
57 elsif ( $ARGV[0] eq 'test' )
58 {
59 &test();
60 }
61 else
62 {
63 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
64 }
65
66 exit 0;
67
68
69 # __ _ _
70 # / _| | | (_)
71 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
72 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
73 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
74 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
75 #
76 sub hangup
77 {
78 # Kill connectd if running to prevent redial
79 system('/bin/killall', 'connectd');
80
81 unless ( -e "${General::swroot}/red/active" )
82 {
83 &General::log("ConnSched already disconnected");
84 return;
85 }
86
87 &General::log("ConnSched disconnect");
88 unless ( system('/etc/rc.d/init.d/network', 'stop', 'red') == 0 )
89 {
90 &General::log("ConnSched disconnect failed: $?");
91 return;
92 }
93
94 # now wait for active triggerfile and ppp daemon to disappear
95 # wait maximum 60 seconds
96 my $counter = 60;
97 sleep 1;
98 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
99 {
100 sleep 1;
101 $counter--;
102 }
103 }
104
105
106 sub dial
107 {
108 if ( -e "${General::swroot}/red/active" )
109 {
110 &General::log("ConnSched already connected");
111 return;
112 }
113
114 &General::log("ConnSched connect");
115 unless ( system('/etc/rc.d/init.d/network', 'start', 'red') == 0 )
116 {
117 &General::log("ConnSched connect failed: $?");
118 return;
119 }
120
121 # wait maximum 60 seconds for active triggerfile
122 my $counter = 60;
123 until ( -e "${General::swroot}/red/active" || $counter == 0 )
124 {
125 sleep 1;
126 $counter--;
127 }
128 }
129
130
131 sub reconnect
132 {
133 &hangup() if ( -e "${General::swroot}/red/active" );
134 # now wait for active triggerfile and ppp daemon to disappear
135 # wait maximum 60 seconds
136 my $counter = 60;
137 sleep 1;
138 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipfire.pid' || $counter == 0 )
139 {
140 sleep 1;
141 $counter--;
142 }
143 &dial();
144 }
145
146
147 sub profile
148 {
149 my $profile = shift;
150 my $restart_red = 0;
151
152 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles) )
153 {
154 &General::log("ConnSched invalid profile: $profile");
155 return;
156 }
157
158 unless ( -e "${General::swroot}/ppp/settings-$profile" )
159 {
160 &General::log("ConnSched profile file does not exist: $profile");
161 return;
162 }
163
164 if ( -e "${General::swroot}/red/active" )
165 {
166 # remember to restart red after changing profile
167 $restart_red = 1;
168 &hangup();
169 }
170
171 &General::log("ConnSched select profile: $profile");
172
173 # Method to change Profile from pppsetup.cgi
174 unlink("${General::swroot}/ppp/settings");
175 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
176 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
177
178 if ( $restart_red == 1 )
179 {
180 ## FIXME: do we need to do this ?
181 sleep($sleep_after_profile);
182 &dial();
183 }
184 }
185
186
187 # fcronjob entry
188 sub timer
189 {
190 for my $i ( 0 .. $#CONNSCHED::config )
191 {
192 next if ( $CONNSCHED::config[$i]{'ACTIVE'} ne 'on' );
193
194 my $action_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2);
195 my $action_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2);
196
197 next if ( $action_hour != $hour );
198 next if ( $action_minute != $minute );
199
200 if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' )
201 {
202 my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2);
203
204 my $daystart = substr($temp[0], 0, -1);
205 my $dayend = substr($temp[1], 1);
206
207 next if ( ($day < $daystart) || ($day > $dayend) );
208 }
209 else
210 {
211 next if ( index($CONNSCHED::config[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays[$weekday]) == -1 );
212 }
213
214
215 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' )
216 {
217 &reconnect()
218 }
219 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'dial' )
220 {
221 &dial();
222 }
223 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'hangup' )
224 {
225 &hangup();
226 }
227 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' )
228 {
229 &profile($CONNSCHED::config[$i]{'PROFILENR'});
230 }
231 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'reboot' )
232 {
233 &General::log("ConnSched reboot");
234 system ("/usr/local/bin/ipfirereboot", "boot");
235 }
236 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'shutdown' )
237 {
238 &General::log("ConnSched shutdown");
239 system ("/usr/local/bin/ipfirereboot", "down");
240 }
241 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstart' )
242 {
243 &General::log("ConnSched ipsecstart");
244 system ("/usr/local/bin/ipsecctrl", "S");
245 }
246 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'ipsecstop' )
247 {
248 &General::log("ConnSched ipsecstop");
249 system ("/usr/local/bin/ipsecctrl", "D");
250 }
251 else
252 {
253 # okay ? an event we don't know about
254 }
255 }
256 }