initial commit of file from CVS for e-smith-ntp on Wed 12 Jul 09:00:21 BST 2023

This commit is contained in:
Brian Read
2023-07-12 09:00:21 +01:00
parent 59f5643b62
commit 4d143a42fe
37 changed files with 3230 additions and 2 deletions

View File

@@ -0,0 +1 @@
smeserver.pool.ntp.org

View File

@@ -0,0 +1 @@
enabled

View File

@@ -0,0 +1 @@
service

View File

@@ -0,0 +1,7 @@
{
my $ntpd = $DB->get('ntpd');
return unless $ntpd;
return if $ntpd->prop('SyncToHWClockSupported');
$ntpd->set_prop('status', 'enabled') unless ($ntpd->prop('status') || 'disabled') eq 'enabled';
}

View File

@@ -0,0 +1,8 @@
{
my $pool = $DB->get_prop('ntpd', 'NTPServer');
return unless defined $pool;
if ($pool eq 'pool.ntp.org')
{
$DB->set_prop('ntpd', 'NTPServer', 'smeserver.pool.ntp.org')
}
}

View File

@@ -0,0 +1,66 @@
#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 2001-2005 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#----------------------------------------------------------------------
package esmith;
use strict;
use Errno;
use esmith::ConfigDB;
use esmith::templates;
my $db = esmith::ConfigDB->open or die "Could not open config db";
# Obtain the TimeZone configuration database value
my $timezone = $db->get("TimeZone");
if (defined $timezone)
{
$timezone = $timezone->value;
}
else
{
# Initialise timezone from /etc/sysconfig/clock if not already set
if (open(CLOCK, "/etc/sysconfig/clock"))
{
while(<CLOCK>)
{
$timezone = $1 if /^ZONE="(.*)"/;
last if defined $timezone;
}
close(CLOCK) or warn("Could not close CLOCK: $!");
}
else
{
warn("Could not open /etc/sysconfig/clock: $!");
}
# If all else fails ...
$timezone = "US/Eastern" unless defined $timezone;
$db->new_record('TimeZone')->set_value($timezone);
}
# Undo the previous symlink and remake it to the correct time zone
unlink "/etc/localtime";
symlink "../usr/share/zoneinfo/$timezone", "/etc/localtime";
# Process the template for /etc/sysconfig/clock
processTemplate ({TEMPLATE_PATH => "/etc/sysconfig/clock"});
exit (0);

View File

@@ -0,0 +1,4 @@
#!/bin/sh
/bin/date "$2"
/sbin/clock -u -w

View File

