#!/bin/bash
# info: run cli command
# options: USER FILE
#
# The function runs a limited list of cli commands with dropped privileges as the specific hestia user

user=$1
clicmd=$2

# Includes
source $HESTIA/func/main.sh

#----------------------------------------------------------#
#                    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 [ -z "$(which "$clicmd")" ]; then
    check_result $E_NOTEXIST "Error: Cli command does not exist"
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" != 'php5.6'     -a \
     "$basecmd" != 'php7.0'     -a \
     "$basecmd" != 'php7.1'     -a \
     "$basecmd" != 'php7.2'     -a \
     "$basecmd" != 'php7.3'     -a \
     "$basecmd" != 'php7.4'     -a \
     "$basecmd" != 'php'        -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
if [ $? -ne 0 ]; then 
    echo "Error: cmd exited with errors"
    exit 3
fi

# Logging
log_event "$OK" "$ARGUMENTS"

exit
