v-run-cli-cmd 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/wp-cli"
  36. fi
  37. if [ -z "$(which "$clicmd")" ]; then
  38. check_result "$E_NOTEXIST" "Error: Cli command does not exist"
  39. fi
  40. basecmd="$(basename "$clicmd")"
  41. if [ "$basecmd" != 'ps' -a \
  42. "$basecmd" != 'ls' -a \
  43. "$basecmd" != 'tar' -a \
  44. "$basecmd" != 'zip' -a \
  45. "$basecmd" != 'unzip' -a \
  46. "$basecmd" != 'gzip' -a \
  47. "$basecmd" != 'gunzip' -a \
  48. "$basecmd" != 'mkdir' -a \
  49. "$basecmd" != 'find' -a \
  50. "$basecmd" != 'id' -a \
  51. "$basecmd" != 'grep' -a \
  52. "$basecmd" != 'egrep' -a \
  53. "$basecmd" != 'sed' -a \
  54. "$basecmd" != 'cat' -a \
  55. "$basecmd" != 'php5.6' -a \
  56. "$basecmd" != 'php7.0' -a \
  57. "$basecmd" != 'php7.1' -a \
  58. "$basecmd" != 'php7.2' -a \
  59. "$basecmd" != 'php7.3' -a \
  60. "$basecmd" != 'php7.4' -a \
  61. "$basecmd" != 'php8.0' -a \
  62. "$basecmd" != 'php' -a \
  63. "$basecmd" != "wp" -a \
  64. "$basecmd" != 'composer' ]; then
  65. check_result "$E_FORBIDEN" "Error: Cli command not enabled"
  66. fi
  67. all_scriptargs=("$@")
  68. for ((I=3; I <= $# ; I++)); do
  69. cmdArgs="$cmdArgs ${all_scriptargs[${I}-1]}"
  70. done
  71. runuser -u "$user" -- $clicmd $cmdArgs
  72. if [ $? -ne 0 ]; then
  73. echo "Error: cmd exited with errors"
  74. exit 3
  75. fi
  76. # Logging
  77. log_event "$OK" "$ARGUMENTS"
  78. exit