2 # Copyright 2007-2008 Roy Marples
5 # libc subscriber for resolvconf
6 # You could symlink /etc/resolv.conf to resolvconf/run/resolv.conf
7 # for a read only etc, if resolvconf/run is linked to /var/run/resolvconf.
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 RESOLVCONF=/etc/resolv.conf
33 SYSCONFDIR=@SYSCONFDIR@
34 RESOLVCONFS="$(resolvconf -l)"
35 BASE="${SYSCONFDIR}/resolv.conf.d/base"
43 *) result="${result} $1";;
50 # sed may not be available, and this is faster on small files
53 local key="$1" value= x= line=
59 "${key}"*) echo "${line##${key}}";;
66 "${key}"*) echo "${line##${key}}";;
74 if [ -e "${BASE}" ]; then
75 OUR_NS="$(key_get_value "nameserver " "${BASE}")"
79 $(echo "${RESOLVCONFS}" | key_get_value "nameserver "))"
81 # libc only allows for 3 nameservers
82 # truncate after 127 as well
86 for N in ${OUR_NS}; do
89 [ "${i}" = "3" ] && break
91 127.*) LOCALH=true; break;;
96 # If we have a local nameserver then assume they are intelligent enough
97 # to be forwarding domain requests to the correct nameserver and not search
98 # ones. This means we prefer search then domain, otherwise, we use them in
99 # the order given to us.
102 if [ -e "${BASE}" ]; then
103 OUR_SEARCH="$(key_get_value "search " "${BASE}")"
105 RESS="$(echo "${RESOLVCONFS}" | key_get_value "search ")"
106 OUR_SEARCH="${OUR_SEARCH} ${RESS}"
107 if [ -e "${BASE}" ]; then
108 RESD="$(key_get_value "domain " "${BASE}")"
109 OUR_SEARCH="${OUR_SEARCH} ${RESD}"
111 RESD="$(echo "${RESOLVCONFS}" | key_get_value "domain ")"
112 OUR_SEARCH="${OUR_SEARCH} ${RESD}"
114 if [ -e "${BASE}" ]; then
115 RESS="$(key_get_value "search " "${BASE}")"
116 RESD="$(key_get_value "domain " "${BASE}")"
117 OUR_SEARCH="${RESS} ${RESD}"
119 RESS="$(echo "${RESOLVCONFS}" | key_get_value "search ")"
120 RESD="$(echo "${RESOLVCONFS}" | key_get_value "domain ")"
121 OUR_SEARCH="${OUR_SEARCH} ${RESS} ${RESD}"
124 # libc only allows for 6 search domains
127 for S in $(uniqify ${OUR_SEARCH}); do
129 SEARCH="${SEARCH} ${S}"
130 [ "${i}" = "6" ] && break
132 [ -n "${SEARCH}" ] && SEARCH="search${SEARCH}"
134 # Hold our new resolv.conf in a variable to save on temporary files
136 [ -e "${SYSCONFDIR}"/resolv.conf.d/head ] \
137 && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.d/head)\n"
138 [ -n "${SEARCH}" ] && NEWCONF="${NEWCONF}${SEARCH}\n"
140 NEWCONF="${NEWCONF}nameserver ${N}\n"
143 # Now get any configured options
145 if [ -e "${BASE}" ]; then
146 OPTS="$(key_get_value "options " "${BASE}")"
148 OPTS="${OPTS}${OPTS:+ }$(echo ${RESOLVCONFS} | key_get_value "options ")"
149 if [ -n "${OPTS}" ]; then
150 NEWCONF="${NEWCONF}options"
151 for OPT in $(uniqify ${OPTS}); do
152 NEWCONF="${NEWCONF} ${OPT}"
154 NEWCONF="${NEWCONF}\n"
157 [ -e "${SYSCONFDIR}"/resolv.conf.d/tail ] \
158 && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.d/tail)\n"
160 # Check if the file has actually changed or not
161 if [ -e "${RESOLVCONF}" ]; then
162 [ "$(cat "${RESOLVCONF}")" = "$(printf "${NEWCONF}")" ] && exit 0
165 # Create our resolv.conf now
166 (umask 022; printf "${NEWCONF}" > "${RESOLVCONF}")
168 resolvconf -s nscd restart
171 # Notify users of the resolver
172 for x in "${SYSCONFDIR}"/update-libc.d/*; do
173 if [ -e "${x}" ]; then
175 retval=$((${retval} + $?))