eibgrad

tomato-ovpn-server-restrict.sh

Aug 4th, 2024 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.92 KB | Source Code | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: tomato-ovpn-server-restrict.sh
  5. #       version: 1.0.1, 13-sep-2024, by eibgrad
  6. #       purpose: restrict access to openvpn server(s) by ip/network/domain
  7. #   script type: openvpn (route-up, route-pre-down)
  8. #  installation:
  9. #    1. enable jffs (administration->jffs)
  10. #    2. enable syslog (status->logs->logging configuration->syslog)
  11. #    3. use shell (telnet/ssh) to execute one of the following commands:
  12. #         curl -kLs bit.ly/tomato-installer|tr -d '\r'|sh -s -- --dir /jffs RbdQNmay
  13. #       or
  14. #         wget -qO - bit.ly/tomato-installer|tr -d '\r'|sh -s -- --dir /jffs RbdQNmay
  15. #    4. use vi editor to modify script w/ your preferred permissions:
  16. #         vi /jffs/tomato-ovpn-server-restrict.sh
  17. #    5. create symbolic links:
  18. #         ln -sf /jffs/tomato-ovpn-server-restrict.sh /jffs/route-up
  19. #         ln -sf /jffs/tomato-ovpn-server-restrict.sh /jffs/route-down
  20. #    6. change firewall setting of openvpn server(s) from automatic to custom
  21. #    7. add the following to openvpn server(s) custom configuration:
  22. #         script-security 2
  23. #         route-up /jffs/route-up
  24. #         route-pre-down /jffs/route-down
  25. #    8. reboot
  26. {
  27. # ----------------------------------- FYI ------------------------------------ #
  28. # * one ip/network/domain per line
  29. # * commented lines and anything after ip/network/domain are ignored
  30. # * access is only permitted when source matches ip/network/domain
  31. # * the order of permissions doesn't matter (there is no order of precedence)
  32. # ---------------------------------------------------------------------------- #
  33.  
  34. PERMITTED='
  35. # ----------------------------- BEGIN PERMITTED ------------------------------ #
  36. #0.0.0.0/0 # discontinue further checking; permit ALL
  37. 192.168.63.103
  38. 111.111.111.111 this comment is ignored
  39. 112.112.112.112 # this comment is ignored
  40. #113.113.113.113 this whole line is ignored
  41. 114.114.114.0/24
  42. epdg.epc.mnc071.mcc505.pub.3gppnetwork.org
  43. # ------------------------------ END PERMITTED ------------------------------- #
  44. '
  45. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  46.  
  47. SID="${dev:4:1}"
  48.  
  49. CHAIN="ovpn_server${SID}_restrict"
  50. PROTO="$(nvram get vpn_server${SID}_proto | cut -c1-3)"
  51. PORT="$(nvram get vpn_server${SID}_port)"
  52.  
  53. # function up()
  54. up() {
  55. # create and initialize permissions chain
  56. iptables -N $CHAIN 2>/dev/null || iptables -F $CHAIN
  57.  
  58. # add user-defined permissions to chain
  59. OIFS="$IFS"; IFS=$'\n'
  60. for src in $PERMITTED; do
  61.     # ignore comments and blank lines
  62.     echo $src | grep -Eq '^\s*(#|$)' && continue
  63.  
  64.     # add the permission to the chain
  65.     iptables -A $CHAIN -s $(echo $src | cut -f1) -j ACCEPT
  66. done
  67. IFS="$OIFS"
  68.  
  69. # finalize permissions chain
  70. iptables -A $CHAIN -j DROP
  71.  
  72. # enable tunnel on input and forward chains
  73. iptables -I INPUT -i $dev -j ACCEPT
  74. iptables -I FORWARD -i $dev -j ACCEPT
  75.  
  76. # open server port w/ link to permissions chain
  77. iptables -I INPUT -p $PROTO --dport $PORT -m state --state NEW -j $CHAIN
  78.  
  79. # enable access through the prerouting chain
  80. iptables -t nat -I PREROUTING -p $PROTO --dport $PORT -j ACCEPT
  81. }
  82.  
  83. # function down()
  84. down() {
  85. # disable access through the prerouting chain
  86. iptables -t nat -D PREROUTING -p $PROTO --dport $PORT -j ACCEPT
  87.  
  88. # close server port and unlink from permissions chain
  89. iptables -D INPUT -p $PROTO --dport $PORT -m state --state NEW -j $CHAIN
  90.  
  91. # disable tunnel on input and forward chains
  92. iptables -D INPUT -i $dev -j ACCEPT
  93. iptables -D FORWARD -i $dev -j ACCEPT
  94.  
  95. # flush and delete permissions chain
  96. iptables -F $CHAIN
  97. iptables -X $CHAIN
  98. }
  99.  
  100. # trap event-driven callbacks by openvpn and take appropriate action(s)
  101. case "$script_type" in
  102.           'route-up') up;;
  103.     'route-pre-down') down;;
  104.                    *) echo "warning: unexpected invocation: $script_type";;
  105. esac
  106.  
  107. exit 0
  108. } 2>&1 | logger -t "$(basename $0 | grep -Eo '^.{0,23}')[$$]"
Add Comment
Please, Sign In to add comment