SHARE
TWEET

Untitled

a guest Feb 18th, 2011 693 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # Login Info
  4. PPTP_USER=<vpn_user_name>
  5. PPTP_PASSWORD=<vpn_password>
  6.  
  7. # PPTP Server Info
  8. PPTP_DOMAIN=<vpn_domain>
  9. PPTP_SERVER=<vpn_server_name_or_ip>
  10. PPPD_OPTIONS=
  11. DEF_GATEWAY=No
  12. ADD_SUBNETS=<vpn_extra_subnets_space_delimited> # for example, "192.168.100.0 192.168.101.0"
  13.                                                 # this is needed if you're not using default gateway from VPN network,
  14.                                                 # and your VPN network has multiple subnets.
  15. UNIT=5 # anything but 0
  16.  
  17. # Do not change anything below unless you know what you're doing...
  18.  
  19. SCRIPTS_PATH=/tmp/ppp/ppp${UNIT}
  20. FIRE_PATH=/tmp/config
  21. DNSMASQ_CUSTOM=/etc/dnsmasq.custom
  22. TMPF=/tmp/$(date '+%y%d%m%H%M%S')
  23.  
  24. _up()
  25. {
  26.         echo "IP-UP"
  27.  
  28.         if [ x$IFNAME == x ]; then
  29.                 return 0
  30.         fi
  31.  
  32.         echo $IFNAME > $SCRIPTS_PATH/link
  33.  
  34.         ifconfig $IFNAME arp multicast up
  35.  
  36.         nvram set ppp${UNIT}_get_ip=$IPLOCAL
  37.         nvram set ppp${UNIT}_gateway=$IPREMOTE
  38.  
  39.         echo "rebind-domain-ok=$PPTP_DOMAIN" > $TMPF
  40.         echo "server=/$PPTP_SERVER/#" >> $TMPF
  41.         [ -n $DNS1 ] && echo "server=/$PPTP_DOMAIN/$DNS1" >> $TMPF
  42.  
  43.         # configure routes via remote gateway
  44.         if [ x$IPREMOTE != x ]; then
  45.  
  46.                 # delete existing routes
  47.                 route del default   dev $IFNAME
  48.                 route del $IPREMOTE dev $IFNAME
  49.  
  50.                 # add routes to the gateway and to itself
  51.                 route add $IPREMOTE metric 0 dev $IFNAME
  52.                 route add $IPLOCAL  metric 0 dev $IFNAME
  53.  
  54.                 # add routes to DNS servers
  55.                 [ -n $DNS1 ] && route add $DNS1 gw $IPREMOTE metric 0 dev $IFNAME
  56.                 [ -n $DNS2 ] && route add $DNS2 gw $IPREMOTE metric 0 dev $IFNAME
  57.  
  58.                 # add route to the gateway subnet
  59.                 route add -net ${IPREMOTE%'.'*}.0 gw $IPREMOTE netmask 255.255.255.0 metric 0 dev $IFNAME
  60.                
  61.                 # default gateway
  62.                 metric=1
  63.                 if [ "$DEF_GATEWAY" == "Yes" ]; then
  64.                         metric=0
  65.                 fi
  66.                 route add default gw $IPREMOTE metric $metric dev $IFNAME
  67.  
  68.                 # add routes to additional subnets if any
  69.                 for subnet in $ADD_SUBNETS
  70.                 do
  71.                         route add -net $subnet gw $IPREMOTE netmask 255.255.255.0 metric 0 dev $IFNAME
  72.                 done
  73.  
  74.                 # remove route to the gateway - no longer needed
  75.                 route del $IPREMOTE dev $IFNAME
  76.         fi
  77.  
  78.         # firewall script
  79.         mkdir -p $FIRE_PATH
  80.         echo '#!/bin/sh
  81.         if [ -f /var/run/ppp'$UNIT'.pid ] && [ -n $(cat /var/run/ppp'$UNIT'.pid) ]; then
  82.         iptables -t nat -A PREROUTING -i '$IFNAME' -d '$(nvram get lan_ipaddr)'/'$(nvram get lan_netmask)' -j DROP
  83.         iptables -t nat -A POSTROUTING -o '$IFNAME' -j MASQUERADE
  84.         iptables -A FORWARD -i '$IFNAME' -j wanin
  85.         iptables -A FORWARD -o '$IFNAME' -j wanout
  86.         iptables -A FORWARD -i '$IFNAME' -j upnp
  87.         fi
  88.         ' > "$FIRE_PATH/ppp$UNIT.fire"
  89.         chmod +x "$FIRE_PATH/ppp$UNIT.fire"
  90.  
  91.         mv -f $TMPF $DNSMASQ_CUSTOM
  92.         service routing restart
  93.         service dnsmasq restart
  94.         service firewall restart
  95. }
  96.  
  97. _down()
  98. {
  99.         echo "IP-DOWN"
  100.  
  101.         nvram unset ppp${UNIT}_get_ip
  102.         nvram unset ppp${UNIT}_gateway
  103.  
  104.         rm -f $FIRE_PATH/ppp$UNIT.fire
  105.         rm -f $SCRIPTS_PATH/link
  106.         echo "" > $DNSMASQ_CUSTOM
  107.  
  108.         service routing restart
  109.         service dnsmasq restart
  110.         service firewall restart
  111. }
  112.  
  113. _stop()
  114. {
  115.         echo "Stopping pppd$UNIT..."
  116.  
  117.         killall ppp$UNIT-up
  118.         killall ppp$UNIT-down
  119.         [ -f /var/run/ppp$UNIT.pid ] && kill $(cat /var/run/ppp$UNIT.pid)
  120. }
  121.  
  122. _write_config()
  123. {
  124.         mkdir -p $SCRIPTS_PATH
  125.  
  126.         [ -x "$SCRIPTS_PATH/ppp$UNIT-up" ] || echo '#!/bin/sh
  127. '$0' up' > "$SCRIPTS_PATH/ppp$UNIT-up"
  128.         chmod +x "$SCRIPTS_PATH/ppp$UNIT-up"
  129.  
  130.         [ -x "$SCRIPTS_PATH/ppp$UNIT-down" ] || echo '#!/bin/sh
  131. '$0' down' > "$SCRIPTS_PATH/ppp$UNIT-down"
  132.         chmod +x "$SCRIPTS_PATH/ppp$UNIT-down"
  133.  
  134.         echo "  unit $UNIT
  135.         usepeerdns
  136.         plugin pptp.so
  137.         pptp_server $PPTP_SERVER
  138.         user '$PPTP_USER'
  139.         password '$PPTP_PASSWORD'
  140.         default-asyncmap
  141.         nopcomp
  142.         noaccomp
  143.         novj
  144.         nobsdcomp
  145.         nodeflate
  146.         noauth
  147.         refuse-eap
  148.         maxfail 0
  149.         ip-up-script '$SCRIPTS_PATH/ppp$UNIT-up'
  150.         ip-down-script '$SCRIPTS_PATH/ppp$UNIT-down'
  151.         lcp-echo-interval 15
  152.         lcp-echo-failure 6
  153.         lcp-echo-adaptive
  154.         persist
  155.         holdoff 20
  156.         nomppe-stateful
  157.         mtu 1400" > "$SCRIPTS_PATH/options"
  158. }
  159.  
  160. _start()
  161. {
  162.         echo "Starting pppd$UNIT..."
  163.  
  164.         pppd file "$SCRIPTS_PATH/options"
  165. }
  166.  
  167.  
  168. case $1 in
  169.         start)
  170.                 _stop
  171.                 sleep 2
  172.                 _write_config
  173.                 _start
  174.                 ;;
  175.         stop)
  176.                 _stop
  177.                 ;;
  178.         up)
  179.                 _up
  180.                 ;;
  181.         down)
  182.                 _down
  183.                 ;;
  184.         config)
  185.                 _write_config
  186.                 ;;
  187.   *)
  188.                 echo "usage: $0 (start|stop|up|down|config)"
  189.                 exit 1
  190. esac
  191.  
  192. exit $?
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top