Merge branch 'vendor/OPENRESOLV' with the following changes:
[dragonfly.git] / contrib / openresolv / named.in
1 #!/bin/sh
2 # Copyright (c) 2007-2016 Roy Marples
3 # All rights reserved
4
5 # named 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
29 [ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30 . "@SYSCONFDIR@/resolvconf.conf" || exit 1
31 [ -z "${named_zones}${named_options}" ] && exit 0
32 [ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
33 NL="
34 "
35
36 # Platform specific kludges
37 if [ -z "${named_service}${named_restart}" ] &&
38    [ -d "$RCDIR" ] && ! [ -x "$RCDIR"/named ]
39 then
40         if [ -x "$RCDIR"/bind9 ]; then
41                 # Debian and derivatives
42                 named_service=bind9
43         elif [ -x "$RCDIR"/rc.bind ]; then
44                 # Slackware
45                 named_service=rc.bind
46         fi
47 fi
48 : ${named_service:=named}
49
50 : ${named_pid:=/var/run/$named_service.pid}
51 [ -s "$named_pid" ] || named_pid=/var/run/$named_service/$named_service.pid
52 [ -s "$named_pid" ] || unset named_pid
53
54 newoptions="# Generated by resolvconf$NL"
55 newzones="$newoptions"
56
57 forward=
58 for n in $NAMESERVERS; do
59         case "$forward" in
60         *"$NL   $n;"*);;
61         *) forward="$forward$NL $n;";;
62         esac
63 done
64 if [ -n "$forward" ]; then
65         newoptions="${newoptions}forward first;${NL}forwarders {$forward${NL}};$NL"
66 fi
67
68 for d in $DOMAINS; do
69         newzones="${newzones}zone \"${d%%:*}\" {$NL"
70         newzones="$newzones     type forward;$NL"
71         newzones="$newzones     forward first;$NL       forwarders {$NL"
72         ns="${d#*:}"
73         while [ -n "$ns" ]; do
74                 newzones="$newzones             ${ns%%,*};$NL"
75                 [ "$ns" = "${ns#*,}" ] && break
76                 ns="${ns#*,}"
77         done
78         newzones="$newzones     };$NL};$NL"
79 done
80
81 # Try to ensure that config dirs exist
82 if type config_mkdirs >/dev/null 2>&1; then
83         config_mkdirs "$named_options" "$named_zones"
84 else
85         @SBINDIR@/resolvconf -D "$named_options" "$named_zones"
86 fi
87
88 # No point in changing files or reloading bind if the end result has not
89 # changed
90 changed=false
91 if [ -n "$named_options" ]; then
92         if [ ! -f "$named_options" ] || \
93                 [ "$(cat "$named_options")" != "$(printf %s "$newoptions")" ]
94         then
95                 printf %s "$newoptions" >"$named_options"
96                 changed=true
97         fi
98 fi
99 if [ -n "$named_zones" ]; then
100         if [ ! -f "$named_zones" ] || \
101                 [ "$(cat "$named_zones")" != "$(printf %s "$newzones")" ]
102         then
103                 printf %s "$newzones" >"$named_zones"
104                 changed=true
105         fi
106 fi
107
108 # named does not seem to work with SIGHUP which is a same
109 if $changed; then
110         if [ -n "$named_restart" ]; then
111                 eval $named_restart
112         elif [ -n "$RESTARTCMD" ]; then
113                 set -- ${named_service}
114                 eval "$RESTARTCMD"
115         else
116                 @SBINDIR@/resolvconf -r ${named_service}
117         fi
118 fi