Advertisement
Guest User

Untitled

a guest
Feb 28th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 55.56 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # PiVPN: Trivial OpenVPN setup and configuration
  3. # Easiest setup and mangement of OpenVPN on Raspberry Pi
  4. # http://pivpn.io
  5. # Heavily adapted from the pi-hole.net project and...
  6. # https://github.com/StarshipEngineer/OpenVPN-Setup/
  7. #
  8. # Install with this command (from your Pi):
  9. #
  10. # curl -L https://install.pivpn.io | bash
  11. # Make sure you have `curl` installed
  12.  
  13. set -e
  14. ######## VARIABLES #########
  15.  
  16. tmpLog="/tmp/pivpn-install.log"
  17. instalLogLoc="/etc/pivpn/install.log"
  18. setupVars=/etc/pivpn/setupVars.conf
  19. useUpdateVars=false
  20.  
  21. ### PKG Vars ###
  22. PKG_MANAGER="apt-get"
  23. PKG_CACHE="/var/lib/apt/lists/"
  24. UPDATE_PKG_CACHE="${PKG_MANAGER} update"
  25. PKG_INSTALL="${PKG_MANAGER} --yes --no-install-recommends install"
  26. PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true"
  27. PIVPN_DEPS=( openvpn git dhcpcd5 tar wget grep iptables-persistent dnsutils expect whiptail net-tools)
  28. ### ###
  29.  
  30. pivpnGitUrl="https://github.com/pivpn/pivpn.git"
  31. pivpnFilesDir="/etc/.pivpn"
  32. easyrsaVer="3.0.1-pivpn1"
  33. easyrsaRel="https://github.com/pivpn/easy-rsa/releases/download/${easyrsaVer}/EasyRSA-${easyrsaVer}.tgz"
  34.  
  35. # Find the rows and columns. Will default to 80x24 if it can not be detected.
  36. screen_size=$(stty size 2>/dev/null || echo 24 80)
  37. rows=$(echo $screen_size | awk '{print $1}')
  38. columns=$(echo $screen_size | awk '{print $2}')
  39.  
  40. # Divide by two so the dialogs take up half of the screen, which looks nice.
  41. r=$(( rows / 2 ))
  42. c=$(( columns / 2 ))
  43. # Unless the screen is tiny
  44. r=$(( r < 20 ? 20 : r ))
  45. c=$(( c < 70 ? 70 : c ))
  46.  
  47. ######## Undocumented Flags. Shhh ########
  48. skipSpaceCheck=false
  49. reconfigure=false
  50. runUnattended=false
  51.  
  52. # Find IP used to route to outside world
  53.  
  54. IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
  55. IPv4addr=$(ip route get 8.8.8.8| awk '{print $7}')
  56. IPv4gw=$(ip route get 8.8.8.8 | awk '{print $3}')
  57.  
  58. availableInterfaces=$(ip -o link | grep "state UP" | awk '{print $2}' | cut -d':' -f1 | cut -d'@' -f1)
  59. dhcpcdFile=/etc/dhcpcd.conf
  60.  
  61. # Next see if we are on a tested and supported OS
  62. function noOS_Support() {
  63. whiptail --msgbox --backtitle "INVALID OS DETECTED" --title "Invalid OS" "We have not been able to detect a supported OS.
  64. Currently this installer supports Raspbian and Debian (Jessie and Stretch), Devuan (Jessie) and Ubuntu from 14.04 (trusty) to 17.04 (zesty).
  65. 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}
  66. exit 1
  67. }
  68.  
  69. function maybeOS_Support() {
  70. if (whiptail --backtitle "Not Supported OS" --title "Not Supported OS" --yesno "You are on an OS that we have not tested but MAY work.
  71. Currently this installer supports Raspbian and Debian (Jessie and Stretch), Devuan (Jessie) and Ubuntu from 14.04 (trusty) to 17.04 (zesty).
  72. Would you like to continue anyway?" ${r} ${c}) then
  73. echo "::: Did not detect perfectly supported OS but,"
  74. echo "::: Continuing installation at user's own risk..."
  75. else
  76. echo "::: Exiting due to unsupported OS"
  77. exit 1
  78. fi
  79. }
  80.  
  81. # Compatibility
  82. distro_check() {
  83. # if lsb_release command is on their system
  84. if hash lsb_release 2>/dev/null; then
  85. PLAT=$(lsb_release -si)
  86. OSCN=$(lsb_release -sc) # We want this to be trusty xenial or jessie
  87.  
  88. case ${PLAT} in
  89. Ubuntu|Raspbian|Debian|Devuan)
  90. case ${OSCN} in
  91. trusty|xenial|jessie|stretch)
  92. ;;
  93. *)
  94. maybeOS_Support
  95. ;;
  96. esac
  97. ;;
  98. *)
  99. noOS_Support
  100. ;;
  101. esac
  102. # else get info from os-release
  103. elif grep -q devuan /etc/os-release; then
  104. if grep -q jessie /etc/os-release; then
  105. PLAT="Raspvuan"
  106. OSCN="jessie"
  107. else
  108. noOS_Support
  109. fi
  110. elif grep -q debian /etc/os-release; then
  111. if grep -q jessie /etc/os-release; then
  112. PLAT="Raspbian"
  113. OSCN="jessie"
  114. elif grep -q stretch /etc/os-release; then
  115. PLAT="Raspbian"
  116. OSCN="stretch"
  117. else
  118. PLAT="Ubuntu"
  119. OSCN="unknown"
  120. maybeOS_Support
  121. fi
  122. # else we prob don't want to install
  123. else
  124. noOS_Support
  125. fi
  126.  
  127. echo "${PLAT}" > /tmp/DET_PLATFORM
  128. }
  129.  
  130. ####### FUNCTIONS ##########
  131. spinner()
  132. {
  133. local pid=$1
  134. local delay=0.50
  135. local spinstr='/-\|'
  136. while [ "$(ps a | awk '{print $1}' | grep "${pid}")" ]; do
  137. local temp=${spinstr#?}
  138. printf " [%c] " "${spinstr}"
  139. local spinstr=${temp}${spinstr%"$temp"}
  140. sleep ${delay}
  141. printf "\b\b\b\b\b\b"
  142. done
  143. printf " \b\b\b\b"
  144. }
  145.  
  146. welcomeDialogs() {
  147. # Display the welcome dialog
  148. whiptail --msgbox --backtitle "Welcome" --title "PiVPN Automated Installer" "This installer will transform your Raspberry Pi into an OpenVPN server!" ${r} ${c}
  149.  
  150. # Explain the need for a static address
  151. 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.
  152.  
  153. In the next section, you can choose to use your current network settings (DHCP) or to manually edit them." ${r} ${c}
  154. }
  155.  
  156. chooseUser() {
  157. # Explain the local user
  158. whiptail --msgbox --backtitle "Parsing User List" --title "Local Users" "Choose a local user that will hold your ovpn configurations." ${r} ${c}
  159. # First, let's check if there is a user available.
  160. numUsers=$(awk -F':' 'BEGIN {count=0} $3>=500 && $3<=60000 { count++ } END{ print count }' /etc/passwd)
  161. if [ "$numUsers" -eq 0 ]
  162. then
  163. # We don't have a user, let's ask to add one.
  164. 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)
  165. then
  166. # See http://askubuntu.com/a/667842/459815
  167. PASSWORD=$(whiptail --title "password dialog" --passwordbox "Please enter the new user password" ${r} ${c} 3>&1 1>&2 2>&3)
  168. CRYPT=$(perl -e 'printf("%s\n", crypt($ARGV[0], "password"))' "${PASSWORD}")
  169. $SUDO useradd -m -p "${CRYPT}" -s /bin/bash "${userToAdd}"
  170. if [[ $? = 0 ]]; then
  171. echo "Succeeded"
  172. ((numUsers+=1))
  173. else
  174. exit 1
  175. fi
  176. else
  177. exit 1
  178. fi
  179. fi
  180. availableUsers=$(awk -F':' '$3>=500 && $3<=60000 {print $1}' /etc/passwd)
  181. local userArray=()
  182. local firstloop=1
  183.  
  184. while read -r line
  185. do
  186. mode="OFF"
  187. if [[ $firstloop -eq 1 ]]; then
  188. firstloop=0
  189. mode="ON"
  190. fi
  191. userArray+=("${line}" "" "${mode}")
  192. done <<< "${availableUsers}"
  193. chooseUserCmd=(whiptail --title "Choose A User" --separate-output --radiolist "Choose (press space to select):" ${r} ${c} ${numUsers})
  194. chooseUserOptions=$("${chooseUserCmd[@]}" "${userArray[@]}" 2>&1 >/dev/tty)
  195. if [[ $? = 0 ]]; then
  196. for desiredUser in ${chooseUserOptions}; do
  197. pivpnUser=${desiredUser}
  198. echo "::: Using User: $pivpnUser"
  199. echo "${pivpnUser}" > /tmp/pivpnUSR
  200. done
  201. else
  202. echo "::: Cancel selected, exiting...."
  203. exit 1
  204. fi
  205. }
  206.  
  207. verifyFreeDiskSpace() {
  208. # If user installs unattended-upgrades we'd need about 60MB so will check for 75MB free
  209. echo "::: Verifying free disk space..."
  210. local required_free_kilobytes=76800
  211. local existing_free_kilobytes=$(df -Pk | grep -m1 '\/$' | awk '{print $4}')
  212.  
  213. # - Unknown free disk space , not a integer
  214. if ! [[ "${existing_free_kilobytes}" =~ ^([0-9])+$ ]]; then
  215. echo "::: Unknown free disk space!"
  216. echo "::: We were unable to determine available free disk space on this system."
  217. echo "::: You may continue with the installation, however, it is not recommended."
  218. read -r -p "::: If you are sure you want to continue, type YES and press enter :: " response
  219. case $response in
  220. [Y][E][S])
  221. ;;
  222. *)
  223. echo "::: Confirmation not received, exiting..."
  224. exit 1
  225. ;;
  226. esac
  227. # - Insufficient free disk space
  228. elif [[ ${existing_free_kilobytes} -lt ${required_free_kilobytes} ]]; then
  229. echo "::: Insufficient Disk Space!"
  230. echo "::: Your system appears to be low on disk space. PiVPN recommends a minimum of $required_free_kilobytes KiloBytes."
  231. echo "::: You only have ${existing_free_kilobytes} KiloBytes free."
  232. echo "::: If this is a new install on a Raspberry Pi you may need to expand your disk."
  233. echo "::: Try running 'sudo raspi-config', and choose the 'expand file system option'"
  234. echo "::: After rebooting, run this installation again. (curl -L https://install.pivpn.io | bash)"
  235.  
  236. echo "Insufficient free space, exiting..."
  237. exit 1
  238. fi
  239. }
  240.  
  241.  
  242. chooseInterface() {
  243. # Turn the available interfaces into an array so it can be used with a whiptail dialog
  244. local interfacesArray=()
  245. # Number of available interfaces
  246. local interfaceCount
  247. # Whiptail variable storage
  248. local chooseInterfaceCmd
  249. # Temporary Whiptail options storage
  250. local chooseInterfaceOptions
  251. # Loop sentinel variable
  252. local firstLoop=1
  253.  
  254. if [[ $(echo "${availableInterfaces}" | wc -l) -eq 1 ]]; then
  255. pivpnInterface="${availableInterfaces}"
  256. echo "${pivpnInterface}" > /tmp/pivpnINT
  257. return
  258. fi
  259.  
  260. while read -r line; do
  261. mode="OFF"
  262. if [[ ${firstloop} -eq 1 ]]; then
  263. firstloop=0
  264. mode="ON"
  265. fi
  266. interfacesArray+=("${line}" "available" "${mode}")
  267. done <<< "${availableInterfaces}"
  268.  
  269. # Find out how many interfaces are available to choose from
  270. interfaceCount=$(echo "${availableInterfaces}" | wc -l)
  271. chooseInterfaceCmd=(whiptail --separate-output --radiolist "Choose An Interface (press space to select):" ${r} ${c} ${interfaceCount})
  272. chooseInterfaceOptions=$("${chooseInterfaceCmd[@]}" "${interfacesArray[@]}" 2>&1 >/dev/tty)
  273. if [[ $? = 0 ]]; then
  274. for desiredInterface in ${chooseInterfaceOptions}; do
  275. pivpnInterface=${desiredInterface}
  276. echo "::: Using interface: $pivpnInterface"
  277. echo "${pivpnInterface}" > /tmp/pivpnINT
  278. done
  279. else
  280. echo "::: Cancel selected, exiting...."
  281. exit 1
  282. fi
  283. }
  284.  
  285. avoidStaticIPv4Ubuntu() {
  286. # If we are in Ubuntu then they need to have previously set their network, so just use what you have.
  287. 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.
  288. 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}
  289. }
  290.  
  291. getStaticIPv4Settings() {
  292. local ipSettingsCorrect
  293. # Grab their current DNS Server
  294. IPv4dns=$(nslookup 127.0.0.1 | grep Server: | awk '{print $2}')
  295. # Ask if the user wants to use DHCP settings as their static IP
  296. if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Do you want to use your current network settings as a static address?
  297. IP address: ${IPv4addr}
  298. Gateway: ${IPv4gw}" ${r} ${c}); then
  299. # If they choose yes, let the user know that the IP address will not be available via DHCP and may cause a conflict.
  300. 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.
  301. If you are worried, either manually set the address, or modify the DHCP reservation pool so it does not include the IP you want.
  302. 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}
  303. # Nothing else to do since the variables are already set above
  304. else
  305. # Otherwise, we need to ask the user to input their desired settings.
  306. # Start by getting the IPv4 address (pre-filling it with info gathered from DHCP)
  307. # Start a loop to let the user enter their information with the chance to go back and edit it if necessary
  308. until [[ ${ipSettingsCorrect} = True ]]; do
  309. # Ask for the IPv4 address
  310. IPv4addr=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 address" --inputbox "Enter your desired IPv4 address" ${r} ${c} "${IPv4addr}" 3>&1 1>&2 2>&3)
  311. if [[ $? = 0 ]]; then
  312. echo "::: Your static IPv4 address: ${IPv4addr}"
  313. # Ask for the gateway
  314. 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)
  315. if [[ $? = 0 ]]; then
  316. echo "::: Your static IPv4 gateway: ${IPv4gw}"
  317. # Give the user a chance to review their settings before moving on
  318. if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Are these settings correct?
  319. IP address: ${IPv4addr}
  320. Gateway: ${IPv4gw}" ${r} ${c}); then
  321. # If the settings are correct, then we need to set the pivpnIP
  322. echo "${IPv4addr%/*}" > /tmp/pivpnIP
  323. echo "$pivpnInterface" > /tmp/pivpnINT
  324. # After that's done, the loop ends and we move on
  325. ipSettingsCorrect=True
  326. else
  327. # If the settings are wrong, the loop continues
  328. ipSettingsCorrect=False
  329. fi
  330. else
  331. # Cancelling gateway settings window
  332. ipSettingsCorrect=False
  333. echo "::: Cancel selected. Exiting..."
  334. exit 1
  335. fi
  336. else
  337. # Cancelling IPv4 settings window
  338. ipSettingsCorrect=False
  339. echo "::: Cancel selected. Exiting..."
  340. exit 1
  341. fi
  342. done
  343. # End the if statement for DHCP vs. static
  344. fi
  345. }
  346.  
  347. setDHCPCD() {
  348. # Append these lines to dhcpcd.conf to enable a static IP
  349. echo "interface ${pivpnInterface}
  350. static ip_address=${IPv4addr}
  351. static routers=${IPv4gw}
  352. static domain_name_servers=${IPv4dns}" | $SUDO tee -a ${dhcpcdFile} >/dev/null
  353. }
  354.  
  355. setStaticIPv4() {
  356. # Tries to set the IPv4 address
  357. if [[ -f /etc/dhcpcd.conf ]]; then
  358. if grep -q "${IPv4addr}" ${dhcpcdFile}; then
  359. echo "::: Static IP already configured."
  360. else
  361. setDHCPCD
  362. $SUDO ip addr replace dev "${pivpnInterface}" "${IPv4addr}"
  363. echo ":::"
  364. echo "::: Setting IP to ${IPv4addr}. You may need to restart after the install is complete."
  365. echo ":::"
  366. fi
  367. else
  368. echo "::: Critical: Unable to locate configuration file to set static IPv4 address!"
  369. exit 1
  370. fi
  371. }
  372.  
  373. function valid_ip()
  374. {
  375. local ip=$1
  376. local stat=1
  377.  
  378. if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  379. OIFS=$IFS
  380. IFS='.'
  381. ip=($ip)
  382. IFS=$OIFS
  383. [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
  384. && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
  385. stat=$?
  386. fi
  387. return $stat
  388. }
  389.  
  390. installScripts() {
  391. # Install the scripts from /etc/.pivpn to their various locations
  392. $SUDO echo ":::"
  393. $SUDO echo -n "::: Installing scripts to /opt/pivpn..."
  394. if [ ! -d /opt/pivpn ]; then
  395. $SUDO mkdir /opt/pivpn
  396. $SUDO chown "$pivpnUser":root /opt/pivpn
  397. $SUDO chmod u+srwx /opt/pivpn
  398. fi
  399. $SUDO cp /etc/.pivpn/scripts/makeOVPN.sh /opt/pivpn/makeOVPN.sh
  400. $SUDO cp /etc/.pivpn/scripts/clientStat.sh /opt/pivpn/clientStat.sh
  401. $SUDO cp /etc/.pivpn/scripts/listOVPN.sh /opt/pivpn/listOVPN.sh
  402. $SUDO cp /etc/.pivpn/scripts/removeOVPN.sh /opt/pivpn/removeOVPN.sh
  403. $SUDO cp /etc/.pivpn/scripts/uninstall.sh /opt/pivpn/uninstall.sh
  404. $SUDO cp /etc/.pivpn/scripts/pivpnDebug.sh /opt/pivpn/pivpnDebug.sh
  405. $SUDO cp /etc/.pivpn/scripts/fix_iptables.sh /opt/pivpn/fix_iptables.sh
  406. $SUDO chmod 0755 /opt/pivpn/{makeOVPN,clientStat,listOVPN,removeOVPN,uninstall,pivpnDebug,fix_iptables}.sh
  407. $SUDO cp /etc/.pivpn/pivpn /usr/local/bin/pivpn
  408. $SUDO chmod 0755 /usr/local/bin/pivpn
  409. $SUDO cp /etc/.pivpn/scripts/bash-completion /etc/bash_completion.d/pivpn
  410. . /etc/bash_completion.d/pivpn
  411. # Copy interface setting for debug
  412. $SUDO cp /tmp/pivpnINT /etc/pivpn/pivpnINTERFACE
  413.  
  414. $SUDO echo " done."
  415. }
  416.  
  417. package_check_install() {
  418. dpkg-query -W -f='${Status}' "${1}" 2>/dev/null | grep -c "ok installed" || ${PKG_INSTALL} "${1}"
  419. }
  420.  
  421. update_package_cache() {
  422. #Running apt-get update/upgrade with minimal output can cause some issues with
  423. #requiring user input
  424.  
  425. #Check to see if apt-get update has already been run today
  426. #it needs to have been run at least once on new installs!
  427. timestamp=$(stat -c %Y ${PKG_CACHE})
  428. timestampAsDate=$(date -d @"${timestamp}" "+%b %e")
  429. today=$(date "+%b %e")
  430.  
  431.  
  432. if [ ! "${today}" == "${timestampAsDate}" ]; then
  433. #update package lists
  434. echo ":::"
  435. echo -n "::: ${PKG_MANAGER} update has not been run today. Running now..."
  436. $SUDO ${UPDATE_PKG_CACHE} &> /dev/null
  437. echo " done!"
  438. fi
  439. }
  440.  
  441. notify_package_updates_available() {
  442. # Let user know if they have outdated packages on their system and
  443. # advise them to run a package update at soonest possible.
  444. echo ":::"
  445. echo -n "::: Checking ${PKG_MANAGER} for upgraded packages...."
  446. updatesToInstall=$(eval "${PKG_COUNT}")
  447. echo " done!"
  448. echo ":::"
  449. if [[ ${updatesToInstall} -eq "0" ]]; then
  450. echo "::: Your system is up to date! Continuing with PiVPN installation..."
  451. else
  452. echo "::: There are ${updatesToInstall} updates available for your system!"
  453. echo "::: We recommend you update your OS after installing PiVPN! "
  454. echo ":::"
  455. fi
  456. }
  457.  
  458. install_dependent_packages() {
  459. # Install packages passed in via argument array
  460. # No spinner - conflicts with set -e
  461. declare -a argArray1=("${!1}")
  462.  
  463. echo iptables-persistent iptables-persistent/autosave_v4 boolean true | $SUDO debconf-set-selections
  464. echo iptables-persistent iptables-persistent/autosave_v6 boolean false | $SUDO debconf-set-selections
  465.  
  466. if command -v debconf-apt-progress &> /dev/null; then
  467. $SUDO debconf-apt-progress -- ${PKG_INSTALL} "${argArray1[@]}"
  468. else
  469. for i in "${argArray1[@]}"; do
  470. echo -n "::: Checking for $i..."
  471. $SUDO package_check_install "${i}" &> /dev/null
  472. echo " installed!"
  473. done
  474. fi
  475. }
  476.  
  477. unattendedUpgrades() {
  478. 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}
  479.  
  480. 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
  481. UNATTUPG="unattended-upgrades"
  482. else
  483. UNATTUPG=""
  484. fi
  485. }
  486.  
  487. stopServices() {
  488. # Stop openvpn
  489. $SUDO echo ":::"
  490. $SUDO echo -n "::: Stopping OpenVPN service..."
  491. case ${PLAT} in
  492. Ubuntu|Debian|*vuan)
  493. $SUDO service openvpn stop || true
  494. ;;
  495. *)
  496. $SUDO systemctl stop openvpn.service || true
  497. ;;
  498. esac
  499. $SUDO echo " done."
  500. }
  501.  
  502. checkForDependencies() {
  503. #Running apt-get update/upgrade with minimal output can cause some issues with
  504. #requiring user input (e.g password for phpmyadmin see #218)
  505. #We'll change the logic up here, to check to see if there are any updates available and
  506. # if so, advise the user to run apt-get update/upgrade at their own discretion
  507. #Check to see if apt-get update has already been run today
  508. # it needs to have been run at least once on new installs!
  509.  
  510. timestamp=$(stat -c %Y /var/cache/apt/)
  511. timestampAsDate=$(date -d @"$timestamp" "+%b %e")
  512. today=$(date "+%b %e")
  513.  
  514. case ${PLAT} in
  515. Ubuntu|Debian|Devuan)
  516. case ${OSCN} in
  517. trusty|jessie|wheezy|stretch)
  518. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg| $SUDO apt-key add -
  519. echo "deb http://swupdate.openvpn.net/apt $OSCN main" | $SUDO tee /etc/apt/sources.list.d/swupdate.openvpn.net.list > /dev/null
  520. echo -n "::: Adding OpenVPN repo for $PLAT $OSCN ..."
  521. $SUDO apt-get -qq update & spinner $!
  522. echo " done!"
  523. ;;
  524. esac
  525. ;;
  526. esac
  527. if [[ $PLAT == "Ubuntu" || $PLAT == "Debian" ]]; then
  528. if [[ $OSCN == "trusty" || $OSCN == "jessie" || $OSCN == "wheezy" || $OSCN == "stretch" ]]; then
  529. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg| $SUDO apt-key add -
  530. echo "deb http://build.openvpn.net/debian/openvpn/stable $OSCN main" | $SUDO tee /etc/apt/sources.list.d/swupdate.openvpn.net.list > /dev/null
  531. echo -n "::: Adding OpenVPN repo for $PLAT $OSCN ..."
  532. $SUDO apt-get -qq update & spinner $!
  533. echo " done!"
  534. fi
  535. fi
  536.  
  537. if [ ! "$today" == "$timestampAsDate" ]; then
  538. #update package lists
  539. echo ":::"
  540. echo -n "::: apt-get update has not been run today. Running now..."
  541. $SUDO apt-get -qq update & spinner $!
  542. echo " done!"
  543. fi
  544. echo ":::"
  545. echo -n "::: Checking apt-get for upgraded packages...."
  546. updatesToInstall=$($SUDO apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst)
  547. echo " done!"
  548. echo ":::"
  549. if [[ $updatesToInstall -eq "0" ]]; then
  550. echo "::: Your pi is up to date! Continuing with PiVPN installation..."
  551. else
  552. echo "::: There are $updatesToInstall updates availible for your pi!"
  553. echo "::: We recommend you run 'sudo apt-get upgrade' after installing PiVPN! "
  554. echo ":::"
  555. fi
  556. echo ":::"
  557. echo "::: Checking dependencies:"
  558.  
  559. dependencies=( openvpn git dhcpcd5 tar wget grep iptables-persistent dnsutils expect whiptail net-tools)
  560. for i in "${dependencies[@]}"; do
  561. echo -n "::: Checking for $i..."
  562. if [ "$(dpkg-query -W -f='${Status}' "$i" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then
  563. echo -n " Not found! Installing...."
  564. #Supply answers to the questions so we don't prompt user
  565. if [[ $i = "iptables-persistent" ]]; then
  566. echo iptables-persistent iptables-persistent/autosave_v4 boolean true | $SUDO debconf-set-selections
  567. echo iptables-persistent iptables-persistent/autosave_v6 boolean false | $SUDO debconf-set-selections
  568. fi
  569. if [[ $i == "expect" ]] || [[ $i == "openvpn" ]]; then
  570. ($SUDO apt-get --yes --quiet --no-install-recommends install "$i" > /dev/null || echo "Installation Failed!" && fixApt) & spinner $!
  571. else
  572. ($SUDO apt-get --yes --quiet install "$i" > /dev/null || echo "Installation Failed!" && fixApt) & spinner $!
  573. fi
  574. echo " done!"
  575. else
  576. echo " already installed!"
  577. fi
  578. done
  579. }
  580.  
  581. getGitFiles() {
  582. # Setup git repos for base files
  583. echo ":::"
  584. echo "::: Checking for existing base files..."
  585. if is_repo "${1}"; then
  586. update_repo "${1}"
  587. else
  588. make_repo "${1}" "${2}"
  589. fi
  590. }
  591.  
  592. is_repo() {
  593. # If the directory does not have a .git folder it is not a repo
  594. echo -n "::: Checking $1 is a repo..."
  595. cd "${1}" &> /dev/null || return 1
  596. $SUDO git status &> /dev/null && echo " OK!"; return 0 || echo " not found!"; return 1
  597. }
  598.  
  599. make_repo() {
  600. # Remove the non-repos interface and clone the interface
  601. echo -n "::: Cloning $2 into $1..."
  602. $SUDO rm -rf "${1}"
  603. $SUDO git clone -q "${2}" "${1}" > /dev/null & spinner $!
  604. if [ -z "${TESTING+x}" ]; then
  605. :
  606. else
  607. $SUDO git -C "${1}" checkout test
  608. fi
  609. echo " done!"
  610. }
  611.  
  612. update_repo() {
  613. if [[ "${reconfigure}" == true ]]; then
  614. echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
  615. else
  616. # Pull the latest commits
  617. echo -n "::: Updating repo in $1..."
  618. cd "${1}" || exit 1
  619. $SUDO git stash -q > /dev/null & spinner $!
  620. $SUDO git pull -q > /dev/null & spinner $!
  621. if [ -z "${TESTING+x}" ]; then
  622. :
  623. else
  624. ${SUDOE} git checkout test
  625. fi
  626. echo " done!"
  627. fi
  628. }
  629.  
  630. setCustomProto() {
  631. # Set the available protocols into an array so it can be used with a whiptail dialog
  632. if protocol=$(whiptail --title "Protocol" --radiolist \
  633. "Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP." ${r} ${c} 2 \
  634. "UDP" "" ON \
  635. "TCP" "" OFF 3>&1 1>&2 2>&3)
  636. then
  637. # Convert option into lowercase (UDP->udp)
  638. pivpnProto="${protocol,,}"
  639. echo "::: Using protocol: $pivpnProto"
  640. echo "${pivpnProto}" > /tmp/pivpnPROTO
  641. else
  642. echo "::: Cancel selected, exiting...."
  643. exit 1
  644. fi
  645. # write out the PROTO
  646. PROTO=$pivpnProto
  647. $SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
  648. }
  649.  
  650.  
  651. setCustomPort() {
  652. until [[ $PORTNumCorrect = True ]]
  653. do
  654. portInvalid="Invalid"
  655.  
  656. PROTO=$(cat /etc/pivpn/INSTALL_PROTO)
  657. if [ "$PROTO" = "udp" ]; then
  658. DEFAULT_PORT=1194
  659. else
  660. DEFAULT_PORT=443
  661. fi
  662. 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)
  663. then
  664. if [[ "$PORT" =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
  665. :
  666. else
  667. PORT=$portInvalid
  668. fi
  669. else
  670. echo "::: Cancel selected, exiting...."
  671. exit 1
  672. fi
  673.  
  674. if [[ $PORT == "$portInvalid" ]]; then
  675. 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}
  676. PORTNumCorrect=False
  677. else
  678. if (whiptail --backtitle "Specify Custom Port" --title "Confirm Custom Port Number" --yesno "Are these settings correct?\n PORT: $PORT" ${r} ${c}) then
  679. PORTNumCorrect=True
  680. else
  681. # If the settings are wrong, the loop continues
  682. PORTNumCorrect=False
  683. fi
  684. fi
  685. done
  686. # write out the port
  687. echo ${PORT} > /tmp/INSTALL_PORT
  688. $SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
  689. }
  690.  
  691. setClientDNS() {
  692. 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)
  693. DNSChooseOptions=(Google "" on
  694. OpenDNS "" off
  695. Level3 "" off
  696. DNS.WATCH "" off
  697. Norton "" off
  698. Custom "" off)
  699.  
  700. if DNSchoices=$("${DNSChoseCmd[@]}" "${DNSChooseOptions[@]}" 2>&1 >/dev/tty)
  701. then
  702. case ${DNSchoices} in
  703. Google)
  704. echo "::: Using Google DNS servers."
  705. OVPNDNS1="8.8.8.8"
  706. OVPNDNS2="8.8.4.4"
  707. # These are already in the file
  708. ;;
  709. OpenDNS)
  710. echo "::: Using OpenDNS servers."
  711. OVPNDNS1="208.67.222.222"
  712. OVPNDNS2="208.67.220.220"
  713. $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
  714. $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
  715. ;;
  716. Level3)
  717. echo "::: Using Level3 servers."
  718. OVPNDNS1="209.244.0.3"
  719. OVPNDNS2="209.244.0.4"
  720. $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
  721. $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
  722. ;;
  723. DNS.WATCH)
  724. echo "::: Using DNS.WATCH servers."
  725. OVPNDNS1="84.200.69.80"
  726. OVPNDNS2="84.200.70.40"
  727. $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
  728. $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
  729. ;;
  730. Norton)
  731. echo "::: Using Norton ConnectSafe servers."
  732. OVPNDNS1="199.85.126.10"
  733. OVPNDNS2="199.85.127.10"
  734. $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
  735. $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
  736. ;;
  737. Custom)
  738. until [[ $DNSSettingsCorrect = True ]]
  739. do
  740. strInvalid="Invalid"
  741.  
  742. if OVPNDNS=$(whiptail --backtitle "Specify Upstream DNS Provider(s)" --inputbox "Enter your desired upstream DNS provider(s), seperated by a comma.\n\nFor example '8.8.8.8, 8.8.4.4'" ${r} ${c} "" 3>&1 1>&2 2>&3)
  743. then
  744. OVPNDNS1=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$1}')
  745. OVPNDNS2=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$2}')
  746. if ! valid_ip "$OVPNDNS1" || [ ! "$OVPNDNS1" ]; then
  747. OVPNDNS1=$strInvalid
  748. fi
  749. if ! valid_ip "$OVPNDNS2" && [ "$OVPNDNS2" ]; then
  750. OVPNDNS2=$strInvalid
  751. fi
  752. else
  753. echo "::: Cancel selected, exiting...."
  754. exit 1
  755. fi
  756. if [[ $OVPNDNS1 == "$strInvalid" ]] || [[ $OVPNDNS2 == "$strInvalid" ]]; then
  757. 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}
  758. if [[ $OVPNDNS1 == "$strInvalid" ]]; then
  759. OVPNDNS1=""
  760. fi
  761. if [[ $OVPNDNS2 == "$strInvalid" ]]; then
  762. OVPNDNS2=""
  763. fi
  764. DNSSettingsCorrect=False
  765. else
  766. 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
  767. DNSSettingsCorrect=True
  768. $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
  769. if [ -z ${OVPNDNS2} ]; then
  770. $SUDO sed -i '/\(dhcp-option DNS \)/{n;N;d}' /etc/openvpn/server.conf
  771. else
  772. $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
  773. fi
  774. else
  775. # If the settings are wrong, the loop continues
  776. DNSSettingsCorrect=False
  777. fi
  778. fi
  779. done
  780. ;;
  781. esac
  782. else
  783. echo "::: Cancel selected. Exiting..."
  784. exit 1
  785. fi
  786. }
  787.  
  788. confOpenVPN() {
  789. # Generate a random, alphanumeric identifier of 16 characters for this server so that we can use verify-x509-name later that is unique for this server installation. Source: Earthgecko (https://gist.github.com/earthgecko/3089509)
  790. NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
  791. SERVER_NAME="server_${NEW_UUID}"
  792.  
  793. if [[ ${useUpdateVars} == false ]]; then
  794. # Ask user for desired level of encryption
  795. ENCRYPT=$(whiptail --backtitle "Setup OpenVPN" --title "Encryption strength" --radiolist \
  796. "Choose your desired level of encryption (press space to select):\n This is an encryption key that will be generated on your system. The larger the key, 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 \
  797. "1024" "Use 1024-bit encryption (testing only)" OFF \
  798. "2048" "Use 2048-bit encryption (recommended level)" ON \
  799. "4096" "Use 4096-bit encryption (paranoid level)" OFF 3>&1 1>&2 2>&3)
  800.  
  801. exitstatus=$?
  802. if [ $exitstatus != 0 ]; then
  803. echo "::: Cancel selected. Exiting..."
  804. exit 1
  805. fi
  806. fi
  807.  
  808. # If easy-rsa exists, remove it
  809. if [[ -d /etc/openvpn/easy-rsa/ ]]; then
  810. $SUDO rm -rf /etc/openvpn/easy-rsa/
  811. fi
  812.  
  813. # Get the PiVPN easy-rsa
  814. wget -q -O - "${easyrsaRel}" | $SUDO tar xz -C /etc/openvpn && $SUDO mv /etc/openvpn/EasyRSA-${easyrsaVer} /etc/openvpn/easy-rsa
  815. # fix ownership
  816. $SUDO chown -R root:root /etc/openvpn/easy-rsa
  817. $SUDO mkdir /etc/openvpn/easy-rsa/pki
  818.  
  819. # Write out new vars file
  820. set +e
  821. IFS= read -d '' String <<"EOF"
  822. if [ -z "$EASYRSA_CALLER" ]; then
  823. echo "Nope." >&2
  824. return 1
  825. fi
  826. set_var EASYRSA "/etc/openvpn/easy-rsa"
  827. set_var EASYRSA_PKI "$EASYRSA/pki"
  828. set_var EASYRSA_KEY_SIZE 2048
  829. set_var EASYRSA_ALGO rsa
  830. set_var EASYRSA_CURVE secp384r1
  831. EOF
  832.  
  833. echo "${String}" | $SUDO tee /etc/openvpn/easy-rsa/vars >/dev/null
  834. set -e
  835.  
  836. # Edit the KEY_SIZE variable in the vars file to set user chosen key size
  837. cd /etc/openvpn/easy-rsa || exit
  838. $SUDO sed -i "s/\(KEY_SIZE\).*/\1 ${ENCRYPT}/" vars
  839.  
  840. # Remove any previous keys
  841. ${SUDOE} ./easyrsa --batch init-pki
  842.  
  843. # Build the certificate authority
  844. printf "::: Building CA...\n"
  845. ${SUDOE} ./easyrsa --batch build-ca nopass
  846. printf "\n::: CA Complete.\n"
  847.  
  848. if [[ ${useUpdateVars} == false ]]; then
  849. whiptail --msgbox --backtitle "Setup OpenVPN" --title "Server Information" "The server key, Diffie-Hellman key, and HMAC key will now be generated." ${r} ${c}
  850. fi
  851.  
  852. # Build the server
  853. ${SUDOE} ./easyrsa build-server-full ${SERVER_NAME} nopass
  854.  
  855. if [[ ${useUpdateVars} == false ]]; then
  856. if (whiptail --backtitle "Setup OpenVPN" --title "Version 2.4 improvements" --yesno --defaultno "OpenVPN 2.4 brings support for stronger key exchange using Elliptic Curves and encrypted control channel, along with faster LZ4 compression.\n\nIf you 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
  857. APPLY_TWO_POINT_FOUR=true
  858. $SUDO touch /etc/pivpn/TWO_POINT_FOUR
  859. else
  860. APPLY_TWO_POINT_FOUR=false
  861. fi
  862. fi
  863.  
  864. if [[ ${useUpdateVars} == false ]]; then
  865. if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
  866. 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 a pool of the last 128 generated.\nMore information about this service can be found here: https://2ton.com.au/dhtool/\n\nIf you're paranoid, choose 'No' and Diffie-Hellman parameters will be generated on your device." ${r} ${c}); then
  867. DOWNLOAD_DH_PARAM=true
  868. else
  869. DOWNLOAD_DH_PARAM=false
  870. fi
  871. fi
  872. fi
  873.  
  874. if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
  875. if [ "$ENCRYPT" -ge "4096" ] && [[ ${DOWNLOAD_DH_PARAM} == true ]]; then
  876. # Downloading parameters
  877. RANDOM_INDEX=$(( RANDOM % 128 ))
  878. ${SUDOE} curl "https://2ton.com.au/dhparam/${ENCRYPT}/${RANDOM_INDEX}" -o "/etc/openvpn/easy-rsa/pki/dh${ENCRYPT}.pem"
  879. else
  880. # Generate Diffie-Hellman key exchange
  881. ${SUDOE} ./easyrsa gen-dh
  882. ${SUDOE} mv pki/dh.pem pki/dh${ENCRYPT}.pem
  883. fi
  884. fi
  885.  
  886. # Generate static HMAC key to defend against DDoS
  887. ${SUDOE} openvpn --genkey --secret pki/ta.key
  888.  
  889. # Generate an empty Certificate Revocation List
  890. ${SUDOE} ./easyrsa gen-crl
  891. ${SUDOE} cp pki/crl.pem /etc/openvpn/crl.pem
  892. ${SUDOE} chown nobody:nogroup /etc/openvpn/crl.pem
  893.  
  894. # Write config file for server using the template .txt file
  895. $SUDO cp /etc/.pivpn/server_config.txt /etc/openvpn/server.conf
  896.  
  897. if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
  898. #If they enabled 2.4 change compression algorithm and use tls-crypt instead of tls-auth to encrypt control channel
  899. $SUDO sed -i "s/comp-lzo/compress lz4/" /etc/openvpn/server.conf
  900. $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
  901. fi
  902.  
  903. if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
  904. #If they enabled 2.4 disable dh parameters
  905. $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/dh none/" /etc/openvpn/server.conf
  906. else
  907. # Otherwise set the user encryption key size
  908. $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/\1${ENCRYPT}.pem/" /etc/openvpn/server.conf
  909. fi
  910.  
  911. # if they modified port put value in server.conf
  912. if [ $PORT != 1194 ]; then
  913. $SUDO sed -i "s/1194/${PORT}/g" /etc/openvpn/server.conf
  914. fi
  915.  
  916. # if they modified protocol put value in server.conf
  917. if [ "$PROTO" != "udp" ]; then
  918. $SUDO sed -i "s/proto udp/proto tcp/g" /etc/openvpn/server.conf
  919. fi
  920.  
  921. # write out server certs to conf file
  922. $SUDO sed -i "s/\(key \/etc\/openvpn\/easy-rsa\/pki\/private\/\).*/\1${SERVER_NAME}.key/" /etc/openvpn/server.conf
  923. $SUDO sed -i "s/\(cert \/etc\/openvpn\/easy-rsa\/pki\/issued\/\).*/\1${SERVER_NAME}.crt/" /etc/openvpn/server.conf
  924. }
  925.  
  926. confUnattendedUpgrades() {
  927. if [[ $UNATTUPG == "unattended-upgrades" ]]; then
  928. $SUDO apt-get --yes --quiet --no-install-recommends install "$UNATTUPG" > /dev/null & spinner $!
  929. if [[ $PLAT == "Ubuntu" ]]; then
  930. # Ubuntu 50unattended-upgrades should already just have security enabled
  931. # so we just need to configure the 10periodic file
  932. cat << EOT | $SUDO tee /etc/apt/apt.conf.d/10periodic >/dev/null
  933. APT::Periodic::Update-Package-Lists "1";
  934. APT::Periodic::Download-Upgradeable-Packages "1";
  935. APT::Periodic::AutocleanInterval "5";
  936. APT::Periodic::Unattended-Upgrade "1";
  937. EOT
  938. else
  939. $SUDO sed -i '/\(o=Raspbian,n=jessie\)/c\"o=Raspbian,n=jessie,l=Raspbian-Security";\' /etc/apt/apt.conf.d/50unattended-upgrades
  940. cat << EOT | $SUDO tee /etc/apt/apt.conf.d/02periodic >/dev/null
  941. APT::Periodic::Enable "1";
  942. APT::Periodic::Update-Package-Lists "1";
  943. APT::Periodic::Download-Upgradeable-Packages "1";
  944. APT::Periodic::Unattended-Upgrade "1";
  945. APT::Periodic::AutocleanInterval "7";
  946. APT::Periodic::Verbose "0";
  947. EOT
  948. fi
  949. fi
  950.  
  951. }
  952.  
  953. confNetwork() {
  954. # Enable forwarding of internet traffic
  955. $SUDO sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
  956. $SUDO sysctl -p
  957.  
  958. # if ufw enabled, configure that
  959. if hash ufw 2>/dev/null; then
  960. if $SUDO ufw status | grep -q inactive
  961. then
  962. noUFW=1
  963. else
  964. echo "::: Detected UFW is enabled."
  965. echo "::: Adding UFW rules..."
  966. $SUDO cp /etc/.pivpn/ufw_add.txt /tmp/ufw_add.txt
  967. $SUDO sed -i 's/IPv4dev/'"$IPv4dev"'/' /tmp/ufw_add.txt
  968. $SUDO sed -i "s/\(DEFAULT_FORWARD_POLICY=\).*/\1\"ACCEPT\"/" /etc/default/ufw
  969. $SUDO sed -i -e '/delete these required/r /tmp/ufw_add.txt' -e//N /etc/ufw/before.rules
  970. $SUDO ufw allow "${PORT}/${PROTO}"
  971. $SUDO ufw allow from 10.8.0.0/24
  972. $SUDO ufw reload
  973. echo "::: UFW configuration completed."
  974. fi
  975. else
  976. noUFW=1
  977. fi
  978. # else configure iptables
  979. if [[ $noUFW -eq 1 ]]; then
  980. echo 1 > /tmp/noUFW
  981. $SUDO iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o "$IPv4dev" -j MASQUERADE
  982. case ${PLAT} in
  983. Ubuntu|Debian|Devuan)
  984. $SUDO iptables-save | $SUDO tee /etc/iptables/rules.v4 > /dev/null
  985. ;;
  986. *)
  987. $SUDO netfilter-persistent save
  988. ;;
  989. esac
  990. else
  991. echo 0 > /tmp/noUFW
  992. fi
  993.  
  994. $SUDO cp /tmp/noUFW /etc/pivpn/NO_UFW
  995. }
  996.  
  997. confOVPN() {
  998. if ! IPv4pub=$(dig +short myip.opendns.com @resolver1.opendns.com)
  999. then
  1000. echo "dig failed, now trying to curl eth0.me"
  1001. if ! IPv4pub=$(curl eth0.me)
  1002. then
  1003. echo "eth0.me failed, please check your internet connection/DNS"
  1004. exit $?
  1005. fi
  1006. fi
  1007. $SUDO cp /tmp/pivpnUSR /etc/pivpn/INSTALL_USER
  1008. $SUDO cp /tmp/DET_PLATFORM /etc/pivpn/DET_PLATFORM
  1009.  
  1010. $SUDO cp /etc/.pivpn/Default.txt /etc/openvpn/easy-rsa/pki/Default.txt
  1011.  
  1012. if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
  1013. #If they enabled 2.4 change compression algorithm and remove key-direction options since it's not required
  1014. $SUDO sed -i "s/comp-lzo/compress lz4/" /etc/openvpn/easy-rsa/pki/Default.txt
  1015. $SUDO sed -i "/key-direction 1/d" /etc/openvpn/easy-rsa/pki/Default.txt
  1016. fi
  1017.  
  1018. if [[ ${useUpdateVars} == false ]]; then
  1019. 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 \
  1020. "$IPv4pub" "Use this public IP" "ON" \
  1021. "DNS Entry" "Use a public DNS" "OFF" 3>&1 1>&2 2>&3)
  1022.  
  1023. exitstatus=$?
  1024. if [ $exitstatus != 0 ]; then
  1025. echo "::: Cancel selected. Exiting..."
  1026. exit 1
  1027. fi
  1028.  
  1029. if [ "$METH" == "$IPv4pub" ]; then
  1030. $SUDO sed -i 's/IPv4pub/'"$IPv4pub"'/' /etc/openvpn/easy-rsa/pki/Default.txt
  1031. else
  1032. until [[ $publicDNSCorrect = True ]]
  1033. do
  1034. PUBLICDNS=$(whiptail --title "PiVPN Setup" --inputbox "What is the public DNS name of this Server?" ${r} ${c} 3>&1 1>&2 2>&3)
  1035. exitstatus=$?
  1036. if [ $exitstatus != 0 ]; then
  1037. echo "::: Cancel selected. Exiting..."
  1038. exit 1
  1039. fi
  1040. if (whiptail --backtitle "Confirm DNS Name" --title "Confirm DNS Name" --yesno "Is this correct?\n\n Public DNS Name: $PUBLICDNS" ${r} ${c}) then
  1041. publicDNSCorrect=True
  1042. $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
  1043. else
  1044. publicDNSCorrect=False
  1045. fi
  1046. done
  1047. fi
  1048. else
  1049. $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
  1050. fi
  1051.  
  1052. # if they modified port put value in Default.txt for clients to use
  1053. if [ $PORT != 1194 ]; then
  1054. $SUDO sed -i -e "s/1194/${PORT}/g" /etc/openvpn/easy-rsa/pki/Default.txt
  1055. fi
  1056.  
  1057. # if they modified protocol put value in Default.txt for clients to use
  1058. if [ "$PROTO" != "udp" ]; then
  1059. $SUDO sed -i -e "s/proto udp/proto tcp/g" /etc/openvpn/easy-rsa/pki/Default.txt
  1060. fi
  1061.  
  1062. # verify server name to strengthen security
  1063. $SUDO sed -i "s/SRVRNAME/${SERVER_NAME}/" /etc/openvpn/easy-rsa/pki/Default.txt
  1064.  
  1065. if [ ! -d "/home/$pivpnUser/ovpns" ]; then
  1066. $SUDO mkdir "/home/$pivpnUser/ovpns"
  1067. fi
  1068. $SUDO chmod 0777 -R "/home/$pivpnUser/ovpns"
  1069. }
  1070.  
  1071. confLogging(){
  1072. # Tell rsyslog to log openvpn messages to a specific file
  1073. cat << 'EOT' | $SUDO tee /etc/rsyslog.d/30-openvpn.conf >/dev/null
  1074. if $programname == 'ovpn-server' then /var/log/openvpn.log
  1075. if $programname == 'ovpn-server' then ~
  1076. EOT
  1077.  
  1078. # Enable log rotation, it rotates weekly and keeps the current log and the previous uncompressed, with the older 4 compressed
  1079. cat << 'EOT' | $SUDO tee /etc/logrotate.d/openvpn >/dev/null
  1080. /var/log/openvpn.log
  1081. {
  1082. rotate 4
  1083. weekly
  1084. missingok
  1085. notifempty
  1086. compress
  1087. delaycompress
  1088. sharedscripts
  1089. postrotate
  1090. invoke-rc.d rsyslog rotate >/dev/null 2>&1 || true
  1091. endscript
  1092. }
  1093. EOT
  1094.  
  1095. # Restart the logging service
  1096. case ${PLAT} in
  1097. Ubuntu|Debian|*vuan)
  1098. $SUDO service rsyslog restart || true
  1099. ;;
  1100. *)
  1101. $SUDO systemctl restart rsyslog.service || true
  1102. ;;
  1103. esac
  1104.  
  1105. }
  1106.  
  1107. finalExports() {
  1108. # Update variables in setupVars.conf file
  1109. if [ -e "${setupVars}" ]; then
  1110. sed -i.update.bak '/pivpnUser/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}"
  1111. fi
  1112. {
  1113. echo "pivpnUser=${pivpnUser}"
  1114. echo "UNATTUPG=${UNATTUPG}"
  1115. echo "pivpnInterface=${pivpnInterface}"
  1116. echo "IPv4dns=${IPv4dns}"
  1117. echo "IPv4addr=${IPv4addr}"
  1118. echo "IPv4gw=${IPv4gw}"
  1119. echo "pivpnProto=${pivpnProto}"
  1120. echo "PORT=${PORT}"
  1121. echo "ENCRYPT=${ENCRYPT}"
  1122. echo "APPLY_TWO_POINT_FOUR"="${APPLY_TWO_POINT_FOUR}"
  1123. echo "DOWNLOAD_DH_PARAM=${DOWNLOAD_DH_PARAM}"
  1124. echo "PUBLICDNS=${PUBLICDNS}"
  1125. echo "OVPNDNS1=${OVPNDNS1}"
  1126. echo "OVPNDNS2=${OVPNDNS2}"
  1127. }>> "${setupVars}"
  1128. }
  1129.  
  1130.  
  1131. # I suggest replacing some of these names.
  1132.  
  1133. #accountForRefactor() {
  1134. # # At some point in the future this list can be pruned, for now we'll need it to ensure updates don't break.
  1135. #
  1136. # # Refactoring of install script has changed the name of a couple of variables. Sort them out here.
  1137. # sed -i 's/pivpnUser/PIVPN_USER/g' ${setupVars}
  1138. # #sed -i 's/UNATTUPG/UNATTUPG/g' ${setupVars}
  1139. # sed -i 's/pivpnInterface/PIVPN_INTERFACE/g' ${setupVars}
  1140. # sed -i 's/IPv4dns/IPV4_DNS/g' ${setupVars}
  1141. # sed -i 's/IPv4addr/IPV4_ADDRESS/g' ${setupVars}
  1142. # sed -i 's/IPv4gw/IPV4_GATEWAY/g' ${setupVars}
  1143. # sed -i 's/pivpnProto/TRANSPORT_LAYER/g' ${setupVars}
  1144. # #sed -i 's/PORT/PORT/g' ${setupVars}
  1145. # #sed -i 's/ENCRYPT/ENCRYPT/g' ${setupVars}
  1146. # #sed -i 's/DOWNLOAD_DH_PARAM/DOWNLOAD_DH_PARAM/g' ${setupVars}
  1147. # sed -i 's/PUBLICDNS/PUBLIC_DNS/g' ${setupVars}
  1148. # sed -i 's/OVPNDNS1/OVPN_DNS_1/g' ${setupVars}
  1149. # sed -i 's/OVPNDNS2/OVPN_DNS_2/g' ${setupVars}
  1150. #}
  1151.  
  1152. installPiVPN() {
  1153. stopServices
  1154. $SUDO mkdir -p /etc/pivpn/
  1155. confUnattendedUpgrades
  1156. installScripts
  1157. setCustomProto
  1158. setCustomPort
  1159. confOpenVPN
  1160. confNetwork
  1161. confOVPN
  1162. setClientDNS
  1163. confLogging
  1164. finalExports
  1165. }
  1166.  
  1167. updatePiVPN() {
  1168. #accountForRefactor
  1169. stopServices
  1170. confUnattendedUpgrades
  1171. installScripts
  1172.  
  1173. # setCustomProto
  1174. # write out the PROTO
  1175. PROTO=$pivpnProto
  1176. $SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
  1177.  
  1178. #setCustomPort
  1179. # write out the port
  1180. $SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
  1181.  
  1182. confOpenVPN
  1183. confNetwork
  1184. confOVPN
  1185.  
  1186. # ?? Is this always OK? Also if you only select one DNS server ??
  1187. $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
  1188. $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
  1189.  
  1190. finalExports #re-export setupVars.conf to account for any new vars added in new versions
  1191. }
  1192.  
  1193.  
  1194. displayFinalMessage() {
  1195. # Final completion message to user
  1196. whiptail --msgbox --backtitle "Make it so." --title "Installation Complete!" "Now run 'pivpn add' to create the ovpn profiles.
  1197. Run 'pivpn help' to see what else you can do!
  1198. The install log is in /etc/pivpn." ${r} ${c}
  1199. if (whiptail --title "Reboot" --yesno --defaultno "It is strongly recommended you reboot after installation. Would you like to reboot now?" ${r} ${c}); then
  1200. whiptail --title "Rebooting" --msgbox "The system will now reboot." ${r} ${c}
  1201. printf "\nRebooting system...\n"
  1202. $SUDO sleep 3
  1203. $SUDO shutdown -r now
  1204. fi
  1205. }
  1206.  
  1207. update_dialogs() {
  1208. # reconfigure
  1209. if [ "${reconfigure}" = true ]; then
  1210. opt1a="Repair"
  1211. opt1b="This will retain existing settings"
  1212. strAdd="You will remain on the same version"
  1213. else
  1214. opt1a="Update"
  1215. opt1b="This will retain existing settings."
  1216. strAdd="You will be updated to the latest version."
  1217. fi
  1218. opt2a="Reconfigure"
  1219. opt2b="This will allow you to enter new settings"
  1220.  
  1221. 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 \
  1222. "${opt1a}" "${opt1b}" \
  1223. "${opt2a}" "${opt2b}" 3>&2 2>&1 1>&3) || \
  1224. { echo "::: Cancel selected. Exiting"; exit 1; }
  1225.  
  1226. case ${UpdateCmd} in
  1227. ${opt1a})
  1228. echo "::: ${opt1a} option selected."
  1229. useUpdateVars=true
  1230. ;;
  1231. ${opt2a})
  1232. echo "::: ${opt2a} option selected"
  1233. useUpdateVars=false
  1234. ;;
  1235. esac
  1236. }
  1237.  
  1238. clone_or_update_repos() {
  1239. if [[ "${reconfigure}" == true ]]; then
  1240. echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
  1241. else
  1242. # Get Git files
  1243. getGitFiles ${pivpnFilesDir} ${pivpnGitUrl} || \
  1244. { echo "!!! Unable to clone ${pivpnGitUrl} into ${pivpnFilesDir}, unable to continue."; \
  1245. exit 1; \
  1246. }
  1247. fi
  1248. }
  1249.  
  1250. ######## SCRIPT ############
  1251.  
  1252. main() {
  1253.  
  1254. ######## FIRST CHECK ########
  1255. # Must be root to install
  1256. echo ":::"
  1257. if [[ $EUID -eq 0 ]];then
  1258. echo "::: You are root."
  1259. else
  1260. echo "::: sudo will be used for the install."
  1261. # Check if it is actually installed
  1262. # If it isn't, exit because the install cannot complete
  1263. if [[ $(dpkg-query -s sudo) ]];then
  1264. export SUDO="sudo"
  1265. export SUDOE="sudo -E"
  1266. else
  1267. echo "::: Please install sudo or run this as root."
  1268. exit 1
  1269. fi
  1270. fi
  1271.  
  1272. # Check for supported distribution
  1273. distro_check
  1274.  
  1275. # Check arguments for the undocumented flags
  1276. for var in "$@"; do
  1277. case "$var" in
  1278. "--reconfigure" ) reconfigure=true;;
  1279. "--i_do_not_follow_recommendations" ) skipSpaceCheck=false;;
  1280. "--unattended" ) runUnattended=true;;
  1281. esac
  1282. done
  1283.  
  1284. if [[ -f ${setupVars} ]]; then
  1285. if [[ "${runUnattended}" == true ]]; then
  1286. echo "::: --unattended passed to install script, no whiptail dialogs will be displayed"
  1287. useUpdateVars=true
  1288. else
  1289. update_dialogs
  1290. fi
  1291. fi
  1292.  
  1293. # Start the installer
  1294. # Verify there is enough disk space for the install
  1295. if [[ "${skipSpaceCheck}" == true ]]; then
  1296. echo "::: --i_do_not_follow_recommendations passed to script, skipping free disk space verification!"
  1297. else
  1298. verifyFreeDiskSpace
  1299. fi
  1300.  
  1301. # Install the packages (we do this first because we need whiptail)
  1302. #checkForDependencies
  1303. update_package_cache
  1304.  
  1305. # Notify user of package availability
  1306. notify_package_updates_available
  1307.  
  1308. # Install packages used by this installation script
  1309. install_dependent_packages PIVPN_DEPS[@]
  1310.  
  1311. if [[ ${useUpdateVars} == false ]]; then
  1312. # Display welcome dialogs
  1313. welcomeDialogs
  1314.  
  1315. # Find interfaces and let the user choose one
  1316. chooseInterface
  1317.  
  1318. # Only try to set static on Raspbian, otherwise let user do it
  1319. if [[ $PLAT != "Raspbian" ]]; then
  1320. avoidStaticIPv4Ubuntu
  1321. else
  1322. getStaticIPv4Settings
  1323. setStaticIPv4
  1324. fi
  1325.  
  1326. # Choose the user for the ovpns
  1327. chooseUser
  1328.  
  1329. # Ask if unattended-upgrades will be enabled
  1330. unattendedUpgrades
  1331.  
  1332. # Clone/Update the repos
  1333. clone_or_update_repos
  1334.  
  1335. # Install and log everything to a file
  1336. installPiVPN | tee ${tmpLog}
  1337.  
  1338. echo "::: Install Complete..."
  1339. else
  1340. # Source ${setupVars} for use in the rest of the functions.
  1341. source ${setupVars}
  1342.  
  1343. echo "::: Using IP address: $IPv4addr"
  1344. echo "${IPv4addr%/*}" > /tmp/pivpnIP
  1345. echo "::: Using interface: $pivpnInterface"
  1346. echo "${pivpnInterface}" > /tmp/pivpnINT
  1347. echo "::: Using User: $pivpnUser"
  1348. echo "${pivpnUser}" > /tmp/pivpnUSR
  1349. echo "::: Using protocol: $pivpnProto"
  1350. echo "${pivpnProto}" > /tmp/pivpnPROTO
  1351. echo "::: Using port: $PORT"
  1352. echo ${PORT} > /tmp/INSTALL_PORT
  1353. echo ":::"
  1354.  
  1355. # Only try to set static on Raspbian
  1356. case ${PLAT} in
  1357. Rasp*)
  1358. 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
  1359. ;;
  1360. *)
  1361. echo "::: IP Information"
  1362. echo "::: Since we think you are not using Raspbian, we will not configure a static IP for you."
  1363. echo "::: If you are in Amazon then you can not configure a static IP anyway."
  1364. echo "::: Just ensure before this installer started you had set an elastic IP on your instance."
  1365. ;;
  1366. esac
  1367.  
  1368. # Clone/Update the repos
  1369. clone_or_update_repos
  1370.  
  1371.  
  1372. updatePiVPN | tee ${tmpLog}
  1373. fi
  1374.  
  1375. #Move the install log into /etc/pivpn for storage
  1376. $SUDO mv ${tmpLog} ${instalLogLoc}
  1377.  
  1378. echo "::: Restarting services..."
  1379. # Start services
  1380. case ${PLAT} in
  1381. Ubuntu|Debian|*vuan)
  1382. $SUDO service openvpn start
  1383. ;;
  1384. *)
  1385. $SUDO systemctl enable openvpn.service
  1386. $SUDO systemctl start openvpn.service
  1387. ;;
  1388. esac
  1389.  
  1390. echo "::: done."
  1391.  
  1392. if [[ "${useUpdateVars}" == false ]]; then
  1393. displayFinalMessage
  1394. fi
  1395.  
  1396. echo ":::"
  1397. if [[ "${useUpdateVars}" == false ]]; then
  1398. echo "::: Installation Complete!"
  1399. echo "::: Now run 'pivpn add' to create an ovpn profile for each of your devices."
  1400. echo "::: Run 'pivpn help' to see what else you can do!"
  1401. echo "::: It is strongly recommended you reboot after installation."
  1402. else
  1403. echo "::: Update complete!"
  1404. fi
  1405.  
  1406. echo ":::"
  1407. echo "::: The install log is located at: ${instalLogLoc}"
  1408. }
  1409.  
  1410. if [[ "${PIVPN_TEST}" != true ]] ; then
  1411. main "$@"
  1412. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement