From 1b488c8053cff1f09d025a20dc765a2079417eff Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Fri, 17 Apr 2020 19:15:17 -0400 Subject: [PATCH] update for multiple servers on one host --- client-cert-helper | 27 +++++++---- vpn-mk-client-cert | 30 +++++++----- vpn-server-setup | 117 +++++++++++++++++++++++++++++---------------- 3 files changed, 111 insertions(+), 63 deletions(-) diff --git a/client-cert-helper b/client-cert-helper index f5b35ac..a170813 100755 --- a/client-cert-helper +++ b/client-cert-helper @@ -16,14 +16,23 @@ if [[ -e /etc/openvpn/server ]]; then server_dir=/etc/openvpn/server fi -cafile=$server_dir/ca.crt +cafile=$server_dir/ca-$name.crt +### begin section roughly copied from vpn-server-setup +rsadir=/etc/openvpn/easy-rsa-$name new=true -keyfiles=(/etc/openvpn/easy-rsa/pki/{issued/$common_name.crt,private/$common_name.key}) -if [[ -e /etc/openvpn/easy-rsa/build-ca ]]; then +keyfiles=( + $rsadir/pki/private/$common_name.key + $rsadir/pki/issued/$common_name.crt +) +if [[ -e /etc/openvpn/easy-rsa-$name/build-ca ]]; then new=false - keyfiles=(/etc/openvpn/easy-rsa/keys/$name.{crt,key}) + keyfiles=( + $rsadir/keys/$common_name.key + $rsadir/keys/$common_name.crt + ) fi +### end section roughly copied from vpn-server-setup if [[ ! -e $cafile ]]; then echo: error no cafile found at $cafile >/tmp/errors @@ -40,7 +49,7 @@ done if ! $exists; then - cd /etc/openvpn/easy-rsa + cd /etc/openvpn/easy-rsa-$name if $new; then ./easyrsa build-client-full $common_name nopass >/dev/null else @@ -51,10 +60,10 @@ if ! $exists; then fi d=$(mktemp -d) -cp $cafile $d/$name-ca.crt -cp ${keyfiles[@]} $d - -cp $server_dir/ta.key $d/$name-ta.key +cp $server_dir/ta-$name.key $cafile $d +for f in ${keyfiles[@]}; do + cp $f $d/$name.${f##*.} +done tar cz -C $d . rm -rf $d diff --git a/vpn-mk-client-cert b/vpn-mk-client-cert index f4e5762..1aa9b41 100755 --- a/vpn-mk-client-cert +++ b/vpn-mk-client-cert @@ -101,6 +101,8 @@ if ! ssh root@$host bash -s -- $name $common_name < client-cert-helper \ exit 1 fi +port=$(echo '/^port/ {print $2}' | ssh root@$host awk -f - /etc/openvpn/server/$name.conf | tail -n1) + f=/etc/openvpn/client/$name.crt if ! $shell "test -s $f"; then @@ -115,24 +117,18 @@ $shell "dd of=/etc/openvpn/client/$name.conf" <&2' ERR usage() { cat <<'EOF' -usage: ${0##*/} [-d|-h|--help] [IPV6_ADDR/BITS IPV6_DEFAULT_ROUTE] +usage: ${0##*/} [OPTIONS] [IPV6_ADDR/BITS IPV6_DEFAULT_ROUTE] --r Do not push default route +-4 I prefix of range for ipv4, default 10.8.0 -d Do not push dns +-n Name. default = server. 2 servers on the same host need different names. +-p Port. default 1194 +-r Do not push default route -s Do not start openvpn -h --help print help @@ -49,11 +52,16 @@ EOF dns=true route=true start=true -temp=$(getopt -l help drsh "$@") || usage 1 +ip4=10.8.0 +name=server +temp=$(getopt -l help 4:dn:p:rsh "$@") || usage 1 eval set -- "$temp" while true; do case $1 in + -4) ip4=$2; shift 2 ;; -d) dns=false; shift ;; + -n) name=$2; shift 2 ;; + -p) port=$2; shift 2 ;; -r) route=false; shift ;; -s) start=false; shift ;; -h|--help) usage ;; @@ -64,26 +72,18 @@ done read -r ip6 ip6route <<<"$@" +source /a/bin/distro-functions/src/package-manager-abstractions -apt-get update -# suggests get's us openssl. policy-rc.d is to prevent install from starting services -f=/usr/sbin/policy-rc.d; -dd of=$f <vars ./easyrsa init-pki ./easyrsa --batch build-ca nopass - ./easyrsa build-server-full server nopass - openssl dhparam -out $server_dir/dh2048.pem 2048 + ./easyrsa build-server-full $name nopass else # dun care about settning cert cn etc from the non-example values source vars @@ -131,23 +150,23 @@ if ! $keys_exist; then # 'server' is the default name in our conf file for the name of the file # and I've seen no reason to change it. # Note, this is not idempotent. - { echo -e '\n\n\n\n\n\n\n\n\n\n'; sleep 1; echo -e 'y\ny\n'; } | ./build-key-server server + { echo -e '\n\n\n\n\n\n\n\n\n\n'; sleep 1; echo -e 'y\ny\n'; } | ./build-key-server $name ./build-dh fi fi -cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz $server_dir -gzip -df $server_dir/server.conf.gz - +gzip -dc /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz >$conf +cafile=$server_dir/ca-$name.crt +cp $ca_origin $cafile cp ${keyfiles[@]} $server_dir # for legacy systems -for f in ${keyfiles[@]}; do +for f in ${keyfiles[@]} $cafile; do ln -sf server/${f##*/} /etc/openvpn done -cat >>$server_dir/server.conf <<'EOF' +cat >>$conf <>$server_dir/server.conf <<'EOF' -push "dhcp-option DNS 10.8.0.1" + cat >>$conf <>$server_dir/server.conf <>$conf <>$server_dir/server.conf <<'EOF' + cat >>$conf <<'EOF' # Be the default gateway for clients. push "redirect-gateway def1" EOF if [[ $ip6 ]]; then - cat >>$server_dir/server.conf <<'EOF' + cat >>$conf <<'EOF' push "route-ipv6 2000::/3" EOF fi fi +if [[ $port ]]; then + cat >>$conf <>/etc/sysctl.conf <<'EOF' @@ -225,7 +258,7 @@ net.ipv4.ip_forward=1 EOF sysctl -p /etc/sysctl.conf -gw=$(ip route | sed -rn 's/^default via .* dev (\S+).*/\1/p') +gw=$(ip route | sed -rn 's/^default via .* dev (\S+).*/\1/p' | head -n1) cat >/etc/systemd/system/vpnnat.service <