File conf.ntp.init of Package ntp.import4677

#! /bin/sh
# Copyright (c) 1995-2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Michael Andres
# Author: Michael Skibbe
# Author: Andreas Schneider
#
# /etc/init.d/ntp
#   and its symbolic link
# /usr/sbin/rcntp
#
### BEGIN INIT INFO
# Provides:       ntp ntpd xntpd
# Required-Start: $network $remote_fs $syslog $named
# Required-Stop:  $network $remote_fs $syslog
# Should-Start: network-remotefs
# Should-Stop: network-remotefs
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Network time protocol daemon (ntpd)
# Description:    Start network time protocol daemon (NTPD).
### END INIT INFO

# First reset status of this service
. /etc/rc.status
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

# set default options
NTP_CONF="/etc/ntp.conf"
if [ ! -f ${NTP_CONF} ]; then
  echo -n "Time server configuration file, ${NTP_CONF} does not exist."
  # Tell the user this has skipped
  rc_status -s
  exit 6
fi

NTPD_BIN="/usr/sbin/ntpd"
if [ ! -x ${NTPD_BIN} ]; then
  echo -n "Time server, ${NTPD_BIN} not installed!"
  rc_status -s
  exit 5
fi

NTPD_OPTIONS="-g -u ntp:ntp"
NTPD_RUN_CHROOTED="yes"

NTPDC_BIN="/usr/sbin/ntpdc"

NTP_KEYS=$(awk '/^keys/ { print $2 }' $NTP_CONF)
NTP_KEYID=$(awk '/^requestkey[[:blank:]]/ { print $2 }' $NTP_CONF)
NTP_PASSWD=$(test -n "$NTP_KEYS" -a -n "$NTP_KEYID" -a -r "$NTP_KEYS" && awk '$0 ~ key { print $3 }' key="^[[:blank:]]*$NTP_KEYID[[:blank:]]" $NTP_KEYS)

if [ -n "$NTP_KEYS" ]; then
  if test -z "$NTP_KEYID"; then
    echo -n "NTP key id not defined"
      rc_status -s
      exit 5
  fi
  if test -z "$NTP_PASSWD"; then
    echo -n "No password for requestkey set"
    exit 1
  fi
fi

# Override defaults, if we have the sysconfig file
test -f /etc/sysconfig/ntp && . /etc/sysconfig/ntp

# set Default CHROOT path if not set but wanted
test "${NTPD_RUN_CHROOTED}" = "yes" && \
  CHROOT_PREFIX="/var/lib/ntp" || \
  CHROOT_PREFIX=""

# set default PID variables
NTPD_PID="${CHROOT_PREFIX}/var/run/ntp/ntpd.pid"
NTPD_PID_NOPREFIX="/var/run/ntp/ntpd.pid"

# Create if /var/run is on tmpfs
test -e /var/run/ntp || ln -s ../lib/ntp/var/run/ntp /var/run

function ntpd_is_running() {
  $0 status >/dev/null
}

function parse_symlink() {
  if [ -c "$NTP_PARSE_DEVICE" ]; then
    if [ -n "$NTP_PARSE_LINK" ]; then
    ln -sf $NTP_PARSE_DEVICE $NTP_PARSE_LINK
    fi
  fi
}

