File conf.start-ntpd of Package ntp.9120

#!/bin/bash
# Copyright (c) 1995-2014 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Peter Varkoly

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

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

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

NTPQ_BIN="/usr/sbin/ntpq"

NTP_KEYS=$(awk '/^keys[[:blank:]]/ { print $2; exit }' $NTP_CONF)
NTP_KEYID=$(awk '/^controlkey[[:blank:]]/ { print $2; exit }' $NTP_CONF)
if test -n "$NTP_KEYS" -a -n "$NTP_KEYID" -a -r "$NTP_KEYS"; then
    NTP_KEYTYPE=$(awk '$1 == "'$NTP_KEYID'"{ print $2 }' $NTP_KEYS)
    NTP_PASSWD=$(awk '$1 == "'$NTP_KEYID'"{ print $3 }' $NTP_KEYS)
fi

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

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

function update_cmos() { return 0; }
# Now see if we have to fix the CMOS clock
if [ "$NTPD_FORCE_SYNC_ON_STARTUP" = yes -a "$1" = ntptimeset ] ; then
  test -f /etc/sysconfig/clock && . /etc/sysconfig/clock
  if test -r /proc/xen/capabilities ; then
    read -t1 caps < /proc/xen/capabilities
    test "$caps" = "${caps%control_d*}" && NTPD_FORCE_SYNC_HWCLOCK_ON_STARTUP=no
  fi
  case "$(uname -i)" in
  s390*) NTPD_FORCE_SYNC_HWCLOCK_ON_STARTUP=no
  esac
  if [ "$NTPD_FORCE_SYNC_HWCLOCK_ON_STARTUP" = yes ] ; then
    function update_cmos() {
      if test -e /dev/rtc ; then
	/sbin/hwclock --systohc $HWCLOCK
	return $?
      fi
    }
  fi
fi

# 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"

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

function ntpd_is_running() {
  service ntpd 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 -aL ${configfile} ${CHROOT_PREFIX}${configfile%/*}
    else
        echo
	echo "Warning: ${configfile} not found or not readable"
    fi
  done
  mkdir -p ${CHROOT_PREFIX}/var/log
  mkdir -p ${CHROOT_PREFIX}/proc
  mount -t proc none -o ro,nosuid,nodev "${CHROOT_PREFIX}/proc" 2>/dev/null
  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() {
  NTPC_CMD="keytype $NTP_KEYTYPE\nkeyid $NTP_KEYID\npasswd $NTP_PASSWD\n:config server $@\n"
  NTPQ_LOG=$(echo -e "${NTPC_CMD}quit" | $NTPQ_BIN)
  logger -t $0 "runtime configuration: server $@"
}

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

    $NTPD_BIN -p ${NTPD_PID#${CHROOT_PREFIX}} $NTPD_OPTIONS -c $NTP_CONF

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

  ;;
  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

    service ntpd status 2>&1 >/dev/null
    if test $? = 0;
    then
      add_runtime_server "$@"
    fi
  ;;
  ntptimeset)
    NTPD_PROTO="$( get_ntpd_ip_proto )"
    while read arg1 arg2 arg3 dummy
    do
        test "$arg1" = "server" || continue
        if [ "$arg2" = "-4" -o "$arg2" = "-6" ]
        then
            srv="$arg3"
	    proto="$arg2"
            args="$arg2 $arg3"
        else
            srv="$arg2"
	    proto=""
            args="$arg2"
        fi
	# ignore servers with conflicting IP version
	test -n "$NTP_PROTO" -a "NTPD_PROTO" != "$proto" && continue
	# ignore hardware clock drivers
        test "${srv#127.127.}" != "$srv" && continue
        if /usr/sbin/sntp -S -c $args &> /dev/null
        then
            SYNCHRONISED=$srv
            break
        fi
    done < /etc/ntp.conf
    if [ "$SYNCHRONISED" ]
    then
	    echo "Time synchronized with $SYNCHRONISED"
	    update_cmos
    else
	    echo "Time could not be synchronized"
    fi
  ;;
  *)
    echo "Usage: $0 {start|addserver|ntptimeset}"
    exit 1
  ;;
esac

openSUSE Build Service is sponsored by