2 # Copyright (c) 2007-2015 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@
32 # Disregard dhcpcd setting
33 unset interface_order state_dir
35 # If you change this, change the test in VFLAG and libc.in as well
36 local_nameservers
="127.* 0.0.0.0 255.255.255.255 ::1"
38 dynamic_order
="tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]*"
39 interface_order
="lo lo[0-9]*"
40 name_server_blacklist
="0.0.0.0"
42 # Support original resolvconf configuration layout
43 # as well as the openresolv config file
44 if [ -f "$SYSCONFDIR"/resolvconf.conf
]; then
45 .
"$SYSCONFDIR"/resolvconf.conf
46 [ -n "$state_dir" ] && VARDIR
="$state_dir"
47 elif [ -d "$SYSCONFDIR/resolvconf" ]; then
48 SYSCONFDIR
="$SYSCONFDIR/resolvconf"
49 if [ -f "$SYSCONFDIR"/interface-order
]; then
50 interface_order
="$(cat "$SYSCONFDIR"/interface-order)"
54 IFACEDIR
="$VARDIR/interfaces"
55 METRICDIR
="$VARDIR/metrics"
56 PRIVATEDIR
="$VARDIR/private"
57 EXCLUSIVEDIR
="$VARDIR/exclusive"
58 LOCKDIR
="$VARDIR/lock"
74 Usage: ${RESOLVCONF##*/} [options]
76 Inform the system about any DNS updates.
79 -a \$INTERFACE Add DNS information to the specified interface
80 (DNS supplied via stdin in resolv.conf format)
81 -m metric Give the added DNS information a metric
82 -p Mark the interface as private
83 -x Mark the interface as exclusive
84 -d \$INTERFACE Delete DNS information from the specified interface
85 -f Ignore non existant interfaces
87 -u Run updates from our current DNS information
88 -l [\$PATTERN] Show DNS information, optionally from interfaces
89 that match the specified pattern
90 -i [\$PATTERN] Show interfaces that have supplied DNS information
91 optionally from interfaces that match the specified
93 -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
95 -h Show this help cruft
104 local line
= OIFS
="$IFS"
106 [ -n "$1" -a -f "$IFACEDIR/$1" ] ||
return 1
107 echo "# resolv.conf from $1"
108 # Our variable maker works of the fact each resolv.conf per interface
109 # is separated by blank lines.
110 # So we remove them when echoing them.
111 while read -r line
; do
113 if [ -n "$line" ]; then
114 # We need to set IFS here to preserve any whitespace
116 printf "%s\n" "$line"
118 done < "$IFACEDIR/$1"
123 # Parse resolv.conf's and make variables
124 # for domain name servers, search name servers and global nameservers
127 local line
= ns
= ds
= search
= d
= n
= newns
=
128 local new
=true iface
= private
=false p
= domain
= l
= islocal
=
132 while read -r line
; do
134 "# resolv.conf from "*)
136 iface
="${line#\# resolv.conf from *}"
138 if [ -e "$PRIVATEDIR/$iface" ]; then
144 for p
in $private_interfaces; do
146 "$p"|
"$p":*) private
=true
; break;;
154 for l
in $local_nameservers; do
158 echo "LOCALNAMESERVERS=\"\$LOCALNAMESERVERS ${line#* }\""
163 $islocal || ns
="$ns${line#* } "
166 if [ -z "$domain" ]; then
168 echo "DOMAIN=\"$domain\""
176 [ -n "$line" ] && continue
177 if [ -n "$ns" -a -n "$search" ]; then
180 newns
="$newns${newns:+,}$n"
184 ds
="$ds${ds:+ }$d:$newns"
186 echo "DOMAINS=\"\$DOMAINS $ds\""
188 echo "SEARCH=\"\$SEARCH $search\""
190 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
203 while [ -n "$1" ]; do
206 *) result
="$result $1";;
215 local dir
= OIFS
="$IFS"
224 while [ -n "$2" ]; do
235 [ -n "$f" ] ||
continue
237 if [ ! -d "$d" ]; then
238 if type install >/dev
/null
2>&1; then
239 install -d "$d" || e
=$?
250 [ -d "$IFACEDIR" ] ||
return 0
252 local report
=false list
= retval
=0 cmd
="$1" excl
=
255 case "$IF_EXCLUSIVE" in
256 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1)
257 if [ -d "$EXCLUSIVEDIR" ]; then
273 # If we have an interface ordering list, then use that.
274 # It works by just using pathname expansion in the interface directory.
277 $force || report
=true
280 for i
in $interface_order; do
281 [ -f "$i" ] && list
="$list $i"
282 for ii
in "$i":* "$i".
*; do
283 [ -f "$ii" ] && list
="$list $ii"
286 for i
in $dynamic_order; do
287 if [ -e "$i" -a ! -e "$METRICDIR/"*" $i" ]; then
290 for ii
in "$i":* "$i".
*; do
291 if [ -f "$ii" -a ! -e "$METRICDIR/"*" $ii" ]; then
296 if [ -d "$METRICDIR" ]; then
299 [ -f "$i" ] && list
="$list ${i#* }"
307 for i
in $
(uniqify
$list); do
308 # Only list interfaces which we really have
309 if ! [ -f "$i" ]; then
311 echo "No resolv.conf for interface $i" >&2
317 if [ "$cmd" = i
-o "$cmd" = "-i" ]; then
322 [ $?
= 0 -a "$retval" = 1 ] && retval
=0
324 [ "$cmd" = i
-o "$cmd" = "-i" ] && echo
329 local list
= e
= l
= result
= found
= retval
=0
331 [ -z "$2" ] && return 0
345 retval
=$
(($retval + 1))
357 echo "# Generated by resolvconf"
358 if [ -n "$search_domains" ]; then
359 echo "search $search_domains"
361 for n
in $name_servers; do
369 echo "# Generated by resolvconf"
370 if [ -n "$search_domains_append" ]; then
371 echo "search $search_domains_append"
373 for n
in $name_servers_append; do
381 local r
= k
= f
= v
= val
= sub
=
383 while read -r keyword value
; do
384 for r
in $replace; do
399 for sub
in $value; do
400 for r
in $replace_sub; do
414 val
="$val${val:+ }$sub"
416 printf "%s %s\n" "$keyword" "$val"
422 local newdomains
= d
= dn
= newns
= ns
=
431 if [ -n "$name_servers" -o -n "$search_domains" ]; then
432 eval "$(echo_prepend | parse_resolv)"
434 if [ -z "$VFLAG" ]; then
436 list_resolv
-i "$@" >/dev
/null || IF_EXCLUSIVE
=0
437 eval "$(list_resolv -l "$@
" | replace | parse_resolv)"
439 if [ -n "$name_servers_append" -o -n "$search_domains_append" ]; then
440 eval "$(echo_append | parse_resolv)"
443 # Ensure that we only list each domain once
444 for d
in $DOMAINS; do
446 list_remove domain_blacklist
"$dn" >/dev
/null ||
continue
447 case " $newdomains" in
448 *" ${dn}:"*) continue;;
451 for nd
in $DOMAINS; do
452 if [ "$dn" = "${nd%%:*}" ]; then
454 while [ -n "$ns" ]; do
457 *) list_remove name_server_blacklist \
458 "${ns%%,*}" >/dev
/null \
459 && newns
="$newns${newns:+,}${ns%%,*}";;
461 [ "$ns" = "${ns#*,}" ] && break
466 if [ -n "$newns" ]; then
467 newdomains
="$newdomains${newdomains:+ }$dn:$newns"
470 DOMAIN
="$(list_remove domain_blacklist $DOMAIN)"
471 SEARCH
="$(uniqify $SEARCH)"
472 SEARCH
="$(list_remove domain_blacklist $SEARCH)"
473 NAMESERVERS
="$(uniqify $NAMESERVERS)"
474 NAMESERVERS
="$(list_remove name_server_blacklist $NAMESERVERS)"
475 LOCALNAMESERVERS
="$(uniqify $LOCALNAMESERVERS)"
476 LOCALNAMESERVERS
="$(list_remove name_server_blacklist $LOCALNAMESERVERS)"
477 echo "DOMAIN='$DOMAIN'"
478 echo "SEARCH='$SEARCH'"
479 echo "NAMESERVERS='$NAMESERVERS'"
480 echo "LOCALNAMESERVERS='$LOCALNAMESERVERS'"
481 echo "DOMAINS='$newdomains'"
486 while getopts a
:Dd
:fhIilm
:puvVx OPT
; do
490 m
) IF_METRIC
="$OPTARG";;
494 if [ "$local_nameservers" = \
495 "127.* 0.0.0.0 255.255.255.255 ::1" ]
502 *) cmd
="$OPT"; iface
="$OPTARG";;
505 shift $
(($OPTIND - 1))
506 args
="$iface${iface:+ }$*"
508 # -I inits the state dir
509 if [ "$cmd" = I
]; then
510 if [ -d "$VARDIR" ]; then
516 # -D ensures that the listed config file base dirs exist
517 if [ "$cmd" = D
]; then
522 # -l lists our resolv files, optionally for a specific interface
523 if [ "$cmd" = l
-o "$cmd" = i
]; then
524 list_resolv
"$cmd" "$args"
528 # Not normally needed, but subscribers should be able to run independently
529 if [ "$cmd" = v
-o -n "$VFLAG" ]; then
534 # Test that we have valid options
535 if [ "$cmd" = a
-o "$cmd" = d
]; then
536 if [ -z "$iface" ]; then
537 usage
"Interface not specified"
539 elif [ "$cmd" != u
]; then
540 [ -n "$cmd" -a "$cmd" != h
] && usage
"Unknown option $cmd"
544 if [ "$cmd" = a
]; then
545 for x
in '/' \\ ' ' '*'; do
547 *[$x]*) error_exit
"$x not allowed in interface name";;
550 for x
in '.' '-' '~'; do
553 "$x not allowed at start of interface name";;
556 [ "$cmd" = a
-a -t 0 ] && error_exit
"No file given via stdin"
559 if [ ! -d "$VARDIR" ]; then
560 if [ -L "$VARDIR" ]; then
561 dir
="$(readlink "$VARDIR")"
562 # link maybe relative
564 if ! mkdir
-m 0755 -p "$dir"; then
565 error_exit
"Failed to create needed" \
569 if ! mkdir
-m 0755 -p "$VARDIR"; then
570 error_exit
"Failed to create needed" \
576 if [ ! -d "$IFACEDIR" ]; then
577 mkdir
-m 0755 -p "$IFACEDIR" || \
578 error_exit
"Failed to create needed directory $IFACEDIR"
579 if [ "$cmd" = d
]; then
580 # Provide the same error messages as below
584 warn
"No resolv.conf for interface $i"
592 # An interface was added, changed, deleted or a general update was called.
593 # Due to exclusivity we need to ensure that this is an atomic operation.
594 # Our subscribers *may* need this as well if the init system is sub par.
595 # As such we spinlock at this point as best we can.
596 # We don't use flock(1) because it's not widely available and normally resides
597 # in /usr which we do our very best to operate without.
598 [ -w "$VARDIR" ] || error_exit
"Cannot write to $LOCKDIR"
599 : ${lock_timeout:=10}
601 if mkdir
"$LOCKDIR" 2>/dev
/null
; then
602 trap 'rm -rf "$LOCKDIR";' EXIT
603 trap 'rm -rf "$LOCKDIR"; exit 1' INT QUIT ABRT SEGV ALRM TERM
604 echo $$
>"$LOCKDIR/pid"
607 pid
=$
(cat "$LOCKDIR/pid")
608 if ! kill -0 "$pid"; then
609 warn
"clearing stale lock pid $pid"
613 lock_timeout
=$
(($lock_timeout - 1))
614 if [ "$lock_timeout" -le 0 ]; then
615 error_exit
"timed out waiting for lock from pid $pid"
622 # Read resolv.conf from stdin
626 # If what we are given matches what we have, then do nothing
627 if [ -e "$IFACEDIR/$iface" ]; then
628 if [ "$(echo "$resolv")" != \
629 "$(cat "$IFACEDIR/$iface")" ]
639 # Set metric and private before creating the interface resolv.conf file
640 # to ensure that it will have the correct flags
641 [ ! -d "$METRICDIR" ] && mkdir
"$METRICDIR"
642 oldmetric
="$METRICDIR/"*" $iface"
644 if [ -n "$IF_METRIC" ]; then
645 # Pad metric to 6 characters, so 5 is less than 10
646 while [ ${#IF_METRIC} -le 6 ]; do
647 IF_METRIC
="0$IF_METRIC"
649 newmetric
="$METRICDIR/$IF_METRIC $iface"
651 rm -f "$METRICDIR/"*" $iface"
652 [ "$oldmetric" != "$newmetric" -a \
653 "$oldmetric" != "$METRICDIR/* $iface" ] &&
655 [ -n "$newmetric" ] && echo " " >"$newmetric"
657 case "$IF_PRIVATE" in
658 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1)
659 if [ ! -d "$PRIVATEDIR" ]; then
660 [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
663 [ -e "$PRIVATEDIR/$iface" ] || changed
=true
664 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
667 if [ -e "$PRIVATEDIR/$iface" ]; then
668 rm -f "$PRIVATEDIR/$iface"
675 for x
in "$EXCLUSIVEDIR/"*" $iface"; do
681 case "$IF_EXCLUSIVE" in
682 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1)
683 if [ ! -d "$EXCLUSIVEDIR" ]; then
684 [ -e "$EXCLUSIVEDIR" ] && rm "$EXCLUSIVEDIR"
685 mkdir
"$EXCLUSIVEDIR"
691 if [ "${x#* }" != "$iface" ]; then
692 if [ "$x" = "${x% *}" ]; then
697 if [ "$x" = "0000000" ]; then
698 warn
"exclusive underflow"
702 if [ -d "$EXCLUSIVEDIR" ]; then
703 echo " " >"$EXCLUSIVEDIR/$x $iface"
709 if [ -f "$oldexcl" ]; then
716 if $changedfile; then
717 printf "%s\n" "$resolv" >"$IFACEDIR/$iface" ||
exit $?
718 elif ! $changed; then
721 unset changed changedfile oldmetric newmetric x oldexcl
725 # Delete any existing information about the interface
731 elif ! ${force}; then
732 warn
"No resolv.conf for interface $i"
734 rm -f "$i" "$METRICDIR/"*" $i" \
736 "$EXCLUSIVEDIR/"*" $i" ||
exit $?
738 if ! ${changed}; then
739 # Set the return code based on the forced flag
747 case "${resolvconf:-YES}" in
748 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1) ;;
753 export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
754 : ${list_resolv:=list_resolv -l}
756 for script in "$LIBEXECDIR"/*; do
757 if [ -f "$script" ]; then
758 eval script_enabled
="\$${script##*/}"
759 case "${script_enabled:-YES}" in
760 [Yy
][Ee
][Ss
]|
[Tt
][Rr
][Uu
][Ee
]|
[Oo
][Nn
]|
1) ;;
763 if [ -x "$script" ]; then
764 "$script" "$cmd" "$iface"
766 (set -- "$cmd" "$iface"; .
"$script")
768 retval
=$
(($retval + $?
))