blob: ff88a065a916c87f926517018e6bdf5e726ca36e [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
14if [ -e localrc.vpn ]; then
15 . localrc.vpn
16fi
17
Dean Troyer135fb642011-09-27 12:57:53 -050018# VPN Config
19VPN_SERVER=${VPN_SERVER:-`ifconfig eth0 | awk "/inet addr:/ { print \$2 }" | cut -d: -f2`} # 50.56.12.212
20VPN_PROTO=${VPN_PROTO:-tcp}
21VPN_PORT=${VPN_PORT:-6081}
22VPN_DEV=${VPN_DEV:-tun}
Dean Troyer78f21402011-11-14 17:45:37 -060023VPN_BRIDGE=${VPN_BRIDGE:-br0}
Dean Troyer135fb642011-09-27 12:57:53 -050024VPN_CLIENT_NET=${VPN_CLIENT_NET:-172.16.28.0}
25VPN_CLIENT_MASK=${VPN_CLIENT_MASK:-255.255.255.0}
Dean Troyer78f21402011-11-14 17:45:37 -060026VPN_CLIENT_DHCP="${VPN_CLIENT_DHCP:-172.16.28.1 172.16.28.254}"
Dean Troyer135fb642011-09-27 12:57:53 -050027VPN_LOCAL_NET=${VPN_LOCAL_NET:-10.0.0.0}
28VPN_LOCAL_MASK=${VPN_LOCAL_MASK:-255.255.0.0}
29
30VPN_DIR=/etc/openvpn
31CA_DIR=/etc/openvpn/easy-rsa
32
33usage() {
34 echo "$0 - OpenVPN install and certificate generation"
35 echo ""
36 echo "$0 --client name"
37 echo "$0 --server [name]"
38 echo ""
39 echo " --server mode configures the host with a running OpenVPN server instance"
40 echo " --client mode creates a tarball of a client configuration for this server"
41 exit 1
42}
43
44if [ -z $1 ]; then
45 usage
46fi
47
48# Install OpenVPN
Dean Troyer78f21402011-11-14 17:45:37 -060049VPN_EXEC=`which openvpn`
50if [ -z "$VPN_EXEC" -o ! -x "$VPN_EXEC" ]; then
Dean Troyer135fb642011-09-27 12:57:53 -050051 apt-get install -y openvpn bridge-utils
52fi
53if [ ! -d $CA_DIR ]; then
54 cp -pR /usr/share/doc/openvpn/examples/easy-rsa/2.0/ $CA_DIR
55fi
56
57OPWD=`pwd`
58cd $CA_DIR
59source ./vars
60
61# Override the defaults
62export KEY_COUNTRY="US"
63export KEY_PROVINCE="TX"
64export KEY_CITY="SanAntonio"
65export KEY_ORG="Cloudbuilders"
66export KEY_EMAIL="rcb@lists.rackspace.com"
67
68if [ ! -r $CA_DIR/keys/dh1024.pem ]; then
69 # Initialize a new CA
70 $CA_DIR/clean-all
71 $CA_DIR/build-dh
72 $CA_DIR/pkitool --initca
73 openvpn --genkey --secret $CA_DIR/keys/ta.key ## Build a TLS key
74fi
75
76do_server() {
77 NAME=$1
78 # Generate server certificate
79 $CA_DIR/pkitool --server $NAME
80
81 (cd $CA_DIR/keys;
82 cp $NAME.crt $NAME.key ca.crt dh1024.pem ta.key $VPN_DIR
83 )
Dean Troyer78f21402011-11-14 17:45:37 -060084 cat >$VPN_DIR/br-up <<EOF
85#!/bin/bash
86
87BR="$VPN_BRIDGE"
88TAP="\$1"
89
90for t in \$TAP; do
91 openvpn --mktun --dev \$t
92 brctl addif \$BR \$t
93 ifconfig \$t 0.0.0.0 promisc up
94done
95EOF
96 chmod +x $VPN_DIR/br-up
97 cat >$VPN_DIR/br-down <<EOF
98#!/bin/bash
99
100BR="$VPN_BRIDGE"
101TAP="\$1"
102
103for i in \$TAP; do
104 brctl delif \$BR $t
105 openvpn --rmtun --dev \$i
106done
107EOF
108 chmod +x $VPN_DIR/br-down
Dean Troyer135fb642011-09-27 12:57:53 -0500109 cat >$VPN_DIR/$NAME.conf <<EOF
110proto $VPN_PROTO
111port $VPN_PORT
112dev $VPN_DEV
Dean Troyer78f21402011-11-14 17:45:37 -0600113up $VPN_DIR/br-up
114down $VPN_DIR/br-down
Dean Troyer135fb642011-09-27 12:57:53 -0500115cert $NAME.crt
116key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -0700117ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -0700118dh dh1024.pem
Dean Troyer135fb642011-09-27 12:57:53 -0500119duplicate-cn
Dean Troyer78f21402011-11-14 17:45:37 -0600120#server $VPN_CLIENT_NET $VPN_CLIENT_MASK
121server-bridge $VPN_CLIENT_NET $VPN_CLIENT_MASK $VPN_CLIENT_DHCP
Jesse Andrews2969c702011-09-24 12:31:57 -0700122ifconfig-pool-persist ipp.txt
Dean Troyer135fb642011-09-27 12:57:53 -0500123push "route $VPN_LOCAL_NET $VPN_LOCAL_MASK"
Jesse Andrews2969c702011-09-24 12:31:57 -0700124comp-lzo
Dean Troyer135fb642011-09-27 12:57:53 -0500125user nobody
Dean Troyer78f21402011-11-14 17:45:37 -0600126group nogroup
Jesse Andrews2969c702011-09-24 12:31:57 -0700127persist-key
128persist-tun
129status openvpn-status.log
130EOF
Dean Troyer135fb642011-09-27 12:57:53 -0500131 /etc/init.d/openvpn restart
132}
Jesse Andrews2969c702011-09-24 12:31:57 -0700133
Dean Troyer135fb642011-09-27 12:57:53 -0500134do_client() {
135 NAME=$1
136 # Generate a client certificate
137 $CA_DIR/pkitool $NAME
Jesse Andrews2969c702011-09-24 12:31:57 -0700138
Dean Troyer135fb642011-09-27 12:57:53 -0500139 TMP_DIR=`mktemp -d`
140 (cd $CA_DIR/keys;
141 cp -p ca.crt ta.key $NAME.key $NAME.crt $TMP_DIR
142 )
143 if [ -r $VPN_DIR/hostname ]; then
144 HOST=`cat $VPN_DIR/hostname`
145 else
146 HOST=`hostname`
147 fi
148 cat >$TMP_DIR/$HOST.conf <<EOF
149proto $VPN_PROTO
150port $VPN_PORT
151dev $VPN_DEV
152cert $NAME.crt
153key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -0700154ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -0700155client
Dean Troyer135fb642011-09-27 12:57:53 -0500156remote $VPN_SERVER $VPN_PORT
Jesse Andrews2969c702011-09-24 12:31:57 -0700157resolv-retry infinite
158nobind
Dean Troyer135fb642011-09-27 12:57:53 -0500159user nobody
Dean Troyer78f21402011-11-14 17:45:37 -0600160group nogroup
Jesse Andrews2969c702011-09-24 12:31:57 -0700161persist-key
162persist-tun
163comp-lzo
164verb 3
165EOF
Dean Troyer135fb642011-09-27 12:57:53 -0500166 (cd $TMP_DIR; tar cf $OPWD/$NAME.tar *)
167 rm -rf $TMP_DIR
168 echo "Client certificate and configuration is in $OPWD/$NAME.tar"
169}
170
171# Process command line args
172case $1 in
173 --client) if [ -z $2 ]; then
174 usage
175 fi
176 do_client $2
177 ;;
178 --server) if [ -z $2 ]; then
179 NAME=`hostname`
180 else
181 NAME=$2
182 # Save for --client use
183 echo $NAME >$VPN_DIR/hostname
184 fi
185 do_server $NAME
186 ;;
187 --clean) $CA_DIR/clean-all
188 ;;
189 *) usage
190esac