summaryrefslogtreecommitdiff
blob: 72290b12be4fa800a6fe2d9846df2de56ae76c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# Copyright (c) 2004-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# Contributed by Roy Marples (uberlord@gentoo.org)

# Fix any potential localisation problems
# Note that LC_ALL trumps LC_anything_else according to locale(7)
dhclient() {
	LC_ALL=C /sbin/dhclient "$@"
}

# void dhclient_depend(void)
#
# Sets up the dependancies for the module
dhclient_depend() {
	after interface
	provide dhcp
	functions interface_exists interface_get_address
	variables dhclient dhcp
}

# bool dhclient_check_installed(void)
#
# Returns 1 if dhclient is installed, otherwise 0
dhclient_check_installed() {
	[[ -x /sbin/dhclient ]] && return 0
	${1:-false} && eerror "For DHCP (dhclient) support, emerge net-misc/dhcp"
	return 1
}

# bool dhclient_stop(char *iface)
#
# Stop dhclient on an interface
# Always returns 0
dhclient_stop() {
	local iface="$1" d
	local pidfile="/var/run/dhclient-${iface}.pid"

	[[ ! -f ${pidfile} ]] && return 0

	# We check for a dhclient process first as if we attempt to release
	# an interface for which dhclient has obtained an IP in the past
	# it causes a "RELEASE" event anyway.
	local pid=$( < "${pidfile}" )

	local ifvar=$( bash_variable "${iface}" )
	eval d=\" \$\{dhcp_${ifvar}\} \"
	[[ ${d} == "  " ]] && d=" ${dhcp} "

	ebegin "Stopping dhclient on ${iface}"
	if [[ ${d} == *" release "* ]]; then
		local r=$( dhclient -q -r -pf "${pidfile}" \
		-sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" "${iface}" )
		[[ ${r} == "deconfig" ]]
		eend $? "dhclient returned a ${r}"
		[[ -f "/var/cache/dhcp-${iface}.lease" ]] \
		&& rm -f "/var/cache/dhcp-${iface}.lease"
	else
		kill -s TERM "${pid}" 2>/dev/null
		clean_pidfile "${pidfile}"
		eend 0
	fi

	return 0
}

# bool dhclient_start(char *iface)
#
# Start DHCP on an interface by calling dhclient $iface $options
#
# Returns 0 (true) when a DHCP address is obtained, otherwise 1
dhclient_start() {
	local iface="$1" opts ifvar=$( bash_variable "$1" )
	local pidfile="/var/run/dhclient-${iface}.pid" edit=""
	local cffile="/etc/dhcp/dhclient.conf"
	local d

	interface_exists "${iface}" true || return 1

	eval edit=\"\$\{dhclient_edit_config_${ifvar}\}\"
	[[ -z ${edit} ]] && eval edit="${dhclient_edit_config:-no}"
	if [[ ${edit} == "yes" || ${edit} == "true" ]]; then
		edit=true
	else
		edit=false
	fi

	# Load our options
	eval opts=\" \$\{dhclient_${ifvar}\} \"

	# Work out our cffile
	x="${opts##* -cf }"
	if [[ ${x} != "${opts}" ]]; then
		x="${x%% *}"
		if [[ -n ${x} ]]; then
			cffile="${x}"
			opts="${opts//-cf ${cffile}/}"
		fi
	fi
	opts="${opts} -cf ${cffile}"

	# Ensure that the cffile does not contain any script lines
	# as that will stop our helpers from running
	if [[ -e ${cffile} ]] ; then
		if grep -q "^[ \t]*script " "${cffile}" 2>/dev/null ; then
			if ${edit} ; then
				sed -i '/^[ \t]*script /d' "${cffile}" || return 1
			else
				eerror "You have to remove the script parameter from ${cffile}"
				return 1
			fi
		fi
	else
		${edit} && touch "${cffile}" 2>/dev/null
	fi

	eval d=\" \$\{dhcp_${ifvar}\} \"
	[[ ${d} == "  " ]] && d=" ${dhcp} "

	# Send our hostname by editing cffile
	if ${edit} && [[ -e ${cffile} && ${d} != *" nosendhost "* ]] ; then
		local hname=$( hostname )
		if [[ ${hname} != "(none)" && ${hname} != "localhost" ]]; then
			sed -i '/^[ \t]*send[ \t]*host-name[ \t]*/d' "${cffile}"
			if [[ -s ${cffile} ]]; then
				sed -i '1 isend host-name "'"${hname}"'";' "${cffile}"
			else
				echo "send host-name \"${hname}\";" > "${cffile}"
			fi
		fi
	fi

	# Bring up DHCP for this interface (or alias)
	ebegin "Running dhclient"

	# Stop dhclient if it's already running
	dhclient_stop "${iface}"

	if [[ ${background} == "yes" ]]; then
		eval dhclient ${opts} -pf "${pidfile}" -q \
		-sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" \
		"${iface}" &>/dev/null &
		eend 0
		go_background
	fi

	local x=$( eval dhclient ${opts} -1 -pf "${pidfile}" \
	-sf "${MODULES_DIR}/helpers.d/dhclient-wrapper" -q "${iface}" 2>&1 )
	# We just check the last 5 letters
	[[ ${x:${#x} - 5:5} == "bound" ]]
	if [[ $? != "0" ]]; then
		echo "${x}"
		# We need to kill the process if we fail
		[[ -e ${pidfile} ]] && kill -s TERM $( < "${pidfile}" ) 2>/dev/null
		eend 1
		return 1
	fi
	eend 0

	# DHCP succeeded, show address retrieved
	local addr=$( interface_get_address "${iface}" )
	einfo "${iface} received address ${addr}"

	return 0
}

# vim:ts=4