#!/bin/sh

# 0dns-up by John Hasler 1999-2003.
# Any possessor of a copy of this program may treat it as if it
# were in the public domain.  I waive all rights.

# Rev. Dec 22 1999 to put dynamic nameservers last.
# Rev. Aug 20 2001 to use patch from Sergio Gelato <Sergio.Gelato@astro.su.se>.
# Rev. Dec 12 2002 to delete USEPEERDNS variable and add MS_DNS1 and MS_DNS2.
# Rev. Jan 5 2003 added explanatory text.
# Rev. May 15 2003 to use update-resolv and move operations to /var/run/pppconfig.

# 0dns-up sets up /etc/resolv.conf for the provider being connected to.  In
# conjunction with pppd's usepeerdns option it also handles dynamic dns.
# It expects to be passed the provider name in PPP_IPPARAM.

# Pppconfig creates a file in /etc/ppp/resolv for each provider for which the
# administrator chooses 'Static' or 'Dynamic' in the 'Configure Nameservers'
# screen.  The files for providers for which 'Static' was chosen contain the
# nameservers given by the administrator.  Those for which 'Dynamic' was chosen
# are empty.  0dns-up fills in the nameservers when pppd gets them from the
# provider when the connection comes up.  You can edit these files, adding 
# 'search' or 'domain' directives or additional nameservers.  Read the 
# resolv.conf manual first, though. 


PATH=/sbin:/bin:/usr/sbin:/usr/bin
# If pppconfig has been removed we are not supposed to do anything.
test -f /usr/sbin/pppconfig || exit 0

PROVIDER="$PPP_IPPARAM"
ETC="/etc"
RUNDIR="/var/run/pppconfig"
RESOLVCONF="$ETC/resolv.conf"
PPPRESOLV="$ETC/ppp/resolv"
TEMPLATE="$RUNDIR/0dns.tempXXXXXXXX"
RESOLVBAK="$RUNDIR/resolv.conf.bak.$PROVIDER"

umask 022
cd "$RUNDIR" || exit 1

# If we don't have a provider we have nothing to do.

test -z "$PROVIDER" && exit 0

# Is resolv.conf a non-symlink on a ro root? If so give up.

if test -f "$RESOLVCONF" && test -e /proc/mounts; then
    grep " / " /proc/mounts | grep -q " rw " || exit 0
fi

# Put the resolv.conf for this provider (if it exists) in a temp file.
# If we are using dynamic dns it will be empty or contain any resolver
# options the user added.  Otherwise it will be a complete resolv.conf for
# this provider.

TEMPRESOLV=`mktemp $TEMPLATE` || exit 1
mv "$TEMPRESOLV" "$RUNDIR/0dns.$PROVIDER" || exit 1
TEMPRESOLV="$RUNDIR/0dns.$PROVIDER"

# We trust $PPPRESOLV/$PROVIDER.
test -f "$PPPRESOLV/$PROVIDER" && cat "$PPPRESOLV/$PROVIDER" > "$TEMPRESOLV"

# DNS1 and DNS2 are variables exported by pppd when using 'usepeerdns'.
# Do we have them?  If so, we are using "dynamic dns".  Append a couple of
# nameserver lines to the temp file.

if [ "$DNS1" ] ; then
    echo '' >> "$TEMPRESOLV"
    echo "nameserver $DNS1" >> "$TEMPRESOLV"
    if [ "$DNS2" ] ; then
	echo '' >> "$TEMPRESOLV"
        echo "nameserver $DNS2" >> "$TEMPRESOLV"
    fi
# ipppd uses MS_DNS1 and MS_DNS2 instead of DNS1 and DNS2.
elif [ "$MS_DNS1" ] ; then
    echo '' >> "$TEMPRESOLV"
    echo "nameserver $MS_DNS1" >> "$TEMPRESOLV"
    if [ "$MS_DNS2" ] ; then
	echo '' >> "$TEMPRESOLV"
        echo "nameserver $MS_DNS2" >> "$TEMPRESOLV"
    fi
fi

# We should have something in TEMPRESOLV by now.  If not we'd 
# better quit.

if [ ! -s "$TEMPRESOLV" ]
    then
    rm -f "$TEMPRESOLV"
    exit 1
fi

# Do it the easy way if possible.
if [ -f /usr/sbin/update-resolv ]
    then
    /usr/sbin/update-resolv --activate "$TEMPRESOLV"
    rm -f "$TEMPRESOLV"

else
    # We better not do anything if a RESOLVBAK already exists.
    ls | grep -q "$RESOLVCONF.bak"
    if [ ! $? ]
	then
	rm -f "$TEMPRESOLV"
	exit 1
    fi

    # Back up resolv.conf. Follow symlinks.  Keep TEMPRESOLV
    # around for 0dns-down to look at.
    /bin/cp -Lp "$RESOLVCONF" "$RESOLVBAK" || exit 1
    /bin/cp -Lp "$TEMPRESOLV" "$RESOLVCONF" || exit 1
    chmod 644 "$RESOLVCONF" || exit 1
fi

# Tell nscd about what we've done.
/usr/sbin/nscd -i hosts || exit 0

