]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/time.cgi
Nginx: fixes for rootfile.
[people/teissler/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{'network time'});
237 print <<END
238 <table width='100%'>
239 <tr>
240 <td colspan='2'><strong>$Lang::tr{'ntp common settings'}</strong></td>
241 </tr>
242
243 <tr>
244 <td><input type='checkbox' name='ENABLENTP' $checked{'ENABLENTP'}{'on'} /></td>
245 <td width='100%' colspan='4' class='base'>$Lang::tr{'network time from'}</td>
246 </tr>
247 <tr>
248 <td>&nbsp;</td>
249 <td width='100%' class='base' colspan='4'>
250 END
251 ;
252
253 if ( -e "/var/lock/time/lastset")
254 {
255 print "$Lang::tr{'clock last synchronized at'}\n";
256 open(FILE, "</var/lock/time/lastset") or die "Unable to read lastset";
257 my $output = <FILE>;
258 close FILE;
259 print $output;
260 }
261 else
262 {
263 print "$Lang::tr{'clock has not been synchronized'}\n";
264 }
265
266 print <<END
267 </td></tr>
268 <tr>
269 <td>&nbsp;</td>
270 <td width='25%' class='base'>$Lang::tr{'primary ntp server'}:</td>
271 <td width='25%'><input type='text' name='NTP_ADDR_1' value='$timesettings{'NTP_ADDR_1'}' /></td>
272 <td width='25%' class='base'>$Lang::tr{'secondary ntp server'}: &nbsp;<img src='/blob.gif' align='top' alt='*' /></td>
273 <td width='25%'><input type='text' name='NTP_ADDR_2' value='$timesettings{'NTP_ADDR_2'}' /></td>
274 </tr>
275 <tr>
276 <td>&nbsp;</td>
277 <td class='base' colspan='4'><input type='checkbox' name='ENABLECLNTP' $checked{'ENABLECLNTP'}{'on'} /> $Lang::tr{'clenabled'}</td>
278 </tr>
279 <tr>
280 <td>&nbsp;</td>
281 <td class='base' colspan='4'><input type='checkbox' name='ENABLESETONBOOT' $checked{'ENABLESETONBOOT'}{'on'} /> $Lang::tr{'Set time on boot'}</td>
282 </tr>
283 </table>
284 <table width='100%'>
285 <tr>
286 <td colspan='4'><hr /><strong>$Lang::tr{'ntp sync'}</strong></td>
287 </tr>
288 <tr>
289 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>
290 <td width='10%'>$Lang::tr{'every'}</td>
291 <td width='45%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />
292 <select name='UPDATE_PERIOD'>
293 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>
294 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>
295 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>
296 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>
297 </select></td>
298 <td width='50%'>&nbsp;</td>
299 </tr>
300 <tr>
301 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>
302 <td colspan='2'>$Lang::tr{'manually'}</td>
303 </tr>
304 <tr>
305 <td colspan='4'><hr /><strong>$Lang::tr{'update time'}</strong></td>
306 </tr>
307 <tr>
308 <td>&nbsp;</td>
309 <td class='base' colspan='3'>$Lang::tr{'set time now help'}</td>
310 </tr>
311 END
312 ;
313
314 if ( -e "/var/lock/time/settimenow") {
315 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
316 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";
317 }
318 print <<END
319 </table>
320 <br />
321 <hr />
322 <table width='100%'>
323 <tr>
324 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'this field may be blank'}</td>
325 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>
326 <td width='25%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
327 <td width='5%' align='right'>&nbsp;</td>
328 </tr>
329 </table>
330 END
331 ;
332
333 &Header::closebox();
334
335 print "</form>\n";
336
337 &Header::closebigbox();
338
339 &Header::closepage();
340