Skip to content

Commit

Permalink
Now 'ucsf vpn --flavor=...' asserts that generic hook scripts are ins…
Browse files Browse the repository at this point in the history
…talled
  • Loading branch information
HenrikBengtsson committed May 20, 2024
1 parent b4aeb9b commit 4e4432a
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 64 additions & 28 deletions bin/ucsf-vpn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -851,9 +851,17 @@ function openconnect_start() {

minfo "Preparing to connect to VPN server '$server'"

## Assert that --flavor=<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=<flavor> exists, if specified
flavor_home > /dev/null
fi

assert_sudo "start"

## Load user credentials from file?
Expand Down Expand Up @@ -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}"
}

Expand Down Expand Up @@ -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
}


Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/incl/openconnect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ function openconnect_start() {

minfo "Preparing to connect to VPN server '$server'"

## Assert that --flavor=<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=<flavor> exists, if specified
flavor_home > /dev/null
fi

assert_sudo "start"

## Load user credentials from file?
Expand Down
78 changes: 53 additions & 25 deletions src/ucsf-vpn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
}

Expand Down Expand Up @@ -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
}


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4e4432a

Please sign in to comment.