@@ -0,0 +1,239 @@
<lexicon lang="en-us">
<entry>
<base>FORM_TITLE</base>
<trans>Date and time configuration</trans>
</entry>
<entry>
<base>INITIAL_DESC</base>
<trans>
This is where you configure the date and time of this server. You may use an existing network time server or
manually set the date and time for your time zone.
</trans>
</entry>
<entry>
<base>SET_DATE_TITLE</base>
<trans>Set Date and Time</trans>
</entry>
<entry>
<base>COULD_NOT_OPEN_TZ_FILE</base>
<trans>Error: Could not open timezone file for reading: </trans>
</entry>
<entry>
<base>JANUARY</base>
<trans>January</trans>
</entry>
<entry>
<base>FEBRUARY</base>
<trans>February</trans>
</entry>
<entry>
<base>MARCH</base>
<trans>March</trans>
</entry>
<entry>
<base>APRIL</base>
<trans>April</trans>
</entry>
<entry>
<base>MAY</base>
<trans>May</trans>
</entry>
<entry>
<base>JUNE</base>
<trans>June</trans>
</entry>
<entry>
<base>JULY</base>
<trans>July</trans>
</entry>
<entry>
<base>AUGUST</base>
<trans>August</trans>
</entry>
<entry>
<base>SEPTEMBER</base>
<trans>September</trans>
</entry>
<entry>
<base>OCTOBER</base>
<trans>October</trans>
</entry>
<entry>
<base>NOVEMBER</base>
<trans>November</trans>
</entry>
<entry>
<base>DECEMBER</base>
<trans>December</trans>
</entry>
<entry>
<base>NEW_M/D/Y</base>
<trans>New month/day/year:</trans>
</entry>
<entry>
<base>NEW_H/M/S</base>
<trans>New hour/min/sec:</trans>
</entry>
<entry>
<base>AM/PM_AND_TZ</base>
<trans>AM/PM and time zone:</trans>
</entry>
<entry>
<base>NTP_ENABLE_DESC</base>
<trans>
The server can periodically synchronize the system clock to a network time protocol (NTP) server. If you select this option, enter the hostname or IP address of the NTP server below.
</trans>
</entry>
<entry>
<base>NTP_CONFIGURE_DESC</base>
<trans>
The server is periodically synchronizing the system clock to the network time protocol (NTP) server specified below. To synchronize to a different NTP server, enter a different hostname or IP address in the field below.
</trans>
</entry>
<entry>
<base>NTP_DISABLE_DESC</base>
<trans>
Choose this option to stop syncronizing the system clock to the NTP
server. When the NTP service is disabled, you can set the system date
and time manually from this page.
</trans>
</entry>
<entry>
<base>NTP_SERVER</base>
<trans>NTP Server</trans>
</entry>
<entry>
<base>VERIFY_DATE_AND_TIME</base>
<trans>Verify date and time</trans>
</entry>
<entry>
<base>NEW_DATE_AND_TIME</base>
<trans>New date and time setting:</trans>
</entry>
<entry>
<base>SETTING_DATE_AND_TIME</base>
<trans>Setting date and time</trans>
</entry>
<entry>
<base>INVALID_DAY</base>
<trans>Error: invalid day of month: </trans>
</entry>
<entry>
<base>BETWEEN_1_AND_31</base>
<trans>Please choose a day between 1 and 31.</trans>
</entry>
<entry>
<base>INVALID_YEAR</base>
<trans>Error: invalid year: </trans>
</entry>
<entry>
<base>FOUR_DIGIT_YEAR</base>
<trans>Please choose a four-digit year between 1900 and 2200.</trans>
</entry>
<entry>
<base>INVALID_HOUR</base>
<trans>Error: invalid hour: </trans>
</entry>
<entry>
<base>BETWEEN_1_AND_12</base>
<trans>Please choose an hour between 1 and 12.</trans>
</entry>
<entry>
<base>INVALID_MINUTE</base>
<trans>Error: invalid minute: </trans>
</entry>
<entry>
<base>BETWEEN_0_AND_59</base>
<trans>Please choose a minute between 0 and 59.</trans>
</entry>
<entry>
<base>INVALID_SECOND</base>
<trans>Error: invalid second</trans>
</entry>
<entry>
<base>INVALID_MONTH</base>
<trans>Error: invalid month</trans>
</entry>
<entry>
<base>MONTH_BETWEEN_1_AND_12</base>
<trans>Please choose a month value between 1 and 12.</trans>
</entry>
<entry>
<base>UPDATING_CLOCK</base>
<trans>
<![CDATA[
System clock is being updated. Please wait for a few seconds,
then click <A HREF="datetime?page=1&wherenext=Verify" TARGET="main">here</A>
to verify changes.
]]>
</trans>
</entry>
<entry>
<base>ERR_SETTING_CLOCK</base>
<trans>Error occurred while setting system time and hardware clock.</trans>
</entry>
<entry>
<base>SERVER_DISABLED</base>
<trans>Network time server disabled successfully</trans>
</entry>
<entry>
<base>SERVER_DISABLED_DESC</base>
<trans>
<![CDATA[
You have disabled this service: The server will rely on its internal
clock, and <b>will not</b> try to synchronize from a time server.
]]>
</trans>
</entry>
<entry>
<base>ERR_CHANGING_TS</base>
<trans>Error while changing network time server setting</trans>
</entry>
<entry>
<base>INVALID_NTP_ADDR</base>
<trans>Invalid NTP server address: </trans>
</entry>
<entry>
<base>SETTINGS_CHANGED</base>
<trans>Network time server setting changed successfully</trans>
</entry>
<entry>
<base>SYNC_WITH</base>
<trans>This server is now configured to synchronize periodically
(via the Internet) with:
</trans>
</entry>
<entry>
<base>INVALID_NTP_SERVER</base>
<trans>
<![CDATA[
Invalid NTP server, the server <b>will not</b> try to synchronize
from a time server.
]]>
</trans>
</entry>
<entry>
<base>NETWORK_TIME_SERVER</base>
<trans>Network Time Server</trans>
</entry>
<entry>
<base>NTP_CONFIGURE_TITLE</base>
<trans>Configure Network Time Server</trans>
</entry>
<entry>
<base>NTP_ENABLE_TITLE</base>
<trans>Enable Network Time Server</trans>
</entry>
<entry>
<base>NTP_DISABLE_TITLE</base>
<trans>Disable Network Time Server</trans>
</entry>
<entry>
<base>CURRENT_SETTING</base>
<trans>Current setting</trans>
</entry>
<entry>
<base>Date and time</base>
<trans>Date and time</trans>
</entry>
</lexicon>

View File

@@ -0,0 +1,8 @@
/var/log/ntp.log \{
missingok
weekly
rotate 24
compress
notifempty
copytruncate
\}

View File

