Improve build system a little.
[openresolv] / libc.in
diff --git a/libc.in b/libc.in
index a01d431c56ef1a0f3166a9686611420f5bf6f453..e11a70e26728bc8f7f1ed4954a27826a5b39a458 100644 (file)
--- a/libc.in
+++ b/libc.in
@@ -1,10 +1,8 @@
 #!/bin/sh
-# Copyright 2007-2008 Roy Marples
+# Copyright 2007-2009 Roy Marples
 # All rights reserved
 
 # libc subscriber for resolvconf
-# You could symlink /etc/resolv.conf to resolvconf/run/resolv.conf
-# for a read only etc, if resolvconf/run is linked to /var/run/resolvconf.
 
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-# Load our variables from resolvconf
-eval "$(resolvconf -v)"
-
-RESOLVCONF=/etc/resolv.conf
-PREFIX=@PREFIX@
+RESOLVCONF="${RESOLVCONF:-resolvconf}"
 SYSCONFDIR=@SYSCONFDIR@
-BASE="${SYSCONFDIR}/resolv.conf.d/base"
-
-uniqify()
-{
-    local result=
-    while [ -n "$1" ]; do
-               case " ${result} " in
-               *" $1 "*);;
-               *) result="${result} $1";;
-               esac
-               shift
-       done
-    echo "${result# *}"
-}
+LIBEXECDIR=@LIBEXECDIR@
+VARDIR=@VARDIR@
+IFACEDIR="${VARDIR}/interfaces"
 
 # sed may not be available, and this is faster on small files
 key_get_value()
@@ -72,59 +55,101 @@ key_get_value()
        fi
 }
 
-NEWNS=
-NEWSEARCH=
-if [ -e "${BASE}" ]; then
-       NEWNS="$(key_get_value "nameserver " "${BASE}")"
-       NEWSEARCH="$(key_get_value "search " "${BASE}")"
-       if [ -z "${NEWSEARCH}" ]; then
-               NEWSEARCH="$(key_get_value "domain " "${BASE}")"
+# Support original resolvconf configuration layout
+# as well as the openresolv config file
+if [ -f "${SYSCONFDIR}"/resolvconf.conf ]; then
+       . "${SYSCONFDIR}"/resolvconf.conf
+elif [ -d "${SYSCONFDIR}/resolvconf" ]; then
+       SYSCONFDIR="${SYSCONFDIR}/resolvconf/resolv.conf.d"
+       base="${SYSCONFDIR}/resolv.conf.d/base"
+       if [ -f "${BASE}" ]; then
+               name_servers="$(key_get_value "nameserver " "${base}")"
+               search_domains="$(key_get_value "search " "${base}")"
+               if [ -z "${search_domains}" ]; then
+                       search_domains="$(key_get_value "domain " "${base}")"
+               fi
+               resolv_conf_options="$(key_get_value "options " "${base}")"
+       fi
+       if [ -f "${SYSCONFDIR}"/resolv.conf.d/head ]; then
+               resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.d/head)"
+       fi
+       if [ -f "${SYSCONFDIR}"/resolv.conf.d/tail ]; then
+               resolv_conf_tail="$(cat "${SYSCONFDIR}"/resolv.conf.d/tail)"
        fi
 fi
-NEWSEARCH="$(uniqify ${NEWSEARCH} ${SEARCH})"
-NEWNS="$(uniqify ${NEWNS} ${NAMESERVERS})"
+resolv_conf="${resolv_conf:-/etc/resolv.conf}"
 
-# Hold our new resolv.conf in a variable to save on temporary files
-NEWCONF=""
-[ -e "${SYSCONFDIR}"/resolv.conf.d/head ] \
-       && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.d/head)\n"
-[ -n "${NEWSEARCH}" ] && NEWCONF="${NEWCONF}search ${NEWSEARCH}\n"
-for N in ${NEWNS}; do
-       NEWCONF="${NEWCONF}nameserver ${N}\n"
-done
+uniqify()
+{
+       local result=
+       while [ -n "$1" ]; do
+               case " ${result} " in
+               *" $1 "*);;
+               *) result="${result} $1";;
+               esac
+               shift
+       done
+       echo "${result# *}"
+}
 
