2 # Copyright 2007-2008 Roy Marples
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 SYSCONFDIR=@SYSCONFDIR@
30 VARDIR=@VARBASE@/run/resolvconf
31 IFACEDIR="${VARDIR}/interfaces"
40 Usage: ${ARGV0##*/} [options]
42 Inform the system about any DNS updates.
45 -a \$INTERFACE Add DNS information to the specified interface
46 (DNS supplied via stdin in resolv.conf format)
47 -d \$INTERFACE Delete DNS information from the specified interface
48 -f Ignore non existant interfaces
49 -u Run updates from our current DNS information
50 -l [\$PATTERN] Show DNS information, optionally from interfaces
51 that match the specified pattern
52 -i [\$PATTERN] Show interfaces that have supplied DNS information
53 optionally from interfaces that match the specified
55 -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
57 -s \$SVC \$CMD Do \$CMD for the system service \$SVC
58 -h Show this help cruft
66 [ -n "$1" -a -e "${IFACEDIR}/$1" ] || return 1
67 echo "# resolv.conf for interface $1"
77 *) result="${result} $1";;
85 while getopts a:d:fhils:uv OPT; do
89 s) CMD=s; SERVICE="${OPTARG}";;
91 *) CMD="${OPT}"; IFACE="${OPTARG}";;
94 shift $((${OPTIND} - 1))
95 ARGS="${IFACE}${IFACE:+ }$@"
97 # We do our service restarting here so that our subscribers don't have to know
98 # about the OS's init system.
99 if [ "${CMD}" = "s" ]; then
104 [ -z "${ACTION}" ] && usage "Action not specified"
106 # If restarting check if service is running or not if we can
107 if [ "${ACTION}" = "restart" ]; then
108 if [ -s /var/run/"${SERVICE}".pid ]; then
109 kill -0 $(cat /var/run/"${SERVICE}".pid) 2>/dev/null
110 elif [ -s /var/run/"${SERVICE}"/"${SERVICE}".pid ]; then
111 kill -0 $(cat /var/run/"${SERVICE}".pid) 2>/dev/null
112 elif [ -s /var/run/"${SERVICE}"/pid ]; then
113 kill -0 $(cat /var/run/"${SERVICE}"/pid) 2>/dev/null
117 # Service not running, so don't restart
118 [ $? != 0 ] && exit 0
120 if [ -x /sbin/service ]; then
121 service "${SERVICE}" "${ACTION}" "$@"
122 elif [ -x /etc/init.d/"${SERVICE}" -a -x /sbin/runscript ]; then
123 if [ "${ACTION}" = "restart" ]; then
124 /etc/init.d/"${SERVICE}" --quiet --nodeps conditionalrestart "$@"
126 /etc/init.d/"${SERVICE}" --quiet --nodeps "${ACTION}" "$@"
128 elif [ -x /etc/init.d/"${SERVICE}" ]; then
129 /etc/init.d/"${SERVICE}" "${ACTION}" "$@"
130 elif [ -x /etc/rc.d/"${SERVICE}" ]; then
131 /etc/rc.d/"${SERVICE}" "${ACTION}" "$@"
132 elif [ -x /etc/rc.d/rc."${SERVICE}" ]; then
133 /etc/rc.d/rc."${SERVICE}" "${ACTION}" "$@"
135 error_exit "Don't know how to interact with services on this platform"
140 # -l lists our resolv files, optionally for a specific interface
141 if [ "${CMD}" = "l" -o "${CMD}" = "i" ]; then
142 [ -d "${IFACEDIR}" ] || exit 0
145 # If we have an interface ordering list, then use that.
146 # It works by just using pathname expansion in the interface directory.
147 if [ -n "${ARGS}" ]; then
149 ${FORCE} || REPORT=true
150 elif [ -r "${SYSCONFDIR}"/interface-order ]; then
151 LIST="$(cat "${SYSCONFDIR}"/interface-order)"
154 # If we don't have a list then prefer lo, tunnels, ppp
155 # and then anything else.
156 if [ -z "${LIST}" ]; then
157 LIST="lo lo[0-9]* tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]* *"
162 for IFACE in $(uniqify ${LIST}); do
163 # Only list interfaces which we really have
164 if ! [ -e "${IFACE}" ]; then
166 echo "No resolv.conf for interface ${IFACE}" >&2
167 RETVAL=$((${RETVAL} + 1))
172 if [ "${CMD}" = "i" ]; then
175 echo_resolv "${IFACE}"
178 [ "${CMD}" = "i" ] && echo
182 if [ "${CMD}" = "v" ]; then
189 LINES="$("${ARGV0}" -l "${IFACE}" |
190 sed -e "s/'/'\\\\''/g" -e "s/^/'/g" -e "s/$/'/g")"
198 NS="${NS}${LINE#* } "
201 [ -z "${SEARCH}" ] && DOMAIN="${LINE#* }"
208 if [ -z "${LINE}" ]; then
210 if [ -n "${DOMAIN}" ]; then
211 NEWDOMAIN="${NEWDOMAIN} ${DOMAIN},${N}"
212 elif [ -n "${SEARCH}" ]; then
213 for S in ${SEARCH}; do
214 NEWSEARCH="${NEWSEARCH} ${S},${N}"
217 NEWNS="${NEWNS} ${N}"
228 # Prefer DOMAIN nameservers over SEARCH nameservers
229 # if we are supplied both.
230 NEWDOMAIN="$(uniqify ${NEWDOMAIN})"
231 NEWSEARCH="$(uniqify ${NEWSEARCH})"
232 NEWNS="$(uniqify ${NEWNS})"
233 for S in ${NEWSEARCH}; do
234 for DN in ${NEWDOMAIN}; do
235 if [ "${S%,*}" = "${DN%,*}" ]; then
236 NEWSEARCH="$(echo "${NEWSEARCH}" |
237 sed -e "s/${S}/${DN}/g")"
238 NEWDOMAIN="$(echo "${NEWDOMAIN}" |
239 sed -e "s/${DN}//g")"
245 echo "NEWDOMAIN='${NEWDOMAIN}'"
246 echo "NEWSEARCH='${NEWSEARCH}'"
247 echo "NEWNS='${NEWNS}'"
251 # Test that we have valid options
252 if [ "${CMD}" = "a" -o "${CMD}" = "d" ]; then
253 if [ -z "${IFACE}" ]; then
254 usage "Interface not specified"
256 elif [ "${CMD}" != "u" ]; then
257 [ -n "${CMD}" -a "${CMD}" != "h" ] && usage "Unknown option ${CMD}"
260 if [ "${CMD}" = "a" ]; then
261 for x in '/' \\ ' ' '*'; do
263 *[${x}]*) error_exit "${x} not allowed in interface name";;
266 for x in '.' '-' '~'; do
268 [${x}]*) error_exit "${x} not allowed at start of interface name";;
271 [ "${CMD}" = "a" -a -t 0 ] && error_exit "No file given via stdin"
272 IFACERESOLV="${IFACEDIR}/${IFACE}"
275 # Ensure that libdir exists
276 if [ ! -d "${IFACEDIR}" ]; then
277 if [ ! -d "${VARDIR}" ]; then
278 if [ -L "${VARDIR}" ]; then
279 DIR="$(readlink "${VARDIR}")"
280 # Change to /etc as link maybe relative
282 if ! mkdir -m 0755 -p "${DIR}"; then
283 error_exit "Failed to create needed directory ${DIR}"
286 if ! mkdir -m 0755 -p "${VARDIR}"; then
287 error_exit "Failed to create needed directory ${VARDIR}"
291 mkdir -m 0755 -p "${IFACEDIR}" || \
292 error_exit "Failed to create needed directory ${IFACEDIR}"
294 # Delete any existing information about the interface
295 if [ "${CMD}" = "a" -o "${CMD}" = "d" ]; then
297 for ARG in ${ARGS}; do
298 if [ "${CMD}" = "d" -a ! -e "${ARG}" ]; then
300 error_exit "No resolv.conf for interface ${ARG}"
302 rm -f "${ARG}" || exit $?
307 if [ "${CMD}" = "a" ]; then
308 # Create our resolv.conf file
309 cat >"${IFACEDIR}"/"${IFACE}" || exit $?
313 for SCRIPT in "${SYSCONFDIR}"/update.d/*; do
314 if [ -e "${SCRIPT}" ]; then
315 "${SCRIPT}" "${CMD}" "${IFACE}"
316 RETVAL=$((${RETVAL} + $?))