#!/bin/bash

CONF_FILE="/etc/ntp.conf"
PATH=/usr/sbin:/sbin:$PATH
hostName="$2"
ntpversion="$3"

i=0
num_colon=0
is_a_dot=
have_a_dot=
at_least_one_dot=
while [ $i -lt ${#hostName} ]
do
    # Note that hostnames can only be Latin-1 chars (English), but domain names
    # can be internationized. Prohibit known illegal characters from being entered
    # as NTP serever names/addresses
    if [ "${hostName:$i:1}"  == "~" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "\`" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "!" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "@" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "#" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "$" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "%" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "^" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "&" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "*" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "(" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == ")" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "_" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "+" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "=" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "{" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "}" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "[" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "]" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "<" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == ">" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "?" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "," ]; then
        exit 3
    elif [ "${hostName:$i:1}" == ";" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "\'" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "'" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "\"" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "\/" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "/" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "\\" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == "|" ]; then
        exit 3
    elif [ "${hostName:$i:1}" == ":" ]; then
        num_colon=$((num_colon+1));

        if [ "$at_least_one_dot" != "" ]; then
            # can't mix colons and dots
            exit 3
        fi
    elif [ "${hostName:$i:1}" == "." ]; then
        at_least_one_dot=yes
        is_a_dot=got_a_dot

        if [ "$have_a_dot" != "" ]; then
            # do not allow two '.' chars in a row
            exit 3
        fi

        if [[ $i -eq 0 || $i -eq $((${#hostName}-1)) ]]; then
            # do not start or end with '.' char
            exit 3
        fi
    fi

    # set up for two '.' chars in a row check
    if [ "$is_a_dot" == "got_a_dot" ]; then
        have_a_dot=have_dot
    else
        have_a_dot=
    fi

    # reset for next char
    is_a_dot=
    i=$((i+1));
done

if [ $num_colon -gt 7 ]; then
    # too many ':' chars for IPv6
    exit 3
fi

# If IPv6, then do not allow non-hex chars
if [[ $num_colon -gt 0 ]]; then
    if ! echo $hostName | grep -q '^[a-fA-F0-9:]\+$'; then
        exit 3
    fi
fi

# expand single '.' chars into '\.' chars for the subsequent grep
x=`echo $hostName | sed -e s/'\.'/"\\\\\."/g`

if [ "$1" == "add" ]; then
  grep -qi "^server $x$" $CONF_FILE 2>&1 >/dev/null
  if [ $? -ne 0 ]; then
     # ntp server entry without the 'version' param does not exist, so
     # check if it actually does exist with the 'version' param
     grep -qi "^server $x version [0-9]" $CONF_FILE 2>&1 >/dev/null
     if [ $? -ne 0 ]; then
        # no match - add the new entry
        cp $CONF_FILE /etc/ntp.conf.previous
        if [ "$ntpversion" == "" ]; then
           echo "server $hostName" >>$CONF_FILE
        else
           echo "server $hostName version $ntpversion" >>$CONF_FILE
        fi
     else
        # ntp server already exists in conf file and has a version
        # associated with it
        exit 5
     fi
  else
     # exact NTP server name already exists in config file
     exit 5
  fi
fi

if [ "$1" == "remove" ]; then
  unique=$RANDOM
  grep -qi "^server $x$" $CONF_FILE 2>&1 >/dev/null
  if [ $? -eq 0 ]; then
     # exact server names exists in the conf file - no versioning
     sed -e /"server $hostName\$"/d $CONF_FILE >/tmp/ntpconf.$unique
     cp $CONF_FILE /etc/ntp.conf.previous
     mv /tmp/ntpconf.$unique $CONF_FILE
  else
     grep -qi "^server $x version [0-9]" $CONF_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]; then
        # ntp server exists in the conf file with version param
        sed -e /"server $hostName version"/d $CONF_FILE >/tmp/ntpconf.$unique
        cp $CONF_FILE /etc/ntp.conf.previous
        mv /tmp/ntpconf.$unique $CONF_FILE
     else
        # server does not exist to remove
        exit 1
     fi
  fi
fi

# Only restart xntp if it is already running
/etc/rc.d/hmcxntpd status 2>&1 >/dev/null
if [ $? -eq 0 ]
then
   /etc/rc.d/hmcxntpd restart 2>&1 >/dev/null
fi

exit 0  
