X-Git-Url: https://iankelling.org/git/?p=vpn-setup;a=blobdiff_plain;f=vpn-server-setup;h=0710dc801072e749df602364226a5fbd216d082b;hp=ded2a785f59250b265f2307a85ccdb3e04cfb98c;hb=1b488c8053cff1f09d025a20dc765a2079417eff;hpb=50a29b33506900a8bc5d87e67ea0d3fd9bd69369 diff --git a/vpn-server-setup b/vpn-server-setup index ded2a78..0710dc8 100755 --- a/vpn-server-setup +++ b/vpn-server-setup @@ -1,4 +1,4 @@ -#!/bin/bash -x +#!/bin/bash # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,10 +21,13 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR usage() { cat <<'EOF' -usage: ${0##*/} [-d|-h|--help] +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 @@ -32,9 +35,15 @@ Sets up a vpn server which pushes gateway route and dns server so all traffic goes through the vpn. requires systemd, and might have some debian specific paths. +For ipv6, we assume ipv6_addr routes to the server. + You can save all the keys by storing /etc/openvpn/easy-rsa/keys, and the script will not generate them if it sees they exist already. +For future updates to this script, this is a good place to +take inspiration. +https://github.com/angristan/openvpn-install/blob/master/openvpn-install.sh + Note: Uses GNU getopt options parsing style EOF exit $1 @@ -43,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 ;; @@ -56,25 +70,20 @@ while true; do esac done -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 @@ -122,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 <>$conf <>/etc/sysctl.conf <<'EOF' +net.ipv6.conf.all.forwarding=1 +EOF + fi + if $route; then - cat >>$server_dir/server.conf <<'EOF' + cat >>$conf <<'EOF' # Be the default gateway for clients. push "redirect-gateway def1" EOF + if [[ $ip6 ]]; then + cat >>$conf <<'EOF' +push "route-ipv6 2000::/3" +EOF + fi fi +if [[ $port ]]; then + cat >>$conf <>/etc/sysctl.conf <<'EOF' net.ipv4.ip_forward=1 -net.ipv6.conf.all.forwarding=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 <