2 # Copyright (c) 2007-2009 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 LIBEXECDIR=@LIBEXECDIR@
31 # Support original resolvconf configuration layout
32 # as well as the openresolv config file
33 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
34 . "$SYSCONFDIR"/resolvconf.conf
35 [ -n "$state_dir" ] && VARDIR="$state_dir"
36 elif [ -d "$SYSCONFDIR/resolvconf" ]; then
37 SYSCONFDIR="$SYSCONFDIR/resolvconf"
38 if [ -f "$SYSCONFDIR"/interface-order ]; then
39 interface_order="$(cat "$SYSCONFDIR"/interface-order)"
42 IFACEDIR="$VARDIR/interfaces"
43 METRICDIR="$VARDIR/metrics"
44 PRIVATEDIR="$VARDIR/private"
46 : ${dynamic_order:=tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]*}
47 : ${interface_order:=lo lo[0-9]*}
58 Usage: ${RESOLVCONF##*/} [options]
60 Inform the system about any DNS updates.
63 -a \$INTERFACE Add DNS information to the specified interface
64 (DNS supplied via stdin in resolv.conf format)
65 -m metric Give the added DNS information a metric
66 -p Mark the interface as private
67 -d \$INTERFACE Delete DNS information from the specified interface
68 -f Ignore non existant interfaces
70 -u Run updates from our current DNS information
71 -l [\$PATTERN] Show DNS information, optionally from interfaces
72 that match the specified pattern
73 -i [\$PATTERN] Show interfaces that have supplied DNS information
74 optionally from interfaces that match the specified
76 -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
78 -h Show this help cruft
88 [ -n "$1" -a -e "$IFACEDIR/$1" ] || return 1
89 echo "# resolv.conf from $1"
90 # Our variable maker works of the fact each resolv.conf per interface
91 # is separated by blank lines.
92 # So we remove them when echoing them.
94 [ -n "$line" ] && echo "$line"
99 # Parse resolv.conf's and make variables
100 # for domain name servers, search name servers and global nameservers
103 local line= ns= ds= search= d= n= newns=
104 local new=true iface= private=false
112 "# resolv.conf from "*)
114 iface="${line#\# resolv.conf from *}"
116 case " $private_interfaces " in
121 if [ -e "$PRIVATEDIR/$iface" ]; then
132 127.*|0.0.0.0|255.255.255.255) continue;;
136 "domain "*|"search "*)
140 [ -n "$line" ] && continue
141 if [ -n "$ns" -a -n "$search" ]; then
144 newns="$newns${newns:+,}$n"
148 ds="$ds${ds:+ }$d:$newns"
150 echo "DOMAINS=\"\$DOMAINS $ds\""
152 echo "SEARCH=\"\$SEARCH $search\""
154 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
167 while [ -n "$1" ]; do
170 *) result="$result $1";;
179 [ -d "$IFACEDIR" ] || return 0
181 local report=false list= retval=0 cmd="$1"
184 # If we have an interface ordering list, then use that.
185 # It works by just using pathname expansion in the interface directory.
188 $force || report=true
191 for i in $interface_order; do
192 [ -e "$i" ] && list="$list $i"
194 for i in $dynamic_order; do
195 if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
199 if [ -d "$METRICDIR" ]; then
209 for i in $(uniqify $list); do
210 # Only list interfaces which we really have
211 if ! [ -e "$i" ]; then
213 echo "No resolv.conf for interface $i" >&2
214 retval=$(($retval + 1))
219 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
225 [ "$cmd" = i -o "$cmd" = "-i" ] && echo
231 eval "$(list_resolv -l "$@" | parse_resolv)"
233 # Ensure that we only list each domain once
235 for d in $DOMAINS; do
237 case " $newdomains" in
238 *" ${dn}:"*) continue;;
240 newdomains="$newdomains${newdomains:+ }$dn:"
242 for nd in $DOMAINS; do
243 if [ "$dn" = "${nd%%:*}" ]; then
245 while [ -n "$ns" ]; do
248 *) newns="$newns${newns:+,}${ns%%,*}";;
250 [ "$ns" = "${ns#*,}" ] && break
255 newdomains="$newdomains$newns"
257 echo "DOMAINS='$newdomains'"
258 echo "SEARCH='$(uniqify $SEARCH)'"
259 echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
263 while getopts a:d:fhilm:puv OPT; do
267 m) IF_METRIC="$OPTARG";;
270 *) cmd="$OPT"; iface="$OPTARG";;
273 shift $(($OPTIND - 1))
274 args="$iface${iface:+ }$@"
276 # -I inits the state dir
277 if [ "$cmd" = I ]; then
278 if [ -d "$VARDIR" ]; then
284 # -l lists our resolv files, optionally for a specific interface
285 if [ "$cmd" = l -o "$cmd" = i ]; then
286 list_resolv "$cmd" "$args"
290 # Not normally needed, but subscribers should be able to run independently
291 if [ "$cmd" = v ]; then
296 # Test that we have valid options
297 if [ "$cmd" = a -o "$cmd" = d ]; then
298 if [ -z "$iface" ]; then
299 usage "Interface not specified"
301 elif [ "$cmd" != u ]; then
302 [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
305 if [ "$cmd" = a ]; then
306 for x in '/' \\ ' ' '*'; do
308 *[$x]*) error_exit "$x not allowed in interface name";;
311 for x in '.' '-' '~'; do
314 "$x not allowed at start of interface name";;
317 [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
320 if [ ! -d "$IFACEDIR" ]; then
321 if [ ! -d "$VARDIR" ]; then
322 if [ -L "$VARDIR" ]; then
323 dir="$(readlink "$VARDIR")"
324 # link maybe relative
326 if ! mkdir -m 0755 -p "$dir"; then
327 error_exit "Failed to create needed" \
331 if ! mkdir -m 0755 -p "$VARDIR"; then
332 error_exit "Failed to create needed" \
337 mkdir -m 0755 -p "$IFACEDIR" || \
338 error_exit "Failed to create needed directory $IFACEDIR"
340 # Delete any existing information about the interface
341 if [ "$cmd" = d ]; then
344 if [ "$cmd" = d -a ! -e "$i" ]; then
346 error_exit "No resolv.conf for" \
349 rm -f "$i" "$METRICDIR/"*" $i" \
350 "$PRIVATEDIR/$i" || exit $?
355 if [ "$cmd" = a ]; then
356 # Read resolv.conf from stdin
358 # If what we are given matches what we have, then do nothing
359 if [ -e "$IFACEDIR/$iface" ]; then
360 if [ "$(printf "$resolv")" = \
361 "$(cat "$IFACEDIR/$iface")" ]
365 rm "$IFACEDIR/$iface"
367 printf "$resolv" >"$IFACEDIR/$iface" || exit $?
368 [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
369 rm -f "$METRICDIR/"*" $iface"
370 if [ -n "$IF_METRIC" ]; then
371 # Pad metric to 6 characters, so 5 is less than 10
372 while [ ${#IF_METRIC} -le 6 ]; do
373 IF_METRIC="0$IF_METRIC"
375 echo " " >"$METRICDIR/$IF_METRIC $iface"
377 case "$IF_PRIVATE" in
378 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
379 if [ ! -d "$PRIVATEDIR" ]; then
380 [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
383 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
386 if [ -e "$PRIVATEDIR/$iface" ]; then
387 rm -f "$PRIVATEDIR/$iface"
394 : ${list_resolv:=list_resolv -l}
396 for script in "$LIBEXECDIR"/*; do
397 if [ -f "$script" ]; then
398 if [ -x "$script" ]; then
399 "$script" "$cmd" "$iface"
401 (. "$script" "$cmd" "$fiace")
403 retval=$(($retval + $?))