HinzugefĆ¼gt:
[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 ('/bin/touch', "${General::swroot}/time/enable");
127 &General::log($Lang::tr{'ntp syncro enabled'});
128 unlink "${General::swroot}/time/counter";
129 if ($timesettings{'UPDATE_METHOD'} eq 'periodically')
130 {
131 open(FILE, ">/${General::swroot}/time/counter") or die "Unable to write counter file";
132 flock(FILE, 2);
133 print FILE "$updateperiod\n";
134 close FILE;
135 }
136 if ($timesettings{'ENABLECLNTP'} eq 'on') # DPC added to 1.3.1
137 {
138 system ('/bin/touch', "${General::swroot}/time/allowclients"); # DPC added to 1.3.1
139 &General::log($Lang::tr{'ntpd restarted'}); # DPC added to 1.3.1
140 } else {
141 unlink "${General::swroot}/time/allowclients";
142 }
143
144 }
145 else
146 {
147 unlink "${General::swroot}/time/enable";
148 unlink "${General::swroot}/time/settimenow";
149 unlink "${General::swroot}/time/allowclients"; # DPC added to 1.3.1
150 &General::log($Lang::tr{'ntp syncro disabled'})
151 }
152 if (! $errormessage) {
153 system ('/usr/local/bin/restartntpd'); # DPC added to 1.3.1
154 }
155 }
156
157 # To enter an ' into a pushbutton solution is to use &#039; in it's definition
158 # but returned value when pressed is ' not the code. Cleanhtml recode the ' to enable comparison.
159 $timesettings{'ACTION'} = &Header::cleanhtml ($timesettings{'ACTION'});
160 if ($timesettings{'ACTION'} eq $Lang::tr{'set time now'} && $timesettings{'ENABLENTP'} eq 'on')
161 {
162 system ('/bin/touch', "${General::swroot}/time/settimenow");
163 system ('/usr/local/bin/timecheckctrl >& /dev/null');
164 }
165
166 &General::readhash("${General::swroot}/time/settings", \%timesettings);
167
168 if ($timesettings{'VALID'} eq '')
169 {
170 $timesettings{'ENABLENTP'} = 'off';
171 $timesettings{'UPDATE_METHOD'} = 'manually';
172 $timesettings{'UPDATE_VALUE'} = '1';
173 $timesettings{'UPDATE_PERIOD'} = 'daily';
174 $timesettings{'NTP_ADDR_1'} = 'de.pool.ntp.org';
175 $timesettings{'NTP_ADDR_2'} = 'pool.ntp.org';
176 }
177
178 unless ($errormessage) {
179 $timesettings{'SETMONTH'} = `date +'%m %e %Y %H %M'|cut -c 1-2`;
180 $timesettings{'SETDAY'} = `date +'%m %e %Y %H %M'|cut -c 4-5`;
181 $timesettings{'SETYEAR'} = `date +'%m %e %Y %H %M'|cut -c 7-10`;
182 $timesettings{'SETHOUR'} = `date +'%m %e %Y %H %M'|cut -c 12-13`;
183 $timesettings{'SETMINUTES'} = `date +'%m %e %Y %H %M'|cut -c 15-16`;
184 $_=$timesettings{'SETDAY'};
185 $timesettings{'SETDAY'}=~ tr/ /0/;
186 }
187
188 my %selected=();
189 my %checked=();
190
191 $checked{'ENABLENTP'}{'off'} = '';
192 $checked{'ENABLENTP'}{'on'} = '';
193 $checked{'ENABLENTP'}{$timesettings{'ENABLENTP'}} = "checked='checked'";
194
195 $checked{'ENABLECLNTP'}{'off'} = '';
196 $checked{'ENABLECLNTP'}{'on'} = '';
197 $checked{'ENABLECLNTP'}{$timesettings{'ENABLECLNTP'}} = "checked='checked'";
198
199 $checked{'UPDATE_METHOD'}{'manually'} = '';
200 $checked{'UPDATE_METHOD'}{'periodically'} = '';
201 $checked{'UPDATE_METHOD'}{$timesettings{'UPDATE_METHOD'}} = "checked='checked'";
202
203 $selected{'UPDATE_PERIOD'}{'hourly'} = '';
204 $selected{'UPDATE_PERIOD'}{'daily'} = '';
205 $selected{'UPDATE_PERIOD'}{'weekly'} = '';
206 $selected{'UPDATE_PERIOD'}{'monthly'} = '';
207 $selected{'UPDATE_PERIOD'}{$timesettings{'UPDATE_PERIOD'}} = "selected='selected'";
208
209 # added to v0.0.4 to refresh screen if syncro event queued
210 my $refresh = '';
211 if ( -e "${General::swroot}/time/settimenow") {
212 $refresh = "<meta http-equiv='refresh' content='60;' />";
213 }
214
215 &Header::openpage($Lang::tr{'ntp configuration'}, 1, $refresh);
216
217 &Header::openbigbox('100%', 'left', '', $errormessage);
218
219 # DPC move error message to top so it is seen!
220 if ($errormessage) {
221 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
222 print "<font class='base'>$errormessage&nbsp;</font>\n";
223 &Header::closebox();
224 }
225
226 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
227
228 &Header::openbox('100%', 'left', $Lang::tr{'network time'});
229 print <<END
230 <table width='100%'>
231 <tr>
232 <td><input type='checkbox' name='ENABLENTP' $checked{'ENABLENTP'}{'on'} /></td>
233 <td width='100%' colspan='4' class='base'>$Lang::tr{'network time from'}</td>
234 </tr>
235 <tr>
236 <td>&nbsp;</td>
237 <td width='100%' class='base' colspan='4'>
238 END
239 ;
240
241 if ( -e "${General::swroot}/time/lastset")
242 {
243 print "$Lang::tr{'clock last synchronized at'}\n";
244 my $output = `cat ${General::swroot}/time/lastset`;
245 print $output;
246 }
247 else
248 {
249 print "$Lang::tr{'clock has not been synchronized'}\n";
250 }
251
252 print <<END
253 </td></tr>
254 <tr>
255 <td>&nbsp;</td>
256 <td width='25%' class='base'>$Lang::tr{'primary ntp server'}:</td>
257 <td width='25%'><input type='text' name='NTP_ADDR_1' value='$timesettings{'NTP_ADDR_1'}' /></td>
258 <td width='25%' class='base'>$Lang::tr{'secondary ntp server'}: &nbsp;<img src='/blob.gif' align='top' alt='*' /></td>
259 <td width='25%'><input type='text' name='NTP_ADDR_2' value='$timesettings{'NTP_ADDR_2'}' /></td>
260 </tr>
261 <tr>
262 <td>&nbsp;</td>
263 <td class='base' colspan='4'><input type='checkbox' name='ENABLECLNTP' $checked{'ENABLECLNTP'}{'on'} /> $Lang::tr{'clenabled'}</td>
264 </tr>
265 </table>
266 <table width='100%'>
267 <tr>
268 <td colspan='4'><hr /><b>$Lang::tr{'update time'}</b></td>
269 </tr>
270 <tr>
271 <td>&nbsp;</td>
272 <td class='base' colspan='2'>$Lang::tr{'set time now help'}</td>
273 </tr>
274 <tr>
275 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>
276 <td width='15%'>$Lang::tr{'every'}: </td>
277 <td width='35%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />
278 <select name='UPDATE_PERIOD'>
279 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>
280 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>
281 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>
282 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>
283 </select></td>
284 <td width='50%'>&nbsp;</td>
285 </tr>
286 <tr>
287 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>
288 <td colspan='2'>$Lang::tr{'manually'}</td>
289 </tr>
290 END
291 ;
292
293 if ( -e "${General::swroot}/time/settimenow") {
294 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
295 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";
296 }
297 print <<END
298 </table>
299 <br />
300 <hr />
301 <table width='100%'>
302 <tr>
303 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'this field may be blank'}</td>
304 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>
305 <td width='25%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
306 <td width='5%' align='right'>&nbsp;</td>
307 </tr>
308 </table>
309 END
310 ;
311
312 &Header::closebox();
313
314 print "</form>\n";
315
316 &Header::closebigbox();
317
318 &Header::closepage();
319