Files
fedora-OEMDRV/system_setup/setup_system.inc.sh
T

163 lines
5.8 KiB
Bash
Executable File

#!/usr/bin/env sh
# 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 real logic 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")/../config/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")/../config/setup_system.conf
#Parse additional client-configs
if [[ `ls -1 $(dirname "$0")/../config.d/*.conf 2>/dev/null | wc -l ` -gt 0 ]]; then
source $(dirname "$0")/../config.d/*.conf
fi
#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 output (STD and ERR) to the logfile AND output to screen
# WILL NOT SET RETTXT to make Output directly to screen
#Returns the exit value of the command in $? and in RETNO
$@ 2>&1 | tee -a ${LOGFILE}
RETNO=$?
return ${RETNO}
}
elog_add_command_subshell() {
# Special Version of above, where the command will be completely executed in a subshell and then passed to Variable RETTXT. This is needed for some commands, that may output to
# something else than STD or ERR and otherwise cannot be captured completely.
# Benefit: Really catch everything that is send to output
# Disadvantage: Output wont't display directly, but only after finshed execution
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
echo "No token found here. Getting a new WEBDAV Token for this Device."
echo "Please logon to your Nextcloud instance via SSO/kerberos"
# 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}"
pkill firefox
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.