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) :;;
112 *) newns="$newns${newns:+ }$n";;
115 echo "NAMESERVERS=\"$newns\""
116 echo "LOCALNAMESERVERS="
121 "# resolv.conf from "*)
123 iface="${line#\# resolv.conf from *}"
125 if [ -e "$PRIVATEDIR/$iface" ]; then
131 for p in $private_interfaces; do
132 if [ "$p" = "$iface" ]; then
142 127.*|0.0.0.0|255.255.255.255|::1)
143 echo "LOCALNAMESERVERS=\"\$LOCALNAMESERVERS ${line#* }\""
149 "domain "*|"search "*)
153 [ -n "$line" ] && continue
154 if [ -n "$ns" -a -n "$search" ]; then
157 newns="$newns${newns:+,}$n"
161 ds="$ds${ds:+ }$d:$newns"
163 echo "DOMAINS=\"\$DOMAINS $ds\""
165 echo "SEARCH=\"\$SEARCH $search\""
167 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
180 while [ -n "$1" ]; do
183 *) result="$result $1";;
192 [ -d "$IFACEDIR" ] || return 0
194 local report=false list= retval=0 cmd="$1"
197 # If we have an interface ordering list, then use that.
198 # It works by just using pathname expansion in the interface directory.
201 $force || report=true
204 for i in $interface_order; do
205 [ -e "$i" ] && list="$list $i"
207 for i in $dynamic_order; do
208 if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
212 if [ -d "$METRICDIR" ]; then
222 for i in $(uniqify $list); do
223 # Only list interfaces which we really have
224 if ! [ -e "$i" ]; then
226 echo "No resolv.conf for interface $i" >&2
227 retval=$(($retval + 1))
232 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
238 [ "$cmd" = i -o "$cmd" = "-i" ] && echo
244 eval "$(list_resolv -l "$@" | parse_resolv)"
246 # Ensure that we only list each domain once
248 for d in $DOMAINS; do
250 case " $newdomains" in
251 *" ${dn}:"*) continue;;
253 newdomains="$newdomains${newdomains:+ }$dn:"
255 for nd in $DOMAINS; do
256 if [ "$dn" = "${nd%%:*}" ]; then
258 while [ -n "$ns" ]; do
261 *) newns="$newns${newns:+,}${ns%%,*}";;
263 [ "$ns" = "${ns#*,}" ] && break
268 newdomains="$newdomains$newns"
270 echo "DOMAINS='$newdomains'"
271 echo "SEARCH='$(uniqify $SEARCH)'"
272 echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
273 echo "LOCALNAMESERVERS='$(uniqify $LOCALNAMESERVERS)'"
277 while getopts a:d:fhIilm:puv OPT; do
281 m) IF_METRIC="$OPTARG";;
284 *) cmd="$OPT"; iface="$OPTARG";;
287 shift $(($OPTIND - 1))
288 args="$iface${iface:+ }$@"
290 # -I inits the state dir
291 if [ "$cmd" = I ]; then
292 if [ -d "$VARDIR" ]; then
298 # -l lists our resolv files, optionally for a specific interface
299 if [ "$cmd" = l -o "$cmd" = i ]; then
300 list_resolv "$cmd" "$args"
304 # Not normally needed, but subscribers should be able to run independently
305 if [ "$cmd" = v ]; then
310 # Test that we have valid options
311 if [ "$cmd" = a -o "$cmd" = d ]; then
312 if [ -z "$iface" ]; then
313 usage "Interface not specified"
315 elif [ "$cmd" != u ]; then
316 [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
319 if [ "$cmd" = a ]; then
320 for x in '/' \\ ' ' '*'; do
322 *[$x]*) error_exit "$x not allowed in interface name";;
325 for x in '.' '-' '~'; do
328 "$x not allowed at start of interface name";;
331 [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
334 if [ ! -d "$IFACEDIR" ]; then
335 if [ ! -d "$VARDIR" ]; then
336 if [ -L "$VARDIR" ]; then
337 dir="$(readlink "$VARDIR")"
338 # link maybe relative
340 if ! mkdir -m 0755 -p "$dir"; then
341 error_exit "Failed to create needed" \
345 if ! mkdir -m 0755 -p "$VARDIR"; then
346 error_exit "Failed to create needed" \
351 mkdir -m 0755 -p "$IFACEDIR" || \
352 error_exit "Failed to create needed directory $IFACEDIR"
354 # Delete any existing information about the interface
355 if [ "$cmd" = d ]; then
358 if [ "$cmd" = d -a ! -e "$i" ]; then
360 error_exit "No resolv.conf for" \
363 rm -f "$i" "$METRICDIR/"*" $i" \
364 "$PRIVATEDIR/$i" || exit $?
369 if [ "$cmd" = a ]; then
370 # Read resolv.conf from stdin
372 # If what we are given matches what we have, then do nothing
373 if [ -e "$IFACEDIR/$iface" ]; then
374 if [ "$(echo "$resolv")" = \
375 "$(cat "$IFACEDIR/$iface")" ]
379 rm "$IFACEDIR/$iface"
381 echo "$resolv" >"$IFACEDIR/$iface" || exit $?
382 [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
383 rm -f "$METRICDIR/"*" $iface"
384 if [ -n "$IF_METRIC" ]; then
385 # Pad metric to 6 characters, so 5 is less than 10
386 while [ ${#IF_METRIC} -le 6 ]; do
387 IF_METRIC="0$IF_METRIC"
389 echo " " >"$METRICDIR/$IF_METRIC $iface"
391 case "$IF_PRIVATE" in
392 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
393 if [ ! -d "$PRIVATEDIR" ]; then
394 [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
397 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
400 if [ -e "$PRIVATEDIR/$iface" ]; then
401 rm -f "$PRIVATEDIR/$iface"
408 export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
409 : ${list_resolv:=list_resolv -l}
411 for script in "$LIBEXECDIR"/*; do
412 if [ -f "$script" ]; then
413 if [ -x "$script" ]; then
414 "$script" "$cmd" "$iface"
416 (. "$script" "$cmd" "$iface")
418 retval=$(($retval + $?))