function prepare_chroot() {
  for configfile in /etc/{localtime,ntp.keys} $NTP_CONF $NTPD_CHROOT_FILES; do
    test -d ${CHROOT_PREFIX}${configfile%/*} || mkdir -p ${CHROOT_PREFIX}${configfile%/*}
    if [ -r ${configfile} ]
    then
    	cp -auL ${configfile} ${CHROOT_PREFIX}${configfile%/*}
    else
        echo
	echo "Warning: ${configfile} not found or not readable"
    fi
  done
  mkdir -p ${CHROOT_PREFIX}/proc
  # prepare_chroot is called on every "start", but we need to mount only once
  if ! grep -q " ${CHROOT_PREFIX}/proc proc " /proc/mounts; then
    mount -o bind /proc ${CHROOT_PREFIX}/proc 2>/dev/null
    mount -o remount,ro,nosuid,nodev,bind ${CHROOT_PREFIX}/proc 2>/dev/null
  fi
  NTPD_OPTIONS="${NTPD_OPTIONS} -i ${CHROOT_PREFIX}"
}

function runtime_configuration() {
  for f in /var/run/ntp/servers*; do
    if [ -r ${f} ]; then
      . ${f}
      ntp_server="${ntp_server} ${RUNTIME_SERVERS}"
    fi
  done
  if [ -n "${ntp_server}" ]; then
    for s in ${ntp_server}; do
      add_runtime_server ${s}
    done
  fi
}

function add_runtime_server() {
  /usr/sbin/sntp -s $@
  NTPC_CMD="keyid $NTP_KEYID\npasswd $NTP_PASSWD\naddserver $@\n"
  NTPDC_LOG=$(echo -e "${NTPC_CMD}quit" | $NTPDC_BIN)
  logger -t $0 "runtime configuration: $NTPDC_LOG"
}

function reloaddevices {
  NTPC_CMD="keyid $NTP_KEYID\npasswd $NTP_PASSWD\nifreload\n"
  NTPDC_LOG=$(echo -e "${NTPC_CMD}quit" | $NTPDC_BIN)
  logger -t $0 "runtime configuration: $NTPDC_LOG"
}

function get_ntpd_ip_proto() {
  local -a OPTS
  read -ra OPTS <<< "$NTPD_OPTIONS"
  for i in "${OPTS[@]}"; do
    if [ "$i" = "-4" ] || [ "$i" = "-6" ]; then
      # first occurrence wins safely because ntpd couldn't handle more anyway
      echo "$i"
      return
    fi
  done
  echo ""
  return
}

case "$1" in
  start)
    if [ "$NTPD_FORCE_SYNC_ON_STARTUP" = "yes" ]; then
        # get the initial date from the timeservers configured in ntp.conf
        ntpd_is_running || $0 ntptimeset
    fi
    echo -n "Starting network time protocol daemon (NTPD)"
    # do we need a refclock symlink?
    parse_symlink
    # do we run chrooted?
    test "${NTPD_RUN_CHROOTED}" = "yes" && prepare_chroot

    startproc $NTPD_BIN -p ${NTPD_PID_NOPREFIX} $NTPD_OPTIONS -c $NTP_CONF

    if [ -n "${NTP_KEYS}" ]; then
      runtime_configuration
    fi

    rc_status -v
  ;;
  stop)
    echo -n "Shutting down network time protocol daemon (NTPD)"
    killproc -p ${NTPD_PID} -TERM $NTPD_BIN
    rc_status -v
    test -n "${CHROOT_PREFIX}" && umount ${CHROOT_PREFIX}/proc 2>/dev/null
    rm -f ${NTPD_PID} 2>/dev/null
  ;;
  try-restart)
    $0 status
    if test $? = 0; then
      $0 restart $2
    else
      rc_reset        # Not running is not a failure.
    fi
    # Remember status and be quiet
    rc_status
  ;;
  restart)
    $0 stop
    $0 start $2
    rc_status
  ;;
  try-restart-iburst)
    $0 status
    if test $? = 0; then
      $0 stop
      $0 start iburst
    else
      rc_reset        # Not running is not a failure.
    fi
    # Remember status and be quiet
    rc_status
  ;;
  addserver)
    if [ -z "${NTP_KEYS}" ]; then
      echo "Runtime configuration disabled, no key file specified."
      exit 1
    fi

    if test $# -lt 2; then
      echo "No servers to add specified"
      exit 1
    fi
    shift

    $0 status 2>&1 >/dev/null
    if test $? = 0;
    then
      add_runtime_server "$@"
    fi
  ;;
  force-reload)
    # Does not support signalling to reload
    $0 try-restart
    rc_status
  ;;
  ntptimeset)
    NTPD_PROTO="$( get_ntpd_ip_proto )"
    for i in $(gawk '/^server/ { if( $2 !~ "^127.127." ) print $2","$3 }' $NTP_CONF)
    do
        IFS=',' read -ra SERVER <<< "$i"
        if [ ${SERVER[0]} = "-4" ] || [ ${SERVER[0]} = "-6" ]
        then
            if [ -z "${NTPD_PROTO}" ] || [ "${NTPD_PROTO}" = "${SERVER[0]}" ]
            then
                SNTP_OPT="${SERVER[0]} ${SERVER[1]}"
            else
                # ignore servers with conflicting proto
                continue
            fi
        else
            # here the usual case with empty NTPD_PROTO (default/yast config)
            SNTP_OPT="$NTPD_PROTO ${SERVER[0]}"
        fi
        sntp -t 2 -l /dev/null -s $SNTP_OPT 2> /dev/null && { SYNCHRONISED=$SNTP_OPT; break; };
    done
    if [ "$SYNCHRONISED" ]
    then
            echo "Time synchronized with $SYNCHRONISED"
    else
            echo "Time could not be synchronized"
    fi
  ;;
  reload)
    echo -n "Reload network time protocol daemon (NTPD)"
    # Does not support signalling to reload
    rc_failed 3
    rc_status -v
  ;;
  status)
    checkproc -p ${NTPD_PID} $NTPD_BIN
    if test $? -eq 0; then
      ntpq -p
      echo ""
    fi
    echo -n "Checking for network time protocol daemon (NTPD): "
    checkproc -p ${NTPD_PID} $NTPD_BIN
    rc_status -v
  ;;
  *)
    echo "Usage: $0 {start|stop|status|try-restart|restart|try-restart-iburst|force-reload|reload|addserver|ntptimeset}"
    exit 1
  ;;
esac
rc_exit
openSUSE Build Service is sponsored by