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