Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # install_openvpn.sh - Install OpenVPN and generate required certificates |
| 3 | # |
| 4 | # install_openvpn.sh --client name |
| 5 | # install_openvpn.sh --server [name] |
| 6 | # |
| 7 | # name is used on the CN of the generated cert, and the filename of |
| 8 | # the configuration, certificate and key files. |
| 9 | # |
| 10 | # --server mode configures the host with a running OpenVPN server instance |
| 11 | # --client mode creates a tarball of a client configuration for this server |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 12 | |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 13 | # VPN Config |
| 14 | VPN_SERVER=${VPN_SERVER:-`ifconfig eth0 | awk "/inet addr:/ { print \$2 }" | cut -d: -f2`} # 50.56.12.212 |
| 15 | VPN_PROTO=${VPN_PROTO:-tcp} |
| 16 | VPN_PORT=${VPN_PORT:-6081} |
| 17 | VPN_DEV=${VPN_DEV:-tun} |
| 18 | VPN_CLIENT_NET=${VPN_CLIENT_NET:-172.16.28.0} |
| 19 | VPN_CLIENT_MASK=${VPN_CLIENT_MASK:-255.255.255.0} |
| 20 | VPN_LOCAL_NET=${VPN_LOCAL_NET:-10.0.0.0} |
| 21 | VPN_LOCAL_MASK=${VPN_LOCAL_MASK:-255.255.0.0} |
| 22 | |
| 23 | VPN_DIR=/etc/openvpn |
| 24 | CA_DIR=/etc/openvpn/easy-rsa |
| 25 | |
| 26 | usage() { |
| 27 | echo "$0 - OpenVPN install and certificate generation" |
| 28 | echo "" |
| 29 | echo "$0 --client name" |
| 30 | echo "$0 --server [name]" |
| 31 | echo "" |
| 32 | echo " --server mode configures the host with a running OpenVPN server instance" |
| 33 | echo " --client mode creates a tarball of a client configuration for this server" |
| 34 | exit 1 |
| 35 | } |
| 36 | |
| 37 | if [ -z $1 ]; then |
| 38 | usage |
| 39 | fi |
| 40 | |
| 41 | # Install OpenVPN |
| 42 | if [ ! -x `which openvpn` ]; then |
| 43 | apt-get install -y openvpn bridge-utils |
| 44 | fi |
| 45 | if [ ! -d $CA_DIR ]; then |
| 46 | cp -pR /usr/share/doc/openvpn/examples/easy-rsa/2.0/ $CA_DIR |
| 47 | fi |
| 48 | |
| 49 | OPWD=`pwd` |
| 50 | cd $CA_DIR |
| 51 | source ./vars |
| 52 | |
| 53 | # Override the defaults |
| 54 | export KEY_COUNTRY="US" |
| 55 | export KEY_PROVINCE="TX" |
| 56 | export KEY_CITY="SanAntonio" |
| 57 | export KEY_ORG="Cloudbuilders" |
| 58 | export KEY_EMAIL="rcb@lists.rackspace.com" |
| 59 | |
| 60 | if [ ! -r $CA_DIR/keys/dh1024.pem ]; then |
| 61 | # Initialize a new CA |
| 62 | $CA_DIR/clean-all |
| 63 | $CA_DIR/build-dh |
| 64 | $CA_DIR/pkitool --initca |
| 65 | openvpn --genkey --secret $CA_DIR/keys/ta.key ## Build a TLS key |
| 66 | fi |
| 67 | |
| 68 | do_server() { |
| 69 | NAME=$1 |
| 70 | # Generate server certificate |
| 71 | $CA_DIR/pkitool --server $NAME |
| 72 | |
| 73 | (cd $CA_DIR/keys; |
| 74 | cp $NAME.crt $NAME.key ca.crt dh1024.pem ta.key $VPN_DIR |
| 75 | ) |
| 76 | cat >$VPN_DIR/$NAME.conf <<EOF |
| 77 | proto $VPN_PROTO |
| 78 | port $VPN_PORT |
| 79 | dev $VPN_DEV |
| 80 | cert $NAME.crt |
| 81 | key $NAME.key # This file should be kept secret |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 82 | ca ca.crt |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 83 | dh dh1024.pem |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 84 | duplicate-cn |
| 85 | server $VPN_CLIENT_NET $VPN_CLIENT_MASK |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 86 | ifconfig-pool-persist ipp.txt |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 87 | push "route $VPN_LOCAL_NET $VPN_LOCAL_MASK" |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 88 | comp-lzo |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 89 | user nobody |
| 90 | group nobody |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 91 | persist-key |
| 92 | persist-tun |
| 93 | status openvpn-status.log |
| 94 | EOF |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 95 | /etc/init.d/openvpn restart |
| 96 | } |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 97 | |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 98 | do_client() { |
| 99 | NAME=$1 |
| 100 | # Generate a client certificate |
| 101 | $CA_DIR/pkitool $NAME |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 102 | |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 103 | TMP_DIR=`mktemp -d` |
| 104 | (cd $CA_DIR/keys; |
| 105 | cp -p ca.crt ta.key $NAME.key $NAME.crt $TMP_DIR |
| 106 | ) |
| 107 | if [ -r $VPN_DIR/hostname ]; then |
| 108 | HOST=`cat $VPN_DIR/hostname` |
| 109 | else |
| 110 | HOST=`hostname` |
| 111 | fi |
| 112 | cat >$TMP_DIR/$HOST.conf <<EOF |
| 113 | proto $VPN_PROTO |
| 114 | port $VPN_PORT |
| 115 | dev $VPN_DEV |
| 116 | cert $NAME.crt |
| 117 | key $NAME.key # This file should be kept secret |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 118 | ca ca.crt |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 119 | client |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 120 | remote $VPN_SERVER $VPN_PORT |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 121 | resolv-retry infinite |
| 122 | nobind |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 123 | user nobody |
| 124 | group nobody |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 125 | persist-key |
| 126 | persist-tun |
| 127 | comp-lzo |
| 128 | verb 3 |
| 129 | EOF |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 130 | (cd $TMP_DIR; tar cf $OPWD/$NAME.tar *) |
| 131 | rm -rf $TMP_DIR |
| 132 | echo "Client certificate and configuration is in $OPWD/$NAME.tar" |
| 133 | } |
| 134 | |
| 135 | # Process command line args |
| 136 | case $1 in |
| 137 | --client) if [ -z $2 ]; then |
| 138 | usage |
| 139 | fi |
| 140 | do_client $2 |
| 141 | ;; |
| 142 | --server) if [ -z $2 ]; then |
| 143 | NAME=`hostname` |
| 144 | else |
| 145 | NAME=$2 |
| 146 | # Save for --client use |
| 147 | echo $NAME >$VPN_DIR/hostname |
| 148 | fi |
| 149 | do_server $NAME |
| 150 | ;; |
| 151 | --clean) $CA_DIR/clean-all |
| 152 | ;; |
| 153 | *) usage |
| 154 | esac |