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