You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.2 KiB

  1. #!/usr/bin/env sh
  2. # Script to deploy certificate to KeyHelp
  3. # This deployment required following variables
  4. # export DEPLOY_KEYHELP_BASEURL="https://keyhelp.example.com"
  5. # export DEPLOY_KEYHELP_USERNAME="Your KeyHelp Username"
  6. # export DEPLOY_KEYHELP_PASSWORD="Your KeyHelp Password"
  7. # export DEPLOY_KEYHELP_DOMAIN_ID="Depoly certificate to this Domain ID"
  8. # Open the 'Edit domain' page, and you will see id=xxx at the end of the URL. This is the Domain ID.
  9. # https://DEPLOY_KEYHELP_BASEURL/index.php?page=domains&action=edit&id=xxx
  10. # If have more than one domain name
  11. # export DEPLOY_KEYHELP_DOMAIN_ID="111 222 333"
  12. keyhelp_deploy() {
  13. _cdomain="$1"
  14. _ckey="$2"
  15. _ccert="$3"
  16. _cca="$4"
  17. _cfullchain="$5"
  18. _debug _cdomain "$_cdomain"
  19. _debug _ckey "$_ckey"
  20. _debug _ccert "$_ccert"
  21. _debug _cca "$_cca"
  22. _debug _cfullchain "$_cfullchain"
  23. if [ -z "$DEPLOY_KEYHELP_BASEURL" ]; then
  24. _err "DEPLOY_KEYHELP_BASEURL is not defined."
  25. return 1
  26. else
  27. _savedomainconf DEPLOY_KEYHELP_BASEURL "$DEPLOY_KEYHELP_BASEURL"
  28. fi
  29. if [ -z "$DEPLOY_KEYHELP_USERNAME" ]; then
  30. _err "DEPLOY_KEYHELP_USERNAME is not defined."
  31. return 1
  32. else
  33. _savedomainconf DEPLOY_KEYHELP_USERNAME "$DEPLOY_KEYHELP_USERNAME"
  34. fi
  35. if [ -z "$DEPLOY_KEYHELP_PASSWORD" ]; then
  36. _err "DEPLOY_KEYHELP_PASSWORD is not defined."
  37. return 1
  38. else
  39. _savedomainconf DEPLOY_KEYHELP_PASSWORD "$DEPLOY_KEYHELP_PASSWORD"
  40. fi
  41. if [ -z "$DEPLOY_KEYHELP_DOMAIN_ID" ]; then
  42. _err "DEPLOY_KEYHELP_DOMAIN_ID is not defined."
  43. return 1
  44. else
  45. _savedomainconf DEPLOY_KEYHELP_DOMAIN_ID "$DEPLOY_KEYHELP_DOMAIN_ID"
  46. fi
  47. _info "Logging in to keyhelp panel"
  48. username_encoded="$(printf "%s" "${DEPLOY_KEYHELP_USERNAME}" | _url_encode)"
  49. password_encoded="$(printf "%s" "${DEPLOY_KEYHELP_PASSWORD}" | _url_encode)"
  50. _H1="Content-Type: application/x-www-form-urlencoded"
  51. _response=$(_get "$DEPLOY_KEYHELP_BASEURL/index.php?submit=1&username=$username_encoded&password=$password_encoded" "TRUE")
  52. _cookie="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2)"
  53. # If cookies is not empty then logon successful
  54. if [ -z "$_cookie" ]; then
  55. _err "Fail to get cookie."
  56. return 1
  57. fi
  58. _debug "cookie" "$_cookie"
  59. _info "Uploading certificate"
  60. _date=$(date +"%Y%m%d")
  61. encoded_key="$(_url_encode <"$_ckey")"
  62. encoded_ccert="$(_url_encode <"$_ccert")"
  63. encoded_cca="$(_url_encode <"$_cca")"
  64. certificate_name="$_cdomain-$_date"
  65. _request_body="submit=1&certificate_name=$certificate_name&add_type=upload&text_private_key=$encoded_key&text_certificate=$encoded_ccert&text_ca_certificate=$encoded_cca"
  66. _H1="Cookie: $_cookie"
  67. _response=$(_post "$_request_body" "$DEPLOY_KEYHELP_BASEURL/index.php?page=ssl_certificates&action=add" "" "POST")
  68. _message=$(echo "$_response" | grep -A 2 'message-body' | sed -n '/<div class="message-body ">/,/<\/div>/{//!p;}' | sed 's/<[^>]*>//g' | sed 's/^ *//;s/ *$//')
  69. _info "_message" "$_message"
  70. if [ -z "$_message" ]; then
  71. _err "Fail to upload certificate."
  72. return 1
  73. fi
  74. for DOMAIN_ID in $DEPLOY_KEYHELP_DOMAIN_ID; do
  75. _info "Apply certificate to domain id $DOMAIN_ID"
  76. _response=$(_get "$DEPLOY_KEYHELP_BASEURL/index.php?page=domains&action=edit&id=$DOMAIN_ID")
  77. cert_value=$(echo "$_response" | grep "$certificate_name" | sed -n 's/.*value="\([^"]*\).*/\1/p')
  78. target_type=$(echo "$_response" | grep 'target_type' | grep 'checked' | sed -n 's/.*value="\([^"]*\).*/\1/p')
  79. _debug "cert_value" "$cert_value"
  80. if [ -z "$cert_value" ]; then
  81. _err "Fail to get certificate id."
  82. return 1
  83. fi
  84. _request_body="submit=1&id=$DOMAIN_ID&target_type=$target_type&certificate_type=custom&certificate_id=$cert_value"
  85. _response=$(_post "$_request_body" "$DEPLOY_KEYHELP_BASEURL/index.php?page=domains&action=edit" "" "POST")
  86. _message=$(echo "$_response" | grep -A 2 'message-body' | sed -n '/<div class="message-body ">/,/<\/div>/{//!p;}' | sed 's/<[^>]*>//g' | sed 's/^ *//;s/ *$//')
  87. _info "_message" "$_message"
  88. if [ -z "$_message" ]; then
  89. _err "Fail to apply certificate."
  90. return 1
  91. fi
  92. done
  93. _info "Domain $_cdomain certificate successfully deployed to KeyHelp Domain ID $DEPLOY_KEYHELP_DOMAIN_ID."
  94. return 0
  95. }