#!/usr/bin/perl # # IPFire CGIs # # This file is part of the IPFire Project # # This code is distributed under the terms of the GPL # # (c) Eric Oberlander June 2002 # # (c) Darren Critchley June 2003 - added real time clock setting, etc # # $Id: time.cgi,v 1.4.2.11 2005/05/28 12:16:18 eoberlander Exp $ # use strict; # enable only the following on debugging purpose #use warnings; #use CGI::Carp 'fatalsToBrowser'; require '/var/ipfire/general-functions.pl'; require "${General::swroot}/lang.pl"; require "${General::swroot}/header.pl"; my %timesettings=(); my $errormessage = ''; &Header::showhttpheaders(); $timesettings{'ACTION'} = ''; $timesettings{'VALID'} = ''; $timesettings{'ENABLENTP'} = 'off'; $timesettings{'NTP_ADDR_1'} = ''; $timesettings{'NTP_ADDR_2'} = ''; $timesettings{'UPDATE_METHOD'} = 'manually'; $timesettings{'UPDATE_VALUE'} = '0'; $timesettings{'UPDATE_PERIOD'} = ''; $timesettings{'ENABLECLNTP'} = 'off'; &Header::getcgihash(\%timesettings); if ($timesettings{'ACTION'} eq $Lang::tr{'save'}) { if ($timesettings{'ENABLENTP'} eq 'on') { if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_1'}) || &General::validip ($timesettings{'NTP_ADDR_1'}))) { $errormessage = $Lang::tr{'invalid primary ntp'}; goto ERROR; } } if ($timesettings{'NTP_ADDR_2'}) { if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_2'}) || &General::validip ($timesettings{'NTP_ADDR_2'}))) { $errormessage = $Lang::tr{'invalid secondary ntp'}; goto ERROR; } } if (!($timesettings{'NTP_ADDR_1'}) && $timesettings{'NTP_ADDR_2'}) { $errormessage = $Lang::tr{'cannot specify secondary ntp without specifying primary'}; goto ERROR; } if (!($timesettings{'UPDATE_VALUE'} =~ /^\d+$/) || $timesettings{'UPDATE_VALUE'} <= 0) { $errormessage = $Lang::tr{'invalid time period'}; goto ERROR; } if ($timesettings{'ENABLENTP'} ne "on" && $timesettings{'ENABLECLNTP'} eq "on") { $errormessage = $Lang::tr{'ntp must be enabled to have clients'}; goto ERROR; } if ($timesettings{'ENABLENTP'} eq "on" && !($timesettings{'NTP_ADDR_1'}) && !($timesettings{'NTP_ADDR_2'})) { $errormessage = $Lang::tr{'cannot enable ntp without specifying primary'}; goto ERROR; } ERROR: if ($errormessage) { $timesettings{'VALID'} = 'no'; } else { $timesettings{'VALID'} = 'yes'; } &General::writehash("${General::swroot}/time/settings", \%timesettings); open(FILE, ">/${General::swroot}/time/settime.conf") or die "Unable to write settime.conf file"; flock(FILE, 2); print FILE "$timesettings{'NTP_ADDR_1'} $timesettings{'NTP_ADDR_2'}\n"; close FILE; my $updateperiod=0; if ($timesettings{'UPDATE_PERIOD'} eq 'daily') { $updateperiod = $timesettings{'UPDATE_VALUE'} * 1440; } elsif ($timesettings{'UPDATE_PERIOD'} eq 'weekly') { $updateperiod = $timesettings{'UPDATE_VALUE'} * 10080; } elsif ($timesettings{'UPDATE_PERIOD'} eq 'monthly') { $updateperiod = $timesettings{'UPDATE_VALUE'} * 40320; } else { $updateperiod = $timesettings{'UPDATE_VALUE'} * 60; } $updateperiod = $updateperiod - 5; if ($updateperiod <= 5) { $updateperiod = 5; } open(FILE, ">/${General::swroot}/time/counter.conf") or die "Unable to write counter.conf file"; flock(FILE, 2); print FILE "$updateperiod\n"; close FILE; if ($timesettings{'ENABLENTP'} eq 'on' && $timesettings{'VALID'} eq 'yes') { system ('/usr/bin/touch', "${General::swroot}/time/enable"); system ('/usr/local/bin/timectrl enable >/dev/null 2>&1'); &General::log($Lang::tr{'ntp syncro enabled'}); unlink "${General::swroot}/time/counter"; if ($timesettings{'UPDATE_METHOD'} eq 'periodically') { open(FILE, ">/${General::swroot}/time/counter") or die "Unable to write counter file"; flock(FILE, 2); print FILE "$updateperiod\n"; close FILE; } if ($timesettings{'ENABLECLNTP'} eq 'on') # DPC added to 1.3.1 { system ('/usr/bin/touch', "${General::swroot}/time/allowclients"); # DPC added to 1.3.1 &General::log($Lang::tr{'ntpd restarted'}); # DPC added to 1.3.1 } else { unlink "${General::swroot}/time/allowclients"; } } else { unlink "${General::swroot}/time/enable"; unlink "${General::swroot}/time/settimenow"; unlink "${General::swroot}/time/allowclients"; # DPC added to 1.3.1 system ('/usr/local/bin/timectrl disable >/dev/null 2>&1'); &General::log($Lang::tr{'ntp syncro disabled'}) } if (! $errormessage) { system ('/usr/local/bin/timectrl restart >/dev/null 2>&1'); # DPC added to 1.3.1 } } # To enter an ' into a pushbutton solution is to use ' in it's definition # but returned value when pressed is ' not the code. Cleanhtml recode the ' to enable comparison. $timesettings{'ACTION'} = &Header::cleanhtml ($timesettings{'ACTION'}); if ($timesettings{'ACTION'} eq $Lang::tr{'set time now'} && $timesettings{'ENABLENTP'} eq 'on') { system ('/usr/bin/touch', "${General::swroot}/time/settimenow"); } &General::readhash("${General::swroot}/time/settings", \%timesettings); if ($timesettings{'VALID'} eq '') { $timesettings{'ENABLENTP'} = 'off'; $timesettings{'UPDATE_METHOD'} = 'manually'; $timesettings{'UPDATE_VALUE'} = '1'; $timesettings{'UPDATE_PERIOD'} = 'daily'; $timesettings{'NTP_ADDR_1'} = 'de.pool.ntp.org'; $timesettings{'NTP_ADDR_2'} = 'pool.ntp.org'; } unless ($errormessage) { $timesettings{'SETMONTH'} = `date +'%m %e %Y %H %M'|cut -c 1-2`; $timesettings{'SETDAY'} = `date +'%m %e %Y %H %M'|cut -c 4-5`; $timesettings{'SETYEAR'} = `date +'%m %e %Y %H %M'|cut -c 7-10`; $timesettings{'SETHOUR'} = `date +'%m %e %Y %H %M'|cut -c 12-13`; $timesettings{'SETMINUTES'} = `date +'%m %e %Y %H %M'|cut -c 15-16`; $_=$timesettings{'SETDAY'}; $timesettings{'SETDAY'}=~ tr/ /0/; } my %selected=(); my %checked=(); $checked{'ENABLENTP'}{'off'} = ''; $checked{'ENABLENTP'}{'on'} = ''; $checked{'ENABLENTP'}{$timesettings{'ENABLENTP'}} = "checked='checked'"; $checked{'ENABLECLNTP'}{'off'} = ''; $checked{'ENABLECLNTP'}{'on'} = ''; $checked{'ENABLECLNTP'}{$timesettings{'ENABLECLNTP'}} = "checked='checked'"; $checked{'UPDATE_METHOD'}{'manually'} = ''; $checked{'UPDATE_METHOD'}{'periodically'} = ''; $checked{'UPDATE_METHOD'}{$timesettings{'UPDATE_METHOD'}} = "checked='checked'"; $selected{'UPDATE_PERIOD'}{'hourly'} = ''; $selected{'UPDATE_PERIOD'}{'daily'} = ''; $selected{'UPDATE_PERIOD'}{'weekly'} = ''; $selected{'UPDATE_PERIOD'}{'monthly'} = ''; $selected{'UPDATE_PERIOD'}{$timesettings{'UPDATE_PERIOD'}} = "selected='selected'"; # added to v0.0.4 to refresh screen if syncro event queued my $refresh = ''; if ( -e "${General::swroot}/time/settimenow") { $refresh = ""; } &Header::openpage($Lang::tr{'ntp configuration'}, 1, $refresh); &Header::openbigbox('100%', 'left', '', $errormessage); # DPC move error message to top so it is seen! if ($errormessage) { &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); print "$errormessage \n"; &Header::closebox(); } print "
\n"; &Header::openbox('100%', 'left', $Lang::tr{'network time'}); print < $Lang::tr{'network time from'}   END ; if ( -e "${General::swroot}/time/lastset") { print "$Lang::tr{'clock last synchronized at'}\n"; my $output = `cat ${General::swroot}/time/lastset`; print $output; } else { print "$Lang::tr{'clock has not been synchronized'}\n"; } print <   $Lang::tr{'primary ntp server'}: $Lang::tr{'secondary ntp server'}:  *   $Lang::tr{'clenabled'} END ; if ( -e "${General::swroot}/time/settimenow") { print "\n\n"; print "\n"; } print <


$Lang::tr{'update time'}
  $Lang::tr{'set time now help'}
$Lang::tr{'every'}:  
$Lang::tr{'manually'}
$Lang::tr{'waiting to synchronize clock'}...
* $Lang::tr{'this field may be blank'}  
END ; &Header::closebox(); print "\n"; &Header::closebigbox(); &Header::closepage();