178344Sobrien#!/bin/sh 278344Sobrien# 398184Sgordon# $FreeBSD: stable/10/etc/rc.d/ntpd 335952 2018-07-04 14:12:09Z ian $ 478344Sobrien# 578344Sobrien 678344Sobrien# PROVIDE: ntpd 7240336Sobrien# REQUIRE: DAEMON ntpdate FILESYSTEMS devfs 898184Sgordon# BEFORE: LOGIN 9180564Sdougb# KEYWORD: nojail shutdown 1078344Sobrien 1178344Sobrien. /etc/rc.subr 1278344Sobrien 13193119Sdougbname="ntpd" 14230099Sdougbrcvar="ntpd_enable" 15101851Sgordoncommand="/usr/sbin/${name}" 16101851Sgordonpidfile="/var/run/${name}.pid" 17295461Scyextra_commands="fetch" 18295461Scyfetch_cmd="ntpd_fetch_leapfile" 19135194Sseancstart_precmd="ntpd_precmd" 20101851Sgordon 21335952Sian_ntp_tmp_leapfile="/var/run/ntpd.leap-seconds.list" 22326604Scy 23157840Sflzload_rc_config $name 24157840Sflz 2598184Sgordonntpd_precmd() 2698184Sgordon{ 27157840Sflz rc_flags="-c ${ntpd_config} ${ntpd_flags}" 28157840Sflz 29135194Sseanc if checkyesno ntpd_sync_on_start; then 30187879Skeramida rc_flags="-g $rc_flags" 31135194Sseanc fi 32135194Sseanc 33304879Scy ntpd_init_leapfile 34304879Scy 35295619Scy if [ ! -f $ntp_db_leapfile ]; then 36295619Scy ntpd_fetch_leapfile 37295619Scy fi 38295619Scy 3998184Sgordon if [ -z "$ntpd_chrootdir" ]; then 4098184Sgordon return 0; 4198184Sgordon fi 4298184Sgordon 4398184Sgordon # If running in a chroot cage, ensure that the appropriate files 44104980Sschweikh # exist inside the cage, as well as helper symlinks into the cage 4598184Sgordon # from outside. 4698184Sgordon # 4798184Sgordon # As this is called after the is_running and required_dir checks 4898184Sgordon # are made in run_rc_command(), we can safely assume ${ntpd_chrootdir} 4998184Sgordon # exists and ntpd isn't running at this point (unless forcestart 5098184Sgordon # is used). 5198184Sgordon # 5298184Sgordon if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then 5398184Sgordon rm -f "${ntpd_chrootdir}/dev/clockctl" 5498184Sgordon ( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" ) 5598184Sgordon fi 5698184Sgordon ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift 57335952Sian ln -fs "${ntpd_chrootdir}${_ntp_tmp_leapfile}" ${_ntp_tmp_leapfile} 5898184Sgordon 5998184Sgordon # Change run_rc_commands()'s internal copy of $ntpd_flags 6098184Sgordon # 6198184Sgordon rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags" 6298184Sgordon} 6398184Sgordon 64295461Scycurrent_ntp_ts() { 65295461Scy # Seconds between 1900-01-01 and 1970-01-01 66295461Scy # echo $(((70*365+17)*86400)) 67295461Scy ntp_to_unix=2208988800 68295461Scy 69295461Scy echo $(($(date -u +%s)+$ntp_to_unix)) 70295461Scy} 71295461Scy 72295461Scyget_ntp_leapfile_ver() { 73304879Scy # Leapfile update date (version number). 74295461Scy expr "$(awk '$1 == "#$" { print $2 }' "$1" 2>/dev/null)" : \ 75295461Scy '^\([1-9][0-9]*\)$' \| 0 76295461Scy} 77295461Scy 78295461Scyget_ntp_leapfile_expiry() { 79304879Scy # Leapfile expiry date. 80295461Scy expr "$(awk '$1 == "#@" { print $2 }' "$1" 2>/dev/null)" : \ 81295461Scy '^\([1-9][0-9]*\)$' \| 0 82295461Scy} 83295461Scy 84304879Scyntpd_init_leapfile() { 85304879Scy # Refresh working leapfile with an invalid hash due to 86304879Scy # FreeBSD id header. Ntpd will ignore leapfiles with a 87304879Scy # mismatch hash. The file must be the virgin file from 88304879Scy # the source. 89304879Scy if [ ! -f $ntp_db_leapfile ]; then 90304879Scy cp -p $ntp_src_leapfile $ntp_db_leapfile 91304879Scy fi 92304879Scy} 93304879Scy 94295461Scyntpd_fetch_leapfile() { 95326604Scy local rc verbose 96295461Scy 97295461Scy if checkyesno ntp_leapfile_fetch_verbose; then 98295461Scy verbose=echo 99295461Scy else 100295461Scy verbose=: 101295461Scy fi 102295461Scy 103295461Scy ntp_ver_no_src=$(get_ntp_leapfile_ver $ntp_src_leapfile) 104304879Scy ntp_expiry_src=$(get_ntp_leapfile_expiry $ntp_src_leapfile) 105295461Scy ntp_ver_no_db=$(get_ntp_leapfile_ver $ntp_db_leapfile) 106304879Scy ntp_expiry_db=$(get_ntp_leapfile_expiry $ntp_db_leapfile) 107295461Scy $verbose ntp_src_leapfile version is $ntp_ver_no_src 108295461Scy $verbose ntp_db_leapfile version is $ntp_ver_no_db 109295461Scy 110304879Scy if [ "$ntp_ver_no_src" -gt "$ntp_ver_no_db" -o \ 111304879Scy "$ntp_ver_no_src" -eq "$ntp_ver_no_db" -a \ 112304879Scy "$ntp_expiry_src" -gt "$ntp_expiry_db" ]; then 113295461Scy $verbose replacing $ntp_db_leapfile with $ntp_src_leapfile 114295461Scy cp -p $ntp_src_leapfile $ntp_db_leapfile 115295461Scy ntp_ver_no_db=$ntp_ver_no_src 116295461Scy else 117295461Scy $verbose not replacing $ntp_db_leapfile with $ntp_src_leapfile 118295461Scy fi 119304879Scy ntp_leapfile_expiry_seconds=$((ntp_leapfile_expiry_days*86400)) 120295461Scy ntp_leap_expiry=$(get_ntp_leapfile_expiry $ntp_db_leapfile) 121295461Scy ntp_leap_fetch_date=$((ntp_leap_expiry-ntp_leapfile_expiry_seconds)) 122295461Scy if [ $(current_ntp_ts) -ge $ntp_leap_fetch_date ]; then 123295461Scy $verbose Within ntp leapfile expiry limit, initiating fetch 124295461Scy for url in $ntp_leapfile_sources ; do 125295461Scy $verbose fetching $url 126335952Sian fetch $ntp_leapfile_fetch_opts -o $_ntp_tmp_leapfile $url && break 127295461Scy done 128335952Sian ntp_ver_no_tmp=$(get_ntp_leapfile_ver $_ntp_tmp_leapfile) 129335952Sian ntp_expiry_tmp=$(get_ntp_leapfile_expiry $_ntp_tmp_leapfile) 130327859Scy if [ "$ntp_expiry_tmp" -gt "$ntp_expiry_db" -o \ 131327859Scy "$ntp_expiry_tmp" -eq "$ntp_expiry_db" -a \ 132327859Scy "$ntp_ver_no_tmp" -gt "$ntp_ver_no_db" ]; then 133295461Scy $verbose using $url as $ntp_db_leapfile 134335952Sian mv -f $_ntp_tmp_leapfile $ntp_db_leapfile || 135335952Sian $verbose "warning: cannot replace $ntp_db_leapfile (read-only fs?)" 136295461Scy else 137295461Scy $verbose using existing $ntp_db_leapfile 138295461Scy fi 139295461Scy fi 140295461Scy} 141295461Scy 14278344Sobrienrun_rc_command "$1" 143