2 # Copyright (c) 2007-2020 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 OPENRESOLV_VERSION="3.12.0"
29 SYSCONFDIR=@SYSCONFDIR@
30 LIBEXECDIR=@LIBEXECDIR@
33 RESTARTCMD=@RESTARTCMD@
35 if [ "$1" = "--version" ]; then
36 echo "openresolv $OPENRESOLV_VERSION"
37 echo "Copyright (c) 2007-2020 Roy Marples"
41 # Disregard dhcpcd setting
42 unset interface_order state_dir
44 # If you change this, change the test in VFLAG and libc.in as well
45 local_nameservers="127.* 0.0.0.0 255.255.255.255 ::1"
47 dynamic_order="tap[0-9]* tun[0-9]* vpn vpn[0-9]* wg[0-9]* ppp[0-9]* ippp[0-9]*"
48 interface_order="lo lo[0-9]*"
49 name_server_blacklist="0.0.0.0"
51 # Support original resolvconf configuration layout
52 # as well as the openresolv config file
53 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
54 . "$SYSCONFDIR"/resolvconf.conf
55 [ -n "$state_dir" ] && VARDIR="$state_dir"
56 elif [ -d "$SYSCONFDIR/resolvconf" ]; then
57 SYSCONFDIR="$SYSCONFDIR/resolvconf"
58 if [ -f "$SYSCONFDIR"/interface-order ]; then
59 interface_order="$(cat "$SYSCONFDIR"/interface-order)"
63 IFACEDIR="$VARDIR/interfaces"
64 METRICDIR="$VARDIR/metrics"
65 PRIVATEDIR="$VARDIR/private"
66 EXCLUSIVEDIR="$VARDIR/exclusive"
67 DEPRECATEDDIR="$VARDIR/deprecated"
68 LOCKDIR="$VARDIR/lock"
85 Usage: ${RESOLVCONF##*/} [options] command [argument]
87 Inform the system about any DNS updates.
90 -a \$INTERFACE Add DNS information to the specified interface
91 (DNS supplied via stdin in resolv.conf format)
92 -C \$PATTERN Deprecate DNS information for matched interfaces
93 -c \$PATTERN Configure DNS information for matched interfaces
94 -d \$INTERFACE Delete DNS information from the specified interface
95 -h Show this help cruft
96 -i [\$PATTERN] Show interfaces that have supplied DNS information
97 optionally from interfaces that match the specified
99 -l [\$PATTERN] Show DNS information, optionally from interfaces
100 that match the specified pattern
102 -u Run updates from our current DNS information
103 --version Echo the ${RESOLVCONF##*/} version
106 -f Ignore non existent interfaces
107 -m metric Give the added DNS information a metric
108 -p Mark the interface as private
109 -x Mark the interface as exclusive
111 Subscriber and System Init Commands:
112 -I Init the state dir
113 -r \$SERVICE Restart the system service
114 (restarting a non-existent or non-running service
115 should have no output and return 0)
116 -R Show the system service restart command
117 -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
119 -V [\$PATTERN] Same as -v, but only uses configuration in
120 $SYSCONFDIR/resolvconf.conf
122 [ -z "$1" ] && exit 0
127 # Strip any trailing dot from each name as a FQDN does not belong
129 # If you think otherwise, capture a DNS trace and you'll see libc
130 # will strip it regardless.
131 # This also solves setting up duplicate zones in our subscribers.
132 # Also strip any comments denoted by #.
140 printf "%s%s" "$space${word%.}"
151 # Public interfaces override private ones.
152 for p in $public_interfaces; do
154 "$p"|"$p":*) return 1;;
158 if [ -e "$PRIVATEDIR/$iface" ]; then
162 for p in $private_interfaces; do
164 "$p"|"$p":*) return 0;;
168 # Not a private interface
172 # Parse resolv.conf's and make variables
173 # for domain name servers, search name servers and global nameservers
183 while read -r line; do
184 stripped_line="$(resolv_strip ${line#* })"
186 "# resolv.conf from "*)
188 iface="${line#\# resolv.conf from *}"
190 if private_iface "$iface"; then
199 for l in $local_nameservers; do
200 case "$stripped_line" in
208 echo "LOCALNAMESERVERS=\"\$LOCALNAMESERVERS $stripped_line\""
210 ns="$ns$stripped_line "
214 search="$stripped_line"
215 if [ -z "$domain" ]; then
217 echo "DOMAIN=\"$domain\""
221 search="$stripped_line"
224 [ -n "$line" ] && continue
225 if [ -n "$ns" ] && [ -n "$search" ]; then
228 newns="$newns${newns:+,}$n"
232 ds="$ds${ds:+ }$d:$newns"
234 echo "DOMAINS=\"\$DOMAINS $ds\""
236 echo "SEARCH=\"\$SEARCH $search\""
238 echo "NAMESERVERS=\"\$NAMESERVERS $ns\""
251 while [ -n "$1" ]; do
254 *) result="$result $1";;
272 while [ -n "$2" ]; do
282 [ -n "$f" ] || continue
284 if [ ! -d "$d" ]; then
285 mkdir -p "$d" || return $?
291 # With the advent of alternative init systems, it's possible to have
292 # more than one installed. So we need to try and guess what one we're
293 # using unless overriden by configure.
294 # Note that restarting a service is a last resort - the subscribers
295 # should make a reasonable attempt to reconfigre the service via some
296 # method, normally SIGHUP.
299 [ -n "$RESTARTCMD" ] && return 0
301 # Detect the running init system.
302 # As systemd and OpenRC can be installed on top of legacy init
303 # systems we try to detect them first.
306 if [ -x /bin/systemctl ] && [ -S /run/systemd/private ]; then
308 if /bin/systemctl --quiet is-active $1.service
310 /bin/systemctl restart $1.service
312 elif [ -x /usr/bin/systemctl ] && [ -S /run/systemd/private ]; then
314 if /usr/bin/systemctl --quiet is-active $1.service
316 /usr/bin/systemctl restart $1.service
318 elif [ -x /sbin/rc-service ] &&
319 { [ -s /libexec/rc/init.d/softlevel ] ||
320 [ -s /run/openrc/softlevel ]; }
322 RESTARTCMD='/sbin/rc-service -i $1 -- -Ds restart'
323 elif [ -x /usr/sbin/invoke-rc.d ]; then
326 if /usr/sbin/invoke-rc.d --quiet $1 status >/dev/null 2>&1
328 /usr/sbin/invoke-rc.d $1 restart
330 elif [ -x /sbin/service ]; then
334 if /sbin/service $1; then
335 /sbin/service $1 restart
337 elif [ -x /usr/sbin/service ]; then
340 if /usr/sbin/service \$1 $status >/dev/null 2>&1
342 /usr/sbin/service \$1 restart
344 elif [ -x /bin/sv ]; then
345 RESTARTCMD='/bin/sv status $1 >/dev/null 2>&1 &&
346 /bin/sv try-restart $1'
347 elif [ -x /usr/bin/sv ]; then
348 RESTARTCMD='/usr/bin/sv status $1 >/dev/null 2>&1 &&
349 /usr/bin/sv try-restart $1'
350 elif [ -e /etc/arch-release ] && [ -d /etc/rc.d ]; then
353 if [ -e /var/run/daemons/$1 ]
357 elif [ -e /etc/slackware-version ] && [ -d /etc/rc.d ]; then
359 if /etc/rc.d/rc.$1 status >/dev/null 2>&1
361 /etc/rc.d/rc.$1 restart
363 elif [ -e /etc/rc.d/rc.subr ] && [ -d /etc/rc.d ]; then
366 if /etc/rc.d/$1 check >/dev/null 2>&1
371 for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
372 [ -d $x ] || continue
374 if $x/\$1 $status >/dev/null 2>&1
382 if [ -z "$RESTARTCMD" ]; then
383 if [ "$_NOINIT_WARNED" != true ]; then
384 warn "could not detect a useable init system"
397 [ -n "$1" ] && [ -f "$IFACEDIR/$1" ] || return 1
398 echo "# resolv.conf from $1"
399 # Our variable maker works of the fact each resolv.conf per interface
400 # is separated by blank lines.
401 # So we remove them when echoing them.
402 while read -r line; do
404 if [ -n "$line" ]; then
405 # We need to set IFS here to preserve any whitespace
407 printf "%s\n" "$line"
409 done < "$IFACEDIR/$1"
413 deprecated_interface()
415 [ -d "$DEPRECATEDDIR" ] || return 1
420 [ -f "$daf" ] || continue
431 [ -d "$IFACEDIR" ] || return 0
440 case "$IF_EXCLUSIVE" in
441 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
443 if [ -d "$EXCLUSIVEDIR" ]; then
453 for i in $inclusive_interfaces; do
454 if [ -f "$i" ] && [ "$list" = "$i" ]; then
463 # If we have an interface ordering list, then use that.
464 # It works by just using pathname expansion in the interface directory.
467 $force || report=true
471 for i in $interface_order; do
472 [ -f "$i" ] && list="$list $i"
473 for ii in "$i":* "$i".*; do
474 [ -f "$ii" ] && list="$list $ii"
478 for i in $dynamic_order; do
479 if [ -e "$i" ] && ! [ -e "$METRICDIR/"*" $i" ]; then
482 for ii in "$i":* "$i".*; do
483 if [ -f "$ii" ] && ! [ -e "$METRICDIR/"*" $ii" ]
490 # Interfaces have an implicit metric of 0 if not specified.
492 if [ -f "$i" ] && ! [ -e "$METRICDIR/"*" $i" ]; then
497 if [ -d "$METRICDIR" ]; then
500 [ -f "$i" ] && list="$list ${i#* }"
504 # Move deprecated interfaces to the back
508 if deprecated_interface "$i"; then
509 deprecated="$deprecated $i"
514 list="$active $deprecated"
519 for i in $(uniqify $list); do
520 # Only list interfaces which we really have
521 if ! [ -f "$i" ]; then
523 echo "No resolv.conf for interface $i" >&2
529 if ! $ALLIFACES; then
530 if [ -n "$allow_interfaces" ]; then
532 for j in $allow_interfaces; do
533 if [ "$i" = "$j" ]; then
539 for j in $deny_interfaces; do
540 if [ "$i" = "$j" ]; then
546 if [ "$cmd" = i ] || [ "$cmd" = "-i" ]; then
549 echo_resolv "$i" && echo
551 [ $? = 0 ] && [ "$retval" = 1 ] && retval=0
553 [ "$cmd" = i ] || [ "$cmd" = "-i" ] && echo
559 [ -z "$2" ] && return 0
575 retval=$(($retval + 1))
587 echo "# Generated by resolvconf"
588 if [ -n "$search_domains" ]; then
589 echo "search $search_domains"
591 for n in $name_servers; do
599 echo "# Generated by resolvconf"
600 if [ -n "$search_domains_append" ]; then
601 echo "search $search_domains_append"
603 for n in $name_servers_append; do
611 while read -r keyword value; do
612 for r in $replace; do
627 for sub in $value; do
628 for r in $replace_sub; do
642 val="$val${val:+ }$sub"
644 printf "%s %s\n" "$keyword" "$val"
657 if [ -n "${name_servers}${search_domains}" ]; then
658 eval "$(echo_prepend | parse_resolv)"
660 if [ -z "$VFLAG" ]; then
662 list_resolv -i "$@" >/dev/null || IF_EXCLUSIVE=0
663 eval "$(list_resolv -l "$@" | replace | parse_resolv)"
665 if [ -n "${name_servers_append}${search_domains_append}" ]; then
666 eval "$(echo_append | parse_resolv)"
669 # Ensure that we only list each domain once
671 for d in $DOMAINS; do
673 list_remove domain_blacklist "$dn" >/dev/null || continue
674 case " $newdomains" in
675 *" ${dn}:"*) continue;;
678 for nd in $DOMAINS; do
679 if [ "$dn" = "${nd%%:*}" ]; then
681 while [ -n "$ns" ]; do
684 *) list_remove name_server_blacklist \
685 "${ns%%,*}" >/dev/null \
686 && newns="$newns${newns:+,}${ns%%,*}";;
688 [ "$ns" = "${ns#*,}" ] && break
693 if [ -n "$newns" ]; then
694 newdomains="$newdomains${newdomains:+ }$dn:$newns"
697 DOMAIN="$(list_remove domain_blacklist $DOMAIN)"
698 SEARCH="$(uniqify $SEARCH)"
699 SEARCH="$(list_remove domain_blacklist $SEARCH)"
700 NAMESERVERS="$(uniqify $NAMESERVERS)"
701 NAMESERVERS="$(list_remove name_server_blacklist $NAMESERVERS)"
702 LOCALNAMESERVERS="$(uniqify $LOCALNAMESERVERS)"
703 LOCALNAMESERVERS="$(list_remove name_server_blacklist $LOCALNAMESERVERS)"
704 echo "DOMAIN='$DOMAIN'"
705 echo "SEARCH='$SEARCH'"
706 echo "NAMESERVERS='$NAMESERVERS'"
707 echo "LOCALNAMESERVERS='$LOCALNAMESERVERS'"
708 echo "DOMAINS='$newdomains'"
713 while getopts a:C:c:Dd:fhIilm:pRruvVx OPT; do
717 m) IF_METRIC="$OPTARG";;
721 if [ "$local_nameservers" = \
722 "127.* 0.0.0.0 255.255.255.255 ::1" ]
729 *) cmd="$OPT"; iface="$OPTARG";;
732 shift $(($OPTIND - 1))
733 args="$iface${iface:+ }$*"
735 # -I inits the state dir
736 if [ "$cmd" = I ]; then
737 if [ -d "$VARDIR" ]; then
743 # -D ensures that the listed config file base dirs exist
744 if [ "$cmd" = D ]; then
749 # -l lists our resolv files, optionally for a specific interface
750 if [ "$cmd" = l ] || [ "$cmd" = i ]; then
752 list_resolv "$cmd" "$args"
757 # Restart a service or echo the command to restart a service
758 if [ "$cmd" = r ] || [ "$cmd" = R ]; then
759 detect_init || exit 1
760 if [ "$cmd" = r ]; then
765 sed -e '/^$/d' -e 's/^ //g'
770 # Not normally needed, but subscribers should be able to run independently
771 if [ "$cmd" = v ] || [ -n "$VFLAG" ]; then
776 # Test that we have valid options
779 if [ -z "$iface" ]; then
780 error_exit "Interface not specified"
785 if [ -n "$cmd" ] && [ "$cmd" != h ]; then
786 error_exit "Unknown option $cmd"
792 if [ "$cmd" = a ]; then
793 for x in '/' \\ ' ' '*'; do
795 *[$x]*) error_exit "$x not allowed in interface name";;
798 for x in '.' '-' '~'; do
801 "$x not allowed at start of interface name";;
804 [ "$cmd" = a ] && [ -t 0 ] && error_exit "No file given via stdin"
807 if [ ! -d "$VARDIR" ]; then
808 if [ -L "$VARDIR" ]; then
809 dir="$(readlink "$VARDIR")"
810 # link maybe relative
812 if ! mkdir -m 0755 -p "$dir"; then
813 error_exit "Failed to create needed" \
817 if ! mkdir -m 0755 -p "$VARDIR"; then
818 error_exit "Failed to create needed" \
824 if [ ! -d "$IFACEDIR" ]; then
825 mkdir -m 0755 -p "$IFACEDIR" || \
826 error_exit "Failed to create needed directory $IFACEDIR"
827 if [ "$cmd" = d ]; then
828 # Provide the same error messages as below
832 warn "No resolv.conf for interface $i"
840 # An interface was added, changed, deleted or a general update was called.
841 # Due to exclusivity we need to ensure that this is an atomic operation.
842 # Our subscribers *may* need this as well if the init system is sub par.
843 # As such we spinlock at this point as best we can.
844 # We don't use flock(1) because it's not widely available and normally resides
845 # in /usr which we do our very best to operate without.
846 [ -w "$VARDIR" ] || error_exit "Cannot write to $LOCKDIR"
847 : ${lock_timeout:=10}
852 if mkdir "$LOCKDIR" 2>/dev/null; then
853 trap 'rm -rf "$LOCKDIR";' EXIT
854 trap 'rm -rf "$LOCKDIR"; exit 1' INT QUIT ABRT SEGV ALRM TERM
855 echo $$ >"$LOCKDIR/pid"
858 pid=$(cat "$LOCKDIR/pid" 2>/dev/null)
859 if [ "$pid" -gt 0 ] 2>/dev/null; then
864 clear_nopids=$(($clear_nopids - 1))
865 if [ "$clear_nopids" -le 0 ]; then
866 warn "not seen a pid, clearing lock directory"
869 lock_timeout=$(($lock_timeout - 1))
874 if $have_pid && ! kill -0 "$pid"; then
875 warn "clearing stale lock pid $pid"
879 lock_timeout=$(($lock_timeout - 1))
880 if [ "$lock_timeout" -le 0 ]; then
882 error_exit "timed out waiting for lock from pid $pid"
885 error_exit "timed out waiting for lock" \
888 error_exit "timed out waiting for lock"
894 unset have_pid had_pid clear_nopids
898 # Read resolv.conf from stdin
902 # If what we are given matches what we have, then do nothing
903 if [ -e "$IFACEDIR/$iface" ]; then
904 if [ "$(echo "$resolv")" != \
905 "$(cat "$IFACEDIR/$iface")" ]
915 # Set metric and private before creating the interface resolv.conf file
916 # to ensure that it will have the correct flags
917 [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
918 oldmetric="$METRICDIR/"*" $iface"
920 if [ -n "$IF_METRIC" ]; then
921 # Pad metric to 6 characters, so 5 is less than 10
922 while [ ${#IF_METRIC} -le 6 ]; do
923 IF_METRIC="0$IF_METRIC"
925 newmetric="$METRICDIR/$IF_METRIC $iface"
927 rm -f "$METRICDIR/"*" $iface"
928 [ "$oldmetric" != "$newmetric" ] &&
929 [ "$oldmetric" != "$METRICDIR/* $iface" ] &&
931 [ -n "$newmetric" ] && echo " " >"$newmetric"
933 case "$IF_PRIVATE" in
934 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
935 if [ ! -d "$PRIVATEDIR" ]; then
936 [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
939 [ -e "$PRIVATEDIR/$iface" ] || changed=true
940 [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface"
943 if [ -e "$PRIVATEDIR/$iface" ]; then
944 rm -f "$PRIVATEDIR/$iface"
951 for x in "$EXCLUSIVEDIR/"*" $iface"; do
957 case "$IF_EXCLUSIVE" in
958 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
959 if [ ! -d "$EXCLUSIVEDIR" ]; then
960 [ -e "$EXCLUSIVEDIR" ] && rm "$EXCLUSIVEDIR"
961 mkdir "$EXCLUSIVEDIR"
967 if [ "${x#* }" != "$iface" ]; then
968 if [ "$x" = "${x% *}" ]; then
973 if [ "$x" = "0000000" ]; then
974 warn "exclusive underflow"
978 if [ -d "$EXCLUSIVEDIR" ]; then
979 echo " " >"$EXCLUSIVEDIR/$x $iface"
985 if [ -f "$oldexcl" ]; then
992 if $changedfile; then
993 printf "%s\n" "$resolv" >"$IFACEDIR/$iface" || exit $?
994 elif ! $changed; then
997 unset changed changedfile oldmetric newmetric x oldexcl
1001 # Delete any existing information about the interface
1005 if [ -e "$i" ]; then
1007 elif ! ${force}; then
1008 warn "No resolv.conf for interface $i"
1010 rm -f "$i" "$METRICDIR/"*" $i" \
1012 "$EXCLUSIVEDIR/"*" $i" || exit $?
1016 # Set the return code based on the forced flag
1024 # Mark interface as deprecated
1025 [ ! -d "$DEPRECATEDDIR" ] && mkdir "$DEPRECATEDDIR"
1029 if [ ! -e "$i" ]; then
1031 echo " " >"$i" || exit $?
1039 # Mark interface as active
1040 if [ -d "$DEPRECATEDDIR" ]; then
1044 if [ -e "$i" ]; then
1055 case "${resolvconf:-YES}" in
1056 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
1060 # Try and detect a suitable init system for our scripts
1062 export RESTARTCMD RCDIR _NOINIT_WARNED
1065 export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
1066 : ${list_resolv:=list_resolv -l}
1069 # Run scripts in the same directory resolvconf is run from
1070 # in case any scripts accidentally dump files in the wrong place.
1072 for script in "$LIBEXECDIR"/*; do
1073 if [ -f "$script" ]; then
1074 eval script_enabled="\$${script##*/}"
1075 case "${script_enabled:-YES}" in
1076 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
1079 if [ -x "$script" ]; then
1080 "$script" "$cmd" "$iface"
1082 (set -- "$cmd" "$iface"; . "$script")
1084 retval=$(($retval + $?))