Advertisement
Guest User

autoinstallvpn

a guest
Mar 22nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 94.80 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. # Detect Debian users running the script with "sh" instead of bash                                                      
  12. if readlink /proc/$$/exe | grep -qs "dash"; then                                                                        
  13.         echo "This script needs to be run with bash, not sh"                                                            
  14.         exit 1                                                                                                          
  15. fi                                                                                                                      
  16.                                                                                                                        
  17. if [[ "$EUID" -ne 0 ]]; then                                                                                            
  18.         echo "Sorry, you need to run this as root"                                                                      
  19.         exit 2                                                                                                          
  20. fi                                                                                                                      
  21.                                                                                                                        
  22. if [[ ! -e /dev/net/tun ]]; then                                                                                        
  23.         echo "TUN is not available"                                                                                    
  24.         exit 3                                                                                                          
  25. fi                                                                                                                      
  26.                                                                                                                        
  27. if grep -qs "CentOS release 5" "/etc/redhat-release"; then                                                              
  28.         echo "CentOS 5 is too old and not supported"                                                                    
  29.         exit 4                                                                                                          
  30. fi                                                                                                                      
  31. if [[ -e /etc/debian_version ]]; then                                                                                  
  32.         OS=debian                                                                                                      
  33.         GROUPNAME=nogroup                                                                                              
  34.         RCLOCAL='/etc/rc.local'                                                                                        
  35. elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then                                                      
  36.         OS=centos                                                                                                      
  37.         GROUPNAME=nobody                                                                                                
  38.         RCLOCAL='/etc/rc.d/rc.local'                                                                                    
  39. else                                                                                                                    
  40.         echo "Looks like you aren't running this installer on a Debian, Ubuntu or CentOS system"                        
  41.         exit 5                                                                                                          
  42. fi                                                                                                                      
  43.                                                                                                                        
  44. newclient () {                                                                                                          
  45.         # Generates the custom client.ovpn                                                                              
  46.         cp /etc/openvpn/client-common.txt ~/$1.ovpn                                                                    
  47.         echo "<ca>" >> ~/$1.ovpn                                                                                        
  48.         cat /etc/openvpn/easy-rsa/pki/ca.crt >> ~/$1.ovpn                                                              
  49.         echo "</ca>" >> ~/$1.ovpn                                                                                      
  50.         echo "<cert>" >> ~/$1.ovpn                                                                                      
  51.         cat /etc/openvpn/easy-rsa/pki/issued/$1.crt >> ~/$1.ovpn                                                        
  52.         echo "</cert>" >> ~/$1.ovpn                                                                                    
  53.         echo "<key>" >> ~/$1.ovpn                                                                                      
  54.         cat /etc/openvpn/easy-rsa/pki/private/$1.key >> ~/$1.ovpn                                                      
  55.         echo "</key>" >> ~/$1.ovpn                                                                                      
  56.         echo "<tls-auth>" >> ~/$1.ovpn                                                                                  
  57.         cat /etc/openvpn/ta.key >> ~/$1.ovpn                                                                            
  58.         echo "</tls-auth>" >> ~/$1.ovpn                                                                                
  59. }                                                                                                                      
  60.                                                                                                                        
  61. # Try to get our IP from the system and fallback to the Internet.                                                      
  62. # I do this to make the script compatible with NATed servers (lowendspirit.com)                                        
  63. # and to avoid getting an IPv6.                                                                                        
  64. 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}
  65. \.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)                                                                        
  66. if [[ "$IP" = "" ]]; then                                                                                              
  67.                 IP=$(wget -4qO- "http://whatismyip.akamai.com/")                                                        
  68. fi                                                                                                                      
  69.                                                                                                                        
  70. if [[ -e /etc/openvpn/server.conf ]]; then                                                                              
  71.         while :                                                                                                        
  72.         do                                                                                                              
  73.         clear                                                                                                          
  74.                 echo "Looks like OpenVPN is already installed"                                                          
  75.                 echo ""                                                                                                
  76.                 echo "What do you want to do?"                                                                          
  77.                 echo "   1) Add a new user"                                                                            
  78.                 echo "   2) Revoke an existing user"                                                                    
  79.                 echo "   3) Remove OpenVPN"                                                                            
  80.                 echo "   4) Exit"                                                                                      
  81.                 read -p "Select an option [1-4]: " option                                                              
  82.                 case $option in                                                                                        
  83.                         1)                                                                                              
  84.                         echo ""                                                                                        
  85.                         echo "Tell me a name for the client certificate"                                                
  86.                         echo "Please, use one word only, no special characters"                                        
  87.                         read -p "Client name: " -e -i client CLIENT                                                    
  88.                         cd /etc/openvpn/easy-rsa/                                                                      
  89.                         ./easyrsa build-client-full $CLIENT nopass                                                      
  90.                         # Generates the custom client.ovpn                                                              
  91.                         newclient "$CLIENT"                                                                            
  92.                         echo ""                                                                                        
  93.                         echo "Client $CLIENT added, configuration is available at" ~/"$CLIENT.ovpn"                    
  94.                         exit                                                                                            
  95.                         ;;                                                                                              
  96.                         2)                                                                                              
  97.                         # This option could be documented a bit better and maybe even be simplimplified                
  98.                         # ...but what can I say, I want some sleep too                                                  
  99.                         NUMBEROFCLIENTS=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep -c "^V")                
  100.                         if [[ "$NUMBEROFCLIENTS" = '0' ]]; then                                                        
  101.                                 echo ""                                                                                
  102.                                 echo "You have no existing clients!"                                                    
  103.                                 exit 6                                                                                  
  104.                         fi                                                                                              
  105.                         echo ""                                                                                        
  106.                         echo "Select the existing client certificate you want to revoke"                                
  107.                         tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '      
  108.                         if [[ "$NUMBEROFCLIENTS" = '1' ]]; then                                                        
  109.                                 read -p "Select one client [1]: " CLIENTNUMBER                                          
  110.                         else                                                                                            
  111.                                 read -p "Select one client [1-$NUMBEROFCLIENTS]: " CLIENTNUMBER                        
  112.                         fi                                                                                              
  113.                         CLIENT=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "
  114. $CLIENTNUMBER"p)                                                                                                        
  115.                         cd /etc/openvpn/easy-rsa/                                                                      
  116.                         ./easyrsa --batch revoke $CLIENT                                                                
  117.                         ./easyrsa gen-crl                                                                              
  118.                         rm -rf pki/reqs/$CLIENT.req                                                                    
  119.                         rm -rf pki/private/$CLIENT.key                                                                  
  120.                         rm -rf pki/issued/$CLIENT.crt                                                                  
  121.                         rm -rf /etc/openvpn/crl.pem                                                                    
  122.                         cp /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn/crl.pem                                      
  123.                         # CRL is read with each client connection, when OpenVPN is dropped to nobody                    
  124.                         chown nobody:$GROUPNAME /etc/openvpn/crl.pem                                                    
  125.                         echo ""                                                                                        
  126.                         echo "Certificate for client $CLIENT revoked"                                                  
  127.                         exit                                                                                            
  128.                         ;;                                                                                              
  129.                         3)                                                                                              
  130.                         echo ""                                                                                        
  131.                         read -p "Do you really want to remove OpenVPN? [y/n]: " -e -i n REMOVE                          
  132.                         if [[ "$REMOVE" = 'y' ]]; then                                                                  
  133.                                 PORT=$(grep '^port ' /etc/openvpn/server.conf | cut -d " " -f 2)                        
  134.                                 PROTOCOL=$(grep '^proto ' /etc/openvpn/server.conf | cut -d " " -f 2)                  
  135.                                 IP=$(grep 'iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to ' $RCLOCAL | cut -
  136. d " " -f 11)                                                                                                            
  137.                                 if pgrep firewalld; then                                                                
  138.                                         # Using both permanent and not permanent rules to avoid a firewalld reload.    
  139.                                         firewall-cmd --zone=public --remove-port=$PORT/$PROTOCOL                        
  140.                                         firewall-cmd --zone=trusted --remove-source=10.8.0.0/24                        
  141.                                         firewall-cmd --permanent --zone=public --remove-port=$PORT/$PROTOCOL            
  142.                                         firewall-cmd --permanent --zone=trusted --remove-source=10.8.0.0/24            
  143.                                 fi                                                                                      
  144.                                 if iptables -L -n | grep -qE 'REJECT|DROP|ACCEPT'; then                                
  145.                                         iptables -D INPUT -p $PROTOCOL --dport $PORT -j ACCEPT                          
  146.                                         iptables -D FORWARD -s 10.8.0.0/24 -j ACCEPT                                    
  147.                                         iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT              
  148.                                         sed -i "/iptables -I INPUT -p $PROTOCOL --dport $PORT -j ACCEPT/d" $RCLOCAL    
  149.                                         sed -i "/iptables -I FORWARD -s 10.8.0.0\/24 -j ACCEPT/d" $RCLOCAL              
  150.                                         sed -i "/iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT/d" $
  151. RCLOCAL                                                                                                                
  152.                                 fi                                                                                      
  153.                                 iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP                          
  154.                                 sed -i '/iptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -j SNAT --to /d' $RCLOCAL      
  155.                                 if hash sestatus 2>/dev/null; then                                                      
  156.                                         if sestatus | grep "Current mode" | grep -qs "enforcing"; then                  
  157.                                                 if [[ "$PORT" != '1194' || "$PROTOCOL" = 'tcp' ]]; then                
  158.                                                         semanage port -d -t openvpn_port_t -p $PROTOCOL $PORT          
  159.                                                 fi                                                                      
  160.                                         fi                                                                              
  161.                                 fi                                                                                      
  162.                                 if [[ "$OS" = 'debian' ]]; then                                                        
  163.                                         apt-get remove --purge -y openvpn openvpn-blacklist                            
  164.                                 else                                                                                    
  165.                                         yum remove openvpn -y                                                          
  166.                                 fi                                                                                      
  167.                                 rm -rf /etc/openvpn                                                                    
  168.                                 rm -rf /usr/share/doc/openvpn*                                                          
  169.                                 echo ""                                                                                
  170.                                 echo "OpenVPN removed!"                                                                
  171.                         else                                                                                            
  172.                                 echo ""                                                                                
  173.                                 echo "Removal aborted!"                                                                
  174.                         fi                                                                                              
  175.                         exit                                                                                            
  176.                         ;;                                                                                              
  177.                         4) exit;;                                                                                      
  178.                 esac                                                                                                    
  179.         done                                                                                                            
  180. else                                                                                                                    
  181.         clear                                                                                                          
  182.         echo 'Welcome to this quick OpenVPN "road warrior" installer'                                                  
  183.         echo ""                                                                                                        
  184.         # OpenVPN setup and first user creation                                                                        
  185.         echo "I need to ask you a few questions before starting the setup"                                              
  186.         echo "You can leave the default options and just press enter if you are ok with them"                          
  187.         echo ""                                                                                                        
  188.         echo "First I need to know the IPv4 address of the network interface you want OpenVPN"                          
  189.         echo "listening to."                                                                                            
  190.         read -p "IP address: " -e -i $IP IP                                                                            
  191.         echo ""                                                                                                        
  192.         echo "Which protocol do you want for OpenVPN connections?"                                                      
  193.         echo "   1) UDP (recommended)"                                                                                  
  194.         echo "   2) TCP"                                                                                                
  195.         read -p "Protocol [1-2]: " -e -i 1 PROTOCOL                                                                    
  196.         case $PROTOCOL in                                                                                              
  197.                 1)                                                                                                      
  198.                 PROTOCOL=udp                                                                                            
  199.                 ;;                                                                                                      
  200.                 2)                                                                                                      
  201.                 PROTOCOL=tcp                                                                                            
  202.                 ;;                                                                                                      
  203.         esac                                                                                                            
  204.         echo ""                                                                                                        
  205.         echo "What port do you want OpenVPN listening to?"                                                              
  206.         read -p "Port: " -e -i 1194 PORT                                                                                
  207.         echo ""                                                                                                        
  208.         echo "Which DNS do you want to use with the VPN?"                                                              
  209.         echo "   1) Current system resolvers"                                                                          
  210.         echo "   2) Google"                                                                                            
  211.         echo "   3) OpenDNS"                                                                                            
  212.         echo "   4) NTT"                                                                                                
  213.         echo "   5) Hurricane Electric"                                                                                
  214.         echo "   6) Verisign"                                                                                          
  215.         read -p "DNS [1-6]: " -e -i 1 DNS                                                                              
  216.         echo ""                                                                                                        
  217.         echo "Finally, tell me your name for the client certificate"                                                    
  218.         echo "Please, use one word only, no special characters"                                                        
  219.         read -p "Client name: " -e -i client CLIENT                                                                    
  220.         echo ""                                                                                                        
  221.         echo "Okay, that was all I needed. We are ready to setup your OpenVPN server now"                              
  222.         read -n1 -r -p "Press any key to continue..."                                                                  
  223.         if [[ "$OS" = 'debian' ]]; then                                                                                
  224.                 apt-get update                                                                                          
  225.                 apt-get install openvpn iptables openssl ca-certificates -y                                            
  226.         else                                                                                                            
  227.                 # Else, the distro is CentOS                                                                            
  228.                 yum install epel-release -y                                                                            
  229.                 yum install openvpn iptables openssl wget ca-certificates -y                                            
  230.         fi                                                                                                              
  231.         # An old version of easy-rsa was available by default in some openvpn packages                                  
  232.         if [[ -d /etc/openvpn/easy-rsa/ ]]; then                                                                        
  233.                 rm -rf /etc/openvpn/easy-rsa/                                                                          
  234.         fi                                                                                                              
  235.         # Get easy-rsa                                                                                                  
  236.         wget -O ~/EasyRSA-3.0.1.tgz "https://github.com/OpenVPN/easy-rsa/releases/download/3.0.1/EasyRSA-3.0.1.tgz"    
  237.         tar xzf ~/EasyRSA-3.0.1.tgz -C ~/                                                                              
  238.         mv ~/EasyRSA-3.0.1/ /etc/openvpn/                                                                              
  239.         mv /etc/openvpn/EasyRSA-3.0.1/ /etc/openvpn/easy-rsa/                                                          
  240.         chown -R root:root /etc/openvpn/easy-rsa/                                                                      
  241.         rm -rf ~/EasyRSA-3.0.1.tgz                                                                                      
  242.         cd /etc/openvpn/easy-rsa/                                                                                      
  243.         # Create the PKI, set up the CA, the DH params and the server + client certificates                            
  244.         ./easyrsa init-pki                                                                                              
  245.         ./easyrsa --batch build-ca nopass                                                                              
  246.         ./easyrsa gen-dh                                                                                                
  247.         ./easyrsa build-server-full server nopass                                                                      
  248.         ./easyrsa build-client-full $CLIENT nopass                                                                      
  249.         ./easyrsa gen-crl                                                                                              
  250.         # Move the stuff we need                                                                                        
  251.         cp pki/ca.crt pki/private/ca.key pki/dh.pem pki/issued/server.crt pki/private/server.key /etc/openvpn/easy-rsa/p
  252. ki/crl.pem /etc/openvpn                                                                                                
  253.         # CRL is read with each client connection, when OpenVPN is dropped to nobody                                    
  254.         chown nobody:$GROUPNAME /etc/openvpn/crl.pem                                                                    
  255.         # Generate key for tls-auth                                                                                    
  256.         openvpn --genkey --secret /etc/openvpn/ta.key                                                                  
  257.         # Generate server.conf                                                                                          
  258.         echo "port $PORT                                                                                                
  259. proto $PROTOCOL                                                                                                        
  260. dev tun                                                                                                                
  261. sndbuf 0                                                                                                                
  262. rcvbuf 0                                                                                                                
  263. ca ca.crt                                                                                                              
  264. cert server.crt                                                                                                        
  265. key server.key                                                                                                          
  266. dh dh.pem                                                                                                              
  267. tls-auth ta.key 0                                                                                                      
  268. topology subnet                                                                                                        
  269. server 10.8.0.0 255.255.255.0                                                                                          
  270. ifconfig-pool-persist ipp.txt" > /etc/openvpn/server.conf                                                              
  271.         echo 'push "redirect-gateway def1 bypass-dhcp"' >> /etc/openvpn/server.conf                                    
  272.         # DNS                                                                                                          
  273.         case $DNS in                                                                                                    
  274.                 1)                                                                                                      
  275.                 # Obtain the resolvers from resolv.conf and use them for OpenVPN                                        
  276.                 grep -v '#' /etc/resolv.conf | grep 'nameserver' | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]
  277. {1,3}' | while read line; do                                                                                            
  278.                         echo "push \"dhcp-option DNS $line\"" >> /etc/openvpn/server.conf                              
  279.                 done                                                                                                    
  280.                 ;;                                                                                                      
  281.                 2)                                                                                                      
  282.                 echo 'push "dhcp-option DNS 8.8.8.8"' >> /etc/openvpn/server.conf                                      
  283.                 echo 'push "dhcp-option DNS 8.8.4.4"' >> /etc/openvpn/server.conf                                      
  284.                 ;;                                                                                                      
  285.                 3)                                                                                                      
  286.                 echo 'push "dhcp-option DNS 208.67.222.222"' >> /etc/openvpn/server.conf                                
  287.                 echo 'push "dhcp-option DNS 208.67.220.220"' >> /etc/openvpn/server.conf                                
  288.                 ;;                                                                                                      
  289.                 4)                                                                                                      
  290.                 echo 'push "dhcp-option DNS 129.250.35.250"' >> /etc/openvpn/server.conf                                
  291.                 echo 'push "dhcp-option DNS 129.250.35.251"' >> /etc/openvpn/server.conf                                
  292.                 ;;                                                                                                      
  293.                 5)                                                                                                      
  294.                 echo 'push "dhcp-option DNS 74.82.42.42"' >> /etc/openvpn/server.conf                                  
  295.                 ;;                                                                                                      
  296.                 6)                                                                                                      
  297.                 echo 'push "dhcp-option DNS 64.6.64.6"' >> /etc/openvpn/server.conf                                    
  298.                 echo 'push "dhcp-option DNS 64.6.65.6"' >> /etc/openvpn/server.conf                                    
  299.                 ;;                                                                                                      
  300.         esac                                                                                                            
  301.         echo "keepalive 10 120                                                                                          
  302. cipher AES-256-CBC                                                                                                      
  303. comp-lzo                                                                                                                
  304. user nobody                                                                                                            
  305. group $GROUPNAME                                                                                                        
  306. persist-key                                                                                                            
  307. persist-tun                                                                                                            
  308. status openvpn-status.log                                                                                              
  309. verb 3                                                                                                                  
  310. crl-verify crl.pem" >> /etc/openvpn/server.conf                                                                        
  311.         # Enable net.ipv4.ip_forward for the system                                                                    
  312.         sed -i '/\<net.ipv4.ip_forward\>/c\net.ipv4.ip_forward=1' /etc/sysctl.conf                                      
  313.         if ! grep -q "\<net.ipv4.ip_forward\>" /etc/sysctl.conf; then                                                  
  314.                 echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf                                                        
  315.         fi                                                                                                              
  316.         # Avoid an unneeded reboot                                                                                      
  317.         echo 1 > /proc/sys/net/ipv4/ip_forward                                                                          
  318.         # Needed to use rc.local with some systemd distros                                                              
  319.         if [[ "$OS" = 'debian' && ! -e $RCLOCAL ]]; then                                                                
  320.                 echo '#!/bin/sh -e                                                                                      
  321. exit 0' > $RCLOCAL                                                                                                      
  322.         fi                                                                                                              
  323.         chmod +x $RCLOCAL                                                                                              
  324.         # Set NAT for the VPN subnet                                                                                    
  325.         iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP                                                  
  326.         sed -i "1 a\iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP" $RCLOCAL                            
  327.         if pgrep firewalld; then                                                                                        
  328.                 # We don't use --add-service=openvpn because that would only work with                                  
  329.                 # the default port and protocol. Using both permanent and not permanent                                
  330.                 # rules to avoid a firewalld reload.                                                                    
  331.                 firewall-cmd --zone=public --add-port=$PORT/$PROTOCOL                                                  
  332.                 firewall-cmd --zone=trusted --add-source=10.8.0.0/24                                                    
  333.                 firewall-cmd --permanent --zone=public --add-port=$PORT/$PROTOCOL                                      
  334.                 firewall-cmd --permanent --zone=trusted --add-source=10.8.0.0/24                                        
  335.         fi                                                                                                              
  336.         if iptables -L -n | grep -qE 'REJECT|DROP'; then                                                                
  337.                 # If iptables has at least one REJECT rule, we asume this is needed.                                    
  338.                 # Not the best approach but I can't think of other and this shouldn't                                  
  339.                 # cause problems.                                                                                      
  340.                 iptables -I INPUT -p $PROTOCOL --dport $PORT -j ACCEPT                                                  
  341.                 iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT                                                            
  342.                 iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT                                      
  343.                 sed -i "1 a\iptables -I INPUT -p $PROTOCOL --dport $PORT -j ACCEPT" $RCLOCAL                            
  344.                 sed -i "1 a\iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT" $RCLOCAL                                      
  345.                 sed -i "1 a\iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" $RCLOCAL                
  346.         fi                                                                                                              
  347.         # If SELinux is enabled and a custom port or TCP was selected, we need this                                    
  348.         if hash sestatus 2>/dev/null; then                                                                              
  349.                 if sestatus | grep "Current mode" | grep -qs "enforcing"; then                                          
  350.                         if [[ "$PORT" != '1194' || "$PROTOCOL" = 'tcp' ]]; then                                        
  351.                                 # semanage isn't available in CentOS 6 by default                                      
  352.                                 if ! hash semanage 2>/dev/null; then                                                    
  353.                                         yum install policycoreutils-python -y                                          
  354.                                 fi                                                                                      
  355.                                 semanage port -a -t openvpn_port_t -p $PROTOCOL $PORT                                  
  356.                         fi                                                                                              
  357.                 fi                                                                                                      
  358.         fi                                                                                                              
  359.         # And finally, restart OpenVPN                                                                                  
  360.         if [[ "$OS" = 'debian' ]]; then                                                                                
  361.                 # Little hack to check for systemd                                                                      
  362.                 if pgrep systemd-journal; then                                                                          
  363.                         systemctl restart openvpn@server.service                                                        
  364.                 else                                                                                                    
  365.                         /etc/init.d/openvpn restart                                                                    
  366.                 fi                                                                                                      
  367.         else                                                                                                            
  368.                 if pgrep systemd-journal; then                                                                          
  369.                         systemctl restart openvpn@server.service                                                        
  370.                         systemctl enable openvpn@server.service                                                        
  371.                 else                                                                                                    
  372.                         service openvpn restart                                                                        
  373.                         chkconfig openvpn on                                                                            
  374.                 fi                                                                                                      
  375.         fi                                                                                                              
  376.         # Try to detect a NATed connection and ask about it to potential LowEndSpirit users                            
  377.         EXTERNALIP=$(wget -4qO- "http://whatismyip.akamai.com/")                                                        
  378.         if [[ "$IP" != "$EXTERNALIP" ]]; then                                                                          
  379.                 echo ""                                                                                                
  380.                 echo "Looks like your server is behind a NAT!"                                                          
  381.                 echo ""                                                                                                
  382.                 echo "If your server is NATed (e.g. LowEndSpirit), I need to know the external IP"                      
  383.                 echo "If that's not the case, just ignore this and leave the next field blank"                          
  384.                 read -p "External IP: " -e USEREXTERNALIP                                                              
  385.                 if [[ "$USEREXTERNALIP" != "" ]]; then                                                                  
  386.                         IP=$USEREXTERNALIP                                                                              
  387.                 fi                                                                                                      
  388.         fi                                                                                                              
  389.         # client-common.txt is created so we have a template to add further users later                                
  390.         echo "client                                                                                                    
  391. dev tun                                                                                                                
  392. proto $PROTOCOL                                                                                                        
  393. sndbuf 0                                                                                                                
  394. rcvbuf 0                                                                                                                
  395. remote $IP $PORT                                                                                                        
  396. resolv-retry infinite                                                                                                  
  397. nobind                                                                                                                  
  398. persist-key                                                                                                            
  399. root@tunneltest:~# cat openvpn-install.sh
  400. #!/bin/bash
  401. # OpenVPN road warrior installer for Debian, Ubuntu and CentOS
  402.  
  403. # This script will work on Debian, Ubuntu, CentOS and probably other distros
  404. # of the same families, although no support is offered for them. It isn't
  405. # bulletproof but it will probably work if you simply want to setup a VPN on
  406. # your Debian/Ubuntu/CentOS box. It has been designed to be as unobtrusive and
  407. # universal as possible.
  408.  
  409.  
  410. # Detect Debian users running the script with "sh" instead of bash
  411. if readlink /proc/$$/exe | grep -qs "dash"; then
  412.        echo "This script needs to be run with bash, not sh"
  413.        exit 1
  414. fi
  415.  
  416. if [[ "$EUID" -ne 0 ]]; then
  417.        echo "Sorry, you need to run this as root"
  418.        exit 2
  419. fi
  420.  
  421. if [[ ! -e /dev/net/tun ]]; then                                                                                        
  422.        echo "TUN is not available"                                                                                    
  423.        exit 3                                                                                                          
  424. fi                                                                                                                      
  425.                                                                                                                        
  426. if grep -qs "CentOS release 5" "/etc/redhat-release"; then                                                              
  427.        echo "CentOS 5 is too old and not supported"                                                                    
  428.        exit 4                                                                                                          
  429. fi                                                                                                                      
  430. if [[ -e /etc/debian_version ]]; then                                                                                  
  431.        OS=debian                                                                                                      
  432.        GROUPNAME=nogroup                                                                                              
  433.        RCLOCAL='/etc/rc.local'                                                                                        
  434. elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then                                                      
  435.        OS=centos                                                                                                      
  436.        GROUPNAME=nobody                                                                                                
  437.        RCLOCAL='/etc/rc.d/rc.local'                                                                                    
  438. else                                                                                                                    
  439.        echo "Looks like you aren't running this installer on a Debian, Ubuntu or CentOS system"                        
  440.        exit 5                                                                                                          
  441. fi                                                                                                                      
  442.                                                                                                                        
  443. newclient () {                                                                                                          
  444.        # Generates the custom client.ovpn                                                                              
  445.        cp /etc/openvpn/client-common.txt ~/$1.ovpn                                                                    
  446.        echo "<ca>" >> ~/$1.ovpn                                                                                        
  447.        cat /etc/openvpn/easy-rsa/pki/ca.crt >> ~/$1.ovpn                                                              
  448.        echo "</ca>" >> ~/$1.ovpn                                                                                      
  449.        echo "<cert>" >> ~/$1.ovpn                                                                                      
  450.        cat /etc/openvpn/easy-rsa/pki/issued/$1.crt >> ~/$1.ovpn                                                        
  451.        echo "</cert>" >> ~/$1.ovpn                                                                                    
  452.        echo "<key>" >> ~/$1.ovpn                                                                                      
  453.        cat /etc/openvpn/easy-rsa/pki/private/$1.key >> ~/$1.ovpn                                                      
  454.        echo "</key>" >> ~/$1.ovpn                                                                                      
  455.        echo "<tls-auth>" >> ~/$1.ovpn                                                                                  
  456.        cat /etc/openvpn/ta.key >> ~/$1.ovpn                                                                            
  457.        echo "</tls-auth>" >> ~/$1.ovpn                                                                                
  458. }                                                                                                                      
  459.                                                                                                                        
  460. # Try to get our IP from the system and fallback to the Internet.                                                      
  461. # I do this to make the script compatible with NATed servers (lowendspirit.com)                                        
  462. # and to avoid getting an IPv6.                                                                                        
  463. 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}
  464. \.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)                                                                        
  465. if [[ "$IP" = "" ]]; then                                                                                              
  466.                IP=$(wget -4qO- "http://whatismyip.akamai.com/")                                                        
  467. fi                                                                                                                      
  468.                                                                                                                        
  469. if [[ -e /etc/openvpn/server.conf ]]; then                                                                              
  470.        while :                                                                                                        
  471.        do                                                                                                              
  472.        clear                                                                                                          
  473.                echo "Looks like OpenVPN is already installed"                                                          
  474.                echo ""                                                                                                
  475.                echo "What do you want to do?"                                                                          
  476.                echo "   1) Add a new user"                                                                            
  477.                echo "   2) Revoke an existing user"                                                                    
  478.                echo "   3) Remove OpenVPN"                                                                            
  479.                echo "   4) Exit"                                                                                      
  480.                read -p "Select an option [1-4]: " option                                                              
  481.                case $option in                                                                                        
  482.                        1)                                                                                              
  483.                        echo ""                                                                                        
  484.                        echo "Tell me a name for the client certificate"                                                
  485.                        echo "Please, use one word only, no special characters"                                        
  486.                        read -p "Client name: " -e -i client CLIENT                                                    
  487.                        cd /etc/openvpn/easy-rsa/                                                                      
  488.                        ./easyrsa build-client-full $CLIENT nopass                                                      
  489.                        # Generates the custom client.ovpn                                                              
  490.                        newclient "$CLIENT"                                                                            
  491.                        echo ""                                                                                        
  492.                        echo "Client $CLIENT added, configuration is available at" ~/"$CLIENT.ovpn"                    
  493.                        exit                                                                                            
  494.                        ;;                                                                                              
  495.                        2)                                                                                              
  496.                        # This option could be documented a bit better and maybe even be simplimplified                
  497.                        # ...but what can I say, I want some sleep too                                                  
  498.                        NUMBEROFCLIENTS=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep -c "^V")                
  499.                        if [[ "$NUMBEROFCLIENTS" = '0' ]]; then                                                        
  500.                                echo ""                                                                                
  501.                                echo "You have no existing clients!"                                                    
  502.                                exit 6                                                                                  
  503.                        fi                                                                                              
  504.                        echo ""                                                                                        
  505.                        echo "Select the existing client certificate you want to revoke"                                
  506.                        tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '      
  507.                        if [[ "$NUMBEROFCLIENTS" = '1' ]]; then                                                        
  508.                                read -p "Select one client [1]: " CLIENTNUMBER                                          
  509.                        else                                                                                            
  510.                                read -p "Select one client [1-$NUMBEROFCLIENTS]: " CLIENTNUMBER                        
  511.                        fi                                                                                              
  512.                        CLIENT=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "
  513. $CLIENTNUMBER"p)                                                                                                        
  514.                        cd /etc/openvpn/easy-rsa/                                                                      
  515.                        ./easyrsa --batch revoke $CLIENT                                                                
  516.                        ./easyrsa gen-crl                                                                              
  517.                        rm -rf pki/reqs/$CLIENT.req                                                                    
  518.                        rm -rf pki/private/$CLIENT.key                                                                  
  519.                        rm -rf pki/issued/$CLIENT.crt                                                                  
  520.                        rm -rf /etc/openvpn/crl.pem                                                                    
  521.                        cp /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn/crl.pem                                      
  522.                        # CRL is read with each client connection, when OpenVPN is dropped to nobody                    
  523.                        chown nobody:$GROUPNAME /etc/openvpn/crl.pem                                                    
  524.                        echo ""                                                                                        
  525.                        echo "Certificate for client $CLIENT revoked"                                                  
  526.                        exit                                                                                            
  527.                        ;;                                                                                              
  528.                        3)                                                                                              
  529.                        echo ""                                                                                        
  530.                        read -p "Do you really want to remove OpenVPN? [y/n]: " -e -i n REMOVE                          
  531.                        if [[ "$REMOVE" = 'y' ]]; then                                                                  
  532.                                PORT=$(grep '^port ' /etc/openvpn/server.conf | cut -d " " -f 2)                        
  533.                                PROTOCOL=$(grep '^proto ' /etc/openvpn/server.conf | cut -d " " -f 2)                  
  534.                                IP=$(grep 'iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to ' $RCLOCAL | cut -
  535. d " " -f 11)                                                                                                            
  536.                                if pgrep firewalld; then                                                                
  537.                                        # Using both permanent and not permanent rules to avoid a firewalld reload.    
  538.                                        firewall-cmd --zone=public --remove-port=$PORT/$PROTOCOL                        
  539.                                        firewall-cmd --zone=trusted --remove-source=10.8.0.0/24                        
  540.                                        firewall-cmd --permanent --zone=public --remove-port=$PORT/$PROTOCOL            
  541.                                        firewall-cmd --permanent --zone=trusted --remove-source=10.8.0.0/24            
  542.                                fi                                                                                      
  543.                                if iptables -L -n | grep -qE 'REJECT|DROP|ACCEPT'; then                                
  544.                                        iptables -D INPUT -p $PROTOCOL --dport $PORT -j ACCEPT                          
  545.                                        iptables -D FORWARD -s 10.8.0.0/24 -j ACCEPT                                    
  546.                                        iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT              
  547.                                        sed -i "/iptables -I INPUT -p $PROTOCOL --dport $PORT -j ACCEPT/d" $RCLOCAL    
  548.                                        sed -i "/iptables -I FORWARD -s 10.8.0.0\/24 -j ACCEPT/d" $RCLOCAL              
  549.                                        sed -i "/iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT/d" $
  550. RCLOCAL                                                                                                                
  551.                                fi                                                                                      
  552.                                iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP                          
  553.                                sed -i '/iptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -j SNAT --to /d' $RCLOCAL      
  554.                                if hash sestatus 2>/dev/null; then                                                      
  555.                                        if sestatus | grep "Current mode" | grep -qs "enforcing"; then                  
  556.                                                if [[ "$PORT" != '1194' || "$PROTOCOL" = 'tcp' ]]; then                
  557.                                                        semanage port -d -t openvpn_port_t -p $PROTOCOL $PORT          
  558.                                                fi                                                                      
  559.                                        fi                                                                              
  560.                                fi                                                                                      
  561.                                if [[ "$OS" = 'debian' ]]; then                                                        
  562.                                        apt-get remove --purge -y openvpn openvpn-blacklist                            
  563.                                else                                                                                    
  564.                                        yum remove openvpn -y                                                          
  565.                                fi                                                                                      
  566.                                rm -rf /etc/openvpn                                                                    
  567.                                rm -rf /usr/share/doc/openvpn*                                                          
  568.                                echo ""                                                                                
  569.                                echo "OpenVPN removed!"                                                                
  570.                        else                                                                                            
  571.                                echo ""                                                                                
  572.                                echo "Removal aborted!"                                                                
  573.                        fi                                                                                              
  574.                        exit                                                                                            
  575.                        ;;                                                                                              
  576.                        4) exit;;                                                                                      
  577.                esac                                                                                                    
  578.        done                                                                                                            
  579. else                                                                                                                    
  580.        clear                                                                                                          
  581.        echo 'Welcome to this quick OpenVPN "road warrior" installer'                                                  
  582.        echo ""                                                                                                        
  583.        # OpenVPN setup and first user creation                                                                        
  584.        echo "I need to ask you a few questions before starting the setup"                                              
  585.        echo "You can leave the default options and just press enter if you are ok with them"                          
  586.        echo ""                                                                                                        
  587.        echo "First I need to know the IPv4 address of the network interface you want OpenVPN"                          
  588.        echo "listening to."                                                                                            
  589.        read -p "IP address: " -e -i $IP IP                                                                            
  590.        echo ""                                                                                                        
  591.        echo "Which protocol do you want for OpenVPN connections?"                                                      
  592.        echo "   1) UDP (recommended)"                                                                                  
  593.        echo "   2) TCP"                                                                                                
  594.        read -p "Protocol [1-2]: " -e -i 1 PROTOCOL                                                                    
  595.        case $PROTOCOL in                                                                                              
  596.                1)                                                                                                      
  597.                PROTOCOL=udp                                                                                            
  598.                ;;                                                                                                      
  599.                2)                                                                                                      
  600.                PROTOCOL=tcp                                                                                            
  601.                ;;                                                                                                      
  602.        esac                                                                                                            
  603.        echo ""                                                                                                        
  604.        echo "What port do you want OpenVPN listening to?"                                                              
  605.        read -p "Port: " -e -i 1194 PORT                                                                                
  606.        echo ""                                                                                                        
  607.        echo "Which DNS do you want to use with the VPN?"                                                              
  608.        echo "   1) Current system resolvers"                                                                          
  609.        echo "   2) Google"                                                                                            
  610.        echo "   3) OpenDNS"                                                                                            
  611.        echo "   4) NTT"                                                                                                
  612.        echo "   5) Hurricane Electric"                                                                                
  613.        echo "   6) Verisign"                                                                                          
  614.        read -p "DNS [1-6]: " -e -i 1 DNS                                                                              
  615.        echo ""                                                                                                        
  616.        echo "Finally, tell me your name for the client certificate"                                                    
  617.        echo "Please, use one word only, no special characters"                                                        
  618.        read -p "Client name: " -e -i client CLIENT                                                                    
  619.        echo ""                                                                                                        
  620.        echo "Okay, that was all I needed. We are ready to setup your OpenVPN server now"                              
  621.        read -n1 -r -p "Press any key to continue..."                                                                  
  622.        if [[ "$OS" = 'debian' ]]; then                                                                                
  623.                apt-get update                                                                                          
  624.                apt-get install openvpn iptables openssl ca-certificates -y                                            
  625.        else                                                                                                            
  626.                # Else, the distro is CentOS                                                                            
  627.                yum install epel-release -y                                                                            
  628.                yum install openvpn iptables openssl wget ca-certificates -y                                            
  629.        fi                                                                                                              
  630.        # An old version of easy-rsa was available by default in some openvpn packages                                  
  631.        if [[ -d /etc/openvpn/easy-rsa/ ]]; then                                                                        
  632.                rm -rf /etc/openvpn/easy-rsa/                                                                          
  633.        fi                                                                                                              
  634.        # Get easy-rsa                                                                                                  
  635.        wget -O ~/EasyRSA-3.0.1.tgz "https://github.com/OpenVPN/easy-rsa/releases/download/3.0.1/EasyRSA-3.0.1.tgz"    
  636.        tar xzf ~/EasyRSA-3.0.1.tgz -C ~/                                                                              
  637.        mv ~/EasyRSA-3.0.1/ /etc/openvpn/                                                                              
  638.        mv /etc/openvpn/EasyRSA-3.0.1/ /etc/openvpn/easy-rsa/                                                          
  639.        chown -R root:root /etc/openvpn/easy-rsa/                                                                      
  640.        rm -rf ~/EasyRSA-3.0.1.tgz                                                                                      
  641.        cd /etc/openvpn/easy-rsa/                                                                                      
  642.        # Create the PKI, set up the CA, the DH params and the server + client certificates                            
  643.        ./easyrsa init-pki                                                                                              
  644.        ./easyrsa --batch build-ca nopass                                                                              
  645.        ./easyrsa gen-dh                                                                                                
  646.        ./easyrsa build-server-full server nopass                                                                      
  647.        ./easyrsa build-client-full $CLIENT nopass                                                                      
  648.        ./easyrsa gen-crl                                                                                              
  649.        # Move the stuff we need                                                                                        
  650.        cp pki/ca.crt pki/private/ca.key pki/dh.pem pki/issued/server.crt pki/private/server.key /etc/openvpn/easy-rsa/p
  651. ki/crl.pem /etc/openvpn                                                                                                
  652.        # CRL is read with each client connection, when OpenVPN is dropped to nobody                                    
  653.        chown nobody:$GROUPNAME /etc/openvpn/crl.pem                                                                    
  654.        # Generate key for tls-auth                                                                                    
  655.        openvpn --genkey --secret /etc/openvpn/ta.key                                                                  
  656.        # Generate server.conf                                                                                          
  657.        echo "port $PORT                                                                                                
  658. proto $PROTOCOL                                                                                                        
  659. dev tun                                                                                                                
  660. sndbuf 0                                                                                                                
  661. rcvbuf 0                                                                                                                
  662. ca ca.crt                                                                                                              
  663. cert server.crt                                                                                                        
  664. key server.key                                                                                                          
  665. dh dh.pem                                                                                                              
  666. tls-auth ta.key 0                                                                                                      
  667. topology subnet                                                                                                        
  668. server 10.8.0.0 255.255.255.0                                                                                          
  669. ifconfig-pool-persist ipp.txt" > /etc/openvpn/server.conf                                                              
  670.        echo 'push "redirect-gateway def1 bypass-dhcp"' >> /etc/openvpn/server.conf                                    
  671.        # DNS                                                                                                          
  672.        case $DNS in                                                                                                    
  673.                1)                                                                                                      
  674.                # Obtain the resolvers from resolv.conf and use them for OpenVPN                                        
  675.                grep -v '#' /etc/resolv.conf | grep 'nameserver' | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]
  676. {1,3}' | while read line; do                                                                                            
  677.                        echo "push \"dhcp-option DNS $line\"" >> /etc/openvpn/server.conf                              
  678.                done                                                                                                    
  679.                ;;                                                                                                      
  680.                2)                                                                                                      
  681.                echo 'push "dhcp-option DNS 8.8.8.8"' >> /etc/openvpn/server.conf                                      
  682.                echo 'push "dhcp-option DNS 8.8.4.4"' >> /etc/openvpn/server.conf                                      
  683.                ;;                                                                                                      
  684.                3)                                                                                                      
  685.                echo 'push "dhcp-option DNS 208.67.222.222"' >> /etc/openvpn/server.conf                                
  686.                echo 'push "dhcp-option DNS 208.67.220.220"' >> /etc/openvpn/server.conf                                
  687.                ;;                                                                                                      
  688.                4)                                                                                                      
  689.                echo 'push "dhcp-option DNS 129.250.35.250"' >> /etc/openvpn/server.conf                                
  690.                echo 'push "dhcp-option DNS 129.250.35.251"' >> /etc/openvpn/server.conf                                
  691.                ;;                                                                                                      
  692.                5)                                                                                                      
  693.                echo 'push "dhcp-option DNS 74.82.42.42"' >> /etc/openvpn/server.conf                                  
  694.                ;;                                                                                                      
  695.                6)                                                                                                      
  696.                echo 'push "dhcp-option DNS 64.6.64.6"' >> /etc/openvpn/server.conf                                    
  697.                echo 'push "dhcp-option DNS 64.6.65.6"' >> /etc/openvpn/server.conf                                    
  698.                ;;                                                                                                      
  699.        esac                                                                                                            
  700.        echo "keepalive 10 120                                                                                          
  701. cipher AES-256-CBC                                                                                                      
  702. comp-lzo                                                                                                                
  703. user nobody                                                                                                            
  704. group $GROUPNAME                                                                                                        
  705. persist-key                                                                                                            
  706. persist-tun                                                                                                            
  707. status openvpn-status.log                                                                                              
  708. verb 3                                                                                                                  
  709. crl-verify crl.pem" >> /etc/openvpn/server.conf                                                                        
  710.        # Enable net.ipv4.ip_forward for the system                                                                    
  711.        sed -i '/\<net.ipv4.ip_forward\>/c\net.ipv4.ip_forward=1' /etc/sysctl.conf                                      
  712.        if ! grep -q "\<net.ipv4.ip_forward\>" /etc/sysctl.conf; then                                                  
  713.                echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf                                                        
  714.        fi                                                                                                              
  715.        # Avoid an unneeded reboot                                                                                      
  716.        echo 1 > /proc/sys/net/ipv4/ip_forward                                                                          
  717.        # Needed to use rc.local with some systemd distros                                                              
  718.        if [[ "$OS" = 'debian' && ! -e $RCLOCAL ]]; then                                                                
  719.                echo '#!/bin/sh -e                                                                                      
  720. exit 0' > $RCLOCAL                                                                                                      
  721.        fi                                                                                                              
  722.        chmod +x $RCLOCAL                                                                                              
  723.        # Set NAT for the VPN subnet                                                                                    
  724.        iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP                                                  
  725.        sed -i "1 a\iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP" $RCLOCAL                            
  726.        if pgrep firewalld; then                                                                                        
  727.                # We don't use --add-service=openvpn because that would only work with                                  
  728.                 # the default port and protocol. Using both permanent and not permanent                                
  729.                 # rules to avoid a firewalld reload.                                                                    
  730.                 firewall-cmd --zone=public --add-port=$PORT/$PROTOCOL                                                  
  731.                 firewall-cmd --zone=trusted --add-source=10.8.0.0/24                                                    
  732.                 firewall-cmd --permanent --zone=public --add-port=$PORT/$PROTOCOL                                      
  733.                 firewall-cmd --permanent --zone=trusted --add-source=10.8.0.0/24                                        
  734.         fi                                                                                                              
  735.         if iptables -L -n | grep -qE 'REJECT|DROP'; then                                                                
  736.                 # If iptables has at least one REJECT rule, we asume this is needed.                                    
  737.                 # Not the best approach but I can't think of other and this shouldn't                                  
  738.                 # cause problems.                                                                                      
  739.                 iptables -I INPUT -p $PROTOCOL --dport $PORT -j ACCEPT                                                  
  740.                 iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT                                                            
  741.                 iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT                                      
  742.                 sed -i "1 a\iptables -I INPUT -p $PROTOCOL --dport $PORT -j ACCEPT" $RCLOCAL                            
  743.                 sed -i "1 a\iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT" $RCLOCAL                                      
  744.                 sed -i "1 a\iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" $RCLOCAL                
  745.         fi                                                                                                              
  746.         # If SELinux is enabled and a custom port or TCP was selected, we need this                                    
  747.         if hash sestatus 2>/dev/null; then                                                                              
  748.                 if sestatus | grep "Current mode" | grep -qs "enforcing"; then                                          
  749.                         if [[ "$PORT" != '1194' || "$PROTOCOL" = 'tcp' ]]; then                                        
  750.                                 # semanage isn't available in CentOS 6 by default                                      
  751.                                 if ! hash semanage 2>/dev/null; then                                                    
  752.                                         yum install policycoreutils-python -y                                          
  753.                                 fi                                                                                      
  754.                                 semanage port -a -t openvpn_port_t -p $PROTOCOL $PORT                                  
  755.                         fi                                                                                              
  756.                 fi                                                                                                      
  757.         fi                                                                                                              
  758.         # And finally, restart OpenVPN                                                                                  
  759.         if [[ "$OS" = 'debian' ]]; then                                                                                
  760.                 # Little hack to check for systemd                                                                      
  761.                 if pgrep systemd-journal; then                                                                          
  762.                         systemctl restart openvpn@server.service                                                        
  763.                 else                                                                                                    
  764.                         /etc/init.d/openvpn restart                                                                    
  765.                 fi                                                                                                      
  766.         else                                                                                                            
  767.                 if pgrep systemd-journal; then                                                                          
  768.                         systemctl restart openvpn@server.service                                                        
  769.                         systemctl enable openvpn@server.service                                                        
  770.                 else                                                                                                    
  771.                         service openvpn restart                                                                        
  772.                         chkconfig openvpn on                                                                            
  773.                 fi                                                                                                      
  774.         fi                                                                                                              
  775.         # Try to detect a NATed connection and ask about it to potential LowEndSpirit users                            
  776.         EXTERNALIP=$(wget -4qO- "http://whatismyip.akamai.com/")                                                        
  777.         if [[ "$IP" != "$EXTERNALIP" ]]; then                                                                          
  778.                 echo ""                                                                                                
  779.                 echo "Looks like your server is behind a NAT!"                                                          
  780.                 echo ""                                                                                                
  781.                 echo "If your server is NATed (e.g. LowEndSpirit), I need to know the external IP"                      
  782.                 echo "If that's not the case, just ignore this and leave the next field blank"                          
  783.                 read -p "External IP: " -e USEREXTERNALIP                                                              
  784.                 if [[ "$USEREXTERNALIP" != "" ]]; then                                                                  
  785.                         IP=$USEREXTERNALIP                                                                              
  786.                 fi                                                                                                      
  787.         fi                                                                                                              
  788.         # client-common.txt is created so we have a template to add further users later                                
  789.         echo "client                                                                                                    
  790. dev tun                                                                                                                
  791. proto $PROTOCOL                                                                                                        
  792. sndbuf 0                                                                                                                
  793. rcvbuf 0                                                                                                                
  794. remote $IP $PORT                                                                                                        
  795. resolv-retry infinite                                                                                                  
  796. nobind                                                                                                                  
  797. persist-key                                                                                                            
  798. persist-tun                                                                                                            
  799. remote-cert-tls server                                                                                                  
  800. cipher AES-256-CBC                                                                                                      
  801. comp-lzo                                                                                                                
  802. setenv opt block-outside-dns                                                                                            
  803. key-direction 1                                                                                                        
  804. verb 3" > /etc/openvpn/client-common.txt                                                                                
  805.         # Generates the custom client.ovpn                                                                              
  806.         newclient "$CLIENT"                                                                                            
  807.         echo ""                                                                                                        
  808.         echo "Finished!"                                                                                                
  809.         echo ""                                                                                                        
  810.         echo "Your client configuration is available at" ~/"$CLIENT.ovpn"                                              
  811.         echo "If you want to add more clients, you simply need to run this script again!"                              
  812. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement