2017-07-10 13:36:16 +02:00
|
|
|
#!/usr/bin/env sh
|
2017-07-02 21:24:14 +02:00
|
|
|
# Here is the script to deploy the cert to your cpanel using the cpanel API.
|
2017-07-10 14:43:42 +02:00
|
|
|
# Uses command line uapi. --user option is needed only if run as root.
|
2017-07-10 13:36:16 +02:00
|
|
|
# Returns 0 when success.
|
2017-07-02 21:24:14 +02:00
|
|
|
# Written by Santeri Kannisto <santeri.kannisto@2globalnomads.info>
|
|
|
|
# Public domain, 2017
|
2017-02-11 06:24:00 +01:00
|
|
|
|
|
|
|
#export DEPLOY_CPANEL_USER=myusername
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#domain keyfile certfile cafile fullchain
|
2017-07-02 21:24:14 +02:00
|
|
|
|
2017-08-25 14:51:31 +02:00
|
|
|
cpanel_uapi_deploy() {
|
2017-02-11 06:24:00 +01:00
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
_debug _ckey "$_ckey"
|
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
2017-08-25 14:51:31 +02:00
|
|
|
if ! _exists uapi; then
|
|
|
|
_err "The command uapi is not found."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if ! _exists php; then
|
|
|
|
_err "The command php is not found."
|
|
|
|
return 1
|
|
|
|
fi
|
2017-07-02 21:24:14 +02:00
|
|
|
# read cert and key files and urlencode both
|
2017-07-10 13:36:16 +02:00
|
|
|
_certstr=$(cat "$_ccert")
|
|
|
|
_keystr=$(cat "$_ckey")
|
2017-07-02 21:24:14 +02:00
|
|
|
_cert=$(php -r "echo urlencode(\"$_certstr\");")
|
|
|
|
_key=$(php -r "echo urlencode(\"$_keystr\");")
|
|
|
|
|
|
|
|
_debug _cert "$_cert"
|
|
|
|
_debug _key "$_key"
|
|
|
|
|
2017-07-10 13:36:16 +02:00
|
|
|
if [ "$(id -u)" = 0 ]; then
|
2017-08-25 14:51:31 +02:00
|
|
|
if [ -z "$DEPLOY_CPANEL_USER" ]; then
|
|
|
|
_err "It seems that you are root, please define the target user name: export DEPLOY_CPANEL_USER=username"
|
2017-08-25 14:54:10 +02:00
|
|
|
return 1
|
2017-08-25 14:51:31 +02:00
|
|
|
fi
|
|
|
|
_savedomainconf DEPLOY_CPANEL_USER "$DEPLOY_CPANEL_USER"
|
2017-07-10 14:43:42 +02:00
|
|
|
_response=$(uapi --user="$DEPLOY_CPANEL_USER" SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
|
|
|
|
else
|
|
|
|
_response=$(uapi SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
|
2017-07-10 13:36:16 +02:00
|
|
|
fi
|
2017-07-02 21:24:14 +02:00
|
|
|
|
2017-07-10 13:36:16 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
2017-07-02 21:24:14 +02:00
|
|
|
_err "Error in deploying certificate:"
|
2017-07-10 13:36:16 +02:00
|
|
|
_err "$_response"
|
2017-07-02 21:24:14 +02:00
|
|
|
return 1
|
|
|
|
fi
|
2017-02-11 06:24:00 +01:00
|
|
|
|
2017-07-10 13:36:16 +02:00
|
|
|
_debug response "$_response"
|
2017-07-02 21:24:14 +02:00
|
|
|
_info "Certificate successfully deployed"
|
2017-07-10 13:36:16 +02:00
|
|
|
return 0
|
2017-02-11 06:24:00 +01:00
|
|
|
}
|