v-run-cli-cmd 2.1 KB

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