]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/time.cgi
BUILDFIXES:
[ipfire-2.x.git] / html / cgi-bin / time.cgi
1 #!/usr/bin/perl
2 #
3 # IPCop CGIs
4 #
5 # This file is part of the IPCop 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 'CONFIG_ROOT/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{'instant update'})
50 {
51 if ($timesettings{'SETHOUR'} eq '' || $timesettings{'SETHOUR'} < 0 || $timesettings{'SETHOUR'} > 23) {
52 $errormessage = $Lang::tr{'invalid time entered'};
53 goto UPDTERROR;
54 }
55 if ($timesettings{'SETMINUTES'} eq '' || $timesettings{'SETMINUTES'} < 0 || $timesettings{'SETMINUTES'} > 59) {
56 $errormessage = $Lang::tr{'invalid time entered'};
57 goto UPDTERROR;
58 }
59 if ($timesettings{'SETDAY'} eq '' || $timesettings{'SETDAY'} < 1 || $timesettings{'SETDAY'} > 31) {
60 $errormessage = $Lang::tr{'invalid date entered'};
61 goto UPDTERROR;
62 }
63 if ($timesettings{'SETMONTH'} eq '' || $timesettings{'SETMONTH'} < 1 || $timesettings{'SETMONTH'} > 12) {
64 $errormessage = $Lang::tr{'invalid date entered'};
65 goto UPDTERROR;
66 }
67 if ($timesettings{'SETYEAR'} eq '' || $timesettings{'SETYEAR'} < 2003 || $timesettings{'SETYEAR'} > 2030) {
68 $errormessage = $Lang::tr{'invalid date entered'};
69 goto UPDTERROR;
70 }
71
72 UPDTERROR:
73 if ($errormessage) {
74 $timesettings{'VALID'} = 'no'; }
75 else {
76 $timesettings{'VALID'} = 'yes'; }
77
78 if ($timesettings{'VALID'} eq 'yes') {
79 # we want date in YYYY-MM-DD HH:MM format for date command
80 # EAO changed datestring to ISO 6801 format 2003-08-11
81 my $datestring = "$timesettings{'SETYEAR'}-$timesettings{'SETMONTH'}-$timesettings{'SETDAY'}";
82 my $timestring = "$timesettings{'SETHOUR'}:$timesettings{'SETMINUTES'}";
83 # EAO setdate.c also revised for ISO 6801 date format 2003-08-11
84 system ('/usr/local/bin/setdate', $datestring, $timestring);
85 &General::log("$Lang::tr{'time date manually reset'} $datestring $timestring");
86 }
87 unless ($errormessage) {
88 undef %timesettings;
89 }
90 }
91
92 if ($timesettings{'ACTION'} eq $Lang::tr{'save'})
93 {
94 if ($timesettings{'ENABLENTP'} eq 'on')
95 {
96 if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_1'}) ||
97 &General::validip ($timesettings{'NTP_ADDR_1'})))
98 {
99 $errormessage = $Lang::tr{'invalid primary ntp'};
100 goto ERROR;
101 }
102 }
103 if ($timesettings{'NTP_ADDR_2'})
104 {
105 if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_2'}) ||
106 &General::validip ($timesettings{'NTP_ADDR_2'})))
107 {
108 $errormessage = $Lang::tr{'invalid secondary ntp'};
109 goto ERROR;
110 }
111 }
112 if (!($timesettings{'NTP_ADDR_1'}) && $timesettings{'NTP_ADDR_2'})
113 {
114 $errormessage = $Lang::tr{'cannot specify secondary ntp without specifying primary'};
115 goto ERROR;
116 }
117
118 if (!($timesettings{'UPDATE_VALUE'} =~ /^\d+$/) || $timesettings{'UPDATE_VALUE'} <= 0)
119 {
120 $errormessage = $Lang::tr{'invalid time period'};
121 goto ERROR;
122 }
123
124 if ($timesettings{'ENABLENTP'} ne "on" && $timesettings{'ENABLECLNTP'} eq "on")
125 {
126 $errormessage = $Lang::tr{'ntp must be enabled to have clients'};
127 goto ERROR;
128 }
129 if ($timesettings{'ENABLENTP'} eq "on" && !($timesettings{'NTP_ADDR_1'}) && !($timesettings{'NTP_ADDR_2'}))
130 {
131 $errormessage = $Lang::tr{'cannot enable ntp without specifying primary'};
132 goto ERROR;
133 }
134 ERROR:
135 if ($errormessage) {
136 $timesettings{'VALID'} = 'no'; }
137 else {
138 $timesettings{'VALID'} = 'yes'; }
139
140 &General::writehash("${General::swroot}/time/settings", \%timesettings);
141 open(FILE, ">/${General::swroot}/time/settime.conf") or die "Unable to write settime.conf file";
142 flock(FILE, 2);
143 print FILE "$timesettings{'NTP_ADDR_1'} $timesettings{'NTP_ADDR_2'}\n";
144 close FILE;
145
146 my $updateperiod=0;
147
148 if ($timesettings{'UPDATE_PERIOD'} eq 'daily') {
149 $updateperiod = $timesettings{'UPDATE_VALUE'} * 1440; }
150 elsif ($timesettings{'UPDATE_PERIOD'} eq 'weekly') {
151 $updateperiod = $timesettings{'UPDATE_VALUE'} * 10080; }
152 elsif ($timesettings{'UPDATE_PERIOD'} eq 'monthly') {
153 $updateperiod = $timesettings{'UPDATE_VALUE'} * 40320; }
154 else {
155 $updateperiod = $timesettings{'UPDATE_VALUE'} * 60; }
156
157 $updateperiod = $updateperiod - 5;
158
159 if ($updateperiod <= 5) {
160 $updateperiod = 5; }
161
162 open(FILE, ">/${General::swroot}/time/counter.conf") or die "Unable to write counter.conf file";
163 flock(FILE, 2);
164 print FILE "$updateperiod\n";
165 close FILE;
166
167 if ($timesettings{'ENABLENTP'} eq 'on' && $timesettings{'VALID'} eq 'yes')
168 {
169 system ('/bin/touch', "${General::swroot}/time/enable");
170 &General::log($Lang::tr{'ntp syncro enabled'});
171 unlink "${General::swroot}/time/counter";
172 if ($timesettings{'UPDATE_METHOD'} eq 'periodically')
173 {
174 open(FILE, ">/${General::swroot}/time/counter") or die "Unable to write counter file";
175 flock(FILE, 2);
176 print FILE "$updateperiod\n";
177 close FILE;
178 }
179 if ($timesettings{'ENABLECLNTP'} eq 'on') # DPC added to 1.3.1
180 {
181 system ('/bin/touch', "${General::swroot}/time/allowclients"); # DPC added to 1.3.1
182 &General::log($Lang::tr{'ntpd restarted'}); # DPC added to 1.3.1
183 } else {
184 unlink "${General::swroot}/time/allowclients";
185 }
186
187 }
188 else
189 {
190 unlink "${General::swroot}/time/enable";
191 unlink "${General::swroot}/time/settimenow";
192 unlink "${General::swroot}/time/allowclients"; # DPC added to 1.3.1
193 &General::log($Lang::tr{'ntp syncro disabled'})
194 }
195 if (! $errormessage) {
196 system ('/usr/local/bin/restartntpd'); # DPC added to 1.3.1
197 }
198 }
199
200 # To enter an ' into a pushbutton solution is to use &#039; in it's definition
201 # but returned value when pressed is ' not the code. Cleanhtml recode the ' to enable comparison.
202 $timesettings{'ACTION'} = &Header::cleanhtml ($timesettings{'ACTION'});
203 if ($timesettings{'ACTION'} eq $Lang::tr{'set time now'} && $timesettings{'ENABLENTP'} eq 'on')
204 {
205 system ('/bin/touch', "${General::swroot}/time/settimenow");
206 }
207
208 &General::readhash("${General::swroot}/time/settings", \%timesettings);
209
210 if ($timesettings{'VALID'} eq '')
211 {
212 $timesettings{'ENABLENTP'} = 'off';
213 $timesettings{'UPDATE_METHOD'} = 'manually';
214 $timesettings{'UPDATE_VALUE'} = '1';
215 $timesettings{'UPDATE_PERIOD'} = 'daily';
216 $timesettings{'NTP_ADDR_1'} = 'pool.ntp.org';
217 $timesettings{'NTP_ADDR_2'} = 'pool.ntp.org';
218 }
219
220 unless ($errormessage) {
221 $timesettings{'SETMONTH'} = `date +'%m %e %Y %H %M'|cut -c 1-2`;
222 $timesettings{'SETDAY'} = `date +'%m %e %Y %H %M'|cut -c 4-5`;
223 $timesettings{'SETYEAR'} = `date +'%m %e %Y %H %M'|cut -c 7-10`;
224 $timesettings{'SETHOUR'} = `date +'%m %e %Y %H %M'|cut -c 12-13`;
225 $timesettings{'SETMINUTES'} = `date +'%m %e %Y %H %M'|cut -c 15-16`;
226 $_=$timesettings{'SETDAY'};
227 $timesettings{'SETDAY'}=~ tr/ /0/;
228 }
229
230 my %selected=();
231 my %checked=();
232
233 $checked{'ENABLENTP'}{'off'} = '';
234 $checked{'ENABLENTP'}{'on'} = '';
235 $checked{'ENABLENTP'}{$timesettings{'ENABLENTP'}} = "checked='checked'";
236
237 $checked{'ENABLECLNTP'}{'off'} = '';
238 $checked{'ENABLECLNTP'}{'on'} = '';
239 $checked{'ENABLECLNTP'}{$timesettings{'ENABLECLNTP'}} = "checked='checked'";
240
241 $checked{'UPDATE_METHOD'}{'manually'} = '';
242 $checked{'UPDATE_METHOD'}{'periodically'} = '';
243 $checked{'UPDATE_METHOD'}{$timesettings{'UPDATE_METHOD'}} = "checked='checked'";
244
245 $selected{'UPDATE_PERIOD'}{'hourly'} = '';
246 $selected{'UPDATE_PERIOD'}{'daily'} = '';
247 $selected{'UPDATE_PERIOD'}{'weekly'} = '';
248 $selected{'UPDATE_PERIOD'}{'monthly'} = '';
249 $selected{'UPDATE_PERIOD'}{$timesettings{'UPDATE_PERIOD'}} = "selected='selected'";
250
251 # added to v0.0.4 to refresh screen if syncro event queued
252 my $refresh = '';
253 if ( -e "${General::swroot}/time/settimenow") {
254 $refresh = "<meta http-equiv='refresh' content='60;' />";
255 }
256
257 &Header::openpage($Lang::tr{'ntp configuration'}, 1, $refresh);
258
259 &Header::openbigbox('100%', 'left', '', $errormessage);
260
261 # DPC move error message to top so it is seen!
262 if ($errormessage) {
263 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
264 print "<font class='base'>$errormessage&nbsp;</font>\n";
265 &Header::closebox();
266 }
267
268 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
269
270 &Header::openbox('100%', 'left', $Lang::tr{'network time'});
271 print <<END
272 <table width='100%'>
273 <tr>
274 <td><input type='checkbox' name='ENABLENTP' $checked{'ENABLENTP'}{'on'} /></td>
275 <td width='100%' colspan='4' class='base'>$Lang::tr{'network time from'}</td>
276 </tr>
277 <tr>
278 <td>&nbsp;</td>
279 <td width='100%' class='base' colspan='4'>
280 END
281 ;
282
283 if ( -e "${General::swroot}/time/lastset")
284 {
285 print "$Lang::tr{'clock last synchronized at'}\n";
286 my $output = `cat ${General::swroot}/time/lastset`;
287 print $output;
288 }
289 else
290 {
291 print "$Lang::tr{'clock has not been synchronized'}\n";
292 }
293
294 print <<END
295 </td></tr>
296 <tr>
297 <td>&nbsp;</td>
298 <td width='25%' class='base'>$Lang::tr{'primary ntp server'}:</td>
299 <td width='25%'><input type='text' name='NTP_ADDR_1' value='$timesettings{'NTP_ADDR_1'}' /></td>
300 <td width='25%' class='base'>$Lang::tr{'secondary ntp server'}: &nbsp;<img src='/blob.gif' align='top' alt='*' /></td>
301 <td width='25%'><input type='text' name='NTP_ADDR_2' value='$timesettings{'NTP_ADDR_2'}' /></td>
302 </tr>
303 <tr>
304 <td>&nbsp;</td>
305 <td class='base' colspan='4'><input type='checkbox' name='ENABLECLNTP' $checked{'ENABLECLNTP'}{'on'} /> $Lang::tr{'clenabled'}</td>
306 </tr>
307 </table>
308 <table width='100%'>
309 <tr>
310 <td colspan='4'><hr /><b>$Lang::tr{'update time'}</b></td>
311 </tr>
312 <tr>
313 <td>&nbsp;</td>
314 <td class='base' colspan='2'>$Lang::tr{'set time now help'}</td>
315 </tr>
316 <tr>
317 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>
318 <td width='15%'>$Lang::tr{'every'}: </td>
319 <td width='35%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />
320 <select name='UPDATE_PERIOD'>
321 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>
322 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>
323 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>
324 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>
325 </select></td>
326 <td width='50%'>&nbsp;</td>
327 </tr>
328 <tr>
329 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>
330 <td colspan='2'>$Lang::tr{'manually'}</td>
331 </tr>
332 END
333 ;
334
335 if ( -e "${General::swroot}/time/settimenow") {
336 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
337 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";
338 }
339 print <<END
340 </table>
341 <br />
342 <hr />
343 <table width='100%'>
344 <tr>
345 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'this field may be blank'}</td>
346 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>
347 <td width='25%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
348 <td width='5%' align='right'>
349 <a href='${General::adminmanualurl}/services.html#services_time' target='_blank'><img src='/images/web-support.png' title='$Lang::tr{'online help en'}' /></a>
350 </td>
351 </tr>
352 </table>
353 END
354 ;
355
356 &Header::closebox();
357
358 &Header::openbox('100%', 'left', $Lang::tr{'update time'});
359
360 print <<END
361 <table width='100%'>
362 <tr>
363 <td width='65%' class='base'>
364 <table>
365 <tr>
366 <td>$Lang::tr{'year'}:&nbsp;</td>
367 <td><input type='text' name='SETYEAR' size='4' maxlength='4' value='$timesettings{'SETYEAR'}' /></td>
368 <td>&nbsp;$Lang::tr{'month'}:&nbsp;</td>
369 <td><input type='text' name='SETMONTH' size='2' maxlength='2' value='$timesettings{'SETMONTH'}' /></td>
370 <td>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
371 <td><input type='text' name='SETDAY' size='2' maxlength='2' value='$timesettings{'SETDAY'}' /></td>
372 <td>&nbsp;&nbsp;&nbsp;&nbsp;$Lang::tr{'hours2'}:&nbsp;</td>
373 <td><input type='text' name='SETHOUR' size='2' maxlength='2' value='$timesettings{'SETHOUR'}' /></td>
374 <td>&nbsp;$Lang::tr{'minutes'}:&nbsp;</td>
375 <td><input type='text' name='SETMINUTES' size='2' maxlength='2' value='$timesettings{'SETMINUTES'}' /></td>
376 </tr>
377 </table>
378 </td>
379 <td width='35%' align='center' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
380 </tr>
381 </table>
382 END
383 ;
384 &Header::closebox();
385
386 print "</form>\n";
387
388 &Header::closebigbox();
389
390 &Header::closepage();
391