forked from obel1x/fedora-OEMDRV
488f04d387
Improved error logging and added function handling calls with log and return values Improved check for matching sudo rule
143 lines
4.8 KiB
Bash
143 lines
4.8 KiB
Bash
# Includes for System Setup
|
|
#
|
|
# SPDX-FileCopyrightText: Daniel Pätzold
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
#
|
|
# This is not a runnig script-file. No Logik to execute. Its used for includes in other scripts.
|
|
|
|
#Check if we are root
|
|
# Deprectaed - use if Statement itself
|
|
#check_root()
|
|
#{
|
|
# if [ "$EUID" -ne 0 ]; then
|
|
# return 1
|
|
# fi
|
|
# return 0
|
|
#}
|
|
if [ ! -f $(dirname "$0")/setup_system.conf ]; then
|
|
echo "System configuration not found. Please make a copy of setup_system.conf.dist, name it setup_system.conf and check the settings in it before running."
|
|
echo "Press any key to continue" && read -n 1 -s -r && exit 1
|
|
fi
|
|
source $(dirname "$0")/setup_system.conf
|
|
|
|
#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
|
|
}
|
|
|
|
# Functions for logging
|
|
elog_init() {
|
|
#Create a new logfile and put some text in it
|
|
echo $@ | tee ${LOGFILE}
|
|
}
|
|
elog_add() {
|
|
#Append some text to the logile
|
|
echo $@ | tee ${LOGFILE} -a
|
|
}
|
|
elog_add_command() {
|
|
#Run a command, capture all output (STD and ERR) to the logfile AND in variable RETTXT AND output to screen
|
|
#Returns the exit value of the command in $? and in RETNO
|
|
RETTXT=$( { $@ > >(tee -a ${LOGFILE}); } 2> >(tee -a ${LOGFILE}) )
|
|
RETNO=$?
|
|
echo "${RETTXT}"
|
|
return ${RETNO}
|
|
}
|
|
|
|
# Will set variable DAVTOKEN_USER and DAVTOKEN_PASS to the stored value or get a new one
|
|
get_nc_token() {
|
|
export DAVTOKEN_USER=""
|
|
export DAVTOKEN_PASS=""
|
|
|
|
if [ "$EUID" -eq 0 ]; then
|
|
echo "get_nc_token(): Called as superuser, which is denied."
|
|
return 1 # Token for Superuser makes no sense and cannot work
|
|
fi
|
|
|
|
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
|
|
export DAVTOKEN_USER=$( echo "${POLLJSON}" | grep -oP '(?<="loginName":")[^"]+(?=")' )
|
|
export 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
|
|
}
|
|
|
|
# This is not a runnig script-file. No Logik to execute. Its used for includes in other scripts.
|