blob: a3a2346fb69766a98ea0e79142df3200c01b75ef [file] [log] [blame]
Dean Troyer135fb642011-09-27 12:57:53 -05001#!/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 Andrews2969c702011-09-24 12:31:57 -070012
Dean Troyer135fb642011-09-27 12:57:53 -050013# VPN Config
14VPN_SERVER=${VPN_SERVER:-`ifconfig eth0 | awk "/inet addr:/ { print \$2 }" | cut -d: -f2`} # 50.56.12.212
15VPN_PROTO=${VPN_PROTO:-tcp}
16VPN_PORT=${VPN_PORT:-6081}
17VPN_DEV=${VPN_DEV:-tun}
18VPN_CLIENT_NET=${VPN_CLIENT_NET:-172.16.28.0}
19VPN_CLIENT_MASK=${VPN_CLIENT_MASK:-255.255.255.0}
20VPN_LOCAL_NET=${VPN_LOCAL_NET:-10.0.0.0}
21VPN_LOCAL_MASK=${VPN_LOCAL_MASK:-255.255.0.0}
22
23VPN_DIR=/etc/openvpn
24CA_DIR=/etc/openvpn/easy-rsa
25
26usage() {
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
37if [ -z $1 ]; then
38 usage
39fi
40
41# Install OpenVPN
42if [ ! -x `which openvpn` ]; then
43 apt-get install -y openvpn bridge-utils
44fi
45if [ ! -d $CA_DIR ]; then
46 cp -pR /usr/share/doc/openvpn/examples/easy-rsa/2.0/ $CA_DIR
47fi
48
49OPWD=`pwd`
50cd $CA_DIR
51source ./vars
52
53# Override the defaults
54export KEY_COUNTRY="US"
55export KEY_PROVINCE="TX"
56export KEY_CITY="SanAntonio"
57export KEY_ORG="Cloudbuilders"
58export KEY_EMAIL="rcb@lists.rackspace.com"
59
60if [ ! -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
66fi
67
68do_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
77proto $VPN_PROTO
78port $VPN_PORT
79dev $VPN_DEV
80cert $NAME.crt
81key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -070082ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -070083dh dh1024.pem
Dean Troyer135fb642011-09-27 12:57:53 -050084duplicate-cn
85server $VPN_CLIENT_NET $VPN_CLIENT_MASK
Jesse Andrews2969c702011-09-24 12:31:57 -070086ifconfig-pool-persist ipp.txt
Dean Troyer135fb642011-09-27 12:57:53 -050087push "route $VPN_LOCAL_NET $VPN_LOCAL_MASK"
Jesse Andrews2969c702011-09-24 12:31:57 -070088comp-lzo
Dean Troyer135fb642011-09-27 12:57:53 -050089user nobody
90group nobody
Jesse Andrews2969c702011-09-24 12:31:57 -070091persist-key
92persist-tun
93status openvpn-status.log
94EOF
Dean Troyer135fb642011-09-27 12:57:53 -050095 /etc/init.d/openvpn restart
96}
Jesse Andrews2969c702011-09-24 12:31:57 -070097
Dean Troyer135fb642011-09-27 12:57:53 -050098do_client() {
99 NAME=$1
100 # Generate a client certificate
101 $CA_DIR/pkitool $NAME
Jesse Andrews2969c702011-09-24 12:31:57 -0700102
Dean Troyer135fb642011-09-27 12:57:53 -0500103 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
113proto $VPN_PROTO
114port $VPN_PORT
115dev $VPN_DEV
116cert $NAME.crt
117key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -0700118ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -0700119client
Dean Troyer135fb642011-09-27 12:57:53 -0500120remote $VPN_SERVER $VPN_PORT
Jesse Andrews2969c702011-09-24 12:31:57 -0700121resolv-retry infinite
122nobind
Dean Troyer135fb642011-09-27 12:57:53 -0500123user nobody
124group nobody
Jesse Andrews2969c702011-09-24 12:31:57 -0700125persist-key
126persist-tun
127comp-lzo
128verb 3
129EOF
Dean Troyer135fb642011-09-27 12:57:53 -0500130 (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
136case $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
154esac