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=
112 "# resolv.conf from "*)
114 iface="${line#\# resolv.conf from *}"
116 if [ -e "$PRIVATEDIR/$iface" ]; then
122 for p in $private_interfaces; do
123 if [ "$p" = "$iface" ]; then
133 127.*|0.0.0.0|255.255.255.255) continue;;
137 "domain "*|"search "*)
141 [ -n "$line" ] && continue
142 if [ -n "$ns" -a -n "$search" ]; then
145 newns="$newns${newns:+,}$n"
149 ds="$ds${ds:+ }$d:$newns"
151 echo "DOMAINS=\"\$DOMAINS $ds\""
153 echo "SEARCH=\"\$SEARCH $search\""
155 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
168 while [ -n "$1" ]; do
171 *) result="$result $1";;
180 [ -d "$IFACEDIR" ] || return 0
182 local report=false list= retval=0 cmd="$1"
185 # If we have an interface ordering list, then use that.
186 # It works by just using pathname expansion in the interface directory.
189 $force || report=true
192 for i in $interface_order; do
193 [ -e "$i" ] && list="$list $i"
195 for i in $dynamic_order; do
196 if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
200 if [ -d "$METRICDIR" ]; then
210 for i in $(uniqify $list); do
211 # Only list interfaces which we really have
212 if ! [ -e "$i" ]; then
214 echo "No resolv.conf for interface $i" >&2
215 retval=$(($retval + 1))
220 if [ "$cmd" = i -o "$cmd" = "-i" ]; then
226 [ "$cmd" = i -o "$cmd" = "-i" ] && echo
232 eval "$(list_resolv -l "$@" | parse_resolv)"
234 # Ensure that we only list each domain once
236 for d in $DOMAINS; do
238 case " $newdomains" in
239 *" ${dn}:"*) continue;;
241 newdomains="$newdomains${newdomains:+ }$dn:"
243 for nd in $DOMAINS; do
244 if [ "$dn" = "${nd%%:*}" ]; then
246 while [ -n "$ns" ]; do
249 *) newns="$newns${newns:+,}${ns%%,*}";;
251 [ "$ns" = "${ns#*,}" ] && break
256 newdomains="$newdomains$newns"
258 echo "DOMAINS='$newdomains'"
259 echo "SEARCH='$(uniqify $SEARCH)'"
260 echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
264 while getopts a:d:fhilm:puv OPT; do
268 m) IF_METRIC="$OPTARG";;
271 *) cmd="$OPT"; iface="$OPTARG";;
274 shift $(($OPTIND - 1))
275 args="$iface${iface:+ }$@"
277 # -I inits the state dir
278 if [ "$cmd" = I ]; then
279 if [ -d "$VARDIR" ]; then
285 # -l lists our resolv files, optionally for a specific interface
286 if [ "$cmd" = l -o "$cmd" = i ]; then
287 list_resolv "$cmd" "$args"
291 # Not normally needed, but subscribers should be able to run independently
292 if [ "$cmd" = v ]; then
297 # Test that we have valid options
298 if [ "$cmd" = a -o "$cmd" = d ]; then
299 if [ -z "$iface" ]; then
300 usage "Interface not specified"
302 elif [ "$cmd" != u ]; then
303 [ -n "$cmd" -a "$cmd" != h ] && usage "Unknown option $cmd"
306 if [ "$cmd" = a ]; then
307 for x in '/' \\ ' ' '*'; do
309 *[$x]*) error_exit "$x not allowed in interface name";;
312 for x in '.' '-' '~'; do
315 "$x not allowed at start of interface name";;
318 [ "$cmd" = a -a -t 0 ] && error_exit "No file given via stdin"
321 if [ ! -d "$IFACEDIR" ]; then
322 if [ ! -d "$VARDIR" ]; then
323 if [ -L "$VARDIR" ]; then
324 dir="$(readlink "$VARDIR")"
325 # link maybe relative
327 if ! mkdir -m 0755 -p "$dir"; then
328 error_exit "Failed to create needed" \
332 if ! mkdir -m 0755 -p "$VARDIR"; then
333 error_exit "Failed to create needed" \
338 mkdir -m 0755 -p "$IFACEDIR" || \
339 error_exit "Failed to create needed directory $IFACEDIR"
341 # Delete any existing information about the interface
342 if [ "$cmd" = d ]; then
345 if [ "$cmd" = d -a ! -e "$i" ]; then
347 error_exit "No resolv.conf for" \
350 rm -f "$i" "$METRICDIR/"*" $i" \
351 "$PRIVATEDIR/$i" || exit $?
356 if [ "$cmd" = a ]; then
357 # Read resolv.conf from stdin
359 # If what we are given matches what we have, then do nothing
360 if [ -e "$IFACEDIR/$iface" ]; then
361 if [ "$(printf "$resolv")" = \
362 "$(cat "$IFACEDIR/$iface")" ]
366 rm "$IFACEDIR/$iface"
368 printf "$resolv" >"$IFACEDIR/$iface" || exit $?
369 [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
370 rm -f "$METRICDIR/"*" $iface"
371 if [ -n "$IF_METRIC" ]; then
372 # Pad metric to 6 characters, so 5 is less than 10
373 while [ ${#IF_METRIC} -le 6 ]; do
374 IF_METRIC="0$IF_METRIC"
376 echo " " >"$METRICDIR/$IF_METRIC $iface"
378 case "$IF_PRIVATE" in
379 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
380 if [ ! -d "$PRIVATEDIR" ]; then
381 [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
384 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
387 if [ -e "$PRIVATEDIR/$iface" ]; then
388 rm -f "$PRIVATEDIR/$iface"
395 export RESOLVCONF DOMAINS SEARCH NAMESERVERS
396 : ${list_resolv:=list_resolv -l}
398 for script in "$LIBEXECDIR"/*; do
399 if [ -f "$script" ]; then
400 if [ -x "$script" ]; then
401 "$script" "$cmd" "$iface"
403 (. "$script" "$cmd" "$fiace")
405 retval=$(($retval + $?))