xref: /freebsd/libexec/rc/rc.d/local_unbound (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5
6# PROVIDE: local_unbound
7# REQUIRE: FILESYSTEMS defaultroute netwait resolv
8# BEFORE: NETWORKING
9# KEYWORD: shutdown
10
11. /etc/rc.subr
12
13name="local_unbound"
14desc="Local caching forwarding resolver"
15rcvar="local_unbound_enable"
16
17command="/usr/sbin/local-unbound"
18extra_commands="anchor configtest reload setup"
19start_precmd="local_unbound_prestart"
20start_postcmd="local_unbound_poststart"
21reload_precmd="local_unbound_configtest"
22anchor_cmd="local_unbound_anchor"
23configtest_cmd="local_unbound_configtest"
24setup_cmd="local_unbound_setup"
25pidfile="/var/run/${name}.pid"
26
27load_rc_config $name
28
29: ${local_unbound_workdir:=/var/unbound}
30: ${local_unbound_config:=${local_unbound_workdir}/unbound.conf}
31: ${local_unbound_flags:="-c ${local_unbound_config}"}
32: ${local_unbound_forwardconf:=${local_unbound_workdir}/forward.conf}
33: ${local_unbound_controlconf:=${local_unbound_workdir}/control.conf}
34: ${local_unbound_anchor:=${local_unbound_workdir}/root.key}
35: ${local_unbound_forwarders:=}
36: ${local_unbound_tls:=}
37: ${local_unbound_pidfile:=${pidfile}}
38pidfile=${local_unbound_pidfile}
39
40do_as_unbound()
41{
42	echo "$@" | su -m unbound
43}
44
45#
46# Retrieve or update the DNSSEC root anchor
47#
48local_unbound_anchor()
49{
50	do_as_unbound ${command}-anchor -a ${local_unbound_anchor}
51	# we can't trust the exit code - check if the file exists
52	[ -f ${local_unbound_anchor} ]
53}
54
55#
56# Check the unbound configuration file
57#
58local_unbound_configtest()
59{
60	do_as_unbound ${command}-checkconf ${local_unbound_config}
61}
62
63#
64# Create the unbound configuration file and update resolv.conf to
65# point to unbound.
66#
67local_unbound_setup()
68{
69	local tls_flag
70	if checkyesno local_unbound_tls ; then
71		tls_flag="-t"
72	fi
73	echo "Performing initial setup."
74	${command}-setup -n \
75	    -u unbound \
76	    -w ${local_unbound_workdir} \
77	    -c ${local_unbound_config} \
78	    -f ${local_unbound_forwardconf} \
79	    -o ${local_unbound_controlconf} \
80	    -a ${local_unbound_anchor} \
81	    ${tls_flag} \
82	    ${local_unbound_forwarders}
83}
84
85#
86# Before starting, check that the configuration file and root anchor
87# exist.  If not, attempt to generate them.
88#
89local_unbound_prestart()
90{
91	# Create configuration file
92	if [ ! -f ${local_unbound_config} ] ; then
93		run_rc_command setup
94	fi
95
96	# Retrieve DNSSEC root key
97	if [ ! -s ${local_unbound_anchor} ] ; then
98		run_rc_command anchor
99	fi
100}
101
102#
103# After starting, wait for Unbound to report that it is ready to avoid
104# race conditions with services which require functioning DNS.
105#
106local_unbound_poststart()
107{
108	local retry=5
109
110	echo -n "Waiting for nameserver to start..."
111	until "${command}-control" -c "${local_unbound_config}" status | grep -q "is running" ; do
112		if [ $((retry -= 1)) -eq 0 ] ; then
113			echo " giving up"
114			return 1
115		fi
116		echo -n "."
117		sleep 1
118	done
119	echo " good"
120}
121
122load_rc_config $name
123run_rc_command "$1"
124