Browse code

Major refactor of vpn install

Dean Troyer authored on 2011/09/28 02:57:53
Showing 1 changed files
... ...
@@ -1,60 +1,154 @@
1
-# rough history from wilk - need to cleanup
2
-apt-get install -y openvpn bridge-utils
3
-cp -R /usr/share/doc/openvpn/examples/easy-rsa/2.0/ /etc/openvpn/easy-rsa/
4
-cd /etc/openvpn/easy-rsa
5
-source vars
6
-./clean-all
7
-./build-dh
8
-./pkitool --initca
9
-./pkitool --server server
10
-./pkitool client1
11
-cd keys
12
-openvpn --genkey --secret ta.key  ## Build a TLS key
13
-cp server.crt server.key ca.crt dh1024.pem ta.key ../../
14
-cd ../../
15
-
16
-cat >/etc/openvpn/server.conf <<EOF
17
-duplicate-cn
18
-port 6081
19
-proto tcp
20
-dev tun
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
12
+
13
+# VPN Config
14
+VPN_SERVER=${VPN_SERVER:-`ifconfig eth0 | awk "/inet addr:/ { print \$2 }" | cut -d: -f2`}  # 50.56.12.212
15
+VPN_PROTO=${VPN_PROTO:-tcp}
16
+VPN_PORT=${VPN_PORT:-6081}
17
+VPN_DEV=${VPN_DEV:-tun}
18
+VPN_CLIENT_NET=${VPN_CLIENT_NET:-172.16.28.0}
19
+VPN_CLIENT_MASK=${VPN_CLIENT_MASK:-255.255.255.0}
20
+VPN_LOCAL_NET=${VPN_LOCAL_NET:-10.0.0.0}
21
+VPN_LOCAL_MASK=${VPN_LOCAL_MASK:-255.255.0.0}
22
+
23
+VPN_DIR=/etc/openvpn
24
+CA_DIR=/etc/openvpn/easy-rsa
25
+
26
+usage() {
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
+
37
+if [ -z $1 ]; then
38
+    usage
39
+fi
40
+
41
+# Install OpenVPN
42
+if [ ! -x `which openvpn` ]; then
43
+    apt-get install -y openvpn bridge-utils
44
+fi
45
+if [ ! -d $CA_DIR ]; then
46
+    cp -pR /usr/share/doc/openvpn/examples/easy-rsa/2.0/ $CA_DIR
47
+fi
48
+
49
+OPWD=`pwd`
50
+cd $CA_DIR
51
+source ./vars
52
+
53
+# Override the defaults
54
+export KEY_COUNTRY="US"
55
+export KEY_PROVINCE="TX"
56
+export KEY_CITY="SanAntonio"
57
+export KEY_ORG="Cloudbuilders"
58
+export KEY_EMAIL="rcb@lists.rackspace.com"
59
+
60
+if [ ! -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
66
+fi
67
+
68
+do_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
77
+proto $VPN_PROTO
78
+port $VPN_PORT
79
+dev $VPN_DEV
80
+cert $NAME.crt
81
+key $NAME.key  # This file should be kept secret
21 82
 ca ca.crt
22
-cert server.crt
23
-key server.key  # This file should be kept secret
24 83
 dh dh1024.pem
25
-server 172.16.28.0 255.255.255.0
84
+duplicate-cn
85
+server $VPN_CLIENT_NET $VPN_CLIENT_MASK
26 86
 ifconfig-pool-persist ipp.txt
27
-push "route 10.0.0.0 255.255.255.224"
87
+push "route $VPN_LOCAL_NET $VPN_LOCAL_MASK"
28 88
 comp-lzo
89
+user nobody
90
+group nobody
29 91
 persist-key
30 92
 persist-tun
31 93
 status openvpn-status.log
32 94
 EOF
33
-/etc/init.d/openvpn restart
34
-
35
-echo Use the following ca for your client:
36
-cat /etc/openvpn/ca.crt
37
-
38
-echo
39
-echo Use the following cert for your client
40
-cat /etc/openvpn/easy-rsa/keys/client1.crt 
41
-echo
42
-echo Use the following key for your client
43
-cat /etc/openvpn/easy-rsa/keys/client1.key 
44
-echo
45
-echo Use the following client config:
46
-cat <<EOF
95
+    /etc/init.d/openvpn restart
96
+}
97
+
98
+do_client() {
99
+    NAME=$1
100
+    # Generate a client certificate
101
+    $CA_DIR/pkitool $NAME
102
+
103
+    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
113
+proto $VPN_PROTO
114
+port $VPN_PORT
115
+dev $VPN_DEV
116
+cert $NAME.crt
117
+key $NAME.key  # This file should be kept secret
47 118
 ca ca.crt
48
-cert client.crt
49
-key client.key
50 119
 client
51
-dev tun
52
-proto tcp
53
-remote 50.56.12.212 6081
120
+remote $VPN_SERVER $VPN_PORT
54 121
 resolv-retry infinite
55 122
 nobind
123
+user nobody
124
+group nobody
56 125
 persist-key
57 126
 persist-tun
58 127
 comp-lzo
59 128
 verb 3
60 129
 EOF
130
+    (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
136
+case $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
154
+esac