openvpn.sh: Implement init step and cert gen

* Initialize and configure the OpenVPN server
* Generate PKI keys, CA, and certs when needed
This commit is contained in:
Kyle Manna 2014-06-04 01:39:38 -07:00
parent 9e4de074d0
commit a1c174f6f5
2 changed files with 50 additions and 7 deletions

View File

@ -4,7 +4,7 @@ FROM ubuntu:14.04
MAINTAINER Kyle Manna <kyle@kylemanna.com> MAINTAINER Kyle Manna <kyle@kylemanna.com>
RUN apt-get install -y openvpn iptables curl git-core RUN apt-get install -y openvpn iptables git-core dnsutils
# Update checkout to use tags when v3.0 is finally released # Update checkout to use tags when v3.0 is finally released
RUN git clone https://github.com/OpenVPN/easy-rsa.git /usr/local/share/easy-rsa RUN git clone https://github.com/OpenVPN/easy-rsa.git /usr/local/share/easy-rsa

View File

@ -22,14 +22,58 @@ if [ $# -lt 1 ]; then
fi fi
do_openvpn() { do_openvpn() {
if [ ! -d /dev/net ]; then
mkdir -p /dev/net mkdir -p /dev/net
fi
if [ ! -c /dev/net/tun ]; then if [ ! -c /dev/net/tun ]; then
mknod /dev/net/tun c 10 200 mknod /dev/net/tun c 10 200
fi fi
cd /etc/easyrsa iptables -t nat -A POSTROUTING -s 192.168.255.0/24 -o eth0 -j MASQUERADE
openvpn --config "$OPENVPN/udp1194.conf"
}
do_init() {
cn=$1
# Provides a sufficient warning before erasing pre-existing files
easyrsa init-pki
# For a CA key with a password, manually init; this is autopilot
easyrsa build-ca nopass
easyrsa gen-dh
openvpn --genkey --secret $OPENVPN/pki/ta.key
if [ -z "$cn"]; then
#TODO: Handle IPv6 (when I get a VPS with IPv6)...
ip4=$(dig +short myip.opendns.com @resolver1.opendns.com)
ptr=$(dig +short -x $ip4 | sed -e 's:\.$::')
[ -n "$ptr" ] && cn=$ptr || cn=$ip4
fi
easyrsa build-server-full $cn nopass
[ -f "$OPENVPN/udp1194.conf" ] || cat > "$OPENVPN/udp1194.conf" <<EOF
server 192.168.255.128 255.255.255.128
verb 3
#duplicate-cn
key $EASYRSA_PKI/private/$cn.key
ca $EASYRSA_PKI/ca.crt
cert $EASYRSA_PKI/issued/$cn.crt
dh $EASYRSA_PKI/dh.pem
#tls-auth $EASYRSA_PKI/ta.key 0
keepalive 10 60
persist-key
persist-tun
push "dhcp-option DNS 8.8.4.4"
push "dhcp-option DNS 8.8.8.8"
proto udp
port 1194
dev tun1194
status /tmp/openvpn-status-1194.log
EOF
} }
# Read arguments from command line # Read arguments from command line
@ -39,10 +83,9 @@ shift
case "$cmd" in case "$cmd" in
# nop for volume creation # nop for volume creation
init) init)
exit 0 do_init "$@"
;; ;;
easyrsa) easyrsa)
cd $OPENVPN
easyrsa "$@" easyrsa "$@"
;; ;;
easyrsa-export-vars) easyrsa-export-vars)