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