PageRenderTime 101ms CodeModel.GetById 96ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/ppp/ip-up.d/0dns-up

http://github.com/brinkman83/bashrc
Shell | 120 lines | 53 code | 27 blank | 40 comment | 21 complexity | b6bca8a403a34713eb6e7018a02e032c MD5 | raw file
  1#!/bin/sh
  2
  3# $Id: 0dns-up,v 1.1.1.1 2004/05/07 03:12:59 john Exp $
  4
  5# 0dns-up by John Hasler 1999-2006.
  6# Any possessor of a copy of this program may treat it as if it
  7# were in the public domain.  I waive all rights.
  8
  9# Rev. Dec 22 1999 to put dynamic nameservers last.
 10# Rev. Aug 20 2001 to use patch from Sergio Gelato <Sergio.Gelato@astro.su.se>.
 11# Rev. Dec 12 2002 to delete USEPEERDNS variable and add MS_DNS1 and MS_DNS2.
 12# Rev. Jan 5 2003 added explanatory text.
 13# Rev. May 15 2003 to move operations to /var/run/pppconfig.
 14# Rev. Apr 12 2004 to use resolvconf if installed.
 15
 16# 0dns-up sets up /etc/resolv.conf for the provider being connected to.  In
 17# conjunction with pppd's usepeerdns option it also handles dynamic dns.
 18# It expects to be passed the provider name in PPP_IPPARAM.
 19
 20# Pppconfig creates a file in /etc/ppp/resolv for each provider for which the
 21# administrator chooses 'Static' or 'Dynamic' in the 'Configure Nameservers'
 22# screen.  The files for providers for which 'Static' was chosen contain the
 23# nameservers given by the administrator.  Those for which 'Dynamic' was chosen
 24# are empty.  0dns-up fills in the nameservers when pppd gets them from the
 25# provider when the connection comes up.  You can edit these files, adding 
 26# 'search' or 'domain' directives or additional nameservers.  Read the 
 27# resolv.conf manual first, though. 
 28
 29
 30PATH=/sbin:/bin:/usr/sbin:/usr/bin
 31# If pppconfig has been removed we are not supposed to do anything.
 32test -f /usr/sbin/pppconfig || exit 0
 33
 34# If we don't have a provider we have nothing to do.
 35test -z "$PPP_IPPARAM" && exit 0
 36
 37# Strip options.
 38PROVIDER=`echo "$PPP_IPPARAM" | cut -d' ' -f1`
 39
 40ETC="/etc"
 41RUNDIR="/var/cache/pppconfig"
 42RESOLVCONF="$ETC/resolv.conf"
 43PPPRESOLV="$ETC/ppp/resolv"
 44TEMPLATE="$RUNDIR/0dns.tempXXXXXXXX"
 45RESOLVBAK="$RUNDIR/resolv.conf.bak.$PROVIDER"
 46
 47# Is PROVIDER something we can use?
 48
 49test -f "$PPPRESOLV/$PROVIDER" || exit 0
 50
 51if [ -x /sbin/resolvconf ]; then
 52	test -n "$PPP_IFACE" || exit 1
 53	/sbin/resolvconf -a "${PPP_IFACE}.pppconfig" < "$PPPRESOLV/$PROVIDER"
 54	exit
 55fi
 56
 57umask 022
 58cd "$RUNDIR" || exit 1
 59
 60# Is resolv.conf a non-symlink on a ro root? If so give up.
 61
 62[ -e /proc/mounts ] || { echo "$0: Error: Could not read /proc/mounts" ; exit 1 ; }
 63[ -L  "$RESOLVCONF" ] || grep " / " /proc/mounts | grep -q " rw " || exit 0
 64
 65
 66# Put the resolv.conf for this provider in a temp file.  If we are using
 67# dynamic dns it will be empty or contain any resolver options the user
 68# added.  Otherwise it will be a complete resolv.conf for this provider.
 69
 70TEMPRESOLV=`mktemp $TEMPLATE` || exit 1
 71mv "$TEMPRESOLV" "$RUNDIR/0dns.$PROVIDER" || exit 1
 72TEMPRESOLV="$RUNDIR/0dns.$PROVIDER"
 73cat "$PPPRESOLV/$PROVIDER" > "$TEMPRESOLV"
 74
 75# DNS1 and DNS2 are variables exported by pppd when using 'usepeerdns'.
 76# Do we have them?  If so, we are using "dynamic dns".  Append a couple of
 77# nameserver lines to the temp file.
 78
 79if [ "$DNS1" ] ; then
 80    echo '' >> "$TEMPRESOLV"
 81    echo "nameserver $DNS1" >> "$TEMPRESOLV"
 82    if [ "$DNS2" ] ; then
 83	echo '' >> "$TEMPRESOLV"
 84        echo "nameserver $DNS2" >> "$TEMPRESOLV"
 85    fi
 86# ipppd uses MS_DNS1 and MS_DNS2 instead of DNS1 and DNS2.
 87elif [ "$MS_DNS1" ] ; then
 88    echo '' >> "$TEMPRESOLV"
 89    echo "nameserver $MS_DNS1" >> "$TEMPRESOLV"
 90    if [ "$MS_DNS2" ] ; then
 91	echo '' >> "$TEMPRESOLV"
 92        echo "nameserver $MS_DNS2" >> "$TEMPRESOLV"
 93    fi
 94fi
 95
 96# We should have something in TEMPRESOLV by now.  If not we'd 
 97# better quit.
 98
 99if [ ! -s "$TEMPRESOLV" ]
100    then
101    rm -f "$TEMPRESOLV"
102    exit 1
103fi
104
105# We better not do anything if a RESOLVBAK already exists.
106if  ls | grep -q "resolv.conf.bak"
107    then
108    rm -f "$TEMPRESOLV"
109    exit 1
110fi
111
112# Back up resolv.conf. Follow symlinks.  Keep TEMPRESOLV
113# around for 0dns-down to look at.
114/bin/cp -Lp "$RESOLVCONF" "$RESOLVBAK" || exit 1
115/bin/cp -Lp "$TEMPRESOLV" "$RESOLVCONF" || exit 1
116chmod 644 "$RESOLVCONF" || exit 1
117
118
119# Restart nscd because resolv.conf has changed
120[ -x /etc/init.d/nscd ] && { /etc/init.d/nscd restart || true ; }