2016-04-19 18:01:55 +00:00
#!/usr/bin/env bash
2016-11-05 13:42:37 +00:00
# PiVPN: Trivial OpenVPN setup and configuration
# Easiest setup and mangement of OpenVPN on Raspberry Pi
2016-04-20 21:33:54 +00:00
# http://pivpn.io
# Heavily adapted from the pi-hole.net project and...
2016-04-19 18:01:55 +00:00
# https://github.com/StarshipEngineer/OpenVPN-Setup/
#
# Install with this command (from your Pi):
#
2016-04-29 17:18:28 +00:00
# curl -L https://install.pivpn.io | bash
2016-11-05 13:42:37 +00:00
# Make sure you have `curl` installed
2017-03-13 04:00:28 +00:00
set -e
2016-04-19 18:01:55 +00:00
2019-03-03 09:18:45 +00:00
######## VARIABLES #########
2017-03-13 04:00:28 +00:00
setupVars = /etc/pivpn/setupVars.conf
useUpdateVars = false
2016-12-24 05:20:45 +00:00
### PKG Vars ###
PKG_MANAGER = "apt-get"
PKG_CACHE = "/var/lib/apt/lists/"
UPDATE_PKG_CACHE = " ${ PKG_MANAGER } update "
PKG_INSTALL = " ${ PKG_MANAGER } --yes --no-install-recommends install "
PKG_COUNT = " ${ PKG_MANAGER } -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true "
2019-08-27 22:20:00 +00:00
PIVPN_DEPS = ( openvpn git tar wget grep iptables-persistent dnsutils expect whiptail net-tools grepcidr jq)
2019-08-08 08:34:17 +00:00
2016-12-24 05:20:45 +00:00
### ###
2016-04-20 01:49:34 +00:00
pivpnGitUrl = "https://github.com/pivpn/pivpn.git"
2016-04-19 18:01:55 +00:00
pivpnFilesDir = "/etc/.pivpn"
2019-03-03 09:18:45 +00:00
easyrsaVer = "3.0.6"
easyrsaRel = " https://github.com/OpenVPN/easy-rsa/releases/download/v ${ easyrsaVer } /EasyRSA-unix-v ${ easyrsaVer } .tgz "
2018-09-04 10:06:52 +00:00
2019-07-23 20:12:35 +00:00
# Raspbian's unattended-upgrades package downloads Debian's config, so this is the link for the proper config
2019-03-03 09:18:45 +00:00
UNATTUPG_RELEASE = "1.9"
UNATTUPG_CONFIG = " https://github.com/mvo5/unattended-upgrades/archive/ ${ UNATTUPG_RELEASE } .tar.gz "
2016-04-19 18:01:55 +00:00
2016-11-08 17:03:42 +00:00
# Find the rows and columns. Will default to 80x24 if it can not be detected.
2017-01-22 11:44:36 +00:00
screen_size = $( stty size 2>/dev/null || echo 24 80)
2016-11-08 17:03:42 +00:00
rows = $( echo $screen_size | awk '{print $1}' )
columns = $( echo $screen_size | awk '{print $2}' )
2016-04-19 18:01:55 +00:00
# Divide by two so the dialogs take up half of the screen, which looks nice.
r = $(( rows / 2 ))
c = $(( columns / 2 ))
2016-11-08 17:03:42 +00:00
# Unless the screen is tiny
r = $(( r < 20 ? 20 : r ))
c = $(( c < 70 ? 70 : c ))
2016-04-19 18:01:55 +00:00
2017-03-13 04:00:28 +00:00
######## Undocumented Flags. Shhh ########
skipSpaceCheck = false
reconfigure = false
runUnattended = false
2016-04-19 18:01:55 +00:00
# Find IP used to route to outside world
IPv4dev = $( ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}' )
2017-01-27 20:42:42 +00:00
IPv4addr = $( ip route get 8.8.8.8| awk '{print $7}' )
2016-04-19 18:01:55 +00:00
IPv4gw = $( ip route get 8.8.8.8 | awk '{print $3}' )
2017-01-27 20:42:42 +00:00
availableInterfaces = $( ip -o link | grep "state UP" | awk '{print $2}' | cut -d':' -f1 | cut -d'@' -f1)
2016-04-19 18:01:55 +00:00
dhcpcdFile = /etc/dhcpcd.conf
2016-05-10 15:49:29 +00:00
# Next see if we are on a tested and supported OS
function noOS_Support( ) {
whiptail --msgbox --backtitle "INVALID OS DETECTED" --title "Invalid OS" " We have not been able to detect a supported OS.
2017-10-08 19:10:57 +00:00
Currently this installer supports Raspbian and Debian ( Jessie and Stretch) , Devuan ( Jessie) and Ubuntu from 14.04 ( trusty) to 17.04 ( zesty) .
2016-11-22 18:23:51 +00:00
If you think you received this message in error, you can post an issue on the GitHub at https://github.com/pivpn/pivpn/issues." ${ r } ${ c }
2016-05-10 15:49:29 +00:00
exit 1
}
2016-05-16 01:43:32 +00:00
function maybeOS_Support( ) {
2016-09-27 12:24:14 +00:00
if ( whiptail --backtitle "Not Supported OS" --title "Not Supported OS" --yesno " You are on an OS that we have not tested but MAY work.
2019-10-12 16:34:37 +00:00
Currently suppoerted:
Raspbian, Debian, Devuan ( Jessie, strech, buster)
Ubuntu from 14.04 ( trusty) to 18.04 ( zesty) .
2017-03-13 04:00:28 +00:00
Would you like to continue anyway?" ${ r } ${ c } ) then
echo "::: Did not detect perfectly supported OS but,"
echo "::: Continuing installation at user's own risk..."
2016-05-16 01:43:32 +00:00
else
2017-03-13 04:00:28 +00:00
echo "::: Exiting due to unsupported OS"
exit 1
2016-09-27 12:24:14 +00:00
fi
2017-03-13 04:00:28 +00:00
}
2016-04-22 19:16:48 +00:00
2017-03-13 04:00:28 +00:00
# Compatibility
distro_check( ) {
2018-09-04 10:06:52 +00:00
# if lsb_release command is on their system
if hash lsb_release 2>/dev/null; then
2018-04-02 10:07:58 +00:00
2018-09-04 10:06:52 +00:00
PLAT = $( lsb_release -si)
2019-10-12 16:34:37 +00:00
OSCN = $( lsb_release -sc) # We want this to be trusty xenial, jessie, stretch, buster
2018-04-02 10:07:58 +00:00
2018-09-04 10:06:52 +00:00
else # else get info from os-release
2018-04-02 10:07:58 +00:00
2018-09-04 10:06:52 +00:00
source /etc/os-release
PLAT = $( awk '{print $1}' <<< " $NAME " )
VER = " $VERSION_ID "
2019-07-23 20:12:35 +00:00
declare -A VER_MAP = ( [ "10" ] = "buster" [ "9" ] = "stretch" [ "8" ] = "jessie" [ "18.04" ] = "bionic" [ "16.04" ] = "xenial" [ "14.04" ] = "trusty" )
2018-09-04 10:06:52 +00:00
OSCN = ${ VER_MAP [ " ${ VER } " ] }
fi
2018-04-02 10:07:58 +00:00
2018-09-04 10:06:52 +00:00
if [ [ ${ OSCN } != "bionic" ] ] ; then
PIVPN_DEPS += ( dhcpcd5)
2019-07-23 20:12:35 +00:00
2018-09-04 10:06:52 +00:00
fi
2018-04-02 10:07:58 +00:00
2018-09-04 10:06:52 +00:00
case ${ PLAT } in
Ubuntu| Raspbian| Debian| Devuan)
case ${ OSCN } in
2019-10-12 16:34:37 +00:00
trusty| xenial| jessie| stretch| buster| bionic)
2018-09-04 10:06:52 +00:00
; ;
*)
maybeOS_Support
; ;
esac
; ;
2018-04-02 10:07:58 +00:00
*)
2018-09-04 10:06:52 +00:00
noOS_Support
; ;
esac
2017-06-04 20:18:26 +00:00
2018-09-04 10:06:52 +00:00
echo " ${ PLAT } " > /tmp/DET_PLATFORM
2017-03-13 04:00:28 +00:00
}
2016-05-06 01:04:57 +00:00
2016-04-19 18:01:55 +00:00
####### FUNCTIONS ##########
spinner( )
{
local pid = $1
local delay = 0.50
local spinstr = '/-\|'
2016-11-10 15:51:19 +00:00
while [ " $( ps a | awk '{print $1}' | grep " ${ pid } " ) " ] ; do
2016-04-19 18:01:55 +00:00
local temp = ${ spinstr #? }
2016-11-10 15:51:19 +00:00
printf " [%c] " " ${ spinstr } "
local spinstr = ${ temp } ${ spinstr % " $temp " }
sleep ${ delay }
2016-04-19 18:01:55 +00:00
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
welcomeDialogs( ) {
# Display the welcome dialog
2016-11-22 18:23:51 +00:00
whiptail --msgbox --backtitle "Welcome" --title "PiVPN Automated Installer" "This installer will transform your Raspberry Pi into an OpenVPN server!" ${ r } ${ c }
2016-04-19 18:01:55 +00:00
# Explain the need for a static address
whiptail --msgbox --backtitle "Initiating network interface" --title "Static IP Needed" " The PiVPN is a SERVER so it needs a STATIC IP ADDRESS to function properly.
2016-09-27 12:24:14 +00:00
2016-11-22 18:23:51 +00:00
In the next section, you can choose to use your current network settings ( DHCP) or to manually edit them." ${ r } ${ c }
2016-04-19 18:01:55 +00:00
}
chooseUser( ) {
# Explain the local user
2016-11-22 18:23:51 +00:00
whiptail --msgbox --backtitle "Parsing User List" --title "Local Users" "Choose a local user that will hold your ovpn configurations." ${ r } ${ c }
2016-10-27 13:45:02 +00:00
# First, let's check if there is a user available.
2018-09-04 10:06:52 +00:00
numUsers = $( awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd)
2016-10-27 13:45:02 +00:00
if [ " $numUsers " -eq 0 ]
then
# We don't have a user, let's ask to add one.
2016-11-22 18:23:51 +00:00
if userToAdd = $( whiptail --title "Choose A User" --inputbox "No non-root user account was found. Please type a new username." ${ r } ${ c } 3>& 1 1>& 2 2>& 3)
2016-10-27 13:45:02 +00:00
then
# See http://askubuntu.com/a/667842/459815
2016-11-22 18:23:51 +00:00
PASSWORD = $( whiptail --title "password dialog" --passwordbox "Please enter the new user password" ${ r } ${ c } 3>& 1 1>& 2 2>& 3)
2016-12-05 19:55:30 +00:00
CRYPT = $( perl -e 'printf("%s\n", crypt($ARGV[0], "password"))' " ${ PASSWORD } " )
$SUDO useradd -m -p " ${ CRYPT } " -s /bin/bash " ${ userToAdd } "
if [ [ $? = 0 ] ] ; then
2016-10-27 13:45:02 +00:00
echo "Succeeded"
( ( numUsers += 1) )
else
exit 1
fi
else
exit 1
fi
fi
2018-09-04 10:06:52 +00:00
availableUsers = $( awk -F':' '$3>=1000 && $3<=60000 {print $1}' /etc/passwd)
2016-12-05 19:55:30 +00:00
local userArray = ( )
local firstloop = 1
2016-04-19 18:01:55 +00:00
while read -r line
do
mode = "OFF"
if [ [ $firstloop -eq 1 ] ] ; then
firstloop = 0
mode = "ON"
fi
2016-12-05 19:55:30 +00:00
userArray += ( " ${ line } " "" " ${ mode } " )
2016-11-22 18:23:51 +00:00
done <<< " ${ availableUsers } "
2017-04-17 13:49:52 +00:00
chooseUserCmd = ( whiptail --title "Choose A User" --separate-output --radiolist "Choose (press space to select):" ${ r } ${ c } ${ numUsers } )
2016-12-05 19:55:30 +00:00
chooseUserOptions = $( " ${ chooseUserCmd [@] } " " ${ userArray [@] } " 2>& 1 >/dev/tty)
if [ [ $? = 0 ] ] ; then
for desiredUser in ${ chooseUserOptions } ; do
2019-09-01 17:39:37 +00:00
INSTALL_USER = ${ desiredUser }
echo " ::: Using User: $INSTALL_USER "
echo " ${ INSTALL_USER } " > /tmp/INSTALL_USER
2016-04-19 18:01:55 +00:00
done
else
echo "::: Cancel selected, exiting...."
exit 1
fi
}
verifyFreeDiskSpace( ) {
2016-05-03 16:34:04 +00:00
# If user installs unattended-upgrades we'd need about 60MB so will check for 75MB free
2016-11-10 15:51:19 +00:00
echo "::: Verifying free disk space..."
local required_free_kilobytes = 76800
local existing_free_kilobytes = $( df -Pk | grep -m1 '\/$' | awk '{print $4}' )
# - Unknown free disk space , not a integer
if ! [ [ " ${ existing_free_kilobytes } " = ~ ^( [ 0-9] ) +$ ] ] ; then
echo "::: Unknown free disk space!"
echo "::: We were unable to determine available free disk space on this system."
echo "::: You may continue with the installation, however, it is not recommended."
read -r -p "::: If you are sure you want to continue, type YES and press enter :: " response
case $response in
[ Y] [ E] [ S] )
; ;
*)
echo "::: Confirmation not received, exiting..."
exit 1
2017-01-22 11:44:36 +00:00
; ;
2016-11-10 15:51:19 +00:00
esac
# - Insufficient free disk space
elif [ [ ${ existing_free_kilobytes } -lt ${ required_free_kilobytes } ] ] ; then
echo "::: Insufficient Disk Space!"
echo " ::: Your system appears to be low on disk space. PiVPN recommends a minimum of $required_free_kilobytes KiloBytes. "
echo " ::: You only have ${ existing_free_kilobytes } KiloBytes free. "
echo "::: If this is a new install on a Raspberry Pi you may need to expand your disk."
echo "::: Try running 'sudo raspi-config', and choose the 'expand file system option'"
echo "::: After rebooting, run this installation again. (curl -L https://install.pivpn.io | bash)"
2016-04-19 18:01:55 +00:00
echo "Insufficient free space, exiting..."
exit 1
fi
}
chooseInterface( ) {
# Turn the available interfaces into an array so it can be used with a whiptail dialog
2016-11-10 16:13:31 +00:00
local interfacesArray = ( )
# Number of available interfaces
local interfaceCount
# Whiptail variable storage
local chooseInterfaceCmd
# Temporary Whiptail options storage
local chooseInterfaceOptions
# Loop sentinel variable
2018-08-15 19:15:24 +00:00
local firstloop = 1
2016-04-19 18:01:55 +00:00
2017-01-27 20:42:42 +00:00
if [ [ $( echo " ${ availableInterfaces } " | wc -l) -eq 1 ] ] ; then
pivpnInterface = " ${ availableInterfaces } "
2017-01-27 22:46:34 +00:00
echo " ${ pivpnInterface } " > /tmp/pivpnINT
2017-01-27 20:42:42 +00:00
return
fi
2016-11-10 16:13:31 +00:00
while read -r line; do
2016-04-19 18:01:55 +00:00
mode = "OFF"
2016-11-10 16:13:31 +00:00
if [ [ ${ firstloop } -eq 1 ] ] ; then
2016-04-19 18:01:55 +00:00
firstloop = 0
mode = "ON"
fi
2016-11-10 16:13:31 +00:00
interfacesArray += ( " ${ line } " "available" " ${ mode } " )
2016-12-05 19:55:30 +00:00
done <<< " ${ availableInterfaces } "
2016-04-19 18:01:55 +00:00
# Find out how many interfaces are available to choose from
2016-11-10 16:13:31 +00:00
interfaceCount = $( echo " ${ availableInterfaces } " | wc -l)
2017-04-17 13:49:52 +00:00
chooseInterfaceCmd = ( whiptail --separate-output --radiolist "Choose An Interface (press space to select):" ${ r } ${ c } ${ interfaceCount } )
2016-11-10 16:13:31 +00:00
chooseInterfaceOptions = $( " ${ chooseInterfaceCmd [@] } " " ${ interfacesArray [@] } " 2>& 1 >/dev/tty)
if [ [ $? = 0 ] ] ; then
for desiredInterface in ${ chooseInterfaceOptions } ; do
pivpnInterface = ${ desiredInterface }
2016-04-19 18:01:55 +00:00
echo " ::: Using interface: $pivpnInterface "
echo " ${ pivpnInterface } " > /tmp/pivpnINT
done
else
echo "::: Cancel selected, exiting...."
exit 1
fi
}
2016-04-22 19:16:48 +00:00
avoidStaticIPv4Ubuntu( ) {
# If we are in Ubuntu then they need to have previously set their network, so just use what you have.
2016-05-25 21:58:08 +00:00
whiptail --msgbox --backtitle "IP Information" --title "IP Information" " Since we think you are not using Raspbian, we will not configure a static IP for you.
2016-11-22 18:23:51 +00:00
If you are in Amazon then you can not configure a static IP anyway. Just ensure before this installer started you had set an elastic IP on your instance." ${ r } ${ c }
2016-04-22 19:16:48 +00:00
}
2016-04-19 18:01:55 +00:00
getStaticIPv4Settings( ) {
2017-03-13 04:00:28 +00:00
local ipSettingsCorrect
2016-11-23 04:18:24 +00:00
# Grab their current DNS Server
IPv4dns = $( nslookup 127.0.0.1 | grep Server: | awk '{print $2}' )
2016-04-19 18:01:55 +00:00
# Ask if the user wants to use DHCP settings as their static IP
if ( whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno " Do you want to use your current network settings as a static address?
2016-11-10 17:18:17 +00:00
IP address: ${ IPv4addr }
Gateway: ${ IPv4gw } " ${ r } ${ c } ); then
2016-04-19 18:01:55 +00:00
# If they choose yes, let the user know that the IP address will not be available via DHCP and may cause a conflict.
whiptail --msgbox --backtitle "IP information" --title "FYI: IP Conflict" " It is possible your router could still try to assign this IP to a device, which would cause a conflict. But in most cases the router is smart enough to not do that.
If you are worried, either manually set the address, or modify the DHCP reservation pool so it does not include the IP you want.
2016-11-10 17:18:17 +00:00
It is also possible to use a DHCP reservation, but if you are going to do that, you might as well set a static address." ${ r } ${ c }
2016-04-19 18:01:55 +00:00
# Nothing else to do since the variables are already set above
else
# Otherwise, we need to ask the user to input their desired settings.
# Start by getting the IPv4 address (pre-filling it with info gathered from DHCP)
# Start a loop to let the user enter their information with the chance to go back and edit it if necessary
2016-11-10 17:18:17 +00:00
until [ [ ${ ipSettingsCorrect } = True ] ] ; do
2016-04-19 18:01:55 +00:00
# Ask for the IPv4 address
2016-11-10 17:18:17 +00:00
IPv4addr = $( whiptail --backtitle "Calibrating network interface" --title "IPv4 address" --inputbox "Enter your desired IPv4 address" ${ r } ${ c } " ${ IPv4addr } " 3>& 1 1>& 2 2>& 3)
if [ [ $? = 0 ] ] ; then
echo " ::: Your static IPv4 address: ${ IPv4addr } "
2016-04-19 18:01:55 +00:00
# Ask for the gateway
2016-11-10 17:18:17 +00:00
IPv4gw = $( whiptail --backtitle "Calibrating network interface" --title "IPv4 gateway (router)" --inputbox "Enter your desired IPv4 default gateway" ${ r } ${ c } " ${ IPv4gw } " 3>& 1 1>& 2 2>& 3)
if [ [ $? = 0 ] ] ; then
echo " ::: Your static IPv4 gateway: ${ IPv4gw } "
2016-04-19 18:01:55 +00:00
# Give the user a chance to review their settings before moving on
if ( whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno " Are these settings correct?
2016-11-10 17:18:17 +00:00
IP address: ${ IPv4addr }
Gateway: ${ IPv4gw } " ${ r } ${ c } ); then
# If the settings are correct, then we need to set the pivpnIP
2016-04-19 18:01:55 +00:00
echo " ${ IPv4addr %/* } " > /tmp/pivpnIP
echo " $pivpnInterface " > /tmp/pivpnINT
# After that's done, the loop ends and we move on
ipSettingsCorrect = True
else
# If the settings are wrong, the loop continues
ipSettingsCorrect = False
fi
else
# Cancelling gateway settings window
ipSettingsCorrect = False
echo "::: Cancel selected. Exiting..."
exit 1
fi
else
# Cancelling IPv4 settings window
ipSettingsCorrect = False
echo "::: Cancel selected. Exiting..."
exit 1
fi
done
# End the if statement for DHCP vs. static
fi
}
setDHCPCD( ) {
# Append these lines to dhcpcd.conf to enable a static IP
2016-12-07 18:03:32 +00:00
echo " interface ${ pivpnInterface }
2019-05-13 22:06:14 +00:00
static ip_address = ${ IPv4addr }
2016-11-10 17:18:17 +00:00
static routers = ${ IPv4gw }
2016-11-22 18:23:51 +00:00
static domain_name_servers = ${ IPv4dns } " | $SUDO tee -a ${ dhcpcdFile } >/dev/null
2016-04-19 18:01:55 +00:00
}
setStaticIPv4( ) {
# Tries to set the IPv4 address
2016-11-10 17:25:31 +00:00
if [ [ -f /etc/dhcpcd.conf ] ] ; then
if grep -q " ${ IPv4addr } " ${ dhcpcdFile } ; then
echo "::: Static IP already configured."
else
setDHCPCD
2019-05-13 22:06:14 +00:00
$SUDO ip addr replace dev " ${ pivpnInterface } " " ${ IPv4addr } "
2016-11-10 17:25:31 +00:00
echo ":::"
echo " ::: Setting IP to ${ IPv4addr } . You may need to restart after the install is complete. "
echo ":::"
fi
2016-04-19 18:01:55 +00:00
else
2016-11-10 17:25:31 +00:00
echo "::: Critical: Unable to locate configuration file to set static IPv4 address!"
exit 1
2016-04-19 18:01:55 +00:00
fi
}
function valid_ip( )
{
local ip = $1
local stat = 1
if [ [ $ip = ~ ^[ 0-9] { 1,3} \. [ 0-9] { 1,3} \. [ 0-9] { 1,3} \. [ 0-9] { 1,3} $ ] ] ; then
OIFS = $IFS
IFS = '.'
ip = ( $ip )
IFS = $OIFS
[ [ ${ ip [0] } -le 255 && ${ ip [1] } -le 255 \
&& ${ ip [2] } -le 255 && ${ ip [3] } -le 255 ] ]
stat = $?
fi
return $stat
}
2019-03-11 00:06:49 +00:00
#Call this function to use a regex to check user input for a valid custom domain
function valid_domain( )
{
local domain = $1
local stat = 1
2019-10-14 09:59:00 +00:00
if [ [ $domain = ~ ^( ( [ a-zA-Z0-9] { 1,63} | ( [ a-zA-Z0-9] { 1,60} [ -a-zA-Z0-9( ) ] { 0,2} [ a-zA-Z0-9] { 1,60} ) ) \. ) { 1,6} ( [ a-zA-Z] { 2,} ) $ ] ] ; then
2019-03-11 00:06:49 +00:00
stat = $?
fi
return $stat
}
2016-04-19 18:01:55 +00:00
installScripts( ) {
# Install the scripts from /etc/.pivpn to their various locations
$SUDO echo ":::"
$SUDO echo -n "::: Installing scripts to /opt/pivpn..."
if [ ! -d /opt/pivpn ] ; then
$SUDO mkdir /opt/pivpn
2019-09-01 17:39:37 +00:00
$SUDO chown " $INSTALL_USER " :root /opt/pivpn
2019-09-01 14:10:53 +00:00
$SUDO chmod 0755 /opt/pivpn
2016-04-19 18:01:55 +00:00
fi
2019-10-12 16:34:37 +00:00
$SUDO cp /etc/.pivpn/scripts/*.sh /opt/pivpn/
$SUDO chmod 0755 /opt/pivpn/*.sh
2016-04-19 18:01:55 +00:00
$SUDO cp /etc/.pivpn/pivpn /usr/local/bin/pivpn
$SUDO chmod 0755 /usr/local/bin/pivpn
$SUDO cp /etc/.pivpn/scripts/bash-completion /etc/bash_completion.d/pivpn
2016-04-21 13:37:27 +00:00
. /etc/bash_completion.d/pivpn
2017-01-28 18:58:10 +00:00
# Copy interface setting for debug
$SUDO cp /tmp/pivpnINT /etc/pivpn/pivpnINTERFACE
2016-04-19 18:01:55 +00:00
$SUDO echo " done."
}
2016-12-24 05:20:45 +00:00
package_check_install( ) {
dpkg-query -W -f= '${Status}' " ${ 1 } " 2>/dev/null | grep -c "ok installed" || ${ PKG_INSTALL } " ${ 1 } "
}
2018-04-02 10:07:58 +00:00
addSoftwareRepo( ) {
# Add the official OpenVPN repo for distros that don't have the latest version in their default repos
case ${ PLAT } in
Ubuntu| Debian| Devuan)
case ${ OSCN } in
trusty| xenial| wheezy| jessie)
wget -qO- https://swupdate.openvpn.net/repos/repo-public.gpg | $SUDO apt-key add -
echo " deb http://build.openvpn.net/debian/openvpn/stable $OSCN main " | $SUDO tee /etc/apt/sources.list.d/swupdate.openvpn.net.list > /dev/null
echo -n " ::: Adding OpenVPN repo for $PLAT $OSCN ... "
2019-09-03 08:09:48 +00:00
$SUDO $PKG_MANAGER -qq update & spinner $!
2018-04-02 10:07:58 +00:00
echo " done!"
; ;
esac
; ;
esac
}
2016-12-24 05:20:45 +00:00
update_package_cache( ) {
#Running apt-get update/upgrade with minimal output can cause some issues with
#requiring user input
#Check to see if apt-get update has already been run today
#it needs to have been run at least once on new installs!
timestamp = $( stat -c %Y ${ PKG_CACHE } )
timestampAsDate = $( date -d @" ${ timestamp } " "+%b %e" )
today = $( date "+%b %e" )
if [ ! " ${ today } " = = " ${ timestampAsDate } " ] ; then
#update package lists
echo ":::"
2019-09-02 11:35:54 +00:00
echo -ne " ::: ${ PKG_MANAGER } update has not been run today. Running now...\n "
2016-12-25 23:06:32 +00:00
$SUDO ${ UPDATE_PKG_CACHE } & > /dev/null
2016-12-24 05:20:45 +00:00
echo " done!"
fi
}
notify_package_updates_available( ) {
# Let user know if they have outdated packages on their system and
# advise them to run a package update at soonest possible.
echo ":::"
echo -n " ::: Checking ${ PKG_MANAGER } for upgraded packages.... "
updatesToInstall = $( eval " ${ PKG_COUNT } " )
echo " done!"
echo ":::"
if [ [ ${ updatesToInstall } -eq "0" ] ] ; then
echo "::: Your system is up to date! Continuing with PiVPN installation..."
else
echo " ::: There are ${ updatesToInstall } updates available for your system! "
echo "::: We recommend you update your OS after installing PiVPN! "
echo ":::"
fi
}
install_dependent_packages( ) {
2018-09-04 12:38:21 +00:00
# Install packages passed in via argument array
# No spinner - conflicts with set -e
declare -a argArray1 = ( " ${ !1 } " )
2017-01-22 11:44:36 +00:00
2019-07-23 20:12:35 +00:00
if [ [ ${ OSCN } = = "buster" ] ] ; then
$SUDO update-alternatives --set iptables /usr/sbin/iptables-legacy
$SUDO update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
fi
2018-09-04 12:38:21 +00:00
echo iptables-persistent iptables-persistent/autosave_v4 boolean true | $SUDO debconf-set-selections
echo iptables-persistent iptables-persistent/autosave_v6 boolean false | $SUDO debconf-set-selections
2016-12-24 05:20:45 +00:00
2019-03-03 09:18:45 +00:00
# Add support for https repositories if there are any that use it otherwise the installation will silently fail
if grep -q https /etc/apt/sources.list; then
PIVPN_DEPS += ( "apt-transport-https" )
fi
2018-09-04 12:38:21 +00:00
2019-03-03 09:18:45 +00:00
if command -v debconf-apt-progress & > /dev/null; then
$SUDO debconf-apt-progress -- ${ PKG_INSTALL } " ${ argArray1 [@] } "
2018-09-04 12:38:21 +00:00
else
for i in " ${ argArray1 [@] } " ; do
echo -n " ::: Checking for $i ... "
$SUDO package_check_install " ${ i } " & > /dev/null
echo " installed!"
done
fi
2016-12-10 05:58:36 +00:00
}
2016-05-03 14:32:17 +00:00
unattendedUpgrades( ) {
2016-11-22 18:23:51 +00:00
whiptail --msgbox --backtitle "Security Updates" --title "Unattended Upgrades" "Since this server will have at least one port open to the internet, it is recommended you enable unattended-upgrades.\nThis feature will check daily for security package updates only and apply them when necessary.\nIt will NOT automatically reboot the server so to fully apply some updates you should periodically reboot." ${ r } ${ c }
2016-05-03 14:32:17 +00:00
2016-11-22 18:23:51 +00:00
if ( whiptail --backtitle "Security Updates" --title "Unattended Upgrades" --yesno "Do you want to enable unattended upgrades of security patches to this server?" ${ r } ${ c } ) then
2016-05-03 14:32:17 +00:00
UNATTUPG = "unattended-upgrades"
else
UNATTUPG = ""
fi
}
2016-04-19 18:01:55 +00:00
stopServices( ) {
# Stop openvpn
$SUDO echo ":::"
2016-11-05 13:42:37 +00:00
$SUDO echo -n "::: Stopping OpenVPN service..."
2017-06-04 20:18:26 +00:00
case ${ PLAT } in
2017-06-06 22:27:23 +00:00
Ubuntu| Debian| *vuan)
2017-06-04 20:18:26 +00:00
$SUDO service openvpn stop || true
; ;
*)
$SUDO systemctl stop openvpn.service || true
; ;
esac
2016-04-19 18:01:55 +00:00
$SUDO echo " done."
}
getGitFiles( ) {
# Setup git repos for base files
echo ":::"
echo "::: Checking for existing base files..."
2016-11-18 22:22:36 +00:00
if is_repo " ${ 1 } " ; then
2019-07-23 20:12:35 +00:00
update_repo " ${ 1 } " " ${ 2 } "
2016-04-19 18:01:55 +00:00
else
2016-11-18 22:22:36 +00:00
make_repo " ${ 1 } " " ${ 2 } "
2016-04-19 18:01:55 +00:00
fi
}
is_repo( ) {
# If the directory does not have a .git folder it is not a repo
echo -n " ::: Checking $1 is a repo... "
2016-11-18 22:22:36 +00:00
cd " ${ 1 } " & > /dev/null || return 1
$SUDO git status & > /dev/null && echo " OK!" ; return 0 || echo " not found!" ; return 1
2016-04-19 18:01:55 +00:00
}
make_repo( ) {
# Remove the non-repos interface and clone the interface
echo -n " ::: Cloning $2 into $1 ... "
2016-11-18 22:22:36 +00:00
$SUDO rm -rf " ${ 1 } "
2018-09-23 18:43:51 +00:00
$SUDO git clone -q --depth 1 --no-single-branch " ${ 2 } " " ${ 1 } " > /dev/null & spinner $!
2016-10-25 18:11:32 +00:00
if [ -z " ${ TESTING +x } " ] ; then
2016-04-24 00:35:49 +00:00
:
else
2016-11-18 22:22:36 +00:00
$SUDO git -C " ${ 1 } " checkout test
2016-04-24 00:35:49 +00:00
fi
2016-04-19 18:01:55 +00:00
echo " done!"
}
update_repo( ) {
2017-03-13 04:00:28 +00:00
if [ [ " ${ reconfigure } " = = true ] ] ; then
echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
2016-04-23 22:18:48 +00:00
else
2017-03-13 04:00:28 +00:00
# Pull the latest commits
echo -n " ::: Updating repo in $1 ... "
2018-09-23 18:43:51 +00:00
$SUDO rm -rf " ${ 1 } "
2019-07-23 20:12:35 +00:00
# Go back to /etc otherwhise git will complain when the current working directory has
# just been deleted (/etc/.pivpn).
cd /etc
2018-09-23 18:43:51 +00:00
$SUDO git clone -q --depth 1 --no-single-branch " ${ 2 } " " ${ 1 } " > /dev/null & spinner $!
2017-03-13 04:00:28 +00:00
cd " ${ 1 } " || exit 1
if [ -z " ${ TESTING +x } " ] ; then
:
else
${ SUDOE } git checkout test
fi
echo " done!"
2016-04-23 22:18:48 +00:00
fi
2016-04-19 18:01:55 +00:00
}
2016-09-27 12:24:14 +00:00
setCustomProto( ) {
2016-09-27 12:31:41 +00:00
# Set the available protocols into an array so it can be used with a whiptail dialog
2016-10-25 18:11:32 +00:00
if protocol = $( whiptail --title "Protocol" --radiolist \
2017-04-17 13:49:52 +00:00
"Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP." ${ r } ${ c } 2 \
2016-10-09 10:30:13 +00:00
"UDP" "" ON \
"TCP" "" OFF 3>& 1 1>& 2 2>& 3)
2016-10-25 18:11:32 +00:00
then
2016-10-09 10:30:13 +00:00
# Convert option into lowercase (UDP->udp)
pivpnProto = " ${ protocol ,, } "
echo " ::: Using protocol: $pivpnProto "
echo " ${ pivpnProto } " > /tmp/pivpnPROTO
2016-09-27 12:24:14 +00:00
else
echo "::: Cancel selected, exiting...."
exit 1
fi
# write out the PROTO
PROTO = $pivpnProto
$SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
}
2016-04-28 02:13:00 +00:00
setCustomPort( ) {
until [ [ $PORTNumCorrect = True ] ]
do
portInvalid = "Invalid"
2016-10-25 18:11:32 +00:00
PROTO = $( cat /etc/pivpn/INSTALL_PROTO)
2016-10-09 11:40:19 +00:00
if [ " $PROTO " = "udp" ] ; then
DEFAULT_PORT = 1194
else
DEFAULT_PORT = 443
fi
2016-11-22 18:23:51 +00:00
if PORT = $( whiptail --title "Default OpenVPN Port" --inputbox "You can modify the default OpenVPN port. \nEnter a new value or hit 'Enter' to retain the default" ${ r } ${ c } $DEFAULT_PORT 3>& 1 1>& 2 2>& 3)
2016-10-25 18:11:32 +00:00
then
if [ [ " $PORT " = ~ ^[ 0-9] +$ ] ] && [ " $PORT " -ge 1 ] && [ " $PORT " -le 65535 ] ; then
2016-04-28 02:13:00 +00:00
:
else
PORT = $portInvalid
fi
else
echo "::: Cancel selected, exiting...."
exit 1
fi
if [ [ $PORT = = " $portInvalid " ] ] ; then
2016-11-22 18:23:51 +00:00
whiptail --msgbox --backtitle "Invalid Port" --title "Invalid Port" "You entered an invalid Port number.\n Please enter a number from 1 - 65535.\n If you are not sure, please just keep the default." ${ r } ${ c }
2016-04-28 02:13:00 +00:00
PORTNumCorrect = False
else
2016-11-22 18:23:51 +00:00
if ( whiptail --backtitle "Specify Custom Port" --title "Confirm Custom Port Number" --yesno " Are these settings correct?\n PORT: $PORT " ${ r } ${ c } ) then
2016-04-28 02:13:00 +00:00
PORTNumCorrect = True
else
# If the settings are wrong, the loop continues
PORTNumCorrect = False
fi
fi
done
2016-05-16 03:36:40 +00:00
# write out the port
echo ${ PORT } > /tmp/INSTALL_PORT
$SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
2016-04-28 02:13:00 +00:00
}
2016-04-23 22:18:48 +00:00
setClientDNS( ) {
2017-04-17 13:49:52 +00:00
DNSChoseCmd = ( whiptail --separate-output --radiolist "Select the DNS Provider for your VPN Clients (press space to select). To use your own, select Custom." ${ r } ${ c } 6)
2016-04-23 22:18:48 +00:00
DNSChooseOptions = ( Google "" on
OpenDNS "" off
Level3 "" off
2016-12-04 05:20:37 +00:00
DNS.WATCH "" off
2016-04-23 22:18:48 +00:00
Norton "" off
2018-04-02 10:07:58 +00:00
FamilyShield "" off
2018-05-29 21:19:16 +00:00
CloudFlare "" off
2016-04-23 22:18:48 +00:00
Custom "" off)
2016-10-25 18:11:32 +00:00
if DNSchoices = $( " ${ DNSChoseCmd [@] } " " ${ DNSChooseOptions [@] } " 2>& 1 >/dev/tty)
then
2018-04-02 10:07:58 +00:00
if [ [ ${ DNSchoices } != "Custom" ] ] ; then
echo " ::: Using ${ DNSchoices } servers. "
2018-05-29 21:17:55 +00:00
declare -A DNS_MAP = ( [ "Google" ] = "8.8.8.8 8.8.4.4" [ "OpenDNS" ] = "208.67.222.222 208.67.220.220" [ "Level3" ] = "209.244.0.3 209.244.0.4" [ "DNS.WATCH" ] = "84.200.69.80 84.200.70.40" [ "Norton" ] = "199.85.126.10 199.85.127.10" [ "FamilyShield" ] = "208.67.222.123 208.67.220.123" [ "CloudFlare" ] = "1.1.1.1 1.0.0.1" )
2018-04-02 10:07:58 +00:00
OVPNDNS1 = $( awk '{print $1}' <<< " ${ DNS_MAP [ " ${ DNSchoices } " ] } " )
OVPNDNS2 = $( awk '{print $2}' <<< " ${ DNS_MAP [ " ${ DNSchoices } " ] } " )
$SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1' ${ OVPNDNS1 } '\"/' /etc/openvpn/server.conf
$SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1' ${ OVPNDNS2 } '\"/' /etc/openvpn/server.conf
else
until [ [ $DNSSettingsCorrect = True ] ]
do
strInvalid = "Invalid"
2019-07-23 20:12:35 +00:00
if OVPNDNS = $( whiptail --backtitle "Specify Upstream DNS Provider(s)" --inputbox "Enter your desired upstream DNS provider(s), separated by a comma.\n\nFor example '8.8.8.8, 8.8.4.4'" ${ r } ${ c } "" 3>& 1 1>& 2 2>& 3)
2018-04-02 10:07:58 +00:00
then
2016-04-23 22:18:48 +00:00
OVPNDNS1 = $( echo " $OVPNDNS " | sed 's/[, \t]\+/,/g' | awk -F, '{print$1}' )
OVPNDNS2 = $( echo " $OVPNDNS " | sed 's/[, \t]\+/,/g' | awk -F, '{print$2}' )
if ! valid_ip " $OVPNDNS1 " || [ ! " $OVPNDNS1 " ] ; then
OVPNDNS1 = $strInvalid
fi
if ! valid_ip " $OVPNDNS2 " && [ " $OVPNDNS2 " ] ; then
OVPNDNS2 = $strInvalid
fi
2018-04-02 10:07:58 +00:00
else
2016-04-23 22:18:48 +00:00
echo "::: Cancel selected, exiting...."
exit 1
fi
2018-04-02 10:07:58 +00:00
if [ [ $OVPNDNS1 = = " $strInvalid " ] ] || [ [ $OVPNDNS2 = = " $strInvalid " ] ] ; then
2016-11-22 18:23:51 +00:00
whiptail --msgbox --backtitle "Invalid IP" --title "Invalid IP" " One or both entered IP addresses were invalid. Please try again.\n\n DNS Server 1: $OVPNDNS1 \n DNS Server 2: $OVPNDNS2 " ${ r } ${ c }
2016-04-23 22:18:48 +00:00
if [ [ $OVPNDNS1 = = " $strInvalid " ] ] ; then
OVPNDNS1 = ""
fi
if [ [ $OVPNDNS2 = = " $strInvalid " ] ] ; then
OVPNDNS2 = ""
fi
DNSSettingsCorrect = False
2018-04-02 10:07:58 +00:00
else
2016-11-22 18:23:51 +00:00
if ( whiptail --backtitle "Specify Upstream DNS Provider(s)" --title "Upstream DNS Provider(s)" --yesno " Are these settings correct?\n DNS Server 1: $OVPNDNS1 \n DNS Server 2: $OVPNDNS2 " ${ r } ${ c } ) then
2016-04-23 22:18:48 +00:00
DNSSettingsCorrect = True
2016-04-29 23:18:05 +00:00
$SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1' ${ OVPNDNS1 } '\"/' /etc/openvpn/server.conf
2016-11-09 20:02:40 +00:00
if [ -z ${ OVPNDNS2 } ] ; then
$SUDO sed -i '/\(dhcp-option DNS \)/{n;N;d}' /etc/openvpn/server.conf
else
$SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1' ${ OVPNDNS2 } '\"/' /etc/openvpn/server.conf
fi
2016-04-23 22:18:48 +00:00
else
# If the settings are wrong, the loop continues
DNSSettingsCorrect = False
fi
fi
2018-04-02 10:07:58 +00:00
done
fi
2016-04-23 22:18:48 +00:00
else
2018-04-02 10:07:58 +00:00
echo "::: Cancel selected. Exiting..."
exit 1
2016-04-23 22:18:48 +00:00
fi
}
2019-03-10 17:55:14 +00:00
#This procedure allows a user to specify a custom search domain if they have one.
setCustomDomain( ) {
DomainSettingsCorrect = False
2019-03-11 02:35:18 +00:00
if ( whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Would you like to add a custom search domain? \n (This is only for advanced users who have their own domain)\n" ${ r } ${ c } ) ; then
2019-03-10 17:55:14 +00:00
until [ [ $DomainSettingsCorrect = True ] ]
do
2019-03-11 02:35:18 +00:00
if CUSTOMDomain = $( whiptail --inputbox "Enter Custom Domain\nFormat: mydomain.com" ${ r } ${ c } --title "Custom Domain" 3>& 1 1>& 2 2>& 3) ; then
2019-03-11 02:33:20 +00:00
if valid_domain " $CUSTOMDomain " ; then
2019-03-11 02:35:18 +00:00
if ( whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno " Are these settings correct?\n Custom Search Domain: $CUSTOMDomain " ${ r } ${ c } ) ; then
2019-03-11 02:33:20 +00:00
DomainSettingsCorrect = True
2019-03-10 17:55:14 +00:00
2019-03-11 15:22:31 +00:00
$SUDO sed -i '0,/\(.*dhcp-option.*\)/s//\push "dhcp-option DOMAIN ' ${ CUSTOMDomain } '" \n&/' /etc/openvpn/server.conf
2019-03-10 17:55:14 +00:00
2019-03-11 02:33:20 +00:00
else
# If the settings are wrong, the loop continues
DomainSettingsCorrect = False
fi
2019-03-10 17:55:14 +00:00
else
2019-03-11 02:35:18 +00:00
whiptail --msgbox --backtitle "Invalid Domain" --title "Invalid Domain" " Domain is invalid. Please try again.\n\n DOMAIN: $CUSTOMDomain \n " ${ r } ${ c }
2019-03-11 02:33:20 +00:00
DomainSettingsCorrect = False
2019-03-10 17:55:14 +00:00
fi
else
echo "::: Cancel selected. Exiting..."
exit 1
fi
done
else
echo sleep 0.1
fi
}
2016-04-23 22:18:48 +00:00
confOpenVPN( ) {
2019-06-19 22:48:46 +00:00
# Grab the existing Hostname
2019-10-12 16:34:37 +00:00
host_name = $( hostname -s)
# Generate a random UUID for this server so that we can use verify-x509-name later that is unique for this server installation.
NEW_UUID = $( </proc/sys/kernel/random/uuid)
# Create a unique server name using the host name and UUID
SERVER_NAME = " ${ host_name } _ ${ NEW_UUID } "
2017-06-04 20:18:26 +00:00
2019-07-23 20:12:35 +00:00
declare -A ECDSA_MAP = ( [ "256" ] = "prime256v1" [ "384" ] = "secp384r1" [ "521" ] = "secp521r1" )
2017-06-04 20:18:26 +00:00
2017-03-13 06:44:29 +00:00
if [ [ ${ useUpdateVars } = = false ] ] ; then
# Ask user for desired level of encryption
2018-09-04 10:06:52 +00:00
if [ [ ${ useUpdateVars } = = false ] ] ; then
2019-08-08 08:34:17 +00:00
2019-06-26 08:35:56 +00:00
if [ [ ${ PLAT } = = "Raspbian" ] ] && [ [ ${ OSCN } != "stretch" ] ] && [ [ ${ OSCN } != "buster" ] ] ; then
2019-08-08 08:34:17 +00:00
2018-09-04 10:06:52 +00:00
APPLY_TWO_POINT_FOUR = false
else
2018-09-04 12:38:21 +00:00
if ( whiptail --backtitle "Setup OpenVPN" --title "Installation mode" --yesno "OpenVPN 2.4 brings support for stronger authentication and key exchange using Elliptic Curves, along with encrypted control channel.\n\nIf your clients do run OpenVPN 2.4 or later you can enable these features, otherwise choose 'No' for best compatibility.\n\nNOTE: Current mobile app, that is OpenVPN connect, is supported." ${ r } ${ c } ) ; then
2018-09-04 10:06:52 +00:00
APPLY_TWO_POINT_FOUR = true
$SUDO touch /etc/pivpn/TWO_POINT_FOUR
else
APPLY_TWO_POINT_FOUR = false
fi
fi
fi
if [ [ ${ runUnattended } = = true ] ] && [ [ ${ APPLY_TWO_POINT_FOUR } = = true ] ] ; then
$SUDO touch /etc/pivpn/TWO_POINT_FOUR
fi
if [ [ ${ APPLY_TWO_POINT_FOUR } = = false ] ] ; then
2019-10-19 04:00:00 +00:00
## creates a file to determine if the OpenVPN setup is compatible with the OVPN12 format for iOS keychain integration.
echo "true" > /etc/pivpn/OVPN12_Compatible
ENCRYPT = $( whiptail --backtitle "Setup OpenVPN" --title "RSA certificate size" --radiolist \
2019-03-03 09:18:45 +00:00
"Choose the desired size of your certificate (press space to select):\n This is a certificate that will be generated on your system. The larger the certificate, the more time this will take. For most applications, it is recommended to use 2048 bits. If you are testing, you can use 1024 bits to speed things up, but do not use this for normal use! If you are paranoid about ... things... then grab a cup of joe and pick 4096 bits." ${ r } ${ c } 3 \
"1024" "Use a 1024-bit certificate (testing only)" OFF \
"2048" "Use a 2048-bit certificate (recommended level)" ON \
"4096" "Use a 4096-bit certificate (paranoid level)" OFF 3>& 1 1>& 2 2>& 3)
2018-09-04 10:06:52 +00:00
else
2019-03-03 09:18:45 +00:00
ENCRYPT = $( whiptail --backtitle "Setup OpenVPN" --title "ECDSA certificate size" --radiolist \
2019-07-23 20:12:35 +00:00
"Choose the desired size of your certificate (press space to select):\n This is a certificate that will be generated on your system. The larger the certificate, the more time this will take. For most applications, it is recommended to use 256 bits. You can increase the number of bits if you care about, however, consider that 256 bits are already as secure as 3072 bit RSA." ${ r } ${ c } 3 \
2019-03-03 09:18:45 +00:00
"256" "Use a 256-bit certificate (recommended level)" ON \
"384" "Use a 384-bit certificate" OFF \
"521" "Use a 521-bit certificate (paranoid level)" OFF 3>& 1 1>& 2 2>& 3)
2018-09-04 10:06:52 +00:00
fi
2017-06-04 20:18:26 +00:00
2017-03-13 06:44:29 +00:00
exitstatus = $?
if [ $exitstatus != 0 ] ; then
echo "::: Cancel selected. Exiting..."
exit 1
fi
2016-04-19 18:01:55 +00:00
fi
2016-04-19 23:06:44 +00:00
2016-12-04 05:20:37 +00:00
# If easy-rsa exists, remove it
if [ [ -d /etc/openvpn/easy-rsa/ ] ] ; then
$SUDO rm -rf /etc/openvpn/easy-rsa/
fi
2016-04-19 23:06:44 +00:00
2019-05-13 23:15:34 +00:00
2019-03-03 09:18:45 +00:00
# Get easy-rsa
wget -q -O - " ${ easyrsaRel } " | $SUDO tar xz -C /etc/openvpn && $SUDO mv /etc/openvpn/EasyRSA-v${ easyrsaVer } /etc/openvpn/easy-rsa
2016-12-08 19:09:30 +00:00
# fix ownership
2016-12-04 05:20:37 +00:00
$SUDO chown -R root:root /etc/openvpn/easy-rsa
$SUDO mkdir /etc/openvpn/easy-rsa/pki
2018-09-04 10:06:52 +00:00
cd /etc/openvpn/easy-rsa || exit
2016-12-04 05:20:37 +00:00
# Write out new vars file
2017-03-14 02:39:53 +00:00
set +e
2016-12-04 05:20:37 +00:00
IFS = read -d '' String <<"EOF"
if [ -z " $EASYRSA_CALLER " ] ; then
echo "Nope." >& 2
return 1
fi
set_var EASYRSA "/etc/openvpn/easy-rsa"
set_var EASYRSA_PKI " $EASYRSA /pki "
2018-09-04 10:06:52 +00:00
set_var EASYRSA_CRL_DAYS 3650
2016-12-04 05:20:37 +00:00
EOF
2018-09-04 10:06:52 +00:00
echo " ${ String } " | $SUDO tee vars >/dev/null
2017-03-14 02:39:53 +00:00
set -e
2016-04-21 00:20:52 +00:00
2018-09-04 10:06:52 +00:00
# Set certificate type
if [ [ ${ APPLY_TWO_POINT_FOUR } = = false ] ] ; then
2018-09-04 12:38:21 +00:00
echo "set_var EASYRSA_ALGO rsa" | $SUDO tee -a vars
echo " set_var EASYRSA_KEY_SIZE ${ ENCRYPT } " | $SUDO tee -a vars
2018-09-04 10:06:52 +00:00
else
2018-09-04 12:38:21 +00:00
echo "set_var EASYRSA_ALGO ec" | $SUDO tee -a vars
echo " set_var EASYRSA_CURVE ${ ECDSA_MAP [ " ${ ENCRYPT } " ] } " | $SUDO tee -a vars
2018-09-04 10:06:52 +00:00
fi
2016-04-19 18:01:55 +00:00
# Remove any previous keys
2016-12-05 17:13:09 +00:00
${ SUDOE } ./easyrsa --batch init-pki
2016-04-19 18:01:55 +00:00
# Build the certificate authority
2016-12-04 05:20:37 +00:00
printf "::: Building CA...\n"
${ SUDOE } ./easyrsa --batch build-ca nopass
2016-04-21 00:48:52 +00:00
printf "\n::: CA Complete.\n"
2016-04-19 18:01:55 +00:00
2017-03-13 06:44:29 +00:00
if [ [ ${ useUpdateVars } = = false ] ] ; then
2018-09-04 10:06:52 +00:00
if [ [ ${ APPLY_TWO_POINT_FOUR } = = false ] ] ; then
2019-03-03 09:18:45 +00:00
whiptail --msgbox --backtitle "Setup OpenVPN" --title "Server Information" "The server key, Diffie-Hellman parameters, and HMAC key will now be generated." ${ r } ${ c }
2018-09-04 10:06:52 +00:00
fi
2017-03-13 06:44:29 +00:00
fi
2017-06-04 20:18:26 +00:00
2016-04-19 18:01:55 +00:00
# Build the server
2019-07-23 20:12:35 +00:00
EASYRSA_CERT_EXPIRE = 3650 ${ SUDOE } ./easyrsa build-server-full ${ SERVER_NAME } nopass
2016-04-19 18:01:55 +00:00
2018-02-15 09:14:03 +00:00
if [ [ ${ useUpdateVars } = = false ] ] ; then
2018-04-02 10:07:58 +00:00
if [ [ ${ APPLY_TWO_POINT_FOUR } = = false ] ] ; then
2018-07-18 21:01:36 +00:00
if ( [ " $ENCRYPT " -ge "4096" ] && whiptail --backtitle "Setup OpenVPN" --title "Download Diffie-Hellman Parameters" --yesno --defaultno " Download Diffie-Hellman parameters from a public DH parameter generation service?\n\nGenerating DH parameters for a $ENCRYPT -bit key can take many hours on a Raspberry Pi. You can instead download DH parameters from \"2 Ton Digital\" that are generated at regular intervals as part of a public service. Downloaded DH parameters will be randomly selected from their database.\nMore information about this service can be found here: https://2ton.com.au/safeprimes/\n\nIf you're paranoid, choose 'No' and Diffie-Hellman parameters will be generated on your device. " ${ r } ${ c } ) ; then
2018-04-02 10:07:58 +00:00
DOWNLOAD_DH_PARAM = true
else
DOWNLOAD_DH_PARAM = false
fi
2018-09-04 10:06:52 +00:00
else
DOWNLOAD_DH_PARAM = false
2018-04-02 10:07:58 +00:00
fi
2017-03-13 04:00:28 +00:00
fi
2016-04-19 18:01:55 +00:00
2018-04-02 10:07:58 +00:00
if [ [ ${ APPLY_TWO_POINT_FOUR } = = false ] ] ; then
if [ " $ENCRYPT " -ge "4096" ] && [ [ ${ DOWNLOAD_DH_PARAM } = = true ] ] ; then
# Downloading parameters
2018-07-18 21:01:36 +00:00
${ SUDOE } curl " https://2ton.com.au/getprimes/random/dhparam/ ${ ENCRYPT } " -o " /etc/openvpn/easy-rsa/pki/dh ${ ENCRYPT } .pem "
2018-04-02 10:07:58 +00:00
else
# Generate Diffie-Hellman key exchange
${ SUDOE } ./easyrsa gen-dh
${ SUDOE } mv pki/dh.pem pki/dh${ ENCRYPT } .pem
fi
fi
2018-02-15 09:14:03 +00:00
2016-04-19 18:01:55 +00:00
# Generate static HMAC key to defend against DDoS
2016-12-04 05:20:37 +00:00
${ SUDOE } openvpn --genkey --secret pki/ta.key
2016-04-19 18:01:55 +00:00
2017-09-22 07:46:52 +00:00
# Generate an empty Certificate Revocation List
${ SUDOE } ./easyrsa gen-crl
2017-09-23 19:35:46 +00:00
${ SUDOE } cp pki/crl.pem /etc/openvpn/crl.pem
${ SUDOE } chown nobody:nogroup /etc/openvpn/crl.pem
2017-09-22 07:46:52 +00:00
2018-09-04 10:06:52 +00:00
# Write config file for server using the template.txt file
2016-04-21 16:28:48 +00:00
$SUDO cp /etc/.pivpn/server_config.txt /etc/openvpn/server.conf
2016-09-27 12:24:14 +00:00
2018-04-02 10:07:58 +00:00
if [ [ ${ APPLY_TWO_POINT_FOUR } = = true ] ] ; then
2018-09-04 10:06:52 +00:00
#If they enabled 2.4 use tls-crypt instead of tls-auth to encrypt control channel
2018-04-02 10:07:58 +00:00
$SUDO sed -i "s/tls-auth \/etc\/openvpn\/easy-rsa\/pki\/ta.key 0/tls-crypt \/etc\/openvpn\/easy-rsa\/pki\/ta.key/" /etc/openvpn/server.conf
fi
2018-02-15 09:14:03 +00:00
2018-04-02 10:07:58 +00:00
if [ [ ${ APPLY_TWO_POINT_FOUR } = = true ] ] ; then
2018-09-04 10:06:52 +00:00
#If they enabled 2.4 disable dh parameters since the key exchange will use the matching curve from the ECDSA certificate
$SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/dh none/" /etc/openvpn/server.conf
2018-04-02 10:07:58 +00:00
else
# Otherwise set the user encryption key size
$SUDO sed -i " s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/\1 ${ ENCRYPT } .pem/ " /etc/openvpn/server.conf
fi
2016-09-27 12:24:14 +00:00
2016-04-20 15:16:50 +00:00
# if they modified port put value in server.conf
if [ $PORT != 1194 ] ; then
2016-04-21 16:28:48 +00:00
$SUDO sed -i " s/1194/ ${ PORT } /g " /etc/openvpn/server.conf
2016-04-20 15:16:50 +00:00
fi
2016-04-21 02:43:16 +00:00
2016-09-27 12:31:41 +00:00
# if they modified protocol put value in server.conf
2016-10-25 18:11:32 +00:00
if [ " $PROTO " != "udp" ] ; then
2016-09-27 12:24:14 +00:00
$SUDO sed -i "s/proto udp/proto tcp/g" /etc/openvpn/server.conf
fi
2016-04-21 02:43:16 +00:00
# write out server certs to conf file
2016-12-05 20:54:54 +00:00
$SUDO sed -i " s/\(key \/etc\/openvpn\/easy-rsa\/pki\/private\/\).*/\1 ${ SERVER_NAME } .key/ " /etc/openvpn/server.conf
$SUDO sed -i " s/\(cert \/etc\/openvpn\/easy-rsa\/pki\/issued\/\).*/\1 ${ SERVER_NAME } .crt/ " /etc/openvpn/server.conf
2016-04-19 18:01:55 +00:00
}
2016-05-03 14:32:17 +00:00
confUnattendedUpgrades( ) {
2018-09-04 10:06:52 +00:00
cd /etc/apt/apt.conf.d
2016-05-03 14:32:17 +00:00
if [ [ $UNATTUPG = = "unattended-upgrades" ] ] ; then
2019-09-03 08:09:48 +00:00
$SUDO $PKG_MANAGER --yes --quiet --no-install-recommends install " $UNATTUPG " > /dev/null & spinner $!
2016-05-10 15:49:29 +00:00
if [ [ $PLAT = = "Ubuntu" ] ] ; then
2016-05-03 14:32:17 +00:00
# Ubuntu 50unattended-upgrades should already just have security enabled
# so we just need to configure the 10periodic file
2018-09-04 10:06:52 +00:00
cat << EOT | $SUDO tee 10periodic >/dev/null
2016-05-03 14:32:17 +00:00
APT::Periodic::Update-Package-Lists "1" ;
APT::Periodic::Download-Upgradeable-Packages "1" ;
APT::Periodic::AutocleanInterval "5" ;
APT::Periodic::Unattended-Upgrade "1" ;
EOT
else
2018-09-04 12:38:21 +00:00
# Fix Raspbian config
if [ [ $PLAT = = "Raspbian" ] ] ; then
wget -q -O - " $UNATTUPG_CONFIG " | $SUDO tar xz
2019-03-03 09:18:45 +00:00
$SUDO cp " unattended-upgrades- $UNATTUPG_RELEASE /data/50unattended-upgrades.Raspbian " 50unattended-upgrades
$SUDO rm -rf " unattended-upgrades- $UNATTUPG_RELEASE "
2018-09-04 12:38:21 +00:00
fi
# Add the remaining settings for all other distributions
2018-09-04 10:06:52 +00:00
cat << EOT | $SUDO tee 02periodic >/dev/null
2016-05-03 14:32:17 +00:00
APT::Periodic::Enable "1" ;
APT::Periodic::Update-Package-Lists "1" ;
APT::Periodic::Download-Upgradeable-Packages "1" ;
APT::Periodic::Unattended-Upgrade "1" ;
APT::Periodic::AutocleanInterval "7" ;
APT::Periodic::Verbose "0" ;
EOT
fi
fi
}
2016-04-19 18:01:55 +00:00
confNetwork( ) {
# Enable forwarding of internet traffic
2016-04-20 19:36:27 +00:00
$SUDO sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
2016-04-19 18:01:55 +00:00
$SUDO sysctl -p
2019-03-03 09:18:45 +00:00
# if ufw enabled, configure that (running as root because sometimes the executable is not in the user's $PATH, on Debian for example)
if $SUDO bash -c 'hash ufw' 2>/dev/null; then
2018-04-02 10:07:58 +00:00
if LANG = en_US.UTF-8 $SUDO ufw status | grep -q inactive
2016-10-25 18:11:32 +00:00
then
2016-05-15 02:42:39 +00:00
noUFW = 1
else
echo "::: Detected UFW is enabled."
echo "::: Adding UFW rules..."
2019-03-03 09:18:45 +00:00
$SUDO sed " /delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s 10.8.0.0/24 -o $IPv4dev -j MASQUERADE\nCOMMIT\n " -i /etc/ufw/before.rules
# Insert rules at the beginning of the chain (in case there are other rules that may drop the traffic)
$SUDO ufw insert 1 allow " $PORT " /" $PROTO " >/dev/null
2019-08-06 07:53:14 +00:00
# https://askubuntu.com/a/712202
INSTALLED_UFW = $( dpkg-query --showformat= '${Version}' --show ufw)
MINIMUM_UFW = 0.34
if $SUDO dpkg --compare-versions " $INSTALLED_UFW " ge " $MINIMUM_UFW " ; then
# Don't forward everything, just the traffic originated from the VPN subnet
$SUDO ufw route insert 1 allow in on tun0 from 10.8.0.0/24 out on " $IPv4dev " to any >/dev/null
echo 0 > /tmp/OLD_UFW
else
# This ufw version does not support route command, fallback to policy change
$SUDO sed -i "s/\(DEFAULT_FORWARD_POLICY=\).*/\1\"ACCEPT\"/" /etc/default/ufw
echo 1 > /tmp/OLD_UFW
fi
2019-03-03 09:18:45 +00:00
$SUDO ufw reload >/dev/null
2016-05-15 02:42:39 +00:00
echo "::: UFW configuration completed."
fi
2016-04-22 19:16:48 +00:00
else
2016-05-15 02:42:39 +00:00
noUFW = 1
fi
# else configure iptables
if [ [ $noUFW -eq 1 ] ] ; then
2016-05-15 20:57:42 +00:00
echo 1 > /tmp/noUFW
2019-07-23 20:12:35 +00:00
# Now some checks to detect which rules we need to add. On a newly installed system all policies
# should be ACCEPT, so the only required rule would be the MASQUERADE one.
2019-03-03 09:18:45 +00:00
$SUDO iptables -t nat -I POSTROUTING -s 10.8.0.0/24 -o " $IPv4dev " -j MASQUERADE
2019-07-23 20:12:35 +00:00
# Count how many rules are in the INPUT and FORWARD chain. When parsing input from
# iptables -S, '^-P' skips the policies and 'ufw-' skips ufw chains (in case ufw was found
# installed but not enabled).
# Grep returns non 0 exit code where there are no matches, however that would make the script exit,
# for this reasons we use '|| true' to force exit code 0
INPUT_RULES_COUNT = " $( $SUDO iptables -S INPUT | grep -vcE '(^-P|ufw-)' || true ) "
FORWARD_RULES_COUNT = " $( $SUDO iptables -S FORWARD | grep -vcE '(^-P|ufw-)' || true ) "
INPUT_POLICY = " $( $SUDO iptables -S INPUT | grep '^-P' | awk '{print $3}' ) "
FORWARD_POLICY = " $( $SUDO iptables -S FORWARD | grep '^-P' | awk '{print $3}' ) "
# If rules count is not zero, we assume we need to explicitly allow traffic. Same conclusion if
# there are no rules and the policy is not ACCEPT. Note that rules are being added to the top of the
# chain (using -I).
if [ " $INPUT_RULES_COUNT " -ne 0 ] || [ " $INPUT_POLICY " != "ACCEPT" ] ; then
$SUDO iptables -I INPUT 1 -i " $IPv4dev " -p " $PROTO " --dport " $PORT " -j ACCEPT
INPUT_CHAIN_EDITED = 1
else
INPUT_CHAIN_EDITED = 0
fi
if [ " $FORWARD_RULES_COUNT " -ne 0 ] || [ " $FORWARD_POLICY " != "ACCEPT" ] ; then
$SUDO iptables -I FORWARD 1 -d 10.8.0.0/24 -i " $IPv4dev " -o tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
$SUDO iptables -I FORWARD 2 -s 10.8.0.0/24 -i tun0 -o " $IPv4dev " -j ACCEPT
FORWARD_CHAIN_EDITED = 1
else
FORWARD_CHAIN_EDITED = 0
fi
2017-06-04 20:18:26 +00:00
case ${ PLAT } in
Ubuntu| Debian| Devuan)
$SUDO iptables-save | $SUDO tee /etc/iptables/rules.v4 > /dev/null
; ;
*)
$SUDO netfilter-persistent save
; ;
esac
2016-05-15 20:57:42 +00:00
else
echo 0 > /tmp/noUFW
2016-04-22 19:16:48 +00:00
fi
2016-09-27 12:24:14 +00:00
2019-07-23 20:12:35 +00:00
echo " $INPUT_CHAIN_EDITED " > /tmp/INPUT_CHAIN_EDITED
echo " $FORWARD_CHAIN_EDITED " > /tmp/FORWARD_CHAIN_EDITED
2016-05-15 20:57:42 +00:00
$SUDO cp /tmp/noUFW /etc/pivpn/NO_UFW
2019-08-06 07:53:14 +00:00
$SUDO cp /tmp/OLD_UFW /etc/pivpn/OLD_UFW
2019-07-23 20:12:35 +00:00
$SUDO cp /tmp/INPUT_CHAIN_EDITED /etc/pivpn/INPUT_CHAIN_EDITED
$SUDO cp /tmp/FORWARD_CHAIN_EDITED /etc/pivpn/FORWARD_CHAIN_EDITED
2016-04-19 18:01:55 +00:00
}
confOVPN( ) {
2018-10-12 19:06:11 +00:00
if ! IPv4pub = $( dig +short myip.opendns.com @208.67.222.222) || ! valid_ip " $IPv4pub " ; then
echo "dig failed, now trying to curl checkip.amazonaws.com"
if ! IPv4pub = $( curl -s https://checkip.amazonaws.com) || ! valid_ip " $IPv4pub " ; then
echo "checkip.amazonaws.com failed, please check your internet connection/DNS"
exit 1
2016-10-09 16:27:45 +00:00
fi
fi
2019-09-01 17:39:37 +00:00
$SUDO cp /tmp/INSTALL_USER /etc/pivpn/INSTALL_USER
2016-05-06 01:04:57 +00:00
$SUDO cp /tmp/DET_PLATFORM /etc/pivpn/DET_PLATFORM
2016-04-19 18:01:55 +00:00
2016-12-05 19:55:30 +00:00
$SUDO cp /etc/.pivpn/Default.txt /etc/openvpn/easy-rsa/pki/Default.txt
2016-04-21 16:50:15 +00:00
2018-04-02 10:07:58 +00:00
if [ [ ${ APPLY_TWO_POINT_FOUR } = = true ] ] ; then
2018-09-04 10:06:52 +00:00
#If they enabled 2.4 remove key-direction options since it's not required
2018-04-02 10:07:58 +00:00
$SUDO sed -i "/key-direction 1/d" /etc/openvpn/easy-rsa/pki/Default.txt
fi
2018-02-15 09:14:03 +00:00
2017-03-13 06:44:29 +00:00
if [ [ ${ useUpdateVars } = = false ] ] ; then
2017-04-17 13:49:52 +00:00
METH = $( whiptail --title "Public IP or DNS" --radiolist "Will clients use a Public IP or DNS Name to connect to your server (press space to select)?" ${ r } ${ c } 2 \
2017-03-13 06:44:29 +00:00
" $IPv4pub " "Use this public IP" "ON" \
"DNS Entry" "Use a public DNS" "OFF" 3>& 1 1>& 2 2>& 3)
2017-06-04 20:18:26 +00:00
2017-03-13 06:44:29 +00:00
exitstatus = $?
if [ $exitstatus != 0 ] ; then
2016-04-30 20:00:02 +00:00
echo "::: Cancel selected. Exiting..."
2016-04-19 18:01:55 +00:00
exit 1
2017-03-13 06:44:29 +00:00
fi
2017-06-04 20:18:26 +00:00
2017-03-13 06:44:29 +00:00
if [ " $METH " = = " $IPv4pub " ] ; then
$SUDO sed -i 's/IPv4pub/' " $IPv4pub " '/' /etc/openvpn/easy-rsa/pki/Default.txt
else
until [ [ $publicDNSCorrect = True ] ]
do
PUBLICDNS = $( whiptail --title "PiVPN Setup" --inputbox "What is the public DNS name of this Server?" ${ r } ${ c } 3>& 1 1>& 2 2>& 3)
exitstatus = $?
if [ $exitstatus != 0 ] ; then
echo "::: Cancel selected. Exiting..."
exit 1
fi
if ( whiptail --backtitle "Confirm DNS Name" --title "Confirm DNS Name" --yesno " Is this correct?\n\n Public DNS Name: $PUBLICDNS " ${ r } ${ c } ) then
publicDNSCorrect = True
$SUDO sed -i 's/IPv4pub/' " $PUBLICDNS " '/' /etc/openvpn/easy-rsa/pki/Default.txt
else
publicDNSCorrect = False
fi
done
fi
else
$SUDO sed -i 's/IPv4pub/' " $PUBLICDNS " '/' /etc/openvpn/easy-rsa/pki/Default.txt
2016-04-19 18:01:55 +00:00
fi
2016-09-27 12:24:14 +00:00
2016-04-20 15:16:50 +00:00
# if they modified port put value in Default.txt for clients to use
if [ $PORT != 1194 ] ; then
2016-12-05 19:55:30 +00:00
$SUDO sed -i -e " s/1194/ ${ PORT } /g " /etc/openvpn/easy-rsa/pki/Default.txt
2016-04-20 15:16:50 +00:00
fi
2016-09-27 12:24:14 +00:00
2016-09-27 12:31:41 +00:00
# if they modified protocol put value in Default.txt for clients to use
2016-10-25 18:11:32 +00:00
if [ " $PROTO " != "udp" ] ; then
2016-12-05 19:55:30 +00:00
$SUDO sed -i -e "s/proto udp/proto tcp/g" /etc/openvpn/easy-rsa/pki/Default.txt
2016-09-27 12:24:14 +00:00
fi
2016-05-07 16:33:52 +00:00
# verify server name to strengthen security
2016-12-05 19:55:30 +00:00
$SUDO sed -i " s/SRVRNAME/ ${ SERVER_NAME } / " /etc/openvpn/easy-rsa/pki/Default.txt
2016-05-07 16:33:52 +00:00
2019-09-01 15:39:38 +00:00
INSTALL_HOME = $( grep -m1 " ^ ${ INSTALL_USER } : " /etc/passwd | cut -d: -f6)
INSTALL_HOME = ${ INSTALL_HOME %/ } # remove possible trailing slash
2019-09-01 17:39:37 +00:00
if [ ! -d " $INSTALL_HOME /ovpns " ] ; then
2019-08-13 16:23:08 +00:00
$SUDO mkdir " $INSTALL_HOME /ovpns "
2017-03-22 00:47:55 +00:00
fi
2019-09-01 17:39:37 +00:00
$SUDO chmod 0750 " $INSTALL_HOME /ovpns "
2019-09-02 11:35:54 +00:00
$SUDO chown $INSTALL_USER :$INSTALL_USER " $INSTALL_HOME /ovpns "
2016-04-19 18:01:55 +00:00
}
2018-04-02 10:07:58 +00:00
confLogging( ) {
echo " if \$programname == 'ovpn-server' then /var/log/openvpn.log
2019-03-03 09:18:45 +00:00
if \$ programname = = 'ovpn-server' then stop" | $SUDO tee /etc/rsyslog.d/30-openvpn.conf > /dev/null
2018-02-15 09:14:03 +00:00
2018-04-02 10:07:58 +00:00
echo " /var/log/openvpn.log
2018-02-15 09:14:03 +00:00
{
2018-09-04 10:06:52 +00:00
rotate 4
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
invoke-rc.d rsyslog rotate >/dev/null 2>& 1 || true
endscript
2018-04-02 10:07:58 +00:00
} " | $SUDO tee /etc/logrotate.d/openvpn > /dev/null
2018-02-15 09:14:03 +00:00
# Restart the logging service
case ${ PLAT } in
2018-04-02 10:07:58 +00:00
Ubuntu| Debian| *vuan)
$SUDO service rsyslog restart || true
; ;
*)
$SUDO systemctl restart rsyslog.service || true
; ;
2018-02-15 09:14:03 +00:00
esac
}
2017-03-13 04:00:28 +00:00
finalExports( ) {
# Update variables in setupVars.conf file
if [ -e " ${ setupVars } " ] ; then
2019-09-02 12:27:10 +00:00
$SUDO sed -i.update.bak '/INSTALL_USER/d;/UNATTUPG/d;/pivpnInterface/d;/IPv4dns/d;/IPv4addr/d;/IPv4gw/d;/pivpnProto/d;/PORT/d;/ENCRYPT/d;/DOWNLOAD_DH_PARAM/d;/PUBLICDNS/d;/OVPNDNS1/d;/OVPNDNS2/d;' " ${ setupVars } "
2017-03-13 04:00:28 +00:00
fi
{
2019-09-01 17:39:37 +00:00
echo " INSTALL_USER= ${ INSTALL_USER } "
2017-03-13 06:44:29 +00:00
echo " UNATTUPG= ${ UNATTUPG } "
2017-03-13 04:00:28 +00:00
echo " pivpnInterface= ${ pivpnInterface } "
echo " IPv4dns= ${ IPv4dns } "
echo " IPv4addr= ${ IPv4addr } "
echo " IPv4gw= ${ IPv4gw } "
2017-03-13 06:44:29 +00:00
echo " pivpnProto= ${ pivpnProto } "
echo " PORT= ${ PORT } "
echo " ENCRYPT= ${ ENCRYPT } "
2018-04-02 10:07:58 +00:00
echo " APPLY_TWO_POINT_FOUR= ${ APPLY_TWO_POINT_FOUR } "
2017-03-13 06:44:29 +00:00
echo " DOWNLOAD_DH_PARAM= ${ DOWNLOAD_DH_PARAM } "
echo " PUBLICDNS= ${ PUBLICDNS } "
echo " OVPNDNS1= ${ OVPNDNS1 } "
echo " OVPNDNS2= ${ OVPNDNS2 } "
2018-04-02 10:07:58 +00:00
} | $SUDO tee " ${ setupVars } " > /dev/null
2017-03-13 04:00:28 +00:00
}
2017-03-13 06:44:29 +00:00
# I suggest replacing some of these names.
2017-03-13 04:00:28 +00:00
#accountForRefactor() {
# # At some point in the future this list can be pruned, for now we'll need it to ensure updates don't break.
#
# # Refactoring of install script has changed the name of a couple of variables. Sort them out here.
2019-09-01 17:39:37 +00:00
# sed -i 's/INSTALL_USER/PIVPN_USER/g' ${setupVars}
2017-03-13 06:44:29 +00:00
# #sed -i 's/UNATTUPG/UNATTUPG/g' ${setupVars}
2017-03-13 04:00:28 +00:00
# sed -i 's/pivpnInterface/PIVPN_INTERFACE/g' ${setupVars}
# sed -i 's/IPv4dns/IPV4_DNS/g' ${setupVars}
# sed -i 's/IPv4addr/IPV4_ADDRESS/g' ${setupVars}
# sed -i 's/IPv4gw/IPV4_GATEWAY/g' ${setupVars}
2017-03-13 06:44:29 +00:00
# sed -i 's/pivpnProto/TRANSPORT_LAYER/g' ${setupVars}
# #sed -i 's/PORT/PORT/g' ${setupVars}
# #sed -i 's/ENCRYPT/ENCRYPT/g' ${setupVars}
# #sed -i 's/DOWNLOAD_DH_PARAM/DOWNLOAD_DH_PARAM/g' ${setupVars}
2017-03-13 21:48:16 +00:00
# sed -i 's/PUBLICDNS/PUBLIC_DNS/g' ${setupVars}
# sed -i 's/OVPNDNS1/OVPN_DNS_1/g' ${setupVars}
# sed -i 's/OVPNDNS2/OVPN_DNS_2/g' ${setupVars}
2017-03-13 04:00:28 +00:00
#}
2016-04-19 18:01:55 +00:00
installPiVPN( ) {
stopServices
$SUDO mkdir -p /etc/pivpn/
2017-03-13 04:00:28 +00:00
confUnattendedUpgrades
installScripts
setCustomProto
setCustomPort
confOpenVPN
confNetwork
confOVPN
setClientDNS
2019-03-10 17:55:14 +00:00
setCustomDomain
2018-02-15 09:14:03 +00:00
confLogging
2017-03-13 04:00:28 +00:00
finalExports
}
2017-03-13 06:44:29 +00:00
updatePiVPN( ) {
2017-03-13 04:00:28 +00:00
#accountForRefactor
stopServices
confUnattendedUpgrades
2016-04-19 18:01:55 +00:00
installScripts
2017-06-04 20:18:26 +00:00
2017-03-13 06:44:29 +00:00
# setCustomProto
# write out the PROTO
PROTO = $pivpnProto
$SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
2017-06-04 20:18:26 +00:00
2017-03-13 06:44:29 +00:00
#setCustomPort
# write out the port
$SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
2017-06-04 20:18:26 +00:00
2016-04-19 18:01:55 +00:00
confOpenVPN
confNetwork
confOVPN
2017-06-04 20:18:26 +00:00
2017-03-14 02:25:39 +00:00
# ?? Is this always OK? Also if you only select one DNS server ??
2017-03-13 06:44:29 +00:00
$SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1' ${ OVPNDNS1 } '\"/' /etc/openvpn/server.conf
$SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1' ${ OVPNDNS2 } '\"/' /etc/openvpn/server.conf
2017-03-13 04:00:28 +00:00
finalExports #re-export setupVars.conf to account for any new vars added in new versions
2016-04-19 18:01:55 +00:00
}
2017-03-13 04:00:28 +00:00
2016-04-19 18:01:55 +00:00
displayFinalMessage( ) {
# Final completion message to user
2016-09-27 12:24:14 +00:00
whiptail --msgbox --backtitle "Make it so." --title "Installation Complete!" " Now run 'pivpn add' to create the ovpn profiles.
2019-06-06 14:59:15 +00:00
Run 'pivpn help' to see what else you can do !\n \n If you run into any issue, please read all our documentation carefully.
All incomplete posts or bug reports will be ignored or deleted.\n \n Thank you for using PiVPN." ${ r } ${ c }
2016-11-22 18:23:51 +00:00
if ( whiptail --title "Reboot" --yesno --defaultno "It is strongly recommended you reboot after installation. Would you like to reboot now?" ${ r } ${ c } ) ; then
whiptail --title "Rebooting" --msgbox "The system will now reboot." ${ r } ${ c }
2019-11-29 09:45:32 +00:00
printf "\nRebooting system...\n"
2016-04-21 13:37:27 +00:00
$SUDO sleep 3
$SUDO shutdown -r now
2016-04-19 18:01:55 +00:00
fi
}
2017-03-13 04:00:28 +00:00
update_dialogs( ) {
# reconfigure
if [ " ${ reconfigure } " = true ] ; then
opt1a = "Repair"
opt1b = "This will retain existing settings"
strAdd = "You will remain on the same version"
else
opt1a = "Update"
opt1b = "This will retain existing settings."
strAdd = "You will be updated to the latest version."
fi
opt2a = "Reconfigure"
opt2b = "This will allow you to enter new settings"
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
UpdateCmd = $( whiptail --title "Existing Install Detected!" --menu " \n\nWe have detected an existing install.\n\nPlease choose from the following options: \n( $strAdd ) " ${ r } ${ c } 2 \
" ${ opt1a } " " ${ opt1b } " \
" ${ opt2a } " " ${ opt2b } " 3>& 2 2>& 1 1>& 3) || \
{ echo "::: Cancel selected. Exiting" ; exit 1; }
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
case ${ UpdateCmd } in
${ opt1a } )
echo " ::: ${ opt1a } option selected. "
useUpdateVars = true
; ;
${ opt2a } )
echo " ::: ${ opt2a } option selected "
useUpdateVars = false
; ;
esac
}
2016-12-24 05:20:45 +00:00
2017-03-13 04:00:28 +00:00
clone_or_update_repos( ) {
if [ [ " ${ reconfigure } " = = true ] ] ; then
echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
else
# Get Git files
getGitFiles ${ pivpnFilesDir } ${ pivpnGitUrl } || \
{ echo " !!! Unable to clone ${ pivpnGitUrl } into ${ pivpnFilesDir } , unable to continue. " ; \
exit 1; \
}
fi
}
2016-12-24 05:20:45 +00:00
2019-10-12 16:34:37 +00:00
checkhostname( ) {
###Checks for hostnamesize
host_name = $( hostname -s)
if [ [ ! ${# host_name } -le 28 ] ] ; then
until [ [ ${# host_name } -le 28 && $host_name = ~ ^[ a-zA-Z0-9] [ a-zA-Z0-9-] { 1,28} $ ] ] ; do
host_name = $( whiptail --inputbox "Your hostname is too long.\nEnter new hostname with less then 28 characters\nNo special characters allowed." \
--title "Hostname too long" ${ r } ${ c } 3>& 1 1>& 2 2>& 3)
$SUDO hostnamectl set-hostname " ${ host_name } "
if [ [ ${# host_name } -le 28 && $host_name = ~ ^[ a-zA-Z0-9] [ a-zA-Z0-9-] { 1,28} $ ] ] ; then
echo "::: Hostname valid and length OK, proceeding..."
fi
done
else
echo "::: Hostname length OK"
fi
}
2019-06-06 14:59:15 +00:00
2017-03-13 04:00:28 +00:00
######## SCRIPT ############
2016-12-24 05:20:45 +00:00
2017-03-13 04:00:28 +00:00
main( ) {
2016-04-19 18:01:55 +00:00
2017-03-13 04:00:28 +00:00
######## FIRST CHECK ########
# Must be root to install
echo ":::"
if [ [ $EUID -eq 0 ] ] ; then
echo "::: You are root."
else
echo "::: sudo will be used for the install."
# Check if it is actually installed
# If it isn't, exit because the install cannot complete
if [ [ $( dpkg-query -s sudo) ] ] ; then
export SUDO = "sudo"
export SUDOE = "sudo -E"
else
echo "::: Please install sudo or run this as root."
exit 1
fi
fi
2017-06-04 20:18:26 +00:00
2019-10-12 16:34:37 +00:00
2017-03-13 04:00:28 +00:00
# Check for supported distribution
distro_check
2017-06-04 20:18:26 +00:00
2019-10-12 16:34:37 +00:00
# Checks for hostname Length
checkhostname
2017-03-13 04:00:28 +00:00
# Check arguments for the undocumented flags
for var in " $@ " ; do
case " $var " in
"--reconfigure" ) reconfigure = true; ;
"--i_do_not_follow_recommendations" ) skipSpaceCheck = false; ;
"--unattended" ) runUnattended = true; ;
esac
done
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
if [ [ -f ${ setupVars } ] ] ; then
if [ [ " ${ runUnattended } " = = true ] ] ; then
echo "::: --unattended passed to install script, no whiptail dialogs will be displayed"
useUpdateVars = true
else
update_dialogs
fi
fi
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Start the installer
# Verify there is enough disk space for the install
if [ [ " ${ skipSpaceCheck } " = = true ] ] ; then
echo "::: --i_do_not_follow_recommendations passed to script, skipping free disk space verification!"
else
verifyFreeDiskSpace
fi
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Install the packages (we do this first because we need whiptail)
2018-04-02 10:07:58 +00:00
addSoftwareRepo
2017-03-13 04:00:28 +00:00
update_package_cache
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Notify user of package availability
notify_package_updates_available
2017-06-04 20:18:26 +00:00
2019-07-23 20:12:35 +00:00
2017-03-13 04:00:28 +00:00
# Install packages used by this installation script
install_dependent_packages PIVPN_DEPS[ @]
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
if [ [ ${ useUpdateVars } = = false ] ] ; then
# Display welcome dialogs
welcomeDialogs
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Find interfaces and let the user choose one
chooseInterface
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Only try to set static on Raspbian, otherwise let user do it
if [ [ $PLAT != "Raspbian" ] ] ; then
avoidStaticIPv4Ubuntu
else
getStaticIPv4Settings
setStaticIPv4
fi
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Choose the user for the ovpns
chooseUser
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Ask if unattended-upgrades will be enabled
unattendedUpgrades
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Clone/Update the repos
clone_or_update_repos
2017-06-04 20:18:26 +00:00
2019-05-03 08:26:56 +00:00
# Install
2019-03-03 09:18:45 +00:00
if installPiVPN; then
echo "::: Install Complete..."
else
exit 1
fi
2017-03-13 04:00:28 +00:00
else
2017-03-13 06:44:29 +00:00
# Source ${setupVars} for use in the rest of the functions.
source ${ setupVars }
2017-06-04 20:18:26 +00:00
2017-03-14 02:25:39 +00:00
echo " ::: Using IP address: $IPv4addr "
echo " ${ IPv4addr %/* } " > /tmp/pivpnIP
echo " ::: Using interface: $pivpnInterface "
echo " ${ pivpnInterface } " > /tmp/pivpnINT
2019-09-01 17:39:37 +00:00
echo " ::: Using User: $INSTALL_USER "
echo " ${ INSTALL_USER } " > /tmp/INSTALL_USER
2017-03-14 02:25:39 +00:00
echo " ::: Using protocol: $pivpnProto "
echo " ${ pivpnProto } " > /tmp/pivpnPROTO
echo " ::: Using port: $PORT "
echo ${ PORT } > /tmp/INSTALL_PORT
2017-03-14 02:39:07 +00:00
echo ":::"
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Only try to set static on Raspbian
2017-06-06 22:27:23 +00:00
case ${ PLAT } in
Rasp*)
setStaticIPv4 # This might be a problem if a user tries to modify the ip in the config file and then runs an update because of the way we check for previous configuration in /etc/dhcpcd.conf
; ;
*)
2017-03-13 04:00:28 +00:00
echo "::: IP Information"
echo "::: Since we think you are not using Raspbian, we will not configure a static IP for you."
echo "::: If you are in Amazon then you can not configure a static IP anyway."
echo "::: Just ensure before this installer started you had set an elastic IP on your instance."
2017-06-06 22:27:23 +00:00
; ;
esac
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
# Clone/Update the repos
clone_or_update_repos
2017-06-04 20:18:26 +00:00
2019-03-03 09:18:45 +00:00
updatePiVPN
2017-03-13 04:00:28 +00:00
fi
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
echo "::: Restarting services..."
# Start services
2017-06-04 20:18:26 +00:00
case ${ PLAT } in
2017-06-06 22:27:23 +00:00
Ubuntu| Debian| *vuan)
2017-06-04 20:18:26 +00:00
$SUDO service openvpn start
; ;
*)
$SUDO systemctl enable openvpn.service
$SUDO systemctl start openvpn.service
; ;
esac
2019-07-23 20:12:35 +00:00
2019-05-05 04:39:30 +00:00
# Ensure that cached writes reach persistent storage
echo "::: Flushing writes to disk..."
sync
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
echo "::: done."
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
if [ [ " ${ useUpdateVars } " = = false ] ] ; then
displayFinalMessage
fi
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
echo ":::"
if [ [ " ${ useUpdateVars } " = = false ] ] ; then
echo "::: Installation Complete!"
2017-09-22 19:11:23 +00:00
echo "::: Now run 'pivpn add' to create an ovpn profile for each of your devices."
2017-03-13 04:00:28 +00:00
echo "::: Run 'pivpn help' to see what else you can do!"
echo "::: It is strongly recommended you reboot after installation."
else
echo "::: Update complete!"
fi
2017-06-04 20:18:26 +00:00
2017-03-13 04:00:28 +00:00
echo ":::"
}
2016-04-22 19:16:48 +00:00
2017-03-14 02:25:39 +00:00
if [ [ " ${ PIVPN_TEST } " != true ] ] ; then
2017-03-13 04:00:28 +00:00
main " $@ "
2016-04-22 19:16:48 +00:00
fi