| 1 |
#!/bin/bash
|
| 2 |
# Copyright (c) 2004-2005 Gentoo Foundation
|
| 3 |
# Distributed under the terms of the GNU General Public License v2
|
| 4 |
|
| 5 |
# Contributed by Roy Marples (uberlord@gentoo.org)
|
| 6 |
|
| 7 |
# Fix any potential localisation problems
|
| 8 |
# Note that LC_ALL trumps LC_anything_else according to locale(7)
|
| 9 |
dhclient() {
|
| 10 |
LC_ALL=C /sbin/dhclient "$@"
|
| 11 |
}
|
| 12 |
|
| 13 |
# char* dhclient_provides(void)
|
| 14 |
#
|
| 15 |
# Returns a string to change module definition for starting up
|
| 16 |
dhclient_provides() {
|
| 17 |
echo "dhcp"
|
| 18 |
}
|
| 19 |
|
| 20 |
# void dhclient_depend(void)
|
| 21 |
#
|
| 22 |
# Sets up the dependancies for the module
|
| 23 |
dhclient_depend() {
|
| 24 |
after interface
|
| 25 |
}
|
| 26 |
|
| 27 |
# bool dhclient_check_installed(void)
|
| 28 |
#
|
| 29 |
# Returns 1 if dhclient is installed, otherwise 0
|
| 30 |
dhclient_check_installed() {
|
| 31 |
[[ -x /sbin/dhclient ]] && return 0
|
| 32 |
${1:-false} && eerror "For DHCP (dhclient) support, emerge net-misc/dhcp"
|
| 33 |
return 1
|
| 34 |
}
|
| 35 |
|
| 36 |
# bool dhclient_check_depends(void)
|
| 37 |
#
|
| 38 |
# Checks to see if we have the needed functions
|
| 39 |
dhclient_check_depends() {
|
| 40 |
local f
|
| 41 |
|
| 42 |
for f in interface_exists interface_get_address; do
|
| 43 |
[[ $( type -t ${f} ) == "function" ]] && continue
|
| 44 |
eerror "dhclient: missing required function ${f}\n"
|
| 45 |
return 1
|
| 46 |
done
|
| 47 |
|
| 48 |
return 0
|
| 49 |
}
|
| 50 |
|
| 51 |
# char* dhclient_get_vars(char *interface)
|
| 52 |
#
|
| 53 |
# Returns a string spaced with possible user set
|
| 54 |
# configuration variables
|
| 55 |
dhclient_get_vars() {
|
| 56 |
echo "dhclient_$1 dhcp_$1"
|
| 57 |
}
|
| 58 |
|
| 59 |
# bool dhclient_stop(char *iface)
|
| 60 |
#
|
| 61 |
# Stop dhclient on an interface
|
| 62 |
# Always returns 0
|
| 63 |
dhclient_stop() {
|
| 64 |
local iface="$1" d
|
| 65 |
local pidfile="/var/run/dhclient-${iface}.pid"
|
| 66 |
|
| 67 |
dhclient_check_installed || return 0
|
| 68 |
[[ ! -f ${pidfile} ]] && return 0
|
| 69 |
|
| 70 |
# We check for a dhclient process first as if we attempt to release
|
| 71 |
# an interface for which dhclient has obtained an IP in the past
|
| 72 |
# it causes a "RELEASE" event anyway.
|
| 73 |
local pid=$( < "${pidfile}" )
|
| 74 |
|
| 75 |
local ifvar=$( bash_variable "${iface}" )
|
| 76 |
eval d=\" \$\{dhcp_${ifvar}\} \"
|
| 77 |
[[ ${d} == " " ]] && d=" ${dhcp} "
|
| 78 |
|
| 79 |
ebegin "Stopping dhclient on ${iface}"
|
| 80 |
if [[ ${d} == *" release "* ]]; then
|
| 81 |
local r=$( dhclient -q -r -pf "${pidfile}" \
|
| 82 |
-sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" "${iface}" )
|
| 83 |
[[ ${r} == "deconfig" ]]
|
| 84 |
eend $? "dhclient returned a ${r}"
|
| 85 |
[[ -f "/var/cache/dhcp-${iface}.lease" ]] \
|
| 86 |
&& rm -f "/var/cache/dhcp-${iface}.lease"
|
| 87 |
else
|
| 88 |
kill -s TERM "${pid}" 2>/dev/null
|
| 89 |
clean_pidfile "${pidfile}"
|
| 90 |
eend 0
|
| 91 |
fi
|
| 92 |
|
| 93 |
return 0
|
| 94 |
}
|
| 95 |
|
| 96 |
# bool dhclient_start(char *iface)
|
| 97 |
#
|
| 98 |
# Start DHCP on an interface by calling dhclient $iface $options
|
| 99 |
#
|
| 100 |
# Returns 0 (true) when a DHCP address is obtained, otherwise 1
|
| 101 |
dhclient_start() {
|
| 102 |
local iface="$1" opts ifvar=$( bash_variable "$1" )
|
| 103 |
local pidfile="/var/run/dhclient-${iface}.pid" edit=""
|
| 104 |
local cffile="/etc/dhcp/dhclient.conf"
|
| 105 |
local d
|
| 106 |
|
| 107 |
interface_exists "${iface}" true || return 1
|
| 108 |
|
| 109 |
eval edit=\"\$\{dhclient_edit_config_${ifvar}\}\"
|
| 110 |
[[ -z ${edit} ]] && eval edit="${dhclient_edit_config:-yes}"
|
| 111 |
if [[ ${edit} == "yes" || ${edit} == "true" ]]; then
|
| 112 |
edit=true
|
| 113 |
else
|
| 114 |
edit=false
|
| 115 |
fi
|
| 116 |
|
| 117 |
# Load our options
|
| 118 |
eval opts=\" \$\{dhclient_${ifvar}\} \"
|
| 119 |
|
| 120 |
# Work out our cffile
|
| 121 |
x="${opts##* -cf }"
|
| 122 |
if [[ ${x} != ${opts} ]]; then
|
| 123 |
x="${x%% *}"
|
| 124 |
if [[ -n ${x} ]]; then
|
| 125 |
cffile="${x}"
|
| 126 |
opts="${opts//-cf ${cffile}/}"
|
| 127 |
fi
|
| 128 |
fi
|
| 129 |
opts="${opts} -cf ${cffile}"
|
| 130 |
|
| 131 |
# Ensure that the cffile does not contain any script lines
|
| 132 |
# as that will stop our helpers from running
|
| 133 |
if [[ -e ${cffile} ]] ; then
|
| 134 |
if grep -q "^[ \t]*script " "${cffile}" 2>/dev/null ; then
|
| 135 |
if ${edit} ; then
|
| 136 |
sed -i '/^[ \t]*script /d' "${cffile}" || return 1
|
| 137 |
else
|
| 138 |
eerror "You have to remove the script parameter from ${cffile}"
|
| 139 |
return 1
|
| 140 |
fi
|
| 141 |
fi
|
| 142 |
else
|
| 143 |
${edit} && touch "${cffile}" 2>/dev/null
|
| 144 |
fi
|
| 145 |
|
| 146 |
eval d=\" \$\{dhcp_${ifvar}\} \"
|
| 147 |
[[ ${d} == " " ]] && d=" ${dhcp} "
|
| 148 |
|
| 149 |
# Send our hostname by editing cffile
|
| 150 |
if ${edit} && [[ -e ${cffile} && ${d} != *" nosendhost "* ]] ; then
|
| 151 |
local hostname=$( hostname )
|
| 152 |
if [[ ${hostname} != "(none)" && ${hostname} != "localhost" ]]; then
|
| 153 |
sed -i '/^[ \t]*send[ \t]*host-name[ \t]*/d' "${cffile}"
|
| 154 |
if [[ -s ${cffile} ]]; then
|
| 155 |
sed -i '1 isend host-name "'"${hostname}"'";' "${cffile}"
|
| 156 |
else
|
| 157 |
echo "send host-name \"${hostname}\";" > "${cffile}"
|
| 158 |
fi
|
| 159 |
fi
|
| 160 |
fi
|
| 161 |
|
| 162 |
# Bring up DHCP for this interface (or alias)
|
| 163 |
ebegin "Running dhclient"
|
| 164 |
|
| 165 |
# Stop dhclient if it's already running
|
| 166 |
dhclient_stop "${iface}"
|
| 167 |
|
| 168 |
if [[ ${background} == "yes" ]]; then
|
| 169 |
eval dhclient ${opts} -pf "${pidfile}" -q \
|
| 170 |
-sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" \
|
| 171 |
"${iface}" &>/dev/null &
|
| 172 |
eend 0
|
| 173 |
go_background
|
| 174 |
fi
|
| 175 |
|
| 176 |
local x=$( eval dhclient ${opts} -1 -pf "${pidfile}" \
|
| 177 |
-sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" -q "${iface}" 2>&1 )
|
| 178 |
# We just check the last 5 letters
|
| 179 |
[[ ${x:${#x} - 5:5} == "bound" ]]
|
| 180 |
if [[ $? != 0 ]]; then
|
| 181 |
echo "${x}"
|
| 182 |
# We need to kill the process if we fail
|
| 183 |
[[ -e ${pidfile} ]] && kill -s TERM $( < "${pidfile}" ) 2>/dev/null
|
| 184 |
eend 1
|
| 185 |
return 1
|
| 186 |
fi
|
| 187 |
eend 0
|
| 188 |
|
| 189 |
# DHCP succeeded, show address retrieved
|
| 190 |
local addr=$( interface_get_address "${iface}" )
|
| 191 |
einfo "${iface} received address ${addr}"
|
| 192 |
|
| 193 |
return 0
|
| 194 |
}
|