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 | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 13 | # Get config file |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 14 | if [ -e localrc ]; then |
| 15 | . localrc |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 16 | fi |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 17 | if [ -e vpnrc ]; then |
| 18 | . vpnrc |
| 19 | fi |
| 20 | |
| 21 | # Do some IP manipulation |
| 22 | function cidr2netmask() { |
| 23 | set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0 |
| 24 | if [[ $1 -gt 1 ]]; then |
| 25 | shift $1 |
| 26 | else |
| 27 | shift |
| 28 | fi |
| 29 | echo ${1-0}.${2-0}.${3-0}.${4-0} |
| 30 | } |
| 31 | |
| 32 | FIXED_NET=`echo $FIXED_RANGE | cut -d'/' -f1` |
| 33 | FIXED_CIDR=`echo $FIXED_RANGE | cut -d'/' -f2` |
| 34 | FIXED_MASK=`cidr2netmask $FIXED_CIDR` |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 35 | |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 36 | # VPN Config |
| 37 | VPN_SERVER=${VPN_SERVER:-`ifconfig eth0 | awk "/inet addr:/ { print \$2 }" | cut -d: -f2`} # 50.56.12.212 |
| 38 | VPN_PROTO=${VPN_PROTO:-tcp} |
| 39 | VPN_PORT=${VPN_PORT:-6081} |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 40 | VPN_DEV=${VPN_DEV:-tap0} |
| 41 | VPN_BRIDGE=${VPN_BRIDGE:-br100} |
| 42 | VPN_BRIDGE_IF=${VPN_BRIDGE_IF:-$FLAT_INTERFACE} |
| 43 | VPN_CLIENT_NET=${VPN_CLIENT_NET:-$FIXED_NET} |
| 44 | VPN_CLIENT_MASK=${VPN_CLIENT_MASK:-$FIXED_MASK} |
| 45 | VPN_CLIENT_DHCP="${VPN_CLIENT_DHCP:-net.1 net.254}" |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 46 | |
| 47 | VPN_DIR=/etc/openvpn |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 48 | CA_DIR=$VPN_DIR/easy-rsa |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 49 | |
| 50 | usage() { |
| 51 | echo "$0 - OpenVPN install and certificate generation" |
| 52 | echo "" |
| 53 | echo "$0 --client name" |
| 54 | echo "$0 --server [name]" |
| 55 | echo "" |
| 56 | echo " --server mode configures the host with a running OpenVPN server instance" |
| 57 | echo " --client mode creates a tarball of a client configuration for this server" |
| 58 | exit 1 |
| 59 | } |
| 60 | |
| 61 | if [ -z $1 ]; then |
| 62 | usage |
| 63 | fi |
| 64 | |
| 65 | # Install OpenVPN |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 66 | VPN_EXEC=`which openvpn` |
| 67 | if [ -z "$VPN_EXEC" -o ! -x "$VPN_EXEC" ]; then |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 68 | apt-get install -y openvpn bridge-utils |
| 69 | fi |
| 70 | if [ ! -d $CA_DIR ]; then |
| 71 | cp -pR /usr/share/doc/openvpn/examples/easy-rsa/2.0/ $CA_DIR |
| 72 | fi |
| 73 | |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 74 | # Keep track of the current directory |
| 75 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 76 | TOP_DIR=$(cd $TOOLS_DIR/.. && pwd) |
| 77 | |
| 78 | WEB_DIR=$TOP_DIR/../vpn |
| 79 | if [[ ! -d $WEB_DIR ]]; then |
| 80 | mkdir -p $WEB_DIR |
| 81 | fi |
| 82 | WEB_DIR=$(cd $TOP_DIR/../vpn && pwd) |
| 83 | |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 84 | cd $CA_DIR |
| 85 | source ./vars |
| 86 | |
| 87 | # Override the defaults |
| 88 | export KEY_COUNTRY="US" |
| 89 | export KEY_PROVINCE="TX" |
| 90 | export KEY_CITY="SanAntonio" |
| 91 | export KEY_ORG="Cloudbuilders" |
| 92 | export KEY_EMAIL="rcb@lists.rackspace.com" |
| 93 | |
| 94 | if [ ! -r $CA_DIR/keys/dh1024.pem ]; then |
| 95 | # Initialize a new CA |
| 96 | $CA_DIR/clean-all |
| 97 | $CA_DIR/build-dh |
| 98 | $CA_DIR/pkitool --initca |
| 99 | openvpn --genkey --secret $CA_DIR/keys/ta.key ## Build a TLS key |
| 100 | fi |
| 101 | |
| 102 | do_server() { |
| 103 | NAME=$1 |
| 104 | # Generate server certificate |
| 105 | $CA_DIR/pkitool --server $NAME |
| 106 | |
| 107 | (cd $CA_DIR/keys; |
| 108 | cp $NAME.crt $NAME.key ca.crt dh1024.pem ta.key $VPN_DIR |
| 109 | ) |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 110 | cat >$VPN_DIR/br-up <<EOF |
| 111 | #!/bin/bash |
| 112 | |
| 113 | BR="$VPN_BRIDGE" |
| 114 | TAP="\$1" |
| 115 | |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 116 | if [[ ! -d /sys/class/net/\$BR ]]; then |
| 117 | brctl addbr \$BR |
| 118 | fi |
| 119 | |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 120 | for t in \$TAP; do |
| 121 | openvpn --mktun --dev \$t |
| 122 | brctl addif \$BR \$t |
| 123 | ifconfig \$t 0.0.0.0 promisc up |
| 124 | done |
| 125 | EOF |
| 126 | chmod +x $VPN_DIR/br-up |
| 127 | cat >$VPN_DIR/br-down <<EOF |
| 128 | #!/bin/bash |
| 129 | |
| 130 | BR="$VPN_BRIDGE" |
| 131 | TAP="\$1" |
| 132 | |
| 133 | for i in \$TAP; do |
| 134 | brctl delif \$BR $t |
| 135 | openvpn --rmtun --dev \$i |
| 136 | done |
| 137 | EOF |
| 138 | chmod +x $VPN_DIR/br-down |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 139 | cat >$VPN_DIR/$NAME.conf <<EOF |
| 140 | proto $VPN_PROTO |
| 141 | port $VPN_PORT |
| 142 | dev $VPN_DEV |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 143 | up $VPN_DIR/br-up |
| 144 | down $VPN_DIR/br-down |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 145 | cert $NAME.crt |
| 146 | key $NAME.key # This file should be kept secret |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 147 | ca ca.crt |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 148 | dh dh1024.pem |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 149 | duplicate-cn |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 150 | server-bridge $VPN_CLIENT_NET $VPN_CLIENT_MASK $VPN_CLIENT_DHCP |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 151 | ifconfig-pool-persist ipp.txt |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 152 | comp-lzo |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 153 | user nobody |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 154 | group nogroup |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 155 | persist-key |
| 156 | persist-tun |
| 157 | status openvpn-status.log |
| 158 | EOF |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 159 | /etc/init.d/openvpn restart |
| 160 | } |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 161 | |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 162 | do_client() { |
| 163 | NAME=$1 |
| 164 | # Generate a client certificate |
| 165 | $CA_DIR/pkitool $NAME |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 166 | |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 167 | TMP_DIR=`mktemp -d` |
| 168 | (cd $CA_DIR/keys; |
| 169 | cp -p ca.crt ta.key $NAME.key $NAME.crt $TMP_DIR |
| 170 | ) |
| 171 | if [ -r $VPN_DIR/hostname ]; then |
| 172 | HOST=`cat $VPN_DIR/hostname` |
| 173 | else |
| 174 | HOST=`hostname` |
| 175 | fi |
| 176 | cat >$TMP_DIR/$HOST.conf <<EOF |
| 177 | proto $VPN_PROTO |
| 178 | port $VPN_PORT |
| 179 | dev $VPN_DEV |
| 180 | cert $NAME.crt |
| 181 | key $NAME.key # This file should be kept secret |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 182 | ca ca.crt |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 183 | client |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 184 | remote $VPN_SERVER $VPN_PORT |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 185 | resolv-retry infinite |
| 186 | nobind |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 187 | user nobody |
Dean Troyer | 78f2140 | 2011-11-14 17:45:37 -0600 | [diff] [blame] | 188 | group nogroup |
Jesse Andrews | 2969c70 | 2011-09-24 12:31:57 -0700 | [diff] [blame] | 189 | persist-key |
| 190 | persist-tun |
| 191 | comp-lzo |
| 192 | verb 3 |
| 193 | EOF |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 194 | (cd $TMP_DIR; tar cf $WEB_DIR/$NAME.tar *) |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 195 | rm -rf $TMP_DIR |
Dean Troyer | f44e98d | 2011-11-29 17:39:51 -0600 | [diff] [blame] | 196 | echo "Client certificate and configuration is in $WEB_DIR/$NAME.tar" |
Dean Troyer | 135fb64 | 2011-09-27 12:57:53 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | # Process command line args |
| 200 | case $1 in |
| 201 | --client) if [ -z $2 ]; then |
| 202 | usage |
| 203 | fi |
| 204 | do_client $2 |
| 205 | ;; |
| 206 | --server) if [ -z $2 ]; then |
| 207 | NAME=`hostname` |
| 208 | else |
| 209 | NAME=$2 |
| 210 | # Save for --client use |
| 211 | echo $NAME >$VPN_DIR/hostname |
| 212 | fi |
| 213 | do_server $NAME |
| 214 | ;; |
| 215 | --clean) $CA_DIR/clean-all |
| 216 | ;; |
| 217 | *) usage |
| 218 | esac |