local_unbound revision 278704
1#!/bin/sh
2#
3# $FreeBSD: head/etc/rc.d/local_unbound 278704 2015-02-13 20:49:43Z ngie $
4#
5
6# PROVIDE: local_unbound
7# REQUIRE: FILESYSTEMS netif 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/unbound"
18extra_commands="anchor configtest reload setup"
19start_precmd="local_unbound_prestart"
20reload_precmd="local_unbound_configtest"
21anchor_cmd="local_unbound_anchor"
22configtest_cmd="local_unbound_configtest"
23setup_cmd="local_unbound_setup"
24pidfile="/var/run/${name}.pid"
25
26: ${local_unbound_workdir:=/var/unbound}
27: ${local_unbound_config:=${local_unbound_workdir}/unbound.conf}
28: ${local_unbound_flags:=-c${local_unbound_config}}
29: ${local_unbound_forwardconf:=${local_unbound_workdir}/forward.conf}
30: ${local_unbound_controlconf:=${local_unbound_workdir}/control.conf}
31: ${local_unbound_anchor:=${local_unbound_workdir}/root.key}
32: ${local_unbound_forwarders:=}
33
34load_rc_config $name
35
36do_as_unbound()
37{
38	echo "$@" | su -m unbound
39}
40
41#
42# Retrieve or update the DNSSEC root anchor
43#
44local_unbound_anchor()
45{
46	do_as_unbound /usr/sbin/unbound-anchor -a ${local_unbound_anchor}
47	# we can't trust the exit code - check if the file exists
48	[ -f ${local_unbound_anchor} ]
49}
50
51#
52# Check the unbound configuration file
53#
54local_unbound_configtest()
55{
56	do_as_unbound /usr/sbin/unbound-checkconf ${local_unbound_config}
57}
58
59#
60# Create the unbound configuration file and update resolv.conf to
61# point to unbound.
62#
63local_unbound_setup()
64{
65	echo "Performing initial setup."
66	/usr/sbin/local-unbound-setup -n \
67	    -u unbound \
68	    -w ${local_unbound_workdir} \
69	    -c ${local_unbound_config} \
70	    -f ${local_unbound_forwardconf} \
71	    -o ${local_unbound_controlconf} \
72	    -a ${local_unbound_anchor} \
73	    ${local_unbound_forwarders}
74}
75
76#
77# Before starting, check that the configuration file and root anchor
78# exist.  If not, attempt to generate them.
79#
80local_unbound_prestart()
81{
82	# Create configuration file
83	if [ ! -f ${local_unbound_config} ] ; then
84		run_rc_command setup
85	fi
86
87	# Retrieve DNSSEC root key
88	if [ ! -f ${local_unbound_anchor} ] ; then
89		run_rc_command anchor
90	fi
91}
92
93load_rc_config $name
94run_rc_command "$1"
95