X-Git-Url: https://iankelling.org/git/?p=vpn-setup;a=blobdiff_plain;f=vpn-server-setup;h=a311ba8d0ae09bbc3f543921af3b06a07451fbbd;hp=c901970ce613346cd54bfdd984b0dce4f0043821;hb=e4c2b65e04673dc12575e4c1a182fe86e3dc219a;hpb=09079aab55c72708a3b5963557a144b3911b9f11 diff --git a/vpn-server-setup b/vpn-server-setup index c901970..a311ba8 100755 --- a/vpn-server-setup +++ b/vpn-server-setup @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -x # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,89 +19,207 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" -dns=true -route=true -case $1 in - -r) route=false ;; - -d) dns=false ;; - -h|--help|*) - cat <<'EOF' -usage: ${0##*/} [-d|-h|--help] +usage() { + cat <<'EOF' +usage: ${0##*/} [-d|-h|--help] [IPV6_ADDR/BITS IPV6_DEFAULT_ROUTE] -r Do not push default route -d Do not push dns +-s Do not start openvpn -h --help print help -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. +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 - ;; -esac + exit $1 +} + +dns=true +route=true +start=true +temp=$(getopt -l help drsh "$@") || usage 1 +eval set -- "$temp" +while true; do + case $1 in + -d) dns=false; shift ;; + -r) route=false; shift ;; + -s) start=false; shift ;; + -h|--help) usage ;; + --) shift; break ;; + *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;; + esac +done + +read -r ip6 ip6route <<<"$@" + apt-get update -# suggests get's us openssl & easy rsa +# suggests get's us openssl. policy-rc.d is to prevent install from starting services +f=/usr/sbin/policy-rc.d; +dd of=$f <>$server_dir/server.conf <<'EOF' + +# I cat an extra blank line to start because the example config does +# not have a final newline. .... -cat >>/etc/openvpn/server.conf <<'EOF' # not in example config, but openvpn outputs a warning about insecure # cipher without a setting like this (the default i can understand due # to compatibility issues, but not changing the example config... not -# cool). exact cipher taken from config of vpn provider I trust. This +# cool). # requires the same setting on the client side. -cipher aes-256-cbc +cipher AES-256-CBC # just sets up the ability to have client specific configs client-config-dir /etc/openvpn/client-config + +# duplicate in newer sample configs +tls-auth ta.key 0 # This file is secret + +# depending on sample config, this may not be there, which means i can't +# talk to 10.8.0.1, there might be some other way, but stretch's +# sample config says: +# Should be subnet (addressing via IP) +# unless Windows clients v2.0.9 and lower have to +# be supported (then net30, i.e. a /30 per client) +# Defaults to net30 (not recommended) +topology subnet +EOF + + +# dh improve security, +# remove comp-lzo to increase perf +sed -i --follow-symlinks -f - $server_dir/server.conf <<'EOF' +s/^dh dh1024.pem/dh dh2048.pem/ +/^comp-lzo.*/d EOF + + mkdir -p /etc/openvpn/client-config + +if $dns; then + # Be the dns server for clients + cat >>$server_dir/server.conf <<'EOF' +push "dhcp-option DNS 10.8.0.1" +EOF +fi + +if $ip6; then + cat >>$server_dir/server.conf <>/etc/openvpn/server.conf <<'EOF' + cat >>$server_dir/server.conf <<'EOF' # Be the default gateway for clients. push "redirect-gateway def1" EOF -fi - -if $dns; then - # Be the dns server for clients - cat >>/etc/openvpn/server.conf <<'EOF' -push "dhcp-option DNS 10.8.0.1" + if $ip6; then + cat >>$server_dir/server.conf <<'EOF' +push "route-ipv6 2000::/3" EOF + fi fi -echo "1" > /proc/sys/net/ipv4/ip_forward sed -i --follow-symlinks '/^ *net\.ipv4\.ip_forward=.*/d' /etc/sysctl.conf +sed -i --follow-symlinks '/^ *net.ipv6.conf.all.forwarding=.*/d' /etc/sysctl.conf cat >>/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') @@ -116,10 +234,13 @@ ExecStart=/sbin/iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o $gw -j MASQUERA ExecStop=/sbin/iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o $gw -j MASQUERADE [Install] -WantedBy=openvpn.service +WantedBy=$vpn_service.service EOF systemctl daemon-reload # needed if the file was already there -systemctl enable vpnnat.service -systemctl start vpnnat.service +# note, no need to start it, the vpn_service does that. +systemctl enable vpnnat -systemctl restart openvpn@server +if $start; then + systemctl enable $vpn_service + systemctl restart $vpn_service +fi