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.

131 lines
5.0 KiB

7 months ago
7 months ago
  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. # Optional DEPLOY_KEYHELP_ENFORCE_HTTPS
  48. _getdeployconf DEPLOY_KEYHELP_ENFORCE_HTTPS
  49. # set default values for DEPLOY_KEYHELP_ENFORCE_HTTPS
  50. [ -n "${DEPLOY_KEYHELP_ENFORCE_HTTPS}" ] || DEPLOY_KEYHELP_ENFORCE_HTTPS="1"
  51. _info "Logging in to keyhelp panel"
  52. username_encoded="$(printf "%s" "${DEPLOY_KEYHELP_USERNAME}" | _url_encode)"
  53. password_encoded="$(printf "%s" "${DEPLOY_KEYHELP_PASSWORD}" | _url_encode)"
  54. _H1="Content-Type: application/x-www-form-urlencoded"
  55. _response=$(_get "$DEPLOY_KEYHELP_BASEURL/index.php?submit=1&username=$username_encoded&password=$password_encoded" "TRUE")
  56. _cookie="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2)"
  57. # If cookies is not empty then logon successful
  58. if [ -z "$_cookie" ]; then
  59. _err "Fail to get cookie."
  60. return 1
  61. fi
  62. _debug "cookie" "$_cookie"
  63. _info "Uploading certificate"
  64. _date=$(date +"%Y%m%d")
  65. encoded_key="$(_url_encode <"$_ckey")"
  66. encoded_ccert="$(_url_encode <"$_ccert")"
  67. encoded_cca="$(_url_encode <"$_cca")"
  68. certificate_name="$_cdomain-$_date"
  69. _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"
  70. _H1="Cookie: $_cookie"
  71. _response=$(_post "$_request_body" "$DEPLOY_KEYHELP_BASEURL/index.php?page=ssl_certificates&action=add" "" "POST")
  72. _message=$(echo "$_response" | grep -A 2 'message-body' | sed -n '/<div class="message-body ">/,/<\/div>/{//!p;}' | sed 's/<[^>]*>//g' | sed 's/^ *//;s/ *$//')
  73. _info "_message" "$_message"
  74. if [ -z "$_message" ]; then
  75. _err "Fail to upload certificate."
  76. return 1
  77. fi
  78. for DOMAIN_ID in $DEPLOY_KEYHELP_DOMAIN_ID; do
  79. _info "Apply certificate to domain id $DOMAIN_ID"
  80. _response=$(_get "$DEPLOY_KEYHELP_BASEURL/index.php?page=domains&action=edit&id=$DOMAIN_ID")
  81. cert_value=$(echo "$_response" | grep "$certificate_name" | sed -n 's/.*value="\([^"]*\).*/\1/p')
  82. target_type=$(echo "$_response" | grep 'target_type' | grep 'checked' | sed -n 's/.*value="\([^"]*\).*/\1/p')
  83. if [ "$target_type" = "directory" ]; then
  84. path=$(echo "$_response" | awk '/name="path"/{getline; print}' | sed -n 's/.*value="\([^"]*\).*/\1/p')
  85. fi
  86. echo "$_response" | grep "is_prefer_https" | grep "checked" >/dev/null
  87. if [ $? -eq 0 ]; then
  88. is_prefer_https=1
  89. else
  90. is_prefer_https=0
  91. fi
  92. echo "$_response" | grep "hsts_enabled" | grep "checked" >/dev/null
  93. if [ $? -eq 0 ]; then
  94. hsts_enabled=1
  95. else
  96. hsts_enabled=0
  97. fi
  98. _debug "cert_value" "$cert_value"
  99. if [ -z "$cert_value" ]; then
  100. _err "Fail to get certificate id."
  101. return 1
  102. fi
  103. _request_body="submit=1&id=$DOMAIN_ID&target_type=$target_type&path=$path&is_prefer_https=$is_prefer_https&hsts_enabled=$hsts_enabled&certificate_type=custom&certificate_id=$cert_value&enforce_https=$DEPLOY_KEYHELP_ENFORCE_HTTPS"
  104. _response=$(_post "$_request_body" "$DEPLOY_KEYHELP_BASEURL/index.php?page=domains&action=edit" "" "POST")
  105. _message=$(echo "$_response" | grep -A 2 'message-body' | sed -n '/<div class="message-body ">/,/<\/div>/{//!p;}' | sed 's/<[^>]*>//g' | sed 's/^ *//;s/ *$//')
  106. _info "_message" "$_message"
  107. if [ -z "$_message" ]; then
  108. _err "Fail to apply certificate."
  109. return 1
  110. fi
  111. done
  112. _info "Domain $_cdomain certificate successfully deployed to KeyHelp Domain ID $DEPLOY_KEYHELP_DOMAIN_ID."
  113. return 0
  114. }