]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/time.cgi
suricata: Change midstream policy to "pass-flow"
[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 use POSIX qw(strftime);
24
25 # enable only the following on debugging purpose
26 #use warnings;
27 #use CGI::Carp 'fatalsToBrowser';
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 my %timesettings=();
34 my $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';
48 $timesettings{'ENABLESETONBOOT'} = 'off';
49
50 &Header::getcgihash(\%timesettings);
51
52 if ($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 }
94 ERROR:
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 {
129 &General::system('/usr/bin/touch', "${General::swroot}/time/enable");
130 &General::system('/usr/local/bin/timectrl', 'enable');
131 &General::log($Lang::tr{'ntp syncro enabled'});
132 unlink "/var/lock/time/counter";
133 if ($timesettings{'UPDATE_METHOD'} eq 'periodically')
134 {
135 open(FILE, ">/var/lock/time/counter") or die "Unable to write counter file";
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 {
142 &General::system('/usr/bin/touch', "${General::swroot}/time/allowclients"); # DPC added to 1.3.1
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";
152 unlink "/var/lock/time/settimenow";
153 unlink "${General::swroot}/time/allowclients"; # DPC added to 1.3.1
154 &General::system('/usr/local/bin/timectrl', 'disable');
155 &General::log($Lang::tr{'ntp syncro disabled'})
156 }
157 if (! $errormessage) {
158 &General::system('/usr/local/bin/timectrl', 'restart'); # DPC added to 1.3.1
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'});
165 if ($timesettings{'ACTION'} eq $Lang::tr{'set time now'} && $timesettings{'ENABLENTP'} eq 'on')
166 {
167 &General::system('/usr/bin/touch', "/var/lock/time/settimenow");
168 }
169
170 &General::readhash("${General::swroot}/time/settings", \%timesettings);
171
172 if ($timesettings{'VALID'} eq '')
173 {
174 $timesettings{'ENABLENTP'} = 'off';
175 $timesettings{'UPDATE_METHOD'} = 'manually';
176 $timesettings{'UPDATE_VALUE'} = '1';
177 $timesettings{'UPDATE_PERIOD'} = 'daily';
178 $timesettings{'NTP_ADDR_1'} = '0.ipfire.pool.ntp.org';
179 $timesettings{'NTP_ADDR_2'} = '1.ipfire.pool.ntp.org';
180 $timesettings{'ENABLESETONBOOT'} = 'off';
181 }
182
183 unless ($errormessage) {
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;
196 $_=$timesettings{'SETDAY'};
197 $timesettings{'SETDAY'}=~ tr/ /0/;
198 }
199
200 my %selected=();
201 my %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
211 $checked{'ENABLESETONBOOT'}{'off'} = '';
212 $checked{'ENABLESETONBOOT'}{'on'} = '';
213 $checked{'ENABLESETONBOOT'}{$timesettings{'ENABLESETONBOOT'}} = "checked='checked'";
214
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
226 my $refresh = '';
227 if ( -e "/var/lock/time/settimenow") {
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!
236 if ($errormessage) {
237 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
238 print "<font class='base'>$errormessage&nbsp;</font>\n";
239 &Header::closebox();
240 }
241
242 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
243
244 &Header::openbox('100%', 'left', $Lang::tr{'ntp common settings'});
245 print <<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'>
254 END
255 ;
256
257 if ( -e "/var/lock/time/lastset")
258 {
259 print "$Lang::tr{'clock last synchronized at'}\n";
260 open(FILE, "</var/lock/time/lastset") or die "Unable to read lastset";
261 my $output = <FILE>;
262 close FILE;
263 print $output;
264 }
265 else
266 {
267 print "$Lang::tr{'clock has not been synchronized'}\n";
268 }
269
270 print <<END
271 </td></tr>
272 <tr>
273 <td>&nbsp;</td>
274 <td width='25%' class='base'>$Lang::tr{'primary ntp server'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
275 <td width='25%'><input type='text' name='NTP_ADDR_1' value='$timesettings{'NTP_ADDR_1'}' /></td>
276 <td width='25%' class='base'>$Lang::tr{'secondary ntp server'}:</td>
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>
283 <tr>
284 <td>&nbsp;</td>
285 <td class='base' colspan='4'><input type='checkbox' name='ENABLESETONBOOT' $checked{'ENABLESETONBOOT'}{'on'} /> $Lang::tr{'Set time on boot'}</td>
286 </tr>
287 </table>
288 END
289 ;
290
291 my $now = strftime($Lang::tr{'timeformat'}, localtime);
292 print "<hr>$Lang::tr{'system time'}: $now";
293
294 &Header::closebox();
295 &Header::openbox('100%',1,$Lang::tr{'ntp sync'});
296 print <<END
297 <table width='100%'>
298 <tr>
299 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>
300 <td width='10%'>$Lang::tr{'every'}</td>
301 <td width='45%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />
302 <select name='UPDATE_PERIOD'>
303 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>
304 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>
305 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>
306 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>
307 </select></td>
308 <td width='50%'>&nbsp;</td>
309 </tr>
310 <tr>
311 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>
312 <td colspan='2'>$Lang::tr{'manually'}</td>
313 </tr>
314 <tr>
315 <td colspan='4'><br><br><strong>$Lang::tr{'update time'}</strong></td>
316 </tr>
317 <tr>
318 <td>&nbsp;</td>
319 <td class='base' colspan='3'>$Lang::tr{'set time now help'}</td>
320 </tr>
321 END
322 ;
323
324 if ( -e "/var/lock/time/settimenow") {
325 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
326 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";
327 }
328 print <<END
329 </table>
330 <br />
331 <hr />
332 <table width='100%'>
333 <tr>
334 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'required field'}</td>
335 <td width='65%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>
336 <td width='5%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
337 </tr>
338 </table>
339 END
340 ;
341
342 &Header::closebox();
343
344 print "</form>\n";
345
346 &Header::closebigbox();
347
348 &Header::closepage();
349