2 # Copyright 2006 Gentoo Foundation
3 # Copyright 2007 Roy Marples
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials provided
14 # with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 RESOLVCONF="${PREFIX}"/etc/resolvconf
32 UPDATED="${RESOLVCONF}"/update.d
33 VARDIR="${RESOLVCONF}"/run
34 IFACEDIR="${VARDIR}/interfaces"
43 Usage: ${argv0##*/} [options]
45 Inform the system about any DNS updates.
48 -a \$INTERFACE Add DNS information to the specified interface
49 (DNS supplied via stdin in resolv.conf format)
50 -d \$INTERFACE Delete DNS information from the specified interface
51 -u Run updates from our current DNS information
52 -l [\$PATTERN] Show DNS information, optionally from interfaces
53 that match the specified pattern
54 -i [\$PATTERN] Show interfaces that have supplied DNS information
55 optionally from interfaces that match the specified
57 -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
59 -h Show this help cruft
67 [ -n "$1" -a -e "${IFACEDIR}/$1" ] || return 1
68 echo "# resolv.conf for interface $1"
69 grep "." "${IFACEDIR}/$1"
78 *) result="${result} $1" ;;
94 # -l is a Gentoo option that lists our resolv files
95 # optionally for a specific interface
96 if [ "x${CMD}" = "x-l" -o "x${CMD}" = "x-i" ]; then
97 [ -d "${IFACEDIR}" ] || exit 0
99 # If we have an interface ordering list, then use that.
100 # It works by just using pathname expansion in the interface directory.
101 if [ -n "${IFACE}" ]; then
103 elif [ -r "${RESOLVCONF}"/interface-order ]; then
104 LIST="$(cat "${RESOLVCONF}"/interface-order)"
107 # If we don't have a list then prefer lo, tunnels, ppp
108 # and then anything else.
109 if [ -z "${LIST}" ]; then
110 LIST="lo lo[0-9]* tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]* *"
114 for IFACE in $(uniqify ${LIST}); do
115 # Only list interfaces which we really have
116 [ -e "${IFACE}" ] || continue
118 if [ "x${CMD}" = "x-i" ]; then
121 echo_resolv "${IFACE}"
124 [ "x${CMD}" = "x-i" ] && echo
128 if [ "x${CMD}" = "x-v" ]; then
135 LINES="$("${argv0}" -l "${IFACE}" | sed -e "s/'/'\\\\''/g" -e "s/^/'/g" -e "s/$/'/g")"
143 NS="${NS}${LINE#* } "
146 [ -z "${SEARCH}" ] && DOMAIN="${LINE#* }"
153 if [ -z "${LINE}" ]; then
155 if [ -n "${DOMAIN}" ]; then
156 NEWDOMAIN="${NEWDOMAIN} ${DOMAIN},${N}"
157 elif [ -n "${SEARCH}" ]; then
158 for S in ${SEARCH}; do
159 NEWSEARCH="${NEWSEARCH} ${S},${N}"
162 NEWNS="${NEWNS} ${N}"
173 # Prefer DOMAIN nameservers over SEARCH nameservers
174 # if we are supplied both.
175 NEWDOMAIN="$(uniqify ${NEWDOMAIN})"
176 NEWSEARCH="$(uniqify ${NEWSEARCH})"
177 NEWNS="$(uniqify ${NEWNS})"
178 for S in ${NEWSEARCH}; do
179 for DN in ${NEWDOMAIN}; do
180 if [ "${S%,*}" = "${DN%,*}" ]; then
181 NEWSEARCH="$(echo "${NEWSEARCH}" | sed -e "s/${S}/${DN}/g")"
182 NEWDOMAIN="$(echo "${NEWDOMAIN}" | sed -e "s/${DN}//g")"
188 echo "NEWDOMAIN='${NEWDOMAIN}'"
189 echo "NEWSEARCH='${NEWSEARCH}'"
190 echo "NEWNS='${NEWNS}'"
194 # Test that we have valid options
195 if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
196 if [ -z "${IFACE}" ]; then
197 usage "Interface not specified"
199 elif [ "x${CMD}" != "x-u" ]; then
200 [ -n "x${CMD}" -a "x${CMD}" != "x-h" ] && usage "Unknown option ${CMD}"
203 if [ "x${CMD}" = "x-a" ]; then
204 for x in '/' \\ ' ' '*'; do
206 *[${x}]*) error_exit "${x} not allowed in interface name" ;;
209 for x in '.' '-' '~'; do
211 [${x}]*) error_exit "${x} not allowed at start of interface name" ;;
214 [ "x${CMD}" = "x-a" -a -t 0 ] && error_exit "No file given via stdin"
215 IFACERESOLV="${IFACEDIR}/${IFACE}"
218 # Ensure that libdir exists
219 if [ ! -d "${IFACEDIR}" ]; then
220 if [ ! -d "${VARDIR}" ]; then
221 if [ -L "${VARDIR}" ]; then
222 DIR="$(readlink "${VARDIR}")"
223 # Change to /etc as link maybe relative
225 if ! mkdir -m 0755 -p "${DIR}"; then
226 error_exit "Failed to create needed directory ${DIR}"
229 if ! mkdir -m 0755 -p "${VARDIR}"; then
230 error_exit "Failed to create needed directory ${VARDIR}"
234 mkdir -m 0755 -p "${IFACEDIR}" || \
235 error_exit "Failed to create needed directory ${IFACEDIR}"
237 # Delete any existing information about the interface
238 if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
240 for iface in ${IFACE}; do
241 rm -f "${iface}" || exit $?
246 if [ "x${CMD}" = "x-a" ]; then
247 # Create our resolv.conf file
248 cat >"${IFACEDIR}"/"${IFACE}" || exit $?
252 for x in "${UPDATED}"/*; do
253 if [ -e "${x}" ]; then
254 "${x}" "${CMD}" "${IFACE}"
255 retval=$((${retval} + $?))