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 p=
107 echo "SEARCH=\"$search_domains\""
108 # let our subscribers know about global nameservers
109 for n in $name_servers; do
111 127.*|0.0.0.0|255.255.255.255|::1) continue;;
112 *) newns="$newns${newns:+ }$n"
115 echo "NAMESERVERS=\"$newns\""
120 "# resolv.conf from "*)
122 iface="${line#\# resolv.conf from *}"
124 if [ -e "$PRIVATEDIR/$iface" ]; then
130 for p in $private_interfaces; do
131 if [ "$p" = "$iface" ]; then
141 127.*|0.0.0.0|255.255.255.255|::1) continue;;
145 "domain "*|"search "*)
149 [ -n "$line" ] && continue
150 if [ -n "$ns" -a -n "$search" ]; then
153 newns="$newns${newns:+,}$n"
157 ds="$ds${ds:+ }$d:$newns"
159 echo "DOMAINS=\"\$DOMAINS $ds\""
161 echo "SEARCH=\"\$SEARCH $search\""
163 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
176 while [ -n "$1" ]; do
179 *) result="$result $1";;
188 [ -d "$IFACEDIR" ] || return 0
190 local report=false list= retval=0 cmd="$1"
193 # If we have an interface ordering list, then use that.
194 # It works by just using pathname expansion in the interface directory.
197 $force || report=true
200 for i in $interface_order; do
201 [ -e "$i" ] && list="$list $i"
203 for i in $dynamic_order; do
204 if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
208 if [ -d "$METRICDIR" ]; then
218 for i in $(uniqify $list); do
219 # Only list interfaces which we really have
220 if ! [ -e "$i" ]; then
222 echo "No resolv.conf for interface $i" >&2
223 retval=$(($retval + 1))
228 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
234 [ "$cmd" = i -o "$cmd" = "-i" ] && echo
240 eval "$(list_resolv -l "$@" | parse_resolv)"
242 # Ensure that we only list each domain once
244 for d in $DOMAINS; do
246 case " $newdomains" in
247 *" ${dn}:"*) continue;;
249 newdomains="$newdomains${newdomains:+ }$dn:"
251 for nd in $DOMAINS; do
252 if [ "$dn" = "${nd%%:*}" ]; then
254 while [ -n "$ns" ]; do
257 *) newns="$newns${newns:+,}${ns%%,*}";;
259 [ "$ns" = "${ns#*,}" ] && break
264 newdomains="$newdomains$newns"
266 echo "DOMAINS='$newdomains'"
267 echo "SEARCH='$(uniqify $SEARCH)'"
268 echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
272 while getopts a:d:fhilm:puv OPT; do
276 m) IF_METRIC="$OPTARG";;
279 *) cmd="$OPT"; iface="$OPTARG";;
282 shift $(($OPTIND - 1))
283 args="$iface${iface:+ }$@"
285 # -I inits the state dir
286 if [ "$cmd" = I ]; then
287 if [ -d "$VARDIR" ]; then
293 # -l lists our resolv files, optionally for a specific interface
294 if [ "$cmd" = l -o "$cmd" = i ]; then
295 list_resolv "$cmd" "$args"
299 # Not normally needed, but subscribers should be able to run independently
300 if [ "$cmd" = v ]; then
305 # Test that we have valid options
306 if [ "$cmd" = a -o "$cmd" = d ]; then
307 if [ -z "$iface" ]; then
308 usage "Interface not specified"
310 elif [ "$cmd" != u ]; then
311 [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
314 if [ "$cmd" = a ]; then
315 for x in '/' \\ ' ' '*'; do
317 *[$x]*) error_exit "$x not allowed in interface name";;
320 for x in '.' '-' '~'; do
323 "$x not allowed at start of interface name";;
326 [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
329 if [ ! -d "$IFACEDIR" ]; then
330 if [ ! -d "$VARDIR" ]; then
331 if [ -L "$VARDIR" ]; then
332 dir="$(readlink "$VARDIR")"
333 # link maybe relative
335 if ! mkdir -m 0755 -p "$dir"; then
336 error_exit "Failed to create needed" \
340 if ! mkdir -m 0755 -p "$VARDIR"; then
341 error_exit "Failed to create needed" \
346 mkdir -m 0755 -p "$IFACEDIR" || \
347 error_exit "Failed to create needed directory $IFACEDIR"
349 # Delete any existing information about the interface
350 if [ "$cmd" = d ]; then
353 if [ "$cmd" = d -a ! -e "$i" ]; then
355 error_exit "No resolv.conf for" \
358 rm -f "$i" "$METRICDIR/"*" $i" \
359 "$PRIVATEDIR/$i" || exit $?
364 if [ "$cmd" = a ]; then
365 # Read resolv.conf from stdin
367 # If what we are given matches what we have, then do nothing
368 if [ -e "$IFACEDIR/$iface" ]; then
369 if [ "$(printf "$resolv")" = \
370 "$(cat "$IFACEDIR/$iface")" ]
374 rm "$IFACEDIR/$iface"
376 printf "$resolv" >"$IFACEDIR/$iface" || exit $?
377 [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
378 rm -f "$METRICDIR/"*" $iface"
379 if [ -n "$IF_METRIC" ]; then
380 # Pad metric to 6 characters, so 5 is less than 10
381 while [ ${#IF_METRIC} -le 6 ]; do
382 IF_METRIC="0$IF_METRIC"
384 echo " " >"$METRICDIR/$IF_METRIC $iface"
386 case "$IF_PRIVATE" in
387 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
388 if [ ! -d "$PRIVATEDIR" ]; then
389 [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
392 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
395 if [ -e "$PRIVATEDIR/$iface" ]; then
396 rm -f "$PRIVATEDIR/$iface"
403 export RESOLVCONF DOMAINS SEARCH NAMESERVERS
404 : ${list_resolv:=list_resolv -l}
406 for script in "$LIBEXECDIR"/*; do
407 if [ -f "$script" ]; then
408 if [ -x "$script" ]; then
409 "$script" "$cmd" "$iface"
411 (. "$script" "$cmd" "$iface")
413 retval=$(($retval + $?))