Fix PREFIX again
[openresolv] / resolvconf
index e3cad26fecdd0c72d9ade428fde697de029fa305..55e621474cacee00a7ae1436daa680cf93b74b35 100755 (executable)
@@ -1,13 +1,36 @@
 #!/bin/sh
 # Copyright 2006 Gentoo Foundation
-# Distributed under the terms of the GNU General Public License v2
+# Copyright 2007 Roy Marples
+# All rights reserved
 
-# Written by Roy Marples (uberlord@gentoo.org)
-# Heavily based on Debian resolvconf by Thomas Hood
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+#       copyright notice, this list of conditions and the following
+#       disclaimer in the documentation and/or other materials provided
+#       with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 argv0="$0"
 
-VARDIR=/etc/resolvconf/run
+PREFIX=
+RESOLVCONF="${PREFIX}"/etc/resolvconf
+UPDATED="${RESOLVCONF}"/update.d
+VARDIR="${RESOLVCONF}"/run
 IFACEDIR="${VARDIR}/interfaces"
 
 error_exit() {
@@ -49,7 +72,7 @@ echo_resolv() {
 
 uniqify() {
     local result=
-    while [ -n "$1" ] ; do
+    while [ -n "$1" ]; do
                case " ${result} " in
                        *" $1 "*) ;;
                        *) result="${result} $1" ;;
@@ -59,40 +82,40 @@ uniqify() {
     echo "${result# *}"
 }
 
-if [ -n "$1" ] ; then
+if [ -n "$1" ]; then
        CMD="$1"
        shift
 fi
-if [ -n "$1" ] ; then
+if [ -n "$1" ]; then
        IFACE="$1"
        shift
 fi
 
 # -l is a Gentoo option that lists our resolv files
 # optionally for a specific interface
-if [ "x${CMD}" = "x-l" -o "x${CMD}" = "x-i" ] ; then
-       [ ! -d "${IFACEDIR}" ] && exit 0
+if [ "x${CMD}" = "x-l" -o "x${CMD}" = "x-i" ]; then
+       [ -d "${IFACEDIR}" ] || exit 0
        
        # If we have an interface ordering list, then use that.
        # It works by just using pathname expansion in the interface directory.
-       if [ -n "${IFACE}" ] ; then
+       if [ -n "${IFACE}" ]; then
                LIST="${IFACE} $@"
-       elif [ -r /etc/resolvconf/interface-order ] ; then
-               LIST="$(cat /etc/resolvconf/interface-order)"
+       elif [ -r "${RESOLVCONF}"/interface-order ]; then
+               LIST="$(cat "${RESOLVCONF}"/interface-order)"
        fi
 
        # If we don't have a list then prefer lo, tunnels, ppp
        # and then anything else.
-       if [ -z "${LIST}" ] ; then
+       if [ -z "${LIST}" ]; then
                LIST="lo lo[0-9]* tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]* *"
        fi
 
        cd "${IFACEDIR}"
-       for IFACE in $(uniqify ${LIST}) ; do
+       for IFACE in $(uniqify ${LIST}); do
                # Only list interfaces which we really have
                [ -e "${IFACE}" ] || continue
                
-               if [ "x${CMD}" = "x-i" ] ; then
+               if [ "x${CMD}" = "x-i" ]; then
                        printf "${IFACE} "
                else
                        echo_resolv "${IFACE}"
@@ -102,7 +125,7 @@ if [ "x${CMD}" = "x-l" -o "x${CMD}" = "x-i" ] ; then
        exit 0
 fi
 
-if [ "x${CMD}" = "x-v" ] ; then
+if [ "x${CMD}" = "x-v" ]; then
        NS=
        DOMAIN=
        SEARCH=
@@ -111,7 +134,7 @@ if [ "x${CMD}" = "x-v" ] ; then
        NEWDOMAIN=
        LINES="$("${argv0}" -l "${IFACE}" | sed -e "s/'/'\\\\''/g" -e "s/^/'/g" -e "s/$/'/g")"
        eval set -- ${LINES}
-       for LINE in "$@" ; do
+       for LINE in "$@"; do
                case "${LINE}" in
                        "nameserver "*)
                                case "${LINE#* }" in
@@ -127,12 +150,12 @@ if [ "x${CMD}" = "x-v" ] ; then
                                DOMAIN=
                                ;;
                        *)
-                               if [ -z "${LINE}" ] ; then
-                                       for N in ${NS} ; do
-                                               if [ -n "${DOMAIN}" ] ; then
+                               if [ -z "${LINE}" ]; then
+                                       for N in ${NS}; do
+                                               if [ -n "${DOMAIN}" ]; then
                                                        NEWDOMAIN="${NEWDOMAIN} ${DOMAIN},${N}"
-                                               elif [ -n "${SEARCH}" ] ; then
-                                                       for S in ${SEARCH} ; do
+                                               elif [ -n "${SEARCH}" ]; then
+                                                       for S in ${SEARCH}; do
                                                                NEWSEARCH="${NEWSEARCH} ${S},${N}"
                                                        done
                                                else
