v-run-cli-cmd 2.5 KB

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