Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1. #!/bin/bash
  2. # OpenVPN road warrior installer for Debian, Ubuntu and CentOS
  3.  
  4. # This script will work on Debian, Ubuntu, CentOS and probably other distros
  5. # of the same families, although no support is offered for them. It isn't
  6. # bulletproof but it will probably work if you simply want to setup a VPN on
  7. # your Debian/Ubuntu/CentOS box. It has been designed to be as unobtrusive and
  8. # universal as possible.
  9.  
  10.  
  11. if [[ "$USER" != 'root' ]]; then
  12. echo "Sorry, you need to run this as root"
  13. exit
  14. fi
  15.  
  16.  
  17. if [[ ! -e /dev/net/tun ]]; then
  18. echo "TUN/TAP is not available"
  19. exit
  20. fi
  21.  
  22.  
  23. if grep -qs "CentOS release 5" "/etc/redhat-release"; then
  24. echo "CentOS 5 is too old and not supported"
  25. exit
  26. fi
  27.  
  28. if [[ -e /etc/debian_version ]]; then
  29. OS=debian
  30. RCLOCAL='/etc/rc.local'
  31. elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then
  32. OS=centos
  33. RCLOCAL='/etc/rc.d/rc.local'
  34. # Needed for CentOS 7
  35. chmod +x /etc/rc.d/rc.local
  36. else
  37. echo "Looks like you aren't running this installer on a Debian, Ubuntu or CentOS system"
  38. exit
  39. fi
  40.  
  41. newclient () {
  42. # Generates the client.ovpn
  43. cp /usr/share/doc/openvpn*/*ample*/sample-config-files/client.conf ~/$1.ovpn
  44. sed -i "/ca ca.crt/d" ~/$1.ovpn
  45. sed -i "/cert client.crt/d" ~/$1.ovpn
  46. sed -i "/key client.key/d" ~/$1.ovpn
  47. echo "<ca>" >> ~/$1.ovpn
  48. cat /etc/openvpn/easy-rsa/2.0/keys/ca.crt >> ~/$1.ovpn
  49. echo "</ca>" >> ~/$1.ovpn
  50. echo "<cert>" >> ~/$1.ovpn
  51. cat /etc/openvpn/easy-rsa/2.0/keys/$1.crt >> ~/$1.ovpn
  52. echo "</cert>" >> ~/$1.ovpn
  53. echo "<key>" >> ~/$1.ovpn
  54. cat /etc/openvpn/easy-rsa/2.0/keys/$1.key >> ~/$1.ovpn
  55. echo "</key>" >> ~/$1.ovpn
  56. }
  57.  
  58.  
  59. # Try to get our IP from the system and fallback to the Internet.
  60. # I do this to make the script compatible with NATed servers (lowendspirit.com)
  61. # and to avoid getting an IPv6.
  62. IP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
  63. if [[ "$IP" = "" ]]; then
  64. IP=$(wget -qO- ipv4.icanhazip.com)
  65. fi
  66.  
  67.  
  68. if [[ -e /etc/openvpn/server.conf ]]; then
  69. while :
  70. do
  71. clear
  72. echo "Looks like OpenVPN is already installed"
  73. echo ""
  74. echo "What do you want to do?"
  75. echo " 1) Add a cert for a new user"
  76. echo " 2) Revoke existing user cert"
  77. echo " 3) Remove OpenVPN"
  78. echo " 4) Exit"
  79. read -p "Select an option [1-4]: " option
  80. case $option in
  81. 1)
  82. echo ""
  83. echo "Tell me a name for the client cert"
  84. echo "Please, use one word only, no special characters"
  85. read -p "Client name: " -e -i client CLIENT
  86. cd /etc/openvpn/easy-rsa/2.0/
  87. source ./vars
  88. # build-key for the client
  89. export KEY_CN="$CLIENT"
  90. export EASY_RSA="${EASY_RSA:-.}"
  91. "$EASY_RSA/pkitool" $CLIENT
  92. # Generate the client.ovpn
  93. newclient "$CLIENT"
  94. echo ""
  95. echo "Client $CLIENT added, certs available at ~/$CLIENT.ovpn"
  96. exit
  97. ;;
  98. 2)
  99. # This option could be documented a bit better and maybe even be simplimplified
  100. # ...but what can I say, I want some sleep too
  101. NUMBEROFCLIENTS=$(tail -n +2 /etc/openvpn/easy-rsa/2.0/keys/index.txt | grep "^V" | wc -l)
  102. if [[ "$NUMBEROFCLIENTS" = '0' ]]; then
  103. echo ""
  104. echo "You have no existing clients!"
  105. exit
  106. fi
  107. echo ""
  108. echo "Select the existing client certificate you want to revoke"
  109. tail -n +2 /etc/openvpn/easy-rsa/2.0/keys/index.txt | grep "^V" | cut -d '/' -f 7 | cut -d '=' -f 2 | nl -s ') '
  110. if [[ "$NUMBEROFCLIENTS" = '1' ]]; then
  111. read -p "Select one client [1]: " CLIENTNUMBER
  112. else
  113. read -p "Select one client [1-$NUMBEROFCLIENTS]: " CLIENTNUMBER
  114. fi
  115. CLIENT=$(tail -n +2 /etc/openvpn/easy-rsa/2.0/keys/index.txt | grep "^V" | cut -d '/' -f 7 | cut -d '=' -f 2 | sed -n "$CLIENTNUMBER"p)
  116. cd /etc/openvpn/easy-rsa/2.0/
  117. . /etc/openvpn/easy-rsa/2.0/vars
  118. . /etc/openvpn/easy-rsa/2.0/revoke-full $CLIENT
  119. # If it's the first time revoking a cert, we need to add the crl-verify line
  120. if ! grep -q "crl-verify" "/etc/openvpn/server.conf"; then
  121. echo "crl-verify /etc/openvpn/easy-rsa/2.0/keys/crl.pem" >> "/etc/openvpn/server.conf"
  122. # And restart
  123. if pgrep systemd-journal; then
  124. systemctl restart openvpn@server.service
  125. else
  126. if [[ "$OS" = 'debian' ]]; then
  127. /etc/init.d/openvpn restart
  128. else
  129. service openvpn restart
  130. fi
  131. fi
  132. fi
  133. echo ""
  134. echo "Certificate for client $CLIENT revoked"
  135. exit
  136. ;;
  137. 3)
  138. echo ""
  139. read -p "Do you really want to remove OpenVPN? [y/n]: " -e -i n REMOVE
  140. if [[ "$REMOVE" = 'y' ]]; then
  141. if [[ "$OS" = 'debian' ]]; then
  142. apt-get remove --purge -y openvpn openvpn-blacklist
  143. else
  144. yum remove openvpn -y
  145. fi
  146. rm -rf /etc/openvpn
  147. rm -rf /usr/share/doc/openvpn*
  148. sed -i '/--dport 53 -j REDIRECT --to-port/d' $RCLOCAL
  149. sed -i '/iptables -t nat -A POSTROUTING -s 100.64.0.0/d' $RCLOCAL
  150. echo ""
  151. echo "OpenVPN removed!"
  152. else
  153. echo ""
  154. echo "Removal aborted!"
  155. fi
  156. exit
  157. ;;
  158. 4) exit;;
  159. esac
  160. done
  161. else
  162. clear
  163. echo 'Welcome to this quick OpenVPN "road warrior" installer'
  164. echo ""
  165. # OpenVPN setup and first user creation
  166. echo "I need to ask you a few questions before starting the setup"
  167. echo "You can leave the default options and just press enter if you are ok with them"
  168. echo ""
  169. echo "First I need to know the IPv4 address of the network interface you want OpenVPN"
  170. echo "listening to."
  171. read -p "IP address: " -e -i $IP IP
  172. echo ""
  173. echo "What port do you want for OpenVPN?"
  174. read -p "Port: " -e -i 1194 PORT
  175. echo ""
  176. echo "Do you want OpenVPN to be available at port 53 too?"
  177. echo "This can be useful to connect under restrictive networks"
  178. read -p "Listen at port 53 [y/n]: " -e -i n ALTPORT
  179. echo ""
  180. echo "Do you want to enable internal networking for the VPN?"
  181. echo "This can allow VPN clients to communicate between them"
  182. read -p "Allow internal networking [y/n]: " -e -i n INTERNALNETWORK
  183. echo ""
  184. echo "What DNS do you want to use with the VPN?"
  185. echo " 1) Current system resolvers"
  186. echo " 2) OpenDNS"
  187. echo " 3) Level 3"
  188. echo " 4) NTT"
  189. echo " 5) Hurricane Electric"
  190. echo " 6) Yandex"
  191. read -p "DNS [1-6]: " -e -i 1 DNS
  192. echo ""
  193. echo "Finally, tell me your name for the client cert"
  194. echo "Please, use one word only, no special characters"
  195. read -p "Client name: " -e -i client CLIENT
  196. echo ""
  197. echo "Okay, that was all I needed. We are ready to setup your OpenVPN server now"
  198. read -n1 -r -p "Press any key to continue..."
  199. if [[ "$OS" = 'debian' ]]; then
  200. apt-get update
  201. apt-get install openvpn iptables openssl -y
  202. else
  203. # Else, the distro is CentOS
  204. yum install epel-release -y
  205. yum install openvpn iptables openssl wget -y
  206. fi
  207. # An old version of easy-rsa was available by default in some openvpn packages
  208. if [[ -d /etc/openvpn/easy-rsa/2.0/ ]]; then
  209. rm -f /etc/openvpn/easy-rsa/2.0/
  210. fi
  211. # Get easy-rsa
  212. wget --no-check-certificate -O ~/easy-rsa.tar.gz https://github.com/OpenVPN/easy-rsa/archive/2.2.2.tar.gz
  213. tar xzf ~/easy-rsa.tar.gz -C ~/
  214. mkdir -p /etc/openvpn/easy-rsa/2.0/
  215. cp ~/easy-rsa-2.2.2/easy-rsa/2.0/* /etc/openvpn/easy-rsa/2.0/
  216. rm -rf ~/easy-rsa-2.2.2
  217. rm -rf ~/easy-rsa.tar.gz
  218. cd /etc/openvpn/easy-rsa/2.0/
  219. # Let's fix one thing first...
  220. cp -u -p openssl-1.0.0.cnf openssl.cnf
  221. # Create the PKI
  222. . /etc/openvpn/easy-rsa/2.0/vars
  223. . /etc/openvpn/easy-rsa/2.0/clean-all
  224. # The following lines are from build-ca. I don't use that script directly
  225. # because it's interactive and we don't want that. Yes, this could break
  226. # the installation script if build-ca changes in the future.
  227. export EASY_RSA="${EASY_RSA:-.}"
  228. "$EASY_RSA/pkitool" --initca $*
  229. # Same as the last time, we are going to run build-key-server
  230. export EASY_RSA="${EASY_RSA:-.}"
  231. "$EASY_RSA/pkitool" --server server
  232. # Now the client keys. We need to set KEY_CN or the stupid pkitool will cry
  233. export KEY_CN="$CLIENT"
  234. export EASY_RSA="${EASY_RSA:-.}"
  235. "$EASY_RSA/pkitool" $CLIENT
  236. # DH params
  237. . /etc/openvpn/easy-rsa/2.0/build-dh
  238. # Let's configure the server
  239. cd /usr/share/doc/openvpn*/*ample*/sample-config-files
  240. if [[ "$OS" = 'debian' ]]; then
  241. gunzip -d server.conf.gz
  242. fi
  243. cp server.conf /etc/openvpn/
  244. cd /etc/openvpn/easy-rsa/2.0/keys
  245. cp ca.crt ca.key dh2048.pem server.crt server.key /etc/openvpn
  246. cd /etc/openvpn/
  247. # Set the server configuration
  248. sed -i 's|dh dh1024.pem|dh dh2048.pem|' server.conf
  249. sed -i 's|;push "redirect-gateway def1 bypass-dhcp"|push "redirect-gateway def1 bypass-dhcp"|' server.conf
  250. sed -i "s|port 1194|port $PORT|" server.conf
  251. # DNS
  252. case $DNS in
  253. 1)
  254. # Obtain the resolvers from resolv.conf and use them for OpenVPN
  255. grep -v '#' /etc/resolv.conf | grep 'nameserver' | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | while read line; do
  256. sed -i "/;push \"dhcp-option DNS 208.67.220.220\"/a\push \"dhcp-option DNS $line\"" server.conf
  257. done
  258. ;;
  259. 2)
  260. sed -i 's|;push "dhcp-option DNS 208.67.222.222"|push "dhcp-option DNS 208.67.222.222"|' server.conf
  261. sed -i 's|;push "dhcp-option DNS 208.67.220.220"|push "dhcp-option DNS 208.67.220.220"|' server.conf
  262. ;;
  263. 3)
  264. sed -i 's|;push "dhcp-option DNS 208.67.222.222"|push "dhcp-option DNS 4.2.2.2"|' server.conf
  265. sed -i 's|;push "dhcp-option DNS 208.67.220.220"|push "dhcp-option DNS 4.2.2.4"|' server.conf
  266. ;;
  267. 4)
  268. sed -i 's|;push "dhcp-option DNS 208.67.222.222"|push "dhcp-option DNS 129.250.35.250"|' server.conf
  269. sed -i 's|;push "dhcp-option DNS 208.67.220.220"|push "dhcp-option DNS 129.250.35.251"|' server.conf
  270. ;;
  271. 5)
  272. sed -i 's|;push "dhcp-option DNS 208.67.222.222"|push "dhcp-option DNS 74.82.42.42"|' server.conf
  273. ;;
  274. 6)
  275. sed -i 's|;push "dhcp-option DNS 208.67.222.222"|push "dhcp-option DNS 77.88.8.8"|' server.conf
  276. sed -i 's|;push "dhcp-option DNS 208.67.220.220"|push "dhcp-option DNS 77.88.8.1"|' server.conf
  277. ;;
  278. esac
  279. # Listen at port 53 too if user wants that
  280. if [[ "$ALTPORT" = 'y' ]]; then
  281. iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-port $PORT
  282. sed -i "1 a\iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-port $PORT" $RCLOCAL
  283. fi
  284. # Enable net.ipv4.ip_forward for the system
  285. if [[ "$OS" = 'debian' ]]; then
  286. sed -i 's|#net.ipv4.ip_forward=1|net.ipv4.ip_forward=1|' /etc/sysctl.conf
  287. else
  288. # CentOS 5 and 6
  289. sed -i 's|net.ipv4.ip_forward = 0|net.ipv4.ip_forward = 1|' /etc/sysctl.conf
  290. # CentOS 7
  291. if ! grep -q "net.ipv4.ip_forward=1" "/etc/sysctl.conf"; then
  292. echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
  293. fi
  294. fi
  295. # Avoid an unneeded reboot
  296. echo 1 > /proc/sys/net/ipv4/ip_forward
  297. # Set iptables
  298. if [[ "$INTERNALNETWORK" = 'y' ]]; then
  299. iptables -t nat -A POSTROUTING -s 100.64.0.0/10 ! -d 100.64.0.0/10 -j SNAT --to $IP
  300. sed -i "1 a\iptables -t nat -A POSTROUTING -s 100.64.0.0/10 ! -d 100.64.0.0/10 -j SNAT --to $IP" $RCLOCAL
  301. else
  302. iptables -t nat -A POSTROUTING -s 100.64.0.0/10 -j SNAT --to $IP
  303. sed -i "1 a\iptables -t nat -A POSTROUTING -s 100.64.0.0/10 -j SNAT --to $IP" $RCLOCAL
  304. fi
  305. # And finally, restart OpenVPN
  306. if [[ "$OS" = 'debian' ]]; then
  307. # Little hack to check for systemd
  308. if pgrep systemd-journal; then
  309. systemctl restart openvpn@server.service
  310. else
  311. /etc/init.d/openvpn restart
  312. fi
  313. else
  314. if pgrep systemd-journal; then
  315. systemctl restart openvpn@server.service
  316. systemctl enable openvpn@server.service
  317. else
  318. service openvpn restart
  319. chkconfig openvpn on
  320. fi
  321. fi
  322. # Try to detect a NATed connection and ask about it to potential LowEndSpirit
  323. # users
  324. EXTERNALIP=$(wget -qO- ipv4.icanhazip.com)
  325. if [[ "$IP" != "$EXTERNALIP" ]]; then
  326. echo ""
  327. echo "Looks like your server is behind a NAT!"
  328. echo ""
  329. echo "If your server is NATed (LowEndSpirit), I need to know the external IP"
  330. echo "If that's not the case, just ignore this and leave the next field blank"
  331. read -p "External IP: " -e USEREXTERNALIP
  332. if [[ "$USEREXTERNALIP" != "" ]]; then
  333. IP=$USEREXTERNALIP
  334. fi
  335. fi
  336. # IP/port set on the default client.conf so we can add further users
  337. # without asking for them
  338. sed -i "s|remote my-server-1 1194|remote $IP $PORT|" /usr/share/doc/openvpn*/*ample*/sample-config-files/client.conf
  339. # Generate the client.ovpn
  340. newclient "$CLIENT"
  341. echo ""
  342. echo "Finished!"
  343. echo ""
  344. echo "Your client config is available at ~/$CLIENT.ovpn"
  345. echo "If you want to add more clients, you simply need to run this script another time!"
  346. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement