#!/bin/sh
# this is a real quick and dirty script, and does nothing fancy
# this program does no ip address verification, because of this we
# keep a previous copy of the bootptab and dhcpd.conf files
#
# Directions?  We don't need no stinkin' directions..
#
# this program is designed to be suid root, and  be run by a user in the
# group dhcp when it is finished it will chown the configuration files to be
# owned by root in the group dhcp, and set permissions to be 664 
# set the ownership of this program to be "chown root.dhcp" with permissions
# "chown 4550"
#
# This program assumes you have at least one "common" configuration
# set up.  (i.e. tc=common)  I use the following and is automatically
# part of the dhcp response:
# 
# common:sm=255.255.255.0:dn="foo.com":ds=1.2.3.4 5.6.7.8:dl=28800
#
# obviously adjust you subnet mask,replace foo.com with your domain name
# and ds with your name server(s).  you can also add things like wins
# servers (ws) etc.  Basically this is the line that is common to a 
# network range or ranges and you can make different entries for special
# cases.  i.e. I have a lot of dialup ISDN routers, they get addresses
# also via dhcp but with a different mask, thus I have an entry called
# "isdn"
#
# The questions asked are pretty straight forward, the netmask is asked
# because it is needed for the dhcpd.conf file, it is not written to the
# bootptab file, the mask is read from the "common" line
#
# when adding the gateway for your network, enter the primary gateway if
# you have multiple gateways.  When the entries are created, you will be
# asked to enter any additional ip gateways for the network.  It is very
# important if you are using something like Cisco's HSRP (hot standby
# routing protocol) to list the *REAL* addresses of the routers here
# otherwise you will not get addresses reliably.
#
# February 17, 1997
# Attempt to be a little more intelligent about increasing ranges - JR
#
#
# where are all my config files located?
ROOT="/etc"

current_range()
{
    FRONT=`echo ${DHCP_NET} | cut -d"." -f-3 | tr '.' '-'`
    BEGIN=`eval "egrep '^dynamic${FRONT}' ${ROOT}/bootptab | cut -d':' -f-1 | \
    cut -d'-' -f4- | sort -bn | head -1"`
    TAIL=`eval "egrep '^dynamic${FRONT}' ${ROOT}/bootptab | cut -d':' -f-1 | \
    cut -d'-' -f4- | sort -bn | tail -1"`
    echo "range already exists, it starts at "${BEGIN}" and goes to "${TAIL}
}
bkup()
{
    cp /home/jross/scripts/bootptab /home/jross/scripts/bootptab.old
    cp /home/jross/scripts/dhcpd.conf /home/jross/scripts/dhcpd.conf.old
}
breakup()
{
    IP1=`echo ${DHCP_NET} | cut -d"." -f1`
    IP2=`echo ${DHCP_NET} | cut -d"." -f2`
    IP3=`echo ${DHCP_NET} | cut -d"." -f3`
    NET=`echo ${DHCP_NET} | cut -d"." -f4`
    START=`echo ${DHCP_LOW} | cut -d"." -f4`
    END=`echo ${DHCP_HIGH} | cut -d"." -f4`
}
creat_tab()
{
    while [ "${START}" -le "${END}" ]
    do
	echo "dynamic"${IP1}"-"${IP2}"-"${IP3}"-"${START}":ip="${IP1}"."${IP2}"."${IP3}"."${START}":dy:gw="${DHCP_GATEWAY}":tc="${DHCP_COMMON}":"
	START=`expr ${START} + 1`
    done >> ${ROOT}/bootptab
}

dhcp_gate_conf()
{
    echo "gateway "${DHCP_GATEWAY}" "${DHCP_MASK}" subnet"${IP1}"-"${IP2}"-"${IP3}"-"${NET} >> ${ROOT}/dhcpd.conf
}

creat_dhcp_conf()
{
    echo "network subnet"${IP1}"-"${IP2}"-"${IP3}"-"${NET}" "${IP1}"."${IP2}"."${IP3}"."${NET}" "${DHCP_MASK}" dhcp" >> ${ROOT}/dhcpd.conf
    dhcp_gate_conf
}

check_net()
{    
    grep -s ${DHCP_NET} ${ROOT}/dhcpd.conf
    NEW="$?"
    if [ "${NEW}" -eq "0" ]; then
    	    current_range
	    echo "Since the range already exists, the ${ROOT}/dhcpd.conf file will not be"
	    echo "updated.  When adding additional addresses do not overlap existing addresses"
	    UPDATE="1"
    else
	    UPDATE="0"
    fi
}

#clear
echo -n "enter the Network number -> "
read DHCP_NET
check_net
#
# check to see if this network already exists so we don't update the
# dhcpd.conf file
#
echo -n "enter the start IP address for this network -> "
read DHCP_LOW
echo -n "enter the ending IP address for this network -> "
read DHCP_HIGH
echo "if there are multiple gateways for this network, enter the primary gateway"
echo -n "just enter the gateway IP address for this network -> "
read DHCP_GATEWAY
echo -n "enter the netmask for the network -> "
read DHCP_MASK
echo -n "enter the common configuration name -> "
read DHCP_COMMON
bkup
breakup
creat_tab

if [ "${UPDATE}" -eq "0" ]; then
    creat_dhcp_conf
    echo -n "are there multiple gateways for this network (y/n)? "
    read MULTI
    while [ "${MULTI}" = "y" -o "${MULTI}" = "Y" ]
    	do
	    echo -n "Enter next Gateway address -> "
	    read DHCP_GATEWAY
	    dhcp_gate_conf
	    echo -n "any additional gateways (y/n)? "
	    read MULTI
    	done
fi
/usr/etc/chown root.dhcp ${ROOT}/bootptab ${ROOT}/dhcpd.conf
/bin/chmod 664 ${ROOT}/bootptab ${ROOT}/dhcpd.conf

exit 0
