1#!/bin/sh
2# Copyright (c) 2009-2023 Roy Marples
3# All rights reserved
4
5# unbound subscriber for resolvconf
6
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#     * Redistributions of source code must retain the above copyright
11#       notice, this list of conditions and the following disclaimer.
12#     * Redistributions in binary form must reproduce the above
13#       copyright notice, this list of conditions and the following
14#       disclaimer in the documentation and/or other materials provided
15#       with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29unbound_insecure=
30unbound_private=
31
32[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
33. "@SYSCONFDIR@/resolvconf.conf" || exit 1
34[ -z "$unbound_conf" ] && exit 0
35[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
36NL="
37"
38
39: ${unbound_pid:=/var/run/unbound.pid}
40: ${unbound_service:=unbound}
41newconf="# Generated by resolvconf$NL"
42
43for d in $DOMAINS; do
44          dn="${d%%:*}"
45          ns="${d#*:}"
46          create_unbound_insecure=false
47          create_unbound_private=false
48          case "$unbound_insecure" in
49          [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
50                    create_unbound_insecure=true ;;
51          esac
52          case "$unbound_private" in
53          [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
54                    create_unbound_private=true ;;
55          esac
56          if $create_unbound_insecure || $create_unbound_private; then
57                    newconf="$newconf${NL}server:$NL"
58                    if $create_unbound_insecure; then
59                              newconf="$newconf   domain-insecure: \"$dn\"$NL"
60                    fi
61                    if $create_unbound_private; then
62                              newconf="$newconf   private-domain: \"$dn\"$NL"
63                    fi
64          fi
65          newconf="$newconf${NL}forward-zone:$NL  name: \"$dn\"$NL"
66          if [ -n "$unbound_forward_zone_options" ]; then
67                    newconf="$newconf   $unbound_forward_zone_options${NL}"
68          fi
69          while [ -n "$ns" ]; do
70                    newconf="$newconf   forward-addr: ${ns%%,*}$NL"
71                    [ "$ns" = "${ns#*,}" ] && break
72                    ns="${ns#*,}"
73          done
74done
75
76if [ -n "$NAMESERVERS" ]; then
77          newconf="$newconf${NL}forward-zone:$NL  name: \".\"$NL"
78          if [ -n "$unbound_forward_zone_options" ]; then
79                    newconf="$newconf   $unbound_forward_zone_options${NL}"
80          fi
81          for n in $NAMESERVERS; do
82                    newconf="$newconf   forward-addr: $n$NL"
83          done
84fi
85
86# Try to ensure that config dirs exist
87if command -v config_mkdirs >/dev/null 2>&1; then
88          config_mkdirs "$unbound_conf"
89else
90          @SBINDIR@/resolvconf -D "$unbound_conf"
91fi
92
93restart_unbound()
94{
95          if [ -n "$unbound_restart" ]; then
96                    eval $unbound_restart
97          elif [ -n "$RESTARTCMD" ]; then
98                    set -- ${unbound_service}
99                    eval "$RESTARTCMD"
100          else
101                    @SBINDIR@/resolvconf -r ${unbound_service}
102          fi
103}
104
105if [ ! -f "$unbound_conf" ] || \
106          [ "$(cat "$unbound_conf")" != "$(printf %s "$newconf")" ]
107then
108          printf %s "$newconf" >"$unbound_conf"
109          # If we can't sent a HUP then force a restart
110          if [ -s "$unbound_pid" ]; then
111                    if ! kill -HUP $(cat "$unbound_pid") 2>/dev/null; then
112                              restart_unbound
113                    fi
114          else
115                    restart_unbound
116          fi
117fi
118