osx-ssl-certs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env bash
  2. requirements_osx_update_openssl_cert_is_old()
  3. {
  4. [[ ! -f "$cert_file" ||
  5. "$cert_file" -ot /Library/Keychains/System.keychain ||
  6. "$cert_file" -ot /System/Library/Keychains/SystemRootCertificates.keychain
  7. ]] || (( ${rvm_force_flag:-0} > 0 ))
  8. }
  9. requirements_osx_update_openssl_cert_create_dir_sudo()
  10. {
  11. rvm_warn "mkdir -p \"$ssl_dir\" failed, retrying with sudo"
  12. if __rvm_try_sudo mkdir -p "$ssl_dir"
  13. then rvm_log "and sudo mkdir worked"
  14. else return $?
  15. fi
  16. }
  17. requirements_osx_update_openssl_cert_create_dir()
  18. {
  19. [[ -d "$ssl_dir" ]] ||
  20. mkdir -p "$ssl_dir" ||
  21. requirements_osx_update_openssl_cert_create_dir_sudo ||
  22. {
  23. \typeset __ret=$?
  24. rvm_error "Can not create directory '$ssl_dir' for certificates."
  25. return ${__ret}
  26. }
  27. }
  28. requirements_osx_update_openssl_cert_target_select()
  29. {
  30. if
  31. [[ -f "$cert_file" && -w "$cert_file" ]] ||
  32. [[ ! -e "$cert_file" && -d "$ssl_dir" && -w "$ssl_dir" ]]
  33. then
  34. __target="$cert_file"
  35. else
  36. __target="$(
  37. TMPDIR="${rvm_tmp_path}" mktemp ${TMPDIR:-/tmp}/tmp.XXXXXXXXXXXXXXXXXX
  38. )"
  39. fi
  40. }
  41. requirements_osx_update_openssl_cert_create_cert()
  42. {
  43. security find-certificate -a -p /Library/Keychains/System.keychain > "$__target"
  44. security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain >> "$__target"
  45. }
  46. requirements_osx_update_openssl_cert_target_move()
  47. {
  48. [[ "$__target" == "$cert_file" ]] ||
  49. __rvm_try_sudo \command \tee "$cert_file" < "$__target" > /dev/null ||
  50. {
  51. \typeset __ret=$?
  52. rm -f "$__target"
  53. return ${__ret}
  54. }
  55. }
  56. requirements_osx_update_openssl_cert_ensure_readable()
  57. {
  58. __target="$cert_file"
  59. while
  60. [[ ! -r "$__target" && "$__target" != "" ]]
  61. do
  62. __rvm_try_sudo \chmod ugo+rX "$__target"
  63. __target="${__target%/*}"
  64. done
  65. }
  66. requirements_osx_update_openssl_cert_run()
  67. {
  68. \typeset __target="$cert_file"
  69. if
  70. [[ -z "$cert_file" ]]
  71. then
  72. rvm_error "Empty path passed to certificates update, functions stack: ${FUNCNAME[*]}"
  73. return 1
  74. fi
  75. rvm_log "Updating certificates bundle '$cert_file'"
  76. requirements_osx_update_openssl_cert_create_dir || return $?
  77. requirements_osx_update_openssl_cert_target_select || return $?
  78. requirements_osx_update_openssl_cert_create_cert || return $?
  79. requirements_osx_update_openssl_cert_target_move || return $?
  80. }
  81. requirements_osx_update_openssl_cert()
  82. {
  83. [[ "Darwin" == "${_system_type}" ]] || return 0
  84. \typeset ssl_binary ssl_dir cert_file
  85. ssl_binary="${1:-$(__rvm_which openssl)}"
  86. __rvm_osx_ssl_certs_file_from_openssl "${ssl_binary}" || return $?
  87. ssl_dir="${cert_file%/*}"
  88. case "${rvm_autolibs_flag_number}" in
  89. (0)
  90. rvm_debug "Skipping update of certificates bundle '$cert_file', to force update run:
  91. rvm osx-ssl-certs update ${cert_file}
  92. "
  93. return 0
  94. ;;
  95. esac
  96. if
  97. requirements_osx_update_openssl_cert_is_old
  98. then
  99. rvm_requiremnts_fail_or_run_action 2 \
  100. "Skipping update of certificates bundle '$cert_file', to force update run:
  101. rvm osx-ssl-certs update ${cert_file}
  102. " \
  103. requirements_osx_update_openssl_cert_run ||
  104. return $?
  105. else
  106. rvm_log "Certificates bundle '$cert_file' is already up to date."
  107. fi
  108. requirements_osx_update_openssl_cert_ensure_readable || return $?
  109. }
  110. __rvm_osx_ssl_certs_update_for_path()
  111. {
  112. \typeset ssl_dir cert_file
  113. cert_file="${1:-$( __rvm_osx_ssl_certs_file_for_ruby )}"
  114. ssl_dir="${cert_file%/*}"
  115. if (( ${rvm_silent_flag:-0} == 0 ))
  116. then printf "%b" "Updating certificates bundle ${cert_file}: "
  117. fi
  118. if
  119. requirements_osx_update_openssl_cert_is_old
  120. then
  121. if
  122. requirements_osx_update_openssl_cert_run
  123. then
  124. if (( ${rvm_silent_flag:-0} == 0 ))
  125. then printf "%b" "Updated.\n"
  126. fi
  127. else
  128. \typeset result=$?
  129. if (( ${rvm_silent_flag:-0} == 0 ))
  130. then printf "%b" "Failed.\n"
  131. else printf "%b" "Updating certificates for ${cert_file}: Failed.\n"
  132. fi
  133. return $result
  134. fi
  135. else
  136. if (( ${rvm_silent_flag:-0} == 0 ))
  137. then printf "%b" "Already up to date.\n"
  138. fi
  139. fi
  140. requirements_osx_update_openssl_cert_ensure_readable || return $?
  141. }
  142. __rvm_osx_ssl_certs_status_for_path()
  143. {
  144. \typeset ssl_dir cert_file
  145. cert_file="$1"
  146. ssl_dir="${cert_file%/*}"
  147. printf "%b" "Certificates bundle ${cert_file} is "
  148. if requirements_osx_update_openssl_cert_is_old
  149. then printf "%b" "old.\n"
  150. else printf "%b" "up to date.\n"
  151. fi
  152. }
  153. __rvm_osx_ssl_certs_file_for_ruby()
  154. {
  155. "${1:-ruby}" -ropenssl -e 'puts OpenSSL::X509::DEFAULT_CERT_FILE' 2>/dev/null
  156. }
  157. __rvm_osx_ssl_certs_ensure_for_ruby()
  158. {
  159. [[ "${_system_name}" == "OSX" ]] || return 0
  160. \typeset ssl_dir cert_file
  161. cert_file="$( __rvm_osx_ssl_certs_file_for_ruby "$1" )"
  162. ssl_dir="${cert_file%/*}"
  163. if
  164. requirements_osx_update_openssl_cert_is_old
  165. then
  166. rvm_requiremnts_fail_or_run_action 2 \
  167. "Skipping update of certificates bundle '$cert_file'." \
  168. requirements_osx_update_openssl_cert_run ||
  169. true # Ignore failure - ruby is already installed
  170. fi
  171. requirements_osx_update_openssl_cert_ensure_readable || return $?
  172. true # for osx
  173. }
  174. __rvm_osx_ssl_certs_file_from_openssl()
  175. {
  176. cert_file="$( "${1:-openssl}" version -d )" ||
  177. rvm_requiremnts_fail_always 2 "Failed reading certificates path for '${1:-openssl}' with return code: ($__result)." $? ||
  178. return $?
  179. cert_file="${cert_file#*\"}"
  180. cert_file="${cert_file%\"*}"
  181. cert_file="${cert_file}/cert.pem"
  182. }
  183. __sm.cron.show()
  184. {
  185. EDITOR="\command \cat" crontab -e 2>/dev/null
  186. }
  187. __rvm_cron_find()
  188. {
  189. __sm.cron.show | __rvm_grep "$1" >/dev/null || return $?
  190. }
  191. __rvm_cron_uninstall()
  192. {
  193. __sm.cron.show | __rvm_grep -v "$1" | crontab -
  194. }
  195. __rvm_cron_install()
  196. {
  197. {
  198. __sm.cron.show
  199. echo "@daily $1"
  200. } | crontab -
  201. }
  202. export RVM_OSX_SSL_UPDATER="$rvm_path/bin/rvm --silent osx-ssl-certs update all"