Home | History | Annotate | Line # | Download | only in dist
      1 #!/bin/sh
      2 # Copyright (c) 2007-2025 Roy Marples
      3 # All rights reserved
      4 
      5 # Redistribution and use in source and binary forms, with or without
      6 # modification, are permitted provided that the following conditions
      7 # are met:
      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.
     14 #
     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.
     26 
     27 RESOLVCONF="$0"
     28 OPENRESOLV_VERSION="3.17.4"
     29 SYSCONFDIR=@SYSCONFDIR@
     30 LIBEXECDIR=@LIBEXECDIR@
     31 VARDIR=@VARDIR@
     32 RCDIR=@RCDIR@
     33 RESTARTCMD=@RESTARTCMD@
     34 
     35 if [ "$1" = "--version" ]; then
     36 	echo "openresolv $OPENRESOLV_VERSION"
     37 	echo "Copyright (c) 2007-2025 Roy Marples"
     38 	exit 0
     39 fi
     40 
     41 # Disregard dhcpcd setting
     42 unset interface_order state_dir
     43 
     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"
     46 
     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"
     50 
     51 # Poor mans cat
     52 # /usr might not be available
     53 cat()
     54 {
     55 	OIFS="$IFS"
     56 	IFS=''
     57 	if [ -n "$1" ]; then
     58 		while read -r line; do
     59 			printf "%s\n" "$line"
     60 		done < "$1"
     61 	else
     62 		while read -r line; do
     63 			printf "%s\n" "$line"
     64 		done
     65 	fi
     66 	retval=$?
     67 	IFS="$OIFS"
     68 	return $retval
     69 }
     70 
     71 
     72 # Support original resolvconf configuration layout
     73 # as well as the openresolv config file
     74 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
     75 	. "$SYSCONFDIR"/resolvconf.conf
     76 	[ -n "$state_dir" ] && VARDIR="$state_dir"
     77 elif [ -d "$SYSCONFDIR/resolvconf" ]; then
     78 	SYSCONFDIR="$SYSCONFDIR/resolvconf"
     79 	if [ -f "$SYSCONFDIR"/interface-order ]; then
     80 		interface_order="$(cat "$SYSCONFDIR"/interface-order)"
     81 	fi
     82 fi
     83 
     84 KEYDIR="$VARDIR/keys"
     85 METRICDIR="$VARDIR/metrics"
     86 PRIVATEDIR="$VARDIR/private"
     87 NOSEARCHDIR="$VARDIR/nosearch"
     88 EXCLUSIVEDIR="$VARDIR/exclusive"
     89 DEPRECATEDDIR="$VARDIR/deprecated"
     90 LOCKDIR="$VARDIR/lock"
     91 _PWD="$PWD"
     92 
     93 # Compat
     94 if [ ! -d "$KEYDIR" ] && [ -d "$VARDIR/interfaces" ]; then
     95 	KEYDIR="$VARDIR/interfaces"
     96 fi
     97 : ${allow_keys:="$allow_interfaces"}
     98 : ${deny_keys:="$deny_interfaces"}
     99 : ${key_order:="$interface_order"}
    100 : ${inclusive_keys:="$inclusive_interfaces"}
    101 : ${exclusive_keys:="$exclusive_interfaces"}
    102 : ${private_keys:="$private_interfaces"}
    103 : ${public_keys:="$public_interfaces"}
    104 
    105 warn()
    106 {
    107 	echo "${RESOLVCONF##*/}: $*" >&2
    108 }
    109 
    110 error_exit()
    111 {
    112 	warn "$*"
    113 	exit 1
    114 }
    115 
    116 usage()
    117 {
    118 	cat <<-EOF
    119 	Usage: ${RESOLVCONF##*/} [options] command [argument]
    120 
    121 	Inform the system about any DNS updates.
    122 
    123 	Commands:
    124 	  -a \$KEY          Add DNS information to the specified key
    125 	                   (DNS supplied via stdin in resolv.conf format)
    126 	  -C \$PATTERN      Deprecate DNS information for matched key
    127 	  -c \$PATTERN      Configure DNS information for matched key
    128 	  -d \$PATTERN      Delete DNS information from the matched key
    129 	  -h               Show this help cruft
    130 	  -i [\$PATTERN]    Show keys that have supplied DNS information
    131                    optionally from keys that match the specified
    132                    pattern
    133 	  -l [\$PATTERN]    Show DNS information, optionally from keys
    134 	                   that match the specified pattern
    135 	  -L [\$PATTERN]    Same as -l, but adjusted by our config
    136 
    137 	  -u               Run updates from our current DNS information
    138 	  --version        Echo the ${RESOLVCONF##*/} version
    139 
    140 	Options:
    141 	  -f               Ignore non existent keys
    142 	  -m metric        Give the added DNS information a metric
    143 	  -p               Mark the resolv.conf as private
    144 	  -x               Mark the resolv.conf as exclusive
    145 
    146 	Subscriber and System Init Commands:
    147 	  -I               Init the state dir
    148 	  -r \$SERVICE      Restart the system service
    149 	                   (restarting a non-existent or non-running service
    150 	                    should have no output and return 0)
    151 	  -R               Show the system service restart command
    152 	  -v [\$PATTERN]    echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
    153 	  		   the console
    154 	  -V [\$PATTERN]    Same as -v, but only uses configuration in
    155 	                   $SYSCONFDIR/resolvconf.conf
    156 	EOF
    157 	[ -z "$1" ] && exit 0
    158 	echo
    159 	error_exit "$@"
    160 }
    161 
    162 public_key() {
    163 	key="$1"
    164 
    165 	# Allow expansion
    166 	cd "$KEYDIR"
    167 
    168 	# Public keys override private ones.
    169 	for p in $public_keys; do
    170 		case "$key" in
    171 		"$p"|"$p":*) return 0;;
    172 		esac
    173 	done
    174 
    175 	return 1
    176 }
    177 
    178 private_key()
    179 {
    180 	key="$1"
    181 
    182 	if public_key "$key"; then
    183 	       	return 1
    184 	fi
    185 
    186 	if [ -e "$PRIVATEDIR/$key" ]; then
    187 		return 0
    188 	fi
    189 
    190 	for p in $private_keys; do
    191 		case "$key" in
    192 		"$p"|"$p":*) return 0;;
    193 		esac
    194 	done
    195 
    196 	# Not a private key
    197 	return 1
    198 }
    199 
    200 nosearch_key()
    201 {
    202 	key="$1"
    203 
    204 	if public_key "$key"; then
    205 	       	return 1
    206 	fi
    207 
    208 	if [ -e "$NOSEARCHDIR/$key" ]; then
    209 		return 0
    210 	fi
    211 
    212 	for p in $nosearch_keys; do
    213 		case "$key" in
    214 		"$p"|"$p":*) return 0;;
    215 		esac
    216 	done
    217 
    218 	# Not a non searchable key
    219 	return 1
    220 }
    221 
    222 exclusive_key()
    223 {
    224 	key="$1"
    225 
    226 	for x in "$EXCLUSIVEDIR/"*" $key"; do
    227 		if [ -f "$x" ]; then
    228 			return 0
    229 		fi
    230 	done
    231 
    232 	# Not an exclusive key
    233 	return 1
    234 }
    235 
    236 # Quote input so it can be safely used for variable assignment via eval
    237 quote()
    238 {
    239 	if [ -z "$1" ]; then
    240 		R="''"
    241 	else
    242 		R=
    243 		for W; do
    244 			while [ -n "$W" ]; do
    245 				case "$W" in
    246 				\'*)  R="$R\\'"; W=${W#?};;
    247 				?\'*) R="$R\\${W%%\'*}"; W="${W#?}";;
    248 				*\'*) R="$R'${W%%\'*}'"; W="'${W#*\'}";;
    249 				?)    R="$R\\$W"; W=;;
    250 				*)    R="$R'$W'"; W=;;
    251 				esac
    252 			done
    253 		done
    254 	fi
    255 
    256 	printf '%s\n' "$R"
    257 	return 0
    258 }
    259 
    260 # Parse resolv.conf's and make variables
    261 # for domain name servers, search name servers and global nameservers
    262 # Important! Each printf here should use the above quote function
    263 # to ensure that user input is quoted for eval.
    264 parse_resolv()
    265 {
    266 	domain=
    267 	new=true
    268 	newns=
    269 	ns=
    270 	private=false
    271 	nosearch=false
    272 	search=
    273 
    274 	while read -r line; do
    275 		value="${line#* }"
    276 		case "$line" in
    277 		"# resolv.conf from "*)
    278 			if ${new}; then
    279 				key="${line#\# resolv.conf from *}"
    280 				new=false
    281 				if nosearch_key "$key"; then
    282 					private=true
    283 					nosearch=true
    284 				elif private_key "$key"; then
    285 					private=true
    286 					nosearch=false
    287 				else
    288 					private=false
    289 					nosearch=false
    290 				fi
    291 			fi
    292 			;;
    293 		"nameserver "*)
    294 			islocal=false
    295 			for l in $local_nameservers; do
    296 				case "$value" in
    297 				$l)
    298 					islocal=true
    299 					break
    300 					;;
    301 				esac
    302 			done
    303 			if $islocal; then
    304 				printf 'LOCALNAMESERVERS="$LOCALNAMESERVERS "%s\n' "$(quote "$value")"
    305 			else
    306 				ns="$ns${ns:+ }$value"
    307 			fi
    308 			;;
    309 		"domain "*)
    310 			search="$value"
    311 			if [ -z "$domain" ]; then
    312 				domain="$search"
    313 				if ! $nosearch; then
    314 					printf 'DOMAIN=%s\n' "$(quote "$domain")"
    315 				fi
    316 			fi
    317 			;;
    318 		"search "*)
    319 			search="$value"
    320 			;;
    321 		*)
    322 			[ -n "$line" ] && continue
    323 			if [ -n "$ns" ] && [ -n "$search" ]; then
    324 				newns=
    325 				for n in $ns; do
    326 					newns="$newns${newns:+,}$n"
    327 				done
    328 				ds=
    329 				for d in $search; do
    330 					ds="$ds${ds:+ }$d:$newns"
    331 				done
    332 				printf 'DOMAINS="$DOMAINS "%s\n' "$(quote "$ds")"
    333 			fi
    334 			if ! $nosearch; then
    335 				printf 'SEARCH="$SEARCH "%s\n' "$(quote "$search")"
    336 			fi
    337 			if ! $private; then
    338 				printf 'NAMESERVERS="$NAMESERVERS "%s\n' "$(quote "$ns")"
    339 			fi
    340 			ns=
    341 			search=
    342 			new=true
    343 			;;
    344 		esac
    345 	done
    346 }
    347 
    348 uniqify()
    349 {
    350 	result=
    351 	while [ -n "$1" ]; do
    352 		case " $result " in
    353 		*" $1 "*);;
    354 		*) result="$result $1";;
    355 		esac
    356 		shift
    357 	done
    358 	echo "${result# *}"
    359 }
    360 
    361 dirname()
    362 {
    363 	OIFS="$IFS"
    364 	IFS=/
    365 	set -- $@
    366 	IFS="$OIFS"
    367 	if [ -n "$1" ]; then
    368 		printf %s .
    369 	else
    370 		shift
    371 	fi
    372 	while [ -n "$2" ]; do
    373 		printf "/%s" "$1"
    374 		shift
    375 	done
    376 	printf "\n"
    377 }
    378 
    379 config_mkdirs()
    380 {
    381 	for f; do
    382 		[ -n "$f" ] || continue
    383 		d="$(dirname "$f")"
    384 		if [ ! -d "$d" ]; then
    385 			mkdir -p "$d" || return $?
    386 		fi
    387 	done
    388 	return 0
    389 }
    390 
    391 # With the advent of alternative init systems, it's possible to have
    392 # more than one installed. So we need to try and guess what one we're
    393 # using unless overridden by configure.
    394 # Note that restarting a service is a last resort - the subscribers
    395 # should make a reasonable attempt to reconfigure the service via some
    396 # method, normally SIGHUP.
    397 detect_init()
    398 {
    399 	[ -n "$RESTARTCMD" ] && return 0
    400 
    401 	# Detect the running init system.
    402 	# As systemd and OpenRC can be installed on top of legacy init
    403 	# systems we try to detect them first.
    404 	status="@STATUSARG@"
    405 	: ${status:=status}
    406 	if [ -x /bin/systemctl ] && [ -S /run/systemd/private ]; then
    407 		RESTARTCMD='
    408 			if /bin/systemctl --quiet is-active $1.service
    409 			then
    410 				/bin/systemctl restart $1.service
    411 			fi'
    412 	elif [ -x /usr/bin/systemctl ] && [ -S /run/systemd/private ]; then
    413 		RESTARTCMD='
    414 			if /usr/bin/systemctl --quiet is-active $1.service
    415 			then
    416 				/usr/bin/systemctl restart $1.service
    417 			fi'
    418 	elif [ -x /sbin/rc-service ] &&
    419 	     { [ -s /libexec/rc/init.d/softlevel ] ||
    420 	     [ -s /run/openrc/softlevel ]; }
    421 	then
    422 		RESTARTCMD='/sbin/rc-service -i $1 -- -Ds restart'
    423 	elif [ -x /usr/sbin/invoke-rc.d ]; then
    424 		RCDIR=/etc/init.d
    425 		RESTARTCMD='
    426 		   if /usr/sbin/invoke-rc.d --quiet $1 status >/dev/null 2>&1
    427 		   then
    428 			/usr/sbin/invoke-rc.d $1 restart
    429 		   fi'
    430 	elif [ -x /usr/bin/s6-rc ] && [ -x /usr/bin/s6-svc ]; then
    431 		RESTARTCMD='
    432 		   if s6-rc -a list 2>/dev/null | grep -qFx $1-srv
    433 		   then
    434 			s6-svc -r /run/service/$1-srv
    435 		   fi'
    436 	elif [ -x /sbin/service ]; then
    437 		# Old RedHat
    438 		RCDIR=/etc/init.d
    439 		RESTARTCMD='
    440 			if /sbin/service $1; then
    441 				/sbin/service $1 restart
    442 			fi'
    443 	elif [ -x /usr/sbin/service ]; then
    444 		# Could be FreeBSD
    445 		RESTARTCMD="
    446 			if /usr/sbin/service \$1 $status >/dev/null 2>&1
    447 			then
    448 				/usr/sbin/service \$1 restart
    449 			fi"
    450 	elif [ -x /bin/sv ]; then
    451 		RESTARTCMD='/bin/sv status $1 >/dev/null 2>&1 &&
    452 			    /bin/sv try-restart $1'
    453 	elif [ -x /usr/bin/sv ]; then
    454 		RESTARTCMD='/usr/bin/sv status $1 >/dev/null 2>&1 &&
    455 			    /usr/bin/sv try-restart $1'
    456 	elif [ -e /etc/arch-release ] && [ -d /etc/rc.d ]; then
    457 		RCDIR=/etc/rc.d
    458 		RESTARTCMD='
    459 			if [ -e /var/run/daemons/$1 ]
    460 			then
    461 				/etc/rc.d/$1 restart
    462 			fi'
    463 	elif [ -e /etc/slackware-version ] && [ -d /etc/rc.d ]; then
    464 		RESTARTCMD='
    465 			if /etc/rc.d/rc.$1 status >/dev/null 2>&1
    466 			then
    467 				/etc/rc.d/rc.$1 restart
    468 			fi'
    469 	elif [ -e /etc/rc.d/rc.subr ] && [ -d /etc/rc.d ]; then
    470 		# OpenBSD
    471 		RESTARTCMD='
    472 			if /etc/rc.d/$1 check >/dev/null 2>&1
    473 			then
    474 				/etc/rc.d/$1 restart
    475 			fi'
    476 	elif [ -d /etc/dinit.d ] && command -v dinitctl >/dev/null 2>&1; then
    477 		RESTARTCMD='dinitctl --quiet restart --ignore-unstarted $1'
    478 	else
    479 		for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
    480 			[ -d $x ] || continue
    481 			RESTARTCMD="
    482 				if $x/\$1 $status >/dev/null 2>&1
    483 				then
    484 					$x/\$1 restart
    485 				fi"
    486 			break
    487 		done
    488 	fi
    489 
    490 	if [ -z "$RESTARTCMD" ]; then
    491 		if [ "$_NOINIT_WARNED" != true ]; then
    492 			warn "could not detect a useable init system"
    493 			_NOINIT_WARNED=true
    494 		fi
    495 		return 1
    496 	fi
    497 	_NOINIT_WARNED=
    498 	return 0
    499 }
    500 
    501 echo_resolv()
    502 {
    503 	OIFS="$IFS"
    504 
    505 	[ -n "$1" ] && [ -f "$KEYDIR/$1" ] || return 1
    506 	echo "# resolv.conf from $1"
    507 	# Our variable maker works of the fact each resolv.conf per key
    508 	# is separated by blank lines.
    509 	# So we remove them when echoing them.
    510 	while read -r line; do
    511 		IFS="$OIFS"
    512 		if [ -n "$line" ]; then
    513 			# We need to set IFS here to preserve any whitespace
    514 			IFS=''
    515 			printf "%s\n" "$line"
    516 		fi
    517 	done < "$KEYDIR/$1"
    518 	IFS="$OIFS"
    519 }
    520 
    521 deprecated_key()
    522 {
    523 	[ -d "$DEPRECATEDDIR" ] || return 1
    524 
    525 	cd "$DEPRECATEDDIR"
    526 	for da; do
    527 		for daf in *; do
    528 			[ -f "$daf" ] || continue
    529 			case "$da" in
    530 			$daf) return 0;;
    531 			esac
    532 		done
    533 	done
    534 	return 1
    535 }
    536 
    537 match()
    538 {
    539 	match="$1"
    540 	file="$2"
    541 	retval=1
    542 	count=0
    543 
    544 	while read -r keyword value; do
    545 		new_match=
    546 		for om in $match; do
    547 			m="$om"
    548 			keep=
    549 			while [ -n "$m" ]; do
    550 				k="${m%%/*}"
    551 				r="${m#*/}"
    552 				f="${r%%/*}"
    553 				r="${r#*/}"
    554 				# If the length of m is the same as k/f then
    555 				# we know that we are done
    556 				if [ ${#m} = $((${#k} + 1 + ${#f})) ]; then
    557 					r=
    558 				fi
    559 				m="$r"
    560 				matched=false
    561 				case "$keyword" in
    562 				$k)
    563 					case "$value" in
    564 					$f)
    565 						matched=true
    566 						;;
    567 					esac
    568 					;;
    569 				esac
    570 				if ! $matched; then
    571 					keep="$keep${keep:+/}$k/$f"
    572 				fi
    573 			done
    574 			if [ -n "$om" ] && [ -z "$keep" ]; then
    575 				retval=0
    576 				break 2
    577 			fi
    578 			new_match="${new_match}${new_match:+ }${keep}"
    579 		done
    580 		match="${new_match}"
    581 	done < "$file"
    582 	return $retval
    583 }
    584 
    585 list_keys() {
    586 	list_cmd="$1"
    587 	shift
    588 
    589 	[ -d "$KEYDIR" ] || return 0
    590 	cd "$KEYDIR"
    591 
    592 	[ -n "$1" ] || set -- "*"
    593 	list=
    594 	retval=0
    595 	if [ "$list_cmd" = -i ] || [ "$list_cmd" = -l ]; then
    596 		for i in $@; do
    597 			if [ ! -f "$i" ]; then
    598 				if ! $force && [ "$i" != "*" ]; then
    599 					echo "No resolv.conf for key $i" >&2
    600 				fi
    601 				retval=2
    602 			       	continue
    603 			fi
    604 			list="$list $i"
    605 		done
    606 		[ -z "$list" ] || uniqify $list
    607 		return $retval
    608 	fi
    609 
    610 	if [ "$list_cmd" != -I ] && [ "$list_cmd" != -L ]; then
    611 		echo "list_keys: unknown command $list_cmd" >&2
    612 		return 1
    613 	fi
    614 
    615 	if [ -d "$EXCLUSIVEDIR" ]; then
    616 		cd "$EXCLUSIVEDIR"
    617 		for i in $EXCLUSIVEDIR/*; do
    618 			if [ -f "$i" ]; then
    619 				cd "$KEYDIR"
    620 				for ii in $inclusive_keys; do
    621 					if [ -f "$ii" ] && [ "${i#* }" = "$ii" ]; then
    622 						continue 2
    623 					fi
    624 				done
    625 				list="${i#* }"
    626 				break
    627 			fi
    628 		done
    629 		cd "$KEYDIR"
    630 		if [ -n "$list" ]; then
    631 			for i in $@; do
    632 				# list will be one item due to the above
    633 				if [ -f "$i" ] && [ "$i" = "$list" ]; then
    634 					echo "$i"
    635 					return 0
    636 				fi
    637 			done
    638 			return 0
    639 		fi
    640 	fi
    641 
    642 	for i in $key_order; do
    643 		for ii in "$i" "$i":* "$i".*; do
    644 			[ -f "$ii" ] && list="$list $ii"
    645 		done
    646 	done
    647 
    648 	for i in $dynamic_order; do
    649 		for ii in "$i" "$i":* "$i".*; do
    650 			if [ -f "$ii" ] && ! [ -e "$METRICDIR/"*" $ii" ]
    651 			then
    652 				list="$list $ii"
    653 			fi
    654 		done
    655 	done
    656 
    657 	# Interfaces have an implicit metric of 0 if not specified.
    658 	for i in *; do
    659 		if [ -f "$i" ] && ! [ -e "$METRICDIR/"*" $i" ]; then
    660 			list="$list $i"
    661 		fi
    662 	done
    663 
    664 	if [ -d "$METRICDIR" ]; then
    665 		cd "$METRICDIR"
    666 		for i in *; do
    667 			[ -f "$i" ] && list="$list ${i#* }"
    668 		done
    669 		cd "$KEYDIR"
    670 	fi
    671 
    672 	# Move deprecated keys to the back
    673 	active=
    674 	deprecated=
    675 	for i in $list; do
    676 		if deprecated_key "$i"; then
    677 			deprecated="$deprecated $i"
    678 		else
    679 			active="$active $i"
    680 		fi
    681 	done
    682 	list="$active $deprecated"
    683 
    684 	retval=0
    685 	if [ "$1" != "*" ]; then
    686 		cd "$KEYDIR"
    687 		matched=
    688 		for i in $@; do
    689 			if ! [ -f "$i" ]; then
    690 				if ! $force; then
    691 					echo "No resolv.conf for key $i" >&2
    692 				fi
    693 				retval=2
    694 				continue
    695 			fi
    696 			for ii in $list; do
    697 				if [ "$i" = "$ii" ]; then
    698 					matched="$matched${matched:+ }$i"
    699 					break
    700 				fi
    701 			done
    702 		done
    703 		if [ -z "$matched" ]; then
    704 			return $retval
    705 		fi
    706 		list="$matched"
    707 	fi
    708 
    709 	allowed=
    710 	for i in $(uniqify $list); do
    711 		if [ -n "$allow_keys" ]; then
    712 			x=false
    713 			for ii in $allow_keys; do
    714 				if [ "$i" = "$ii" ]; then
    715 					x=true
    716 					break
    717 				fi
    718 			done
    719 			$x || continue
    720 		fi
    721 		for ii in $deny_keys; do
    722 			if [ "$i" = "$ii" ]; then
    723 				continue 2
    724 			fi
    725 		done
    726 
    727 		if [ -n "$exclude" ] && match "$exclude" "$i"; then
    728 			continue
    729 		fi
    730 		allowed="$allowed${allowed:+ }$i"
    731 	done
    732 
    733 	cd "$KEYDIR"
    734 	for i in $exclusive_keys; do
    735 		for ii in $allowed; do
    736 			if [ "$i" = "$ii" ]; then
    737 				echo "$i"
    738 				return
    739 			fi
    740 		done
    741 	done
    742 	[ -z "$allowed" ] || echo "$allowed"
    743 }
    744 
    745 list_resolv()
    746 {
    747 	keys="$(list_keys "$@")"
    748 	retval=$?
    749 	if [ "$retval" != 0 ]; then
    750 		return $retval
    751 	fi
    752 	for i in $keys; do
    753 		echo_resolv "$i" && echo
    754 	done
    755 }
    756 
    757 list_private()
    758 {
    759 	KEYS=
    760 	cd "$KEYDIR"
    761 	if [ -z "$1" ]; then
    762 		set -- "*"
    763 	fi
    764 	for i in $@; do
    765 		if private_key "$i"; then
    766 		KEYS="${KEYS}${KEYS:+ }$i"
    767 		fi
    768 	done
    769 	if [ -n "$KEYS" ]; then
    770 		echo "$KEYS"
    771 	fi
    772 }
    773 
    774 list_nosearch()
    775 {
    776 
    777 	KEYS=
    778 	cd "$KEYDIR"
    779 	if [ -z "$1" ]; then
    780 		set -- "*"
    781 	fi
    782 	for i in $@; do
    783 		if nosearch_key "$i"; then
    784 			KEYS="${KEYS}${KEYS:+ }$i"
    785 		fi
    786 	done
    787 	if [ -n "$KEYS" ]; then
    788 		echo "$KEYS"
    789 	fi
    790 }
    791 
    792 list_exclusive()
    793 {
    794 	KEYS=
    795 	cd "$KEYDIR"
    796 	if [ -z "$1" ]; then
    797 		set -- "*"
    798 	fi
    799 	for i in $@; do
    800 		if exclusive_key "$i"; then
    801 			KEYS="${KEYS}${KEYS:+ }$i"
    802 		fi
    803 	done
    804 	if [ -n "$KEYS" ]; then
    805 		echo "$KEYS"
    806 	fi
    807 }
    808 
    809 list_remove()
    810 {
    811 	[ -z "$2" ] && return 0
    812 	eval list=\"\$$1\"
    813 	shift
    814 	result=
    815 	retval=0
    816 
    817 	set -f
    818 	for e; do
    819 		found=false
    820 		for l in $list; do
    821 			case "$e" in
    822 			$l) found=true;;
    823 			esac
    824 			$found && break
    825 		done
    826 		if $found; then
    827 			retval=$(($retval + 1))
    828 		else
    829 			result="$result $e"
    830 		fi
    831 	done
    832 	set +f
    833 	echo "${result# *}"
    834 	return $retval
    835 }
    836 
    837 echo_prepend()
    838 {
    839 	echo "# Generated by resolvconf"
    840 	if [ -n "$search_domains" ]; then
    841 		echo "search $search_domains"
    842 	fi
    843 	for n in $name_servers; do
    844 		echo "nameserver $n"
    845 	done
    846 	echo
    847 }
    848 
    849 echo_append()
    850 {
    851 	echo "# Generated by resolvconf"
    852 	if [ -n "$search_domains_append" ]; then
    853 		echo "search $search_domains_append"
    854 	fi
    855 	for n in $name_servers_append; do
    856 		echo "nameserver $n"
    857 	done
    858 	echo
    859 }
    860 
    861 tolower() {
    862 	# There is no good way of doing this portably in shell :(
    863 	# Luckily we are only doing this for domain names which we
    864 	# know have to be ASCII.
    865 	# Non ASCII domains *should* be translated to ASCII *before*
    866 	# we get to this stage.
    867 	# We could use echo "$@" | tr '[:upper:]' '[:lower:]' but
    868 	# tr is in /usr/bin and may not be available when data is fed
    869 	# to resolvconf.
    870 	# So it's the cost of a pipe + fork vs this slow loop
    871 	#
    872 	for word; do
    873 		# Check if we have any upper to avoid looping per char
    874 		case "$word" in
    875 			*[A-Z]*) ;;
    876 			*) printf "%s " "$word"; continue;;
    877 		esac
    878 
    879 		while [ -n "$word" ]; do
    880 			# Remove everything except the first character
    881 			afterchar="${word#?}"
    882 			# Remove the afterchar to get the first character
    883 			char="${word%%$afterchar}"
    884 			# Assign afterchar back to word for looping
    885 			word="$afterchar"
    886 
    887 			# Now enforce lowercase a-z
    888 			case "$char" in
    889 			A) char=a;;
    890 			B) char=b;;
    891 			C) char=c;;
    892 			D) char=d;;
    893 			E) char=e;;
    894 			F) char=f;;
    895 			G) char=g;;
    896 			H) char=h;;
    897 			I) char=i;;
    898 			J) char=j;;
    899 			K) char=k;;
    900 			L) char=l;;
    901 			M) char=m;;
    902 			N) char=n;;
    903 			O) char=o;;
    904 			P) char=p;;
    905 			Q) char=q;;
    906 			R) char=r;;
    907 			S) char=s;;
    908 			T) char=t;;
    909 			U) char=u;;
    910 			V) char=v;;
    911 			W) char=w;;
    912 			X) char=x;;
    913 			Y) char=y;;
    914 			Z) char=z;;
    915 			esac
    916 			printf %s "$char"
    917 		done
    918 		printf " "
    919 	done
    920 	printf "\n"
    921 }
    922 
    923 # Strip any trailing dot from each name as a FQDN does not belong
    924 # in resolv.conf(5).
    925 # While DNS is not case sensitive, our labels for building the zones
    926 # are, so ensure it's lower case.
    927 process_domain()
    928 {
    929 	for word in $(tolower "$@"); do
    930 		printf "%s " "${word%.}"
    931 	done
    932 	printf "\n"
    933 }
    934 
    935 process_resolv()
    936 {
    937 	while read -r keyword value; do
    938 		for r in $replace; do
    939 			k="${r%%/*}"
    940 			r="${r#*/}"
    941 			f="${r%%/*}"
    942 			r="${r#*/}"
    943 			v="${r%%/*}"
    944 			case "$keyword" in
    945 			$k)
    946 				case "$value" in
    947 				$f) value="$v";;
    948 				esac
    949 				;;
    950 			esac
    951 		done
    952 		val=
    953 		for sub in $value; do
    954 			for r in $replace_sub; do
    955 				k="${r%%/*}"
    956 				r="${r#*/}"
    957 				f="${r%%/*}"
    958 				r="${r#*/}"
    959 				v="${r%%/*}"
    960 				case "$keyword" in
    961 				$k)
    962 					case "$sub" in
    963 					$f) sub="$v";;
    964 					esac
    965 					;;
    966 				esac
    967 			done
    968 			val="$val${val:+ }$sub"
    969 		done
    970 		case "$keyword" in
    971 		\#)
    972 			case "$val" in
    973 			"resolv.conf from "*) ;;
    974 			*) continue;;
    975 			esac
    976 			;;
    977 		\#*) continue;;
    978 		esac
    979 		case "$keyword" in
    980 		domain|search) val="$(process_domain $val)";;
    981 		esac
    982 		printf "%s %s\n" "$keyword" "$val"
    983 	done
    984 }
    985 
    986 make_vars()
    987 {
    988 	# Clear variables
    989 	DOMAIN=
    990 	DOMAINS=
    991 	SEARCH=
    992 	NAMESERVERS=
    993 	LOCALNAMESERVERS=
    994 
    995 	if [ -n "${name_servers}${search_domains}" ]; then
    996 		eval "$(echo_prepend | parse_resolv)"
    997 	fi
    998 	if [ -z "$VFLAG" ]; then
    999 		eval "$(list_resolv -L "$@" | process_resolv | parse_resolv)"
   1000 	fi
   1001 	if [ -n "${name_servers_append}${search_domains_append}" ]; then
   1002 		eval "$(echo_append | parse_resolv)"
   1003 	fi
   1004 
   1005 	# Ensure that we only list each domain once
   1006 	newdomains=
   1007 	for d in $DOMAINS; do
   1008 		dn="${d%%:*}"
   1009 		list_remove domain_blacklist "$dn" >/dev/null || continue
   1010 		case " $newdomains" in
   1011 		*" ${dn}:"*) continue;;
   1012 		esac
   1013 		newns=
   1014 		for nd in $DOMAINS; do
   1015 			if [ "$dn" = "${nd%%:*}" ]; then
   1016 				ns="${nd#*:}"
   1017 				while [ -n "$ns" ]; do
   1018 					case ",$newns," in
   1019 					*,${ns%%,*},*) ;;
   1020 					*) list_remove name_server_blacklist \
   1021 						"${ns%%,*}" >/dev/null \
   1022 					&& newns="$newns${newns:+,}${ns%%,*}";;
   1023 					esac
   1024 					[ "$ns" = "${ns#*,}" ] && break
   1025 					ns="${ns#*,}"
   1026 				done
   1027 			fi
   1028 		done
   1029 		if [ -n "$newns" ]; then
   1030 			newdomains="$newdomains${newdomains:+ }$dn:$newns"
   1031 		fi
   1032 	done
   1033 
   1034 	DOMAIN="$(list_remove domain_blacklist $DOMAIN)"
   1035 	SEARCH="$(uniqify $SEARCH)"
   1036 	SEARCH="$(list_remove domain_blacklist $SEARCH)"
   1037 	NAMESERVERS="$(uniqify $NAMESERVERS)"
   1038 	NAMESERVERS="$(list_remove name_server_blacklist $NAMESERVERS)"
   1039 	LOCALNAMESERVERS="$(uniqify $LOCALNAMESERVERS)"
   1040 	LOCALNAMESERVERS="$(list_remove name_server_blacklist $LOCALNAMESERVERS)"
   1041 
   1042 	# Ensure output is quoted for eval
   1043 	printf 'DOMAIN=%s\n' "$(quote "$DOMAIN")"
   1044 	printf 'SEARCH=%s\n' "$(quote "$SEARCH")"
   1045 	printf 'NAMESERVERS=%s\n' "$(quote "$NAMESERVERS")"
   1046 	printf 'LOCALNAMESERVERS=%s\n' "$(quote "$LOCALNAMESERVERS")"
   1047 	printf 'DOMAINS=%s\n' "$(quote "$newdomains")"
   1048 }
   1049 
   1050 force=false
   1051 LFLAG=
   1052 VFLAG=
   1053 while getopts a:C:c:Dd:fhIiLlm:pRruvVx OPT; do
   1054 	case "$OPT" in
   1055 	f) force=true;;
   1056 	h) usage;;
   1057 	m) IF_METRIC="$OPTARG";;
   1058 	p)
   1059 		if [ "$IF_PRIVATE" = 1 ]; then
   1060 			IF_NOSEARCH=1
   1061 		else
   1062 			IF_PRIVATE=1
   1063 		fi
   1064 		;;
   1065 	V)
   1066 		VFLAG=1
   1067 		if [ "$local_nameservers" = \
   1068 		    "127.* 0.0.0.0 255.255.255.255 ::1" ]
   1069 		then
   1070 			local_nameservers=
   1071 		fi
   1072 		;;
   1073 	x) IF_EXCLUSIVE=1;;
   1074 	'?') exit 1;;
   1075 	*)
   1076 		[ "$OPT" != L ] || LFLAG=1
   1077 		cmd="$OPT"; key="$OPTARG";;
   1078 	esac
   1079 done
   1080 shift $(($OPTIND - 1))
   1081 if [ -n "$key" ]; then
   1082 	set -- "$key" "$@"
   1083 fi
   1084 
   1085 if [ -z "$cmd" ]; then
   1086 	if [ "$IF_PRIVATE" = 1 ]; then
   1087 		cmd=p
   1088 	elif [ "$IF_EXCLUSIVE" = 1 ]; then
   1089 		cmd=x
   1090 	fi
   1091 fi
   1092 
   1093 # -D ensures that the listed config file base dirs exist
   1094 if [ "$cmd" = D ]; then
   1095 	config_mkdirs "$@"
   1096 	exit $?
   1097 fi
   1098 
   1099 # -i lists which keys have a resolv file
   1100 if [ "$cmd" = i ]; then
   1101 	# If the -L modifier is given, the list is post-processed
   1102 	if [ "$LFLAG" = 1 ]; then
   1103 		cmd="L"
   1104 	fi
   1105 	list_keys "-$cmd" "$@"
   1106 	exit $?
   1107 fi
   1108 
   1109 # -l lists our resolv files, optionally for a specific key
   1110 if [ "$cmd" = l ]; then
   1111 	list_resolv "-$cmd" "$@"
   1112 	exit $?
   1113 fi
   1114 # -L is the same as -l, but post-processed from our config
   1115 if [ "$cmd" = L ]; then
   1116 	list_resolv "-$cmd" "$@" | process_resolv
   1117 	exit $?
   1118 fi
   1119 
   1120 if [ "$cmd" = p ]; then
   1121 	if [ "$IF_NOSEARCH" = 1 ]; then
   1122 		list_nosearch "$@"
   1123 	else
   1124 		list_private "$@"
   1125 	fi
   1126 	exit $?
   1127 fi
   1128 
   1129 if [ "$cmd" = x ]; then
   1130 	list_exclusive "$@"
   1131 	exit $?
   1132 fi
   1133 
   1134 # Restart a service or echo the command to restart a service
   1135 if [ "$cmd" = r ] || [ "$cmd" = R ]; then
   1136 	detect_init || exit 1
   1137 	if [ "$cmd" = r ]; then
   1138 		eval "$RESTARTCMD"
   1139 	else
   1140 		echo "$RESTARTCMD" |
   1141 			sed -e '/^$/d' -e 's/^			//g'
   1142 	fi
   1143 	exit $?
   1144 fi
   1145 
   1146 # Not normally needed, but subscribers should be able to run independently
   1147 if [ "$cmd" = v ] || [ -n "$VFLAG" ]; then
   1148 	make_vars "$@"
   1149 	exit $?
   1150 fi
   1151 
   1152 # Test that we have valid options
   1153 case "$cmd" in
   1154 a|d|C|c)
   1155 	if [ -z "$key" ]; then
   1156 		error_exit "Key not specified"
   1157 	fi
   1158 	;;
   1159 I|u)	;;
   1160 *)
   1161 	if [ -n "$cmd" ] && [ "$cmd" != h ]; then
   1162 		error_exit "Unknown option $cmd"
   1163 	fi
   1164 	usage
   1165 	;;
   1166 esac
   1167 
   1168 if [ "$cmd" = a ]; then
   1169 	for x in '/' '\' ' ' '*'; do
   1170 		case "$key" in
   1171 		"$x"|"$x"*|*"$x"|*"$x"*) error_exit "$x not allowed in key name";;
   1172 		esac
   1173 	done
   1174 	for x in '.' '-' '~'; do
   1175 		case "$key" in
   1176 		"$x"*) error_exit \
   1177 			"$x not allowed at start of key name";;
   1178 		esac
   1179 	done
   1180 	[ "$cmd" = a ] && [ -t 0 ] && error_exit "No file given via stdin"
   1181 fi
   1182 
   1183 if [ ! -d "$VARDIR" ]; then
   1184 	if [ -L "$VARDIR" ]; then
   1185 		dir="$(readlink "$VARDIR")"
   1186 		# link maybe relative
   1187 		cd "${VARDIR%/*}"
   1188 		if ! mkdir -m 0755 -p "$dir"; then
   1189 			error_exit "Failed to create needed" \
   1190 				"directory $dir"
   1191 		fi
   1192 	else
   1193 		if ! mkdir -m 0755 -p "$VARDIR"; then
   1194 			error_exit "Failed to create needed" \
   1195 				"directory $VARDIR"
   1196 		fi
   1197 	fi
   1198 fi
   1199 
   1200 if [ ! -d "$KEYDIR" ]; then
   1201 	mkdir -m 0755 -p "$KEYDIR" || \
   1202 		error_exit "Failed to create needed directory $KEYDIR"
   1203 	if [ "$cmd" = d ]; then
   1204 		# Provide the same error messages as below
   1205 		if ! ${force}; then
   1206 			cd "$KEYDIR"
   1207 			for i in $@; do
   1208 				warn "No resolv.conf for key $i"
   1209 			done
   1210 		fi
   1211 		${force}
   1212 		exit $?
   1213 	fi
   1214 fi
   1215 
   1216 # A key was added, changed, deleted or a general update was called.
   1217 # Due to exclusivity we need to ensure that this is an atomic operation.
   1218 # Our subscribers *may* need this as well if the init system is sub par.
   1219 # As such we spinlock at this point as best we can.
   1220 # We don't use flock(1) because it's not widely available and normally resides
   1221 # in /usr which we do our very best to operate without.
   1222 [ -w "$VARDIR" ] || error_exit "Cannot write to $LOCKDIR"
   1223 : ${lock_timeout:=10}
   1224 : ${clear_nopids:=5}
   1225 have_pid=false
   1226 had_pid=false
   1227 while true; do
   1228 	if mkdir "$LOCKDIR" 2>/dev/null; then
   1229 		trap 'rm -rf "$LOCKDIR";' EXIT
   1230 		trap 'rm -rf "$LOCKDIR"; exit 1' INT QUIT ABRT SEGV ALRM TERM
   1231 		echo $$ >"$LOCKDIR/pid"
   1232 		break
   1233 	fi
   1234 	pid=$(cat "$LOCKDIR/pid" 2>/dev/null)
   1235 	if [ "$pid" -gt 0 ] 2>/dev/null; then
   1236 		have_pid=true
   1237 		had_pid=true
   1238 	else
   1239 		have_pid=false
   1240 		clear_nopids=$(($clear_nopids - 1))
   1241 		if [ "$clear_nopids" -le 0 ]; then
   1242 			warn "not seen a pid, clearing lock directory"
   1243 			rm -rf "$LOCKDIR"
   1244 		else
   1245 			lock_timeout=$(($lock_timeout - 1))
   1246 			sleep 1
   1247 		fi
   1248 		continue
   1249 	fi
   1250 	if $have_pid && ! kill -0 "$pid"; then
   1251 		warn "clearing stale lock pid $pid"
   1252 		rm -rf "$LOCKDIR"
   1253 		continue
   1254 	fi
   1255 	lock_timeout=$(($lock_timeout - 1))
   1256 	if [ "$lock_timeout" -le 0 ]; then
   1257 		if $have_pid; then
   1258 			error_exit "timed out waiting for lock from pid $pid"
   1259 		else
   1260 			if $had_pid; then
   1261 				error_exit "timed out waiting for lock" \
   1262 					"from some pids"
   1263 			else
   1264 				error_exit "timed out waiting for lock"
   1265 			fi
   1266 		fi
   1267 	fi
   1268 	sleep 1
   1269 done
   1270 unset have_pid had_pid clear_nopids
   1271 
   1272 case "$cmd" in
   1273 a)
   1274 	# Read resolv.conf from stdin
   1275 	resolv="$(cat)"
   1276 	changed=false
   1277 	changedfile=false
   1278 	# If what we are given matches what we have, then do nothing
   1279 	if [ -e "$KEYDIR/$key" ]; then
   1280 		if [ "$(echo "$resolv")" != \
   1281 			"$(cat "$KEYDIR/$key")" ]
   1282 		then
   1283 			changed=true
   1284 			changedfile=true
   1285 		fi
   1286 	else
   1287 		changed=true
   1288 		changedfile=true
   1289 	fi
   1290 
   1291 	# Set metric and private before creating the resolv.conf file
   1292 	# to ensure that it will have the correct flags
   1293 	[ ! -d "$METRICDIR" ] && mkdir "$METRICDIR"
   1294 	oldmetric="$METRICDIR/"*" $key"
   1295 	newmetric=
   1296 	if [ -n "$IF_METRIC" ]; then
   1297 		# Pad metric to 6 characters, so 5 is less than 10
   1298 		while [ ${#IF_METRIC} -le 6 ]; do
   1299 			IF_METRIC="0$IF_METRIC"
   1300 		done
   1301 		newmetric="$METRICDIR/$IF_METRIC $key"
   1302 	fi
   1303 	rm -f "$METRICDIR/"*" $key"
   1304 	[ "$oldmetric" != "$newmetric" ] &&
   1305 	    [ "$oldmetric" != "$METRICDIR/* $key" ] &&
   1306 		changed=true
   1307 	[ -n "$newmetric" ] && echo " " >"$newmetric"
   1308 
   1309 	case "$IF_PRIVATE" in
   1310 	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
   1311 		if [ ! -d "$PRIVATEDIR" ]; then
   1312 			[ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR"
   1313 			mkdir "$PRIVATEDIR"
   1314 		fi
   1315 		[ -e "$PRIVATEDIR/$key" ] || changed=true
   1316 		[ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$key"
   1317 		;;
   1318 	*)
   1319 		if [ -e "$PRIVATEDIR/$key" ]; then
   1320 			rm -f "$PRIVATEDIR/$key"
   1321 			changed=true
   1322 		fi
   1323 		;;
   1324 	esac
   1325 
   1326 	case "$IF_NOSEARCH" in
   1327 	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
   1328 		if [ ! -d "$NOSEARCHDIR" ]; then
   1329 			[ -e "$NOSEARCHDIR" ] && rm "$NOSEARCHDIR"
   1330 			mkdir "$NOSEARCHDIR"
   1331 		fi
   1332 		[ -e "$NOSEARCHDIR/$key" ] || changed=true
   1333 		[ -d "$NOSEARCHDIR" ] && echo " " >"$NOSEARCHDIR/$key"
   1334 		;;
   1335 	*)
   1336 		if [ -e "$NOSEARCHDIR/$key" ]; then
   1337 			rm -f "$NOSEARCHDIR/$key"
   1338 			changed=true
   1339 		fi
   1340 		;;
   1341 	esac
   1342 	set +x
   1343 
   1344 	oldexcl=
   1345 	for x in "$EXCLUSIVEDIR/"*" $key"; do
   1346 		if [ -f "$x" ]; then
   1347 			oldexcl="$x"
   1348 			break
   1349 		fi
   1350 	done
   1351 	case "$IF_EXCLUSIVE" in
   1352 	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
   1353 		if [ ! -d "$EXCLUSIVEDIR" ]; then
   1354 			[ -e "$EXCLUSIVEDIR" ] && rm "$EXCLUSIVEDIR"
   1355 			mkdir "$EXCLUSIVEDIR"
   1356 		fi
   1357 		cd "$EXCLUSIVEDIR"
   1358 		for x in *; do
   1359 			[ -f "$x" ] && break
   1360 		done
   1361 		if [ "${x#* }" != "$key" ]; then
   1362 			if [ "$x" = "${x% *}" ]; then
   1363 				x=10000000
   1364 			else
   1365 				x="${x% *}"
   1366 			fi
   1367 			if [ "$x" = "0000000" ]; then
   1368 				warn "exclusive underflow"
   1369 			else
   1370 				x=$(($x - 1))
   1371 			fi
   1372 			if [ -d "$EXCLUSIVEDIR" ]; then
   1373 				echo " " >"$EXCLUSIVEDIR/$x $key"
   1374 			fi
   1375 			changed=true
   1376 		fi
   1377 		;;
   1378 	*)
   1379 		if [ -f "$oldexcl" ]; then
   1380 			rm -f "$oldexcl"
   1381 			changed=true
   1382 		fi
   1383 		;;
   1384 	esac
   1385 
   1386 	if $changedfile; then
   1387 		printf "%s\n" "$resolv" >"$KEYDIR/$key" || exit $?
   1388 	elif ! $changed && [ ! -e "$VARDIR"/error ]; then
   1389 		exit 0
   1390 	fi
   1391 	unset changed changedfile oldmetric newmetric x oldexcl
   1392 	;;
   1393 
   1394 d)
   1395 	# Delete any existing information about the key
   1396 	cd "$KEYDIR"
   1397 	changed=false
   1398 	for i in $@; do
   1399 		if [ -e "$i" ]; then
   1400 			changed=true
   1401 		elif ! ${force}; then
   1402 			warn "No resolv.conf for key $i"
   1403 		fi
   1404 		rm -f "$i" "$METRICDIR/"*" $i" \
   1405 			"$PRIVATEDIR/$i" \
   1406 			"$EXCLUSIVEDIR/"*" $i" || exit $?
   1407 	done
   1408 
   1409 	if ! $changed && [ ! -e "$VARDIR"/error ]; then
   1410 		# Set the return code based on the forced flag
   1411 		$force
   1412 		exit $?
   1413 	fi
   1414 	unset changed i
   1415 	;;
   1416 
   1417 C)
   1418 	# Mark key as deprecated
   1419 	[ ! -d "$DEPRECATEDDIR" ] && mkdir "$DEPRECATEDDIR"
   1420 	cd "$DEPRECATEDDIR"
   1421 	changed=false
   1422 	for i in $@; do
   1423 		if [ ! -e "$i" ]; then
   1424 			changed=true
   1425 			echo " " >"$i" || exit $?
   1426 		fi
   1427 	done
   1428 	if ! $changed && [ ! -e "$VARDIR"/error ]; then
   1429 		exit 0
   1430 	fi
   1431 	unset changed i
   1432 	;;
   1433 
   1434 c)
   1435 	# Mark key as active
   1436 	if [ -d "$DEPRECATEDDIR" ]; then
   1437 		cd "$DEPRECATEDDIR"
   1438 		changed=false
   1439 		for i in $@; do
   1440 			if [ -e "$i" ]; then
   1441 				changed=true
   1442 				rm "$i" || exit $?
   1443 			fi
   1444 		done
   1445 		if ! $changed && [ ! -e "$VARDIR"/error ]; then
   1446 			exit 0
   1447 		fi
   1448 		unset changed i
   1449 	fi
   1450 	;;
   1451 I)
   1452 	# Init the state dir, keeping our lock and key directories only
   1453 	for i in "$VARDIR"/*; do
   1454 		case "$i" in
   1455 			"$LOCKDIR") ;;
   1456 			"$KEYDIR") rm -rf "$KEYDIR"/*;;
   1457 			*) rm -rf "$i";;
   1458 		esac
   1459 	done
   1460 	;;
   1461 esac
   1462 
   1463 case "${resolvconf:-YES}" in
   1464 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
   1465 *) exit 0;;
   1466 esac
   1467 
   1468 # Try and detect a suitable init system for our scripts
   1469 detect_init
   1470 export RESTARTCMD RCDIR _NOINIT_WARNED
   1471 
   1472 eval "$(make_vars)"
   1473 export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
   1474 : ${list_resolv:=list_resolv -L}
   1475 retval=0
   1476 
   1477 # Run scripts in the same directory resolvconf is run from
   1478 # in case any scripts accidentally dump files in the wrong place.
   1479 cd "$_PWD"
   1480 for script in "$LIBEXECDIR"/*; do
   1481 	if [ -f "$script" ]; then
   1482 		script_var="${script##*/}"
   1483 		while [ "${script_var%%-*}" != "$script_var" ]; do
   1484 			script_var="${script_var%%-*}_${script_var#*-}"
   1485 		done
   1486 		eval script_enabled="\$$script_var"
   1487 		case "${script_enabled:-YES}" in
   1488 		[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;;
   1489 		*) continue;;
   1490 		esac
   1491 		if [ -x "$script" ]; then
   1492 			"$script" "$cmd" "$key"
   1493 		else
   1494 			(set -- "$cmd" "$key"; . "$script")
   1495 		fi
   1496 		retval=$(($retval + $?))
   1497 	fi
   1498 done
   1499 if [ "$retval" = 0 ]; then
   1500 	rm -f "$VARDIR"/error
   1501 else
   1502 	echo "$retval" >"$VARDIR"/error
   1503 fi
   1504 exit $retval
   1505