-# Now get any configured options
-OPTS=
-if [ -e "${BASE}" ]; then
-       OPTS="$(key_get_value "options " "${BASE}")"
-fi
-OPTS="${OPTS}${OPTS:+ }$(resolvconf -l | key_get_value "options ")"
-if [ -n "${OPTS}" ]; then
-       NEWCONF="${NEWCONF}options"
-       for OPT in $(uniqify ${OPTS}); do
-               NEWCONF="${NEWCONF} ${OPT}"
+case "${resolv_conf_passthrough:-NO}" in
+[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
+       newest=
+       for conf in "${IFACEDIR}"/*; do
+               if [ -z "${newest}" -o "${conf}" -nt "${newest}" ]; then
+                       newest="${conf}"
+               fi
+       done
+       [ -z "${newest}" ] && exit 0
+       newconf="$(cat "${newest}")\n"
+       ;;
+*)
+       eval "$(${RESOLVCONF} -v)"
+       newsearch="$(uniqify ${search_domains} ${SEARCH})"
+       newns="$(uniqify ${name_servers} ${NAMESERVERS})"
+
+       # Hold our new resolv.conf in a variable to save on temporary files
+       newconf="# Generated by resolvconf\n"
+       if [ -n "${resolv_conf_head}" ]; then
+               newconf="${newconf}${resolv_conf_head}\n"
+       fi
+       [ -n "${newsearch}" ] && newconf="${newconf}search ${newsearch}\n"
+       for n in ${newns}; do
+               newconf="${newconf}nameserver ${n}\n"
        done
-       NEWCONF="${NEWCONF}\n"
-fi
 
-[ -e "${SYSCONFDIR}"/resolv.conf.d/tail ] \
-       && NEWCONF="${NEWCONF}$(cat "${SYSCONFDIR}"/resolv.conf.d/tail)\n"
+       # Now get any configured options
+       opts="${resolv_conf_options}${resolv_conf_options:+ }"
+       opts="${opts}$(${RESOLVCONF} -l | key_get_value "options ")"
+       if [ -n "${opts}" ]; then
+               newconf="${newconf}options"
+               for opt in $(uniqify ${opts}); do
+                       newconf="${newconf} ${opt}"
+               done
+               newconf="${newconf}\n"
+       fi
+
+       if [ -n "{resolv_conf_tail}" ]; then
+               newconf="${newconf}${resolv_conf_tail}\n"
+       fi
+       ;;
+esac
 
 # Check if the file has actually changed or not
-if [ -e "${RESOLVCONF}" ]; then
-       [ "$(cat "${RESOLVCONF}")" = "$(printf "${NEWCONF}")" ] && exit 0
+if [ -e "${resolv_conf}" ]; then
+       [ "$(cat "${resolv_conf}")" = "$(printf "${newconf}")" ] && exit 0
 fi
 
 # Create our resolv.conf now
-(umask 022; printf "${NEWCONF}" > "${RESOLVCONF}")
+(umask 022; printf "${newconf}" > "${resolv_conf}")
 
-resolvconf -s nscd restart
+${RESOLVCONF} -s nscd restart
 retval=$?
 
 # Notify users of the resolver
-for x in "${SYSCONFDIR}"/update-libc.d/*; do
-       if [ -e "${x}" ]; then
-               "${x}" "$@"
+for script in "${LIBEXECDIR}"/libc.d/*; do
+       if [ -f "${script}" -a -x "${script}" ]; then
+               RESOLVCONF="${RESOLVCONF}" "${script}" "$@"
                retval=$((${retval} + $?))
        fi
 done