1# Generate /etc/resolv.conf
2# Support resolvconf(8) if available
3# We can merge other dhcpcd resolv.conf files into one like resolvconf,
4# but resolvconf is preferred as other applications like VPN clients
5# can readily hook into it.
6# Also, resolvconf can configure local nameservers such as bind
7# or dnsmasq. This is important as the libc resolver isn't that powerful.
8
9resolv_conf_dir="${state_dir}/resolv.conf"
10
11build_resolv_conf()
12{
13	local cf="/etc/resolv.conf.${interface}"
14	local interfaces= header= search= srvs= servers= x=
15
16	# Build a list of interfaces
17	interfaces=$(list_interfaces "${resolv_conf_dir}")
18
19	# Build the resolv.conf
20	if [ -n "${interfaces}" ]; then
21		# Build the header
22		for x in ${interfaces}; do
23			header="${header}${header:+, }${x}"
24		done
25
26		# Build the search list
27		search=$(cd "${resolv_conf_dir}"; \
28			key_get_value "search " ${interfaces})
29		[ -n "${search}" ] && search="search $(uniqify ${search})\n"
30
31		# Build the nameserver list
32		srvs=$(cd "${resolv_conf_dir}"; \
33			key_get_value "nameserver " ${interfaces})
34		for x in $(uniqify ${srvs}); do
35			servers="${servers}nameserver ${x}\n"
36		done
37	fi
38	header="${signature_base}${header:+ ${from} }${header}"
39
40	# Assemble resolv.conf using our head and tail files
41	[ -f "${cf}" ] && rm -f "${cf}"
42	echo "${header}" > "${cf}"
43	if [ -f /etc/resolv.conf.head ]; then
44		cat /etc/resolv.conf.head >> "${cf}"
45	else
46		echo "# /etc/resolv.conf.head can replace this line" >> "${cf}"
47	fi
48	printf "${search}${servers}" >> "${cf}"
49	if [ -f /etc/resolv.conf.tail ]; then
50		cat /etc/resolv.conf.tail >> "${cf}"
51	else
52		echo "# /etc/resolv.conf.tail can replace this line" >> "${cf}"
53	fi
54	mv -f "${cf}" /etc/resolv.conf
55}
56
57add_resolv_conf()
58{
59	local x= conf="${signature}\n"
60
61	# If we don't have any configuration, remove it
62	if [ -z "${new_domain_name_servers}" -a \
63		-z "${new_domain_name}" -a \
64		-z "${new_domain_search}" ]; then
65		remove_resolv_conf
66		return $?
67	fi
68
69	if [ -n "${new_domain_search}" ]; then
70		conf="${conf}search ${new_domain_search}\n"
71	elif [ -n "${new_domain_name}" ]; then
72		conf="${conf}search ${new_domain_name}\n"
73	fi
74	for x in ${new_domain_name_servers}; do
75		conf="${conf}nameserver ${x}\n"
76	done
77	if type resolvconf >/dev/null 2>&1; then
78		printf "${conf}" | resolvconf -a "${interface}"
79		return $?
80	fi
81
82	if [ -e "${resolv_conf_dir}/${interface}" ]; then
83		rm -f "${resolv_conf_dir}/${interface}"
84	fi
85	if [ ! -d "${resolv_conf_dir}" ]; then
86		mkdir -p "${resolv_conf_dir}"
87	fi
88	printf "${conf}" > "${resolv_conf_dir}/${interface}"
89	build_resolv_conf
90}
91
92remove_resolv_conf()
93{
94	if type resolvconf >/dev/null 2>&1; then
95		resolvconf -d "${interface}" -f
96	else
97		if [ -e "${resolv_conf_dir}/${interface}" ]; then
98			rm -f "${resolv_conf_dir}/${interface}"
99		fi
100		build_resolv_conf
101	fi
102}
103
104case "${reason}" in
105BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)	add_resolv_conf;;
106PREINIT|EXPIRE|FAIL|IPV4LL|RELEASE|STOP)	remove_resolv_conf;;
107esac
108