@@ -152,9 +175,9 @@ if [ "x${CMD}" = "x-v" ] ; then
        NEWDOMAIN="$(uniqify ${NEWDOMAIN})"
        NEWSEARCH="$(uniqify ${NEWSEARCH})"
        NEWNS="$(uniqify ${NEWNS})"
-       for S in ${NEWSEARCH} ; do
-               for DN in ${NEWDOMAIN} ; do
-                       if [ "${S%,*}" = "${DN%,*}" ] ; then
+       for S in ${NEWSEARCH}; do
+               for DN in ${NEWDOMAIN}; do
+                       if [ "${S%,*}" = "${DN%,*}" ]; then
                                NEWSEARCH="$(echo "${NEWSEARCH}" | sed -e "s/${S}/${DN}/g")"
                                NEWDOMAIN="$(echo "${NEWDOMAIN}" | sed -e "s/${DN}//g")"
                                break
@@ -168,29 +191,22 @@ if [ "x${CMD}" = "x-v" ] ; then
        exit 0
 fi
 
-# Only root can update resolv.conf
-if ! touch "${VARDIR}"/.test ; then
-       error_exit "You must be root to run ${argv0##*/}"
-fi
-rm "${VARDIR}"/.test
-
 # Test that we have valid options
-if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ] ; then
-       if [ -z "${IFACE}" ] ; then
+if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
+       if [ -z "${IFACE}" ]; then
                usage "Interface not specified"
        fi
-elif [ "x${CMD}" != "x-u" ] ; then
+elif [ "x${CMD}" != "x-u" ]; then
        [ -n "x${CMD}" -a "x${CMD}" != "x-h" ] && usage "Unknown option ${CMD}"
        usage
 fi
-if [ "x${CMD}" = "x-a" ] ; then
-       q='\'
-       for x in '/' '\' ' ' '*' ; do
+if [ "x${CMD}" = "x-a" ]; then
+       for x in '/' \\ ' ' '*'; do
                case "${IFACE}" in
                        *[${x}]*) error_exit "${x} not allowed in interface name" ;;
                esac
        done
-       for x in '.' '-' '~' ; do
+       for x in '.' '-' '~'; do
                case "${IFACE}" in
                        [${x}]*) error_exit "${x} not allowed at start of interface name" ;;
                esac
@@ -200,36 +216,46 @@ if [ "x${CMD}" = "x-a" ] ; then
 fi
 
 # Ensure that libdir exists
-if [ ! -d "${IFACEDIR}" ] ; then
-       if [ ! -d "${VARDIR}" ] ; then
-               if [ -L "${VARDIR}" ] ; then
-                       DIR="$(readlink -f "${VARDIR}")"
-                       [ -z "${DIR}" ] && DIR="$(readlink "${VARDIR}")"
+if [ ! -d "${IFACEDIR}" ]; then
+       if [ ! -d "${VARDIR}" ]; then
+               if [ -L "${VARDIR}" ]; then
+                       DIR="$(readlink "${VARDIR}")"
+                       # Change to /etc as link maybe relative
+                       cd "${VARDIR%/*}"
+                       if ! mkdir -m 0755 -p "${DIR}"; then
+                               error_exit "Failed to create needed directory ${DIR}"
+                       fi
+               else
+                       if ! mkdir -m 0755 -p "${VARDIR}"; then
+                               error_exit "Failed to create needed directory ${VARDIR}"
+                       fi
                fi
-               # Change to /etc as link maybe relative
-               cd "${VARDIR%/*}"
-               mkdir -m 0755 -p "${DIR}" \
-                       || error_exit "Failed to create needed directory ${DIR}"
        fi
        mkdir -m 0755 -p "${IFACEDIR}" || \
                error_exit "Failed to create needed directory ${IFACEDIR}"
 else
        # Delete any existing information about the interface
-       if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ] ; then
+       if [ "x${CMD}" = "x-a" -o "x${CMD}" = "x-d" ]; then
                cd "${IFACEDIR}"
-               for iface in ${IFACE} ; do
-                       rm -f "${iface}"
+               for iface in ${IFACE}; do
+                       rm -f "${iface}" || exit $?
                done
        fi
 fi
 
-if [ "x${CMD}" = "x-a" ] ; then
+if [ "x${CMD}" = "x-a" ]; then
        # Create our resolv.conf file
-       cat >"${IFACEDIR}"/"${IFACE}"
+       cat >"${IFACEDIR}"/"${IFACE}" || exit $?
 fi
 
-for x in /etc/resolvconf/update.d/* ; do
-       [ -e "${x}" ] && "${x}" "${CMD}" "${IFACE}"
+retval=0
+for x in "${UPDATED}"/*; do
+       if [ -e "${x}" ]; then
+               "${x}" "${CMD}" "${IFACE}"
+               retval=$((${retval} + $?))
+       fi
 done
 
+exit ${retval}
+
 # vim: set ts=4 :