Subversion Repositories ALCASAR

Rev

Blame | Last modification | View Log

#/bin/bash
# alcasar-dhcp.sh

# by Raphaël Pion
# This script is distributed under the Gnu General Public License (GPL)

# active ou desactive l'option NTP du DHCP sur le réseau de consultation
# enable or disable the NTP option of DHCP service on consultation LAN


#configuration files
SED="/bin/sed -i"
CHILLI_CONF_FILE="/etc/chilli.conf"
ALCASAR_CONF_FILE="/usr/local/etc/alcasar.conf"

#NTP parameters
NTP_STATE=$(grep 'DHCP=' $ALCASAR_CONF_FILE | cut -d'=' -f2)
CHILLI_NTP_IP=$(grep 'CHILLI_NTP_IP=' $ALCASAR_CONF_FILE | cut -d'=' -f2)

usage="Usage: alcasar-chilli_ntp.sh {--on | -on} | {--off | -off} "
nb_args=$#
args=$1
if [ $nb_args -eq 0 ]
then
        echo "$usage"
        exit 1
fi
case $args in
        -\? | -h | --h)
                echo "$usage"
                exit 0
                ;;
        --off|-off) # disable NTP options over DHCP of coovachilli
                $SED "s?^CHILLI_NTP_ENABLED.*?CHILLI_NTP_ENABLED=off?g" $ALCASAR_CONF_FILE
                $SED "s?.*dhcpopt\t.*?#dhcpopt\t\t$CHILLI_NTP_IP?g" $CHILLI_CONF_FILE
                #/usr/bin/systemctl restart chilli
                ;;
        --on|-on) # enable NTP options over DHCP of coovachilli
                if [ $NTP_STATE == "on" ]
                then
                        #DHCP Options : rfc2132
                        #dhcp option value will be convert in hexa.
                        #NTP option (or 'option 42') is like :
                        #                       
                        #    Code   Len         Address 1               Address 2
                        #   +-----+-----+-----+-----+-----+-----+-----+-----+--
                        #   |  42 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
                        #   +-----+-----+-----+-----+-----+-----+-----+-----+--
                        #
                        #Code : 42 => 2a
                        #Len : 4 => 04
                        #Convert CHILLI_NTP_IP into hexadecimal value
                        if [ $CHILLI_NTP_IP != "none" ]
                        then
                                a1=$( printf "%02x\n" $(cat /usr/local/etc/alcasar.conf | grep CHILLI_NTP_IP | cut -d'=' -f2 | cut -d'.' -f1))
                                a2=$( printf "%02x\n" $(cat /usr/local/etc/alcasar.conf | grep CHILLI_NTP_IP | cut -d'=' -f2 | cut -d'.' -f2))
                                a3=$( printf "%02x\n" $(cat /usr/local/etc/alcasar.conf | grep CHILLI_NTP_IP | cut -d'=' -f2 | cut -d'.' -f3))
                                a4=$( printf "%02x\n" $(cat /usr/local/etc/alcasar.conf | grep CHILLI_NTP_IP | cut -d'=' -f2 | cut -d'.' -f4))
                                HEXA_CHILLI_NTP_IP="2a04$a1$a2$a3$a4"
                                
                                $SED "s?.*dhcpopt.*?dhcpopt\t\t$HEXA_CHILLI_NTP_IP?g" $CHILLI_CONF_FILE
                                $SED "s?^CHILLI_NTP_ENABLED.*?CHILLI_NTP_ENABLED=on?g" $ALCASAR_CONF_FILE
                                /usr/bin/systemctl restart chilli
                        else
                                echo "Vous devez spécifier une addresse IP désignant votre serveur NTP"
                        fi
                else
                        echo "L'option NTP du DHCP de coovachilli ne peut pas s'activer. En effet le DHCP est 'off' sur ALCASAR"
                fi
                ;;
        *)
                echo "Argument inconnu :$1";
                echo "$usage"
                exit 1
                ;;
esac