summaryrefslogtreecommitdiffstats
path: root/builder/modules.d/wlan-boot/scripts/dhcpcd-hooks/20-resolv.conf
blob: e4db368dd8efbb2da99492841e6a03c38c70d006 (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Generate /etc/resolv.conf
# Support resolvconf(8) if available
# We can merge other dhcpcd resolv.conf files into one like resolvconf,
# but resolvconf is preferred as other applications like VPN clients
# can readily hook into it.
# Also, resolvconf can configure local nameservers such as bind
# or dnsmasq. This is important as the libc resolver isn't that powerful.

resolv_conf_dir="$state_dir/resolv.conf"
NL="
"
: ${resolvconf:=resolvconf}

build_resolv_conf()
{
	local cf="$state_dir/resolv.conf.$ifname"
	local interfaces= header= search= srvs= servers= x=

	# Build a list of interfaces
	interfaces=$(list_interfaces "$resolv_conf_dir")

	# Build the resolv.conf
	if [ -n "$interfaces" ]; then
		# Build the header
		for x in ${interfaces}; do
			header="$header${header:+, }$x"
		done

		# Build the search list
		domain=$(cd "$resolv_conf_dir"; \
			key_get_value "domain " ${interfaces})
		search=$(cd "$resolv_conf_dir"; \
			key_get_value "search " ${interfaces})
		set -- ${domain}
		domain="$1"
		[ -n "$2" ] && search="$search $*"
		[ -n "$search" ] && search="$(uniqify $search)"
		[ "$domain" = "$search" ] && search=
		[ -n "$domain" ] && domain="domain $domain$NL"
		[ -n "$search" ] && search="search $search$NL"

		# Build the nameserver list
		srvs=$(cd "$resolv_conf_dir"; \
			key_get_value "nameserver " ${interfaces})
		for x in $(uniqify ${srvs}); do
			servers="${servers}nameserver $x$NL"
		done
	fi
	header="$signature_base${header:+ $from }$header"

	# Assemble resolv.conf using our head and tail files
	[ -f "$cf" ] && rm -f "$cf"
	[ -d "$resolv_conf_dir" ] || mkdir -p "$resolv_conf_dir"
	echo "$header" > "$cf"
	if [ -f /etc/resolv.conf.head ]; then
		cat /etc/resolv.conf.head >> "$cf"
	else
		echo "# /etc/resolv.conf.head can replace this line" >> "$cf"
	fi
	printf %s "$domain$search$servers" >> "$cf"
	if [ -f /etc/resolv.conf.tail ]; then
		cat /etc/resolv.conf.tail >> "$cf"
	else
		echo "# /etc/resolv.conf.tail can replace this line" >> "$cf"
	fi
	if change_file /etc/resolv.conf "$cf"; then
		chmod 644 /etc/resolv.conf
	fi
	rm -f "$cf"
}

# Extract any ND DNS options from the RA
# For now, we ignore the lifetime of the DNS options unless they
# are absent or zero.
# In this case they are removed from consideration.
# See draft-gont-6man-slaac-dns-config-issues-01 for issues
# regarding DNS option lifetime in ND messages.
eval_nd_dns()
{

	eval ltime=\$nd${i}_rdnss${j}_lifetime
	if [ -z "$ltime" -o "$ltime" = 0 ]; then
		rdnss=
	else
		eval rdnss=\$nd${i}_rdnss${j}_servers
	fi
	eval ltime=\$nd${i}_dnssl${j}_lifetime
	if [ -z "$ltime" -o "$ltime" = 0 ]; then
		dnssl=
	else
		eval dnssl=\$nd${i}_dnssl${j}_search
	fi

	[ -z "$rdnss" -a -z "$dnssl" ] && return 1

	[ -n "$rdnss" ] && new_rdnss="$new_rdnss${new_rdnss:+ }$rdnss"
	[ -n "$dnssl" ] && new_dnssl="$new_dnssl${new_dnssl:+ }$dnssl"
	j=$(($j + 1))
	return 0
}

add_resolv_conf()
{
	local x= conf="$signature$NL" warn=true
	local i j ltime rdnss dnssl new_rdnss new_dnssl

	# Loop to extract the ND DNS options using our indexed shell values
	i=1
	j=1
	while true; do
		while true; do
			eval_nd_dns || break
		done
		i=$(($i + 1))
		j=1
		eval_nd_dns || break
	done
	[ -n "$new_rdnss" ] && \
	    new_domain_name_servers="$new_domain_name_servers${new_domain_name_servers:+ }$new_rdnss"
	[ -n "$new_dnssl" ] && \
	    new_domain_search="$new_domain_search${new_domain_search:+ }$new_dnssl"

	# Derive a new domain from our various hostname options
	if [ -z "$new_domain_name" ]; then
		if [ "$new_dhcp6_fqdn" != "${new_dhcp6_fqdn#*.}" ]; then
			new_domain_name="${new_dhcp6_fqdn#*.}"
		elif [ "$new_fqdn" != "${new_fqdn#*.}" ]; then
			new_domain_name="${new_fqdn#*.}"
		elif [ "$new_host_name" != "${new_host_name#*.}" ]; then
			new_domain_name="${new_host_name#*.}"
		fi
	fi

	# If we don't have any configuration, remove it
	if [ -z "$new_domain_name_servers" -a \
		-z "$new_domain_name" -a \
		-z "$new_domain_search" ]; then
		remove_resolv_conf
		return $?
	fi

	if [ -n "$new_domain_name" ]; then
		set -- $new_domain_name
		if valid_domainname "$1"; then
			conf="${conf}domain $1$NL"
		else
			syslog err "Invalid domain name: $1"
		fi
		# If there is no search this, make this one
		if [ -z "$new_domain_search" ]; then
			new_domain_search="$new_domain_name"
			[ "$new_domain_name" = "$1" ] && warn=true
		fi
	fi
	if [ -n "$new_domain_search" ]; then
		if valid_domainname_list $new_domain_search; then
			conf="${conf}search $new_domain_search$NL"
		elif ! $warn; then
			syslog err "Invalid domain name in list:" \
			    "$new_domain_search"
		fi
	fi
	for x in ${new_domain_name_servers}; do
		conf="${conf}nameserver $x$NL"
	done
	if type "$resolvconf" >/dev/null 2>&1; then
		[ -n "$ifmetric" ] && export IF_METRIC="$ifmetric"
		printf %s "$conf" | "$resolvconf" -a "$ifname"
		return $?
	fi

	if [ -e "$resolv_conf_dir/$ifname" ]; then
		rm -f "$resolv_conf_dir/$ifname"
	fi
	[ -d "$resolv_conf_dir" ] || mkdir -p "$resolv_conf_dir"
	printf %s "$conf" > "$resolv_conf_dir/$ifname"
	build_resolv_conf
}

remove_resolv_conf()
{
	if type "$resolvconf" >/dev/null 2>&1; then
		"$resolvconf" -d "$ifname" -f
	else
		if [ -e "$resolv_conf_dir/$ifname" ]; then
			rm -f "$resolv_conf_dir/$ifname"
		fi
		build_resolv_conf
	fi
}

# For ease of use, map DHCP6 names onto our DHCP4 names
case "$reason" in
BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
	new_domain_name_servers="$new_dhcp6_name_servers"
	new_domain_search="$new_dhcp6_domain_search"
	;;
esac

if $if_up || [ "$reason" = ROUTERADVERT ]; then
	add_resolv_conf
elif $if_down; then
	remove_resolv_conf
fi