3 # Copyright (c) 2013 Dag-Erling Smørgrav
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # Configuration variables
53 self=$(basename $(realpath "$0"))
54 bkext=$(date "+%Y%m%d.%H%M%S")
57 # Set default values for unset configuration variables.
61 : ${workdir:=/var/unbound}
62 : ${confdir:=${workdir}/conf.d}
63 : ${unbound_conf:=${workdir}/unbound.conf}
64 : ${forward_conf:=${workdir}/forward.conf}
65 : ${lanzones_conf:=${workdir}/lan-zones.conf}
66 : ${control_conf:=${workdir}/control.conf}
67 : ${control_socket:=/var/run/local_unbound.ctl}
68 : ${anchor:=${workdir}/root.key}
69 : ${pidfile:=/var/run/local_unbound.pid}
70 : ${resolv_conf:=/etc/resolv.conf}
71 : ${resolvconf_conf:=/etc/resolvconf.conf}
72 : ${service:=local_unbound}
73 : ${start_unbound:=yes}
77 # Verify that the configuration files are inside the working
78 # directory, and if so, set the chroot directory accordingly.
81 chrootdir="${workdir}"
82 for file in "${unbound_conf}" "${forward_conf}" \
83 "${lanzones_conf}" "${control_conf}" "${anchor}" ; do
84 if [ "${file#${workdir%/}/}" = "${file}" ] ; then
85 echo "warning: ${file} is outside ${workdir}" >&2
89 if [ -z "${chrootdir}" ] ; then
90 echo "warning: disabling chroot" >&2
95 # Scan through /etc/resolv.conf looking for uncommented nameserver
96 # lines that don't point to localhost and return their values.
100 local bareline=${line%%\#*}
101 local key=${bareline%% *}
102 local value=${bareline#* }
106 127.0.0.1|::1|localhost|localhost.*)
118 # Scan through /etc/resolv.conf looking for uncommented nameserver
119 # lines. Comment out any that don't point to localhost. Finally,
120 # append a nameserver line that points to localhost, if there wasn't
121 # one already, and enable the edns0 option.
127 local bareline=${line%%\#*}
128 local key=${bareline%% *}
129 local value=${bareline#* }
133 127.0.0.1|::1|localhost|localhost.*)
151 if [ "${localhost}" = "no" ] ; then
152 echo "nameserver 127.0.0.1"
154 if [ "${edns0}" = "no" ] ; then
163 echo "# This file was generated by $self."
164 echo "# Modifications will be overwritten."
168 # Generate resolvconf.conf so it updates forward.conf in addition to
169 # resolv.conf. Note "in addition to" rather than "instead of",
170 # because we still want it to update the domain name and search path
171 # if they change. Setting name_servers to "127.0.0.1" ensures that
172 # the libc resolver will try unbound first.
174 gen_resolvconf_conf() {
177 echo "resolv_conf=\"/dev/null\" # prevent updating ${resolv_conf}"
178 if [ "${style}" = "dynamic" ] ; then
179 echo "unbound_conf=\"${forward_conf}\""
180 echo "unbound_pid=\"${pidfile}\""
181 echo "unbound_service=\"${service}\""
182 # resolvconf(8) likes to restart rather than reload
183 echo "unbound_restart=\"service ${service} reload\""
185 echo "# Static DNS configuration"
190 # Generate forward.conf
197 if expr "${forwarder}" : "^[0-9A-Fa-f:.]\{1,\}$" >/dev/null ; then
198 echo " forward-addr: ${forwarder}"
200 echo " forward-host: ${forwarder}"
206 # Generate lan-zones.conf
208 gen_lanzones_conf() {
211 echo " # Unblock reverse lookups for LAN addresses"
212 echo " unblock-lan-zones: yes"
213 echo " insecure-lan-zones: yes"
217 # Generate control.conf
221 echo "remote-control:"
222 echo " control-enable: yes"
223 echo " control-interface: ${control_socket}"
224 echo " control-use-cert: no"
228 # Generate unbound.conf
233 echo " username: ${user}"
234 echo " directory: ${workdir}"
235 echo " chroot: ${chrootdir}"
236 echo " pidfile: ${pidfile}"
237 echo " auto-trust-anchor-file: ${anchor}"
239 if [ -f "${forward_conf}" ] ; then
240 echo "include: ${forward_conf}"
242 if [ -f "${lanzones_conf}" ] ; then
243 echo "include: ${lanzones_conf}"
245 if [ -f "${control_conf}" ] ; then
246 echo "include: ${control_conf}"
248 if [ -d "${confdir}" ] ; then
249 echo "include: ${confdir}/*.conf"
254 # Replace one file with another, making a backup copy of the first,
255 # but only if the new file is different from the old.
260 if [ ! -f "${file}" ] ; then
261 echo "${file} created"
262 mv "${newfile}" "${file}"
263 elif ! cmp -s "${file}" "${newfile}" ; then
264 local oldfile="${file}.${bkext}"
265 echo "original ${file} saved as ${oldfile}"
266 mv "${file}" "${oldfile}"
267 mv "${newfile}" "${file}"
269 echo "${file} not modified"
275 # Print usage message and exit
279 echo "usage: $self [options] [forwarder ...]"
281 echo " -n do not start unbound"
282 echo " -a path full path to trust anchor file"
283 echo " -C path full path to additional configuration directory"
284 echo " -c path full path to unbound configuration file"
285 echo " -f path full path to forwarding configuration"
286 echo " -O path full path to remote control socket"
287 echo " -o path full path to remote control configuration"
288 echo " -p path full path to pid file"
289 echo " -R path full path to resolvconf.conf"
290 echo " -r path full path to resolv.conf"
291 echo " -s service name of unbound service"
292 echo " -u user user to run unbound as"
293 echo " -w path full path to working directory"
304 # Parse and validate command-line options
306 while getopts "a:C:c:f:no:p:R:r:s:u:w:" option ; do
315 unbound_conf="$OPTARG"
318 forward_conf="$OPTARG"
324 control_socket="$OPTARG"
327 control_conf="$OPTARG"
333 resolvconf_conf="$OPTARG"
336 resolv_conf="$OPTARG"
356 # Get the list of forwarders, either from the command line or
360 if [ -z "$forwarders" ] ; then
361 echo "Extracting forwarders from ${resolv_conf}."
362 forwarders=$(get_nameservers <"${resolv_conf}")
369 # Generate forward.conf.
371 if [ -z "${forwarders}" ] ; then
372 echo -n "No forwarders found in ${resolv_conf##*/}, "
373 if [ -f "${forward_conf}" ] ; then
374 echo "using existing ${forward_conf##*/}."
376 echo "unbound will recurse."
379 local tmp_forward_conf=$(mktemp -u "${forward_conf}.XXXXX")
380 gen_forward_conf ${forwarders} | unexpand >"${tmp_forward_conf}"
381 replace "${forward_conf}" "${tmp_forward_conf}"
385 # Generate lan-zones.conf.
387 local tmp_lanzones_conf=$(mktemp -u "${lanzones_conf}.XXXXX")
388 gen_lanzones_conf | unexpand >"${tmp_lanzones_conf}"
389 replace "${lanzones_conf}" "${tmp_lanzones_conf}"
392 # Generate control.conf.
394 local tmp_control_conf=$(mktemp -u "${control_conf}.XXXXX")
395 gen_control_conf | unexpand >"${tmp_control_conf}"
396 replace "${control_conf}" "${tmp_control_conf}"
399 # Generate unbound.conf.
401 local tmp_unbound_conf=$(mktemp -u "${unbound_conf}.XXXXX")
403 gen_unbound_conf | unexpand >"${tmp_unbound_conf}"
404 replace "${unbound_conf}" "${tmp_unbound_conf}"
407 # Start unbound, unless requested not to. Stop immediately if
408 # it is not enabled so we don't end up with a resolv.conf that
409 # points into nothingness. We could "onestart" it, but it
412 if [ "${start_unbound}" = "no" ] ; then
414 elif ! service "${service}" enabled ; then
415 echo "Please enable $service in rc.conf(5) and try again."
417 elif ! service "${service}" restart ; then
418 echo "Failed to start $service."
423 # Rewrite resolvconf.conf so resolvconf updates forward.conf
424 # instead of resolv.conf.
426 local tmp_resolvconf_conf=$(mktemp -u "${resolvconf_conf}.XXXXX")
427 gen_resolvconf_conf "${style}" | unexpand >"${tmp_resolvconf_conf}"
428 replace "${resolvconf_conf}" "${tmp_resolvconf_conf}"
431 # Finally, rewrite resolv.conf.
433 local tmp_resolv_conf=$(mktemp -u "${resolv_conf}.XXXXX")
434 gen_resolv_conf <"${resolv_conf}" | unexpand >"${tmp_resolv_conf}"
435 replace "${resolv_conf}" "${tmp_resolv_conf}"