]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/scripts/connscheduler
HinzugefĆ¼gt:
[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 #
7
8 use strict;
9
10 require '/var/ipfire/general-functions.pl';
11 require '/var/ipfire/connscheduler/lib.pl';
12
13
14 # seems to be necessary
15 my $sleep_after_profile = 5;
16
17 my ($second, $minute, $hour, $day, $month ,$year, $weekday) = localtime(time);
18 # correction for weekday, I am used to weeks starting with Monday (= 0) ;-)
19 $weekday = ($weekday + 6) % 7;
20 # get the closest thing possible
21 $minute = int($minute / 5) * 5;
22
23
24 if ( $ARGV[0] eq 'hangup' )
25 {
26 &hangup();
27 }
28 elsif ( $ARGV[0] eq 'dial' )
29 {
30 &dial();
31 }
32 elsif ( $ARGV[0] eq 'reconnect' )
33 {
34 &reconnect();
35 }
36 elsif ( $ARGV[0] eq 'profile' )
37 {
38 &profile($ARGV[1]);
39 }
40 elsif ( $ARGV[0] eq 'timer' )
41 {
42 &timer();
43 }
44 elsif ( $ARGV[0] eq 'test' )
45 {
46 &test();
47 }
48 else
49 {
50 print "Usage: $0 {dial | hangup | reconnect | profile nr# }\n";
51 }
52
53 exit 0;
54
55
56 # __ _ _
57 # / _| | | (_)
58 # | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
59 # | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
60 # | | | |_| | | | | (__| |_| | (_) | | | \__ \
61 # |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
62 #
63 sub hangup
64 {
65 unless ( -e "${General::swroot}/red/active" )
66 {
67 &General::log("ConnSched already disconnected");
68 return;
69 }
70
71 &General::log("ConnSched disconnect");
72 unless ( system('/etc/rc.d/rc.red', 'stop') == 0 )
73 {
74 &General::log("ConnSched disconnect failed: $?");
75 return;
76 }
77
78 # now wait for active triggerfile and ppp daemon to disappear
79 sleep 1;
80 while ( -e "${General::swroot}/red/active" || -e '/var/run/ppp-ipcop.pid' )
81 {
82 sleep 1;
83 }
84 }
85
86
87 sub dial
88 {
89 if ( -e "${General::swroot}/red/active" )
90 {
91 &General::log("ConnSched already connected");
92 return;
93 }
94
95 &General::log("ConnSched connect");
96 unless ( system('/etc/rc.d/rc.red', 'start') == 0 )
97 {
98 &General::log("ConnSched connect failed: $?");
99 return;
100 }
101
102 # wait maximum 60 seconds for active triggerfile
103 my $counter = 60;
104 until ( -e "${General::swroot}/red/active" || $counter == 0 )
105 {
106 sleep 1;
107 $counter--;
108 }
109 }
110
111
112 sub reconnect
113 {
114 &hangup() if ( -e "${General::swroot}/red/active" );
115 &dial();
116 }
117
118
119 sub profile
120 {
121 my $profile = shift;
122 my $restart_red = 0;
123
124 unless ( ($profile > 0) and ($profile < $CONNSCHED::maxprofiles) )
125 {
126 &General::log("ConnSched invalid profile: $profile");
127 return;
128 }
129
130 unless ( -e "${General::swroot}/ppp/settings-$profile" )
131 {
132 &General::log("ConnSched profile file does not exist: $profile");
133 return;
134 }
135
136 if ( -e "${General::swroot}/red/active" )
137 {
138 # remember to restart red after changing profile
139 $restart_red = 1;
140 &hangup();
141 }
142
143 &General::log("ConnSched select profile: $profile");
144
145 # Method to change Profile from pppsetup.cgi
146 unlink("${General::swroot}/ppp/settings");
147 link("${General::swroot}/ppp/settings-$profile", "${General::swroot}/ppp/settings");
148 system ("/bin/touch", "${General::swroot}/ppp/updatesettings");
149
150 if ( $restart_red == 1 )
151 {
152 ## FIXME: do we need to do this ?
153 sleep($sleep_after_profile);
154 &dial();
155 }
156 }
157
158
159 # fcronjob entry
160 sub timer
161 {
162 for my $i ( 0 .. $#CONNSCHED::config )
163 {
164 next if ( $CONNSCHED::config[$i]{'ACTIVE'} ne 'on' );
165
166 my $action_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2);
167 my $action_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2);
168
169 next if ( $action_hour != $hour );
170 next if ( $action_minute != $minute );
171
172 if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' )
173 {
174 my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2);
175
176 my $daystart = substr($temp[0], 0, -1);
177 my $dayend = substr($temp[1], 1);
178
179 next if ( ($day < $daystart) || ($day > $dayend) );
180 }
181 else
182 {
183 next if ( index($CONNSCHED::config[$i]{'WEEKDAYS'}, $CONNSCHED::weekdays[$weekday]) == -1 );
184 }
185
186
187 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'reconnect' )
188 {
189 &reconnect()
190 }
191 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'dial' )
192 {
193 &dial();
194 }
195 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'hangup' )
196 {
197 &hangup();
198 }
199 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' )
200 {
201 &profile($CONNSCHED::config[$i]{'PROFILENR'});
202 }
203 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'reboot' )
204 {
205 &General::log("ConnSched reboot");
206 system ("/usr/local/bin/ipfirereboot", "boot");
207 }
208 elsif ( $CONNSCHED::config[$i]{'ACTION'} eq 'shutdown' )
209 {
210 &General::log("ConnSched shutdown");
211 system ("/usr/local/bin/ipfirereboot", "down");
212 }
213 }
214 }