blob: 9a4f0369d547cf1fedaee9d0dc7e99d88674bbd1 [file] [log] [blame]
Dean Troyer135fb642011-09-27 12:57:53 -05001#!/bin/bash
Dean Troyere62ba4d2012-06-27 22:07:34 -05002
3# **install_openvpn.sh**
4
5# Install OpenVPN and generate required certificates
Dean Troyer135fb642011-09-27 12:57:53 -05006#
7# install_openvpn.sh --client name
8# install_openvpn.sh --server [name]
9#
10# name is used on the CN of the generated cert, and the filename of
11# the configuration, certificate and key files.
12#
13# --server mode configures the host with a running OpenVPN server instance
14# --client mode creates a tarball of a client configuration for this server
Jesse Andrews2969c702011-09-24 12:31:57 -070015
Dean Troyer78f21402011-11-14 17:45:37 -060016# Get config file
Dean Troyerf44e98d2011-11-29 17:39:51 -060017if [ -e localrc ]; then
18 . localrc
Dean Troyer78f21402011-11-14 17:45:37 -060019fi
Dean Troyerf44e98d2011-11-29 17:39:51 -060020if [ -e vpnrc ]; then
21 . vpnrc
22fi
23
24# Do some IP manipulation
Ian Wienandaee18c72014-02-21 15:35:08 +110025function cidr2netmask {
Dean Troyerf44e98d2011-11-29 17:39:51 -060026 set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
27 if [[ $1 -gt 1 ]]; then
28 shift $1
29 else
30 shift
31 fi
32 echo ${1-0}.${2-0}.${3-0}.${4-0}
33}
34
35FIXED_NET=`echo $FIXED_RANGE | cut -d'/' -f1`
36FIXED_CIDR=`echo $FIXED_RANGE | cut -d'/' -f2`
37FIXED_MASK=`cidr2netmask $FIXED_CIDR`
Dean Troyer78f21402011-11-14 17:45:37 -060038
Dean Troyer135fb642011-09-27 12:57:53 -050039# VPN Config
40VPN_SERVER=${VPN_SERVER:-`ifconfig eth0 | awk "/inet addr:/ { print \$2 }" | cut -d: -f2`} # 50.56.12.212
41VPN_PROTO=${VPN_PROTO:-tcp}
42VPN_PORT=${VPN_PORT:-6081}
Dean Troyerf44e98d2011-11-29 17:39:51 -060043VPN_DEV=${VPN_DEV:-tap0}
44VPN_BRIDGE=${VPN_BRIDGE:-br100}
45VPN_BRIDGE_IF=${VPN_BRIDGE_IF:-$FLAT_INTERFACE}
46VPN_CLIENT_NET=${VPN_CLIENT_NET:-$FIXED_NET}
47VPN_CLIENT_MASK=${VPN_CLIENT_MASK:-$FIXED_MASK}
48VPN_CLIENT_DHCP="${VPN_CLIENT_DHCP:-net.1 net.254}"
Dean Troyer135fb642011-09-27 12:57:53 -050049
50VPN_DIR=/etc/openvpn
Dean Troyerf44e98d2011-11-29 17:39:51 -060051CA_DIR=$VPN_DIR/easy-rsa
Dean Troyer135fb642011-09-27 12:57:53 -050052
Ian Wienandaee18c72014-02-21 15:35:08 +110053function usage {
Dean Troyer135fb642011-09-27 12:57:53 -050054 echo "$0 - OpenVPN install and certificate generation"
55 echo ""
56 echo "$0 --client name"
57 echo "$0 --server [name]"
58 echo ""
59 echo " --server mode configures the host with a running OpenVPN server instance"
60 echo " --client mode creates a tarball of a client configuration for this server"
61 exit 1
62}
63
64if [ -z $1 ]; then
65 usage
66fi
67
68# Install OpenVPN
Dean Troyer78f21402011-11-14 17:45:37 -060069VPN_EXEC=`which openvpn`
70if [ -z "$VPN_EXEC" -o ! -x "$VPN_EXEC" ]; then
Dean Troyer135fb642011-09-27 12:57:53 -050071 apt-get install -y openvpn bridge-utils
72fi
73if [ ! -d $CA_DIR ]; then
74 cp -pR /usr/share/doc/openvpn/examples/easy-rsa/2.0/ $CA_DIR
75fi
76
Dean Troyerf44e98d2011-11-29 17:39:51 -060077# Keep track of the current directory
78TOOLS_DIR=$(cd $(dirname "$0") && pwd)
79TOP_DIR=$(cd $TOOLS_DIR/.. && pwd)
80
81WEB_DIR=$TOP_DIR/../vpn
82if [[ ! -d $WEB_DIR ]]; then
83 mkdir -p $WEB_DIR
84fi
85WEB_DIR=$(cd $TOP_DIR/../vpn && pwd)
86
Dean Troyer135fb642011-09-27 12:57:53 -050087cd $CA_DIR
88source ./vars
89
90# Override the defaults
91export KEY_COUNTRY="US"
92export KEY_PROVINCE="TX"
93export KEY_CITY="SanAntonio"
94export KEY_ORG="Cloudbuilders"
95export KEY_EMAIL="rcb@lists.rackspace.com"
96
97if [ ! -r $CA_DIR/keys/dh1024.pem ]; then
98 # Initialize a new CA
99 $CA_DIR/clean-all
100 $CA_DIR/build-dh
101 $CA_DIR/pkitool --initca
102 openvpn --genkey --secret $CA_DIR/keys/ta.key ## Build a TLS key
103fi
104
Ian Wienandaee18c72014-02-21 15:35:08 +1100105function do_server {
Dean Troyer135fb642011-09-27 12:57:53 -0500106 NAME=$1
107 # Generate server certificate
108 $CA_DIR/pkitool --server $NAME
109
110 (cd $CA_DIR/keys;
111 cp $NAME.crt $NAME.key ca.crt dh1024.pem ta.key $VPN_DIR
112 )
Dean Troyer78f21402011-11-14 17:45:37 -0600113 cat >$VPN_DIR/br-up <<EOF
114#!/bin/bash
115
116BR="$VPN_BRIDGE"
117TAP="\$1"
118
Dean Troyerf44e98d2011-11-29 17:39:51 -0600119if [[ ! -d /sys/class/net/\$BR ]]; then
120 brctl addbr \$BR
121fi
122
Dean Troyer78f21402011-11-14 17:45:37 -0600123for t in \$TAP; do
124 openvpn --mktun --dev \$t
125 brctl addif \$BR \$t
126 ifconfig \$t 0.0.0.0 promisc up
127done
128EOF
129 chmod +x $VPN_DIR/br-up
130 cat >$VPN_DIR/br-down <<EOF
131#!/bin/bash
132
133BR="$VPN_BRIDGE"
134TAP="\$1"
135
136for i in \$TAP; do
137 brctl delif \$BR $t
138 openvpn --rmtun --dev \$i
139done
140EOF
141 chmod +x $VPN_DIR/br-down
Dean Troyer135fb642011-09-27 12:57:53 -0500142 cat >$VPN_DIR/$NAME.conf <<EOF
143proto $VPN_PROTO
144port $VPN_PORT
145dev $VPN_DEV
Dean Troyer78f21402011-11-14 17:45:37 -0600146up $VPN_DIR/br-up
147down $VPN_DIR/br-down
Dean Troyer135fb642011-09-27 12:57:53 -0500148cert $NAME.crt
149key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -0700150ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -0700151dh dh1024.pem
Dean Troyer135fb642011-09-27 12:57:53 -0500152duplicate-cn
Dean Troyer78f21402011-11-14 17:45:37 -0600153server-bridge $VPN_CLIENT_NET $VPN_CLIENT_MASK $VPN_CLIENT_DHCP
Jesse Andrews2969c702011-09-24 12:31:57 -0700154ifconfig-pool-persist ipp.txt
Jesse Andrews2969c702011-09-24 12:31:57 -0700155comp-lzo
Dean Troyer135fb642011-09-27 12:57:53 -0500156user nobody
Dean Troyer78f21402011-11-14 17:45:37 -0600157group nogroup
Jesse Andrews2969c702011-09-24 12:31:57 -0700158persist-key
159persist-tun
160status openvpn-status.log
161EOF
Dean Troyer135fb642011-09-27 12:57:53 -0500162 /etc/init.d/openvpn restart
163}
Jesse Andrews2969c702011-09-24 12:31:57 -0700164
Ian Wienandaee18c72014-02-21 15:35:08 +1100165function do_client {
Dean Troyer135fb642011-09-27 12:57:53 -0500166 NAME=$1
167 # Generate a client certificate
168 $CA_DIR/pkitool $NAME
Jesse Andrews2969c702011-09-24 12:31:57 -0700169
Dean Troyer135fb642011-09-27 12:57:53 -0500170 TMP_DIR=`mktemp -d`
171 (cd $CA_DIR/keys;
172 cp -p ca.crt ta.key $NAME.key $NAME.crt $TMP_DIR
173 )
174 if [ -r $VPN_DIR/hostname ]; then
175 HOST=`cat $VPN_DIR/hostname`
176 else
177 HOST=`hostname`
178 fi
179 cat >$TMP_DIR/$HOST.conf <<EOF
180proto $VPN_PROTO
181port $VPN_PORT
182dev $VPN_DEV
183cert $NAME.crt
184key $NAME.key # This file should be kept secret
Jesse Andrews2969c702011-09-24 12:31:57 -0700185ca ca.crt
Jesse Andrews2969c702011-09-24 12:31:57 -0700186client
Dean Troyer135fb642011-09-27 12:57:53 -0500187remote $VPN_SERVER $VPN_PORT
Jesse Andrews2969c702011-09-24 12:31:57 -0700188resolv-retry infinite
189nobind
Dean Troyer135fb642011-09-27 12:57:53 -0500190user nobody
Dean Troyer78f21402011-11-14 17:45:37 -0600191group nogroup
Jesse Andrews2969c702011-09-24 12:31:57 -0700192persist-key
193persist-tun
194comp-lzo
195verb 3
196EOF
Dean Troyerf44e98d2011-11-29 17:39:51 -0600197 (cd $TMP_DIR; tar cf $WEB_DIR/$NAME.tar *)
Dean Troyer135fb642011-09-27 12:57:53 -0500198 rm -rf $TMP_DIR
Dean Troyerf44e98d2011-11-29 17:39:51 -0600199 echo "Client certificate and configuration is in $WEB_DIR/$NAME.tar"
Dean Troyer135fb642011-09-27 12:57:53 -0500200}
201
202# Process command line args
203case $1 in
204 --client) if [ -z $2 ]; then
205 usage
206 fi
207 do_client $2
208 ;;
209 --server) if [ -z $2 ]; then
210 NAME=`hostname`
211 else
212 NAME=$2
213 # Save for --client use
214 echo $NAME >$VPN_DIR/hostname
215 fi
216 do_server $NAME
217 ;;
218 --clean) $CA_DIR/clean-all
219 ;;
220 *) usage
221esac