]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - etc/rc.d/local_unbound
MFH (r287917, r287918, r289063): upgrade to latest Unbound
[FreeBSD/stable/10.git] / etc / rc.d / local_unbound
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5
6 # PROVIDE: local_unbound
7 # REQUIRE: FILESYSTEMS netif resolv
8 # BEFORE: NETWORKING
9 # KEYWORD: shutdown
10
11 . /etc/rc.subr
12
13 name="local_unbound"
14 desc="local caching forwarding resolver"
15 rcvar="local_unbound_enable"
16
17 command="/usr/sbin/unbound"
18 extra_commands="anchor configtest reload setup"
19 start_precmd="local_unbound_prestart"
20 start_postcmd="local_unbound_poststart"
21 reload_precmd="local_unbound_configtest"
22 anchor_cmd="local_unbound_anchor"
23 configtest_cmd="local_unbound_configtest"
24 setup_cmd="local_unbound_setup"
25 pidfile="/var/run/${name}.pid"
26
27 load_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_anchor:=${local_unbound_workdir}/root.key}
34 : ${local_unbound_forwarders:=}
35
36 do_as_unbound()
37 {
38         echo "$@" | su -m unbound
39 }
40
41 #
42 # Retrieve or update the DNSSEC root anchor
43 #
44 local_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 #
54 local_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 #
63 local_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             -a ${local_unbound_anchor} \
72             ${local_unbound_forwarders}
73 }
74
75 #
76 # Before starting, check that the configuration file and root anchor
77 # exist.  If not, attempt to generate them.
78 #
79 local_unbound_prestart()
80 {
81         # Create configuration file
82         if [ ! -f ${local_unbound_config} ] ; then
83                 run_rc_command setup
84         fi
85
86         # Retrieve DNSSEC root key
87         if [ ! -f ${local_unbound_anchor} ] ; then
88                 run_rc_command anchor
89         fi
90 }
91
92 #
93 # After starting, wait for Unbound to report that it is ready to avoid
94 # race conditions with services which require functioning DNS.
95 #
96 local_unbound_poststart()
97 {
98         local retry=5
99
100         echo -n "Waiting for nameserver to start..."
101         until "${command}-control" status | grep -q "is running" ; do
102                 if [ $((retry -= 1)) -eq 0 ] ; then
103                         echo " giving up"
104                         return 1
105                 fi
106                 echo -n "."
107                 sleep 1
108         done
109         echo " good"
110 }
111
112 load_rc_config $name
113 run_rc_command "$1"