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