Files
fedora-OEMDRV/system_setup/setup_system.inc
T
Daniel Pätzold f48c656997 First Commit
2026-01-02 11:24:52 +01:00

112 lines
3.6 KiB
PHP

# Includes for System Setup
#
# SPDX-FileCopyrightText: Daniel Pätzold
# SPDX-License-Identifier: AGPL-3.0-or-later
#
#Check if we are root
check_root()
{
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
return 1
fi
return 0
}
#Check if the Data- Directory is encrypted
check_data_isecrypted() {
CHECKRES=$( cat /etc/mtab | grep "${DECRYPTEDDATADIR}" | grep "fuse.gocryptfs" )
if [ "${CHECKRES}." == "." ]; then
return 1 # Error: Directory is not mounted
else
return 0 # Directory is mounted
fi
}
# Will set variable DAVTOKEN_USER and DAVTOKEN_PASS to the stored value or get a new one
get_nc_token() {
DAVTOKEN_USER=""
DAVTOKEN_PASS=""
if [ ${IPAVAULTUSE} == "true" ]; then
check_data_isecrypted
if [ $? -ne 0 ]; then
echo "Data Directory is not encrypted. Please mount it first."
return 1
fi
fi
if [ ! -f ${DAVTOKENFILENAME} ]; then
# Directory is ok, but no Tokenfile was found, need to generate a new one
REQJSON=$( curl -s -A "WEBDAV:${HOSTNM}" -X POST "https://${SERVERFQDN_NC}/index.php/login/v2" )
# echo "JSON is:"
# echo "${REQJSON}"
REQTOKEN=$( echo "${REQJSON}" | grep -oP '(?<="token":")[^"]+(?=")' )
REQURL=$( echo "${REQJSON}" | grep -oP '(?<="login":")[^"]+(?=")' )
/usr/bin/firefox "${REQURL}" &
for i in {1..200}
do
echo "Waiting 6 seconds"
sleep 6
echo -n "Poll Number ${i}..."
POLLJSON=$( curl -s -X POST "https://${SERVERFQDN_NC}/login/v2/poll" -d "token=${REQTOKEN}" )
if [[ "${POLLJSON}" == *"appPassword"* ]]; then
echo "${POLLJSON}" > ${DAVTOKENFILENAME}
echo "found token. Token has been written to ${DAVTOKENFILENAME}"
break
else
echo "failed"
fi
done
else
# Tokenfile found, reading it
POLLJSON=$( cat ${DAVTOKENFILENAME} )
fi
DAVTOKEN_USER=$( echo "${POLLJSON}" | grep -oP '(?<="loginName":")[^"]+(?=")' )
DAVTOKEN_PASS=$( echo "${POLLJSON}" | grep -oP '(?<="appPassword":")[^"]+(?=")' )
}
# Custom `select` implementation that allows *empty* input.
# Pass the choices as individual arguments.
# Output is the chosen item, or "", if the user just pressed ENTER.
# Example:
# choice=$(selectWithDefault 'one' 'two' 'three')
selectWithDefault() {
local item i=0 numItems=$#
# Print numbered menu items, based on the arguments passed.
for item; do # Short for: for item in "$@"; do
printf '%s\n' "$((++i))) $item"
done >&2 # Print to stderr, as `select` does.
# Prompt the user for the index of the desired item.
while :; do
printf %s "${PS3-#? }" >&2 # Print the prompt string to stderr, as `select` does.
read -r index
# Make sure that the input is either empty or that a valid index was entered.
[[ -z $index ]] && break # empty input
(( index >= 1 && index <= numItems )) 2>/dev/null || { echo "Invalid selection. Please try again." >&2; continue; }
break
done
# Output the selected item, if any.
[[ -n $index ]] && printf %s "${@: index:1}"
}
selectExample() {
# Print the prompt message and call the custom select function.
echo "Include audits (default is 'Nope')?"
optionsAudits=('Yep' 'Nope')
opt=$(selectWithDefault "${optionsAudits[@]}")
# Process the selected item.
case $opt in
'Yep') includeAudits=true; ;;
''|'Nope') includeAudits=false; ;; # $opt is '' if the user just pressed ENTER
esac
}