2 # Here is the script to deploy the cert to your cpanel using the cpanel API.
3 # Uses command line uapi. --user option is needed only if run as root.
4 # Returns 0 when success.
5 # Written by Santeri Kannisto <santeri.kannisto@2globalnomads.info>
8 #export DEPLOY_CPANEL_USER=myusername
10 ######## Public functions #####################
12 #domain keyfile certfile cafile fullchain
14 cpanel_uapi_deploy
() {
21 _debug _cdomain
"$_cdomain"
23 _debug _ccert
"$_ccert"
25 _debug _cfullchain
"$_cfullchain"
27 if ! _exists uapi
; then
28 _err
"The command uapi is not found."
31 if ! _exists php
; then
32 _err
"The command php is not found."
35 # read cert and key files and urlencode both
36 _certstr
=$
(cat "$_ccert")
37 _keystr
=$
(cat "$_ckey")
38 _cert
=$
(php
-r "echo urlencode(\"$_certstr\");")
39 _key
=$
(php
-r "echo urlencode(\"$_keystr\");")
44 if [ "$(id -u)" = 0 ]; then
45 if [ -z "$DEPLOY_CPANEL_USER" ]; then
46 _err
"It seems that you are root, please define the target user name: export DEPLOY_CPANEL_USER=username"
49 _savedomainconf DEPLOY_CPANEL_USER
"$DEPLOY_CPANEL_USER"
50 _response
=$
(uapi
--user="$DEPLOY_CPANEL_USER" SSL install_ssl domain
="$_cdomain" cert
="$_cert" key
="$_key")
52 _response
=$
(uapi SSL install_ssl domain
="$_cdomain" cert
="$_cert" key
="$_key")
54 error_response
="status: 0"
55 if test "${_response#*$error_response}" != "$_response"; then
56 _err
"Error in deploying certificate:"
61 _debug response
"$_response"
62 _info
"Certificate successfully deployed"