- Rewrite of /sbin/dhclient-script (make the script a little more readable,

discontinue use of ifconfig in favor of ip, store backup copies of orig
    files in /var rather than in /etc)
This commit is contained in:
David Cantrell 2008-08-22 21:05:05 +00:00
parent 6961ce445f
commit ee7ca6e29b
2 changed files with 407 additions and 379 deletions

View File

@ -4,7 +4,7 @@
Summary: DHCP (Dynamic Host Configuration Protocol) server and relay agent Summary: DHCP (Dynamic Host Configuration Protocol) server and relay agent
Name: dhcp Name: dhcp
Version: 4.0.0 Version: 4.0.0
Release: 19%{?dist} Release: 20%{?dist}
# NEVER CHANGE THE EPOCH on this package. The previous maintainer (prior to # NEVER CHANGE THE EPOCH on this package. The previous maintainer (prior to
# dcantrell maintaining the package) made incorrect use of the epoch and # dcantrell maintaining the package) made incorrect use of the epoch and
# that's why it is at 12 now. It should have never been used, but it was. # that's why it is at 12 now. It should have never been used, but it was.
@ -440,6 +440,11 @@ fi
%{_libdir}/libdhcp4client.so %{_libdir}/libdhcp4client.so
%changelog %changelog
* Fri Aug 22 2008 David Cantrell <dcantrell@redhat.com> - 12:4.0.0-20
- Rewrite of /sbin/dhclient-script (make the script a little more readable,
discontinue use of ifconfig in favor of ip, store backup copies of orig
files in /var rather than in /etc)
* Wed Aug 06 2008 David Cantrell <dcantrell@redhat.com> - 12:4.0.0-19 * Wed Aug 06 2008 David Cantrell <dcantrell@redhat.com> - 12:4.0.0-19
- Remove 'c' from the domain-search format string in common/tables.c - Remove 'c' from the domain-search format string in common/tables.c
- Prevent \032 from appearing in resolv.conf search line (#450042) - Prevent \032 from appearing in resolv.conf search line (#450042)

637
linux
View File

@ -1,171 +1,145 @@
#!/bin/bash #!/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:
# dhclient-script for Linux. Dan Halbert, March, 1997. # dhclient-script for Linux. Dan Halbert, March, 1997.
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
# No guarantees about this. I'm a novice at the details of Linux
# networking.
#
# Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL # Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL
# ----------
#
# Notes: PATH=/bin:/usr/bin:/sbin
SAVEDIR=/var/lib/dhclient
# 0. This script is based on the netbsd script supplied with dhcp-970306. LOGFACILITY="local7"
LOGLEVEL="notice"
# 1. ifconfig down apparently deletes all relevant routes and flushes logmessage() {
# the arp cache, so this doesn't need to be done explicitly. msg="${1}"
logger -p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
}
# 2. The alias address handling here has not been tested AT ALL. save_previous() {
# I'm just going by the doc of modern Linux ip aliasing, which uses origfile="${1}"
# notations like eth0:0, eth0:1, for each alias. savefile="${SAVEDIR}/${origfile##*/}.predhclient.${interface}"
# 3. I have to calculate the network address, and calculate the broadcast if [ ! -d ${SAVEDIR} ]; then
# address if it is not supplied. This might be much more easily done mkdir -p ${SAVEDIR}
# by the dhclient C code, and passed on. fi
PATH=/bin:/usr/bin if [ -e ${origfile} ]; then
mv ${origfile} ${savefile}
function save_previous() {
if [ -e $1 ]; then
mv $1 $1.predhclient.$interface
else else
echo ''> $1.predhclient.$interface echo > ${savefile}
fi fi
if [ -x /sbin/restorecon ]; then restorecon ${savefile} >/dev/null 2>&1
/sbin/restorecon $1.predhclient.$interface >/dev/null 2>&1
fi
} }
make_resolv_conf() { make_resolv_conf() {
if [ "${PEERDNS}" == "no" ]; then [ "${PEERDNS}" = "no" ] && return
if [ "${reason}" = "RENEW" ] &&
[ "${new_domain_name}" = "${old_domain_name}" ] &&
[ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
return return
fi fi
if [ x$reason == xRENEW ] && if [ -n "${new_domain_name}" ] ||
[ "$new_domain_name" == "$old_domain_name" ] && [ -n "${new_domain_name_servers}" ] ||
[ "$new_domain_name_servers" == "$old_domain_name_servers" ]; then [ -n "${new_domain_search}" ]; then
return
fi
if [ -n "$new_domain_name" ] || [ -n "$new_domain_name_servers" ] || [ -n "$new_domain_search" ]; then
save_previous /etc/resolv.conf save_previous /etc/resolv.conf
rscf=`mktemp /tmp/XXXXXX`; rscf="$(mktemp /tmp/XXXXXX)"
echo '; generated by /sbin/dhclient-script' > $rscf echo "; generated by /sbin/dhclient-script" > ${rscf}
if [ -n "$SEARCH" ]; then if [ -n "${SEARCH}" ]; then
echo "search $SEARCH" >> $rscf echo "search ${SEARCH}" >> $rscf
else else
if [ -n "$new_domain_search" ]; then if [ -n "${new_domain_search}" ]; then
echo "search ${new_domain_search//\\032/ }" >> $rscf echo "search ${new_domain_search//\\032/ }" >> ${rscf}
elif [ -n "$new_domain_name" ]; then elif [ -n "${new_domain_name}" ]; then
echo "search ${new_domain_name//\\032/ }" >> $rscf echo "search ${new_domain_name//\\032/ }" >> ${rscf}
fi fi
fi fi
if [ -n "$RES_OPTIONS" ]; then if [ -n "${RES_OPTIONS}" ]; then
echo "options $RES_OPTIONS" >> $rscf echo "options ${RES_OPTIONS}" >> ${rscf}
fi fi
for nameserver in $new_domain_name_servers; do for nameserver in ${new_domain_name_servers} ; do
echo nameserver $nameserver >> $rscf echo "nameserver ${nameserver}" >> ${rscf}
done done
change_resolv_conf $rscf change_resolv_conf ${rscf}
rm -f $rscf rm -f ${rscf}
fi fi
} }
# Must be used on exit. Invokes the local dhcp client exit hooks, if any.
exit_with_hooks() { exit_with_hooks() {
exit_status=$1 exit_status="${1}"
if [ -f /etc/dhclient-exit-hooks ]; then if [ -x /etc/dhclient-exit-hooks ]; then
. /etc/dhclient-exit-hooks . /etc/dhclient-exit-hooks
fi fi
# probably should do something with exit status of the local script exit ${exit_status}
exit $exit_status
} }
# Invoke the local dhcp client enter hooks, if they exist. quad2num() {
if [ -f /etc/dhclient-enter-hooks ]; then
exit_status=0
. /etc/dhclient-enter-hooks
# allow the local script to abort processing of this state
# local script must set exit_status variable to nonzero.
if [ $exit_status -ne 0 ]; then
exit $exit_status
fi
fi
# Import Red Hat Linux configuration
cd /etc/sysconfig/network-scripts;
. /etc/sysconfig/network-scripts/network-functions
. /etc/rc.d/init.d/functions
[ -f ../network ] && . ../network
[ -f ../networking/network ] && . ../networking/network
CONFIG=$interface
need_config ${CONFIG}
if [ -f "${CONFIG}" ]; then
source_config
else
echo $"$0: configuration for $interface not found. Continuing with defaults." >&2
fi
source_config
release=$(uname -r)
relmajor=$(echo $release | cut -f1 -d'.')
relminor=$(echo $release | cut -f2 -d'.')
# simple IP arithmetic functions:
function quad2num() {
if [ $# -eq 4 ]; then if [ $# -eq 4 ]; then
let n="$1<<24|$2<<16|$3<<8|$4" let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
echo $n echo "${n}"
return 0 return 0
fi else
echo '0' echo "0"
return 1 return 1
fi
} }
function ip2num() { ip2num() {
IFS='.' quad2num $1 IFS="." quad2num ${1}
} }
function num2ip() { num2ip() {
let n="$1" let n="${1}"
let o1='(n>>24)&0xff' let o1="(n >> 24) & 0xff"
let o2='(n>>16)&0xff' let o2="(n >> 16) & 0xff"
let o3='(n>>8)&0xff' let o3="(n >> 8) & 0xff"
let o4='n & 0xff' let o4="n & 0xff"
echo $o1.$o2.$o3.$o4 echo "${o1}.${o2}.${o3}.${o4}"
} }
function mask() { mask() {
ip=$1 ip="${1}"
m=$2 m="${2}"
let ip=$(IFS='.' ip2num $ip) let ip="$(IFS="." ip2num ${ip})"
let m=$(IFS='.' ip2num $m) let m="$(IFS="." ip2num ${m})"
let n='ip&m' let n="ip & m"
num2ip $n num2ip ${n}
} }
function mask_bits() { class_bits() {
ip=$1
let ip=$(IFS='.' ip2num $ip)
let bits=0
for ((bit=1; '((ip&bit)==0) && (bits < 32)'; 'bit<<=1')) do
let bits+=1
done
let n_bits=32-bits
echo $n_bits
}
function class_bits() {
let ip=$(IFS='.' ip2num $1) let ip=$(IFS='.' ip2num $1)
let bits=32 let bits=32
let mask='255' let mask='255'
@ -180,71 +154,78 @@ function class_bits() {
echo $bits echo $bits
} }
function routerReachable() { is_router_reachable() {
# Handle silly DHCP servers that give us a router not on our subnet: # handle DHCP servers that give us a router not on our subnet
router=$1 router="${1}"
routerSubnet=$(mask $router $new_subnet_mask) routersubnet="$(mask ${router} ${new_subnet_mask})"
mySubnet=$(mask $new_ip_address $new_subnet_mask) mysubnet="$(mask ${new_ip_address} ${new_subnet_mask})"
unreachable=0 unreachable=0
if [ "$routerSubnet" != "$mySubnet" ]; then
if [ ! "${routersubnet}" = "${mysubnet}" ]; then
unreachable=1 unreachable=1
if /sbin/arping -f -q -I $interface -w2 $router; then if arping -f -q -I ${interface} -w2 ${router}; then
/sbin/ip route add ${router}/32 dev $interface ip route add ${router}/32 dev ${interface}
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
unreachable=0 unreachable=0
else else
/usr/bin/logger -p local7.notice -t "NET" "dhclient: failed to create host route for unreachable router $router not on subnet $mySubnet"; logmessage "failed to create host router for unreachable router ${router} not on subnet ${mysubnet}"
fi fi
else else
unreachable=1 unreachable=1
if [ -x /usr/bin/logger ]; then logmessage "DHCP router ${router} is unreachable on DHCP subnet ${mysubnet} router subnet ${routersubnet}"
/usr/bin/logger -p local7.notice -t "NET" "dhclient: DHCP router $router is unreachable on DHCP subnet $mySubnet router subnet $routerSubnet";
fi fi
fi fi
fi
return $unreachable return ${unreachable}
} }
function add_default_gateway() { add_default_gateway() {
router=$1 router="${1}"
metric='' metric=""
if [ $# -gt 1 ] && [ "$2" -gt 0 ]; then
metric="metric $2" if [ $# -gt 1 ] && [ "${2}" -gt 0 ]; then
metric="metric ${2}"
fi fi
if routerReachable $router ; then
/sbin/ip route replace default via $router dev $interface $metric if is_router_reachable ${router} ; then
ip route replace default via ${router} dev ${interface} ${metric}
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
/usr/bin/logger -p local7.notice -t "NET" 'dhclient: failed to create default route: '$router dev $interface $metric logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
return 1 return 1
else else
return 0 return 0
fi fi
fi fi
return 1 return 1
} }
function dhconfig() { dhconfig() {
if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && [ x$alias_ip_address != x$old_ip_address ]; then if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
# Possible new alias. Remove old alias. [ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
ifconfig $interface:0- inet 0 # possible new alias, remove old alias first
ip -family inet addr del ${old_ip_address} dev ${interface}:0
fi fi
if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then if [ -n "${old_ip_address}" ] &&
[ ! "${old_ip_address}" = "${new_ip_address}" ]; then
# IP address changed. Bringing down the interface will delete all # IP address changed. Bringing down the interface will delete all
# routes, and clear the ARP cache. # routes, and clear the ARP cache.
ifconfig $interface inet 0 down ip -family inet addr flush dev ${interface}
ip -family inet link set dev ${interface} down
fi fi
if [ x$reason = xBOUND ] || [ x$reason = xREBOOT ] || if [ "${REASON}" = "BOUND" ] || "${REASON}" = "REBOOT" ] ||
[ x$old_ip_address != x$new_ip_address ] || [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
[ x$old_subnet_mask != x$new_subnet_mask ] || [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
[ x$new_network_number != x$new_network_number ] || [ ! "${old_network_number}" = "${new_network_number}" ] ||
[ x$old_broadcast_address != x$new_broadcast_address ] || [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
[ "x$old_routers" != "x$new_routers" ] || [ ! "${old_routers}" = "${new_routers}" ] ||
[ x$old_interface_mtu != x$new_interface_mtu ] ; then [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
ifconfig $interface inet $new_ip_address $new_subnet_arg $new_broadcast_arg ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
if [ -n "$new_interface_mtu" ]; then
/sbin/ip link set $interface mtu $new_interface_mtu if [ -n "${new_interface_mtu}" ]; then
ip link set ${interface} mtu ${new_interface_mtu}
fi fi
if [ -x /etc/dhclient-${interface}-up-hooks ]; then if [ -x /etc/dhclient-${interface}-up-hooks ]; then
@ -253,50 +234,51 @@ function dhconfig() {
. /etc/dhclient-up-hooks . /etc/dhclient-up-hooks
fi fi
prefix_bits=$(mask_bits $new_subnet_mask) if [[ (( -z "${GATEWAYDEV}" ) ||
# Add a network route to the computed network address. ( "${GATEWAYDEV}" = "${interface}" )) &&
if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then (( -z "$GATEWAY" ) ||
/sbin/ip route replace ${new_network_number}/${prefix_bits} dev $interface (( -n "$DHCLIENT_IGNORE_GATEWAY" ) &&
if [ $added_old_broadcast_route -eq 1 ]; then ( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* ))) ]]; then
/sbin/ip route del default metric="${METRIC:-''}"
fi let i="${METRIC:-0}"
fi
if [[ (( -z "$GATEWAYDEV" ) || ( "$GATEWAYDEV" = "$interface" )) && (( -z "$GATEWAY" ) || (( -n "$DHCLIENT_IGNORE_GATEWAY" ) && ( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* ))) ]]; then
metric=${METRIC:-''}
let i=${METRIC:-0}
default_routers=() default_routers=()
for router in $new_routers; do for router in ${new_routers} ; do
added_router=0 added_router=-
for r in ${default_routers[@]}; do for r in ${default_routers[@]} ; do
if [ "$r" == "$router" ]; then if [ "${r}" = "${router}" ]; then
added_router=1 added_router=1
fi fi
done done
if [ -z "$router" ] || [ "$added_router" -eq 1 ] || [ $(IFS=. ip2num $router) -le 0 ] || [[ ( "$router" = "$new_broadcast_address" ) && ( "$new_subnet_mask" != "255.255.255.255" ) ]]; then if [ -z "${router}" ] ||
[ "${added_router}" = "1" ] ||
[ $(IFS=. ip2num ${router}) -le 0 ] ||
[[ ( "${router}" = "${new_broadcast_address}" ) &&
( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
continue continue
fi fi
default_routers=(${default_routers[@]} $router) default_routers=(${default_routers[@]} ${router})
add_default_gateway $router $metric add_default_gateway ${router} ${metric}
let i=i+1 let i=i+1
metric=$i metric=${i}
done done
elif [[ (( -z "$GATEWAYDEV" ) || ( "$GATEWAYDEV" = "$interface" )) && ( -n "$GATEWAY" ) ]]; then elif [[ (( -z "${GATEWAYDEV}" ) ||
routerSubnet=$(mask $GATEWAY $new_subnet_mask) ( "${GATEWAYDEV}" = "${interface}" )) &&
mySubnet=$(mask $new_ip_address $new_subnet_mask) ( -n "${GATEWAY}" ) ]]; then
routersubnet=$(mask ${GATEWAY} ${new_subnet_mask})
mysubnet=$(mask ${new_ip_address} ${new_subnet_mask})
if [ "$routerSubnet" = "$mySubnet" ]; then if [ "${routersubnet}" = "${mysubnet}" ]; then
/sbin/ip route replace default via $GATEWAY dev $interface ip route replace default via ${GATEWAY} dev ${interface}
fi fi
fi fi
# static routes # static routes
if [ "x$new_static_routes" != x ]; then if [ -n "${new_static_routes}" ]; then
IFS=', ' static_routes=($new_static_routes) IFS=', |' static_routes=(${new_static_routes})
route_targets=() route_targets=()
for((i=0; i<${#static_routes[@]}; i+=2)); do for((i=0; i<${#static_routes[@]}; i+=2)); do
@ -305,8 +287,8 @@ function dhconfig() {
metric='' metric=''
for t in ${route_targets[@]}; do for t in ${route_targets[@]}; do
if [ $t == $target ]; then if [ ${t} = ${target} ]; then
if [ -z "$metric" ]; then if [ -z "${metric}" ]; then
metric=1 metric=1
else else
((metric=metric+1)) ((metric=metric+1))
@ -314,77 +296,81 @@ function dhconfig() {
fi fi
done done
if [ -n "$metric" ]; then if [ -n "${metric}" ]; then
metric="metric $metric" metric="metric ${metric}"
fi fi
if routerReachable $gateway; then if is_router_reachable ${gateway}; then
/sbin/ip route replace ${target}/$(class_bits $target) via ${gateway} dev $interface ${metric} ip route replace ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
/usr/bin/logger -p local7.notice -t 'NET' 'dhclient: failed to create static route:' ${target}/`class_bits $target` via ${gateway} dev $interface ${metric} logmessage "failed to create static route: ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}"
else else
route_targets=(${route_targets[@]} $target) route_targets=(${route_targets[@]} ${target})
fi fi
fi fi
done done
fi fi
fi fi
if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ]; then if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
ifconfig $interface:0- inet 0 [ -n "${alias_ip_address}" ]; then
ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg ip -family inet addr flush dev ${interface}:0
/sbin/ip route replace ${alias_ip_address}/32 dev $interface:0 ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
ip route replace ${alias_ip_address}/32 dev ${interface}:0
fi fi
make_resolv_conf make_resolv_conf
if [ -n "$new_host_name" ] && need_hostname; then if [ -n "${new_host_name}" ] && need_hostname; then
hostname $new_host_name hostname ${new_host_name}
fi fi
if [ "${PEERNIS}" = no ]; then if [ ! "${PEERNIS}" = "no" ]; then
: if [ -n "${new_nis_domain}" ]; then
elif [ -n "$new_nis_domain" ]; then domainname "${new_nis_domain}"
domainname "$new_nis_domain"
save_previous /etc/yp.conf save_previous /etc/yp.conf
let contents=0 let contents=0
echo '# generated by /sbin/dhclient-script' > /etc/yp.conf echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
if [ -n "$new_nis_servers" ]; then if [ -n "${new_nis_servers}" ]; then
for I in $new_nis_servers; do for i in ${new_nis_servers} ; do
echo "domain $new_nis_domain server $I" >> /etc/yp.conf echo "domain ${new_nis_domain} server ${i}" >> /etc/yp.conf
let contents=contents+1 let contents=contents+1
done done
else else
echo "domain $new_nis_domain broadcast" >> /etc/yp.conf echo "domain ${new_nis_domain} broadcast" >> /etc/yp.conf
let contents=contents+1 let contents=contents+1
fi fi
if [ $contents -gt 0 ]; then if [ ${contents} -gt 0 ]; then
if [ -x /etc/rc.d/init.d/ypbind ] && [ -r /var/run/ypbind.pid ]; then if [ -x /etc/rc.d/init.d/ypbind ] &&
[ -r /var/run/ypbind.pid ]; then
service ypbind restart >/dev/null 2>&1 service ypbind restart >/dev/null 2>&1
fi fi
fi fi
elif [ -n "$new_nis_servers" ]; then elif [ -n "${new_nis_servers}" ]; then
save_previous /etc/yp.conf save_previous /etc/yp.conf
echo '# generated by /sbin/dhclient-script' > /etc/yp.conf echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
let contents=0 let contents=0
for I in $new_nis_servers; do for i in ${new_nis_servers} ; do
echo "ypserver $I" >> /etc/yp.conf echo "ypserver ${i}" >> /etc/yp.conf
let contents=contents+1 let contents=contents+1
done done
if [ $contents -gt 0 ]; then if [ $contents -gt 0 ]; then
if [ -x /etc/rc.d/init.d/ypbind ] && [ -r /var/run/ypbind.pid ]; then if [ -x /etc/rc.d/init.d/ypbind ] &&
service ypbind restart >/dev/null 2>&1 [ -r /var/run/ypbind.pid ]; then
server ypbind restart >/dev/null 2>&1
fi
fi fi
fi fi
fi fi
if [ -n "$DHCP_TIME_OFFSET_SETS_TIMEZONE" ] && [[ "$DHCP_TIME_OFFSET_SETS_TIMEZONE" = [yY1]* ]]; then if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
if [ -n "$new_time_offset" ]; then [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
if [ -n "${new_time_offset}" ]; then
# DHCP option "time-offset" is requested by default and should be # DHCP option "time-offset" is requested by default and should be
# handled. The geographical zone abbreviation cannot be determined # handled. The geographical zone abbreviation cannot be determined
# from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
@ -397,133 +383,160 @@ function dhconfig() {
((hoursWest*=-1)) ((hoursWest*=-1))
fi fi
tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' $hoursWest) tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
if [ -e $tzfile ]; then if [ -e ${tzfile} ]; then
/bin/mv -f /etc/localtime /etc/localtime.predhclient.$interface save_previous /etc/localtime
/bin/cp -fp $tzfile /etc/localtime cp -fp ${tzfile} /etc/localtime
/bin/touch /etc/localtime touch /etc/localtime
fi fi
fi fi
fi fi
if [ "${PEERNTP}" = no ]; then if [ ! "${PEERNTP}" = "no" ] &&
: [ -n "${new_ntp_servers}" ] && [ -e /etc/ntp.conf ]; then
elif [ -n "$new_ntp_servers" ] && [ -e /etc/ntp.conf ]; then
save_previous /etc/ntp.conf save_previous /etc/ntp.conf
/bin/egrep -v '^server .* # added by /sbin/dhclient-script$'< /etc/ntp.conf.predhclient.$interface > /etc/ntp.conf egrep -v '^server .* # added by /sbin/dhclient-script$'< ${SAVEDIR}/ntp.conf.predhclient.${interface} > /etc/ntp.conf
if [ -x /sbin/restorecon ]; then restorecon /etc/ntp.conf >/dev/null 2>&1
/sbin/restorecon /etc/ntp.conf >/dev/null 2>&1
fi
for s in $new_ntp_servers; do for s in ${new_ntp_servers} ; do
echo "server $s # added by /sbin/dhclient-script" >> /etc/ntp.conf echo "server ${s} # added by /sbin/dhclient-script" >> /etc/ntp.conf
done done
if [ -x /usr/bin/diff ] && /usr/bin/diff -q /etc/ntp.conf /etc/ntp.conf.predhclient.$interface >/dev/null 2>&1; then diff -q /etc/ntp.conf ${SAVEDIR}/ntp.conf.predhclient.${interface} >/dev/null 2>&1
: if [ $? -eq 1 ]; then
else service ntpd condrestart >/dev/null 2>&1
/sbin/service ntpd condrestart >/dev/null 2>&1
fi fi
fi fi
} }
if [ x$new_broadcast_address != x ] && [ x$new_subnet_mask != x ] && [ "$new_subnet_mask" != "255.255.255.255" ]; then
new_broadcast_arg="broadcast $new_broadcast_address" #
# ### MAIN
#
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
}
if [ ! -r /etc/sysconfig/network-scripts/network-functions ]; then
echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
exit 1
fi fi
if [ x$old_broadcast_address != x ]; then if [ ! -r /etc/rc.d/init.d/functions ]; then
old_broadcast_arg="broadcast $old_broadcast_address" echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
exit 1
fi fi
if [ x$new_subnet_mask != x ]; then . /etc/sysconfig/network-scripts/network-functions
new_subnet_arg="netmask $new_subnet_mask" . /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
fi fi
if [ x$old_subnet_mask != x ]; then if [ -f /etc/sysconfig/networking/network ]; then
old_subnet_arg="netmask $old_subnet_mask" . /etc/sysconfig/networking/network
fi fi
if [ x$alias_subnet_mask != x ]; then CONFIG="${interface}"
alias_subnet_arg="netmask $alias_subnet_mask" need_config ${CONFIG}
if [ -f "${CONFIG}" ]; then
source_config
else
echo "${0}: configuration for ${interface} not found. Continuing with defaults." >&2
fi fi
if [ x$reason = xMEDIUM ]; then source_config
# Linux doesn't do mediums (ok, ok, media).
if [ -n "${new_subnet_mask}" ]; then
new_prefix="$(ipcalc -p ${new_ip_address} ${new_subnet_mask} | cut -d '=' -f 2)"
fi
if [ -n "${old_subnet_mask}" ]; then
old_prefix="$(ipcalc -p ${old_ip_address} ${new_subnet_mask} | cut -d '=' -f 2)"
fi
if [ -n "${alias_subnet_mask}" ]; then
alias_prefix="$(ipcalc -p ${alias_ip_address} ${alias_subnet_mask} | cut -d '=' -f 2)"
fi
case "${reason}" in
MEDIUM)
# Linux doesn't handle mediums (media)
exit_with_hooks 0 exit_with_hooks 0
fi ;;
added_old_broadcast_route=0 PREINIT)
if [ x$reason = xPREINIT ]; then if [ -n "${alias_ip_address}" ]; then
if [ x$alias_ip_address != x ]; then # Bring down alias interface, its routes will disappear too.
# Bring down alias interface. Its routes will disappear too. ip -family inet link set ${interface}:0 down
ifconfig $interface:0- inet 0
fi fi
if [ x$keep_old_ip = xyes ]; then if [ "${keep_old_ip}" = "yes" ]; then
ifconfig $interface up ip -family inet link set ${interface} up
elif [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ) then
ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
# Add route to make broadcast work. Do not omit netmask.
/sbin/ip route replace default dev $interface && added_old_broadcast_route=1
else else
ifconfig $interface 0 up ip -family inet addr flush dev ${interface}
ip -family inet link set ${interface} up
fi fi
# We need to give the kernel some time to get the interface up. if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
# sleep 1 sleep ${DHCLIENT_DELAY}
# I don't think this is necessary with modern kernels - no problems found
# during testing - JVD, 2005-06-17
# but just in case:
if [ -n "$DHCLIENT_DELAY" ] && [ "$DHCLIENT_DELAY" -gt 0 ] ; then
sleep $DHCLIENT_DELAY
fi fi
exit_with_hooks 0 exit_with_hooks 0
fi ;;
if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then ARPCHECK|ARPSEND)
if [ -z "$new_ip_address" ] || [ -z "$interface" ] || /sbin/arping -q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then 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 exit_with_hooks 0
else else
exit_with_hooks 1 exit_with_hooks 1
fi fi
fi ;;
if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \ BOUND|RENEW|REBIND|REBOOT)
[ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
dhconfig dhconfig
exit_with_hooks 0 exit_with_hooks 0
fi ;;
if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \ EXPIRE|FAIL|RELEASE|STOP)
|| [ x$reason = xSTOP ]; then
# only restore config files if there are no other dhclient processes # only restore config files if there are no other dhclient processes
# running (#306381) # running (#306381)
any_other_clients="$(/bin/ps -eo pid,ppid,comm | /bin/grep dhclient | /bin/grep -v $PPID)" any_other_clients="$(ps -eo pid,ppid,comm | grep dhclient | grep -v ${PPID})"
if [ -n "$any_other_clients" ]; then if [ -n "${any_other_clients}" ]; then
if [ -f /etc/resolv.conf.predhclient.$interface ]; then if [ -f ${SAVEDIR}/resolv.conf.predhclient.${interface} ]; then
change_resolv_conf /etc/resolv.conf.predhclient.$interface change_resolv_conf ${SAVEDIR}/resolv.conf.predhclient.${interface}
rm -f /etc/resolv.conf.predhclient.$interface rm -f ${SAVEDIR}/resolv.conf.predhclient.${interface}
fi fi
if [ -n "$DHCP_TIME_OFFSET_SETS_TIMEZONE" ] && [[ "$DHCP_TIME_OFFSET_SETS_TIMEZONE" = [yY1]* ]]; then if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
if [ -e /etc/localtime.predhclient.$interface ]; then [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
/bin/rm -f /etc/localtime if [ -e ${SAVEDIR}/localtime.predhclient.${interface} ]; then
/bin/mv -f /etc/localtime.predhclient.$interface /etc/localtime rm -f /etc/localtime
/bin/touch /etc/localtime mv ${SAVEDIR}/localtime.predhclient.${interface} /etc/localtime
touch /etc/localtime
fi fi
fi fi
if [ -f /etc/ntp.conf.predhclient.$interface ]; then if [ -f ${SAVEDIR}/ntp.conf.predhclient.${interface} ]; then
/bin/rm -f /etc/ntp.conf rm -f /etc/ntp.conf
/bin/mv -f /etc/ntp.conf.predhclient.$interface /etc/ntp.conf mv -f ${SAVEDIR}/ntp.conf.predhclient.${interface} /etc/ntp.conf
service ntpd condrestart >/dev/null 2>&1 service ntpd condrestart >/dev/null 2>&1
fi fi
if [ -f /etc/yp.conf.predhclient.$interface ]; then if [ -f ${SAVEDIR}/yp.conf.predhclient.${interface} ]; then
/bin/rm -f /etc/yp.conf rm -f /etc/yp.conf
/bin/mv -f /etc/yp.conf.predhclient.$interface /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 if [ -x /etc/rc.d/init.d/ypbind ] && [ -r /var/run/ypbind.pid ]; then
service ypbind restart >/dev/null 2>&1 service ypbind restart >/dev/null 2>&1
fi fi
@ -536,41 +549,51 @@ if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
. /etc/dhclient-down-hooks . /etc/dhclient-down-hooks
fi fi
if [ x$alias_ip_address != x ]; then if [ -n "${alias_ip_address}" ]; then
# Turn off alias interface. # Turn off alias interface
ifconfig $interface:0- inet 0 ip -family inet link set ${interface}:0 down
fi fi
if [ x$old_ip_address != x ]; then if [ -n "${old_ip_address}" ]; then
# Shut down interface, which will delete routes and clear arp cache. # Shut down interface, which will delete routes and clear arp cache.
ifconfig $interface inet 0 down ip -family inet addr flush dev ${interface}
ip -family inet link set ${interface} down
fi fi
if [ x$alias_ip_address != x ]; then if [ -n "${alias_ip_address}" ]; then
ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
/sbin/ip route replace ${alias_ip_address}/32 $interface:0 ip -family inet route replace ${alias_ip_address}/32 ${interface}:0
fi fi
exit_with_hooks 0 exit_with_hooks 0
fi ;;
if [ x$reason = xTIMEOUT ] && [ "x$new_routers" != 'x' ]; then TIMEOUT)
if [ x$alias_ip_address != x ]; then if [ -n "${new_routers}" ]; then
ifconfig $interface:0- inet 0 if [ -n "${alias_ip_address}" ]; then
ip -family inet addr flush dev ${interface}:0
fi fi
ifconfig $interface inet $new_ip_address $new_subnet_arg $new_broadcast_arg ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
set $new_routers set ${new_routers}
if ping -q -c 1 -w 10 -I $interface $1; then if ping -q -c 1 -w 10 -I ${interface} ${1}; then
dhconfig dhconfig
exit_with_hooks 0 exit_with_hooks 0
fi fi
ifconfig $interface inet 0 down ip -family inet addr flush dev ${interface}
ip -family inet link set ${interface} down
exit_with_hooks 1 exit_with_hooks 1
elif [ x$reason = xTIMEOUT ]; then else
exit_with_hooks 1 exit_with_hooks 1
fi fi
;;
*)
logmessage "unhandled state: ${reason}"
exit_with_hooks 1
;;
esac
exit_with_hooks 0 exit_with_hooks 0