#!/bin/bash # Setup Strong strongSwan server for Ubuntu and Debian # # Copyright (C) 2014-2015 Phil Plückthun # Based on Strongswan on Docker # https://github.com/philplckthun/docker-strongswan # # This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 # Unported License: http://creativecommons.org/licenses/by-sa/3.0/ if [ `id -u` -ne 0 ] then echo "Please start this script with root privileges!" echo "Try again with sudo." exit 0 fi ################################################################# # Variables [ -z "$STRONGSWAN_TMP" ] && STRONGSWAN_TMP="/tmp/strongswan" [ -z "$STRONGSWAN_VERSION" ] && STRONGSWAN_VERSION="5.5.1" [ -z "$KEYSIZE" ] && KEYSIZE=16 #STRONGSWAN_USER #STRONGSWAN_PASSWORD #STRONGSWAN_PSK if [ -z "$INTERACTIVE" ]; then INTERACTIVE=1 fi [[ $INTERACTIVE = "true" ]] && INTERACTIVE=1 [[ $INTERACTIVE = "false" ]] && INTERACTIVE=0 ################################################################# # Functions call () { eval "$@ > /dev/null 2>&1" } checkForError () { if [ "$?" = "1" ] then bigEcho "An unexpected error occured!" exit 1 fi } generateKey () { KEY=`cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c $KEYSIZE` } bigEcho () { echo "" echo "============================================================" echo "$@" echo "============================================================" echo "" } pacapt () { eval "$STRONGSWAN_TMP/pacapt $@" } backupCredentials () { if [ -f /etc/ipsec.secrets ]; then cp /etc/ipsec.secrets /etc/ipsec.secrets.backup fi if [ -f /etc/ppp/l2tp-secrets ]; then cp /etc/ppp/l2tp-secrets /etc/ppp/l2tp-secrets.backup fi } writeCredentials () { bigEcho "Saving credentials" cat > /etc/ipsec.secrets < /etc/ppp/chap-secrets < $STRONGSWAN_TMP/pacapt if [ "$?" = "1" ]; then bigEcho "An unexpected error occured while downloading pacapt!" exit 1 fi call chmod +x $STRONGSWAN_TMP/pacapt echo "" ################################################################# bigEcho "Installing necessary dependencies" call pacapt -Sy --noconfirm checkForError call pacapt -S --noconfirm -- make g++ gcc iptables xl2tpd libssl-dev module-init-tools curl openssl-devel checkForError ################################################################# bigEcho "Installing StrongSwan..." call mkdir -p $STRONGSWAN_TMP/src curl -sSL "https://download.strongswan.org/strongswan-$STRONGSWAN_VERSION.tar.gz" | tar -zxC $STRONGSWAN_TMP/src --strip-components 1 checkForError cd $STRONGSWAN_TMP/src ./configure --prefix=/usr --sysconfdir=/etc \ --enable-eap-radius \ --enable-eap-mschapv2 \ --enable-eap-identity \ --enable-eap-md5 \ --enable-eap-mschapv2 \ --enable-eap-tls \ --enable-eap-ttls \ --enable-eap-peap \ --enable-eap-tnc \ --enable-eap-dynamic \ --enable-xauth-eap \ --enable-openssl \ --disable-gmp checkForError make checkForError make install checkForError ################################################################# bigEcho "Preparing various configuration files..." cat > /etc/ipsec.conf < /etc/strongswan.conf < /etc/xl2tpd/xl2tpd.conf < /etc/ppp/options.xl2tpd < /proc/sys/net/ipv4/ip_forward for each in /proc/sys/net/ipv4/conf/* do echo 0 > $each/accept_redirects echo 0 > $each/send_redirects done ################################################################# bigEcho "Create /etc/init.d/vpn-assist helper..." cat > /etc/init.d/vpn-assist <<'EOF' #!/bin/sh ### BEGIN INIT INFO # Provides: vpn # Required-Start: $network $local_fs # Required-Stop: $network $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Strongswan and L2TPD helper # Description: Service that starts up XL2TPD and IPSEC ### END INIT INFO # Author: Phil Plückthun case "$1" in start) iptables --table nat --append POSTROUTING --jump MASQUERADE echo 1 > /proc/sys/net/ipv4/ip_forward for each in /proc/sys/net/ipv4/conf/* do echo 0 > $each/accept_redirects echo 0 > $each/send_redirects done /usr/sbin/xl2tpd -p /var/run/xl2tpd.pid -c /etc/xl2tpd/xl2tpd.conf -C /var/run/xl2tpd.control ipsec start ;; stop) iptables --table nat --flush echo 0 > /proc/sys/net/ipv4/ip_forward kill $(cat /var/run/xl2tpd.pid) ipsec stop ;; restart) echo "Restarting IPSec and XL2TPD" iptables --table nat --append POSTROUTING --jump MASQUERADE echo 1 > /proc/sys/net/ipv4/ip_forward for each in /proc/sys/net/ipv4/conf/* do echo 0 > $each/accept_redirects echo 0 > $each/send_redirects done kill $(cat /var/run/xl2tpd.pid) /usr/sbin/xl2tpd -p /var/run/xl2tpd.pid -c /etc/xl2tpd/xl2tpd.conf -C /var/run/xl2tpd.control ipsec restart ;; esac exit 0 EOF chmod +x /etc/init.d/vpn-assist ################################################################# bigEcho "Starting up VPN..." /etc/init.d/vpn-assist start ################################################################# echo "============================================================" echo "PSK Key: $STRONGSWAN_PSK" echo "Username: $STRONGSWAN_USER" echo "Password: $STRONGSWAN_PASSWORD" echo "============================================================" echo "Note:" echo "* Before connecting with a Windows client, please see: http://support.microsoft.com/kb/926179" echo "* UDP Ports 1701, 500 and 4500 must be opened" echo "* A specific host or public IP is not necessary as Strongswan utilises NAT traversal" ################################################################# bigEcho "Cleaning up..." call rm -rf $STRONGSWAN_TMP sleep 2 exit 0