Changeset View
Changeset View
Standalone View
Standalone View
libc.in
| #!/bin/sh | #!/bin/sh | ||||
| # Copyright (c) 2007-2019 Roy Marples | # Copyright (c) 2007-2025 Roy Marples | ||||
| # All rights reserved | # All rights reserved | ||||
| # libc subscriber for resolvconf | # libc subscriber for resolvconf | ||||
| # 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 | ||||
| Show All 13 Lines | |||||
| # 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. | ||||
| SYSCONFDIR=@SYSCONFDIR@ | SYSCONFDIR=@SYSCONFDIR@ | ||||
| LIBEXECDIR=@LIBEXECDIR@ | LIBEXECDIR=@LIBEXECDIR@ | ||||
| VARDIR=@VARDIR@ | VARDIR=@VARDIR@ | ||||
| IFACEDIR="$VARDIR/interfaces" | KEYDIR="$VARDIR/keys" | ||||
| # Compat | |||||
| if [ ! -d "$KEYDIR" ] && [ -d "$VARDIR/interfaces" ]; then | |||||
| KEYDIR="$VARDIR/interfaces" | |||||
| fi | |||||
| CMD="$1" | |||||
| KEY="$2" | |||||
| NL=" | NL=" | ||||
| " | " | ||||
| warn() | |||||
| { | |||||
| echo "${0##*/}: $*" >&2 | |||||
| } | |||||
| # sed may not be available, and this is faster on small files | # sed may not be available, and this is faster on small files | ||||
| key_get_value() | key_get_value() | ||||
| { | { | ||||
| key="$1" | key="$1" | ||||
| shift | shift | ||||
| if [ $# -eq 0 ]; then | if [ $# -eq 0 ]; then | ||||
| while read -r line; do | while read -r line; do | ||||
| ▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | elif [ -d "$SYSCONFDIR"/resolvconf ]; then | ||||
| if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then | if [ -f "$SYSCONFDIR"/resolv.conf.d/head ]; then | ||||
| resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)" | resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)" | ||||
| fi | fi | ||||
| if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then | if [ -f "$SYSCONFDIR"/resolv.conf.d/tail ]; then | ||||
| resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)" | resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)" | ||||
| fi | fi | ||||
| fi | fi | ||||
| : ${resolv_conf:=/etc/resolv.conf} | : ${resolv_conf:=/etc/resolv.conf} | ||||
| if [ "$resolv_conf" = "/dev/null" ]; then | |||||
| exit 0 | |||||
| fi | |||||
| : ${resolv_conf_tmp:="$resolv_conf.$$.openresolv"} | |||||
| : ${libc_service:=nscd} | : ${libc_service:=nscd} | ||||
| : ${list_resolv:=@SBINDIR@/resolvconf -l} | : ${list_resolv:=@SBINDIR@/resolvconf -L} | ||||
| if [ "${resolv_conf_head-x}" = x ] && [ -f "$SYSCONFDIR"/resolv.conf.head ] | if [ "${resolv_conf_head-x}" = x ] && [ -f "$SYSCONFDIR"/resolv.conf.head ] | ||||
| then | then | ||||
| resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)" | resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)" | ||||
| fi | fi | ||||
| if [ "${resolv_conf_tail-x}" = x ] && [ -f "$SYSCONFDIR"/resolv.conf.tail ] | if [ "${resolv_conf_tail-x}" = x ] && [ -f "$SYSCONFDIR"/resolv.conf.tail ] | ||||
| then | then | ||||
| resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)" | resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.tail)" | ||||
| fi | fi | ||||
| backup=true | |||||
| signature="# Generated by resolvconf" | signature="# Generated by resolvconf" | ||||
| uniqify() | uniqify() | ||||
| { | { | ||||
| result= | result= | ||||
| while [ -n "$1" ]; do | while [ -n "$1" ]; do | ||||
| case " $result " in | case " $result " in | ||||
| *" $1 "*);; | *" $1 "*);; | ||||
| *) result="$result $1";; | *) result="$result $1";; | ||||
| esac | esac | ||||
| shift | shift | ||||
| done | done | ||||
| echo "${result# *}" | echo "${result# *}" | ||||
| } | } | ||||
| case "${resolv_conf_passthrough:-NO}" in | case "${resolv_conf_passthrough:-NO}" in | ||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | ||||
| backup=false | |||||
| newest= | newest= | ||||
| for conf in "$IFACEDIR"/*; do | for conf in "$KEYDIR"/*; do | ||||
| if [ -z "$newest" ] || [ "$conf" -nt "$newest" ]; then | if [ -z "$newest" ] || [ "$conf" -nt "$newest" ]; then | ||||
| newest="$conf" | newest="$conf" | ||||
| fi | fi | ||||
| done | done | ||||
| [ -z "$newest" ] && exit 0 | [ -z "$newest" ] && exit 0 | ||||
| newconf="$(cat "$newest")$NL" | newconf="$signature$NL$(cat "$newest")$NL" | ||||
| ;; | ;; | ||||
| /dev/null|[Nn][Uu][Ll][Ll]) | /dev/null|[Nn][Uu][Ll][Ll]) | ||||
| : ${resolv_conf_local_only:=NO} | : ${resolv_conf_local_only:=NO} | ||||
| if [ "$local_nameservers" = "127.* 0.0.0.0 255.255.255.255 ::1" ]; then | if [ "$local_nameservers" = "127.* 0.0.0.0 255.255.255.255 ::1" ]; then | ||||
| local_nameservers= | local_nameservers= | ||||
| fi | fi | ||||
| # Need to overwrite our variables. | # Need to overwrite our variables. | ||||
| eval "$(@SBINDIR@/resolvconf -V)" | eval "$(@SBINDIR@/resolvconf -V)" | ||||
| ▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | *) | ||||
| if [ -n "$resolv_conf_tail" ]; then | if [ -n "$resolv_conf_tail" ]; then | ||||
| newconf="$newconf$resolv_conf_tail$NL" | newconf="$newconf$resolv_conf_tail$NL" | ||||
| fi | fi | ||||
| ;; | ;; | ||||
| esac | esac | ||||
| # Check if the file has actually changed or not | # Check if the file has actually changed or not | ||||
| if [ -e "$resolv_conf" ]; then | if [ -e "$resolv_conf" ]; then | ||||
| [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] && exit 0 | if [ "$CMD" != u ] && \ | ||||
| [ "$(cat "$resolv_conf")" = "$(printf %s "$newconf")" ] | |||||
| then | |||||
| exit 0 | |||||
| fi | fi | ||||
| # Change is good. | |||||
| # If the old file does not have our signature, back it up. | |||||
| # If the new file just has our signature, restore the backup. | |||||
| if $backup; then | |||||
| if [ "$newconf" = "$signature$NL" ]; then | |||||
| if [ -e "$resolv_conf.bak" ]; then | |||||
| newconf="$(cat "$resolv_conf.bak")$NL" | |||||
| fi | |||||
| elif [ -e "$resolv_conf" ]; then | |||||
| read line <"$resolv_conf" | read line <"$resolv_conf" | ||||
| if [ "$line" != "$signature" ]; then | if [ "$line" != "$signature" ]; then | ||||
| if [ "$CMD" != u ]; then | |||||
| warn "signature mismatch: $resolv_conf" | |||||
| warn "run \`resolvconf -u\` to update" | |||||
| exit 1 | |||||
| fi | |||||
| cp "$resolv_conf" "$resolv_conf.bak" | cp "$resolv_conf" "$resolv_conf.bak" | ||||
| fi | fi | ||||
| fi | fi | ||||
| fi | |||||
| # There are pros and cons for writing directly to resolv.conf | |||||
| # instead of a temporary file and then moving it over. | |||||
| # The default is to write to resolv.conf as it has the least | |||||
| # issues and has been the long standing default behaviour. | |||||
| # resolv.conf could also be bind mounted for network namespaces | |||||
| # so we cannot move in this instance. | |||||
| case "${resolv_conf_mv:-NO}" in | |||||
| [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) | |||||
| # Protect against symlink attack, ensure new file does not exist | |||||
| rm -f "$resolv_conf_tmp" | |||||
| # Keep original file owner, group and mode | |||||
| [ -r "$resolv_conf" ] && cp -p "$resolv_conf" "$resolv_conf_tmp" | |||||
| # Create our resolv.conf now | # Create our resolv.conf now | ||||
| if (umask 022; printf %s "$newconf" >"$resolv_conf_tmp"); then | |||||
| mv "$resolv_conf_tmp" "$resolv_conf" | |||||
| fi | |||||
| ;; | |||||
| *) | |||||
| (umask 022; printf %s "$newconf" >"$resolv_conf") | (umask 022; printf %s "$newconf" >"$resolv_conf") | ||||
| ;; | |||||
| esac | |||||
| if [ -n "$libc_restart" ]; then | if [ -n "$libc_restart" ]; then | ||||
| eval $libc_restart | eval $libc_restart | ||||
| elif [ -n "$RESTARTCMD" ]; then | elif [ -n "$RESTARTCMD" ]; then | ||||
| set -- ${libc_service} | set -- ${libc_service} | ||||
| eval "$RESTARTCMD" | eval "$RESTARTCMD" | ||||
| else | else | ||||
| @SBINDIR@/resolvconf -r ${libc_service} | @SBINDIR@/resolvconf -r ${libc_service} | ||||
| fi | fi | ||||
| Show All 14 Lines | |||||