aboutsummaryrefslogtreecommitdiff
blob: 4f5587093355b02e63b91f369bf8e13a8f46ef86 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/sbin/openrc-run
# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: 6842888f82472d095704e78b3fefdbd2678df7ba $

extra_commands="configtest"

: ${DHCPD_CONF:=/etc/dhcp/${SVCNAME}.conf}

depend() {
	need net
	use logger dns #@slapd@
}

get_var() {
	local var
	var="$( sed -n 's/^[[:blank:]]\?'"$1"' "*\([^#";]\+\).*/\1/p' "${chroot}${DHCPD_CONF}" )"
	printf '%s' "${var:-${2}}"
}

setup_opts() {
	DHCPD_CHROOT=${DHCPD_CHROOT%/}

	# Work out our cffile if it's in our DHCPD_OPTS
	case " ${DHCPD_OPTS} " in
		*" -cf "*)
			DHCPD_CONF=" ${DHCPD_OPTS} "
			DHCPD_CONF="${DHCPD_CONF##* -cf }"
			DHCPD_CONF="${DHCPD_CONF%% *}"
			;;
		*)	DHCPD_OPTS="${DHCPD_OPTS} -cf ${DHCPD_CONF}"
			;;
	esac
}

checkconfig() {
	local rc
	declare -i rc

	set -- ${DHCPD_OPTS} ${DHCPD_CHROOT:+-chroot "${DHCPD_CHROOT:-/}"} -t

	dhcpd "${@}" >/dev/null 2>&1
	rc=${?}

	if [ ${rc} -ne 0 ] ; then
		eerror "${SVCNAME} has detected a syntax error in your configuration files:"
		dhcpd "${@}"
	fi

	return ${rc}
}

configtest() {
	setup_opts

	ebegin "Checking ${SVCNAME} configuration"
	checkconfig
	eend ${?}
}

start() {
	local chroot="${DHCPD_CHROOT:-}" git=""

	setup_opts

	if [ -n "${DHCPD_USE_GIT:-}" ] && [ "${DHCPD_USE_GIT}" != "0" ]; then
		type -pf git >/dev/null 2>&1 && git="git"
	fi

	# see comment in get_var() above
	if [ ! -f "${chroot}${DHCPD_CONF}" ] ; then
		eerror "${chroot}${DHCPD_CONF} does not exist"
		return 1
	fi

	if [ -n "${chroot}" ] ; then
		# dhcpd config test wants these to exist
		mkdir -p \
			"${chroot}"/var/run/dhcp \
			"${chroot}"/var/lib/dhcp \
			"${chroot}"/etc/dhcp
	fi

	checkconfig || return 1

	checkpath -d -o dhcp:dhcp "${chroot}"/var/run/dhcp "${chroot}"/var/lib/dhcp

	local leasefile
	leasefile="$( get_var 'lease-file-name' "/var/lib/dhcp/${SVCNAME}.leases" )"
	checkpath -f -o dhcp:dhcp "${chroot}${leasefile}"

	if [ -n "${chroot:-}" ] && [ -d "${chroot}" ] ; then
		local lib name

		ebegin "Populating chroot directory"

		mkdir -p "${chroot}"/lib "${chroot}"/usr/sbin "${chroot}"/usr/lib
		checkpath -d -o root:root -m 0755 "${chroot}"/usr "${chroot}"/usr/sbin

		${git:-} rm "${chroot}"/lib/*.so* "${chroot}"/usr/lib/*.so* 2>/dev/null
		cp -a /usr/sbin/dhcpd "${chroot}"/usr/sbin/
		for lib in $( ldd /usr/sbin/dhcpd | grep '=>' | sed 's/^.*=>//' | cut -d" " -f 2 | grep -v "^$" ) /lib/libresolv.so.2 /lib/libnss_dns.so.2; do
			cp "${lib}" "${chroot}"/"${lib}" || { eerror "Cannot copy \"$lib\" to \"${chroot}\"" ; return 1 ; }
		done
		find "${chroot}"/lib -type l -delete
		for lib in libresolv.so libnss_dns.so; do
			name="$( readlink -e /lib/"${lib}"* )"
			cp "${name}" "${chroot}"/lib/ || { eerror "Cannot copy \"${lib}\" to \"${chroot}\"" ; return 1 ; }
			ln -s "$( basename "${name}" )" "${chroot}"/lib/"${lib}" || { eerror "Cannot create \"${name}\" symlink" ; return 1 ; }
		done

		eend 0

		checkpath -d -o root:root -m 755 "${chroot}"/dev "${chroot}"/etc "${chroot}"/proc
		rm "${chroot}"/etc/{localtime,resolv.conf}
		cp -Lp /etc/{localtime,resolv.conf} "${chroot}"/etc/

		if [ -d "${chroot}"/.git ] && [ -n "${git:-}" ]; then
			if ! [ -s "${chroot}"/.gitignore ]; then
				cat >>"${chroot}"/.gitignore <<-EOF
/etc/dhcp/*.sample
/proc/**
**/.keep*
*.pid
*.leases
*.leases~
				EOF
			fi
			pushd "${chroot}" >/dev/null 2>&1 &&
			git status 2>/dev/null | grep -Eq '^(Untracked files:$|:\s+(deleted|modified|new file):\s+)' &&
			einfo "Committing chroot changes to git"
			git add --all . >/dev/null 2>&1 &&
			git commit -m "Record changes on ${SVCNAME} start" >/dev/null 2>&1
			popd >/dev/null 2>&1
		fi

		# Setup LD_PRELOAD so name resolution works in our chroot.
		export LD_PRELOAD="${LD_PRELOAD} libresolv.so libnss_dns.so"
		if ! mountinfo -q "${chroot}/proc" ; then
			mount --bind /proc "${chroot}/proc"
		fi
	fi

	local pidfile
	pidfile="$( get_var 'pid-file-name' "/var/run/dhcp/${SVCNAME}.pid" )"

	ebegin "Starting ${chroot:+chrooted }${SVCNAME}"
	start-stop-daemon --start --pidfile "${chroot}/${pidfile}" \
		--exec /usr/sbin/dhcpd -- ${DHCPD_OPTS} -q -pf "${pidfile}" \
		-lf "${leasefile}" -user dhcp -group dhcp \
		${chroot:+-chroot "${chroot:-/}"} ${DHCPD_IFACE:-}
	if eend ${?}; then
		save_options 'dhcpd_chroot' "${chroot}" &&
		save_options 'pidfile' "${pidfile}"
	else
		set -o xtrace
		/usr/sbin/dhcpd ${DHCPD_OPTS} -pf "${pidfile}" -lf "${leasefile}" -user dhcp -group dhcp ${chroot:+-chroot "${chroot:-/}"} ${DHCPD_IFACE:-}
		#set +o xtrace
	fi
}

stop() {
	local chroot rc
	declare -i rc

	chroot="$( get_options 'dhcpd_chroot' )"
	[ -z "${chroot:-}" ] && chroot="$( get_options 'chroot' )"

	ebegin "Stopping ${chroot:+chrooted }${SVCNAME}"

	start-stop-daemon --stop --exec /usr/sbin/dhcpd \
		--pidfile "${chroot}/$( get_options 'pidfile' )"
	rc=${?}

	if [ ${rc} -eq 0 ] && [ -n "${chroot}" ] ; then
		if mountinfo -q "${chroot}/proc" ; then
			umount "${chroot}/proc"
		fi
	fi

	eend ${rc}
}