rvmrc_trust 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/env bash
  2. __rvm_md5_for_contents()
  3. {
  4. if builtin command -v md5 > /dev/null
  5. then md5 | __rvm_awk '{print $1}'
  6. elif builtin command -v md5sum > /dev/null
  7. then md5sum | __rvm_awk '{print $1}'
  8. elif builtin command -v openssl > /dev/null
  9. then openssl md5 | __rvm_awk '{print $1}'
  10. else return 1
  11. fi
  12. true # for osx
  13. }
  14. __rvm_sha256_for_contents()
  15. {
  16. if builtin command -v sha256sum > /dev/null
  17. then sha256sum | __rvm_awk '{print $1}'
  18. elif builtin command -v sha256 > /dev/null
  19. then sha256 | __rvm_awk '{print $1}'
  20. elif builtin command -v shasum > /dev/null
  21. then shasum -a256 | __rvm_awk '{print $1}'
  22. elif builtin command -v openssl > /dev/null
  23. then openssl sha -sha256 | __rvm_awk '{print $1}'
  24. else return 1
  25. fi
  26. true # for osx
  27. }
  28. __rvm_checksum_for_contents()
  29. {
  30. \typeset __sum
  31. __sum=$( echo "$1" | \command \cat - "$1" | __rvm_md5_for_contents ) ||
  32. {
  33. rvm_error "Neither md5 nor md5sum were found in the PATH"
  34. return 1
  35. }
  36. __sum+=$( echo "$1" | \command \cat - "$1" | __rvm_sha256_for_contents ) ||
  37. {
  38. rvm_error "Neither sha256sum nor shasum found in the PATH"
  39. return 1
  40. }
  41. echo ${__sum}
  42. }
  43. __rvm_rvmrc_key()
  44. {
  45. printf "%b" "$1" | \command \tr '[#/.=()]' _
  46. return $?
  47. }
  48. __rvm_reset_rvmrc_trust()
  49. {
  50. if [[ "$1" == all ]]
  51. then
  52. echo "" > "${rvm_user_path:-${rvm_path}/user}/rvmrcs"
  53. else
  54. __rvm_db_ "${rvm_user_path:-${rvm_path}/user}/rvmrcs" "$(__rvm_rvmrc_key "$1")" "delete" >/dev/null 2>&1
  55. fi
  56. }
  57. __rvm_trust_rvmrc()
  58. {
  59. [[ -f "$1" ]] || return 1
  60. __rvm_reset_rvmrc_trust "$1"
  61. __rvm_db_ "${rvm_user_path:-${rvm_path}/user}/rvmrcs" "$(__rvm_rvmrc_key "$1")" "1;$(__rvm_checksum_for_contents "$1")" >/dev/null 2>&1 ||
  62. return $?
  63. }
  64. __rvm_untrust_rvmrc()
  65. {
  66. [[ -f "$1" ]] || return 1
  67. __rvm_reset_rvmrc_trust "$1"
  68. __rvm_db_ "${rvm_user_path:-${rvm_path}/user}/rvmrcs" "$(__rvm_rvmrc_key "$1")" "0;$(__rvm_checksum_for_contents "$1")" >/dev/null 2>&1 ||
  69. return $?
  70. }
  71. __rvm_rvmrc_stored_trust()
  72. {
  73. [[ -f "$1" ]] || return 1
  74. __rvm_db_ "${rvm_user_path:-${rvm_path}/user}/rvmrcs" "$(__rvm_rvmrc_key "$1")" ||
  75. return $?
  76. }
  77. __rvm_rvmrc_stored_trust_check()
  78. {
  79. \typeset _first _second _rvmrc _rvmrc_base
  80. if [[ -n "${ZSH_VERSION:-}" ]]
  81. then _first=1
  82. else _first=0
  83. fi
  84. _second=$(( _first + 1 ))
  85. _rvmrc="${1}"
  86. _rvmrc_base="$(basename "${_rvmrc}")"
  87. if [[ -f "$_rvmrc" ]]
  88. then
  89. saveIFS=$IFS
  90. IFS=$';'
  91. trust=($(__rvm_rvmrc_stored_trust "$_rvmrc"))
  92. IFS=$saveIFS
  93. if
  94. [[ "${trust[${_second}]:-'#'}" != "$(__rvm_checksum_for_contents "$_rvmrc")" ]]
  95. then
  96. echo "The '$_rvmrc' contains unreviewed changes."
  97. return 1
  98. elif
  99. [[ "${trust[${_first}]}" == '1' ]]
  100. then
  101. echo "The '$_rvmrc' is currently trusted."
  102. return 0
  103. elif
  104. [[ "${trust[${_first}]}" == '0' ]]
  105. then
  106. echo "The '$_rvmrc' is currently untrusted."
  107. return 1
  108. else
  109. echo "The trustiworthiness of '$_rvmrc' is currently unknown."
  110. return 1
  111. fi
  112. else
  113. echo "There is no '$_rvmrc'"
  114. return 1
  115. fi
  116. }
  117. __rvm_check_rvmrc_trustworthiness()
  118. {
  119. # Trust when they have the flag... of doom!
  120. (( ${rvm_trust_rvmrcs_flag:-0} == 0 )) || return 0
  121. # Fail if no file given or no extra params
  122. [[ -n "$1" ]] || (( $# > 1 )) || return 1
  123. \typeset _first _second saveIFS
  124. if [[ -n "${ZSH_VERSION:-}" ]]
  125. then _first=1
  126. else _first=0
  127. fi
  128. _second=$(( _first + 1 ))
  129. saveIFS="$IFS"
  130. IFS=$';'
  131. \typeset -a trust
  132. trust=( $( __rvm_rvmrc_stored_trust "$1" ) )
  133. IFS="$saveIFS"
  134. if
  135. [[ "${trust[${_second}]:-'#'}" == "$(__rvm_checksum_for_contents "$1")" ]]
  136. then
  137. [[ "${trust[${_first}]}" == '1' ]] || return $?
  138. else
  139. __rvm_ask_to_trust "$@" || return $?
  140. fi
  141. true
  142. }
  143. __rvm_display_rvmrc()
  144. {
  145. __rvm_file_notice_display_pre
  146. __rvm_wait_anykey "(( press a key to review the ${_rvmrc_base} file ))"
  147. printf "%b" "${rvm_warn_clr}"
  148. command cat -v "${_rvmrc}"
  149. printf "%b" "${rvm_reset_clr}"
  150. __rvm_file_notice_display_post
  151. }
  152. __rvm_ask_to_trust()
  153. {
  154. \typeset trusted value anykey _rvmrc _rvmrc_base
  155. _rvmrc="${1}"
  156. _rvmrc_base="$(basename "${_rvmrc}")"
  157. if [[ ! -t 0 || -n "$MC_SID" ]] || (( ${rvm_promptless:=0} == 1 ))
  158. then return 2
  159. fi
  160. __rvm_file_notice_initial
  161. trusted=0
  162. while (( ! trusted ))
  163. do
  164. printf "%b" 'y[es], n[o], v[iew], c[ancel]> '
  165. builtin read response
  166. value="$(echo -n "${response}" | \command \tr '[[:upper:]]' '[[:lower:]]' | __rvm_strip)"
  167. case "${value:-n}" in
  168. v|view)
  169. __rvm_display_rvmrc
  170. ;;
  171. y|yes)
  172. trusted=1
  173. ;;
  174. n|no)
  175. break
  176. ;;
  177. c|cancel)
  178. return 1
  179. ;;
  180. esac
  181. done
  182. if (( trusted ))
  183. then
  184. __rvm_trust_rvmrc "$1"
  185. return 0
  186. else
  187. __rvm_untrust_rvmrc "$1"
  188. return 1
  189. fi
  190. }
  191. __rvm_file_notice_initial()
  192. {
  193. case "${_rvmrc}" in
  194. (*/.rvmrc) __rvm_rvmrc_notice_initial ;;
  195. (*) __rvm_env_file_notice_initial ;;
  196. esac
  197. }
  198. __rvm_rvmrc_notice_initial()
  199. {
  200. __rvm_table "NOTICE" <<-TEXT
  201. RVM has encountered a new or modified ${_rvmrc_base} file in the current directory, this is a shell script and therefore may contain any shell commands.
  202. Examine the contents of this file carefully to be sure the contents are safe before trusting it!
  203. Do you wish to trust '${_rvmrc}'?
  204. Choose v[iew] below to view the contents
  205. TEXT
  206. }
  207. __rvm_env_file_notice_initial()
  208. {
  209. __rvm_table "NOTICE" <<-TEXT
  210. RVM has encountered a new or modified ${_rvmrc_base} file in the current directory, environment variables from this file will be exported and therefore may influence your shell.
  211. Examine the contents of this file carefully to be sure the contents are safe before trusting it!
  212. Do you wish to trust '${_rvmrc}'?
  213. Choose v[iew] below to view the contents
  214. TEXT
  215. }
  216. __rvm_file_notice_display_pre()
  217. {
  218. __rvm_table <<-TEXT
  219. The contents of the ${_rvmrc_base} file will now be displayed.
  220. After reading the file, you will be prompted again for 'yes or no' to set the trust level for this particular version of the file.
  221. Note: You will be re-prompted each time the ${_rvmrc_base} file's contents change
  222. changes, and may change the trust setting manually at any time.
  223. TEXT
  224. }
  225. __rvm_file_notice_display_post()
  226. {
  227. case "${_rvmrc}" in
  228. (*/.rvmrc) __rvm_rvmrc_notice_display_post ;;
  229. (*) __rvm_env_file_notice_display_post ;;
  230. esac
  231. }
  232. __rvm_rvmrc_notice_display_post()
  233. {
  234. __rvm_table "Viewing of ${_rvmrc} complete." <<-TEXT
  235. Trusting an ${_rvmrc_base} file means that whenever you cd into this directory, RVM will run this ${_rvmrc_base} shell script.
  236. Note that if the contents of the file change, you will be re-prompted to review the file and adjust its trust settings. You may also change the trust settings manually at any time with the 'rvm rvmrc' command.
  237. TEXT
  238. }
  239. __rvm_env_file_notice_display_post()
  240. {
  241. __rvm_table "Viewing of ${_rvmrc} complete." <<-TEXT
  242. Trusting an ${_rvmrc_base} file means that whenever you cd into this directory, RVM will export environment variables from ${_rvmrc_base}.
  243. Note that if the contents of the file change, you will be re-prompted to review the file and adjust its trust settings. You may also change the trust settings manually at any time with the 'rvm rvmrc' command.
  244. TEXT
  245. }