@@ -0,0 +1,19 @@
{
# vim: ft=perl:
my $server = $ntpd{NTPServer} || '';
return "# sync to hw clock" if $server =~ m#^\s*$#;
if ( ($ntpd{'SupportLargeDrift'} || 'disabled') eq 'enabled' ){
$OUT .= "tinker panic 0\n";
}
if ($server =~ /pool.ntp.org/)
{
# The 0, 1, 2, 3 prefixes ensure NTP pool round-robin
$OUT .= "server $_.$server\n" for (0..3);
}
else
{
$OUT .= "server $server\n";
}
}

View File

@@ -0,0 +1,9 @@
{
$OUT = '';
unless ( ($ntpd{'SupportLargeDrift'} || 'disabled') eq 'enabled' ){
$OUT .= <<EOF;
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
EOF
}
}

View File

@@ -0,0 +1,3 @@
{
$OUT = "driftfile /var/lib/ntp/drift";
}

View File

@@ -0,0 +1,2 @@
restrict default noquery

View File

@@ -0,0 +1 @@
restrict 127.0.0.1

View File

@@ -0,0 +1,19 @@
{
my $NTPenabled = $ntpd{'status'} || 'disabled';
my $server = $ntpd{NTPServer} || '';
if ($NTPenabled eq 'enabled')
{
if ($server =~ /pool.ntp.org/)
{
$OUT .= "0.$server";
}
else
{
$OUT .= "$server";
}
}
else
{
$OUT .= "# ntpd service is disabled";
}
}

View File

@@ -0,0 +1,4 @@
#ntp
:programname, isequal, "ntpd" /var/log/ntp.log
& stop

View File

@@ -0,0 +1,5 @@
{
my $timezone = $TimeZone || "US/Eastern";
$OUT .= "ZONE=\"$timezone\"";
}

View File

@@ -0,0 +1,3 @@
{
$OUT .= "UTC=true";
}

View File

@@ -0,0 +1,3 @@
{
$OUT .= "ARC=false";
}

View File

@@ -0,0 +1,4 @@
# nut ntpd specific subservices
# this would conflict with ntpd
disable ntpdate.service

View File

@@ -0,0 +1,20 @@
[Service]
{
# memory limit to catch any memory leak before it kills the system
use constant MIN_MEMORY_LIMIT => 35000000;
# If there is a memory limit in the database and it is below the minimum, delete it.
if (defined $ntpd{MemLimit} && $ntpd{MemLimit} < MIN_MEMORY_LIMIT)
{
$DB->get_prop_and_delete('ntpd','MemLimit');
delete $ntpd{MemLimit};
}
$OUT .= "MemoryLimit=";
$OUT .= $ntpd{MemLimit} || MIN_MEMORY_LIMIT;
$OUT .= "\n";
}
ExecStartPre=-/sbin/e-smith/expand-template /etc/ntp.conf
ExecStartPre=-/sbin/e-smith/systemd/initializedate
Restart=always

View File

@@ -0,0 +1,2 @@
[Install]
WantedBy=sme-server.target

View File

@@ -0,0 +1,47 @@
#!/usr/bin/perl -wT
#----------------------------------------------------------------------
# heading : Configuration
# description : Date and time
# navigation : 6000 6100
#
# Copyright (c) 2001 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Technical support for this program is available from e-smith, inc.
# Please visit our web site www.e-smith.com for details.
#----------------------------------------------------------------------
use strict;
use esmith::FormMagick::Panel::datetime;
my $panel = esmith::FormMagick::Panel::datetime->new();
$|=1;
$panel->display();
__DATA__
<form title="FORM_TITLE" header="/etc/e-smith/web/common/head.tmpl"
footer="/etc/e-smith/web/common/foot.tmpl">
<page name="First" pre-event="print_status_message()"
post-event="performAndShowResult()">
<description>INITIAL_DESC</description>
<subroutine src="showInitial()"/>
</page>
<page name="Verify" pre-event="turn_off_buttons">
<title>VERIFY_DATE_AND_TIME</title>
<subroutine src="verifyResult()"/>
</page>
</form>

View File

@@ -0,0 +1,25 @@
#!/bin/bash
#first move old drift file if in the way
OLDFILE='/etc/ntp/drift'
NEWFILE='/var/lib/ntp/drift'
if [ -f "$NEWFILE" ]; then
if [ -f "$OLDFILE" ];then
/usr/bin/rm -f $OLDFILE
fi
else
if [ -f "$OLDFILE" ];then
/usr/bin/mv $OLDFILE $NEWFILE
fi
fi
if [ -f "$NEWFILE" ]; then
/usr/bin/chown ntp:ntp $NEWFILE
fi
# Run ntpdate so we're not way off on startup.
ntpstep=/etc/ntp/step-tickers
tickers=`/bin/sed -e 's/\#.*$//g' $ntpstep`
#get current date (will need to update this command soon to be retired : ntpd -q -x -g -g -g -g
/usr/sbin/ntpdate -b -p 8 $tickers
# set hardware clock to the current time
/usr/sbin/hwclock --systohc
exit 0

