3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2013 Dag-Erling Smørgrav
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # Configuration variables
55 self=$(basename $(realpath "$0"))
56 bkext=$(date "+%Y%m%d.%H%M%S")
59 # Set default values for unset configuration variables.
63 : ${workdir:=/var/unbound}
64 : ${confdir:=${workdir}/conf.d}
65 : ${unbound_conf:=${workdir}/unbound.conf}
66 : ${forward_conf:=${workdir}/forward.conf}
67 : ${lanzones_conf:=${workdir}/lan-zones.conf}
68 : ${control_conf:=${workdir}/control.conf}
69 : ${control_socket:=/var/run/local_unbound.ctl}
70 : ${anchor:=${workdir}/root.key}
71 : ${pidfile:=/var/run/local_unbound.pid}
72 : ${resolv_conf:=/etc/resolv.conf}
73 : ${resolvconf_conf:=/etc/resolvconf.conf}
74 : ${service:=local_unbound}
75 : ${start_unbound:=yes}
79 # Verify that the configuration files are inside the working
80 # directory, and if so, set the chroot directory accordingly.
83 chrootdir="${workdir}"
84 for file in "${unbound_conf}" "${forward_conf}" \
85 "${lanzones_conf}" "${control_conf}" "${anchor}" ; do
86 if [ "${file#${workdir%/}/}" = "${file}" ] ; then
87 echo "warning: ${file} is outside ${workdir}" >&2
91 if [ -z "${chrootdir}" ] ; then
92 echo "warning: disabling chroot" >&2
97 # Scan through /etc/resolv.conf looking for uncommented nameserver
98 # lines that don't point to localhost and return their values.
102 local bareline=${line%%\#*}
103 local key=${bareline%% *}
104 local value=${bareline#* }
108 127.0.0.1|::1|localhost|localhost.*)
120 # Scan through /etc/resolv.conf looking for uncommented nameserver
121 # lines. Comment out any that don't point to localhost. Finally,
122 # append a nameserver line that points to localhost, if there wasn't
123 # one already, and enable the edns0 option.
129 local bareline=${line%%\#*}
130 local key=${bareline%% *}
131 local value=${bareline#* }
135 127.0.0.1|::1|localhost|localhost.*)
153 if [ "${localhost}" = "no" ] ; then
154 echo "nameserver 127.0.0.1"
156 if [ "${edns0}" = "no" ] ; then
165 echo "# This file was generated by $self."
166 echo "# Modifications will be overwritten."
170 # Generate resolvconf.conf so it updates forward.conf in addition to
171 # resolv.conf. Note "in addition to" rather than "instead of",
172 # because we still want it to update the domain name and search path
173 # if they change. Setting name_servers to "127.0.0.1" ensures that
174 # the libc resolver will try unbound first.
176 gen_resolvconf_conf() {
179 echo "resolv_conf=\"/dev/null\" # prevent updating ${resolv_conf}"
180 if [ "${style}" = "dynamic" ] ; then
181 echo "unbound_conf=\"${forward_conf}\""
182 echo "unbound_pid=\"${pidfile}\""
183 echo "unbound_service=\"${service}\""
184 # resolvconf(8) likes to restart rather than reload
185 echo "unbound_restart=\"service ${service} reload\""
187 echo "# Static DNS configuration"
192 # Generate forward.conf
199 if expr "${forwarder}" : "^[0-9A-Fa-f:.]\{1,\}$" >/dev/null ; then
200 echo " forward-addr: ${forwarder}"
202 echo " forward-host: ${forwarder}"
208 # Generate lan-zones.conf
210 gen_lanzones_conf() {
213 echo " # Unblock reverse lookups for LAN addresses"
214 echo " unblock-lan-zones: yes"
215 echo " insecure-lan-zones: yes"
219 # Generate control.conf
223 echo "remote-control:"
224 echo " control-enable: yes"
225 echo " control-interface: ${control_socket}"
226 echo " control-use-cert: no"
230 # Generate unbound.conf
235 echo " username: ${user}"
236 echo " directory: ${workdir}"
237 echo " chroot: ${chrootdir}"
238 echo " pidfile: ${pidfile}"
239 echo " auto-trust-anchor-file: ${anchor}"
241 if [ -f "${forward_conf}" ] ; then
242 echo "include: ${forward_conf}"
244 if [ -f "${lanzones_conf}" ] ; then
245 echo "include: ${lanzones_conf}"
247 if [ -f "${control_conf}" ] ; then
248 echo "include: ${control_conf}"
250 if [ -d "${confdir}" ] ; then
251 echo "include: ${confdir}/*.conf"
256 # Replace one file with another, making a backup copy of the first,
257 # but only if the new file is different from the old.
262 if [ ! -f "${file}" ] ; then
263 echo "${file} created"
264 mv "${newfile}" "${file}"
265 elif ! cmp -s "${file}" "${newfile}" ; then
266 local oldfile="${file}.${bkext}"
267 echo "original ${file} saved as ${oldfile}"
268 mv "${file}" "${oldfile}"
269 mv "${newfile}" "${file}"
271 echo "${file} not modified"
277 # Print usage message and exit
281 echo "usage: $self [options] [forwarder ...]"
283 echo " -n do not start unbound"
284 echo " -a path full path to trust anchor file"
285 echo " -C path full path to additional configuration directory"
286 echo " -c path full path to unbound configuration file"
287 echo " -f path full path to forwarding configuration"
288 echo " -O path full path to remote control socket"
289 echo " -o path full path to remote control configuration"
290 echo " -p path full path to pid file"
291 echo " -R path full path to resolvconf.conf"
292 echo " -r path full path to resolv.conf"
293 echo " -s service name of unbound service"
294 echo " -u user user to run unbound as"
295 echo " -w path full path to working directory"
306 # Parse and validate command-line options
308 while getopts "a:C:c:f:no:p:R:r:s:u:w:" option ; do
317 unbound_conf="$OPTARG"
320 forward_conf="$OPTARG"
326 control_socket="$OPTARG"
329 control_conf="$OPTARG"
335 resolvconf_conf="$OPTARG"
338 resolv_conf="$OPTARG"
358 # Get the list of forwarders, either from the command line or
362 if [ -z "$forwarders" ] ; then
363 echo "Extracting forwarders from ${resolv_conf}."
364 forwarders=$(get_nameservers <"${resolv_conf}")
371 # Generate forward.conf.
373 if [ -z "${forwarders}" ] ; then
374 echo -n "No forwarders found in ${resolv_conf##*/}, "
375 if [ -f "${forward_conf}" ] ; then
376 echo "using existing ${forward_conf##*/}."
378 echo "unbound will recurse."
381 local tmp_forward_conf=$(mktemp -u "${forward_conf}.XXXXX")
382 gen_forward_conf ${forwarders} | unexpand >"${tmp_forward_conf}"
383 replace "${forward_conf}" "${tmp_forward_conf}"
387 # Generate lan-zones.conf.
389 local tmp_lanzones_conf=$(mktemp -u "${lanzones_conf}.XXXXX")
390 gen_lanzones_conf | unexpand >"${tmp_lanzones_conf}"
391 replace "${lanzones_conf}" "${tmp_lanzones_conf}"
394 # Generate control.conf.
396 local tmp_control_conf=$(mktemp -u "${control_conf}.XXXXX")
397 gen_control_conf | unexpand >"${tmp_control_conf}"
398 replace "${control_conf}" "${tmp_control_conf}"
401 # Generate unbound.conf.
403 local tmp_unbound_conf=$(mktemp -u "${unbound_conf}.XXXXX")
405 gen_unbound_conf | unexpand >"${tmp_unbound_conf}"
406 replace "${unbound_conf}" "${tmp_unbound_conf}"
409 # Start unbound, unless requested not to. Stop immediately if
410 # it is not enabled so we don't end up with a resolv.conf that
411 # points into nothingness. We could "onestart" it, but it
414 if [ "${start_unbound}" = "no" ] ; then
416 elif ! service "${service}" enabled ; then
417 echo "Please enable $service in rc.conf(5) and try again."
419 elif ! service "${service}" restart ; then
420 echo "Failed to start $service."
425 # Rewrite resolvconf.conf so resolvconf updates forward.conf
426 # instead of resolv.conf.
428 local tmp_resolvconf_conf=$(mktemp -u "${resolvconf_conf}.XXXXX")
429 gen_resolvconf_conf "${style}" | unexpand >"${tmp_resolvconf_conf}"
430 replace "${resolvconf_conf}" "${tmp_resolvconf_conf}"
433 # Finally, rewrite resolv.conf.
435 local tmp_resolv_conf=$(mktemp -u "${resolv_conf}.XXXXX")
436 gen_resolv_conf <"${resolv_conf}" | unexpand >"${tmp_resolv_conf}"
437 replace "${resolv_conf}" "${tmp_resolv_conf}"