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

View File

@ -5,13 +5,13 @@
# #
# USAGE: source bashbot_init.inc.sh # USAGE: source bashbot_init.inc.sh
# #
# DESCRIPTION: extend / overwrite bashbot initialisation # DESCRIPTION: extend / overwrite bashbot initialisation
# #
# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/ # LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/
# AUTHOR: KayM (gnadelwartz), kay@rrr.de # AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 27.01.2021 13:42 # CREATED: 27.01.2021 13:42
# #
#### $$VERSION$$ v1.40-0-gf9dab50 #### $$VERSION$$ v1.45-dev-1-g34455c2
#=============================================================================== #===============================================================================
# shellcheck disable=SC2059 # shellcheck disable=SC2059
@ -54,17 +54,23 @@ bot_init() {
[ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}" [ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}"
done done
printf "Done.\n" printf "Done.\n"
# ask for bashbot user # guess bashbot from botconfig.jssh owner:group
# shellcheck disable=SC2153 [ -f "${BOTCONFIG}.jssh" ] && runuser="$(stat -c '%U' "${BOTCONFIG}.jssh"):$(stat -c '%G' "${BOTCONFIG}.jssh")"
runuser="${RUNUSER}"; [ "${UID}" = "0" ] && runuser="nobody" # 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}]: " printf "Enter User to run bashbot [${runuser}]: "
read -r chown read -r chown
[ -z "${chown}" ] && chown="${runuser}"; touser="${chown%:*}" [ -z "${chown}" ] && chown="${runuser}"
touser="${chown%:*}"
# check user ... # check user ...
if ! id "${touser}" &>/dev/null; then if ! id "${touser}" &>/dev/null; then
printf "${RED}User \"${touser}\" does not exist!${NN}" printf "${RED}User \"${touser}\" does not exist!${NN}"
exit 3 exit 3
elif [[ "${UID}" != "0" && "${touser}" != "${runuser}" ]]; then elif [[ "${UID}" != "0" && "${touser}" != "${RUNUSER}" ]]; then
# different user but not root ... # 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 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 [ -n "${INTERACTIVE}" ] && read -r runuser