From 4e4432aa4ea1901f035a7659f20e0dbe796bdf6e Mon Sep 17 00:00:00 2001 From: Henrik Bengtsson Date: Mon, 20 May 2024 07:11:48 -0700 Subject: [PATCH] Now 'ucsf vpn --flavor=...' asserts that generic hook scripts are installed --- README.md | 2 +- bin/ucsf-vpn | 92 ++++++++++++++++++++++++++++------------- src/incl/openconnect.sh | 14 +++++-- src/ucsf-vpn.sh | 78 +++++++++++++++++++++++----------- 4 files changed, 129 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 5a3e398..b4281a1 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ Useful resources: * UCSF Managing Your Passwords: - https://it.ucsf.edu/services/managing-your-passwords -Version: 5.8.0-9009 +Version: 5.8.0-9010 Copyright: Henrik Bengtsson (2016-2024) License: GPL (>= 2.1) [https://www.gnu.org/licenses/gpl.html] Source: https://github.com/HenrikBengtsson/ucsf-vpn diff --git a/bin/ucsf-vpn b/bin/ucsf-vpn index 2cf1d56..fca61c9 100755 --- a/bin/ucsf-vpn +++ b/bin/ucsf-vpn @@ -110,7 +110,7 @@ ### * UCSF Managing Your Passwords: ### - https://it.ucsf.edu/services/managing-your-passwords ### -### Version: 5.8.0-9009 +### Version: 5.8.0-9010 ### Copyright: Henrik Bengtsson (2016-2024) ### License: GPL (>= 2.1) [https://www.gnu.org/licenses/gpl.html] ### Source: https://github.com/HenrikBengtsson/ucsf-vpn @@ -851,9 +851,17 @@ function openconnect_start() { minfo "Preparing to connect to VPN server '$server'" - ## Assert that --flavor= exists, if specified - flavor_home > /dev/null - + if [[ -n ${flavor} ]]; then + echo "Are vpnc scripts installed?" + if ! install_vpnc "check"; then + merror "Generic ucsf-vpn hook scripts not installed. Please install with 'ucsf vpn install-vpnc'" + fi + echo "Are vpnc scripts installed? done" + + ## Assert that --flavor= exists, if specified + flavor_home > /dev/null + fi + assert_sudo "start" ## Load user credentials from file? @@ -1264,7 +1272,7 @@ function flavor_home() { if [[ "${count}" -eq 0 ]]; then merror "Flavor folder contains no known hook script files: ${path}" fi - + echo "${path}" } @@ -1332,42 +1340,70 @@ HOOK_SCRIPT_EOF } function install_vpnc() { - local file filename dest hooks_dir dir path + local action file filename dest hooks_dir dir path + action=${1:-install} - file="$(mktemp -d)/ucsf-vpn-flavors.sh" - ucsf-vpn-flavors_code > "${file}" - + mdebug "install_vpnc() ..." + mdebug " - action: ${action}" + + ## Locate hooks directory find_vpnc-script > /dev/null hooks_dir=$(find_hooks_dir) - - mdebug "install_vpnc() ..." mdebug " - hooks folder: ${hooks_dir}" - mdebug " - template: ${file}" - - assert_sudo "install-vpnc" - sudo mkdir -p "${hooks_dir}" - [[ -d "${hooks_dir}" ]] || merror "Failed to create directory: ${hooks_dir}" + filename="ucsf-vpn-flavors.sh" - filename=$(basename "${file}") + ## Is ucsf-vpn hook script already installed? dest="${hooks_dir}/${filename}" - sudo cp "${file}" "${dest}" - sudo chmod ugo+r "${dest}" - [[ -f "${dest}" ]] || merror "Failed to create file: ${dest}" - mok "Copied generic hook script: ${dest}" + if [[ $action == "check" ]] && [[ ! -f "${dest}" ]]; then + return 1 + fi + + if $force || [[ ! -f "${dest}" ]]; then + file="$(mktemp -d)/${filename}" + ucsf-vpn-flavors_code > "${file}" + mdebug " - template: ${file}" + assert_sudo "install-vpnc" + + ## Create hooks folder, if missing + if [[ ! -d "${hooks_dir}" ]]; then + sudo mkdir -p "${hooks_dir}" + [[ -d "${hooks_dir}" ]] || merror "Failed to create directory: ${hooks_dir}" + fi + + sudo cp "${file}" "${dest}" + sudo chmod ugo+r "${dest}" + [[ -f "${dest}" ]] || merror "Failed to create file: ${dest}" + mok "Generic hook script added: ${dest}" + if [[ -f "${file}" ]]; then + rm "${file}" + fi + else + mok "Generic hook script already exists: ${dest}" + fi + ## Install symbolic links to ucsf-vpn hook script, if missing for dir in pre-init connect post-connect disconnect post-disconnect attempt-reconnect post-attempt-reconnect reconnect; do path=${hooks_dir}/${dir}.d - sudo mkdir -p "${path}" - [[ -d "${path}" ]] || merror "Failed to create directory: ${path}" dest="${path}/${filename}" - sudo ln -fs "${hooks_dir}/${filename}" "${dest}" - [[ -L "${dest}" ]] || merror "Failed to create symbol link: ${dest} -> ${hooks_dir}/${filename}" - mok "Added symbolic link: ${dest} -> ${hooks_dir}/${filename}" + if [[ $action == "check" ]] && [[ ! -L "${dest}" ]]; then + return 1 + fi + if $force || [[ ! -L "${dest}" ]]; then + assert_sudo "install-vpnc" + sudo mkdir -p "${path}" + [[ -d "${path}" ]] || merror "Failed to create directory: ${path}" + sudo ln -fs "${hooks_dir}/${filename}" "${dest}" + [[ -L "${dest}" ]] || merror "Failed to create symbol link: ${dest} -> ${hooks_dir}/${filename}" + mok "Symbolic link added: ${dest} -> ${hooks_dir}/${filename}" + else + mok "Symbolic link already exists: ${dest} -> ${hooks_dir}/${filename}" + fi done - rm "${file}" mdebug "install_vpnc() ... done" + + return 0 } @@ -1714,7 +1750,7 @@ elif [[ $action == "routing" ]]; then routing_details _exit $? elif [[ $action == "install-vpnc" ]]; then - install_vpnc + install_vpnc "install" _exit $? elif [[ $action == "start" ]]; then openconnect_start diff --git a/src/incl/openconnect.sh b/src/incl/openconnect.sh index e4eb004..216b5f7 100755 --- a/src/incl/openconnect.sh +++ b/src/incl/openconnect.sh @@ -75,9 +75,17 @@ function openconnect_start() { minfo "Preparing to connect to VPN server '$server'" - ## Assert that --flavor= exists, if specified - flavor_home > /dev/null - + if [[ -n ${flavor} ]]; then + echo "Are vpnc scripts installed?" + if ! install_vpnc "check"; then + merror "Generic ucsf-vpn hook scripts not installed. Please install with 'ucsf vpn install-vpnc'" + fi + echo "Are vpnc scripts installed? done" + + ## Assert that --flavor= exists, if specified + flavor_home > /dev/null + fi + assert_sudo "start" ## Load user credentials from file? diff --git a/src/ucsf-vpn.sh b/src/ucsf-vpn.sh index e0b999a..379e25c 100755 --- a/src/ucsf-vpn.sh +++ b/src/ucsf-vpn.sh @@ -107,7 +107,7 @@ ### * UCSF Managing Your Passwords: ### - https://it.ucsf.edu/services/managing-your-passwords ### -### Version: 5.8.0-9009 +### Version: 5.8.0-9011 ### Copyright: Henrik Bengtsson (2016-2024) ### License: GPL (>= 2.1) [https://www.gnu.org/licenses/gpl.html] ### Source: https://github.com/HenrikBengtsson/ucsf-vpn @@ -347,7 +347,7 @@ function flavor_home() { if [[ "${count}" -eq 0 ]]; then merror "Flavor folder contains no known hook script files: ${path}" fi - + echo "${path}" } @@ -375,42 +375,70 @@ function ucsf-vpn-flavors_code() { } function install_vpnc() { - local file filename dest hooks_dir dir path + local action file filename dest hooks_dir dir path + action=${1:-install} - file="$(mktemp -d)/ucsf-vpn-flavors.sh" - ucsf-vpn-flavors_code > "${file}" - + mdebug "install_vpnc() ..." + mdebug " - action: ${action}" + + ## Locate hooks directory find_vpnc-script > /dev/null hooks_dir=$(find_hooks_dir) - - mdebug "install_vpnc() ..." mdebug " - hooks folder: ${hooks_dir}" - mdebug " - template: ${file}" - - assert_sudo "install-vpnc" - sudo mkdir -p "${hooks_dir}" - [[ -d "${hooks_dir}" ]] || merror "Failed to create directory: ${hooks_dir}" + filename="ucsf-vpn-flavors.sh" - filename=$(basename "${file}") + ## Is ucsf-vpn hook script already installed? dest="${hooks_dir}/${filename}" - sudo cp "${file}" "${dest}" - sudo chmod ugo+r "${dest}" - [[ -f "${dest}" ]] || merror "Failed to create file: ${dest}" - mok "Copied generic hook script: ${dest}" + if [[ $action == "check" ]] && [[ ! -f "${dest}" ]]; then + return 1 + fi + + if $force || [[ ! -f "${dest}" ]]; then + file="$(mktemp -d)/${filename}" + ucsf-vpn-flavors_code > "${file}" + mdebug " - template: ${file}" + assert_sudo "install-vpnc" + + ## Create hooks folder, if missing + if [[ ! -d "${hooks_dir}" ]]; then + sudo mkdir -p "${hooks_dir}" + [[ -d "${hooks_dir}" ]] || merror "Failed to create directory: ${hooks_dir}" + fi + + sudo cp "${file}" "${dest}" + sudo chmod ugo+r "${dest}" + [[ -f "${dest}" ]] || merror "Failed to create file: ${dest}" + mok "Generic hook script added: ${dest}" + if [[ -f "${file}" ]]; then + rm "${file}" + fi + else + mok "Generic hook script already exists: ${dest}" + fi + ## Install symbolic links to ucsf-vpn hook script, if missing for dir in pre-init connect post-connect disconnect post-disconnect attempt-reconnect post-attempt-reconnect reconnect; do path=${hooks_dir}/${dir}.d - sudo mkdir -p "${path}" - [[ -d "${path}" ]] || merror "Failed to create directory: ${path}" dest="${path}/${filename}" - sudo ln -fs "${hooks_dir}/${filename}" "${dest}" - [[ -L "${dest}" ]] || merror "Failed to create symbol link: ${dest} -> ${hooks_dir}/${filename}" - mok "Added symbolic link: ${dest} -> ${hooks_dir}/${filename}" + if [[ $action == "check" ]] && [[ ! -L "${dest}" ]]; then + return 1 + fi + if $force || [[ ! -L "${dest}" ]]; then + assert_sudo "install-vpnc" + sudo mkdir -p "${path}" + [[ -d "${path}" ]] || merror "Failed to create directory: ${path}" + sudo ln -fs "${hooks_dir}/${filename}" "${dest}" + [[ -L "${dest}" ]] || merror "Failed to create symbol link: ${dest} -> ${hooks_dir}/${filename}" + mok "Symbolic link added: ${dest} -> ${hooks_dir}/${filename}" + else + mok "Symbolic link already exists: ${dest} -> ${hooks_dir}/${filename}" + fi done - rm "${file}" mdebug "install_vpnc() ... done" + + return 0 } @@ -757,7 +785,7 @@ elif [[ $action == "routing" ]]; then routing_details _exit $? elif [[ $action == "install-vpnc" ]]; then - install_vpnc + install_vpnc "install" _exit $? elif [[ $action == "start" ]]; then openconnect_start