]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/time.cgi
Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into next
[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'} = '0.ipfire.pool.ntp.org';
178 $timesettings{'NTP_ADDR_2'} = '1.ipfire.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{'ntp common settings'});
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 <tr>
276 <td>&nbsp;</td>
277 <td class='base' colspan='4'><input type='checkbox' name='ENABLESETONBOOT' $checked{'ENABLESETONBOOT'}{'on'} /> $Lang::tr{'Set time on boot'}</td>
278 </tr>
279 </table>
280 END
281 ;
282 &Header::closebox();
283 &Header::openbox('100%',1,$Lang::tr{'ntp sync'});
284 print <<END
285 <table width='100%'>
286 <tr>
287 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>
288 <td width='10%'>$Lang::tr{'every'}</td>
289 <td width='45%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />
290 <select name='UPDATE_PERIOD'>
291 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>
292 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>
293 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>
294 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>
295 </select></td>
296 <td width='50%'>&nbsp;</td>
297 </tr>
298 <tr>
299 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>
300 <td colspan='2'>$Lang::tr{'manually'}</td>
301 </tr>
302 <tr>
303 <td colspan='4'><br><br><strong>$Lang::tr{'update time'}</strong></td>
304 </tr>
305 <tr>
306 <td>&nbsp;</td>
307 <td class='base' colspan='3'>$Lang::tr{'set time now help'}</td>
308 </tr>
309 END
310 ;
311
312 if ( -e "/var/lock/time/settimenow") {
313 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
314 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";
315 }
316 print <<END
317 </table>
318 <br />
319 <hr />
320 <table width='100%'>
321 <tr>
322 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'this field may be blank'}</td>
323 <td width='65%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>
324 <td width='5%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
325 </tr>
326 </table>
327 END
328 ;
329
330 &Header::closebox();
331
332 print "</form>\n";
333
334 &Header::closebigbox();
335
336 &Header::closepage();
337