0
root/usr/lib/perl5/.gitignore vendored Normal file
View File

View File

View File

@@ -0,0 +1,774 @@
#!/usr/bin/perl -wT
#
# Copyright (C) 2002 Mitel Networks Corporation
#
# Technical support for this program is available from e-smith, inc.
# Please call us at (613) 236-0743 or visit our web site www.e-smith.net
# for details.
#
# $Id: datetime.pm,v 1.13 2003/04/04 19:24:58 lijied Exp $
#
#----------------------------------------------------------------------
package esmith::FormMagick::Panel::datetime;
use strict;
use Exporter;
use esmith::ConfigDB;
use esmith::FormMagick;
use esmith::cgi;
use esmith::TestUtils;
our @ISA = qw(esmith::FormMagick Exporter);
our @EXPORT = qw(
showInitial performAndShowResult verifyResult flushOn
);
our $VERSION = sprintf '%d.%03d', q$Revision: 1.13 $ =~ /: (\d+).(\d+)/;
=pod
=head1 NAME
esmith::FormMagick::Panel::datetime - useful panel functions
=head1 SYNOPSIS
use esmith::FormMagick::Panel::datetime;
my $panel = esmith::FormMagick::Panel::datetime->new();
$panel->display();
=head1 DESCRIPTION
=head2 new
Exactly as for esmith::FormMagick
=begin testing
use_ok('esmith::FormMagick::Panel::datetime');
$FM = esmith::FormMagick::Panel::datetime->new();
isa_ok($FM, 'esmith::FormMagick::Panel::datetime');
$FM->{cgi} = CGI->new;
=end testing
=cut
sub new
{
shift;
my $self = esmith::FormMagick->new();
$self->{calling_package} = (caller)[0];
bless $self;
return $self;
}
=pod
=head2 showInitial
Display the contents of the initial page
=begin testing
is($FM->showInitial(), '', 'showInitial');
like($_STDOUT_, qr/NEW_M\/D\/Y/, ' .. new m/d/y field');
like($_STDOUT_, qr/NEW_H\/M\/S/, ' .. new h/m/s field');
like($_STDOUT_, qr/SAVE_DATE_TIME/, ' .. save date/time button');
like($_STDOUT_, qr/ENABLE_NTP/, ' .. enable NTP box');
like($_STDOUT_, qr/SAVE_NTP/, ' .. save NTP button');
=end testing
=cut
sub showInitial
{
my $self = shift;
my $q = $self->{cgi};
#--------------------------------------------------
# Get a sorted list of time zones
#--------------------------------------------------
$ENV{BASH_ENV} = '';
if (! open (ZONES, "cd /usr/share/zoneinfo; /usr/bin/find . -type f -or -type l | /bin/grep '^./[A-Z]' |"))
{
print $q->h3 ($self->localise('COULD_NOT_OPEN_TZ_FILE').$!.'.');
return;
}
my $zone;
my @zones = ();
while (defined ($zone = <ZONES>))
{
chop ($zone);
$zone =~ s/^.\///;
push @zones, $zone;
}
close ZONES;
@zones = sort @zones;
#--------------------------------------------------
# Read the time
#--------------------------------------------------
my ($weekdaydefault,
$monthnamedefault,
$monthdefault,
$daydefault,
$yeardefault,
$hourdefault,
$minutedefault,
$seconddefault,
$ampmdefault) = split /\|/,
`/bin/date '+%A|%B|%-m|%-d|%Y|%-I|%M|%S|%p'`;
# get rid of trailing carriage return on last field
chop ($ampmdefault);
#--------------------------------------------------
# Figure out time zone by looking first looking at
# the configuration database value of TimeZone.
# If that is not defined, try and get it from /etc/localtime.
# If that doesn't work, default to US/Eastern.
#--------------------------------------------------
my $localtime;
my $timezonedefault = "US/Eastern";
my $conf = esmith::ConfigDB->open();
if (defined $conf->get('TimeZone'))
{
$timezonedefault = $conf->get('TimeZone')->value;
}
else
{
if (defined ($localtime = readlink '/etc/localtime'))
{
my $pos = index $localtime, 'zoneinfo/';
if ($pos > -1)
{
$timezonedefault = substr $localtime, ($pos + 9);
}
}
}
my $now_string = $self->gen_locale_date_string();
my $oldNTPServer = '';
$conf = esmith::ConfigDB->open();
my $rec = $conf->get('ntpd');
if ($rec)
{
$oldNTPServer = $rec->prop('NTPServer') || '';
}
my $ntpEnabled = 0;
if ($rec and $rec->prop('status') eq "enabled")
{
$ntpEnabled = 1 unless ($rec->prop('SyncToHWClockSupported') || 'yes') eq 'yes' and $oldNTPServer =~ m#^\s*$#;
}
#--------------------------------------------------
# Display the time setting form
#--------------------------------------------------
my %monthlabels = ('1' => $self->localise('JANUARY'),
'2' => $self->localise('FEBRUARY'),
'3' => $self->localise('MARCH'),
'4' => $self->localise('APRIL'),
'5' => $self->localise('MAY'),
'6' => $self->localise('JUNE'),
'7' => $self->localise('JULY'),
'8' => $self->localise('AUGUST'),
'9' => $self->localise('SEPTEMBER'),
'10' => $self->localise('OCTOBER'),
'11' => $self->localise('NOVEMBER'),
'12' => $self->localise('DECEMBER'));
# create radio buttons in the same group
my ($rbDisableNTP, $rbEnableNTP) =
$q->radio_group(-name => 'ntpStatus',
-values => ['disabled', 'enabled'],
-default => $ntpEnabled ? 'enabled' : 'disabled',
-linebreak => 'true',
-labels => {enabled => '', disabled => ''});
print "<tr><td colspan=2><table>\n";
if (! $ntpEnabled)
{
print $q->Tr(
$q->td({-class => "sme-radiobutton"}, $rbDisableNTP),
$q->td($q->h3($self->localise('SET_DATE_TITLE')))),
$q->Tr(
$q->td({-class => "sme-radiobutton"}, "&nbsp;"),
$q->td(
$q->p ($self->localise('CURRENT_SETTING').": ",
$q->b ($now_string)))),
$q->Tr(
$q->td({-class => "sme-radiobutton"}, "&nbsp;"),
$q->td(
$q->table({-class => "sme-noborders"},
$q->Tr (esmith::cgi::genCell ($q,
$self->localise('NEW_M/D/Y'), "sme-noborders-label"),
esmith::cgi::genCell ($q,
$q->popup_menu (-name => 'month', -values => [ 1..12 ],
-default => $monthdefault, -labels => \%monthlabels),
"sme-noborders-content"),
esmith::cgi::genCell ($q,
$q->textfield (-name => 'day', -override => 1,
-default => $daydefault, -size => 12),
"sme-noborders-content"),
esmith::cgi::genCell ($q,
$q->textfield (-name => 'year', -override => 1,
-default => $yeardefault, -size => 12),
"sme-noborders-content")),
$q->Tr (esmith::cgi::genCell ($q,
$self->localise('NEW_H/M/S'), "sme-noborders-label"),
esmith::cgi::genCell ($q,
$q->textfield (-name => 'hour', -override => 1,
-default => $hourdefault, -size => 12),
"sme-noborders-content"),
esmith::cgi::genCell ($q,
$q->textfield (-name => 'minute', -override => 1,
-default => $minutedefault, -size => 12),
"sme-noborders-content"),
esmith::cgi::genCell ($q,
$q->textfield (-name => 'second', -override => 1,
-default => $seconddefault, -size => 12),
"sme-noborders-content")),
$q->Tr (esmith::cgi::genCell ($q,
$self->localise('AM/PM_AND_TZ'), "sme-noborders-label"),
esmith::cgi::genCell ($q,
$q->popup_menu (-name => 'ampm', -values => ['AM', 'PM'],
-default => $ampmdefault),
"sme-noborders-content"),
esmith::cgi::genDoubleCell ($q,
$q->popup_menu (-name => 'timezone', -values => \@zones,
-default => $timezonedefault))),
)
)
),"\n";
}
#--------------------------------------------------
# Display the NTP form
#--------------------------------------------------
my $ntpEnabledTitle;
my $ntpEnabledDesc;
my $showTime;
if ($ntpEnabled)
{
$ntpEnabledTitle = $self->localise('NTP_CONFIGURE_TITLE');
$ntpEnabledDesc = $self->localise('NTP_CONFIGURE_DESC');
$showTime = $q->p ($self->localise('CURRENT_SETTING').": ",
$q->b ($now_string));
}
else
{
$ntpEnabledTitle = $self->localise('NTP_ENABLE_TITLE');
$ntpEnabledDesc = $self->localise('NTP_ENABLE_DESC');
}
print $q->Tr(
$q->td({-class => "sme-radiobutton"}, $rbEnableNTP),
$q->td($q->h3($ntpEnabledTitle))),
$q->Tr(
$q->td({-class => "sme-radiobutton"}, "&nbsp;"),
$q->td($ntpEnabledDesc)),
$q->Tr(
$q->td({-class => "sme-radiobutton"}, "&nbsp;"),
$q->td($showTime)),
$q->Tr(
$q->td({-class => "sme-radiobutton"}, "&nbsp;"),
$q->td(
$q->table ({-class => "sme-noborders"},
$q->Tr (esmith::cgi::genCell ($q,
$self->localise('NTP_SERVER'), "sme-noborders-label"),
esmith::cgi::genCell ($q,
$q->textfield(-name => 'ntpServer', -size => '32',
-default => "$oldNTPServer"),
"sme-noborders-content")),
)
)
),"\n";
if ($ntpEnabled)
{
print $q->Tr(
$q->td({-class => "sme-radiobutton"}, $rbDisableNTP),
$q->td($q->h3 ($self->localise("NTP_DISABLE_TITLE")))),
$q->Tr(
$q->td({-class => "sme-radiobutton"}, "&nbsp;"),
$q->td($self->localise('NTP_DISABLE_DESC'))),"\n";
}
print $q->Tr(
$q->td({-colspan => 2},
$q->table ({-class => "sme-noborders", -width=>'100%'},
$q->Tr(
$q->th({-class => "sme-layout"},
$q->submit (-name => 'action',
-value => $self->localise('SAVE'))))
)
)
),"\n";
print "</table></td></tr>\n";
return '';
}
=pod
=head2 performAndShowResult
Subroutine to perform actions and display result
=for testing
is($FM->performAndShowResult(), '', 'performAndShowResult');
=cut
sub performAndShowResult
{
my $self = shift;
my $q = $self->{cgi};
my $conf = esmith::ConfigDB->open();
my $ntpd = $conf->get('ntpd');
$conf->close();
my $hwSync = $ntpd->prop('SyncToHWClockSupported') || 'yes';
my $oldNtpServer = $ntpd->prop('NTPServer') || '';
my $oldNtpStatus = ($hwSync eq 'yes' and $oldNtpServer =~ m#^\s*$#) ? 'disabled' : ($ntpd->prop('status') || 'disabled');
my $newNtpServer = $q->param('ntpServer') || '';
my $newNtpStatus = $q->param('ntpStatus') || '';
# If ntp status was and still is disabled, then set the time.
if (($oldNtpStatus eq 'disabled') &&
($newNtpStatus eq 'disabled'))
{
$self->_performSetDateTime();
}
# If the ntp status changed, then force a reconfigure.
# If it is enabled and the server changed then force a reconfigure.
if (($oldNtpStatus ne $newNtpStatus) ||
(($newNtpStatus eq 'enabled') &&
($oldNtpServer ne $newNtpServer)))
{
# let ntp enable/disable itself as required
$self->_performSetTimeserver();
}
$q->Delete('ntpServer');
$q->Delete('ntpStatus');
return '';
}
=pod
=head2 verifyResult
Subroutine to verify result
=begin testing
is($FM->verifyResult(), '', 'verifyResult');
like($_STDOUT_, qr/NEW_DATE_AND_TIME/, ' .. new date and time');
=end testing
=cut
sub verifyResult
{
my $self = shift;
my $q = $self->{cgi};
$ENV{BASH_ENV} = '';
#--------------------------------------------------
# Read the time
#--------------------------------------------------
my ($weekdaydefault,
$monthnamedefault,
$monthdefault,
$daydefault,
$yeardefault,
$hourdefault,
$minutedefault,
$seconddefault,
$ampmdefault) = split /\|/, `/bin/date '+%A|%B|%-m|%-d|%Y|%-I|%M|%S|%p'`;
# get rid of trailing carriage return on last field
chop ($ampmdefault);
#--------------------------------------------------
# Figure out time zone by looking at /etc/localtime.
# If that doesn't work, default to US/Eastern.
#--------------------------------------------------
my $localtime;
my $timezonedefault = "US/Eastern";
if (defined ($localtime = readlink '/etc/localtime'))
{
my $pos = index $localtime, 'zoneinfo/';
if ($pos > -1)
{
$timezonedefault = substr $localtime, ($pos + 9);
}
}
#--------------------------------------------------
# Display the time
#--------------------------------------------------
print $q->Tr($q->td($q->p ($self->localise('NEW_DATE_AND_TIME'),
$q->b ($weekdaydefault,
$monthnamedefault,
$daydefault,
$yeardefault . ',',
$hourdefault . ':' . $minutedefault . ':' .
$seconddefault,
$ampmdefault,
$timezonedefault))));
return '';
}
=pod
=head2 _performSetDateTime
Subroutine to set the date and time of the e-smith server
=begin testing
$FM->{cgi}->param(-name=>'day', -value=>'55');
$FM->{cgi}->param(-name=>'year', -value=>'5');
is($FM->_performSetDateTime(), '', 'performSetDateTime');
like($_STDOUT_, qr/INVALID_DAY/, ' .. invalid day check');
$FM->{cgi}->param(-name=>'day', -value=>'10');
$FM->_performSetDateTime();
like($_STDOUT_, qr/INVALID_YEAR/, ' .. invalid year check');
$FM->{cgi}->param(-name=>'year', -value=>'1984');
$FM->{cgi}->param(-name=>'hour', -value=>'60');
$FM->{cgi}->param(-name=>'minute', -value=>'90');
$FM->_performSetDateTime();
like($_STDOUT_, qr/INVALID_HOUR/, ' .. invalid hour check');
$FM->{cgi}->param(-name=>'hour', -value=>'12');
$FM->_performSetDateTime();
like($_STDOUT_, qr/INVALID_MINUTE/, ' .. invalid minute check');
$FM->{cgi}->param(-name=>'minute', -value=>'00');
$FM->{cgi}->param(-name=>'second', -value=>'00');
SKIP: {
skip "unsafe!", 2 unless esmith::TestUtils::destruction_ok();
is($FM->_performSetDateTime(), '', ' .. exited happily');
like($_STDOUT_, qr/UPDATING_CLOCK/, ' .. updating clock in background');
}
=end testing
=cut
sub _performSetDateTime
{
my $self = shift;
my $q = $self->{cgi};
# Turn on autoflush, since setting the time forward makes Apache
# stop waiting for CGI output:
$| = 1;
#--------------------------------------------------
# Untaint parameters and check for validity
#--------------------------------------------------
my $timezone = $q->param ('timezone');
if ($timezone =~ /^([\w\-]+\/?[\w\-+]*)$/) {
$timezone = $1;
} else {
$timezone = "US/Eastern";
}
my $month = $q->param ('month');
if ($month =~ /^(\d{1,2})$/) {
$month = $1;
} else {
$month = "1";
}
if (($month < 1) || ($month > 12))
{
$q->param(-name => "status_message",
-value => $q->p($self->localise('INVALID_MONTH')." $month. ".
$self->localise('MONTH_BETWEEN_1_AND_12')));
return $self->error($self->localise('INVALID_MONTH')." $month. ".
$self->localise('MONTH_BETWEEN_1_AND_12'));
}
my $day = $q->param ('day');
if ($day =~ /^(\d{1,2})$/) {
$day = $1;
} else {
$day = "1";
}
if (($day < 1) || ($day > 31))
{
$q->param(-name => "status_message",
-value => $q->p($self->localise('INVALID_DAY')." $day. ".
$self->localise('BETWEEN_1_AND_31')));
return $self->error($self->localise('INVALID_DAY')." $day. ".
$self->localise('BETWEEN_1_AND_31'));
}
my $year = $q->param ('year');
if ($year =~ /^(\d{4})$/) {
$year = $1;
} else {
$year = "2000";
}
if (($year < 1900) || ($year > 2200))
{
$q->param(-name => "status_message",
-value => $q->p($self->localise('INVALID_YEAR')." $year. ".
$self->localise('FOUR_DIGIT_YEAR')));
return $self->error($self->localise('INVALID_YEAR')." $year. ".
$self->localise('FOUR_DIGIT_YEAR'));
}
my $hour = $q->param ('hour');
if ($hour =~ /^(\d{1,2})$/) {
$hour = $1;
} else {
$hour = "12";
}
if (($hour < 1) || ($hour > 12))
{
$q->param(-name => "status_message",
-value => $q->p($self->localise('INVALID_HOUR')." $hour. ".
$self->localise('BETWEEN_1_AND_12')));
return $self->error($self->localise('INVALID_HOUR')." $hour. ".
$self->localise('BETWEEN_1_AND_12'));
}
my $minute = $q->param ('minute');
if ($minute =~ /^(\d{1,2})$/) {
$minute = $1;
} else {
$minute = "0";
}
if (($minute < 0) || ($minute > 59))
{
$q->param(-name => "status_message",
-value => $q->p($self->localise('INVALID_MINUTE')." $minute. ".
$self->localise('BETWEEN_0_AND_59')));
return $self->error(self->localise('INVALID_MINUTE')." $minute. ".
$self->localise('BETWEEN_0_AND_59'));
}
my $second = $q->param ('second');
if ($second =~ /^(\d{1,2})$/) {
$second = $1;
} else {
$second = "0";
}
if (($second < 0) || ($second > 59))
{
$q->param(-name => "status_message",
-value => $q->p($self->localise('INVALID_SECOND')." $second. ".
$self->localise('BETWEEN_0_AND_59')));
return $self->error($self->localise('INVALID_SECOND')." $second. ".
$self->localise('BETWEEN_0_AND_59'));
}
my $ampm = $q->param ('ampm');
if ($ampm =~ /^(AM|PM)$/) {
$ampm = $1;
} else {
$ampm = "AM";
}
# convert to 24 hour time
$hour = $hour % 12;
if ($ampm eq "PM")
{
$hour = $hour + 12;
}
#--------------------------------------------------
# Store time zone in configuration database
#--------------------------------------------------
my $conf = esmith::ConfigDB->open();
my $old = $conf->get('UnsavedChanges')->value;
my $rec = $conf->get('TimeZone');
unless ($rec)
{
$rec = $conf->new_record('TimeZone',undef);
}
$rec->set_value($timezone);
$conf->get('UnsavedChanges')->set_value($old);
#--------------------------------------------------
# Signal event to change time zone, system time
# and hardware clock
#--------------------------------------------------
my $newdate = sprintf "%02d%02d%02d%02d%04d.%02d",
$month, $day, $hour, $minute, $year, $second;
esmith::util::backgroundCommand(2,
"/sbin/e-smith/signal-event","timezone-update",$newdate);
return $self->success('UPDATING_CLOCK');
}
=pod
=head2 _performSetTimeserver
Subroutine to set the NTP server
=begin testing
SKIP: {
skip "unsafe!", 3 unless esmith::TestUtils::destruction_ok();
is($FM->_performSetTimeserver(), '', 'performSetTimeserver');
$FM->{cgi}->param(-name=>'ntpEnabled', -value=>'on');
$FM->{cgi}->param(-name=>'ntpServer', -value=>'time.nrc.ca');
$FM->_performSetTimeserver();
like($_STDOUT_, qr/SETTINGS_CHANGED/, ' .. changed settings');
$FM->{cgi}->param(-name=>'ntpEnabled', -value=>'off');
$FM->_performSetTimeserver();
like($_STDOUT_, qr/SETTINGS_CHANGED/, ' .. and disabled');
}
=end testing
=cut
sub _performSetTimeserver
{
my $self = shift;
my $q = $self->{cgi};
my $msg;
#------------------------------------------------------------
# Verify the arguments and untaint the variables (see Camel
# book, "Detecting and laundering tainted data", pg. 358)
#------------------------------------------------------------
my $newStatus = '';
my $ntpServer = '';
if ($q->param ('ntpStatus') eq 'enabled')
{
$newStatus = "on";
}
else
{
$newStatus = "off";
}
if (defined ($q->param ('ntpServer')))
{
$ntpServer = $q->param ('ntpServer');
}
else
{
$ntpServer = "";
}
if ($newStatus ne "on") # asking to have NTP disabled
{
# make sure that the parameters are set for disabled
my $conf = esmith::ConfigDB->open();
my $old = $conf->get('UnsavedChanges')->value;
my $rec = $conf->get('ntpd');
if ($rec)
{
$rec->set_prop('status', ($rec->prop('SyncToHWClockSupported') || 'yes') eq 'yes' ? 'enabled' : 'disabled');
$rec->set_prop('NTPServer','');
}
else
{
$rec = $conf->new_record('ntpd',
{type=>'service', status=>'enabled', SyncToHWClockSupported => 'yes', NTPServer=>''});
}
$conf->get('UnsavedChanges')->set_value($old);
$msg = 'SERVER_DISABLED_DESC';
}
else # enable service and synch with ntpServer
{
if ($ntpServer eq "pool.ntp.org")
{
return $self->error('INVALID_NTP_ADDR');
}
elsif ($ntpServer =~ /^([a-zA-Z0-9\.\-]+)$/)
{
$ntpServer = $1;
}
elsif ($ntpServer =~ /^\s*$/)
{
$ntpServer = "";
}
else
{
return $self->error('INVALID_NTP_ADDR');
}
#------------------------------------------------------------
# Looks good; go ahead and change the parameters.
#------------------------------------------------------------
my $conf = esmith::ConfigDB->open();
my $old = $conf->get('UnsavedChanges')->value;
my $rec = $conf->get('ntpd');
if ($rec)
{
$rec->set_prop('status','enabled');
$rec->set_prop('NTPServer',$ntpServer);
}
else
{
$rec = $conf->new_record('ntpd',
{type=>'service',status=>'enabled',SyncToHWClockSupported => 'yes',NTPServer=>$ntpServer});
}
$conf->get('UnsavedChanges')->set_value($old);
$msg = 'SETTINGS_CHANGED';
if ($ntpServer =~ /^\s*$/)
{
$rec->set_prop('status', ($rec->prop('SyncToHWClockSupported') || 'yes') eq 'yes' ? 'enabled' : 'disabled');
$rec->set_prop('NTPServer', '');
$msg = 'INVALID_NTP_SERVER' if ($rec->prop('SyncToHWClockSupported') || 'yes') ne 'yes';
}
}
esmith::util::backgroundCommand(2,
"/sbin/e-smith/signal-event", "timeserver-update");
return $self->success($msg);
}
1;