summaryrefslogtreecommitdiff
blob: 1483cb0e8006a7639becdea2875637e802e03abe (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
#!/bin/bash
# Copyright (c) 2004-2006 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" pidfile="/var/run/dhclient-$1.pid"

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

	ebegin "Stopping dhclient on ${iface}"

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

	if [[ ${d} == *" release "* ]] ; then
		local dhconf="interface \"${iface}\" {\n \
			script \"${MODULES_DIR}/helpers.d/dhclient-wrapper\";\n \
			}\n"
		local r="$(echo -e "${dhconf}" \
			| dhclient -q -r -pf "${pidfile}" "${iface}" )"
		[[ ${r} == "deconfig" ]]
		eend $? "dhclient returned ${r}"
	else
		start-stop-daemon --stop --exec /sbin/dhclient --pidfile "${pidfile}"
		eend $?
	fi
}

# 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" ifvar="$(bash_variable "$1")"
	local pidfile="/var/run/dhclient-${iface}.pid"
	local cffile="/etc/dhcp/dhclient.conf"

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

	# Load any dhclient.conf instructions specified by the user
	local opts="dhclient_conf_${ifvar}"
	local dhconf="${!opts}"
	
	# Load our options
	opts="dhclient_${ifvar}"
	opts="${!opts}"

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

	# Warn that we're going to override parts of their cffile
	if [[ -e ${cffile} ]] ; then
		opts="${opts} -cf ${cffile}"
		if grep -q "^[ \t]*script[ \t]" "${cffile}" ; then
			ewarn "The script specified in ${cffile} will not be used"
		fi
	fi

	local d="dhcp_${ifvar}"
	d=" ${!d} "
	[[ ${d} == "  " ]] && d=" ${dhcp} "

	local ah=""
	# Send our hostname by editing cffile
	if [[ ${d} != *" nosendhost "* ]] ; then
		local hname="$(hostname)"
		if [[ ${hname} != "(none)" && ${hname} != "localhost" ]]; then
			ah="send host-name \"${hname}\"\n;"
			# Warn that we're going to override parts of their cffile
			if [[ -e ${cffile} ]] ; then
				if grep -q "^[ \t]*send[ \t]*host-name[ \t]" "${cffile}" ; then
					ewarn "The host-name \"${hname}\" will be sent instead of"
					ewarn "the one specified in ${cffile}"
					vewarn "Set dhcp_${ifvar}=\"nosendhost\" in /etc/conf.d/net"
					vewarn "to stop this from happening"
				fi
			fi
		fi
	fi
	dhconf="${dhconf} interface \"${iface}\" {\n \
		script \"/${MODULES_DIR}/helpers.d/dhclient-wrapper\";\n \
		 ${ah}
	 	}" 

	# Bring up DHCP for this interface (or alias)
	ebegin "Running dhclient"
	x="$(echo -e "${dhconf}" | dhclient ${opts} -q -1 -pf "${pidfile}")"
	# We just check the last 5 letters
	[[ ${x:${#x} - 5:5} == "bound" ]]
	if [[ $? != "0" ]]; then
		# Kill the offending daemon as it likes to hang around
		start-stop-daemon --stop --exec /sbin/dhclient --pidfile "${pidfile}"
		eend 1 "${x}"
		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