2019-03-19 17:42:47 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
#Here is a script to deploy cert to mailcow.
|
|
|
|
|
|
|
|
#returns 0 means success, otherwise error.
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#domain keyfile certfile cafile fullchain
|
|
|
|
mailcow_deploy() {
|
|
|
|
_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"
|
|
|
|
|
2022-04-10 22:29:55 +00:00
|
|
|
_getdeployconf DEPLOY_MAILCOW_PATH
|
|
|
|
_getdeployconf DEPLOY_MAILCOW_RELOAD
|
2019-03-19 18:09:25 +00:00
|
|
|
|
2022-04-10 22:29:55 +00:00
|
|
|
_debug DEPLOY_MAILCOW_PATH "$DEPLOY_MAILCOW_PATH"
|
|
|
|
_debug DEPLOY_MAILCOW_RELOAD "$DEPLOY_MAILCOW_RELOAD"
|
|
|
|
|
|
|
|
if [ -z "$DEPLOY_MAILCOW_PATH" ]; then
|
2019-03-19 18:15:31 +00:00
|
|
|
_err "Mailcow path is not found, please define DEPLOY_MAILCOW_PATH."
|
|
|
|
return 1
|
2019-03-19 18:09:25 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-10 22:29:55 +00:00
|
|
|
_savedeployconf DEPLOY_MAILCOW_PATH "$DEPLOY_MAILCOW_PATH"
|
|
|
|
[ -n "$DEPLOY_MAILCOW_RELOAD" ] && _savedeployconf DEPLOY_MAILCOW_RELOAD "$DEPLOY_MAILCOW_RELOAD"
|
|
|
|
|
2022-04-11 09:39:21 +00:00
|
|
|
_ssl_path="$DEPLOY_MAILCOW_PATH"
|
2022-04-10 22:29:55 +00:00
|
|
|
if [ -f "$DEPLOY_MAILCOW_PATH/generate_config.sh" ]; then
|
|
|
|
_ssl_path="$DEPLOY_MAILCOW_PATH/data/assets/ssl/"
|
2020-12-01 19:30:56 +00:00
|
|
|
fi
|
|
|
|
|
2019-03-19 18:09:25 +00:00
|
|
|
if [ ! -d "$_ssl_path" ]; then
|
2019-03-19 18:15:31 +00:00
|
|
|
_err "Cannot find mailcow ssl path: $_ssl_path"
|
|
|
|
return 1
|
2019-03-19 17:42:47 +00:00
|
|
|
fi
|
|
|
|
|
2020-12-01 19:30:56 +00:00
|
|
|
# ECC or RSA
|
2022-04-11 09:39:21 +00:00
|
|
|
length=$(_readdomainconf Le_Keylength)
|
|
|
|
if _isEccKey "$length"; then
|
2020-12-07 13:41:08 +00:00
|
|
|
_info "ECC key type detected"
|
|
|
|
_cert_name_prefix="ecdsa-"
|
2020-12-01 19:30:56 +00:00
|
|
|
else
|
2020-12-07 13:41:08 +00:00
|
|
|
_info "RSA key type detected"
|
|
|
|
_cert_name_prefix=""
|
2020-12-01 19:30:56 +00:00
|
|
|
fi
|
2022-04-11 09:39:21 +00:00
|
|
|
|
2019-03-19 17:42:47 +00:00
|
|
|
_info "Copying key and cert"
|
2020-12-01 19:30:56 +00:00
|
|
|
_real_key="$_ssl_path/${_cert_name_prefix}key.pem"
|
2019-03-19 17:42:47 +00:00
|
|
|
if ! cat "$_ckey" >"$_real_key"; then
|
|
|
|
_err "Error: write key file to: $_real_key"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2020-12-01 19:30:56 +00:00
|
|
|
_real_fullchain="$_ssl_path/${_cert_name_prefix}cert.pem"
|
2019-03-19 17:42:47 +00:00
|
|
|
if ! cat "$_cfullchain" >"$_real_fullchain"; then
|
|
|
|
_err "Error: write cert file to: $_real_fullchain"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2022-04-10 22:29:55 +00:00
|
|
|
DEFAULT_MAILCOW_RELOAD="docker restart \$(docker ps --quiet --filter name=nginx-mailcow --filter name=dovecot-mailcow)"
|
2019-03-19 17:42:47 +00:00
|
|
|
_reload="${DEPLOY_MAILCOW_RELOAD:-$DEFAULT_MAILCOW_RELOAD}"
|
|
|
|
|
|
|
|
_info "Run reload: $_reload"
|
|
|
|
if eval "$_reload"; then
|
|
|
|
_info "Reload success!"
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|