2001-06-16 07:16:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
2002-06-13 22:14:37 +00:00
|
|
|
# $FreeBSD$
|
2001-06-16 07:16:14 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
# PROVIDE: ntpd
|
2012-09-11 05:04:59 +00:00
|
|
|
# REQUIRE: DAEMON ntpdate FILESYSTEMS devfs
|
2002-06-13 22:14:37 +00:00
|
|
|
# BEFORE: LOGIN
|
2008-07-16 19:50:29 +00:00
|
|
|
# KEYWORD: nojail shutdown
|
2001-06-16 07:16:14 +00:00
|
|
|
|
|
|
|
. /etc/rc.subr
|
|
|
|
|
2009-05-30 21:51:38 +00:00
|
|
|
name="ntpd"
|
2016-04-23 16:10:54 +00:00
|
|
|
desc="Network Time Protocol daemon"
|
2012-01-14 02:18:41 +00:00
|
|
|
rcvar="ntpd_enable"
|
2002-08-14 05:44:32 +00:00
|
|
|
command="/usr/sbin/${name}"
|
|
|
|
pidfile="/var/run/${name}.pid"
|
2016-01-26 07:06:44 +00:00
|
|
|
extra_commands="fetch"
|
|
|
|
fetch_cmd="ntpd_fetch_leapfile"
|
2004-09-14 03:01:38 +00:00
|
|
|
start_precmd="ntpd_precmd"
|
2006-04-18 15:02:24 +00:00
|
|
|
|
|
|
|
load_rc_config $name
|
|
|
|
|
2002-06-13 22:14:37 +00:00
|
|
|
ntpd_precmd()
|
|
|
|
{
|
2006-04-18 15:02:24 +00:00
|
|
|
rc_flags="-c ${ntpd_config} ${ntpd_flags}"
|
|
|
|
|
2004-09-14 03:01:38 +00:00
|
|
|
if checkyesno ntpd_sync_on_start; then
|
2009-01-29 06:43:29 +00:00
|
|
|
rc_flags="-g $rc_flags"
|
2004-09-14 03:01:38 +00:00
|
|
|
fi
|
|
|
|
|
2016-08-25 13:24:11 +00:00
|
|
|
ntpd_init_leapfile
|
2016-08-25 03:09:23 +00:00
|
|
|
|
2016-01-26 07:06:44 +00:00
|
|
|
if [ ! -f $ntp_db_leapfile ]; then
|
|
|
|
ntpd_fetch_leapfile
|
|
|
|
fi
|
|
|
|
|
2016-02-11 01:16:57 +00:00
|
|
|
if [ -z "$ntpd_chrootdir" ]; then
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
|
2002-06-13 22:14:37 +00:00
|
|
|
# If running in a chroot cage, ensure that the appropriate files
|
2002-10-12 10:31:31 +00:00
|
|
|
# exist inside the cage, as well as helper symlinks into the cage
|
2002-06-13 22:14:37 +00:00
|
|
|
# from outside.
|
|
|
|
#
|
|
|
|
# As this is called after the is_running and required_dir checks
|
|
|
|
# are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
|
|
|
|
# exists and ntpd isn't running at this point (unless forcestart
|
|
|
|
# is used).
|
|
|
|
#
|
|
|
|
if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
|
|
|
|
rm -f "${ntpd_chrootdir}/dev/clockctl"
|
|
|
|
( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
|
|
|
|
fi
|
|
|
|
ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
|
2016-01-26 07:06:44 +00:00
|
|
|
ln -fs "${ntpd_chrootdir}${ntp_tmp_leapfile}" ${ntp_tmp_leapfile}
|
2002-06-13 22:14:37 +00:00
|
|
|
|
|
|
|
# Change run_rc_commands()'s internal copy of $ntpd_flags
|
|
|
|
#
|
|
|
|
rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
|
|
|
|
}
|
|
|
|
|
2016-01-26 07:06:44 +00:00
|
|
|
current_ntp_ts() {
|
|
|
|
# Seconds between 1900-01-01 and 1970-01-01
|
|
|
|
# echo $(((70*365+17)*86400))
|
|
|
|
ntp_to_unix=2208988800
|
|
|
|
|
|
|
|
echo $(($(date -u +%s)+$ntp_to_unix))
|
|
|
|
}
|
|
|
|
|
|
|
|
get_ntp_leapfile_ver() {
|
2016-08-25 02:45:52 +00:00
|
|
|
# Leapfile update date (version number).
|
2016-01-26 07:06:44 +00:00
|
|
|
expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \
|
|
|
|
'^\([1-9][0-9]*\)$' \| 0
|
|
|
|
}
|
|
|
|
|
|
|
|
get_ntp_leapfile_expiry() {
|
2016-08-25 02:45:52 +00:00
|
|
|
# Leapfile expiry date.
|
2016-01-26 07:06:44 +00:00
|
|
|
expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \
|
|
|
|
'^\([1-9][0-9]*\)$' \| 0
|
|
|
|
}
|
|
|
|
|
2016-08-25 13:24:11 +00:00
|
|
|
ntpd_init_leapfile() {
|
2016-08-25 03:09:23 +00:00
|
|
|
# Refresh working leapfile with an invalid hash due to
|
|
|
|
# FreeBSD id header. Ntpd will ignore leapfiles with a
|
|
|
|
# mismatch hash. The file must be the virgin file from
|
|
|
|
# the source.
|
2016-08-25 13:24:11 +00:00
|
|
|
if [ ! -f $ntp_db_leapfile ]; then
|
2016-08-25 03:09:23 +00:00
|
|
|
cp -p $ntp_src_leapfile $ntp_db_leapfile
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-01-26 07:06:44 +00:00
|
|
|
ntpd_fetch_leapfile() {
|
|
|
|
local ntp_tmp_leapfile rc verbose
|
|
|
|
|
|
|
|
if checkyesno ntp_leapfile_fetch_verbose; then
|
|
|
|
verbose=echo
|
|
|
|
else
|
|
|
|
verbose=:
|
|
|
|
fi
|
|
|
|
|
|
|
|
ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list"
|
|
|
|
|
|
|
|
ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile)
|
2016-08-25 02:45:52 +00:00
|
|
|
ntp_expiry_src=$(get_ntp_leapfile_expiry $ntp_src_leapfile)
|
2016-01-26 07:06:44 +00:00
|
|
|
ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile)
|
2016-08-25 02:45:52 +00:00
|
|
|
ntp_expiry_db=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
|
2016-01-26 07:06:44 +00:00
|
|
|
$verbose ntp_src_leapfile version is $ntp_ver_no_src
|
|
|
|
$verbose ntp_db_leapfile version is $ntp_ver_no_db
|
|
|
|
|
2016-08-25 02:45:52 +00:00
|
|
|
if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" -o \
|
|
|
|
"$ntp_ver_no_src" -eq "$ntp_ver_no_db" -a \
|
|
|
|
"$ntp_expiry_src" -gt "$ntp_expiry_db" ]; then
|
2016-01-26 07:06:44 +00:00
|
|
|
$verbose replacing $ntp_db_leapfile with $ntp_src_leapfile
|
|
|
|
cp -p $ntp_src_leapfile $ntp_db_leapfile
|
|
|
|
ntp_ver_no_db=$ntp_ver_no_src
|
|
|
|
else
|
|
|
|
$verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile
|
|
|
|
fi
|
|
|
|
ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400))
|
2016-08-25 02:45:52 +00:00
|
|
|
ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile)
|
2016-01-26 07:06:44 +00:00
|
|
|
ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds))
|
|
|
|
if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then
|
|
|
|
$verbose Within ntp leapfile expiry limit, initiating fetch
|
|
|
|
for url in $ntp_leapfile_sources ; do
|
|
|
|
$verbose fetching $url
|
2016-01-27 02:25:25 +00:00
|
|
|
fetch $ntp_leapfile_fetch_opts -o $ntp_tmp_leapfile $url && break
|
2016-01-26 07:06:44 +00:00
|
|
|
done
|
2016-08-25 02:45:52 +00:00
|
|
|
ntp_ver_no_tmp=$(get_ntp_leapfile_ver $ntp_tmp_leapfile)
|
2016-05-25 01:35:02 +00:00
|
|
|
ntp_expiry_tmp=$(get_ntp_leapfile_expiry $ntp_tmp_leapfile)
|
2016-08-25 02:45:52 +00:00
|
|
|
if [ "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" -o \
|
|
|
|
"$ntp_ver_no_tmp" -eq "$ntp_ver_no_db" -a \
|
|
|
|
"$ntp_expiry_tmp" -gt "$ntp_expiry_db" ]; then
|
2016-01-26 07:06:44 +00:00
|
|
|
$verbose using $url as $ntp_db_leapfile
|
|
|
|
mv $ntp_tmp_leapfile $ntp_db_leapfile
|
|
|
|
else
|
|
|
|
$verbose using existing $ntp_db_leapfile
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2001-06-16 07:16:14 +00:00
|
|
|
run_rc_command "$1"
|