|
| 1 | +#!/bin/bash |
| 2 | +# info: run cli command |
| 3 | +# options: USER FILE |
| 4 | +# |
| 5 | +# The function runs a limited list of cli commands with dropped privileges as the specific hestia user |
| 6 | + |
| 7 | +user=$1 |
| 8 | +clicmd=$2 |
| 9 | + |
| 10 | +# Includes |
| 11 | +source $HESTIA/func/main.sh |
| 12 | + |
| 13 | +#----------------------------------------------------------# |
| 14 | +# Verifications # |
| 15 | +#----------------------------------------------------------# |
| 16 | + |
| 17 | +check_args '2' "$#" 'USER CMD [ARGS]' |
| 18 | +is_format_valid 'user' |
| 19 | +is_object_valid 'user' 'USER' "$user" |
| 20 | + |
| 21 | +# Checking user homedir |
| 22 | +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) |
| 23 | +if [ -z $homedir ]; then |
| 24 | + check_result $E_NOTEXIST "Error: user home directory doesn't exist" |
| 25 | +fi |
| 26 | + |
| 27 | +realcmd="$(which "$clicmd")" |
| 28 | +check_result $? "Unknown cli command" $E_NOTEXIST |
| 29 | + |
| 30 | +if [ ! -x "$realcmd" ]; then |
| 31 | + check_result $E_NOTEXIST "Error: Cli command does not exist" |
| 32 | +fi |
| 33 | + |
| 34 | +if [ "$realcmd" != '/bin/ps' -a \ |
| 35 | + "$realcmd" != '/bin/ls' -a \ |
| 36 | + "$realcmd" != '/bin/tar' -a \ |
| 37 | + "$realcmd" != '/bin/zip' -a \ |
| 38 | + "$realcmd" != '/usr/bin/unzip' -a \ |
| 39 | + "$realcmd" != '/bin/gzip' -a \ |
| 40 | + "$realcmd" != '/bin/gunzip' -a \ |
| 41 | + "$realcmd" != '/bin/mkdir' -a \ |
| 42 | + "$realcmd" != '/usr/bin/find' -a \ |
| 43 | + "$realcmd" != '/bin/grep' -a \ |
| 44 | + "$realcmd" != '/bin/egrep' -a \ |
| 45 | + "$realcmd" != '/bin/sed' -a \ |
| 46 | + "$realcmd" != '/bin/cat' -a \ |
| 47 | + "$realcmd" != '/usr/bin/php5.6' -a \ |
| 48 | + "$realcmd" != '/usr/bin/php7.0' -a \ |
| 49 | + "$realcmd" != '/usr/bin/php7.1' -a \ |
| 50 | + "$realcmd" != '/usr/bin/php7.2' -a \ |
| 51 | + "$realcmd" != '/usr/bin/php7.3' -a \ |
| 52 | + "$realcmd" != '/usr/bin/php' ]; then |
| 53 | + check_result $E_FORBIDEN "Error: Cli command not enabled" |
| 54 | +fi |
| 55 | + |
| 56 | +all_scriptargs=("$@") |
| 57 | +for ((I=3; I <= $# ; I++)); do |
| 58 | + cmdArgs="$cmdArgs ${all_scriptargs[${I}-1]}" |
| 59 | +done |
| 60 | + |
| 61 | +sudo -u $user -- $realcmd $cmdArgs |
| 62 | +if [ $? -ne 0 ]; then |
| 63 | + echo "Error: cmd exited with errors" |
| 64 | + exit 3 |
| 65 | +fi |
| 66 | + |
| 67 | +# Logging |
| 68 | +log_event "$OK" "$ARGUMENTS" |
| 69 | + |
| 70 | +exit |
0 commit comments