We need to process dynamic interfaces without metrics before ones
[openresolv] / named.in
1 #!/bin/sh
2 # Copyright 2007-2009 Roy Marples <roy@marples.name>
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}" -o -z "${named_options}" ] && exit 0
32 eval "$("${RESOLVCONF:-resolvconf}" -v)"
33
34 NEWOPTIONS="# Generated by resolvconf\n"
35 NEWZONES="${NEWOPTIONS}"
36 FORWARD=
37 for N in ${NAMESERVERS}; do
38         case "${FORWARD}" in
39         *"\n\t${N};"*);;
40         *) FORWARD="${FORWARD}\n\t${N};";;
41         esac
42 done
43 if [ -n "${FORWARD}" ]; then
44         NEWOPTIONS="${NEWOPTIONS}forward first;\nforwarders {${FORWARD}\n};\n"
45 fi
46
47 for D in ${DOMAINS}; do
48         NEWZONES="${NEWZONES}zone \"${D%%:*}\" {\n"
49         NEWZONES="${NEWZONES}\ttype forward;\n"
50         NEWZONES="${NEWZONES}\tforward first;\n\tforwarders {\n"
51         NS="${D#*:}"
52         while [ -n "${NS}" ]; do
53                 NEWZONES="${NEWZONES}\t\t${NS%%,*};\n"
54                 [ "${NS}" = "${NS#*,}" ] && break
55                 NS="${NS#*,}"
56         done
57         NEWZONES="${NEWZONES}\t};\n};\n"
58 done
59
60 # No point in changing files or reloading bind if the end result has not
61 # changed
62 RELOAD="no"
63 if [ -f "${named_options}" ]; then 
64         if [ "$(cat "${named_options}")" != "$(printf "${NEWOPTIONS}")" ]; then
65                 printf "${NEWOPTIONS}" > "${named_options}"
66                 RELOAD="yes"
67         fi
68 else
69         printf "${NEWOPTIONS}" > "${named_options}"
70         RELOAD="yes"
71 fi
72 if [ -f "${named_zones}" ]; then
73         if [ "$(cat "${named_zones}")" != "$(printf "${NEWZONES}")" ]; then
74                 printf "${NEWZONES}" > "${named_zones}"
75                 RELOAD="yes"
76         fi
77 else
78         printf "${NEWZONES}" > "${named_zones}"
79         RELOAD="yes"
80 fi
81
82 [ "${RELOAD}" = "yes" ] && resolvconf -s named restart
83 exit 0