dhcp/dhclient-script

598 lines
18 KiB
Plaintext
Raw Normal View History

2008-01-11 00:19:03 +00:00
#!/bin/bash
#
# dhclient-script: Network interface configuration script run by
# dhclient based on DHCP client communication
#
# Copyright (C) 2008 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): David Cantrell <dcantrell@redhat.com>
#
# ----------
# This script is a rewrite/reworking on dhclient-script originally
# included as part of dhcp-970306:
2008-01-11 00:19:03 +00:00
# dhclient-script for Linux. Dan Halbert, March, 1997.
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
# Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL
# ----------
#
2008-01-11 00:19:03 +00:00
PATH=/bin:/usr/bin:/sbin
SAVEDIR=/var/lib/dhclient
2008-01-11 00:19:03 +00:00
LOGFACILITY="local7"
LOGLEVEL="notice"
2008-01-11 00:19:03 +00:00
logmessage() {
msg="${1}"
logger -p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
}
2008-01-11 00:19:03 +00:00
save_previous() {
origfile="${1}"
savefile="${SAVEDIR}/${origfile##*/}.predhclient.${interface}"
2008-01-11 00:19:03 +00:00
if [ ! -d ${SAVEDIR} ]; then
mkdir -p ${SAVEDIR}
fi
2008-01-11 00:19:03 +00:00
if [ -e ${origfile} ]; then
mv ${origfile} ${savefile}
2008-01-11 00:19:03 +00:00
else
echo > ${savefile}
2008-01-11 00:19:03 +00:00
fi
restorecon ${savefile} >/dev/null 2>&1
2008-01-11 00:19:03 +00:00
}
make_resolv_conf() {
[ "${PEERDNS}" = "no" ] && return
2008-01-11 00:19:03 +00:00
if [ "${reason}" = "RENEW" ] &&
[ "${new_domain_name}" = "${old_domain_name}" ] &&
[ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
2008-01-11 00:19:03 +00:00
return
fi
if [ -n "${new_domain_name}" ] ||
[ -n "${new_domain_name_servers}" ] ||
[ -n "${new_domain_search}" ]; then
save_previous /etc/resolv.conf
rscf="$(mktemp /tmp/XXXXXX)"
echo "; generated by /sbin/dhclient-script" > ${rscf}
2008-01-11 00:19:03 +00:00
if [ -n "${SEARCH}" ]; then
echo "search ${SEARCH}" >> $rscf
else
if [ -n "${new_domain_search}" ]; then
echo "search ${new_domain_search//\\032/ }" >> ${rscf}
elif [ -n "${new_domain_name}" ]; then
echo "search ${new_domain_name//\\032/ }" >> ${rscf}
2008-01-11 00:19:03 +00:00
fi
fi
if [ -n "${RES_OPTIONS}" ]; then
echo "options ${RES_OPTIONS}" >> ${rscf}
fi
for nameserver in ${new_domain_name_servers} ; do
echo "nameserver ${nameserver}" >> ${rscf}
2008-01-11 00:19:03 +00:00
done
change_resolv_conf ${rscf}
rm -f ${rscf}
if [ -x /sbin/restorecon ]; then
/sbin/restorecon /etc/resolv.conf >/dev/null 2>&1
fi
2008-01-11 00:19:03 +00:00
fi
}
exit_with_hooks() {
exit_status="${1}"
2008-01-11 00:19:03 +00:00
if [ -x /etc/dhclient-exit-hooks ]; then
2008-01-11 00:19:03 +00:00
. /etc/dhclient-exit-hooks
fi
exit ${exit_status}
2008-01-11 00:19:03 +00:00
}
quad2num() {
2008-01-11 00:19:03 +00:00
if [ $# -eq 4 ]; then
let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
echo "${n}"
2008-01-11 00:19:03 +00:00
return 0
else
echo "0"
return 1
2008-01-11 00:19:03 +00:00
fi
}
ip2num() {
IFS="." quad2num ${1}
2008-01-11 00:19:03 +00:00
}
num2ip() {
let n="${1}"
let o1="(n >> 24) & 0xff"
let o2="(n >> 16) & 0xff"
let o3="(n >> 8) & 0xff"
let o4="n & 0xff"
echo "${o1}.${o2}.${o3}.${o4}"
2008-01-11 00:19:03 +00:00
}
mask() {
ip="${1}"
m="${2}"
let ip="$(IFS="." ip2num ${ip})"
let m="$(IFS="." ip2num ${m})"
let n="ip & m"
num2ip ${n}
2008-01-11 00:19:03 +00:00
}
class_bits() {
2008-01-11 00:19:03 +00:00
let ip=$(IFS='.' ip2num $1)
let bits=32
let mask='255'
for ((i=0; i <= 3; i++, 'mask<<=8')); do
let v='ip&mask'
if [ "$v" -eq 0 ] ; then
let bits-=8
else
break
fi
done
echo $bits
}
is_router_reachable() {
# handle DHCP servers that give us a router not on our subnet
router="${1}"
routersubnet="$(mask ${router} ${new_subnet_mask})"
mysubnet="$(mask ${new_ip_address} ${new_subnet_mask})"
2008-01-11 00:19:03 +00:00
unreachable=0
if [ ! "${routersubnet}" = "${mysubnet}" ]; then
2008-01-11 00:19:03 +00:00
unreachable=1
if arping -f -q -I ${interface} -w2 ${router}; then
ip route add ${router}/32 dev ${interface}
if [ $? -eq 0 ]; then
unreachable=0
else
logmessage "failed to create host router for unreachable router ${router} not on subnet ${mysubnet}"
fi
2008-01-11 00:19:03 +00:00
else
unreachable=1
logmessage "DHCP router ${router} is unreachable on DHCP subnet ${mysubnet} router subnet ${routersubnet}"
2008-01-11 00:19:03 +00:00
fi
fi
return ${unreachable}
2008-01-11 00:19:03 +00:00
}
add_default_gateway() {
router="${1}"
metric=""
if [ $# -gt 1 ] && [ ${2} -gt 0 ]; then
metric="metric ${2}"
2008-01-11 00:19:03 +00:00
fi
if is_router_reachable ${router} ; then
ip route replace default via ${router} dev ${interface} ${metric}
2008-01-11 00:19:03 +00:00
if [ $? -ne 0 ]; then
logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
2008-01-11 00:19:03 +00:00
return 1
else
return 0
fi
fi
2008-01-11 00:19:03 +00:00
return 1
}
dhconfig() {
if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
[ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
# possible new alias, remove old alias first
ip -family inet addr del ${old_ip_address} dev ${interface}:0
2008-01-11 00:19:03 +00:00
fi
if [ -n "${old_ip_address}" ] &&
[ ! "${old_ip_address}" = "${new_ip_address}" ]; then
# IP address changed. Bringing down the interface will delete all
2008-01-11 00:19:03 +00:00
# routes, and clear the ARP cache.
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
ip -family inet link set dev ${interface} down
2008-01-11 00:19:03 +00:00
fi
if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
[ ! "${old_ip_address}" = "${new_ip_address}" ] ||
[ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
[ ! "${old_network_number}" = "${new_network_number}" ] ||
[ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
[ ! "${old_routers}" = "${new_routers}" ] ||
[ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
if [ -n "${new_interface_mtu}" ]; then
ip link set ${interface} mtu ${new_interface_mtu}
2008-01-11 00:19:03 +00:00
fi
if [ -x /etc/dhclient-${interface}-up-hooks ]; then
. /etc/dhclient-${interface}-up-hooks
elif [ -x /etc/dhclient-up-hooks ]; then
. /etc/dhclient-up-hooks
fi
if [[ (( -z "${GATEWAYDEV}" ) ||
( "${GATEWAYDEV}" = "${interface}" )) &&
(( -z "$GATEWAY" ) ||
(( -n "$DHCLIENT_IGNORE_GATEWAY" ) &&
( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* ))) ]]; then
metric="${METRIC:-}"
let i="${METRIC:-0}"
2008-01-11 00:19:03 +00:00
default_routers=()
for router in ${new_routers} ; do
added_router=-
2008-01-11 00:19:03 +00:00
for r in ${default_routers[@]} ; do
if [ "${r}" = "${router}" ]; then
2008-01-11 00:19:03 +00:00
added_router=1
fi
done
if [ -z "${router}" ] ||
[ "${added_router}" = "1" ] ||
[ $(IFS=. ip2num ${router}) -le 0 ] ||
[[ ( "${router}" = "${new_broadcast_address}" ) &&
( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
2008-01-11 00:19:03 +00:00
continue
fi
default_routers=(${default_routers[@]} ${router})
add_default_gateway ${router} ${metric}
2008-01-11 00:19:03 +00:00
let i=i+1
metric=${i}
2008-01-11 00:19:03 +00:00
done
elif [[ (( -z "${GATEWAYDEV}" ) ||
( "${GATEWAYDEV}" = "${interface}" )) &&
( -n "${GATEWAY}" ) ]]; then
routersubnet=$(mask ${GATEWAY} ${new_subnet_mask})
mysubnet=$(mask ${new_ip_address} ${new_subnet_mask})
if [ "${routersubnet}" = "${mysubnet}" ]; then
ip route replace default via ${GATEWAY} dev ${interface}
2008-01-11 00:19:03 +00:00
fi
fi
# static routes
if [ -n "${new_static_routes}" ]; then
IFS=', |' static_routes=(${new_static_routes})
2008-01-11 00:19:03 +00:00
route_targets=()
for((i=0; i<${#static_routes[@]}; i+=2)); do
target=${static_routes[$i]}
gateway=${static_routes[$i+1]}
metric=''
for t in ${route_targets[@]}; do
if [ ${t} = ${target} ]; then
if [ -z "${metric}" ]; then
2008-01-11 00:19:03 +00:00
metric=1
else
((metric=metric+1))
fi
fi
done
if [ -n "${metric}" ]; then
metric="metric ${metric}"
2008-01-11 00:19:03 +00:00
fi
if is_router_reachable ${gateway}; then
ip route replace ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}
2008-01-11 00:19:03 +00:00
if [ $? -ne 0 ]; then
logmessage "failed to create static route: ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}"
2008-01-11 00:19:03 +00:00
else
route_targets=(${route_targets[@]} ${target})
2008-01-11 00:19:03 +00:00
fi
fi
done
fi
fi
if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
[ -n "${alias_ip_address}" ]; then
ip -family inet addr flush dev ${interface}:0 >/dev/null 2>&1
ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
ip route replace ${alias_ip_address}/32 dev ${interface}:0
2008-01-11 00:19:03 +00:00
fi
make_resolv_conf
if [ -n "${new_host_name}" ] && need_hostname; then
hostname ${new_host_name}
2008-01-11 00:19:03 +00:00
fi
if [ ! "${PEERNIS}" = "no" ]; then
if [ -n "${new_nis_domain}" ]; then
domainname "${new_nis_domain}"
save_previous /etc/yp.conf
let contents=0
echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
if [ -n "${new_nis_servers}" ]; then
for i in ${new_nis_servers} ; do
echo "domain ${new_nis_domain} server ${i}" >> /etc/yp.conf
let contents=contents+1
done
else
echo "domain ${new_nis_domain} broadcast" >> /etc/yp.conf
2008-01-11 00:19:03 +00:00
let contents=contents+1
fi
2008-01-11 00:19:03 +00:00
if [ ${contents} -gt 0 ]; then
if [ -x /etc/rc.d/init.d/ypbind ] &&
[ -r /var/run/ypbind.pid ]; then
service ypbind restart >/dev/null 2>&1
fi
fi
elif [ -n "${new_nis_servers}" ]; then
save_previous /etc/yp.conf
echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
let contents=0
for i in ${new_nis_servers} ; do
echo "ypserver ${i}" >> /etc/yp.conf
let contents=contents+1
done
2008-01-11 00:19:03 +00:00
if [ $contents -gt 0 ]; then
if [ -x /etc/rc.d/init.d/ypbind ] &&
[ -r /var/run/ypbind.pid ]; then
service ypbind restart >/dev/null 2>&1
fi
fi
2008-01-11 00:19:03 +00:00
fi
fi
if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
[[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
if [ -n "${new_time_offset}" ]; then
2008-01-11 00:19:03 +00:00
# DHCP option "time-offset" is requested by default and should be
# handled. The geographical zone abbreviation cannot be determined
# from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
# used - note: this disables DST.
((z=new_time_offset/3600))
((hoursWest=$(printf '%+d' $z)))
if (( $hoursWest < 0 )); then
# tzdata treats negative 'hours west' as positive 'gmtoff'!
((hoursWest*=-1))
fi
tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
if [ -e ${tzfile} ]; then
save_previous /etc/localtime
cp -fp ${tzfile} /etc/localtime
touch /etc/localtime
2008-01-11 00:19:03 +00:00
fi
fi
fi
if [ ! "${PEERNTP}" = "no" ] &&
[ -n "${new_ntp_servers}" ] && [ -e /etc/ntp.conf ]; then
2008-01-11 00:19:03 +00:00
save_previous /etc/ntp.conf
egrep -v '^server .* # added by /sbin/dhclient-script$'< ${SAVEDIR}/ntp.conf.predhclient.${interface} > /etc/ntp.conf
restorecon /etc/ntp.conf >/dev/null 2>&1
2008-01-11 00:19:03 +00:00
for s in ${new_ntp_servers} ; do
echo "server ${s} # added by /sbin/dhclient-script" >> /etc/ntp.conf
2008-01-11 00:19:03 +00:00
done
diff -q /etc/ntp.conf ${SAVEDIR}/ntp.conf.predhclient.${interface} >/dev/null 2>&1
if [ $? -eq 1 ]; then
service ntpd condrestart >/dev/null 2>&1
2008-01-11 00:19:03 +00:00
fi
fi
}
get_prefix() {
ip="${1}"
nm="${2}"
if [ -n "${ip}" -a -n "${nm}" ]; then
ipcalc -s -p ${ip} ${nm} | cut -d '=' -f 2
fi
}
2008-01-11 00:19:03 +00:00
#
# ### MAIN
#
2008-01-11 00:19:03 +00:00
if [ -x /etc/dhclient-enter-hooks ]; then
exit_status=0
# dhclient-enter-hooks can abort dhclient-script by setting
# the exit_status variable to a non-zero value
. /etc/dhclient-enter-hooks
if [ ${exit_status} -ne 0 ]; then
exit ${exit_status}
fi
fi
if [ ! -r /etc/sysconfig/network-scripts/network-functions ]; then
echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
exit 1
2008-01-11 00:19:03 +00:00
fi
if [ ! -r /etc/rc.d/init.d/functions ]; then
echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
exit 1
2008-01-11 00:19:03 +00:00
fi
. /etc/sysconfig/network-scripts/network-functions
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
2008-01-11 00:19:03 +00:00
fi
if [ -f /etc/sysconfig/networking/network ]; then
. /etc/sysconfig/networking/network
2008-01-11 00:19:03 +00:00
fi
CONFIG="/etc/sysconfig/network-scripts/ifcfg-${interface}"
need_config ${CONFIG}
source_config >/dev/null 2>&1
2008-01-11 00:19:03 +00:00
new_prefix="$(get_prefix ${new_ip_address} ${new_subnet_mask})"
old_prefix="$(get_prefix ${old_ip_address} ${new_subnet_mask})"
alias_prefix="$(get_prefix ${alias_ip_address} ${alias_subnet_mask})"
2008-01-11 00:19:03 +00:00
case "${reason}" in
MEDIUM)
# Linux doesn't handle mediums (media)
exit_with_hooks 0
;;
PREINIT)
if [ -n "${alias_ip_address}" ]; then
# Bring down alias interface, its routes will disappear too.
ip -family inet link set ${interface}:0 down
fi
2008-01-11 00:19:03 +00:00
if [ "${keep_old_ip}" = "yes" ]; then
ip -family inet link set ${interface} up
else
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
ip -family inet link set ${interface} up
2008-01-11 00:19:03 +00:00
fi
if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
sleep ${DHCLIENT_DELAY}
fi
2008-01-11 00:19:03 +00:00
exit_with_hooks 0
;;
2008-01-11 00:19:03 +00:00
ARPCHECK|ARPSEND)
if [ -z "${new_ip_address}" ] || [ -z "${interface}" ] ||
arping -q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then
exit_with_hooks 0
else
exit_with_hooks 1
2008-01-11 00:19:03 +00:00
fi
;;
2008-01-11 00:19:03 +00:00
BOUND|RENEW|REBIND|REBOOT)
dhconfig
exit_with_hooks 0
;;
EXPIRE|FAIL|RELEASE|STOP)
# only restore config files if there are no other dhclient processes
# running (#306381)
any_other_clients="$(ps -eo pid,ppid,comm | grep dhclient | grep -v ${PPID})"
if [ -n "${any_other_clients}" ]; then
if [ -f ${SAVEDIR}/resolv.conf.predhclient.${interface} ]; then
change_resolv_conf ${SAVEDIR}/resolv.conf.predhclient.${interface}
rm -f ${SAVEDIR}/resolv.conf.predhclient.${interface}
fi
2008-01-11 00:19:03 +00:00
if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
[[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
if [ -e ${SAVEDIR}/localtime.predhclient.${interface} ]; then
rm -f /etc/localtime
mv ${SAVEDIR}/localtime.predhclient.${interface} /etc/localtime
touch /etc/localtime
fi
fi
2008-01-11 00:19:03 +00:00
if [ -f ${SAVEDIR}/ntp.conf.predhclient.${interface} ]; then
rm -f /etc/ntp.conf
mv -f ${SAVEDIR}/ntp.conf.predhclient.${interface} /etc/ntp.conf
service ntpd condrestart >/dev/null 2>&1
fi
2008-01-11 00:19:03 +00:00
if [ -f ${SAVEDIR}/yp.conf.predhclient.${interface} ]; then
rm -f /etc/yp.conf
mv -f ${SAVEDIR}/yp.conf.predhclient.${interface} /etc/yp.conf
if [ -x /etc/rc.d/init.d/ypbind ] && [ -r /var/run/ypbind.pid ]; then
service ypbind restart >/dev/null 2>&1
fi
fi
fi
2008-01-11 00:19:03 +00:00
if [ -x /etc/dhclient-${interface}-down-hooks ]; then
. /etc/dhclient-${interface}-down-hooks
elif [ -x /etc/dhclient-down-hooks ]; then
. /etc/dhclient-down-hooks
fi
2008-01-11 00:19:03 +00:00
if [ -n "${alias_ip_address}" ]; then
# Turn off alias interface
ip -family inet link set ${interface}:0 down
fi
2008-01-11 00:19:03 +00:00
if [ -n "${old_ip_address}" ]; then
# Shut down interface, which will delete routes and clear arp cache.
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
ip -family inet link set ${interface} down
fi
if [ -n "${alias_ip_address}" ]; then
ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
ip -family inet route replace ${alias_ip_address}/32 ${interface}:0
fi
2008-01-11 00:19:03 +00:00
exit_with_hooks 0
;;
2008-01-11 00:19:03 +00:00
TIMEOUT)
if [ -n "${new_routers}" ]; then
if [ -n "${alias_ip_address}" ]; then
ip -family inet addr flush dev ${interface}:0 >/dev/null 2>&1
fi
ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
set ${new_routers}
if ping -q -c 1 -w 10 -I ${interface} ${1}; then
dhconfig
exit_with_hooks 0
fi
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
ip -family inet link set ${interface} down
exit_with_hooks 1
else
exit_with_hooks 1
fi
;;
*)
logmessage "unhandled state: ${reason}"
exit_with_hooks 1
;;
esac
2008-01-11 00:19:03 +00:00
exit_with_hooks 0