1
0

vpnc-script 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/sh
  2. #* reason -- why this script was called, one of: pre-init connect disconnect
  3. #* VPNGATEWAY -- vpn gateway address (always present)
  4. #* TUNDEV -- tunnel device (always present)
  5. #* INTERNAL_IP4_ADDRESS -- address (always present)
  6. #* INTERNAL_IP4_NETMASK -- netmask (often unset)
  7. #* INTERNAL_IP4_DNS -- list of dns serverss
  8. #* INTERNAL_IP4_NBNS -- list of wins servers
  9. #* CISCO_DEF_DOMAIN -- default domain name
  10. #* CISCO_BANNER -- banner from server
  11. #* CISCO_SPLIT_INC -- number of networks in split-network-list
  12. #* CISCO_SPLIT_INC_%d_ADDR -- network address
  13. #* CISCO_SPLIT_INC_%d_MASK -- subnet mask (for example: 255.255.255.0)
  14. #* CISCO_SPLIT_INC_%d_MASKLEN -- subnet masklen (for example: 24)
  15. #* CISCO_SPLIT_INC_%d_PROTOCOL -- protocol (often just 0)
  16. #* CISCO_SPLIT_INC_%d_SPORT -- source port (often just 0)
  17. #* CISCO_SPLIT_INC_%d_DPORT -- destination port (often just 0)
  18. do_pre_init() {
  19. # bevore doing anything, make shure, the tun module is loaded and the
  20. # tun device nodes exist.
  21. if (exec 6<> /dev/net/tun) > /dev/null 2>&1 ; then
  22. :
  23. else # can't open /dev/net/tun
  24. test -e /proc/sys/kernel/modprobe && `cat /proc/sys/kernel/modprobe` tun 2>/dev/null
  25. # fix for broken devfs in kernel 2.6.x
  26. if [ "`readlink /dev/net/tun`" = misc/net/tun \
  27. -a ! -e /dev/net/misc/net/tun -a -e /dev/misc/net/tun ] ; then
  28. ln -sf /dev/misc/net/tun /dev/net/tun
  29. fi
  30. # make sure tun device exists
  31. if [ ! -e /dev/net/tun ]; then
  32. mkdir -p /dev/net
  33. mknod -m 0640 /dev/net/tun c 10 200
  34. fi
  35. fi
  36. echo "pre-init successful."
  37. }
  38. do_connect() {
  39. # after connection is established, we should update resolv.conf
  40. # and the kernel routing table
  41. # set up the interface
  42. ifconfig $TUNDEV $INTERNAL_IP4_ADDRESS pointopoint $INTERNAL_IP4_ADDRESS mtu 1412 up
  43. # set up the route to the remote side and remove any cached routes
  44. ip route add `ip route get "$VPNGATEWAY"`
  45. ip route flush cache
  46. # set up the default routes via vpnc-route
  47. echo "starting vpnc-route"
  48. /etc/vpnc/vpnc-route start
  49. if [ "x$INTERNAL_IP4_DNS" != "x" ]; then
  50. # set up the dns servers (add to resolv.conf)
  51. echo "setting up DNS server"
  52. # simply add the given servers to the resolv.conf file
  53. echo "" > /var/run/vpnc/resolv.conf
  54. for dns in $INTERNAL_IP4_DNS; do
  55. echo "nameserver $dns" >> /var/run/vpnc/resolv.conf
  56. done;
  57. cat /etc/resolv.conf >> /var/run/vpnc/resolv.conf
  58. mv /var/run/vpnc/resolv.conf /etc/resolv.conf
  59. # keep the DNS server IPs for shutdown
  60. echo "$INTERNAL_IP4_DNS" > /var/run/vpnc/dnsserver
  61. fi
  62. }
  63. do_disconnect() {
  64. # remove the nameserver from resolv.conf
  65. # and restore the old routing table
  66. # remove route to gateway
  67. ip route del $VPNGATEWAY
  68. # remove default routes
  69. /etc/vpnc/vpnc-route stop
  70. # remove the dns servers from resolv.conf
  71. if [ -f /var/run/vpnc/dnsserver ]; then
  72. re_dns="";
  73. for dns in `cat /var/run/vpnc/dnsserver`; do
  74. echo "removing DNS server $dns";
  75. if [ "x$re_dns" == "x" ]; then
  76. re_dns=\($dns\);
  77. else
  78. re_dns=$re_dns\|\($dns\);
  79. fi;
  80. done;
  81. echo "re_dns=$re_dns"
  82. cat /etc/resolv.conf | grep -v -E "($re_dns)|(^\ *$)" > /var/run/vpnc/resolv.conf
  83. mv /var/run/vpnc/resolv.conf /etc/resolv.conf
  84. rm /var/run/vpnc/dnsserver
  85. fi;
  86. # deconfigure network interface
  87. ifconfig $TUNDEV down
  88. }
  89. case "$reason" in
  90. pre-init)
  91. do_pre_init
  92. ;;
  93. connect)
  94. do_connect
  95. ;;
  96. disconnect)
  97. do_disconnect
  98. ;;
  99. *)
  100. echo "unknown reason '$reason'. Maybe vpnc-script is out of date" 1>&2
  101. exit 1
  102. ;;
  103. esac
  104. exit 0