2020-05-11 09:18:34 +03:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2020-12-26 09:19:50 +03:00
|
|
|
# Script to set up IKEv2 on Ubuntu, Debian, CentOS/RHEL and Amazon Linux 2
|
2020-05-11 09:18:34 +03:00
|
|
|
#
|
2020-12-15 08:12:15 +03:00
|
|
|
# The latest version of this script is available at:
|
|
|
|
# https://github.com/hwdsl2/setup-ipsec-vpn
|
|
|
|
#
|
2021-01-03 09:35:24 +03:00
|
|
|
# Copyright (C) 2020-2021 Lin Song <linsongui@gmail.com>
|
2020-05-11 09:18:34 +03:00
|
|
|
#
|
|
|
|
# This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
|
|
|
|
# Unported License: http://creativecommons.org/licenses/by-sa/3.0/
|
|
|
|
#
|
|
|
|
# Attribution required: please include my name in any derivative and let me
|
|
|
|
# know how you have improved it!
|
|
|
|
|
|
|
|
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
2020-06-05 18:56:33 +03:00
|
|
|
SYS_DT=$(date +%F-%T | tr ':' '_')
|
2020-05-11 09:18:34 +03:00
|
|
|
|
|
|
|
exiterr() { echo "Error: $1" >&2; exit 1; }
|
|
|
|
bigecho() { echo; echo "## $1"; echo; }
|
|
|
|
bigecho2() { echo; echo "## $1"; }
|
|
|
|
|
|
|
|
check_ip() {
|
|
|
|
IP_REGEX='^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
|
|
|
|
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
|
|
|
|
}
|
|
|
|
|
|
|
|
check_dns_name() {
|
|
|
|
FQDN_REGEX='^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$'
|
|
|
|
printf '%s' "$1" | tr -d '\n' | grep -Eq "$FQDN_REGEX"
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:56:33 +03:00
|
|
|
new_client() {
|
|
|
|
|
|
|
|
bigecho2 "Generating client certificate..."
|
|
|
|
|
2020-06-11 09:16:51 +03:00
|
|
|
sleep $((RANDOM % 3 + 1))
|
2020-06-05 18:56:33 +03:00
|
|
|
|
|
|
|
certutil -z <(head -c 1024 /dev/urandom) \
|
|
|
|
-S -c "IKEv2 VPN CA" -n "$client_name" \
|
|
|
|
-s "O=IKEv2 VPN,CN=$client_name" \
|
2020-07-02 19:48:35 +03:00
|
|
|
-k rsa -g 4096 -v "$client_validity" \
|
2020-06-05 18:56:33 +03:00
|
|
|
-d sql:/etc/ipsec.d -t ",," \
|
|
|
|
--keyUsage digitalSignature,keyEncipherment \
|
2020-06-11 09:16:51 +03:00
|
|
|
--extKeyUsage serverAuth,clientAuth -8 "$client_name" >/dev/null || exit 1
|
2020-06-05 18:56:33 +03:00
|
|
|
|
|
|
|
if [ "$export_ca" = "1" ]; then
|
2020-06-11 09:16:51 +03:00
|
|
|
bigecho "Exporting CA certificate 'IKEv2 VPN CA'..."
|
2020-06-05 18:56:33 +03:00
|
|
|
|
|
|
|
if [ "$in_container" = "0" ]; then
|
2020-06-11 09:16:51 +03:00
|
|
|
certutil -L -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" -a -o ~/"ikev2vpnca-$SYS_DT.cer" || exit 1
|
2020-06-05 18:56:33 +03:00
|
|
|
else
|
2020-06-11 09:16:51 +03:00
|
|
|
certutil -L -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" -a -o "/etc/ipsec.d/ikev2vpnca-$SYS_DT.cer" || exit 1
|
2020-06-05 18:56:33 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
bigecho "Exporting .p12 file..."
|
|
|
|
|
|
|
|
cat <<'EOF'
|
|
|
|
Enter a *secure* password to protect the exported .p12 file.
|
|
|
|
This file contains the client certificate, private key, and CA certificate.
|
|
|
|
When importing into an iOS or macOS device, this password cannot be empty.
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
if [ "$in_container" = "0" ]; then
|
2020-06-11 09:16:51 +03:00
|
|
|
pk12util -d sql:/etc/ipsec.d -n "$client_name" -o ~/"$client_name-$SYS_DT.p12" || exit 1
|
2020-06-05 18:56:33 +03:00
|
|
|
else
|
2020-06-11 09:16:51 +03:00
|
|
|
pk12util -d sql:/etc/ipsec.d -n "$client_name" -o "/etc/ipsec.d/$client_name-$SYS_DT.p12" || exit 1
|
2020-06-05 18:56:33 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:18:34 +03:00
|
|
|
ikev2setup() {
|
|
|
|
|
2021-01-11 03:28:52 +03:00
|
|
|
if grep -qs -e "release 7" -e "release 8" /etc/redhat-release; then
|
|
|
|
os_type=centos
|
|
|
|
if grep -qs "Red Hat" /etc/redhat-release; then
|
|
|
|
os_type=rhel
|
|
|
|
fi
|
|
|
|
elif grep -qs "Amazon Linux release 2" /etc/system-release; then
|
|
|
|
os_type=amzn
|
|
|
|
else
|
|
|
|
os_type=$(lsb_release -si 2>/dev/null)
|
|
|
|
[ -z "$os_type" ] && [ -f /etc/os-release ] && os_type=$(. /etc/os-release && printf '%s' "$ID")
|
|
|
|
case $os_type in
|
|
|
|
[Uu]buntu)
|
|
|
|
os_type=ubuntu
|
|
|
|
;;
|
|
|
|
[Dd]ebian)
|
|
|
|
os_type=debian
|
|
|
|
;;
|
|
|
|
[Rr]aspbian)
|
|
|
|
os_type=raspbian
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
exiterr "This script only supports Ubuntu, Debian, CentOS/RHEL 7/8 and Amazon Linux 2."
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
2020-05-11 09:18:34 +03:00
|
|
|
if [ "$(id -u)" != 0 ]; then
|
|
|
|
exiterr "Script must be run as root. Try 'sudo bash $0'"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ipsec_ver=$(/usr/local/sbin/ipsec --version 2>/dev/null)
|
2021-01-11 03:28:52 +03:00
|
|
|
swan_ver=$(printf '%s' "$ipsec_ver" | sed -e 's/Linux //' -e 's/Libreswan //' -e 's/ (netkey).*//')
|
2020-05-31 01:35:27 +03:00
|
|
|
if ( ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf && ! grep -qs "hwdsl2" /opt/src/run.sh ) \
|
2020-05-11 09:18:34 +03:00
|
|
|
|| ! printf '%s' "$ipsec_ver" | grep -q "Libreswan" \
|
2020-05-31 07:09:32 +03:00
|
|
|
|| [ ! -f /etc/ppp/chap-secrets ] || [ ! -f /etc/ipsec.d/passwd ]; then
|
2020-05-11 09:18:34 +03:00
|
|
|
cat 1>&2 <<'EOF'
|
|
|
|
Error: Your must first set up the IPsec VPN server before setting up IKEv2.
|
|
|
|
See: https://github.com/hwdsl2/setup-ipsec-vpn
|
|
|
|
EOF
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-01-11 03:28:52 +03:00
|
|
|
case $swan_ver in
|
|
|
|
3.19|3.2[01235679]|3.3[12]|4.*)
|
2020-05-11 09:18:34 +03:00
|
|
|
/bin/true
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
cat 1>&2 <<EOF
|
|
|
|
Error: Libreswan version '$swan_ver' is not supported.
|
|
|
|
This script requires one of these versions:
|
2021-01-11 03:28:52 +03:00
|
|
|
3.19-3.23, 3.25-3.27, 3.29, 3.31-3.32 or 4.x
|
|
|
|
To update Libreswan, see:
|
2020-05-11 19:28:37 +03:00
|
|
|
https://github.com/hwdsl2/setup-ipsec-vpn#upgrade-libreswan
|
2020-05-11 09:18:34 +03:00
|
|
|
EOF
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-06-11 09:16:51 +03:00
|
|
|
command -v certutil >/dev/null 2>&1 || exiterr "'certutil' not found. Abort."
|
|
|
|
command -v pk12util >/dev/null 2>&1 || exiterr "'pk12util' not found. Abort."
|
2020-06-05 08:29:15 +03:00
|
|
|
|
2021-01-11 03:28:52 +03:00
|
|
|
in_container=0
|
|
|
|
if grep -qs "hwdsl2" /opt/src/run.sh; then
|
|
|
|
in_container=1
|
|
|
|
fi
|
|
|
|
|
2020-05-31 07:09:32 +03:00
|
|
|
if grep -qs "conn ikev2-cp" /etc/ipsec.conf || [ -f /etc/ipsec.d/ikev2.conf ]; then
|
2020-06-05 08:29:15 +03:00
|
|
|
echo "It looks like IKEv2 has already been set up on this server."
|
|
|
|
printf "Do you want to add a new VPN client? [y/N] "
|
|
|
|
read -r response
|
|
|
|
case $response in
|
|
|
|
[yY][eE][sS]|[yY])
|
|
|
|
echo
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Abort. No changes were made."
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-06-05 18:56:33 +03:00
|
|
|
echo "Provide a name for the IKEv2 VPN client."
|
|
|
|
echo "Use one word only, no special characters except '-' and '_'."
|
2020-06-05 08:29:15 +03:00
|
|
|
read -rp "Client name: " client_name
|
|
|
|
while [ -z "$client_name" ] || [ "${#client_name}" -gt "64" ] \
|
|
|
|
|| printf '%s' "$client_name" | LC_ALL=C grep -q '[^A-Za-z0-9_-]\+' \
|
|
|
|
|| certutil -L -d sql:/etc/ipsec.d -n "$client_name" >/dev/null 2>&1; do
|
2020-06-05 18:56:33 +03:00
|
|
|
if [ -z "$client_name" ] || [ "${#client_name}" -gt "64" ] \
|
|
|
|
|| printf '%s' "$client_name" | LC_ALL=C grep -q '[^A-Za-z0-9_-]\+'; then
|
|
|
|
echo "Invalid client name."
|
|
|
|
else
|
2020-12-31 07:53:19 +03:00
|
|
|
echo "Invalid client name. Client '$client_name' already exists."
|
2020-06-05 18:56:33 +03:00
|
|
|
fi
|
2020-06-05 08:29:15 +03:00
|
|
|
read -rp "Client name: " client_name
|
|
|
|
done
|
|
|
|
|
2020-07-02 19:48:35 +03:00
|
|
|
echo
|
|
|
|
echo "Specify the validity period (in months) for this VPN client certificate."
|
|
|
|
read -rp "Enter a number between 1 and 120: [120] " client_validity
|
|
|
|
[ -z "$client_validity" ] && client_validity=120
|
|
|
|
while printf '%s' "$client_validity" | LC_ALL=C grep -q '[^0-9]\+' \
|
|
|
|
|| [ "$client_validity" -lt "1" ] || [ "$client_validity" -gt "120" ] \
|
|
|
|
|| [ "$client_validity" != "$((10#$client_validity))" ]; do
|
|
|
|
echo "Invalid validity period."
|
|
|
|
read -rp "Enter a number between 1 and 120: [120] " client_validity
|
|
|
|
[ -z "$client_validity" ] && client_validity=120
|
|
|
|
done
|
|
|
|
|
2020-06-05 08:29:15 +03:00
|
|
|
echo
|
2020-06-05 18:56:33 +03:00
|
|
|
echo "The CA certificate was exported during initial IKEv2 setup. Required for iOS clients only."
|
2020-06-05 08:29:15 +03:00
|
|
|
printf "Do you want to export the CA certificate again? [y/N] "
|
|
|
|
read -r response
|
|
|
|
case $response in
|
|
|
|
[yY][eE][sS]|[yY])
|
|
|
|
export_ca=1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
export_ca=0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-06-05 18:56:33 +03:00
|
|
|
# Create client configuration
|
|
|
|
new_client
|
2020-06-05 08:29:15 +03:00
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
=============================================================
|
|
|
|
|
|
|
|
New IKEv2 VPN client "$client_name" added!
|
|
|
|
|
|
|
|
Client configuration is available at:
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
if [ "$in_container" = "0" ]; then
|
|
|
|
printf '%s\n' ~/"$client_name-$SYS_DT.p12"
|
|
|
|
if [ "$export_ca" = "1" ]; then
|
2020-06-11 09:16:51 +03:00
|
|
|
printf '%s\n' ~/"ikev2vpnca-$SYS_DT.cer (for iOS clients)"
|
2020-06-05 08:29:15 +03:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
printf '%s\n' "/etc/ipsec.d/$client_name-$SYS_DT.p12"
|
|
|
|
if [ "$export_ca" = "1" ]; then
|
2020-06-11 09:16:51 +03:00
|
|
|
printf '%s\n' "/etc/ipsec.d/ikev2vpnca-$SYS_DT.cer (for iOS clients)"
|
2020-06-05 08:29:15 +03:00
|
|
|
fi
|
2020-05-11 09:18:34 +03:00
|
|
|
fi
|
|
|
|
|
2020-07-13 01:14:30 +03:00
|
|
|
cat <<'EOF'
|
2020-06-05 08:29:15 +03:00
|
|
|
|
|
|
|
Next steps: Configure IKEv2 VPN clients. See:
|
|
|
|
https://git.io/ikev2clients
|
|
|
|
|
2020-12-20 10:15:00 +03:00
|
|
|
To add more IKEv2 VPN clients, run this script again.
|
|
|
|
|
2020-06-05 08:29:15 +03:00
|
|
|
=============================================================
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
fi
|
2020-05-11 09:18:34 +03:00
|
|
|
|
2020-06-11 09:16:51 +03:00
|
|
|
if certutil -L -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" >/dev/null 2>&1; then
|
|
|
|
exiterr "Certificate 'IKEv2 VPN CA' already exists. Abort."
|
|
|
|
fi
|
|
|
|
|
2020-05-11 09:18:34 +03:00
|
|
|
clear
|
|
|
|
|
|
|
|
cat <<'EOF'
|
|
|
|
Welcome! Use this script to set up IKEv2 after setting up your own IPsec VPN server.
|
|
|
|
Alternatively, you may manually set up IKEv2. See: https://git.io/ikev2
|
|
|
|
|
|
|
|
I need to ask you a few questions before starting setup.
|
|
|
|
You can use the default options and just press enter if you are OK with them.
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
2020-05-12 07:15:05 +03:00
|
|
|
echo "Do you want IKEv2 VPN clients to connect to this server using a DNS name,"
|
2020-05-15 06:41:13 +03:00
|
|
|
printf "e.g. vpn.example.com, instead of its IP address? [y/N] "
|
2020-05-11 09:18:34 +03:00
|
|
|
read -r response
|
|
|
|
case $response in
|
|
|
|
[yY][eE][sS]|[yY])
|
|
|
|
use_dns_name=1
|
|
|
|
echo
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
use_dns_name=0
|
|
|
|
echo
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Enter VPN server address
|
|
|
|
if [ "$use_dns_name" = "1" ]; then
|
|
|
|
read -rp "Enter the DNS name of this VPN server: " server_addr
|
|
|
|
until check_dns_name "$server_addr"; do
|
|
|
|
echo "Invalid DNS name. You must enter a fully qualified domain name (FQDN)."
|
|
|
|
read -rp "Enter the DNS name of this VPN server: " server_addr
|
|
|
|
done
|
|
|
|
else
|
2020-12-14 00:52:03 +03:00
|
|
|
echo "Trying to auto discover IP of this server..."
|
|
|
|
echo
|
2020-05-11 09:18:34 +03:00
|
|
|
public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
|
2020-12-14 00:52:03 +03:00
|
|
|
check_ip "$public_ip" || public_ip=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
|
2020-05-15 06:41:13 +03:00
|
|
|
read -rp "Enter the IPv4 address of this VPN server: [$public_ip] " server_addr
|
2020-05-11 09:18:34 +03:00
|
|
|
[ -z "$server_addr" ] && server_addr="$public_ip"
|
|
|
|
until check_ip "$server_addr"; do
|
|
|
|
echo "Invalid IP address."
|
2020-05-15 06:41:13 +03:00
|
|
|
read -rp "Enter the IPv4 address of this VPN server: [$public_ip] " server_addr
|
2020-05-11 09:18:34 +03:00
|
|
|
[ -z "$server_addr" ] && server_addr="$public_ip"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2020-12-31 07:53:19 +03:00
|
|
|
if certutil -L -d sql:/etc/ipsec.d -n "$server_addr" >/dev/null 2>&1; then
|
|
|
|
exiterr "Certificate '$server_addr' already exists. Abort."
|
|
|
|
fi
|
|
|
|
|
2020-06-05 18:56:33 +03:00
|
|
|
# Enter client name
|
2020-06-05 08:29:15 +03:00
|
|
|
echo
|
2020-06-05 18:56:33 +03:00
|
|
|
echo "Provide a name for the IKEv2 VPN client."
|
|
|
|
echo "Use one word only, no special characters except '-' and '_'."
|
2020-06-05 08:29:15 +03:00
|
|
|
read -rp "Client name: [vpnclient] " client_name
|
|
|
|
[ -z "$client_name" ] && client_name=vpnclient
|
2020-12-31 07:53:19 +03:00
|
|
|
while [ "${#client_name}" -gt "64" ] \
|
|
|
|
|| printf '%s' "$client_name" | LC_ALL=C grep -q '[^A-Za-z0-9_-]\+' \
|
|
|
|
|| certutil -L -d sql:/etc/ipsec.d -n "$client_name" >/dev/null 2>&1; do
|
|
|
|
if [ "${#client_name}" -gt "64" ] \
|
|
|
|
|| printf '%s' "$client_name" | LC_ALL=C grep -q '[^A-Za-z0-9_-]\+'; then
|
|
|
|
echo "Invalid client name."
|
|
|
|
else
|
|
|
|
echo "Invalid client name. Client '$client_name' already exists."
|
|
|
|
fi
|
2020-06-05 08:29:15 +03:00
|
|
|
read -rp "Client name: [vpnclient] " client_name
|
|
|
|
[ -z "$client_name" ] && client_name=vpnclient
|
|
|
|
done
|
|
|
|
|
2020-07-02 19:48:35 +03:00
|
|
|
# Enter validity period
|
|
|
|
echo
|
|
|
|
echo "Specify the validity period (in months) for this VPN client certificate."
|
|
|
|
read -rp "Enter a number between 1 and 120: [120] " client_validity
|
|
|
|
[ -z "$client_validity" ] && client_validity=120
|
|
|
|
while printf '%s' "$client_validity" | LC_ALL=C grep -q '[^0-9]\+' \
|
|
|
|
|| [ "$client_validity" -lt "1" ] || [ "$client_validity" -gt "120" ] \
|
|
|
|
|| [ "$client_validity" != "$((10#$client_validity))" ]; do
|
|
|
|
echo "Invalid validity period."
|
|
|
|
read -rp "Enter a number between 1 and 120: [120] " client_validity
|
|
|
|
[ -z "$client_validity" ] && client_validity=120
|
|
|
|
done
|
|
|
|
|
2020-07-13 01:14:30 +03:00
|
|
|
# Enter custom DNS servers
|
|
|
|
echo
|
|
|
|
echo "By default, clients are set to use Google Public DNS when the VPN is active."
|
|
|
|
printf "Do you want to specify custom DNS servers for IKEv2? [y/N] "
|
|
|
|
read -r response
|
|
|
|
case $response in
|
|
|
|
[yY][eE][sS]|[yY])
|
|
|
|
use_custom_dns=1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
use_custom_dns=0
|
|
|
|
dns_server_1=8.8.8.8
|
|
|
|
dns_server_2=8.8.4.4
|
|
|
|
dns_servers="8.8.8.8 8.8.4.4"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ "$use_custom_dns" = "1" ]; then
|
|
|
|
read -rp "Enter primary DNS server: " dns_server_1
|
|
|
|
until check_ip "$dns_server_1"; do
|
|
|
|
echo "Invalid DNS server."
|
|
|
|
read -rp "Enter primary DNS server: " dns_server_1
|
|
|
|
done
|
|
|
|
|
|
|
|
read -rp "Enter secondary DNS server (Enter to skip): " dns_server_2
|
|
|
|
until [ -z "$dns_server_2" ] || check_ip "$dns_server_2"; do
|
|
|
|
echo "Invalid DNS server."
|
|
|
|
read -rp "Enter secondary DNS server (Enter to skip): " dns_server_2
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -n "$dns_server_2" ]; then
|
|
|
|
dns_servers="$dns_server_1 $dns_server_2"
|
|
|
|
else
|
|
|
|
dns_servers="$dns_server_1"
|
|
|
|
fi
|
2020-12-14 00:52:03 +03:00
|
|
|
else
|
|
|
|
echo "Using Google Public DNS (8.8.8.8, 8.8.4.4)."
|
2020-07-13 01:14:30 +03:00
|
|
|
fi
|
|
|
|
|
2020-05-11 09:18:34 +03:00
|
|
|
# Check for MOBIKE support
|
|
|
|
mobike_support=0
|
2021-01-11 03:28:52 +03:00
|
|
|
case $swan_ver in
|
|
|
|
3.2[35679]|3.3[12]|4.*)
|
2020-05-11 09:18:34 +03:00
|
|
|
mobike_support=1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-11-25 06:27:40 +03:00
|
|
|
if uname -m | grep -qi -e '^arm' -e '^aarch64'; then
|
2020-05-17 06:11:01 +03:00
|
|
|
mobike_support=0
|
|
|
|
fi
|
|
|
|
|
2020-05-11 09:18:34 +03:00
|
|
|
if [ "$mobike_support" = "1" ]; then
|
2020-05-31 07:09:32 +03:00
|
|
|
if [ "$in_container" = "0" ]; then
|
2020-07-13 01:14:30 +03:00
|
|
|
# Linux kernels on Ubuntu do not support MOBIKE
|
2021-01-11 03:28:52 +03:00
|
|
|
if [ "$os_type" = "ubuntu" ]; then
|
2020-05-31 07:09:32 +03:00
|
|
|
mobike_support=0
|
|
|
|
fi
|
2020-05-11 09:18:34 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-12-14 00:52:03 +03:00
|
|
|
echo
|
|
|
|
echo -n "Checking for MOBIKE support... "
|
|
|
|
if [ "$mobike_support" = "1" ]; then
|
|
|
|
if [ "$in_container" = "0" ]; then
|
2020-12-31 07:53:19 +03:00
|
|
|
echo "yes"
|
2020-12-14 00:52:03 +03:00
|
|
|
else
|
2020-12-31 07:53:19 +03:00
|
|
|
echo "running in a container, see notes below"
|
2020-12-14 00:52:03 +03:00
|
|
|
fi
|
|
|
|
else
|
2020-12-31 07:53:19 +03:00
|
|
|
echo "no"
|
2020-12-14 00:52:03 +03:00
|
|
|
fi
|
|
|
|
|
2020-05-11 09:18:34 +03:00
|
|
|
mobike_enable=0
|
|
|
|
if [ "$mobike_support" = "1" ]; then
|
2020-07-13 01:14:30 +03:00
|
|
|
echo
|
|
|
|
echo "The MOBIKE IKEv2 extension allows VPN clients to change network attachment points,"
|
|
|
|
echo "e.g. switch between mobile data and Wi-Fi and keep the IPsec tunnel up on the new IP."
|
2020-05-31 07:09:32 +03:00
|
|
|
if [ "$in_container" = "0" ]; then
|
|
|
|
echo
|
|
|
|
printf "Do you want to enable MOBIKE support? [Y/n] "
|
|
|
|
read -r response
|
|
|
|
case $response in
|
|
|
|
[yY][eE][sS]|[yY]|'')
|
|
|
|
mobike_enable=1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
mobike_enable=0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
echo
|
2020-07-13 01:14:30 +03:00
|
|
|
echo "IMPORTANT: *DO NOT* enable MOBIKE support, if your Docker host runs Ubuntu Linux."
|
2020-05-31 07:09:32 +03:00
|
|
|
printf "Do you want to enable MOBIKE support? [y/N] "
|
|
|
|
read -r response
|
|
|
|
case $response in
|
|
|
|
[yY][eE][sS]|[yY])
|
|
|
|
mobike_enable=1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
mobike_enable=0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2020-05-11 09:18:34 +03:00
|
|
|
fi
|
|
|
|
|
2020-05-15 06:41:13 +03:00
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
Below are the IKEv2 setup options you selected.
|
|
|
|
Please double check before continuing!
|
|
|
|
|
|
|
|
================================================
|
|
|
|
|
|
|
|
VPN server address: $server_addr
|
2020-06-05 08:29:15 +03:00
|
|
|
VPN client name: $client_name
|
2020-12-31 07:53:19 +03:00
|
|
|
|
2020-05-15 06:41:13 +03:00
|
|
|
EOF
|
|
|
|
|
2020-07-02 19:48:35 +03:00
|
|
|
if [ "$client_validity" = "1" ]; then
|
|
|
|
echo "Client cert valid for: 1 month"
|
|
|
|
else
|
|
|
|
echo "Client cert valid for: $client_validity months"
|
|
|
|
fi
|
|
|
|
|
2020-05-15 06:41:13 +03:00
|
|
|
if [ "$mobike_support" = "1" ]; then
|
|
|
|
if [ "$mobike_enable" = "1" ]; then
|
2020-12-14 00:52:03 +03:00
|
|
|
echo "MOBIKE support: Enable"
|
2020-05-15 06:41:13 +03:00
|
|
|
else
|
2020-12-14 00:52:03 +03:00
|
|
|
echo "MOBIKE support: Disable"
|
2020-05-15 06:41:13 +03:00
|
|
|
fi
|
2020-12-14 00:52:03 +03:00
|
|
|
else
|
|
|
|
echo "MOBIKE support: Not available"
|
2020-05-15 06:41:13 +03:00
|
|
|
fi
|
|
|
|
|
2020-12-14 00:52:03 +03:00
|
|
|
cat <<EOF
|
|
|
|
DNS server(s): $dns_servers
|
2020-05-15 06:41:13 +03:00
|
|
|
|
|
|
|
================================================
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
printf "We are ready to set up IKEv2 now. Do you want to continue? [y/N] "
|
2020-05-11 09:18:34 +03:00
|
|
|
read -r response
|
|
|
|
case $response in
|
|
|
|
[yY][eE][sS]|[yY])
|
|
|
|
echo
|
|
|
|
;;
|
|
|
|
*)
|
2020-05-15 06:41:13 +03:00
|
|
|
echo "Abort. No changes were made."
|
2020-05-11 09:18:34 +03:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-06-11 09:16:51 +03:00
|
|
|
bigecho2 "Generating CA certificate..."
|
|
|
|
|
|
|
|
certutil -z <(head -c 1024 /dev/urandom) \
|
|
|
|
-S -x -n "IKEv2 VPN CA" \
|
|
|
|
-s "O=IKEv2 VPN,CN=IKEv2 VPN CA" \
|
|
|
|
-k rsa -g 4096 -v 120 \
|
|
|
|
-d sql:/etc/ipsec.d -t "CT,," -2 >/dev/null <<ANSWERS || exit 1
|
|
|
|
y
|
|
|
|
|
|
|
|
N
|
|
|
|
ANSWERS
|
|
|
|
|
|
|
|
sleep $((RANDOM % 3 + 1))
|
|
|
|
|
|
|
|
bigecho2 "Generating VPN server certificate..."
|
|
|
|
|
|
|
|
if [ "$use_dns_name" = "1" ]; then
|
|
|
|
certutil -z <(head -c 1024 /dev/urandom) \
|
|
|
|
-S -c "IKEv2 VPN CA" -n "$server_addr" \
|
|
|
|
-s "O=IKEv2 VPN,CN=$server_addr" \
|
|
|
|
-k rsa -g 4096 -v 120 \
|
|
|
|
-d sql:/etc/ipsec.d -t ",," \
|
|
|
|
--keyUsage digitalSignature,keyEncipherment \
|
|
|
|
--extKeyUsage serverAuth \
|
|
|
|
--extSAN "dns:$server_addr" >/dev/null || exit 1
|
|
|
|
else
|
|
|
|
certutil -z <(head -c 1024 /dev/urandom) \
|
|
|
|
-S -c "IKEv2 VPN CA" -n "$server_addr" \
|
|
|
|
-s "O=IKEv2 VPN,CN=$server_addr" \
|
|
|
|
-k rsa -g 4096 -v 120 \
|
|
|
|
-d sql:/etc/ipsec.d -t ",," \
|
|
|
|
--keyUsage digitalSignature,keyEncipherment \
|
|
|
|
--extKeyUsage serverAuth \
|
|
|
|
--extSAN "ip:$server_addr,dns:$server_addr" >/dev/null || exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create client configuration
|
|
|
|
export_ca=1
|
|
|
|
new_client
|
|
|
|
|
|
|
|
echo
|
2020-05-31 07:09:32 +03:00
|
|
|
bigecho "Adding a new IKEv2 connection..."
|
|
|
|
|
|
|
|
if ! grep -qs '^include /etc/ipsec\.d/\*\.conf$' /etc/ipsec.conf; then
|
|
|
|
echo >> /etc/ipsec.conf
|
|
|
|
echo 'include /etc/ipsec.d/*.conf' >> /etc/ipsec.conf
|
|
|
|
fi
|
2020-05-11 09:18:34 +03:00
|
|
|
|
2020-05-31 07:09:32 +03:00
|
|
|
cat > /etc/ipsec.d/ikev2.conf <<EOF
|
2020-05-11 09:18:34 +03:00
|
|
|
|
|
|
|
conn ikev2-cp
|
|
|
|
left=%defaultroute
|
|
|
|
leftcert=$server_addr
|
|
|
|
leftid=@$server_addr
|
|
|
|
leftsendcert=always
|
|
|
|
leftsubnet=0.0.0.0/0
|
|
|
|
leftrsasigkey=%cert
|
|
|
|
right=%any
|
|
|
|
rightid=%fromcert
|
|
|
|
rightaddresspool=192.168.43.10-192.168.43.250
|
|
|
|
rightca=%same
|
|
|
|
rightrsasigkey=%cert
|
|
|
|
narrowing=yes
|
|
|
|
dpddelay=30
|
|
|
|
dpdtimeout=120
|
|
|
|
dpdaction=clear
|
|
|
|
auto=add
|
|
|
|
ikev2=insist
|
|
|
|
rekey=no
|
|
|
|
pfs=no
|
2020-11-11 09:27:44 +03:00
|
|
|
fragmentation=yes
|
2020-05-11 09:18:34 +03:00
|
|
|
ike=aes256-sha2,aes128-sha2,aes256-sha1,aes128-sha1,aes256-sha2;modp1024,aes128-sha1;modp1024
|
|
|
|
phase2alg=aes_gcm-null,aes128-sha1,aes256-sha1,aes128-sha2,aes256-sha2
|
2020-07-13 01:14:30 +03:00
|
|
|
encapsulation=yes
|
2020-05-11 09:18:34 +03:00
|
|
|
EOF
|
|
|
|
|
2021-01-11 03:28:52 +03:00
|
|
|
case $swan_ver in
|
|
|
|
3.2[35679]|3.3[12]|4.*)
|
2020-07-13 01:14:30 +03:00
|
|
|
if [ -n "$dns_server_2" ]; then
|
|
|
|
cat >> /etc/ipsec.d/ikev2.conf <<EOF
|
|
|
|
modecfgdns="$dns_servers"
|
|
|
|
EOF
|
|
|
|
else
|
|
|
|
cat >> /etc/ipsec.d/ikev2.conf <<EOF
|
|
|
|
modecfgdns=$dns_server_1
|
2020-05-11 09:18:34 +03:00
|
|
|
EOF
|
2020-07-13 01:14:30 +03:00
|
|
|
fi
|
2020-05-11 09:18:34 +03:00
|
|
|
if [ "$mobike_enable" = "1" ]; then
|
2020-05-31 07:09:32 +03:00
|
|
|
echo " mobike=yes" >> /etc/ipsec.d/ikev2.conf
|
2020-05-11 09:18:34 +03:00
|
|
|
else
|
2020-05-31 07:09:32 +03:00
|
|
|
echo " mobike=no" >> /etc/ipsec.d/ikev2.conf
|
2020-05-11 09:18:34 +03:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
3.19|3.2[012])
|
2020-07-13 01:14:30 +03:00
|
|
|
if [ -n "$dns_server_2" ]; then
|
|
|
|
cat >> /etc/ipsec.d/ikev2.conf <<EOF
|
|
|
|
modecfgdns1=$dns_server_1
|
|
|
|
modecfgdns2=$dns_server_2
|
2020-05-11 09:18:34 +03:00
|
|
|
EOF
|
2020-07-13 01:14:30 +03:00
|
|
|
else
|
|
|
|
cat >> /etc/ipsec.d/ikev2.conf <<EOF
|
|
|
|
modecfgdns1=$dns_server_1
|
|
|
|
EOF
|
|
|
|
fi
|
2020-05-11 09:18:34 +03:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
bigecho "Restarting IPsec service..."
|
|
|
|
|
2020-05-15 06:41:13 +03:00
|
|
|
mkdir -p /run/pluto
|
2020-05-11 09:18:34 +03:00
|
|
|
service ipsec restart
|
|
|
|
|
|
|
|
cat <<EOF
|
2020-05-15 06:41:13 +03:00
|
|
|
|
2020-05-31 07:09:32 +03:00
|
|
|
=============================================================
|
2020-05-11 09:18:34 +03:00
|
|
|
|
|
|
|
IKEv2 VPN setup is now complete!
|
|
|
|
|
2020-07-13 01:14:30 +03:00
|
|
|
VPN server address: $server_addr
|
|
|
|
VPN client name: $client_name
|
|
|
|
|
2020-05-18 02:09:40 +03:00
|
|
|
Client configuration is available at:
|
2020-05-15 06:41:13 +03:00
|
|
|
|
2020-05-18 02:09:40 +03:00
|
|
|
EOF
|
|
|
|
|
2020-05-31 07:09:32 +03:00
|
|
|
if [ "$in_container" = "0" ]; then
|
2020-06-05 08:29:15 +03:00
|
|
|
printf '%s\n' ~/"$client_name-$SYS_DT.p12"
|
2020-06-11 09:16:51 +03:00
|
|
|
printf '%s\n' ~/"ikev2vpnca-$SYS_DT.cer (for iOS clients)"
|
2020-05-31 07:09:32 +03:00
|
|
|
else
|
2020-06-05 08:29:15 +03:00
|
|
|
printf '%s\n' "/etc/ipsec.d/$client_name-$SYS_DT.p12"
|
2020-06-11 09:16:51 +03:00
|
|
|
printf '%s\n' "/etc/ipsec.d/ikev2vpnca-$SYS_DT.cer (for iOS clients)"
|
2020-05-31 07:09:32 +03:00
|
|
|
fi
|
2020-05-18 02:09:40 +03:00
|
|
|
|
2020-07-13 01:14:30 +03:00
|
|
|
cat <<'EOF'
|
2020-05-11 09:18:34 +03:00
|
|
|
|
|
|
|
Next steps: Configure IKEv2 VPN clients. See:
|
|
|
|
https://git.io/ikev2clients
|
|
|
|
|
2020-12-20 10:15:00 +03:00
|
|
|
To add more IKEv2 VPN clients, run this script again.
|
|
|
|
|
2020-05-31 07:09:32 +03:00
|
|
|
=============================================================
|
2020-05-11 09:18:34 +03:00
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
## Defer setup until we have the complete script
|
|
|
|
ikev2setup "$@"
|
|
|
|
|
|
|
|
exit 0
|