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.
28 SYSCONFDIR=@SYSCONFDIR@
29 VARDIR=@VARBASE@/run/resolvconf
30 IFACEDIR="${VARDIR}/interfaces"
41 Usage: ${ARGV0##*/} [options]
43 Inform the system about any DNS updates.
46 -a \$INTERFACE Add DNS information to the specified interface
47 (DNS supplied via stdin in resolv.conf format)
48 -d \$INTERFACE Delete DNS information from the specified interface
49 -f Ignore non existant interfaces
50 -u Run updates from our current DNS information
51 -l [\$PATTERN] Show DNS information, optionally from interfaces
52 that match the specified pattern
53 -i [\$PATTERN] Show interfaces that have supplied DNS information
54 optionally from interfaces that match the specified
56 -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
58 -s \$SVC \$CMD Do \$CMD for the system service \$SVC
59 -h Show this help cruft
68 [ -n "$1" -a -e "${IFACEDIR}/$1" ] || return 1
69 echo "# resolv.conf for interface $1"
74 # Parse resolv.conf's and make variables
75 # for domain name servers, search name servers and global nameservers
78 local LINE= NS= DOMAIN= SEARCH= N= NEWDOMAIN= NEWSEARCH= NEWNS=
93 [ -z "${SEARCH}" ] && DOMAIN="${LINE#* }"
100 if [ -z "${LINE}" ]; then
102 if [ -n "${DOMAIN}" ]; then
103 echo "NEWDOMAIN=\"\${NEWDOMAIN} ${DOMAIN},${N}\""
104 elif [ -n "${SEARCH}" ]; then
105 for S in ${SEARCH}; do
106 echo "NEWSEARCH=\"\${NEWSEARCH} ${S},${N}\""
109 echo "NEWNS=\"\${NEWNS} ${N}\""
124 while [ -n "$1" ]; do
125 case " ${result} " in
127 *) result="${result} $1";;
135 while getopts a:d:fhils:uv OPT; do
139 s) CMD=s; SERVICE="${OPTARG}";;
141 *) CMD="${OPT}"; IFACE="${OPTARG}";;
144 shift $((${OPTIND} - 1))
145 ARGS="${IFACE}${IFACE:+ }$@"
147 # We do our service restarting here so that our subscribers don't have to know
148 # about the OS's init system.
149 if [ "${CMD}" = "s" ]; then
154 [ -z "${ACTION}" ] && usage "Action not specified"
156 # If restarting check if service is running or not if we can
157 if [ "${ACTION}" = "restart" ]; then
158 if [ -s /var/run/"${SERVICE}".pid ]; then
159 kill -0 $(cat /var/run/"${SERVICE}".pid) 2>/dev/null
160 elif [ -s /var/run/"${SERVICE}"/"${SERVICE}".pid ]; then
161 kill -0 $(cat /var/run/"${SERVICE}"/"${SERVICE}".pid) 2>/dev/null
162 elif [ -s /var/run/"${SERVICE}"/pid ]; then
163 kill -0 $(cat /var/run/"${SERVICE}"/pid) 2>/dev/null
167 # Service not running, so don't restart
168 [ $? != 0 ] && exit 0
170 if [ -x /sbin/service ]; then
171 service "${SERVICE}" "${ACTION}" "$@"
172 elif [ -x /etc/init.d/"${SERVICE}" -a -x /sbin/runscript ]; then
173 if [ "${ACTION}" = "restart" ]; then
174 /etc/init.d/"${SERVICE}" --quiet --nodeps conditionalrestart "$@"
176 /etc/init.d/"${SERVICE}" --quiet --nodeps "${ACTION}" "$@"
178 elif [ -x /etc/init.d/"${SERVICE}" ]; then
179 /etc/init.d/"${SERVICE}" "${ACTION}" "$@"
180 elif [ -x /etc/rc.d/"${SERVICE}" ]; then
181 /etc/rc.d/"${SERVICE}" "${ACTION}" "$@"
182 elif [ -x /etc/rc.d/rc."${SERVICE}" ]; then
183 /etc/rc.d/rc."${SERVICE}" "${ACTION}" "$@"
185 error_exit "Don't know how to interact with services on this platform"
190 # -l lists our resolv files, optionally for a specific interface
191 if [ "${CMD}" = "l" -o "${CMD}" = "i" ]; then
192 [ -d "${IFACEDIR}" ] || exit 0
195 # If we have an interface ordering list, then use that.
196 # It works by just using pathname expansion in the interface directory.
197 if [ -n "${ARGS}" ]; then
199 ${FORCE} || REPORT=true
200 elif [ -r "${SYSCONFDIR}"/interface-order ]; then
201 LIST="$(cat "${SYSCONFDIR}"/interface-order)"
204 # If we don't have a list then prefer lo, tunnels, ppp
205 # and then anything else.
206 if [ -z "${LIST}" ]; then
207 LIST="lo lo[0-9]* tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]* *"
212 for IFACE in $(uniqify ${LIST}); do
213 # Only list interfaces which we really have
214 if ! [ -e "${IFACE}" ]; then
216 echo "No resolv.conf for interface ${IFACE}" >&2
217 RETVAL=$((${RETVAL} + 1))
222 if [ "${CMD}" = "i" ]; then
225 echo_resolv "${IFACE}"
228 [ "${CMD}" = "i" ] && echo
232 if [ "${CMD}" = "v" ]; then
233 eval "$("${ARGV0}" -l "${IFACE}" | parse_resolv)"
235 # Prefer DOMAIN nameservers over SEARCH nameservers
236 # if we are supplied both, but put them in the SEARCH list
237 NEWDOMAIN="$(uniqify ${NEWDOMAIN})"
238 NEWSEARCH="$(uniqify ${NEWSEARCH})"
239 NEWNS="$(uniqify ${NEWNS})"
242 for S in ${NEWSEARCH}; do
243 for DN in ${NEWDOMAIN}; do
244 if [ "${S%,*}" = "${DN%,*}" ]; then
249 _NEWSEARCH="${_NEWSEARCH}${_NEWSEARCH:+ }${S}"
251 for DN in ${NEWDOMAIN}; do
252 for S in ${_NEWSEARCH}; do
253 if [ "${S%,*}" = "${DN%,*}" ]; then
258 if [ -n "${DN}" ]; then
259 _NEWDOMAIN="${_NEWDOMAIN}${_NEWDOMAIN:+ }${DN}"
263 echo "NEWDOMAIN='${_NEWDOMAIN}'"
264 echo "NEWSEARCH='${_NEWSEARCH}'"
265 echo "NEWNS='${NEWNS}'"
269 # Test that we have valid options
270 if [ "${CMD}" = "a" -o "${CMD}" = "d" ]; then
271 if [ -z "${IFACE}" ]; then
272 usage "Interface not specified"
274 elif [ "${CMD}" != "u" ]; then
275 [ -n "${CMD}" -a "${CMD}" != "h" ] && usage "Unknown option ${CMD}"
278 if [ "${CMD}" = "a" ]; then
279 for x in '/' \\ ' ' '*'; do
281 *[${x}]*) error_exit "${x} not allowed in interface name";;
284 for x in '.' '-' '~'; do
286 [${x}]*) error_exit "${x} not allowed at start of interface name";;
289 [ "${CMD}" = "a" -a -t 0 ] && error_exit "No file given via stdin"
290 IFACERESOLV="${IFACEDIR}/${IFACE}"
293 # Ensure that libdir exists
294 if [ ! -d "${IFACEDIR}" ]; then
295 if [ ! -d "${VARDIR}" ]; then
296 if [ -L "${VARDIR}" ]; then
297 DIR="$(readlink "${VARDIR}")"
298 # Change to /etc as link maybe relative
300 if ! mkdir -m 0755 -p "${DIR}"; then
301 error_exit "Failed to create needed directory ${DIR}"
304 if ! mkdir -m 0755 -p "${VARDIR}"; then
305 error_exit "Failed to create needed directory ${VARDIR}"
309 mkdir -m 0755 -p "${IFACEDIR}" || \
310 error_exit "Failed to create needed directory ${IFACEDIR}"
312 # Delete any existing information about the interface
313 if [ "${CMD}" = "a" -o "${CMD}" = "d" ]; then
315 for ARG in ${ARGS}; do
316 if [ "${CMD}" = "d" -a ! -e "${ARG}" ]; then
318 error_exit "No resolv.conf for interface ${ARG}"
320 rm -f "${ARG}" || exit $?
325 if [ "${CMD}" = "a" ]; then
326 # Create our resolv.conf file
327 cat >"${IFACEDIR}"/"${IFACE}" || exit $?
331 for SCRIPT in "${SYSCONFDIR}"/update.d/*; do
332 if [ -e "${SCRIPT}" ]; then
333 "${SCRIPT}" "${CMD}" "${IFACE}"
334 RETVAL=$((${RETVAL} + $?))