blob: 44eee728e2d58e0a9936fe66aa00737b8f67e285 [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 Troyer78f21402011-11-14 17:45:37 -060013# Get config file
Dean Troyerf44e98d2011-11-29 17:39:51 -060014if [ -e localrc ]; then
15 . localrc
Dean Troyer78f21402011-11-14 17:45:37 -060016fi
Dean Troyerf44e98d2011-11-29 17:39:51 -060017if [ -e vpnrc ]; then
18 . vpnrc
19fi
20
21# Do some IP manipulation
22function 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
32FIXED_NET=`echo $FIXED_RANGE | cut -d'/' -f1`
33FIXED_CIDR=`echo $FIXED_RANGE | cut -d'/' -f2`
34FIXED_MASK=`cidr2netmask $FIXED_CIDR`
Dean Troyer78f21402011-11-14 17:45:37 -060035
Dean Troyer135fb642011-09-27 12:57:53 -050036# VPN Config
37VPN_SERVER=${VPN_SERVER:-`ifconfig eth0 | awk "/inet addr:/ { print \$2 }" | cut -d: -f2`} # 50.56.12.212
38VPN_PROTO=${VPN_PROTO:-tcp}
39VPN_PORT=${VPN_PORT:-6081}
Dean Troyerf44e98d2011-11-29 17:39:51 -060040VPN_DEV=${VPN_DEV:-tap0}
41VPN_BRIDGE=${VPN_BRIDGE:-br100}
42VPN_BRIDGE_IF=${VPN_BRIDGE_IF:-$FLAT_INTERFACE}
43VPN_CLIENT_NET=${VPN_CLIENT_NET:-$FIXED_NET}
44VPN_CLIENT_MASK=${VPN_CLIENT_MASK:-$FIXED_MASK}
45VPN_CLIENT_DHCP="${VPN_CLIENT_DHCP:-net.1 net.254}"
Dean Troyer135fb642011-09-27 12:57:53 -050046
47VPN_DIR=/etc/openvpn
Dean Troyerf44e98d2011-11-29 17:39:51 -060048CA_DIR=$VPN_DIR/easy-rsa
Dean Troyer135fb642011-09-27 12:57:53 -050049
50usage() {
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
61if [ -z $1 ]; then
62 usage
63fi
64
65# Install OpenVPN
Dean Troyer78f21402011-11-14 17:45:37 -060066VPN_EXEC=`which openvpn`
67if [ -z "$VPN_EXEC" -o ! -x "$VPN_EXEC" ]; then
Dean Troyer135fb642011-09-27 12:57:53 -050068 apt-get install -y openvpn bridge-utils
69fi
70if [ ! -d $CA_DIR ]; then
71 cp -pR /usr/share/doc/openvpn/examples/easy-rsa/2.0/ $CA_DIR
72fi
73
Dean Troyerf44e98d2011-11-29 17:39:51 -060074# Keep track of the current directory
75TOOLS_DIR=$(cd $(dirname "$0") && pwd)
76TOP_DIR=$(cd $TOOLS_DIR/.. && pwd)
77
78WEB_DIR=$TOP_DIR/../vpn
79if [[ ! -d $WEB_DIR ]]; then
80 mkdir -p $WEB_DIR
81fi
82WEB_DIR=$(cd $TOP_DIR/../vpn && pwd)
83
Dean Troyer135fb642011-09-27 12:57:53 -050084cd $CA_DIR
85source ./vars
86
87# Override the defaults
88export KEY_COUNTRY="US"
89export KEY_PROVINCE="TX"
90export KEY_CITY="SanAntonio"
91export KEY_ORG="Cloudbuilders"
92export KEY_EMAIL="rcb@lists.rackspace.com"
93
94if [ ! -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
100fi
101
102do_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 Troyer78f21402011-11-14 17:45:37 -0600110 cat >$VPN_DIR/br-up <<EOF
111#!/bin/bash
112
113BR="$VPN_BRIDGE"
114TAP="\$1"
115
Dean Troyerf44e98d2011-11-29 17:39:51 -0600116if [[ ! -d /sys/class/net/\$BR ]]; then
117 brctl addbr \$BR
118fi
119
Dean Troyer78f21402011-11-14 17:45:37 -0600120for t in \$TAP; do
121 openvpn --mktun --dev \$t
122 brctl addif \$BR \$t
123 ifconfig \$t 0.0.0.0 promisc up
124done
125EOF
126 chmod +x $VPN_DIR/br-up
127 cat >$VPN_DIR/br-down <<EOF
128#!/bin/bash
129
130BR="$VPN_BRIDGE"
131TAP="\$1"
132
133for i in \$TAP; do
134 brctl delif \$BR $t
135 openvpn --rmtun --dev \$i
136done
137EOF
138 chmod +x $VPN_DIR/br-down
Dean Troyer135fb642011-09-27 12:57:53 -0500139 cat >$VPN_DIR/$NAME.conf <<EOF
140proto $VPN_PROTO
141port $VPN_PORT
142dev $VPN_DEV
Dean Troyer78f21402011-11-14 17:45:37 -0600143up $VPN_DIR/br-up
144down $VPN_DIR/br-down
Dean Troyer135fb642011-09-27 12:57:53 -0500145cert $NAME.crt
146key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -0700147ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -0700148dh dh1024.pem
Dean Troyer135fb642011-09-27 12:57:53 -0500149duplicate-cn
Dean Troyer78f21402011-11-14 17:45:37 -0600150server-bridge $VPN_CLIENT_NET $VPN_CLIENT_MASK $VPN_CLIENT_DHCP
Jesse Andrews2969c702011-09-24 12:31:57 -0700151ifconfig-pool-persist ipp.txt
Jesse Andrews2969c702011-09-24 12:31:57 -0700152comp-lzo
Dean Troyer135fb642011-09-27 12:57:53 -0500153user nobody
Dean Troyer78f21402011-11-14 17:45:37 -0600154group nogroup
Jesse Andrews2969c702011-09-24 12:31:57 -0700155persist-key
156persist-tun
157status openvpn-status.log
158EOF
Dean Troyer135fb642011-09-27 12:57:53 -0500159 /etc/init.d/openvpn restart
160}
Jesse Andrews2969c702011-09-24 12:31:57 -0700161
Dean Troyer135fb642011-09-27 12:57:53 -0500162do_client() {
163 NAME=$1
164 # Generate a client certificate
165 $CA_DIR/pkitool $NAME
Jesse Andrews2969c702011-09-24 12:31:57 -0700166
Dean Troyer135fb642011-09-27 12:57:53 -0500167 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
177proto $VPN_PROTO
178port $VPN_PORT
179dev $VPN_DEV
180cert $NAME.crt
181key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -0700182ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -0700183client
Dean Troyer135fb642011-09-27 12:57:53 -0500184remote $VPN_SERVER $VPN_PORT
Jesse Andrews2969c702011-09-24 12:31:57 -0700185resolv-retry infinite
186nobind
Dean Troyer135fb642011-09-27 12:57:53 -0500187user nobody
Dean Troyer78f21402011-11-14 17:45:37 -0600188group nogroup
Jesse Andrews2969c702011-09-24 12:31:57 -0700189persist-key
190persist-tun
191comp-lzo
192verb 3
193EOF
Dean Troyerf44e98d2011-11-29 17:39:51 -0600194 (cd $TMP_DIR; tar cf $WEB_DIR/$NAME.tar *)
Dean Troyer135fb642011-09-27 12:57:53 -0500195 rm -rf $TMP_DIR
Dean Troyerf44e98d2011-11-29 17:39:51 -0600196 echo "Client certificate and configuration is in $WEB_DIR/$NAME.tar"
Dean Troyer135fb642011-09-27 12:57:53 -0500197}
198
199# Process command line args
200case $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
218esac