You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hestiacp/bin/v-run-cli-cmd

94 lines
2.4 KiB

#!/bin/bash
# info: run cli command
# options: USER CMD [ARG...]
#
# example: v-run-cli-cmd user composer require package
#
# This function runs a limited list of cli commands with dropped privileges as the specific hestia user
#----------------------------------------------------------#
# Variables & Functions #
#----------------------------------------------------------#
user=$1
clicmd=$2
# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '2' "$#" 'USER CMD [ARGS]'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"
# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z "$homedir" ]; then
check_result "$E_NOTEXIST" "Error: user home directory doesn't exist"
fi
if [ "$clicmd" = "composer" ]; then
clicmd="$homedir/.composer/composer"
fi
if [ "$clicmd" = "wp" ]; then
clicmd="$homedir/.wp-cli/wp"
fi
if [[ "$clicmd" =~ php[0-9][0-9] ]]; then
php_ver=$(echo "$clicmd" | grep -oP "\d+")
if [ "$LOCAL_PHP" == "yes" ]; then
clicmd="/opt/brepo/php${php_ver}/bin/php"
else
clicmd="/usr/bin/php${php_ver}"
fi
fi
if [ -z "$(which "$clicmd")" ]; then
check_result "$E_NOTEXIST" "Cli command does not exist $clicmd"
fi
basecmd="$(basename "$clicmd")"
if [ "$basecmd" != 'ps' -a \
"$basecmd" != 'ls' -a \
"$basecmd" != 'tar' -a \
"$basecmd" != 'zip' -a \
"$basecmd" != 'unzip' -a \
"$basecmd" != 'gzip' -a \
"$basecmd" != 'gunzip' -a \
"$basecmd" != 'mkdir' -a \
"$basecmd" != 'find' -a \
"$basecmd" != 'id' -a \
"$basecmd" != 'grep' -a \
"$basecmd" != 'egrep' -a \
"$basecmd" != 'sed' -a \
"$basecmd" != 'cat' -a \
"$basecmd" != 'php' -a \
"$basecmd" != "wp" -a \
"$basecmd" != 'composer' ]; then
check_result "$E_FORBIDEN" "Error: Cli command not enabled"
fi
all_scriptargs=("$@")
for ((I = 3; I <= $#; I++)); do
cmdArgs="$cmdArgs ${all_scriptargs[${I} - 1]}"
done
runuser -u "$user" -- $clicmd $cmdArgs 2>&1
if [ $? -ne 0 ]; then
echo "Error: cmd exited with errors"
exit 3
fi
# Logging
log_event "$OK" "$ARGUMENTS"
exit