fix: alias not always working, init: improve for unprivileged user, guess touser from config file

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-02-07 11:02:01 +01:00
parent 34455c2c4c
commit a6ff405cc5
2 changed files with 18 additions and 14 deletions

View File

@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb
# 8 - curl/wget missing
# 10 - not bash!
#
#### $$VERSION$$ v1.41-0-gad1b91f
#### $$VERSION$$ v1.45-dev-1-g34455c2
##################################################################
# are we running in a terminal?
@ -178,10 +178,10 @@ RUNDIR="$(dirname "$0")"
MODULEDIR="${SCRIPTDIR}/modules"
# adjust stuff for source, use return from source without source
alias exit_source='exit'
exit_source() { exit "$1"; }
if [[ "${SCRIPT}" != "${REALME}" || "$1" == "source" ]]; then
SOURCE="yes"
[ -z "$1" ] && alias exit_source='printf "Exit from source ...\n";return'
[ -z "$1" ] && exit_source() { printf "Exit from source ...\n"; return "$1"; }
fi
# emmbeded system may claim bash but it is not
@ -282,16 +282,14 @@ if [ -z "${BOTTOKEN}" ]; then
fi
# check data dir file
if [ ! -w "${DATADIR}" ]; then
printf "${RED}ERROR: ${DATADIR} does not exist or is not writeable!.${NN}"
exit_source 2
printf "${RED}WARNING: ${DATADIR} does not exist or is not writeable!.${NN}"
fi
# setup count file
if [ ! -f "${COUNTFILE}.jssh" ]; then
printf '["counted_user_chat_id"]\t"num_messages_seen"\n' >> "${COUNTFILE}.jssh"
elif [ ! -w "${COUNTFILE}.jssh" ]; then
printf "${RED}ERROR: Can't write to ${COUNTFILE}!.${NN}"
printf "${RED}WARNING: Can't write to ${COUNTFILE}!.${NN}"
ls -l "${COUNTFILE}.jssh"
exit_source 2
fi
# setup blocked file
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then

View File

@ -11,7 +11,7 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 27.01.2021 13:42
#
#### $$VERSION$$ v1.40-0-gf9dab50
#### $$VERSION$$ v1.45-dev-1-g34455c2
#===============================================================================
# shellcheck disable=SC2059
@ -54,17 +54,23 @@ bot_init() {
[ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}"
done
printf "Done.\n"
# ask for bashbot user
# shellcheck disable=SC2153
runuser="${RUNUSER}"; [ "${UID}" = "0" ] && runuser="nobody"
# guess bashbot from botconfig.jssh owner:group
[ -f "${BOTCONFIG}.jssh" ] && runuser="$(stat -c '%U' "${BOTCONFIG}.jssh"):$(stat -c '%G' "${BOTCONFIG}.jssh")"
# empty or ":" use user running init, nobody for root
if [ "${#runuser}" -lt 3 ]; then
# shellcheck disable=SC2153
runuser="${RUNUSER}"
[ "${UID}" = "0" ] && runuser="nobody"
fi
printf "Enter User to run bashbot [${runuser}]: "
read -r chown
[ -z "${chown}" ] && chown="${runuser}"; touser="${chown%:*}"
[ -z "${chown}" ] && chown="${runuser}"
touser="${chown%:*}"
# check user ...
if ! id "${touser}" &>/dev/null; then
printf "${RED}User \"${touser}\" does not exist!${NN}"
exit 3
elif [[ "${UID}" != "0" && "${touser}" != "${runuser}" ]]; then
elif [[ "${UID}" != "0" && "${touser}" != "${RUNUSER}" ]]; then
# different user but not root ...
printf "${ORANGE}You are not root, adjusting permissions may fail. Try \"sudo ./bashbot.sh init\"${NN}Press <CTRL+C> to stop or <Enter> to continue..." 1>&2
[ -n "${INTERACTIVE}" ] && read -r runuser