2 # Copyright 2007 Roy Marples
5 # libc subscriber for resolvconf
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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.
30 RESOLVCONF="${PREFIX}"/etc/resolvconf
31 RESOLVCONFS="$(resolvconf -l)"
32 BASE="${RESOLVCONF}/resolv.conf.d/base"
39 *) result="${result} $1";;
47 if [ -e "${BASE}" ]; then
48 OUR_NS="$(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]*//p' "${BASE}")"
52 $(echo "${RESOLVCONFS}" \
53 | sed -n -e 's/^[[:space:]]*nameserver[[:space:]]*//p') \
56 # libc only allows for 3 nameservers
57 # truncate after 127 as well
61 for N in ${OUR_NS}; do
64 [ "${i}" = "3" ] && break
66 127.*) LOCALH=true; break;;
71 # If we have a local nameserver then assume they are intelligent enough
72 # to be forwarding domain requests to the correct nameserver and not search
73 # ones. This means we prefer search then domain, otherwise, we use them in
74 # the order given to us.
77 if [ -e "${BASE}" ]; then
78 OUR_SEARCH="$(sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' "${BASE}")"
80 OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONFS}" \
81 | sed -n 's/^[[:space:]]*search[[:space:]]*//p')"
82 if [ -e "${BASE}" ]; then
83 OUR_SEARCH="${OUR_SEARCH} $(sed -n -e 's/^[[:space:]]*domain[[:space:]]*//p' "${BASE}")"
85 OUR_SEARCH="${OUR_SEARCH} $( echo "${RESOLVCONFS}" \
86 | sed -n -e 's/^[[:space:]]*domain[[:space:]]*//p')"
88 if [ -e "${BASE}" ]; then
89 OUR_SEARCH="$(sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' \
90 -e 's/^[[:space:]]*domain[[:space:]]*//p' "${BASE}")"
92 OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONFS}" \
93 | sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' \
94 -e 's/^[[:space:]]*domain[[:space:]]*//p')"
97 # libc only allows for 6 search domains
100 for S in $(uniqify ${OUR_SEARCH}); do
102 SEARCH="${SEARCH} ${S}"
103 [ "${i}" = "6" ] && break
105 [ -n "${SEARCH}" ] && SEARCH="search${SEARCH}"
107 # Hold our new resolv.conf in a variable to save on temporary files
108 NEWCONF="# Generated by resolvconf\n"
109 [ -e "${RESOLVCONF}"/resolv.conf.d/head ] \
110 && NEWCONF="${NEWCONF}$(cat "${RESOLVCONF}"/resolv.conf.d/head)\n"
111 [ -n "${SEARCH}" ] && NEWCONF="${NEWCONF}${SEARCH}\n"
113 NEWCONF="${NEWCONF}nameserver ${N}\n"
116 # Now dump everything else from our resolvs
117 if [ -e "${BASE}" ]; then
118 NEWCONF="${NEWCONF}$(sed -e '/^[[:space:]]*$/d' \
119 -e '/^[[:space:]]*nameserver[[:space:]]*.*/d' \
120 -e '/^[[:space:]]*search[[:space:]]*.*/d' \
121 -e '/^[[:space:]]*domain[[:space:]]*.*/d' \
125 # We don't know we're using GNU sed, so we do it like this
126 NEWCONF="${NEWCONF}$(echo "${RESOLVCONFS}" | sed -e '/^[[:space:]]*$/d' \
127 -e '/^[[:space:]]*#/d' \
128 -e '/^[[:space:]]*nameserver[[:space:]]*.*/d' \
129 -e '/^[[:space:]]*search[[:space:]]*.*/d' \
130 -e '/^[[:space:]]*domain[[:space:]]*.*/d' \
132 [ -e "${RESOLVCONF}"/resolv.conf.d/tail ] \
133 && NEWCONF="${NEWCONF}$(cat "${RESOLVCONF}"/resolv.conf.d/tail)"
135 # Check if the file has actually changed or not
136 if [ -e "${RESOLVCONF}"/run/resolv.conf ]; then
137 [ "$(cat "${RESOLVCONF}"/run/resolv.conf)" = "$(printf "${NEWCONF}")" ] && exit 0
140 # Create our resolv.conf now
141 (umask 022; printf "${NEWCONF}" > "${RESOLVCONF}"/run/resolv.conf)
143 resolvconf -s nscd restart
145 # Notify users of the resolver
146 for x in "${REVOLVCONF}"/update-libc.d/*; do
147 [ -e "${x}" ] && "${x}" "$@"