Timectrl geschrieben.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / time.cgi
1 #!/usr/bin/perl
2 #
3 # IPFire CGIs
4 #
5 # This file is part of the IPFire Project
6 #
7 # This code is distributed under the terms of the GPL
8 #
9 # (c) Eric Oberlander June 2002
10 #
11 # (c) Darren Critchley June 2003 - added real time clock setting, etc
12 #
13 # $Id: time.cgi,v 1.4.2.11 2005/05/28 12:16:18 eoberlander Exp $
14 #
15
16 use strict;
17
18 # enable only the following on debugging purpose
19 #use warnings;
20 #use CGI::Carp 'fatalsToBrowser';
21
22 require '/var/ipfire/general-functions.pl';
23 require "${General::swroot}/lang.pl";
24 require "${General::swroot}/header.pl";
25
26 my %timesettings=();
27 my $errormessage = '';
28
29 &Header::showhttpheaders();
30
31 $timesettings{'ACTION'} = '';
32 $timesettings{'VALID'} = '';
33
34 $timesettings{'ENABLENTP'} = 'off';
35 $timesettings{'NTP_ADDR_1'} = '';
36 $timesettings{'NTP_ADDR_2'} = '';
37 $timesettings{'UPDATE_METHOD'} = 'manually';
38 $timesettings{'UPDATE_VALUE'} = '0';
39 $timesettings{'UPDATE_PERIOD'} = '';
40 $timesettings{'ENABLECLNTP'} = 'off';
41 $timesettings{'SETHOUR'} = '';
42 $timesettings{'SETMINUTES'} = '';
43 $timesettings{'SETDAY'} = '';
44 $timesettings{'SETMONTH'} = '';
45 $timesettings{'SETYEAR'} = '';
46
47 &Header::getcgihash(\%timesettings);
48
49 if ($timesettings{'ACTION'} eq $Lang::tr{'save'})
50 {
51 if ($timesettings{'ENABLENTP'} eq 'on')
52 {
53 if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_1'}) ||
54 &General::validip ($timesettings{'NTP_ADDR_1'})))
55 {
56 $errormessage = $Lang::tr{'invalid primary ntp'};
57 goto ERROR;
58 }
59 }
60 if ($timesettings{'NTP_ADDR_2'})
61 {
62 if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_2'}) ||
63 &General::validip ($timesettings{'NTP_ADDR_2'})))
64 {
65 $errormessage = $Lang::tr{'invalid secondary ntp'};
66 goto ERROR;
67 }
68 }
69 if (!($timesettings{'NTP_ADDR_1'}) && $timesettings{'NTP_ADDR_2'})
70 {
71 $errormessage = $Lang::tr{'cannot specify secondary ntp without specifying primary'};
72 goto ERROR;
73 }
74
75 if (!($timesettings{'UPDATE_VALUE'} =~ /^\d+$/) || $timesettings{'UPDATE_VALUE'} <= 0)
76 {
77 $errormessage = $Lang::tr{'invalid time period'};
78 goto ERROR;
79 }
80
81 if ($timesettings{'ENABLENTP'} ne "on" && $timesettings{'ENABLECLNTP'} eq "on")
82 {
83 $errormessage = $Lang::tr{'ntp must be enabled to have clients'};
84 goto ERROR;
85 }
86 if ($timesettings{'ENABLENTP'} eq "on" && !($timesettings{'NTP_ADDR_1'}) && !($timesettings{'NTP_ADDR_2'}))
87 {
88 $errormessage = $Lang::tr{'cannot enable ntp without specifying primary'};
89 goto ERROR;
90 }
91 ERROR:
92 if ($errormessage) {
93 $timesettings{'VALID'} = 'no'; }
94 else {
95 $timesettings{'VALID'} = 'yes'; }
96
97 &General::writehash("${General::swroot}/time/settings", \%timesettings);
98 open(FILE, ">/${General::swroot}/time/settime.conf") or die "Unable to write settime.conf file";
99 flock(FILE, 2);
100 print FILE "$timesettings{'NTP_ADDR_1'} $timesettings{'NTP_ADDR_2'}\n";
101 close FILE;
102
103 my $updateperiod=0;
104
105 if ($timesettings{'UPDATE_PERIOD'} eq 'daily') {
106 $updateperiod = $timesettings{'UPDATE_VALUE'} * 1440; }
107 elsif ($timesettings{'UPDATE_PERIOD'} eq 'weekly') {
108 $updateperiod = $timesettings{'UPDATE_VALUE'} * 10080; }
109 elsif ($timesettings{'UPDATE_PERIOD'} eq 'monthly') {
110 $updateperiod = $timesettings{'UPDATE_VALUE'} * 40320; }
111 else {
112 $updateperiod = $timesettings{'UPDATE_VALUE'} * 60; }
113
114 $updateperiod = $updateperiod - 5;
115
116 if ($updateperiod <= 5) {
117 $updateperiod = 5; }
118
119 open(FILE, ">/${General::swroot}/time/counter.conf") or die "Unable to write counter.conf file";
120 flock(FILE, 2);
121 print FILE "$updateperiod\n";
122 close FILE;
123
124 if ($timesettings{'ENABLENTP'} eq 'on' && $timesettings{'VALID'} eq 'yes')
125 {
126 system ('/usr/bin/touch', "${General::swroot}/time/enable");
127 system ('/usr/local/bin/timectrl enable >/dev/null 2>&1');
128 &General::log($Lang::tr{'ntp syncro enabled'});
129 unlink "${General::swroot}/time/counter";
130 if ($timesettings{'UPDATE_METHOD'} eq 'periodically')
131 {
132 open(FILE, ">/${General::swroot}/time/counter") or die "Unable to write counter file";
133 flock(FILE, 2);
134 print FILE "$updateperiod\n";
135 close FILE;
136 }
137 if ($timesettings{'ENABLECLNTP'} eq 'on') # DPC added to 1.3.1
138 {
139 system ('/usr/bin/touch', "${General::swroot}/time/allowclients"); # DPC added to 1.3.1
140 &General::log($Lang::tr{'ntpd restarted'}); # DPC added to 1.3.1
141 } else {
142 unlink "${General::swroot}/time/allowclients";
143 }
144
145 }
146 else
147 {
148 unlink "${General::swroot}/time/enable";
149 unlink "${General::swroot}/time/settimenow";
150 unlink "${General::swroot}/time/allowclients"; # DPC added to 1.3.1
151 system ('/usr/local/bin/timectrl disable >/dev/null 2>&1');
152 &General::log($Lang::tr{'ntp syncro disabled'})
153 }
154 if (! $errormessage) {
155 system ('/usr/local/bin/timectrl restart >/dev/null 2>&1'); # DPC added to 1.3.1
156 }
157 }
158
159 # To enter an ' into a pushbutton solution is to use &#039; in it's definition
160 # but returned value when pressed is ' not the code. Cleanhtml recode the ' to enable comparison.
161 $timesettings{'ACTION'} = &Header::cleanhtml ($timesettings{'ACTION'});
162 if ($timesettings{'ACTION'} eq $Lang::tr{'set time now'} && $timesettings{'ENABLENTP'} eq 'on')
163 {
164 system ('/usr/bin/touch', "${General::swroot}/time/settimenow");
165 system ('/usr/local/bin/timecheck >& /dev/null');
166 }
167
168 &General::readhash("${General::swroot}/time/settings", \%timesettings);
169
170 if ($timesettings{'VALID'} eq '')
171 {
172 $timesettings{'ENABLENTP'} = 'off';
173 $timesettings{'UPDATE_METHOD'} = 'manually';
174 $timesettings{'UPDATE_VALUE'} = '1';
175 $timesettings{'UPDATE_PERIOD'} = 'daily';
176 $timesettings{'NTP_ADDR_1'} = 'de.pool.ntp.org';
177 $timesettings{'NTP_ADDR_2'} = 'pool.ntp.org';
178 }
179
180 unless ($errormessage) {
181 $timesettings{'SETMONTH'} = `date +'%m %e %Y %H %M'|cut -c 1-2`;
182 $timesettings{'SETDAY'} = `date +'%m %e %Y %H %M'|cut -c 4-5`;
183 $timesettings{'SETYEAR'} = `date +'%m %e %Y %H %M'|cut -c 7-10`;
184 $timesettings{'SETHOUR'} = `date +'%m %e %Y %H %M'|cut -c 12-13`;
185 $timesettings{'SETMINUTES'} = `date +'%m %e %Y %H %M'|cut -c 15-16`;
186 $_=$timesettings{'SETDAY'};
187 $timesettings{'SETDAY'}=~ tr/ /0/;
188 }
189
190 my %selected=();
191 my %checked=();
192
193 $checked{'ENABLENTP'}{'off'} = '';
194 $checked{'ENABLENTP'}{'on'} = '';
195 $checked{'ENABLENTP'}{$timesettings{'ENABLENTP'}} = "checked='checked'";
196
197 $checked{'ENABLECLNTP'}{'off'} = '';
198 $checked{'ENABLECLNTP'}{'on'} = '';
199 $checked{'ENABLECLNTP'}{$timesettings{'ENABLECLNTP'}} = "checked='checked'";
200
201 $checked{'UPDATE_METHOD'}{'manually'} = '';
202 $checked{'UPDATE_METHOD'}{'periodically'} = '';
203 $checked{'UPDATE_METHOD'}{$timesettings{'UPDATE_METHOD'}} = "checked='checked'";
204
205 $selected{'UPDATE_PERIOD'}{'hourly'} = '';
206 $selected{'UPDATE_PERIOD'}{'daily'} = '';
207 $selected{'UPDATE_PERIOD'}{'weekly'} = '';
208 $selected{'UPDATE_PERIOD'}{'monthly'} = '';
209 $selected{'UPDATE_PERIOD'}{$timesettings{'UPDATE_PERIOD'}} = "selected='selected'";
210
211 # added to v0.0.4 to refresh screen if syncro event queued
212 my $refresh = '';
213 if ( -e "${General::swroot}/time/settimenow") {
214 $refresh = "<meta http-equiv='refresh' content='60;' />";
215 }
216
217 &Header::openpage($Lang::tr{'ntp configuration'}, 1, $refresh);
218
219 &Header::openbigbox('100%', 'left', '', $errormessage);
220
221 # DPC move error message to top so it is seen!
222 if ($errormessage) {
223 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
224 print "<font class='base'>$errormessage&nbsp;</font>\n";
225 &Header::closebox();
226 }
227
228 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
229
230 &Header::openbox('100%', 'left', $Lang::tr{'network time'});
231 print <<END
232 <table width='100%'>
233 <tr>
234 <td><input type='checkbox' name='ENABLENTP' $checked{'ENABLENTP'}{'on'} /></td>
235 <td width='100%' colspan='4' class='base'>$Lang::tr{'network time from'}</td>
236 </tr>
237 <tr>
238 <td>&nbsp;</td>
239 <td width='100%' class='base' colspan='4'>
240 END
241 ;
242
243 if ( -e "${General::swroot}/time/lastset")
244 {
245 print "$Lang::tr{'clock last synchronized at'}\n";
246 my $output = `cat ${General::swroot}/time/lastset`;
247 print $output;
248 }
249 else
250 {
251 print "$Lang::tr{'clock has not been synchronized'}\n";
252 }
253
254 print <<END
255 </td></tr>
256 <tr>
257 <td>&nbsp;</td>
258 <td width='25%' class='base'>$Lang::tr{'primary ntp server'}:</td>
259 <td width='25%'><input type='text' name='NTP_ADDR_1' value='$timesettings{'NTP_ADDR_1'}' /></td>
260 <td width='25%' class='base'>$Lang::tr{'secondary ntp server'}: &nbsp;<img src='/blob.gif' align='top' alt='*' /></td>
261 <td width='25%'><input type='text' name='NTP_ADDR_2' value='$timesettings{'NTP_ADDR_2'}' /></td>
262 </tr>
263 <tr>
264 <td>&nbsp;</td>
265 <td class='base' colspan='4'><input type='checkbox' name='ENABLECLNTP' $checked{'ENABLECLNTP'}{'on'} /> $Lang::tr{'clenabled'}</td>
266 </tr>
267 </table>
268 <table width='100%'>
269 <tr>
270 <td colspan='4'><hr /><b>$Lang::tr{'update time'}</b></td>
271 </tr>
272 <tr>
273 <td>&nbsp;</td>
274 <td class='base' colspan='2'>$Lang::tr{'set time now help'}</td>
275 </tr>
276 <tr>
277 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>
278 <td width='15%'>$Lang::tr{'every'}: </td>
279 <td width='35%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />
280 <select name='UPDATE_PERIOD'>
281 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>
282 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>
283 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>
284 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>
285 </select></td>
286 <td width='50%'>&nbsp;</td>
287 </tr>
288 <tr>
289 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>
290 <td colspan='2'>$Lang::tr{'manually'}</td>
291 </tr>
292 END
293 ;
294
295 if ( -e "${General::swroot}/time/settimenow") {
296 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
297 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";
298 }
299 print <<END
300 </table>
301 <br />
302 <hr />
303 <table width='100%'>
304 <tr>
305 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'this field may be blank'}</td>
306 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>
307 <td width='25%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
308 <td width='5%' align='right'>&nbsp;</td>
309 </tr>
310 </table>
311 END
312 ;
313
314 &Header::closebox();
315
316 print "</form>\n";
317
318 &Header::closebigbox();
319
320 &Header::closepage();
321