2 # Copyright 2006 Gentoo Foundation
3 # Copyright 2007 Roy Marples
4 # Distributed under the terms of the GNU General Public License v2
6 # Written by Roy Marples (roy@marples.name)
7 # Based on Debian resolvconf by Thomas Hood
12 RESOLVCONF="${PREFIX}"/etc/resolvconf
13 UPDATED="${RESOLVCONF}"/update.d
14 VARDIR="${RESOLVCONF}"/run
15 IFACEDIR="${VARDIR}/interfaces"
24 Usage: ${argv0##*/} [options]
26 Inform the system about any DNS updates.
29 -a \$INTERFACE Add DNS information to the specified interface
30 (DNS supplied via stdin in resolv.conf format)
31 -d \$INTERFACE Delete DNS information from the specified interface
32 -u Run updates from our current DNS information
33 -l [\$PATTERN] Show DNS information, optionally from interfaces
34 that match the specified pattern
35 -i [\$PATTERN] Show interfaces that have supplied DNS information
36 optionally from interfaces that match the specified
38 -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
40 -h Show this help cruft
48 [ -n "$1" -a -e "${IFACEDIR}/$1" ] || return 1
49 echo "# resolv.conf for interface $1"
50 grep "." "${IFACEDIR}/$1"
59 *) result="${result} $1" ;;
75 # -l is a Gentoo option that lists our resolv files
76 # optionally for a specific interface
77 if [ "x${CMD}" = "x-l" -o "x${CMD}" = "x-i" ]; then
78 [ -d "${IFACEDIR}" ] || exit 0
80 # If we have an interface ordering list, then use that.
81 # It works by just using pathname expansion in the interface directory.
82 if [ -n "${IFACE}" ]; then
84 elif [ -r "${RESOLVCONF}"/interface-order ]; then
85 LIST="$(cat "${RESOLVCONF}"/interface-order)"
88 # If we don't have a list then prefer lo, tunnels, ppp
89 # and then anything else.
90 if [ -z "${LIST}" ]; then
91 LIST="lo lo[0-9]* tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]* *"
95 for IFACE in $(uniqify ${LIST}); do
96 # Only list interfaces which we really have
97 [ -e "${IFACE}" ] || continue
99 if [ "x${CMD}" = "x-i" ]; then
102 echo_resolv "${IFACE}"
105 [ "x${CMD}" = "x-i" ] && echo
109 if [ "x${CMD}" = "x-v" ]; then
116 LINES="$("${argv0}" -l "${IFACE}" | sed -e "s/'/'\\\\''/g" -e "s/^/'/g" -e "s/$/'/g")"
124 NS="${NS}${LINE#* } "
127 [ -z "${SEARCH}" ] && DOMAIN="${LINE#* }"
134 if [ -z "${LINE}" ]; then
136 if [ -n "${DOMAIN}" ]; then
137 NEWDOMAIN="${NEWDOMAIN} ${DOMAIN},${N}"
138 elif [ -n "${SEARCH}" ]; then
139 for S in ${SEARCH}; do
140 NEWSEARCH="${NEWSEARCH} ${S},${N}"
143 NEWNS="${NEWNS} ${N}"
154 # Prefer DOMAIN nameservers over SEARCH nameservers
155 # if we are supplied both.
156 NEWDOMAIN="$(uniqify ${NEWDOMAIN})"
157 NEWSEARCH="$(uniqify ${NEWSEARCH})"
158 NEWNS="$(uniqify ${NEWNS})"
159 for S in ${NEWSEARCH}; do
160 for DN in ${NEWDOMAIN}; do
161 if [ "${S%,*}" = "${DN%,*}" ]; then
162 NEWSEARCH="$(echo "${NEWSEARCH}" | sed -e "s/${S}/${DN}/g")"
163 NEWDOMAIN="$(echo "${NEWDOMAIN}" | sed -e "s/${DN}//g")"
169 echo "NEWDOMAIN='${NEWDOMAIN}'"
170 echo "NEWSEARCH='${NEWSEARCH}'"
171 echo "NEWNS='${NEWNS}'"
175 # Test that we have valid options
176 if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
177 if [ -z "${IFACE}" ]; then
178 usage "Interface not specified"
180 elif [ "x${CMD}" != "x-u" ]; then
181 [ -n "x${CMD}" -a "x${CMD}" != "x-h" ] && usage "Unknown option ${CMD}"
184 if [ "x${CMD}" = "x-a" ]; then
185 for x in '/' \\ ' ' '*'; do
187 *[${x}]*) error_exit "${x} not allowed in interface name" ;;
190 for x in '.' '-' '~'; do
192 [${x}]*) error_exit "${x} not allowed at start of interface name" ;;
195 [ "x${CMD}" = "x-a" -a -t 0 ] && error_exit "No file given via stdin"
196 IFACERESOLV="${IFACEDIR}/${IFACE}"
199 # Ensure that libdir exists
200 if [ ! -d "${IFACEDIR}" ]; then
201 if [ ! -d "${VARDIR}" ]; then
202 if [ -L "${VARDIR}" ]; then
203 DIR="$(readlink "${VARDIR}")"
204 # Change to /etc as link maybe relative
206 if ! mkdir -m 0755 -p "${DIR}"; then
207 error_exit "Failed to create needed directory ${DIR}"
210 if ! mkdir -m 0755 -p "${VARDIR}"; then
211 error_exit "Failed to create needed directory ${VARDIR}"
215 mkdir -m 0755 -p "${IFACEDIR}" || \
216 error_exit "Failed to create needed directory ${IFACEDIR}"
218 # Delete any existing information about the interface
219 if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
221 for iface in ${IFACE}; do
227 if [ "x${CMD}" = "x-a" ]; then
228 # Create our resolv.conf file
229 cat >"${IFACEDIR}"/"${IFACE}"
232 for x in "${UPDATED}"/*; do
233 [ -e "${x}" ] && "${x}" "${CMD}" "${IFACE}"