client.up 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. # Copyright (c) 2005-2018 OpenVPN Inc
  3. # Licensed under the GPL version 2
  4. # First version by Jesse Adelman
  5. # someone at boldandbusted dink com
  6. # http://www.boldandbusted.com/
  7. # PURPOSE: This script automatically sets the proper /etc/resolv.conf entries
  8. # as pulled down from an OpenVPN server.
  9. # INSTALL NOTES:
  10. # Place this in /etc/openvpn/client.up
  11. # Then, add the following to your /etc/openvpn/<clientconfig>.conf:
  12. # client
  13. # up /etc/openvpn/client.up
  14. # Next, "chmod a+x /etc/openvpn/client.up"
  15. # USAGE NOTES:
  16. # Note that this script is best served with the companion "client.down"
  17. # script.
  18. # Tested under Debian lenny with OpenVPN 2.1_rc11
  19. # It should work with any UNIX with a POSIX sh, /etc/resolv.conf or resolvconf
  20. # This runs with the context of the OpenVPN UID/GID
  21. # at the time of execution. This generally means that
  22. # the client "up" script will run fine, but the "down" script
  23. # will require the use of the OpenVPN "down-root" plugin
  24. # which is in the plugins/ directory of the OpenVPN source tree
  25. # A horrid work around, from a security perspective,
  26. # is to run OpenVPN as root. THIS IS NOT RECOMMENDED. You have
  27. # been WARNED.
  28. PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
  29. # init variables
  30. i=1
  31. domains=
  32. fopt=
  33. ndoms=0
  34. nns=0
  35. nl='
  36. '
  37. # $foreign_option_<n> is something like
  38. # "dhcp-option DOMAIN example.com" (multiple allowed)
  39. # or
  40. # "dhcp-option DNS 10.10.10.10" (multiple allowed)
  41. # each DNS option becomes a "nameserver" option in resolv.conf
  42. # if we get one DOMAIN, that becomes "domain" in resolv.conf
  43. # if we get multiple DOMAINS, those become "search" lines in resolv.conf
  44. # if we get no DOMAINS, then don't use either domain or search.
  45. while true; do
  46. eval fopt=\$foreign_option_${i}
  47. [ -z "${fopt}" ] && break
  48. case ${fopt} in
  49. dhcp-option\ DOMAIN\ *)
  50. ndoms=$((ndoms + 1))
  51. domains="${domains} ${fopt#dhcp-option DOMAIN }"
  52. ;;
  53. dhcp-option\ DNS\ *)
  54. nns=$((nns + 1))
  55. if [ $nns -le 3 ]; then
  56. dns="${dns}${dns:+$nl}nameserver ${fopt#dhcp-option DNS }"
  57. else
  58. printf "%s\n" "Too many nameservers - ignoring after third" >&2
  59. fi
  60. ;;
  61. *)
  62. printf "%s\n" "Unknown option \"${fopt}\" - ignored" >&2
  63. ;;
  64. esac
  65. i=$((i + 1))
  66. done
  67. ds=""
  68. if [ $ndoms -eq 1 ]; then
  69. ds="${nl}domain"
  70. elif [ $ndoms -gt 1 ]; then
  71. ds="${nl}search"
  72. fi
  73. # This is the complete file - "$domains" has a leading space already
  74. out="# resolv.conf autogenerated by ${0} (${dev})${nl}${dns}${ds}${domains}"
  75. # use resolvconf if it's available
  76. if type resolvconf >/dev/null 2>&1; then
  77. printf "%s\n" "${out}" | resolvconf -p -a "${dev}"
  78. else
  79. # Preserve the existing resolv.conf
  80. if [ -e /etc/resolv.conf ] ; then
  81. cp /etc/resolv.conf /etc/resolv.conf.ovpnsave
  82. fi
  83. printf "%s\n" "${out}" > /etc/resolv.conf
  84. chmod 644 /etc/resolv.conf
  85. fi
  86. exit 0