v-run-cli-cmd 2.1 KB

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