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