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