]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/unbound/files/unbound.init
Ship init script for unbound
[dsa-puppet.git] / modules / unbound / files / unbound.init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          unbound
5 # Required-Start:    $network $remote_fs $syslog
6 # Required-Stop:     $network $remote_fs $syslog
7 # Default-Start:     2 3 4 5
8 # Default-Stop:      0 1 6
9 ### END INIT INFO
10
11 NAME=unbound
12 DESC="recursive DNS server"
13 DAEMON=/usr/sbin/unbound
14 PIDFILE="/var/run/unbound.pid"
15
16 test -x $DAEMON || exit 0
17 test -x ${DAEMON}-checkconf || exit 0
18
19 . /lib/lsb/init-functions
20
21 UNBOUND_ENABLE=true
22 UNBOUND_CONF=/etc/unbound/unbound.conf
23 UNBOUND_BASE_DIR=$(dirname $UNBOUND_CONF)
24 CHROOT_DIR=$(awk '{if ($1 ~ "^chroot" && $2 != "\"\"") print $2}' $UNBOUND_CONF|sed -e "s#\"##g")
25 ROOT_TRUST_ANCHOR_UPDATE=false
26 ROOT_TRUST_ANCHOR_FILE=/var/lib/unbound/root.key
27 RESOLVCONF=false
28 RESOLVCONF_FORWARDERS=false
29
30 if [ -f /etc/default/$NAME ]; then
31     . /etc/default/$NAME
32     case "x$UNBOUND_ENABLE" in
33         xtrue|x1|xyes)
34             UNBOUND_ENABLE=true
35             ;;
36         *)
37             UNBOUND_ENABLE=false
38             ;;
39     esac
40     case "x$ROOT_TRUST_ANCHOR_UPDATE" in
41         xtrue|x1|xyes)
42             ROOT_TRUST_ANCHOR_UPDATE=true
43             ;;
44         *)
45             ROOT_TRUST_ANCHOR_UPDATE=false
46             ;;
47     esac
48     case "x$RESOLVCONF" in
49         xtrue|x1|xyes)
50             RESOLVCONF=true
51             ;;
52         *)
53             RESOLVCONF=false
54     esac
55     case "x$RESOLVCONF_FORWARDERS" in
56         xtrue|x1|xyes)
57             RESOLVCONF_FORWARDERS=true
58             ;;
59         *)
60             RESOLVCONF_FORWARDERS=false
61     esac
62 fi
63
64 do_resolvconf_start() {
65     if $RESOLVCONF; then
66         if [ -x /sbin/resolvconf ]; then
67             unbound-checkconf $CHROOT_DIR/$UNBOUND_CONF -o interface | (
68                 default=yes
69                 while read interface; do
70                     default=no
71                     if [ "x$interface" = x0.0.0.0 -o "x$interface" = x127.0.0.1 ]; then
72                         echo "nameserver 127.0.0.1"
73                     elif [ "x$interface" = x::0 -o "x$interface" = x::1 ]; then
74                         echo "nameserver ::1"
75                     fi
76                 done
77                 if [ $default = yes ]; then
78                     # unbound defaults to listening on localhost
79                     echo "nameserver 127.0.0.1"
80                 fi
81             ) | /sbin/resolvconf -a lo.unbound
82         fi
83     fi
84 }
85
86 do_resolvconf_stop() {
87     if $RESOLVCONF; then
88         if [ -x /sbin/resolvconf ]; then
89             /sbin/resolvconf -d lo.unbound
90         fi
91     fi
92 }
93
94 do_chroot_setup() {
95     if [ -d "$CHROOT_DIR" -a "$CHROOT_DIR" != "$UNBOUND_BASE_DIR" ]; then
96         cd /
97         tar --overwrite -cf - $(echo $UNBOUND_BASE_DIR | sed 's#^/##') | (cd $CHROOT_DIR && tar -xf -)
98     fi
99 }
100
101 case "$1" in
102     start)
103         if $UNBOUND_ENABLE; then
104             do_chroot_setup
105             if $ROOT_TRUST_ANCHOR_UPDATE; then
106                 unbound-anchor -a $ROOT_TRUST_ANCHOR_FILE -v 2>&1 | logger -p daemon.info -t unbound-anchor
107                 chown unbound:unbound $ROOT_TRUST_ANCHOR_FILE
108             fi
109             log_daemon_msg "Starting $DESC" "$NAME"
110             if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --name $NAME --startas $DAEMON -- $DAEMON_OPTS; then
111                 do_resolvconf_start
112                 log_end_msg 0
113             else
114                 log_end_msg 1
115             fi
116         else
117             log_warning_msg "Not starting $DESC $NAME, disabled via /etc/default/$NAME"
118         fi
119         ;;
120
121     stop)
122         if $UNBOUND_ENABLE; then
123             log_daemon_msg "Stopping $DESC" "$NAME"
124             if start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --name $NAME; then
125                 do_resolvconf_stop
126                 log_end_msg 0
127             else
128                 log_end_msg 1
129             fi
130         fi
131         ;;
132
133     restart|force-reload)
134         if $UNBOUND_ENABLE; then
135             log_daemon_msg "Restarting $DESC" "$NAME"
136             start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME --retry 5
137             do_resolvconf_stop
138             if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --name $NAME --startas $DAEMON -- $DAEMON_OPTS; then
139                 do_chroot_setup
140                 do_resolvconf_start
141                 log_end_msg 0
142             else
143                 log_end_msg 1
144             fi
145         fi
146         ;;
147
148     reload)
149         if $UNBOUND_ENABLE; then
150             log_daemon_msg "Reloading $DESC" "$NAME"
151             if start-stop-daemon --stop --pidfile $PIDFILE --signal 1; then
152                 do_chroot_setup
153                 log_end_msg 0
154             else
155                 log_end_msg 1
156             fi
157         fi
158         ;;
159
160     status)
161         status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
162         ;;
163
164     *)
165         N=/etc/init.d/$NAME
166         echo "Usage: $N {start|stop|restart|status|reload|force-reload}" >&2
167         exit 1
168         ;;
169 esac
170
171 exit 0;