rvmsudo 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bash
  2. sudo_args=()
  3. etc_sudoers_warning()
  4. {
  5. printf "%b" "Warning: $1, falling back to call via \`/usr/bin/env\`, this breaks rules from \`/etc/sudoers\`. Run:
  6. export rvmsudo_secure_path=1
  7. to avoid the warning, put it in shell initialization file to make it persistent.
  8. " >&2
  9. case "${2:-}" in
  10. (cant_read)
  11. printf "%b" "
  12. In case there is no \`secure_path\` in \`/etc/sudoers\`. Run:
  13. export rvmsudo_secure_path=0
  14. to avoid the warning, put it in shell initialization file to make it persistent.
  15. " >&2
  16. ;;
  17. esac
  18. }
  19. while
  20. (( $# ))
  21. do
  22. case "$1" in
  23. (--trace)
  24. export PS4="+ \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()} \${LINENO} > "
  25. set -o xtrace
  26. ;;
  27. (--verbose)
  28. set -o verbose
  29. ;;
  30. (-*)
  31. sudo_args+=("$1")
  32. case "$1" in
  33. (-g|-u|-p|-r|-t|-U|-C)
  34. shift
  35. sudo_args+=("${1:-}")
  36. ;;
  37. esac
  38. ;;
  39. (*)
  40. break
  41. ;;
  42. esac
  43. (( $# == 0 )) || shift
  44. done
  45. if
  46. (( $# ))
  47. then
  48. if
  49. [[ -n "${rvmsudo_secure_path}" ]]
  50. then
  51. if
  52. [[ "${rvmsudo_secure_path:-0}" == 1 ]]
  53. then
  54. sudo_args+=( "/usr/bin/env" )
  55. fi
  56. else
  57. if
  58. [[ -f "/etc/sudoers" ]]
  59. then
  60. if
  61. [[ ! -r "/etc/sudoers" ]]
  62. then
  63. sudo_args+=( "/usr/bin/env" )
  64. etc_sudoers_warning 'can not check `/etc/sudoers` for `secure_path`' cant_read
  65. elif
  66. \grep -E '^[[:space:]]*Defaults[[:space:]]+secure_path=' "/etc/sudoers" >/dev/null
  67. then
  68. sudo_args+=( "/usr/bin/env" )
  69. etc_sudoers_warning '`secure_path` found in `/etc/sudoers`' found_secure_path
  70. fi
  71. fi
  72. fi
  73. eval command sudo \"\${sudo_args[@]}\" $(
  74. /usr/bin/env |
  75. awk -F= 'BEGIN{v=0;}/^[a-zA-Z_][a-zA-Z0-9_]*=/{v=1;}v==1&&$2~/^['\''\$]/{v=2;} v==1&&$2~/^\(\) \{/{v=0;} v==1&&$2~/^\(/{v=3;}v==2&&/'\''$/&&!/'\'\''$/{v=1;}v==3&&/\)$/{v=1;}v{print;}v==1{v=0;}' |
  76. GREP_OPTIONS="" \grep -E '^rvm|^gemset|^http_|^PATH|^IRBRC|RUBY|GEM' |
  77. sed 's/=\(.*\)$/="\1"/'
  78. ) \"\$@\" ||
  79. {
  80. result=$?
  81. case $result in
  82. (126)
  83. printf "%b" "\nTarget user does not have access to the given command, try finding problem below:\n\n"
  84. p="$rvm_path"
  85. dirs=()
  86. while
  87. [[ "$p" != "" ]]
  88. do
  89. dirs+=( "$p" )
  90. p="${p%/*}"
  91. done
  92. \command \ls -ld "${dirs[@]}"
  93. printf "%b" "\nIf you could not find the issue check permissions of all involved users and directories.\n\n"
  94. ;;
  95. esac
  96. exit $result
  97. }
  98. else
  99. printf "%b" "Usage:\n $0 [--trace] [--verbose] [sudo-options] command [command-options]\n"
  100. fi