Changeset View
Changeset View
Standalone View
Standalone View
resolvconf.in
| #!/bin/sh | #!/bin/sh | ||||
| # Copyright (c) 2007-2019 Roy Marples | # Copyright (c) 2007-2025 Roy Marples | ||||
| # All rights reserved | # All rights reserved | ||||
| # Redistribution and use in source and binary forms, with or without | # Redistribution and use in source and binary forms, with or without | ||||
| # modification, are permitted provided that the following conditions | # modification, are permitted provided that the following conditions | ||||
| # are met: | # are met: | ||||
| # * Redistributions of source code must retain the above copyright | # * Redistributions of source code must retain the above copyright | ||||
| # notice, this list of conditions and the following disclaimer. | # notice, this list of conditions and the following disclaimer. | ||||
| # * Redistributions in binary form must reproduce the above | # * Redistributions in binary form must reproduce the above | ||||
| Show All 9 Lines | |||||
| # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| RESOLVCONF="$0" | RESOLVCONF="$0" | ||||
| OPENRESOLV_VERSION="3.9.2" | OPENRESOLV_VERSION="3.17.4" | ||||
| SYSCONFDIR=@SYSCONFDIR@ | SYSCONFDIR=@SYSCONFDIR@ | ||||
| LIBEXECDIR=@LIBEXECDIR@ | LIBEXECDIR=@LIBEXECDIR@ | ||||
| VARDIR=@VARDIR@ | VARDIR=@VARDIR@ | ||||
| RCDIR=@RCDIR@ | RCDIR=@RCDIR@ | ||||
| RESTARTCMD=@RESTARTCMD@ | RESTARTCMD=@RESTARTCMD@ | ||||
| if [ "$1" = "--version" ]; then | if [ "$1" = "--version" ]; then | ||||
| echo "openresolv $OPENRESOLV_VERSION" | echo "openresolv $OPENRESOLV_VERSION" | ||||
| echo "Copyright (c) 2007-2016 Roy Marples" | echo "Copyright (c) 2007-2025 Roy Marples" | ||||
| exit 0 | exit 0 | ||||
| fi | fi | ||||
| # Disregard dhcpcd setting | # Disregard dhcpcd setting | ||||
| unset interface_order state_dir | unset interface_order state_dir | ||||
| # If you change this, change the test in VFLAG and libc.in as well | # If you change this, change the test in VFLAG and libc.in as well | ||||
| local_nameservers="127.* 0.0.0.0 255.255.255.255 ::1" | local_nameservers="127.* 0.0.0.0 255.255.255.255 ::1" | ||||
| dynamic_order="tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]*" | dynamic_order="tap[0-9]* tun[0-9]* vpn vpn[0-9]* wg[0-9]* ppp[0-9]* ippp[0-9]*" | ||||
| interface_order="lo lo[0-9]*" | interface_order="lo lo[0-9]*" | ||||
| name_server_blacklist="0.0.0.0" | name_server_blacklist="0.0.0.0" | ||||
| # Poor mans cat | |||||
| # /usr might not be available | |||||
| cat() | |||||
| { | |||||
| OIFS="$IFS" | |||||
| IFS='' | |||||
| if [ -n "$1" ]; then | |||||
| while read -r line; do | |||||
| printf "%s\n" "$line" | |||||
| done < "$1" | |||||
| else | |||||
| while read -r line; do | |||||
| printf "%s\n" "$line" | |||||
| done | |||||
| fi | |||||
| retval=$? | |||||
| IFS="$OIFS" | |||||
| return $retval | |||||
| } | |||||
| # Support original resolvconf configuration layout | # Support original resolvconf configuration layout | ||||
| # as well as the openresolv config file | # as well as the openresolv config file | ||||
| if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then | if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then | ||||
| . "$SYSCONFDIR"/resolvconf.conf | . "$SYSCONFDIR"/resolvconf.conf | ||||
| [ -n "$state_dir" ] && VARDIR="$state_dir" | [ -n "$state_dir" ] && VARDIR="$state_dir" | ||||
| elif [ -d "$SYSCONFDIR/resolvconf" ]; then | elif [ -d "$SYSCONFDIR/resolvconf" ]; then | ||||
| SYSCONFDIR="$SYSCONFDIR/resolvconf" | SYSCONFDIR="$SYSCONFDIR/resolvconf" | ||||
| if [ -f "$SYSCONFDIR"/interface-order ]; then | if [ -f "$SYSCONFDIR"/interface-order ]; then | ||||
| interface_order="$(cat "$SYSCONFDIR"/interface-order)" | interface_order="$(cat "$SYSCONFDIR"/interface-order)" | ||||
| fi | fi | ||||
| fi | fi | ||||
| IFACEDIR="$VARDIR/interfaces" | |||||
| KEYDIR="$VARDIR/keys" | |||||
| METRICDIR="$VARDIR/metrics" | METRICDIR="$VARDIR/metrics" | ||||
| PRIVATEDIR="$VARDIR/private" | PRIVATEDIR="$VARDIR/private" | ||||
| NOSEARCHDIR="$VARDIR/nosearch" | |||||
| EXCLUSIVEDIR="$VARDIR/exclusive" | EXCLUSIVEDIR="$VARDIR/exclusive" | ||||
| DEPRECATEDDIR="$VARDIR/deprecated" | |||||
| LOCKDIR="$VARDIR/lock" | LOCKDIR="$VARDIR/lock" | ||||
| _PWD="$PWD" | _PWD="$PWD" | ||||
| # Compat | |||||
| if [ ! -d "$KEYDIR" ] && [ -d "$VARDIR/interfaces" ]; then | |||||
| KEYDIR="$VARDIR/interfaces" | |||||
| fi | |||||
| : ${allow_keys:="$allow_interfaces"} | |||||
| : ${deny_keys:="$deny_interfaces"} | |||||
| : ${key_order:="$interface_order"} | |||||
| : ${inclusive_keys:="$inclusive_interfaces"} | |||||
| : ${exclusive_keys:="$exclusive_interfaces"} | |||||
| : ${private_keys:="$private_interfaces"} | |||||
| : ${public_keys:="$public_interfaces"} | |||||
| warn() | warn() | ||||
| { | { | ||||
| echo "$*" >&2 | echo "${RESOLVCONF##*/}: $*" >&2 | ||||
| } | } | ||||
| error_exit() | error_exit() | ||||
| { | { | ||||
| echo "$*" >&2 | warn "$*" | ||||
| exit 1 | exit 1 | ||||
| } | } | ||||
| usage() | usage() | ||||
| { | { | ||||
| cat <<-EOF | cat <<-EOF | ||||
| Usage: ${RESOLVCONF##*/} [options] command [argument] | Usage: ${RESOLVCONF##*/} [options] command [argument] | ||||
| Inform the system about any DNS updates. | Inform the system about any DNS updates. | ||||
| Commands: | Commands: | ||||
| -a \$INTERFACE Add DNS information to the specified interface | -a \$KEY Add DNS information to the specified key | ||||
| (DNS supplied via stdin in resolv.conf format) | (DNS supplied via stdin in resolv.conf format) | ||||
| -d \$INTERFACE Delete DNS information from the specified interface | -C \$PATTERN Deprecate DNS information for matched key | ||||
| -c \$PATTERN Configure DNS information for matched key | |||||
| -d \$PATTERN Delete DNS information from the matched key | |||||
| -h Show this help cruft | -h Show this help cruft | ||||
| -i [\$PATTERN] Show interfaces that have supplied DNS information | -i [\$PATTERN] Show keys that have supplied DNS information | ||||
| optionally from interfaces that match the specified | optionally from keys that match the specified | ||||
| pattern | pattern | ||||
| -l [\$PATTERN] Show DNS information, optionally from interfaces | -l [\$PATTERN] Show DNS information, optionally from keys | ||||
| that match the specified pattern | that match the specified pattern | ||||
| -L [\$PATTERN] Same as -l, but adjusted by our config | |||||
| -u Run updates from our current DNS information | -u Run updates from our current DNS information | ||||
| --version Echo the ${RESOLVCONF##*/} version | --version Echo the ${RESOLVCONF##*/} version | ||||
| Options: | Options: | ||||
| -f Ignore non existent interfaces | -f Ignore non existent keys | ||||
| -m metric Give the added DNS information a metric | -m metric Give the added DNS information a metric | ||||
| -p Mark the interface as private | -p Mark the resolv.conf as private | ||||
| -x Mark the interface as exclusive | -x Mark the resolv.conf as exclusive | ||||
| Subscriber and System Init Commands: | Subscriber and System Init Commands: | ||||
| -I Init the state dir | -I Init the state dir | ||||
| -r \$SERVICE Restart the system service | -r \$SERVICE Restart the system service | ||||
| (restarting a non-existent or non-running service | (restarting a non-existent or non-running service | ||||
| should have no output and return 0) | should have no output and return 0) | ||||
| -R Show the system service restart command | -R Show the system service restart command | ||||
| -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to | -v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to | ||||
| the console | the console | ||||
| -V [\$PATTERN] Same as -v, but only uses configuration in | -V [\$PATTERN] Same as -v, but only uses configuration in | ||||
| $SYSCONFDIR/resolvconf.conf | $SYSCONFDIR/resolvconf.conf | ||||
| EOF | EOF | ||||
| [ -z "$1" ] && exit 0 | [ -z "$1" ] && exit 0 | ||||
| echo | echo | ||||
| error_exit "$*" | error_exit "$@" | ||||
| } | } | ||||
| # Strip any trailing dot from each name as a FQDN does not belong | public_key() { | ||||
| # in resolv.conf(5) | key="$1" | ||||
| # If you think otherwise, capture a DNS trace and you'll see libc | |||||
| # will strip it regardless. | # Allow expansion | ||||
| # This also solves setting up duplicate zones in our subscribers. | cd "$KEYDIR" | ||||
| # Also strip any comments denoted by #. | |||||
| resolv_strip() | # Public keys override private ones. | ||||
| { | for p in $public_keys; do | ||||
| space= | case "$key" in | ||||
| for word; do | "$p"|"$p":*) return 0;; | ||||
| case "$word" in | |||||
| \#*) break;; | |||||
| esac | esac | ||||
| printf "%s%s" "$space${word%.}" | |||||
| space=" " | |||||
| done | done | ||||
| printf "\n" | |||||
| return 1 | |||||
| } | } | ||||
| private_iface() | private_key() | ||||
| { | { | ||||
| # Allow expansion | key="$1" | ||||
| cd "$IFACEDIR" | |||||
| # Public interfaces override private ones. | if public_key "$key"; then | ||||
| for p in $public_interfaces; do | return 1 | ||||
| case "$iface" in | fi | ||||
| "$p"|"$p":*) return 1;; | |||||
| if [ -e "$PRIVATEDIR/$key" ]; then | |||||
| return 0 | |||||
| fi | |||||
| for p in $private_keys; do | |||||
| case "$key" in | |||||
| "$p"|"$p":*) return 0;; | |||||
| esac | esac | ||||
| done | done | ||||
| if [ -e "$PRIVATEDIR/$iface" ]; then | # Not a private key | ||||
| return 1 | |||||
| } | |||||
| nosearch_key() | |||||
| { | |||||
| key="$1" | |||||
| if public_key "$key"; then | |||||
| return 1 | |||||
| fi | |||||
| if [ -e "$NOSEARCHDIR/$key" ]; then | |||||
| return 0 | return 0 | ||||
| fi | fi | ||||
| for p in $private_interfaces; do | for p in $nosearch_keys; do | ||||
| case "$iface" in | case "$key" in | ||||
| "$p"|"$p":*) return 0;; | "$p"|"$p":*) return 0;; | ||||
| esac | esac | ||||
| done | done | ||||
| # Not a private interface | # Not a non searchable key | ||||
| return 1 | return 1 | ||||
| } | } | ||||
| exclusive_key() | |||||
| { | |||||
| key="$1" | |||||
| for x in "$EXCLUSIVEDIR/"*" $key"; do | |||||
| if [ -f "$x" ]; then | |||||
| return 0 | |||||
| fi | |||||
| done | |||||
| # Not an exclusive key | |||||
| return 1 | |||||
| } | |||||
| # Quote input so it can be safely used for variable assignment via eval | |||||
| quote() | |||||
| { | |||||
| if [ -z "$1" ]; then | |||||
| R="''" | |||||
| else | |||||
| R= | |||||
| for W; do | |||||
| while [ -n "$W" ]; do | |||||
| case "$W" in | |||||
| \'*) R="$R\\'"; W=${W#?};; | |||||
| ?\'*) R="$R\\${W%%\'*}"; W="${W#?}";; | |||||
| *\'*) R="$R'${W%%\'*}'"; W="'${W#*\'}";; | |||||
| ?) R="$R\\$W"; W=;; | |||||
| *) R="$R'$W'"; W=;; | |||||
| esac | |||||
| done | |||||
| done | |||||
| fi | |||||
| printf '%s\n' "$R" | |||||
| return 0 | |||||
| } | |||||
| # Parse resolv.conf's and make variables | # Parse resolv.conf's and make variables | ||||
| # for domain name servers, search name servers and global nameservers | # for domain name servers, search name servers and global nameservers | ||||
| # Important! Each printf here should use the above quote function | |||||
| # to ensure that user input is quoted for eval. | |||||
| parse_resolv() | parse_resolv() | ||||
| { | { | ||||
| domain= | domain= | ||||
| new=true | new=true | ||||
| newns= | newns= | ||||
| ns= | ns= | ||||
| private=false | private=false | ||||
| nosearch=false | |||||
| search= | search= | ||||
| while read -r line; do | while read -r line; do | ||||
| stripped_line="$(resolv_strip ${line#* })" | value="${line#* }" | ||||
| case "$line" in | case "$line" in | ||||
| "# resolv.conf from "*) | "# resolv.conf from "*) | ||||
| if ${new}; then | if ${new}; then | ||||
| iface="${line#\# resolv.conf from *}" | key="${line#\# resolv.conf from *}" | ||||
| new=false | new=false | ||||
| if private_iface "$iface"; then | if nosearch_key "$key"; then | ||||
| private=true | private=true | ||||
| nosearch=true | |||||
| elif private_key "$key"; then | |||||
| private=true | |||||
| nosearch=false | |||||
| else | else | ||||
| private=false | private=false | ||||
| nosearch=false | |||||
| fi | fi | ||||
| fi | fi | ||||
| ;; | ;; | ||||
| "nameserver "*) | "nameserver "*) | ||||
| islocal=false | islocal=false | ||||
| for l in $local_nameservers; do | for l in $local_nameservers; do | ||||
| case "$stripped_line" in | case "$value" in | ||||
| $l) | $l) | ||||
| islocal=true | islocal=true | ||||
| break | break | ||||
| ;; | ;; | ||||
| esac | esac | ||||
| done | done | ||||
| if $islocal; then | if $islocal; then | ||||
| echo "LOCALNAMESERVERS=\"\$LOCALNAMESERVERS $stripped_line\"" | printf 'LOCALNAMESERVERS="$LOCALNAMESERVERS "%s\n' "$(quote "$value")" | ||||
| else | else | ||||
| ns="$ns$stripped_line " | ns="$ns${ns:+ }$value" | ||||
| fi | fi | ||||
| ;; | ;; | ||||
| "domain "*) | "domain "*) | ||||
| search="$stripped_line" | search="$value" | ||||
| if [ -z "$domain" ]; then | if [ -z "$domain" ]; then | ||||
| domain="$search" | domain="$search" | ||||
| echo "DOMAIN=\"$domain\"" | if ! $nosearch; then | ||||
| printf 'DOMAIN=%s\n' "$(quote "$domain")" | |||||
| fi | fi | ||||
| fi | |||||
| ;; | ;; | ||||
| "search "*) | "search "*) | ||||
| search="$stripped_line" | search="$value" | ||||
| ;; | ;; | ||||
| *) | *) | ||||
| [ -n "$line" ] && continue | [ -n "$line" ] && continue | ||||
| if [ -n "$ns" ] && [ -n "$search" ]; then | if [ -n "$ns" ] && [ -n "$search" ]; then | ||||
| newns= | newns= | ||||
| for n in $ns; do | for n in $ns; do | ||||
| newns="$newns${newns:+,}$n" | newns="$newns${newns:+,}$n" | ||||
| done | done | ||||
| ds= | ds= | ||||
| for d in $search; do | for d in $search; do | ||||
| ds="$ds${ds:+ }$d:$newns" | ds="$ds${ds:+ }$d:$newns" | ||||
| done | done | ||||
| echo "DOMAINS=\"\$DOMAINS $ds\"" | printf 'DOMAINS="$DOMAINS "%s\n' "$(quote "$ds")" | ||||
| fi | fi | ||||
| echo "SEARCH=\"\$SEARCH $search\"" | if ! $nosearch; then | ||||
| printf 'SEARCH="$SEARCH "%s\n' "$(quote "$search")" | |||||
| fi | |||||
| if ! $private; then | if ! $private; then | ||||
| echo "NAMESERVERS=\"\$NAMESERVERS $ns\"" | printf 'NAMESERVERS="$NAMESERVERS "%s\n' "$(quote "$ns")" | ||||
| fi | fi | ||||
| ns= | ns= | ||||
| search= | search= | ||||
| new=true | new=true | ||||
| ;; | ;; | ||||
| esac | esac | ||||
| done | done | ||||
| } | } | ||||
| Show All 26 Lines | while [ -n "$2" ]; do | ||||
| printf "/%s" "$1" | printf "/%s" "$1" | ||||
| shift | shift | ||||
| done | done | ||||
| printf "\n" | printf "\n" | ||||
| } | } | ||||
| config_mkdirs() | config_mkdirs() | ||||
| { | { | ||||
| e=0 | |||||
| for f; do | for f; do | ||||
| [ -n "$f" ] || continue | [ -n "$f" ] || continue | ||||
| d="$(dirname "$f")" | d="$(dirname "$f")" | ||||
| if [ ! -d "$d" ]; then | if [ ! -d "$d" ]; then | ||||
| if type install >/dev/null 2>&1; then | mkdir -p "$d" || return $? | ||||
| install -d "$d" || e=$? | |||||
| else | |||||
| mkdir "$d" || e=$? | |||||
| fi | fi | ||||
| fi | |||||
| done | done | ||||
| return $e | return 0 | ||||
| } | } | ||||
| # With the advent of alternative init systems, it's possible to have | # With the advent of alternative init systems, it's possible to have | ||||
| # more than one installed. So we need to try and guess what one we're | # more than one installed. So we need to try and guess what one we're | ||||
| # using unless overriden by configure. | # using unless overridden by configure. | ||||
| # Note that restarting a service is a last resort - the subscribers | # Note that restarting a service is a last resort - the subscribers | ||||
| # should make a reasonable attempt to reconfigre the service via some | # should make a reasonable attempt to reconfigure the service via some | ||||
| # method, normally SIGHUP. | # method, normally SIGHUP. | ||||
| detect_init() | detect_init() | ||||
| { | { | ||||
| [ -n "$RESTARTCMD" ] && return 0 | [ -n "$RESTARTCMD" ] && return 0 | ||||
| # Detect the running init system. | # Detect the running init system. | ||||
| # As systemd and OpenRC can be installed on top of legacy init | # As systemd and OpenRC can be installed on top of legacy init | ||||
| # systems we try to detect them first. | # systems we try to detect them first. | ||||
| Show All 18 Lines | then | ||||
| RESTARTCMD='/sbin/rc-service -i $1 -- -Ds restart' | RESTARTCMD='/sbin/rc-service -i $1 -- -Ds restart' | ||||
| elif [ -x /usr/sbin/invoke-rc.d ]; then | elif [ -x /usr/sbin/invoke-rc.d ]; then | ||||
| RCDIR=/etc/init.d | RCDIR=/etc/init.d | ||||
| RESTARTCMD=' | RESTARTCMD=' | ||||
| if /usr/sbin/invoke-rc.d --quiet $1 status >/dev/null 2>&1 | if /usr/sbin/invoke-rc.d --quiet $1 status >/dev/null 2>&1 | ||||
| then | then | ||||
| /usr/sbin/invoke-rc.d $1 restart | /usr/sbin/invoke-rc.d $1 restart | ||||
| fi' | fi' | ||||
| elif [ -x /usr/bin/s6-rc ] && [ -x /usr/bin/s6-svc ]; then | |||||
| RESTARTCMD=' | |||||
| if s6-rc -a list 2>/dev/null | grep -qFx $1-srv | |||||
| then | |||||
| s6-svc -r /run/service/$1-srv | |||||
| fi' | |||||
| elif [ -x /sbin/service ]; then | elif [ -x /sbin/service ]; then | ||||
| # Old RedHat | # Old RedHat | ||||
| RCDIR=/etc/init.d | RCDIR=/etc/init.d | ||||
| RESTARTCMD=' | RESTARTCMD=' | ||||
| if /sbin/service $1; then | if /sbin/service $1; then | ||||
| /sbin/service $1 restart | /sbin/service $1 restart | ||||
| fi' | fi' | ||||
| elif [ -x /usr/sbin/service ]; then | elif [ -x /usr/sbin/service ]; then | ||||
| Show All 24 Lines | RESTARTCMD=' | ||||
| fi' | fi' | ||||
| elif [ -e /etc/rc.d/rc.subr ] && [ -d /etc/rc.d ]; then | elif [ -e /etc/rc.d/rc.subr ] && [ -d /etc/rc.d ]; then | ||||
| # OpenBSD | # OpenBSD | ||||
| RESTARTCMD=' | RESTARTCMD=' | ||||
| if /etc/rc.d/$1 check >/dev/null 2>&1 | if /etc/rc.d/$1 check >/dev/null 2>&1 | ||||
| then | then | ||||
| /etc/rc.d/$1 restart | /etc/rc.d/$1 restart | ||||
| fi' | fi' | ||||
| elif [ -d /etc/dinit.d ] && command -v dinitctl >/dev/null 2>&1; then | |||||
| RESTARTCMD='dinitctl --quiet restart --ignore-unstarted $1' | |||||
| else | else | ||||
| for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do | for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do | ||||
| [ -d $x ] || continue | [ -d $x ] || continue | ||||
| RESTARTCMD=" | RESTARTCMD=" | ||||
| if $x/\$1 $status >/dev/null 2>&1 | if $x/\$1 $status >/dev/null 2>&1 | ||||
| then | then | ||||
| $x/\$1 restart | $x/\$1 restart | ||||
| fi" | fi" | ||||
| Show All 11 Lines | detect_init() | ||||
| _NOINIT_WARNED= | _NOINIT_WARNED= | ||||
| return 0 | return 0 | ||||
| } | } | ||||
| echo_resolv() | echo_resolv() | ||||
| { | { | ||||
| OIFS="$IFS" | OIFS="$IFS" | ||||
| [ -n "$1" ] && [ -f "$IFACEDIR/$1" ] || return 1 | [ -n "$1" ] && [ -f "$KEYDIR/$1" ] || return 1 | ||||
| echo "# resolv.conf from $1" | echo "# resolv.conf from $1" | ||||
| # Our variable maker works of the fact each resolv.conf per interface | # Our variable maker works of the fact each resolv.conf per key | ||||
| # is separated by blank lines. | # is separated by blank lines. | ||||
| # So we remove them when echoing them. | # So we remove them when echoing them. | ||||
| while read -r line; do | while read -r line; do | ||||
| IFS="$OIFS" | IFS="$OIFS" | ||||
| if [ -n "$line" ]; then | if [ -n "$line" ]; then | ||||
| # We need to set IFS here to preserve any whitespace | # We need to set IFS here to preserve any whitespace | ||||
| IFS='' | IFS='' | ||||
| printf "%s\n" "$line" | printf "%s\n" "$line" | ||||
| fi | fi | ||||
| done < "$IFACEDIR/$1" | done < "$KEYDIR/$1" | ||||
| IFS="$OIFS" | IFS="$OIFS" | ||||
| } | } | ||||
| list_resolv() | deprecated_key() | ||||
| { | { | ||||
| [ -d "$IFACEDIR" ] || return 0 | [ -d "$DEPRECATEDDIR" ] || return 1 | ||||
| cmd="$1" | cd "$DEPRECATEDDIR" | ||||
| for da; do | |||||
| for daf in *; do | |||||
| [ -f "$daf" ] || continue | |||||
| case "$da" in | |||||
| $daf) return 0;; | |||||
| esac | |||||
| done | |||||
| done | |||||
| return 1 | |||||
| } | |||||
| match() | |||||
| { | |||||
| match="$1" | |||||
| file="$2" | |||||
| retval=1 | |||||
| count=0 | |||||
| while read -r keyword value; do | |||||
| new_match= | |||||
| for om in $match; do | |||||
| m="$om" | |||||
| keep= | |||||
| while [ -n "$m" ]; do | |||||
| k="${m%%/*}" | |||||
| r="${m#*/}" | |||||
| f="${r%%/*}" | |||||
| r="${r#*/}" | |||||
| # If the length of m is the same as k/f then | |||||
| # we know that we are done | |||||
| if [ ${#m} = $((${#k} + 1 + ${#f})) ]; then | |||||
| r= | |||||
| fi | |||||
| m="$r" | |||||
| matched=false | |||||
| case "$keyword" in | |||||
| $k) | |||||
| case "$value" in | |||||
| $f) | |||||
| matched=true | |||||
| ;; | |||||
| esac | |||||
| ;; | |||||
| esac | |||||
| if ! $matched; then | |||||
| keep="$keep${keep:+/}$k/$f" | |||||
| fi | |||||
| done | |||||
| if [ -n "$om" ] && [ -z "$keep" ]; then | |||||
| retval=0 | |||||
| break 2 | |||||
| fi | |||||
| new_match="${new_match}${new_match:+ }${keep}" | |||||
| done | |||||
| match="${new_match}" | |||||
| done < "$file" | |||||
| return $retval | |||||
| } | |||||
| list_keys() { | |||||
| list_cmd="$1" | |||||
| shift | shift | ||||
| excl=false | |||||
| [ -d "$KEYDIR" ] || return 0 | |||||
| cd "$KEYDIR" | |||||
| [ -n "$1" ] || set -- "*" | |||||
| list= | list= | ||||
| report=false | |||||
| retval=0 | retval=0 | ||||
| if [ "$list_cmd" = -i ] || [ "$list_cmd" = -l ]; then | |||||
| for i in $@; do | |||||
| if [ ! -f "$i" ]; then | |||||
| if ! $force && [ "$i" != "*" ]; then | |||||
| echo "No resolv.conf for key $i" >&2 | |||||
| fi | |||||
| retval=2 | |||||
| continue | |||||
| fi | |||||
| list="$list $i" | |||||
| done | |||||
| [ -z "$list" ] || uniqify $list | |||||
| return $retval | |||||
| fi | |||||
| case "$IF_EXCLUSIVE" in | if [ "$list_cmd" != -I ] && [ "$list_cmd" != -L ]; then | ||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | echo "list_keys: unknown command $list_cmd" >&2 | ||||
| excl=true | return 1 | ||||
| fi | |||||
| if [ -d "$EXCLUSIVEDIR" ]; then | if [ -d "$EXCLUSIVEDIR" ]; then | ||||
| cd "$EXCLUSIVEDIR" | cd "$EXCLUSIVEDIR" | ||||
| for i in *; do | for i in $EXCLUSIVEDIR/*; do | ||||
| if [ -f "$i" ]; then | if [ -f "$i" ]; then | ||||
| cd "$KEYDIR" | |||||
| for ii in $inclusive_keys; do | |||||
| if [ -f "$ii" ] && [ "${i#* }" = "$ii" ]; then | |||||
| continue 2 | |||||
| fi | |||||
| done | |||||
| list="${i#* }" | list="${i#* }" | ||||
| break | break | ||||
| fi | fi | ||||
| done | done | ||||
| cd "$KEYDIR" | |||||
| if [ -n "$list" ]; then | |||||
| for i in $@; do | |||||
| # list will be one item due to the above | |||||
| if [ -f "$i" ] && [ "$i" = "$list" ]; then | |||||
| echo "$i" | |||||
| return 0 | |||||
| fi | fi | ||||
| cd "$IFACEDIR" | |||||
| for i in $inclusive_interfaces; do | |||||
| if [ -f "$i" ] && [ "$list" = "$i" ]; then | |||||
| list= | |||||
| excl=false | |||||
| break | |||||
| fi | |||||
| done | done | ||||
| ;; | return 0 | ||||
| esac | fi | ||||
| fi | |||||
| # If we have an interface ordering list, then use that. | for i in $key_order; do | ||||
| # It works by just using pathname expansion in the interface directory. | for ii in "$i" "$i":* "$i".*; do | ||||
| if [ -n "$1" ]; then | |||||
| list="$*" | |||||
| $force || report=true | |||||
| elif ! $excl; then | |||||
| cd "$IFACEDIR" | |||||
| for i in $interface_order; do | |||||
| [ -f "$i" ] && list="$list $i" | |||||
| for ii in "$i":* "$i".*; do | |||||
| [ -f "$ii" ] && list="$list $ii" | [ -f "$ii" ] && list="$list $ii" | ||||
| done | done | ||||
| done | done | ||||
| for i in $dynamic_order; do | for i in $dynamic_order; do | ||||
| if [ -e "$i" ] && ! [ -e "$METRICDIR/"*" $i" ]; then | for ii in "$i" "$i":* "$i".*; do | ||||
| list="$list $i" | |||||
| fi | |||||
| for ii in "$i":* "$i".*; do | |||||
| if [ -f "$ii" ] && ! [ -e "$METRICDIR/"*" $ii" ] | if [ -f "$ii" ] && ! [ -e "$METRICDIR/"*" $ii" ] | ||||
| then | then | ||||
| list="$list $ii" | list="$list $ii" | ||||
| fi | fi | ||||
| done | done | ||||
| done | done | ||||
| # Interfaces have an implicit metric of 0 if not specified. | # Interfaces have an implicit metric of 0 if not specified. | ||||
| for i in *; do | for i in *; do | ||||
| if [ -f "$i" ] && ! [ -e "$METRICDIR/"*" $i" ]; then | if [ -f "$i" ] && ! [ -e "$METRICDIR/"*" $i" ]; then | ||||
| list="$list $i" | list="$list $i" | ||||
| fi | fi | ||||
| done | done | ||||
| if [ -d "$METRICDIR" ]; then | if [ -d "$METRICDIR" ]; then | ||||
| cd "$METRICDIR" | cd "$METRICDIR" | ||||
| for i in *; do | for i in *; do | ||||
| [ -f "$i" ] && list="$list ${i#* }" | [ -f "$i" ] && list="$list ${i#* }" | ||||
| done | done | ||||
| cd "$KEYDIR" | |||||
| fi | fi | ||||
| # Move deprecated keys to the back | |||||
| active= | |||||
| deprecated= | |||||
| for i in $list; do | |||||
| if deprecated_key "$i"; then | |||||
| deprecated="$deprecated $i" | |||||
| else | |||||
| active="$active $i" | |||||
| fi | fi | ||||
| done | |||||
| list="$active $deprecated" | |||||
| cd "$IFACEDIR" | retval=0 | ||||
| retval=1 | if [ "$1" != "*" ]; then | ||||
| for i in $(uniqify $list); do | cd "$KEYDIR" | ||||
| # Only list interfaces which we really have | matched= | ||||
| for i in $@; do | |||||
| if ! [ -f "$i" ]; then | if ! [ -f "$i" ]; then | ||||
| if $report; then | if ! $force; then | ||||
| echo "No resolv.conf for interface $i" >&2 | echo "No resolv.conf for key $i" >&2 | ||||
| fi | |||||
| retval=2 | retval=2 | ||||
| continue | |||||
| fi | fi | ||||
| for ii in $list; do | |||||
| if [ "$i" = "$ii" ]; then | |||||
| matched="$matched${matched:+ }$i" | |||||
| break | |||||
| fi | |||||
| done | |||||
| done | |||||
| if [ -z "$matched" ]; then | |||||
| return $retval | |||||
| fi | |||||
| list="$matched" | |||||
| fi | |||||
| allowed= | |||||
| for i in $(uniqify $list); do | |||||
| if [ -n "$allow_keys" ]; then | |||||
| x=false | |||||
| for ii in $allow_keys; do | |||||
| if [ "$i" = "$ii" ]; then | |||||
| x=true | |||||
| break | |||||
| fi | |||||
| done | |||||
| $x || continue | |||||
| fi | |||||
| for ii in $deny_keys; do | |||||
| if [ "$i" = "$ii" ]; then | |||||
| continue 2 | |||||
| fi | |||||
| done | |||||
| if [ -n "$exclude" ] && match "$exclude" "$i"; then | |||||
| continue | continue | ||||
| fi | fi | ||||
| allowed="$allowed${allowed:+ }$i" | |||||
| done | |||||
| if [ "$cmd" = i ] || [ "$cmd" = "-i" ]; then | cd "$KEYDIR" | ||||
| printf %s "$i " | for i in $exclusive_keys; do | ||||
| else | for ii in $allowed; do | ||||
| echo_resolv "$i" && echo | if [ "$i" = "$ii" ]; then | ||||
| echo "$i" | |||||
| return | |||||
| fi | fi | ||||
| [ $? = 0 ] && [ "$retval" = 1 ] && retval=0 | |||||
| done | done | ||||
| [ "$cmd" = i ] || [ "$cmd" = "-i" ] && echo | done | ||||
| [ -z "$allowed" ] || echo "$allowed" | |||||
| } | |||||
| list_resolv() | |||||
| { | |||||
| keys="$(list_keys "$@")" | |||||
| retval=$? | |||||
| if [ "$retval" != 0 ]; then | |||||
| return $retval | return $retval | ||||
| fi | |||||
| for i in $keys; do | |||||
| echo_resolv "$i" && echo | |||||
| done | |||||
| } | } | ||||
| list_private() | |||||
| { | |||||
| KEYS= | |||||
| cd "$KEYDIR" | |||||
| if [ -z "$1" ]; then | |||||
| set -- "*" | |||||
| fi | |||||
| for i in $@; do | |||||
| if private_key "$i"; then | |||||
| KEYS="${KEYS}${KEYS:+ }$i" | |||||
| fi | |||||
| done | |||||
| if [ -n "$KEYS" ]; then | |||||
| echo "$KEYS" | |||||
| fi | |||||
| } | |||||
| list_nosearch() | |||||
| { | |||||
| KEYS= | |||||
| cd "$KEYDIR" | |||||
| if [ -z "$1" ]; then | |||||
| set -- "*" | |||||
| fi | |||||
| for i in $@; do | |||||
| if nosearch_key "$i"; then | |||||
| KEYS="${KEYS}${KEYS:+ }$i" | |||||
| fi | |||||
| done | |||||
| if [ -n "$KEYS" ]; then | |||||
| echo "$KEYS" | |||||
| fi | |||||
| } | |||||
| list_exclusive() | |||||
| { | |||||
| KEYS= | |||||
| cd "$KEYDIR" | |||||
| if [ -z "$1" ]; then | |||||
| set -- "*" | |||||
| fi | |||||
| for i in $@; do | |||||
| if exclusive_key "$i"; then | |||||
| KEYS="${KEYS}${KEYS:+ }$i" | |||||
| fi | |||||
| done | |||||
| if [ -n "$KEYS" ]; then | |||||
| echo "$KEYS" | |||||
| fi | |||||
| } | |||||
| list_remove() | list_remove() | ||||
| { | { | ||||
| [ -z "$2" ] && return 0 | [ -z "$2" ] && return 0 | ||||
| eval list=\"\$$1\" | eval list=\"\$$1\" | ||||
| shift | shift | ||||
| result= | result= | ||||
| retval=0 | retval=0 | ||||
| Show All 36 Lines | if [ -n "$search_domains_append" ]; then | ||||
| echo "search $search_domains_append" | echo "search $search_domains_append" | ||||
| fi | fi | ||||
| for n in $name_servers_append; do | for n in $name_servers_append; do | ||||
| echo "nameserver $n" | echo "nameserver $n" | ||||
| done | done | ||||
| echo | echo | ||||
| } | } | ||||
| replace() | tolower() { | ||||
| # There is no good way of doing this portably in shell :( | |||||
| # Luckily we are only doing this for domain names which we | |||||
| # know have to be ASCII. | |||||
| # Non ASCII domains *should* be translated to ASCII *before* | |||||
| # we get to this stage. | |||||
| # We could use echo "$@" | tr '[:upper:]' '[:lower:]' but | |||||
| # tr is in /usr/bin and may not be available when data is fed | |||||
| # to resolvconf. | |||||
| # So it's the cost of a pipe + fork vs this slow loop | |||||
| # | |||||
| for word; do | |||||
| # Check if we have any upper to avoid looping per char | |||||
| case "$word" in | |||||
| *[A-Z]*) ;; | |||||
| *) printf "%s " "$word"; continue;; | |||||
| esac | |||||
| while [ -n "$word" ]; do | |||||
| # Remove everything except the first character | |||||
| afterchar="${word#?}" | |||||
| # Remove the afterchar to get the first character | |||||
| char="${word%%$afterchar}" | |||||
| # Assign afterchar back to word for looping | |||||
| word="$afterchar" | |||||
| # Now enforce lowercase a-z | |||||
| case "$char" in | |||||
| A) char=a;; | |||||
| B) char=b;; | |||||
| C) char=c;; | |||||
| D) char=d;; | |||||
| E) char=e;; | |||||
| F) char=f;; | |||||
| G) char=g;; | |||||
| H) char=h;; | |||||
| I) char=i;; | |||||
| J) char=j;; | |||||
| K) char=k;; | |||||
| L) char=l;; | |||||
| M) char=m;; | |||||
| N) char=n;; | |||||
| O) char=o;; | |||||
| P) char=p;; | |||||
| Q) char=q;; | |||||
| R) char=r;; | |||||
| S) char=s;; | |||||
| T) char=t;; | |||||
| U) char=u;; | |||||
| V) char=v;; | |||||
| W) char=w;; | |||||
| X) char=x;; | |||||
| Y) char=y;; | |||||
| Z) char=z;; | |||||
| esac | |||||
| printf %s "$char" | |||||
| done | |||||
| printf " " | |||||
| done | |||||
| printf "\n" | |||||
| } | |||||
| # Strip any trailing dot from each name as a FQDN does not belong | |||||
| # in resolv.conf(5). | |||||
| # While DNS is not case sensitive, our labels for building the zones | |||||
| # are, so ensure it's lower case. | |||||
| process_domain() | |||||
| { | { | ||||
| for word in $(tolower "$@"); do | |||||
| printf "%s " "${word%.}" | |||||
| done | |||||
| printf "\n" | |||||
| } | |||||
| process_resolv() | |||||
| { | |||||
| while read -r keyword value; do | while read -r keyword value; do | ||||
| for r in $replace; do | for r in $replace; do | ||||
| k="${r%%/*}" | k="${r%%/*}" | ||||
| r="${r#*/}" | r="${r#*/}" | ||||
| f="${r%%/*}" | f="${r%%/*}" | ||||
| r="${r#*/}" | r="${r#*/}" | ||||
| v="${r%%/*}" | v="${r%%/*}" | ||||
| case "$keyword" in | case "$keyword" in | ||||
| Show All 17 Lines | for sub in $value; do | ||||
| case "$sub" in | case "$sub" in | ||||
| $f) sub="$v";; | $f) sub="$v";; | ||||
| esac | esac | ||||
| ;; | ;; | ||||
| esac | esac | ||||
| done | done | ||||
| val="$val${val:+ }$sub" | val="$val${val:+ }$sub" | ||||
| done | done | ||||
| case "$keyword" in | |||||
| \#) | |||||
| case "$val" in | |||||
| "resolv.conf from "*) ;; | |||||
| *) continue;; | |||||
| esac | |||||
| ;; | |||||
| \#*) continue;; | |||||
| esac | |||||
| case "$keyword" in | |||||
| domain|search) val="$(process_domain $val)";; | |||||
| esac | |||||
| printf "%s %s\n" "$keyword" "$val" | printf "%s %s\n" "$keyword" "$val" | ||||
| done | done | ||||
| } | } | ||||
| make_vars() | make_vars() | ||||
| { | { | ||||
| # Clear variables | # Clear variables | ||||
| DOMAIN= | DOMAIN= | ||||
| DOMAINS= | DOMAINS= | ||||
| SEARCH= | SEARCH= | ||||
| NAMESERVERS= | NAMESERVERS= | ||||
| LOCALNAMESERVERS= | LOCALNAMESERVERS= | ||||
| if [ -n "${name_servers}${search_domains}" ]; then | if [ -n "${name_servers}${search_domains}" ]; then | ||||
| eval "$(echo_prepend | parse_resolv)" | eval "$(echo_prepend | parse_resolv)" | ||||
| fi | fi | ||||
| if [ -z "$VFLAG" ]; then | if [ -z "$VFLAG" ]; then | ||||
| IF_EXCLUSIVE=1 | eval "$(list_resolv -L "$@" | process_resolv | parse_resolv)" | ||||
| list_resolv -i "$@" >/dev/null || IF_EXCLUSIVE=0 | |||||
| eval "$(list_resolv -l "$@" | replace | parse_resolv)" | |||||
| fi | fi | ||||
| if [ -n "${name_servers_append}${search_domains_append}" ]; then | if [ -n "${name_servers_append}${search_domains_append}" ]; then | ||||
| eval "$(echo_append | parse_resolv)" | eval "$(echo_append | parse_resolv)" | ||||
| fi | fi | ||||
| # Ensure that we only list each domain once | # Ensure that we only list each domain once | ||||
| newdomains= | newdomains= | ||||
| for d in $DOMAINS; do | for d in $DOMAINS; do | ||||
| Show All 17 Lines | for nd in $DOMAINS; do | ||||
| ns="${ns#*,}" | ns="${ns#*,}" | ||||
| done | done | ||||
| fi | fi | ||||
| done | done | ||||
| if [ -n "$newns" ]; then | if [ -n "$newns" ]; then | ||||
| newdomains="$newdomains${newdomains:+ }$dn:$newns" | newdomains="$newdomains${newdomains:+ }$dn:$newns" | ||||
| fi | fi | ||||
| done | done | ||||
| DOMAIN="$(list_remove domain_blacklist $DOMAIN)" | DOMAIN="$(list_remove domain_blacklist $DOMAIN)" | ||||
| SEARCH="$(uniqify $SEARCH)" | SEARCH="$(uniqify $SEARCH)" | ||||
| SEARCH="$(list_remove domain_blacklist $SEARCH)" | SEARCH="$(list_remove domain_blacklist $SEARCH)" | ||||
| NAMESERVERS="$(uniqify $NAMESERVERS)" | NAMESERVERS="$(uniqify $NAMESERVERS)" | ||||
| NAMESERVERS="$(list_remove name_server_blacklist $NAMESERVERS)" | NAMESERVERS="$(list_remove name_server_blacklist $NAMESERVERS)" | ||||
| LOCALNAMESERVERS="$(uniqify $LOCALNAMESERVERS)" | LOCALNAMESERVERS="$(uniqify $LOCALNAMESERVERS)" | ||||
| LOCALNAMESERVERS="$(list_remove name_server_blacklist $LOCALNAMESERVERS)" | LOCALNAMESERVERS="$(list_remove name_server_blacklist $LOCALNAMESERVERS)" | ||||
| echo "DOMAIN='$DOMAIN'" | |||||
| echo "SEARCH='$SEARCH'" | # Ensure output is quoted for eval | ||||
| echo "NAMESERVERS='$NAMESERVERS'" | printf 'DOMAIN=%s\n' "$(quote "$DOMAIN")" | ||||
| echo "LOCALNAMESERVERS='$LOCALNAMESERVERS'" | printf 'SEARCH=%s\n' "$(quote "$SEARCH")" | ||||
| echo "DOMAINS='$newdomains'" | printf 'NAMESERVERS=%s\n' "$(quote "$NAMESERVERS")" | ||||
| printf 'LOCALNAMESERVERS=%s\n' "$(quote "$LOCALNAMESERVERS")" | |||||
| printf 'DOMAINS=%s\n' "$(quote "$newdomains")" | |||||
| } | } | ||||
| force=false | force=false | ||||
| LFLAG= | |||||
| VFLAG= | VFLAG= | ||||
| while getopts a:Dd:fhIilm:pRruvVx OPT; do | while getopts a:C:c:Dd:fhIiLlm:pRruvVx OPT; do | ||||
| case "$OPT" in | case "$OPT" in | ||||
| f) force=true;; | f) force=true;; | ||||
| h) usage;; | h) usage;; | ||||
| m) IF_METRIC="$OPTARG";; | m) IF_METRIC="$OPTARG";; | ||||
| p) IF_PRIVATE=1;; | p) | ||||
| if [ "$IF_PRIVATE" = 1 ]; then | |||||
| IF_NOSEARCH=1 | |||||
| else | |||||
| IF_PRIVATE=1 | |||||
| fi | |||||
| ;; | |||||
| V) | V) | ||||
| VFLAG=1 | VFLAG=1 | ||||
| if [ "$local_nameservers" = \ | if [ "$local_nameservers" = \ | ||||
| "127.* 0.0.0.0 255.255.255.255 ::1" ] | "127.* 0.0.0.0 255.255.255.255 ::1" ] | ||||
| then | then | ||||
| local_nameservers= | local_nameservers= | ||||
| fi | fi | ||||
| ;; | ;; | ||||
| x) IF_EXCLUSIVE=1;; | x) IF_EXCLUSIVE=1;; | ||||
| '?') ;; | '?') exit 1;; | ||||
| *) cmd="$OPT"; iface="$OPTARG";; | *) | ||||
| [ "$OPT" != L ] || LFLAG=1 | |||||
| cmd="$OPT"; key="$OPTARG";; | |||||
| esac | esac | ||||
| done | done | ||||
| shift $(($OPTIND - 1)) | shift $(($OPTIND - 1)) | ||||
| args="$iface${iface:+ }$*" | if [ -n "$key" ]; then | ||||
| set -- "$key" "$@" | |||||
| fi | |||||
| # -I inits the state dir | if [ -z "$cmd" ]; then | ||||
| if [ "$cmd" = I ]; then | if [ "$IF_PRIVATE" = 1 ]; then | ||||
| if [ -d "$VARDIR" ]; then | cmd=p | ||||
| rm -rf "$VARDIR"/* | elif [ "$IF_EXCLUSIVE" = 1 ]; then | ||||
| cmd=x | |||||
| fi | fi | ||||
| exit $? | |||||
| fi | fi | ||||
| # -D ensures that the listed config file base dirs exist | # -D ensures that the listed config file base dirs exist | ||||
| if [ "$cmd" = D ]; then | if [ "$cmd" = D ]; then | ||||
| config_mkdirs "$@" | config_mkdirs "$@" | ||||
| exit $? | exit $? | ||||
| fi | fi | ||||
| # -l lists our resolv files, optionally for a specific interface | # -i lists which keys have a resolv file | ||||
| if [ "$cmd" = l ] || [ "$cmd" = i ]; then | if [ "$cmd" = i ]; then | ||||
| list_resolv "$cmd" "$args" | # If the -L modifier is given, the list is post-processed | ||||
| if [ "$LFLAG" = 1 ]; then | |||||
| cmd="L" | |||||
| fi | |||||
| list_keys "-$cmd" "$@" | |||||
| exit $? | exit $? | ||||
| fi | fi | ||||
| # -l lists our resolv files, optionally for a specific key | |||||
| if [ "$cmd" = l ]; then | |||||
| list_resolv "-$cmd" "$@" | |||||
| exit $? | |||||
| fi | |||||
| # -L is the same as -l, but post-processed from our config | |||||
| if [ "$cmd" = L ]; then | |||||
| list_resolv "-$cmd" "$@" | process_resolv | |||||
| exit $? | |||||
| fi | |||||
| if [ "$cmd" = p ]; then | |||||
| if [ "$IF_NOSEARCH" = 1 ]; then | |||||
| list_nosearch "$@" | |||||
| else | |||||
| list_private "$@" | |||||
| fi | |||||
| exit $? | |||||
| fi | |||||
| if [ "$cmd" = x ]; then | |||||
| list_exclusive "$@" | |||||
| exit $? | |||||
| fi | |||||
| # Restart a service or echo the command to restart a service | # Restart a service or echo the command to restart a service | ||||
| if [ "$cmd" = r ] || [ "$cmd" = R ]; then | if [ "$cmd" = r ] || [ "$cmd" = R ]; then | ||||
| detect_init || exit 1 | detect_init || exit 1 | ||||
| if [ "$cmd" = r ]; then | if [ "$cmd" = r ]; then | ||||
| set -- $args | |||||
| eval "$RESTARTCMD" | eval "$RESTARTCMD" | ||||
| else | else | ||||
| echo "$RESTARTCMD" | | echo "$RESTARTCMD" | | ||||
| sed -e '/^$/d' -e 's/^ //g' | sed -e '/^$/d' -e 's/^ //g' | ||||
| fi | fi | ||||
| exit $? | exit $? | ||||
| fi | fi | ||||
| # Not normally needed, but subscribers should be able to run independently | # Not normally needed, but subscribers should be able to run independently | ||||
| if [ "$cmd" = v ] || [ -n "$VFLAG" ]; then | if [ "$cmd" = v ] || [ -n "$VFLAG" ]; then | ||||
| make_vars "$iface" | make_vars "$@" | ||||
| exit $? | exit $? | ||||
| fi | fi | ||||
| # Test that we have valid options | # Test that we have valid options | ||||
| if [ "$cmd" = a ] || [ "$cmd" = d ]; then | case "$cmd" in | ||||
| if [ -z "$iface" ]; then | a|d|C|c) | ||||
| usage "Interface not specified" | if [ -z "$key" ]; then | ||||
| error_exit "Key not specified" | |||||
| fi | fi | ||||
| elif [ "$cmd" != u ]; then | ;; | ||||
| [ -n "$cmd" ] && [ "$cmd" != h ] && usage "Unknown option $cmd" | I|u) ;; | ||||
| usage | *) | ||||
| if [ -n "$cmd" ] && [ "$cmd" != h ]; then | |||||
| error_exit "Unknown option $cmd" | |||||
| fi | fi | ||||
| usage | |||||
| ;; | |||||
| esac | |||||
| if [ "$cmd" = a ]; then | if [ "$cmd" = a ]; then | ||||
| for x in '/' \\ ' ' '*'; do | for x in '/' '\' ' ' '*'; do | ||||
| case "$iface" in | case "$key" in | ||||
| *[$x]*) error_exit "$x not allowed in interface name";; | "$x"|"$x"*|*"$x"|*"$x"*) error_exit "$x not allowed in key name";; | ||||
| esac | esac | ||||
| done | done | ||||
| for x in '.' '-' '~'; do | for x in '.' '-' '~'; do | ||||
| case "$iface" in | case "$key" in | ||||
| [$x]*) error_exit \ | "$x"*) error_exit \ | ||||
| "$x not allowed at start of interface name";; | "$x not allowed at start of key name";; | ||||
| esac | esac | ||||
| done | done | ||||
| [ "$cmd" = a ] && [ -t 0 ] && error_exit "No file given via stdin" | [ "$cmd" = a ] && [ -t 0 ] && error_exit "No file given via stdin" | ||||
| fi | fi | ||||
| if [ ! -d "$VARDIR" ]; then | if [ ! -d "$VARDIR" ]; then | ||||
| if [ -L "$VARDIR" ]; then | if [ -L "$VARDIR" ]; then | ||||
| dir="$(readlink "$VARDIR")" | dir="$(readlink "$VARDIR")" | ||||
| # link maybe relative | # link maybe relative | ||||
| cd "${VARDIR%/*}" | cd "${VARDIR%/*}" | ||||
| if ! mkdir -m 0755 -p "$dir"; then | if ! mkdir -m 0755 -p "$dir"; then | ||||
| error_exit "Failed to create needed" \ | error_exit "Failed to create needed" \ | ||||
| "directory $dir" | "directory $dir" | ||||
| fi | fi | ||||
| else | else | ||||
| if ! mkdir -m 0755 -p "$VARDIR"; then | if ! mkdir -m 0755 -p "$VARDIR"; then | ||||
| error_exit "Failed to create needed" \ | error_exit "Failed to create needed" \ | ||||
| "directory $VARDIR" | "directory $VARDIR" | ||||
| fi | fi | ||||
| fi | fi | ||||
| fi | fi | ||||
| if [ ! -d "$IFACEDIR" ]; then | if [ ! -d "$KEYDIR" ]; then | ||||
| mkdir -m 0755 -p "$IFACEDIR" || \ | mkdir -m 0755 -p "$KEYDIR" || \ | ||||
| error_exit "Failed to create needed directory $IFACEDIR" | error_exit "Failed to create needed directory $KEYDIR" | ||||
| if [ "$cmd" = d ]; then | if [ "$cmd" = d ]; then | ||||
| # Provide the same error messages as below | # Provide the same error messages as below | ||||
| if ! ${force}; then | if ! ${force}; then | ||||
| cd "$IFACEDIR" | cd "$KEYDIR" | ||||
| for i in $args; do | for i in $@; do | ||||
| warn "No resolv.conf for interface $i" | warn "No resolv.conf for key $i" | ||||
| done | done | ||||
| fi | fi | ||||
| ${force} | ${force} | ||||
| exit $? | exit $? | ||||
| fi | fi | ||||
| fi | fi | ||||
| # An interface was added, changed, deleted or a general update was called. | # A key was added, changed, deleted or a general update was called. | ||||
| # Due to exclusivity we need to ensure that this is an atomic operation. | # Due to exclusivity we need to ensure that this is an atomic operation. | ||||
| # Our subscribers *may* need this as well if the init system is sub par. | # Our subscribers *may* need this as well if the init system is sub par. | ||||
| # As such we spinlock at this point as best we can. | # As such we spinlock at this point as best we can. | ||||
| # We don't use flock(1) because it's not widely available and normally resides | # We don't use flock(1) because it's not widely available and normally resides | ||||
| # in /usr which we do our very best to operate without. | # in /usr which we do our very best to operate without. | ||||
| [ -w "$VARDIR" ] || error_exit "Cannot write to $LOCKDIR" | [ -w "$VARDIR" ] || error_exit "Cannot write to $LOCKDIR" | ||||
| : ${lock_timeout:=10} | : ${lock_timeout:=10} | ||||
| : ${clear_nopids:=5} | |||||
| have_pid=false | |||||
| had_pid=false | |||||
| while true; do | while true; do | ||||
| if mkdir "$LOCKDIR" 2>/dev/null; then | if mkdir "$LOCKDIR" 2>/dev/null; then | ||||
| trap 'rm -rf "$LOCKDIR";' EXIT | trap 'rm -rf "$LOCKDIR";' EXIT | ||||
| trap 'rm -rf "$LOCKDIR"; exit 1' INT QUIT ABRT SEGV ALRM TERM | trap 'rm -rf "$LOCKDIR"; exit 1' INT QUIT ABRT SEGV ALRM TERM | ||||
| echo $$ >"$LOCKDIR/pid" | echo $$ >"$LOCKDIR/pid" | ||||
| break | break | ||||
| fi | fi | ||||
| pid=$(cat "$LOCKDIR/pid") | pid=$(cat "$LOCKDIR/pid" 2>/dev/null) | ||||
| if ! kill -0 "$pid"; then | if [ "$pid" -gt 0 ] 2>/dev/null; then | ||||
| have_pid=true | |||||
| had_pid=true | |||||
| else | |||||
| have_pid=false | |||||
| clear_nopids=$(($clear_nopids - 1)) | |||||
| if [ "$clear_nopids" -le 0 ]; then | |||||
| warn "not seen a pid, clearing lock directory" | |||||
| rm -rf "$LOCKDIR" | |||||
| else | |||||
| lock_timeout=$(($lock_timeout - 1)) | |||||
| sleep 1 | |||||
| fi | |||||
| continue | |||||
| fi | |||||
| if $have_pid && ! kill -0 "$pid"; then | |||||
| warn "clearing stale lock pid $pid" | warn "clearing stale lock pid $pid" | ||||
| rm -rf "$LOCKDIR" | rm -rf "$LOCKDIR" | ||||
| continue | continue | ||||
| fi | fi | ||||
| lock_timeout=$(($lock_timeout - 1)) | lock_timeout=$(($lock_timeout - 1)) | ||||
| if [ "$lock_timeout" -le 0 ]; then | if [ "$lock_timeout" -le 0 ]; then | ||||
| if $have_pid; then | |||||
| error_exit "timed out waiting for lock from pid $pid" | error_exit "timed out waiting for lock from pid $pid" | ||||
| else | |||||
| if $had_pid; then | |||||
| error_exit "timed out waiting for lock" \ | |||||
| "from some pids" | |||||
| else | |||||
| error_exit "timed out waiting for lock" | |||||
| fi | fi | ||||
| fi | |||||
| fi | |||||
| sleep 1 | sleep 1 | ||||
| done | done | ||||
| unset have_pid had_pid clear_nopids | |||||
| case "$cmd" in | case "$cmd" in | ||||
| a) | a) | ||||
| # Read resolv.conf from stdin | # Read resolv.conf from stdin | ||||
| resolv="$(cat)" | resolv="$(cat)" | ||||
| changed=false | changed=false | ||||
| changedfile=false | changedfile=false | ||||
| # If what we are given matches what we have, then do nothing | # If what we are given matches what we have, then do nothing | ||||
| if [ -e "$IFACEDIR/$iface" ]; then | if [ -e "$KEYDIR/$key" ]; then | ||||
| if [ "$(echo "$resolv")" != \ | if [ "$(echo "$resolv")" != \ | ||||
| "$(cat "$IFACEDIR/$iface")" ] | "$(cat "$KEYDIR/$key")" ] | ||||
| then | then | ||||
| changed=true | changed=true | ||||
| changedfile=true | changedfile=true | ||||
| fi | fi | ||||
| else | else | ||||
| changed=true | changed=true | ||||
| changedfile=true | changedfile=true | ||||
| fi | fi | ||||
| # Set metric and private before creating the interface resolv.conf file | # Set metric and private before creating the resolv.conf file | ||||
| # to ensure that it will have the correct flags | # to ensure that it will have the correct flags | ||||
| [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR" | [ ! -d "$METRICDIR" ] && mkdir "$METRICDIR" | ||||
| oldmetric="$METRICDIR/"*" $iface" | oldmetric="$METRICDIR/"*" $key" | ||||
| newmetric= | newmetric= | ||||
| if [ -n "$IF_METRIC" ]; then | if [ -n "$IF_METRIC" ]; then | ||||
| # Pad metric to 6 characters, so 5 is less than 10 | # Pad metric to 6 characters, so 5 is less than 10 | ||||
| while [ ${#IF_METRIC} -le 6 ]; do | while [ ${#IF_METRIC} -le 6 ]; do | ||||
| IF_METRIC="0$IF_METRIC" | IF_METRIC="0$IF_METRIC" | ||||
| done | done | ||||
| newmetric="$METRICDIR/$IF_METRIC $iface" | newmetric="$METRICDIR/$IF_METRIC $key" | ||||
| fi | fi | ||||
| rm -f "$METRICDIR/"*" $iface" | rm -f "$METRICDIR/"*" $key" | ||||
| [ "$oldmetric" != "$newmetric" ] && | [ "$oldmetric" != "$newmetric" ] && | ||||
| [ "$oldmetric" != "$METRICDIR/* $iface" ] && | [ "$oldmetric" != "$METRICDIR/* $key" ] && | ||||
| changed=true | changed=true | ||||
| [ -n "$newmetric" ] && echo " " >"$newmetric" | [ -n "$newmetric" ] && echo " " >"$newmetric" | ||||
| case "$IF_PRIVATE" in | case "$IF_PRIVATE" in | ||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | ||||
| if [ ! -d "$PRIVATEDIR" ]; then | if [ ! -d "$PRIVATEDIR" ]; then | ||||
| [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR" | [ -e "$PRIVATEDIR" ] && rm "$PRIVATEDIR" | ||||
| mkdir "$PRIVATEDIR" | mkdir "$PRIVATEDIR" | ||||
| fi | fi | ||||
| [ -e "$PRIVATEDIR/$iface" ] || changed=true | [ -e "$PRIVATEDIR/$key" ] || changed=true | ||||
| [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$iface" | [ -d "$PRIVATEDIR" ] && echo " " >"$PRIVATEDIR/$key" | ||||
| ;; | ;; | ||||
| *) | *) | ||||
| if [ -e "$PRIVATEDIR/$iface" ]; then | if [ -e "$PRIVATEDIR/$key" ]; then | ||||
| rm -f "$PRIVATEDIR/$iface" | rm -f "$PRIVATEDIR/$key" | ||||
| changed=true | changed=true | ||||
| fi | fi | ||||
| ;; | ;; | ||||
| esac | esac | ||||
| case "$IF_NOSEARCH" in | |||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | |||||
| if [ ! -d "$NOSEARCHDIR" ]; then | |||||
| [ -e "$NOSEARCHDIR" ] && rm "$NOSEARCHDIR" | |||||
| mkdir "$NOSEARCHDIR" | |||||
| fi | |||||
| [ -e "$NOSEARCHDIR/$key" ] || changed=true | |||||
| [ -d "$NOSEARCHDIR" ] && echo " " >"$NOSEARCHDIR/$key" | |||||
| ;; | |||||
| *) | |||||
| if [ -e "$NOSEARCHDIR/$key" ]; then | |||||
| rm -f "$NOSEARCHDIR/$key" | |||||
| changed=true | |||||
| fi | |||||
| ;; | |||||
| esac | |||||
| set +x | |||||
| oldexcl= | oldexcl= | ||||
| for x in "$EXCLUSIVEDIR/"*" $iface"; do | for x in "$EXCLUSIVEDIR/"*" $key"; do | ||||
| if [ -f "$x" ]; then | if [ -f "$x" ]; then | ||||
| oldexcl="$x" | oldexcl="$x" | ||||
| break | break | ||||
| fi | fi | ||||
| done | done | ||||
| case "$IF_EXCLUSIVE" in | case "$IF_EXCLUSIVE" in | ||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | ||||
| if [ ! -d "$EXCLUSIVEDIR" ]; then | if [ ! -d "$EXCLUSIVEDIR" ]; then | ||||
| [ -e "$EXCLUSIVEDIR" ] && rm "$EXCLUSIVEDIR" | [ -e "$EXCLUSIVEDIR" ] && rm "$EXCLUSIVEDIR" | ||||
| mkdir "$EXCLUSIVEDIR" | mkdir "$EXCLUSIVEDIR" | ||||
| fi | fi | ||||
| cd "$EXCLUSIVEDIR" | cd "$EXCLUSIVEDIR" | ||||
| for x in *; do | for x in *; do | ||||
| [ -f "$x" ] && break | [ -f "$x" ] && break | ||||
| done | done | ||||
| if [ "${x#* }" != "$iface" ]; then | if [ "${x#* }" != "$key" ]; then | ||||
| if [ "$x" = "${x% *}" ]; then | if [ "$x" = "${x% *}" ]; then | ||||
| x=10000000 | x=10000000 | ||||
| else | else | ||||
| x="${x% *}" | x="${x% *}" | ||||
| fi | fi | ||||
| if [ "$x" = "0000000" ]; then | if [ "$x" = "0000000" ]; then | ||||
| warn "exclusive underflow" | warn "exclusive underflow" | ||||
| else | else | ||||
| x=$(($x - 1)) | x=$(($x - 1)) | ||||
| fi | fi | ||||
| if [ -d "$EXCLUSIVEDIR" ]; then | if [ -d "$EXCLUSIVEDIR" ]; then | ||||
| echo " " >"$EXCLUSIVEDIR/$x $iface" | echo " " >"$EXCLUSIVEDIR/$x $key" | ||||
| fi | fi | ||||
| changed=true | changed=true | ||||
| fi | fi | ||||
| ;; | ;; | ||||
| *) | *) | ||||
| if [ -f "$oldexcl" ]; then | if [ -f "$oldexcl" ]; then | ||||
| rm -f "$oldexcl" | rm -f "$oldexcl" | ||||
| changed=true | changed=true | ||||
| fi | fi | ||||
| ;; | ;; | ||||
| esac | esac | ||||
| if $changedfile; then | if $changedfile; then | ||||
| printf "%s\n" "$resolv" >"$IFACEDIR/$iface" || exit $? | printf "%s\n" "$resolv" >"$KEYDIR/$key" || exit $? | ||||
| elif ! $changed; then | elif ! $changed && [ ! -e "$VARDIR"/error ]; then | ||||
| exit 0 | exit 0 | ||||
| fi | fi | ||||
| unset changed changedfile oldmetric newmetric x oldexcl | unset changed changedfile oldmetric newmetric x oldexcl | ||||
| ;; | ;; | ||||
| d) | d) | ||||
| # Delete any existing information about the interface | # Delete any existing information about the key | ||||
| cd "$IFACEDIR" | cd "$KEYDIR" | ||||
| changed=false | changed=false | ||||
| for i in $args; do | for i in $@; do | ||||
| if [ -e "$i" ]; then | if [ -e "$i" ]; then | ||||
| changed=true | changed=true | ||||
| elif ! ${force}; then | elif ! ${force}; then | ||||
| warn "No resolv.conf for interface $i" | warn "No resolv.conf for key $i" | ||||
| fi | fi | ||||
| rm -f "$i" "$METRICDIR/"*" $i" \ | rm -f "$i" "$METRICDIR/"*" $i" \ | ||||
| "$PRIVATEDIR/$i" \ | "$PRIVATEDIR/$i" \ | ||||
| "$EXCLUSIVEDIR/"*" $i" || exit $? | "$EXCLUSIVEDIR/"*" $i" || exit $? | ||||
| done | done | ||||
| if ! ${changed}; then | |||||
| if ! $changed && [ ! -e "$VARDIR"/error ]; then | |||||
| # Set the return code based on the forced flag | # Set the return code based on the forced flag | ||||
| ${force} | $force | ||||
| exit $? | exit $? | ||||
| fi | fi | ||||
| unset changed i | unset changed i | ||||
| ;; | ;; | ||||
| C) | |||||
| # Mark key as deprecated | |||||
| [ ! -d "$DEPRECATEDDIR" ] && mkdir "$DEPRECATEDDIR" | |||||
| cd "$DEPRECATEDDIR" | |||||
| changed=false | |||||
| for i in $@; do | |||||
| if [ ! -e "$i" ]; then | |||||
| changed=true | |||||
| echo " " >"$i" || exit $? | |||||
| fi | |||||
| done | |||||
| if ! $changed && [ ! -e "$VARDIR"/error ]; then | |||||
| exit 0 | |||||
| fi | |||||
| unset changed i | |||||
| ;; | |||||
| c) | |||||
| # Mark key as active | |||||
| if [ -d "$DEPRECATEDDIR" ]; then | |||||
| cd "$DEPRECATEDDIR" | |||||
| changed=false | |||||
| for i in $@; do | |||||
| if [ -e "$i" ]; then | |||||
| changed=true | |||||
| rm "$i" || exit $? | |||||
| fi | |||||
| done | |||||
| if ! $changed && [ ! -e "$VARDIR"/error ]; then | |||||
| exit 0 | |||||
| fi | |||||
| unset changed i | |||||
| fi | |||||
| ;; | |||||
| I) | |||||
| # Init the state dir, keeping our lock and key directories only | |||||
| for i in "$VARDIR"/*; do | |||||
| case "$i" in | |||||
| "$LOCKDIR") ;; | |||||
| "$KEYDIR") rm -rf "$KEYDIR"/*;; | |||||
| *) rm -rf "$i";; | |||||
| esac | esac | ||||
| done | |||||
| ;; | |||||
| esac | |||||
| case "${resolvconf:-YES}" in | case "${resolvconf:-YES}" in | ||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;; | [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;; | ||||
| *) exit 0;; | *) exit 0;; | ||||
| esac | esac | ||||
| # Try and detect a suitable init system for our scripts | # Try and detect a suitable init system for our scripts | ||||
| detect_init | detect_init | ||||
| export RESTARTCMD RCDIR _NOINIT_WARNED | export RESTARTCMD RCDIR _NOINIT_WARNED | ||||
| eval "$(make_vars)" | eval "$(make_vars)" | ||||
| export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS | export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS | ||||
| : ${list_resolv:=list_resolv -l} | : ${list_resolv:=list_resolv -L} | ||||
| retval=0 | retval=0 | ||||
| # Run scripts in the same directory resolvconf is run from | # Run scripts in the same directory resolvconf is run from | ||||
| # in case any scripts accidentally dump files in the wrong place. | # in case any scripts accidentally dump files in the wrong place. | ||||
| cd "$_PWD" | cd "$_PWD" | ||||
| for script in "$LIBEXECDIR"/*; do | for script in "$LIBEXECDIR"/*; do | ||||
| if [ -f "$script" ]; then | if [ -f "$script" ]; then | ||||
| eval script_enabled="\$${script##*/}" | script_var="${script##*/}" | ||||
| while [ "${script_var%%-*}" != "$script_var" ]; do | |||||
| script_var="${script_var%%-*}_${script_var#*-}" | |||||
| done | |||||
| eval script_enabled="\$$script_var" | |||||
| case "${script_enabled:-YES}" in | case "${script_enabled:-YES}" in | ||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;; | [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) ;; | ||||
| *) continue;; | *) continue;; | ||||
| esac | esac | ||||
| if [ -x "$script" ]; then | if [ -x "$script" ]; then | ||||
| "$script" "$cmd" "$iface" | "$script" "$cmd" "$key" | ||||
| else | else | ||||
| (set -- "$cmd" "$iface"; . "$script") | (set -- "$cmd" "$key"; . "$script") | ||||
| fi | fi | ||||
| retval=$(($retval + $?)) | retval=$(($retval + $?)) | ||||
| fi | fi | ||||
| done | done | ||||
| if [ "$retval" = 0 ]; then | |||||
| rm -f "$VARDIR"/error | |||||
| else | |||||
| echo "$retval" >"$VARDIR"/error | |||||
| fi | |||||
| exit $retval | exit $retval | ||||