From b69f385586ffbad59b55e092e4dd6c44fc82dbd4 Mon Sep 17 00:00:00 2001 From: Jordan Wiseman <35307756+jordanwiseman@users.noreply.github.com> Date: Sun, 24 Feb 2019 21:39:06 -0800 Subject: [PATCH 1/8] add postfix, dovecot, and joint PF/DC deploy hooks --- deploy/dovecot.sh | 138 +++++++++++++++++++++----- deploy/postfix-and-dovecot.sh | 180 ++++++++++++++++++++++++++++++++++ deploy/postfix.sh | 113 +++++++++++++++++++++ 3 files changed, 405 insertions(+), 26 deletions(-) create mode 100644 deploy/postfix-and-dovecot.sh create mode 100644 deploy/postfix.sh diff --git a/deploy/dovecot.sh b/deploy/dovecot.sh index 3baf23d9..6edc9520 100644 --- a/deploy/dovecot.sh +++ b/deploy/dovecot.sh @@ -1,26 +1,112 @@ -#!/usr/bin/env sh - -#Here is a script to deploy cert to dovecot server. - -#returns 0 means success, otherwise error. - -######## Public functions ##################### - -#domain keyfile certfile cafile fullchain -dovecot_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" - - _err "Not implemented yet" - return 1 - -} +#!/usr/bin/env sh + +#Here is a script to deploy cert to dovecot servers. + +#returns 0 means success, otherwise error. + +#DEFAULT_DOVECOT_RELOAD="service dovecot restart" +#DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + +######## Public functions ##################### + +#domain keyfile certfile cafile fullchain +dovecot_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" + + _ssl_path="/etc/acme.sh/dovecot" + if ! mkdir -p "$_ssl_path"; then + _err "Can not create folder:$_ssl_path" + return 1 + fi + + _info "Copying key and cert" + _real_key="$_ssl_path/dovecot.key" + if ! cat "$_ckey" >"$_real_key"; then + _err "Error: write key file to: $_real_key" + return 1 + fi + _real_fullchain="$_ssl_path/dovecot.chain.pem" + if ! cat "$_cfullchain" >"$_real_fullchain"; then + _err "Error: write key file to: $_real_fullchain" + return 1 + fi + + DEFAULT_DOVECOT_RELOAD="service dovecot restart" + _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}" + + if [ -z "$IS_RENEW" ]; then + DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}" + + if [ ! -f "$_dovecot_conf" ]; then + if [ -z "$DEPLOY_DOVECOT_CONF" ]; then + _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF" + return 1 + else + _err "It seems that the specified dovecot conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_dovecot_conf" ]; then + _err "The file $_dovecot_conf is not writable, please change the permission." + return 1 + fi + _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak" + _info "Backup $_dovecot_conf to $_backup_dovecot_conf" + cp "$_dovecot_conf" "$_backup_dovecot_conf" + + # dovecot needs the input redirectors ("<") before the filenames here + _info "Modify dovecot conf: $_dovecot_conf" + if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \ + && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \ + && _setopt "$_dovecot_conf" "ssl" "=" "required"; then + _info "Set config success!" + else + _err "Config dovecot server error, please report bug to us." + _info "Restoring dovecot conf" + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restore dovecot conf, please report bug to us." + fi + return 1 + fi + fi + + _info "Run reload: $_reload_dovecot" + if eval "$_reload_dovecot"; then + _info "Reload success!" + if [ "$DEPLOY_DOVECOT_CONF" ]; then + _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF" + else + _cleardomainconf DEPLOY_DOVECOT_CONF + fi + if [ "$DEPLOY_DOVECOT_RELOAD" ]; then + _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD" + else + _cleardomainconf DEPLOY_DOVECOT_RELOAD + fi + return 0 + else + _err "Reload error, restoring conf" + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore dovecot conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restoring dovecot conf, please report bug to us." + fi + return 1 + fi + return 0 +} diff --git a/deploy/postfix-and-dovecot.sh b/deploy/postfix-and-dovecot.sh new file mode 100644 index 00000000..1b282878 --- /dev/null +++ b/deploy/postfix-and-dovecot.sh @@ -0,0 +1,180 @@ +#!/usr/bin/env sh + +#Here is a script to deploy cert to postfix and dovecot servers, when +#they use the same certificate (e.g., when both are on the same host or +#use the same hostname). + +#returns 0 means success, otherwise error. + +#DEFAULT_POSTFIX_RELOAD="service postfix restart" +#DEFAULT_DOVECOT_RELOAD="service dovecot restart" + +#DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" +#DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + + +######## Public functions ##################### + +#domain keyfile certfile cafile fullchain +postfix-and-dovecot_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" + + _ssl_path="/etc/acme.sh/postfix-and-dovecot" + if ! mkdir -p "$_ssl_path"; then + _err "Can not create folder:$_ssl_path" + return 1 + fi + + _info "Copying key and cert" + _real_key="$_ssl_path/postfix-and-dovecot.key" + if ! cat "$_ckey" >"$_real_key"; then + _err "Error: write key file to: $_real_key" + return 1 + fi + _real_fullchain="$_ssl_path/postfix-and-dovecot.chain.pem" + if ! cat "$_cfullchain" >"$_real_fullchain"; then + _err "Error: write key file to: $_real_fullchain" + return 1 + fi + + DEFAULT_POSTFIX_RELOAD="service postfix restart" + _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}" + + DEFAULT_DOVECOT_RELOAD="service dovecot restart" + _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}" + + if [ -z "$IS_RENEW" ]; then + DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" + DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}" + _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}" + + # postfix first + if [ ! -f "$_postfix_conf" ]; then + if [ -z "$DEPLOY_POSTFIX_CONF" ]; then + _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF" + return 1 + else + _err "It seems that the specified postfix conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_postfix_conf" ]; then + _err "The file $_postfix_conf is not writable, please change the permission." + return 1 + fi + _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak" + _info "Backup $_postfix_conf to $_backup_postfix_conf" + cp "$_postfix_conf" "$_backup_postfix_conf" + + _info "Modify postfix conf: $_postfix_conf" + if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \ + && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \ + && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \ + && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then + _info "Set config success!" + else + _err "Config postfix server error, please report bug to us." + _info "Restoring postfix conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore conf success" + eval "$_reload_postfix" + else + _err "Oops, error restore postfix conf, please report bug to us." + fi + return 1 + fi + + # now dovecot + if [ ! -f "$_dovecot_conf" ]; then + if [ -z "$DEPLOY_DOVECOT_CONF" ]; then + _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF" + return 1 + else + _err "It seems that the specified dovecot conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_dovecot_conf" ]; then + _err "The file $_dovecot_conf is not writable, please change the permission." + return 1 + fi + _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak" + _info "Backup $_dovecot_conf to $_backup_dovecot_conf" + cp "$_dovecot_conf" "$_backup_dovecot_conf" + + # dovecot needs the input redirectors ("<") before the filenames here + _info "Modify dovecot conf: $_dovecot_conf" + if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \ + && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \ + && _setopt "$_dovecot_conf" "ssl" "=" "required"; then + _info "Set config success!" + else + _err "Config dovecot server error, please report bug to us." + _info "Restoring dovecot conf" + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restore dovecot conf, please report bug to us." + fi + return 1 + fi + fi + + _info "Run reload: $_reload_postfix && $_reload_dovecot" + if eval "$_reload_postfix && $_reload_dovecot"; then + _info "Reload success!" + if [ "$DEPLOY_POSTFIX_CONF" ]; then + _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF" + else + _cleardomainconf DEPLOY_POSTFIX_CONF + fi + if [ "$DEPLOY_POSTFIX_RELOAD" ]; then + _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD" + else + _cleardomainconf DEPLOY_POSTFIX_RELOAD + fi + return 0 + + if [ "$DEPLOY_DOVECOT_CONF" ]; then + _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF" + else + _cleardomainconf DEPLOY_DOVECOT_CONF + fi + if [ "$DEPLOY_DOVECOT_RELOAD" ]; then + _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD" + else + _cleardomainconf DEPLOY_DOVECOT_RELOAD + fi + return 0 + else + _err "Reload error, restoring conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore postfox conf success" + eval "$_reload_postfix" + else + _err "Oops, error restoring postfix conf, please report bug to us." + fi + return 1 + + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore dovecot conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restoring dovecot conf, please report bug to us." + fi + return 1 + fi + return 0 +} diff --git a/deploy/postfix.sh b/deploy/postfix.sh new file mode 100644 index 00000000..3a43b107 --- /dev/null +++ b/deploy/postfix.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env sh + +#Here is a script to deploy cert to postfix servers. + +#returns 0 means success, otherwise error. + +#DEFAULT_POSTFIX_RELOAD="service postfix restart" +#DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" + + +######## Public functions ##################### + +#domain keyfile certfile cafile fullchain +postfix_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" + + _ssl_path="/etc/acme.sh/postfix" + if ! mkdir -p "$_ssl_path"; then + _err "Can not create folder:$_ssl_path" + return 1 + fi + + _info "Copying key and cert" + _real_key="$_ssl_path/postfix.key" + if ! cat "$_ckey" >"$_real_key"; then + _err "Error: write key file to: $_real_key" + return 1 + fi + _real_fullchain="$_ssl_path/postfix.chain.pem" + if ! cat "$_cfullchain" >"$_real_fullchain"; then + _err "Error: write key file to: $_real_fullchain" + return 1 + fi + + DEFAULT_POSTFIX_RELOAD="service postfix restart" + _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}" + + if [ -z "$IS_RENEW" ]; then + DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" + _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}" + + if [ ! -f "$_postfix_conf" ]; then + if [ -z "$DEPLOY_POSTFIX_CONF" ]; then + _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF" + return 1 + else + _err "It seems that the specified postfix conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_postfix_conf" ]; then + _err "The file $_postfix_conf is not writable, please change the permission." + return 1 + fi + _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak" + _info "Backup $_postfix_conf to $_backup_postfix_conf" + cp "$_postfix_conf" "$_backup_postfix_conf" + + _info "Modify postfix conf: $_postfix_conf" + if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \ + && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \ + && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \ + && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then + _info "Set config success!" + else + _err "Config postfix server error, please report bug to us." + _info "Restoring postfix conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore conf success" + eval "$_reload_postfix" + else + _err "Oops, error restore postfix conf, please report bug to us." + fi + return 1 + fi + fi + + _info "Run reload: $_reload_postfix" + if eval "$_reload_postfix"; then + _info "Reload success!" + if [ "$DEPLOY_POSTFIX_CONF" ]; then + _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF" + else + _cleardomainconf DEPLOY_POSTFIX_CONF + fi + if [ "$DEPLOY_POSTFIX_RELOAD" ]; then + _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD" + else + _cleardomainconf DEPLOY_POSTFIX_RELOAD + fi + return 0 + else + _err "Reload error, restoring conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore postfox conf success" + eval "$_reload_postfix" + else + _err "Oops, error restoring postfix conf, please report bug to us." + fi + return 1 + fi + return 0 +} From 2a1d9fe3b87d509b543d50f08e92b6e43fa3ec68 Mon Sep 17 00:00:00 2001 From: Jordan Wiseman <35307756+jordanwiseman@users.noreply.github.com> Date: Sun, 24 Feb 2019 21:43:05 -0800 Subject: [PATCH 2/8] Update README.md with postfix and dovecot hooks Added sections for postfix, dovecot, and postfix-and-dovecot deploy hooks --- deploy/README.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/deploy/README.md b/deploy/README.md index f290756a..1d84a970 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -391,3 +391,84 @@ acme.sh --deploy --deploy-hook mydevil -d example.com ``` That will remove old certificate and install new one. + + +## 15. Deploy your cert to Postfix + +```sh +acme.sh --deploy -d ftp.example.com --deploy-hook postfix +``` + +The default postfix conf file is `/etc/postfix/main.cf", if your main.cf is +not in the default location, you can specify one: + +```sh +export DEPLOY_POSTFIX_CONF="/other/dir/postfix/main.cf" + +acme.sh --deploy -d ftp.example.com --deploy-hook postfix +``` + +The default command to restart postfix is `service postfix restart`, if +it doesn't work, you can specify one: + +```sh +export DEPLOY_POSTFIX_RELOAD="/etc/init.d/postfix restart" + +acme.sh --deploy -d ftp.example.com --deploy-hook postfix +``` + +## 16. Deploy your cert to Dovecot + +```sh +acme.sh --deploy -d ftp.example.com --deploy-hook dovecot +``` + +The default dovecot conf file is `/etc/dovecot/dovecot.conf", if your dovecot.conf is +not in the default location, you can specify one: + +```sh +export DEPLOY_DOVECOT_CONF="/other/dir/dovecot/dovecot.conf" + +acme.sh --deploy -d ftp.example.com --deploy-hook dovecot +``` + +The default command to restart dovecot is `service dovecot restart`, if +it doesn't work, you can specify one: + +```sh +export DEPLOY_DOVECOT_RELOAD="/etc/init.d/dovecot restart" + +acme.sh --deploy -d ftp.example.com --deploy-hook dovecot +``` + +## 17. Deploy one cert to both Postfix and Dovecot on the same host + +```sh +acme.sh --deploy -d mail.example.com --deploy-hook postfix-and-dovecot +``` + +The default conf files are, respectively: +- `/etc/postfix/main.cf` +- `/etc/dovecot/dovecot.conf` + +If yours are not in the default locations, you can specify new ones: + +```sh +export DEPLOY_POSTFIX_CONF="/other/dir/postfix/main.cf" +export DEPLOY_DOVECOT_CONF="/other/dir/dovecot/dovecot.conf" + +acme.sh --deploy -d mail.example.com --deploy-hook postfix-and-dovecot +``` + +The default commands to restart postfix or dovecot are, respectively: +- `service postfix restart` +- `service dovecot restart` + +If those don't work, you can specify new ones: + +```sh +export DEPLOY_POSTFIX_RELOAD="/etc/init.d/postfix restart" +export DEPLOY_DOVECOT_RELOAD="/etc/init.d/dovecot restart" + +acme.sh --deploy -d mail.example.com --deploy-hook postfix-and-dovecot +``` From 8f7b4bcd9b67b7e12b7be3ce366bcdf16a9893ce Mon Sep 17 00:00:00 2001 From: Jordan Wiseman <35307756+jordanwiseman@users.noreply.github.com> Date: Sun, 24 Feb 2019 22:18:53 -0800 Subject: [PATCH 3/8] change postfix-and-dovecot to postfix_and_dovecot changed "-" to "_" in function name. also changed in file paths for consistency --- deploy/{postfix-and-dovecot.sh => postfix_and_dovecot.sh} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename deploy/{postfix-and-dovecot.sh => postfix_and_dovecot.sh} (93%) diff --git a/deploy/postfix-and-dovecot.sh b/deploy/postfix_and_dovecot.sh similarity index 93% rename from deploy/postfix-and-dovecot.sh rename to deploy/postfix_and_dovecot.sh index 1b282878..435eaa70 100644 --- a/deploy/postfix-and-dovecot.sh +++ b/deploy/postfix_and_dovecot.sh @@ -16,7 +16,7 @@ ######## Public functions ##################### #domain keyfile certfile cafile fullchain -postfix-and-dovecot_deploy() { +postfix_and_dovecot_deploy() { _cdomain="$1" _ckey="$2" _ccert="$3" @@ -29,19 +29,19 @@ postfix-and-dovecot_deploy() { _debug _cca "$_cca" _debug _cfullchain "$_cfullchain" - _ssl_path="/etc/acme.sh/postfix-and-dovecot" + _ssl_path="/etc/acme.sh/postfix_and_dovecot" if ! mkdir -p "$_ssl_path"; then _err "Can not create folder:$_ssl_path" return 1 fi _info "Copying key and cert" - _real_key="$_ssl_path/postfix-and-dovecot.key" + _real_key="$_ssl_path/postfix_and_dovecot.key" if ! cat "$_ckey" >"$_real_key"; then _err "Error: write key file to: $_real_key" return 1 fi - _real_fullchain="$_ssl_path/postfix-and-dovecot.chain.pem" + _real_fullchain="$_ssl_path/postfix_and_dovecot.chain.pem" if ! cat "$_cfullchain" >"$_real_fullchain"; then _err "Error: write key file to: $_real_fullchain" return 1 From e533ac28383a25978b86de94603c32d23680d6c1 Mon Sep 17 00:00:00 2001 From: Jordan Wiseman <35307756+jordanwiseman@users.noreply.github.com> Date: Sun, 24 Feb 2019 22:20:02 -0800 Subject: [PATCH 4/8] changed postfix_and_dovecot here too --- deploy/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deploy/README.md b/deploy/README.md index 1d84a970..2ce46979 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -444,7 +444,7 @@ acme.sh --deploy -d ftp.example.com --deploy-hook dovecot ## 17. Deploy one cert to both Postfix and Dovecot on the same host ```sh -acme.sh --deploy -d mail.example.com --deploy-hook postfix-and-dovecot +acme.sh --deploy -d mail.example.com --deploy-hook postfix_and_dovecot ``` The default conf files are, respectively: @@ -457,7 +457,7 @@ If yours are not in the default locations, you can specify new ones: export DEPLOY_POSTFIX_CONF="/other/dir/postfix/main.cf" export DEPLOY_DOVECOT_CONF="/other/dir/dovecot/dovecot.conf" -acme.sh --deploy -d mail.example.com --deploy-hook postfix-and-dovecot +acme.sh --deploy -d mail.example.com --deploy-hook postfix_and_dovecot ``` The default commands to restart postfix or dovecot are, respectively: @@ -470,5 +470,5 @@ If those don't work, you can specify new ones: export DEPLOY_POSTFIX_RELOAD="/etc/init.d/postfix restart" export DEPLOY_DOVECOT_RELOAD="/etc/init.d/dovecot restart" -acme.sh --deploy -d mail.example.com --deploy-hook postfix-and-dovecot +acme.sh --deploy -d mail.example.com --deploy-hook postfix_and_dovecot ``` From 6c503dab02e32977906856500e3ed12ee0c89fb5 Mon Sep 17 00:00:00 2001 From: Jordan Wiseman Date: Sun, 24 Feb 2019 22:33:50 -0800 Subject: [PATCH 5/8] fix format (crlf to lf) --- deploy/dovecot.sh | 224 ++++++++++----------- deploy/postfix.sh | 226 ++++++++++----------- deploy/postfix_and_dovecot.sh | 360 +++++++++++++++++----------------- 3 files changed, 405 insertions(+), 405 deletions(-) diff --git a/deploy/dovecot.sh b/deploy/dovecot.sh index 6edc9520..c274ab55 100644 --- a/deploy/dovecot.sh +++ b/deploy/dovecot.sh @@ -1,112 +1,112 @@ -#!/usr/bin/env sh - -#Here is a script to deploy cert to dovecot servers. - -#returns 0 means success, otherwise error. - -#DEFAULT_DOVECOT_RELOAD="service dovecot restart" -#DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" - -######## Public functions ##################### - -#domain keyfile certfile cafile fullchain -dovecot_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" - - _ssl_path="/etc/acme.sh/dovecot" - if ! mkdir -p "$_ssl_path"; then - _err "Can not create folder:$_ssl_path" - return 1 - fi - - _info "Copying key and cert" - _real_key="$_ssl_path/dovecot.key" - if ! cat "$_ckey" >"$_real_key"; then - _err "Error: write key file to: $_real_key" - return 1 - fi - _real_fullchain="$_ssl_path/dovecot.chain.pem" - if ! cat "$_cfullchain" >"$_real_fullchain"; then - _err "Error: write key file to: $_real_fullchain" - return 1 - fi - - DEFAULT_DOVECOT_RELOAD="service dovecot restart" - _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}" - - if [ -z "$IS_RENEW" ]; then - DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" - _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}" - - if [ ! -f "$_dovecot_conf" ]; then - if [ -z "$DEPLOY_DOVECOT_CONF" ]; then - _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF" - return 1 - else - _err "It seems that the specified dovecot conf is not valid, please check." - return 1 - fi - fi - if [ ! -w "$_dovecot_conf" ]; then - _err "The file $_dovecot_conf is not writable, please change the permission." - return 1 - fi - _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak" - _info "Backup $_dovecot_conf to $_backup_dovecot_conf" - cp "$_dovecot_conf" "$_backup_dovecot_conf" - - # dovecot needs the input redirectors ("<") before the filenames here - _info "Modify dovecot conf: $_dovecot_conf" - if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \ - && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \ - && _setopt "$_dovecot_conf" "ssl" "=" "required"; then - _info "Set config success!" - else - _err "Config dovecot server error, please report bug to us." - _info "Restoring dovecot conf" - if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then - _info "Restore conf success" - eval "$_reload_dovecot" - else - _err "Oops, error restore dovecot conf, please report bug to us." - fi - return 1 - fi - fi - - _info "Run reload: $_reload_dovecot" - if eval "$_reload_dovecot"; then - _info "Reload success!" - if [ "$DEPLOY_DOVECOT_CONF" ]; then - _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF" - else - _cleardomainconf DEPLOY_DOVECOT_CONF - fi - if [ "$DEPLOY_DOVECOT_RELOAD" ]; then - _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD" - else - _cleardomainconf DEPLOY_DOVECOT_RELOAD - fi - return 0 - else - _err "Reload error, restoring conf" - if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then - _info "Restore dovecot conf success" - eval "$_reload_dovecot" - else - _err "Oops, error restoring dovecot conf, please report bug to us." - fi - return 1 - fi - return 0 -} +#!/usr/bin/env sh + +#Here is a script to deploy cert to dovecot servers. + +#returns 0 means success, otherwise error. + +#DEFAULT_DOVECOT_RELOAD="service dovecot restart" +#DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + +######## Public functions ##################### + +#domain keyfile certfile cafile fullchain +dovecot_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" + + _ssl_path="/etc/acme.sh/dovecot" + if ! mkdir -p "$_ssl_path"; then + _err "Can not create folder:$_ssl_path" + return 1 + fi + + _info "Copying key and cert" + _real_key="$_ssl_path/dovecot.key" + if ! cat "$_ckey" >"$_real_key"; then + _err "Error: write key file to: $_real_key" + return 1 + fi + _real_fullchain="$_ssl_path/dovecot.chain.pem" + if ! cat "$_cfullchain" >"$_real_fullchain"; then + _err "Error: write key file to: $_real_fullchain" + return 1 + fi + + DEFAULT_DOVECOT_RELOAD="service dovecot restart" + _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}" + + if [ -z "$IS_RENEW" ]; then + DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}" + + if [ ! -f "$_dovecot_conf" ]; then + if [ -z "$DEPLOY_DOVECOT_CONF" ]; then + _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF" + return 1 + else + _err "It seems that the specified dovecot conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_dovecot_conf" ]; then + _err "The file $_dovecot_conf is not writable, please change the permission." + return 1 + fi + _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak" + _info "Backup $_dovecot_conf to $_backup_dovecot_conf" + cp "$_dovecot_conf" "$_backup_dovecot_conf" + + # dovecot needs the input redirectors ("<") before the filenames here + _info "Modify dovecot conf: $_dovecot_conf" + if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \ + && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \ + && _setopt "$_dovecot_conf" "ssl" "=" "required"; then + _info "Set config success!" + else + _err "Config dovecot server error, please report bug to us." + _info "Restoring dovecot conf" + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restore dovecot conf, please report bug to us." + fi + return 1 + fi + fi + + _info "Run reload: $_reload_dovecot" + if eval "$_reload_dovecot"; then + _info "Reload success!" + if [ "$DEPLOY_DOVECOT_CONF" ]; then + _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF" + else + _cleardomainconf DEPLOY_DOVECOT_CONF + fi + if [ "$DEPLOY_DOVECOT_RELOAD" ]; then + _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD" + else + _cleardomainconf DEPLOY_DOVECOT_RELOAD + fi + return 0 + else + _err "Reload error, restoring conf" + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore dovecot conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restoring dovecot conf, please report bug to us." + fi + return 1 + fi + return 0 +} diff --git a/deploy/postfix.sh b/deploy/postfix.sh index 3a43b107..97d91fd8 100644 --- a/deploy/postfix.sh +++ b/deploy/postfix.sh @@ -1,113 +1,113 @@ -#!/usr/bin/env sh - -#Here is a script to deploy cert to postfix servers. - -#returns 0 means success, otherwise error. - -#DEFAULT_POSTFIX_RELOAD="service postfix restart" -#DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" - - -######## Public functions ##################### - -#domain keyfile certfile cafile fullchain -postfix_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" - - _ssl_path="/etc/acme.sh/postfix" - if ! mkdir -p "$_ssl_path"; then - _err "Can not create folder:$_ssl_path" - return 1 - fi - - _info "Copying key and cert" - _real_key="$_ssl_path/postfix.key" - if ! cat "$_ckey" >"$_real_key"; then - _err "Error: write key file to: $_real_key" - return 1 - fi - _real_fullchain="$_ssl_path/postfix.chain.pem" - if ! cat "$_cfullchain" >"$_real_fullchain"; then - _err "Error: write key file to: $_real_fullchain" - return 1 - fi - - DEFAULT_POSTFIX_RELOAD="service postfix restart" - _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}" - - if [ -z "$IS_RENEW" ]; then - DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" - _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}" - - if [ ! -f "$_postfix_conf" ]; then - if [ -z "$DEPLOY_POSTFIX_CONF" ]; then - _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF" - return 1 - else - _err "It seems that the specified postfix conf is not valid, please check." - return 1 - fi - fi - if [ ! -w "$_postfix_conf" ]; then - _err "The file $_postfix_conf is not writable, please change the permission." - return 1 - fi - _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak" - _info "Backup $_postfix_conf to $_backup_postfix_conf" - cp "$_postfix_conf" "$_backup_postfix_conf" - - _info "Modify postfix conf: $_postfix_conf" - if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \ - && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \ - && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \ - && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then - _info "Set config success!" - else - _err "Config postfix server error, please report bug to us." - _info "Restoring postfix conf" - if cat "$_backup_postfix_conf" >"$_postfix_conf"; then - _info "Restore conf success" - eval "$_reload_postfix" - else - _err "Oops, error restore postfix conf, please report bug to us." - fi - return 1 - fi - fi - - _info "Run reload: $_reload_postfix" - if eval "$_reload_postfix"; then - _info "Reload success!" - if [ "$DEPLOY_POSTFIX_CONF" ]; then - _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF" - else - _cleardomainconf DEPLOY_POSTFIX_CONF - fi - if [ "$DEPLOY_POSTFIX_RELOAD" ]; then - _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD" - else - _cleardomainconf DEPLOY_POSTFIX_RELOAD - fi - return 0 - else - _err "Reload error, restoring conf" - if cat "$_backup_postfix_conf" >"$_postfix_conf"; then - _info "Restore postfox conf success" - eval "$_reload_postfix" - else - _err "Oops, error restoring postfix conf, please report bug to us." - fi - return 1 - fi - return 0 -} +#!/usr/bin/env sh + +#Here is a script to deploy cert to postfix servers. + +#returns 0 means success, otherwise error. + +#DEFAULT_POSTFIX_RELOAD="service postfix restart" +#DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" + + +######## Public functions ##################### + +#domain keyfile certfile cafile fullchain +postfix_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" + + _ssl_path="/etc/acme.sh/postfix" + if ! mkdir -p "$_ssl_path"; then + _err "Can not create folder:$_ssl_path" + return 1 + fi + + _info "Copying key and cert" + _real_key="$_ssl_path/postfix.key" + if ! cat "$_ckey" >"$_real_key"; then + _err "Error: write key file to: $_real_key" + return 1 + fi + _real_fullchain="$_ssl_path/postfix.chain.pem" + if ! cat "$_cfullchain" >"$_real_fullchain"; then + _err "Error: write key file to: $_real_fullchain" + return 1 + fi + + DEFAULT_POSTFIX_RELOAD="service postfix restart" + _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}" + + if [ -z "$IS_RENEW" ]; then + DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" + _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}" + + if [ ! -f "$_postfix_conf" ]; then + if [ -z "$DEPLOY_POSTFIX_CONF" ]; then + _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF" + return 1 + else + _err "It seems that the specified postfix conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_postfix_conf" ]; then + _err "The file $_postfix_conf is not writable, please change the permission." + return 1 + fi + _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak" + _info "Backup $_postfix_conf to $_backup_postfix_conf" + cp "$_postfix_conf" "$_backup_postfix_conf" + + _info "Modify postfix conf: $_postfix_conf" + if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \ + && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \ + && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \ + && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then + _info "Set config success!" + else + _err "Config postfix server error, please report bug to us." + _info "Restoring postfix conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore conf success" + eval "$_reload_postfix" + else + _err "Oops, error restore postfix conf, please report bug to us." + fi + return 1 + fi + fi + + _info "Run reload: $_reload_postfix" + if eval "$_reload_postfix"; then + _info "Reload success!" + if [ "$DEPLOY_POSTFIX_CONF" ]; then + _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF" + else + _cleardomainconf DEPLOY_POSTFIX_CONF + fi + if [ "$DEPLOY_POSTFIX_RELOAD" ]; then + _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD" + else + _cleardomainconf DEPLOY_POSTFIX_RELOAD + fi + return 0 + else + _err "Reload error, restoring conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore postfox conf success" + eval "$_reload_postfix" + else + _err "Oops, error restoring postfix conf, please report bug to us." + fi + return 1 + fi + return 0 +} diff --git a/deploy/postfix_and_dovecot.sh b/deploy/postfix_and_dovecot.sh index 435eaa70..49a5b3f8 100644 --- a/deploy/postfix_and_dovecot.sh +++ b/deploy/postfix_and_dovecot.sh @@ -1,180 +1,180 @@ -#!/usr/bin/env sh - -#Here is a script to deploy cert to postfix and dovecot servers, when -#they use the same certificate (e.g., when both are on the same host or -#use the same hostname). - -#returns 0 means success, otherwise error. - -#DEFAULT_POSTFIX_RELOAD="service postfix restart" -#DEFAULT_DOVECOT_RELOAD="service dovecot restart" - -#DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" -#DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" - - -######## Public functions ##################### - -#domain keyfile certfile cafile fullchain -postfix_and_dovecot_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" - - _ssl_path="/etc/acme.sh/postfix_and_dovecot" - if ! mkdir -p "$_ssl_path"; then - _err "Can not create folder:$_ssl_path" - return 1 - fi - - _info "Copying key and cert" - _real_key="$_ssl_path/postfix_and_dovecot.key" - if ! cat "$_ckey" >"$_real_key"; then - _err "Error: write key file to: $_real_key" - return 1 - fi - _real_fullchain="$_ssl_path/postfix_and_dovecot.chain.pem" - if ! cat "$_cfullchain" >"$_real_fullchain"; then - _err "Error: write key file to: $_real_fullchain" - return 1 - fi - - DEFAULT_POSTFIX_RELOAD="service postfix restart" - _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}" - - DEFAULT_DOVECOT_RELOAD="service dovecot restart" - _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}" - - if [ -z "$IS_RENEW" ]; then - DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" - DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" - _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}" - _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}" - - # postfix first - if [ ! -f "$_postfix_conf" ]; then - if [ -z "$DEPLOY_POSTFIX_CONF" ]; then - _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF" - return 1 - else - _err "It seems that the specified postfix conf is not valid, please check." - return 1 - fi - fi - if [ ! -w "$_postfix_conf" ]; then - _err "The file $_postfix_conf is not writable, please change the permission." - return 1 - fi - _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak" - _info "Backup $_postfix_conf to $_backup_postfix_conf" - cp "$_postfix_conf" "$_backup_postfix_conf" - - _info "Modify postfix conf: $_postfix_conf" - if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \ - && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \ - && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \ - && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then - _info "Set config success!" - else - _err "Config postfix server error, please report bug to us." - _info "Restoring postfix conf" - if cat "$_backup_postfix_conf" >"$_postfix_conf"; then - _info "Restore conf success" - eval "$_reload_postfix" - else - _err "Oops, error restore postfix conf, please report bug to us." - fi - return 1 - fi - - # now dovecot - if [ ! -f "$_dovecot_conf" ]; then - if [ -z "$DEPLOY_DOVECOT_CONF" ]; then - _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF" - return 1 - else - _err "It seems that the specified dovecot conf is not valid, please check." - return 1 - fi - fi - if [ ! -w "$_dovecot_conf" ]; then - _err "The file $_dovecot_conf is not writable, please change the permission." - return 1 - fi - _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak" - _info "Backup $_dovecot_conf to $_backup_dovecot_conf" - cp "$_dovecot_conf" "$_backup_dovecot_conf" - - # dovecot needs the input redirectors ("<") before the filenames here - _info "Modify dovecot conf: $_dovecot_conf" - if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \ - && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \ - && _setopt "$_dovecot_conf" "ssl" "=" "required"; then - _info "Set config success!" - else - _err "Config dovecot server error, please report bug to us." - _info "Restoring dovecot conf" - if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then - _info "Restore conf success" - eval "$_reload_dovecot" - else - _err "Oops, error restore dovecot conf, please report bug to us." - fi - return 1 - fi - fi - - _info "Run reload: $_reload_postfix && $_reload_dovecot" - if eval "$_reload_postfix && $_reload_dovecot"; then - _info "Reload success!" - if [ "$DEPLOY_POSTFIX_CONF" ]; then - _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF" - else - _cleardomainconf DEPLOY_POSTFIX_CONF - fi - if [ "$DEPLOY_POSTFIX_RELOAD" ]; then - _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD" - else - _cleardomainconf DEPLOY_POSTFIX_RELOAD - fi - return 0 - - if [ "$DEPLOY_DOVECOT_CONF" ]; then - _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF" - else - _cleardomainconf DEPLOY_DOVECOT_CONF - fi - if [ "$DEPLOY_DOVECOT_RELOAD" ]; then - _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD" - else - _cleardomainconf DEPLOY_DOVECOT_RELOAD - fi - return 0 - else - _err "Reload error, restoring conf" - if cat "$_backup_postfix_conf" >"$_postfix_conf"; then - _info "Restore postfox conf success" - eval "$_reload_postfix" - else - _err "Oops, error restoring postfix conf, please report bug to us." - fi - return 1 - - if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then - _info "Restore dovecot conf success" - eval "$_reload_dovecot" - else - _err "Oops, error restoring dovecot conf, please report bug to us." - fi - return 1 - fi - return 0 -} +#!/usr/bin/env sh + +#Here is a script to deploy cert to postfix and dovecot servers, when +#they use the same certificate (e.g., when both are on the same host or +#use the same hostname). + +#returns 0 means success, otherwise error. + +#DEFAULT_POSTFIX_RELOAD="service postfix restart" +#DEFAULT_DOVECOT_RELOAD="service dovecot restart" + +#DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" +#DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + + +######## Public functions ##################### + +#domain keyfile certfile cafile fullchain +postfix_and_dovecot_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" + + _ssl_path="/etc/acme.sh/postfix_and_dovecot" + if ! mkdir -p "$_ssl_path"; then + _err "Can not create folder:$_ssl_path" + return 1 + fi + + _info "Copying key and cert" + _real_key="$_ssl_path/postfix_and_dovecot.key" + if ! cat "$_ckey" >"$_real_key"; then + _err "Error: write key file to: $_real_key" + return 1 + fi + _real_fullchain="$_ssl_path/postfix_and_dovecot.chain.pem" + if ! cat "$_cfullchain" >"$_real_fullchain"; then + _err "Error: write key file to: $_real_fullchain" + return 1 + fi + + DEFAULT_POSTFIX_RELOAD="service postfix restart" + _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}" + + DEFAULT_DOVECOT_RELOAD="service dovecot restart" + _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}" + + if [ -z "$IS_RENEW" ]; then + DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" + DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" + _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}" + _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}" + + # postfix first + if [ ! -f "$_postfix_conf" ]; then + if [ -z "$DEPLOY_POSTFIX_CONF" ]; then + _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF" + return 1 + else + _err "It seems that the specified postfix conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_postfix_conf" ]; then + _err "The file $_postfix_conf is not writable, please change the permission." + return 1 + fi + _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak" + _info "Backup $_postfix_conf to $_backup_postfix_conf" + cp "$_postfix_conf" "$_backup_postfix_conf" + + _info "Modify postfix conf: $_postfix_conf" + if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \ + && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \ + && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \ + && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then + _info "Set config success!" + else + _err "Config postfix server error, please report bug to us." + _info "Restoring postfix conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore conf success" + eval "$_reload_postfix" + else + _err "Oops, error restore postfix conf, please report bug to us." + fi + return 1 + fi + + # now dovecot + if [ ! -f "$_dovecot_conf" ]; then + if [ -z "$DEPLOY_DOVECOT_CONF" ]; then + _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF" + return 1 + else + _err "It seems that the specified dovecot conf is not valid, please check." + return 1 + fi + fi + if [ ! -w "$_dovecot_conf" ]; then + _err "The file $_dovecot_conf is not writable, please change the permission." + return 1 + fi + _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak" + _info "Backup $_dovecot_conf to $_backup_dovecot_conf" + cp "$_dovecot_conf" "$_backup_dovecot_conf" + + # dovecot needs the input redirectors ("<") before the filenames here + _info "Modify dovecot conf: $_dovecot_conf" + if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \ + && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \ + && _setopt "$_dovecot_conf" "ssl" "=" "required"; then + _info "Set config success!" + else + _err "Config dovecot server error, please report bug to us." + _info "Restoring dovecot conf" + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restore dovecot conf, please report bug to us." + fi + return 1 + fi + fi + + _info "Run reload: $_reload_postfix && $_reload_dovecot" + if eval "$_reload_postfix && $_reload_dovecot"; then + _info "Reload success!" + if [ "$DEPLOY_POSTFIX_CONF" ]; then + _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF" + else + _cleardomainconf DEPLOY_POSTFIX_CONF + fi + if [ "$DEPLOY_POSTFIX_RELOAD" ]; then + _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD" + else + _cleardomainconf DEPLOY_POSTFIX_RELOAD + fi + return 0 + + if [ "$DEPLOY_DOVECOT_CONF" ]; then + _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF" + else + _cleardomainconf DEPLOY_DOVECOT_CONF + fi + if [ "$DEPLOY_DOVECOT_RELOAD" ]; then + _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD" + else + _cleardomainconf DEPLOY_DOVECOT_RELOAD + fi + return 0 + else + _err "Reload error, restoring conf" + if cat "$_backup_postfix_conf" >"$_postfix_conf"; then + _info "Restore postfox conf success" + eval "$_reload_postfix" + else + _err "Oops, error restoring postfix conf, please report bug to us." + fi + return 1 + + if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then + _info "Restore dovecot conf success" + eval "$_reload_dovecot" + else + _err "Oops, error restoring dovecot conf, please report bug to us." + fi + return 1 + fi + return 0 +} From 0177584ea671d482506b6da5c19a64876b5209ab Mon Sep 17 00:00:00 2001 From: Jordan Wiseman Date: Sun, 24 Feb 2019 22:50:37 -0800 Subject: [PATCH 6/8] removed extra blank line ('cause git diff) --- deploy/postfix.sh | 1 - deploy/postfix_and_dovecot.sh | 1 - 2 files changed, 2 deletions(-) diff --git a/deploy/postfix.sh b/deploy/postfix.sh index 97d91fd8..ed8c897e 100644 --- a/deploy/postfix.sh +++ b/deploy/postfix.sh @@ -7,7 +7,6 @@ #DEFAULT_POSTFIX_RELOAD="service postfix restart" #DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" - ######## Public functions ##################### #domain keyfile certfile cafile fullchain diff --git a/deploy/postfix_and_dovecot.sh b/deploy/postfix_and_dovecot.sh index 49a5b3f8..99ed58f4 100644 --- a/deploy/postfix_and_dovecot.sh +++ b/deploy/postfix_and_dovecot.sh @@ -12,7 +12,6 @@ #DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" #DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" - ######## Public functions ##################### #domain keyfile certfile cafile fullchain From cb92c27f604a0fd415e07d47e60cdb1148549b93 Mon Sep 17 00:00:00 2001 From: Jordan Wiseman <35307756+jordanwiseman@users.noreply.github.com> Date: Wed, 27 Feb 2019 15:38:58 -0800 Subject: [PATCH 7/8] nop --- deploy/postfix_and_dovecot.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/postfix_and_dovecot.sh b/deploy/postfix_and_dovecot.sh index 99ed58f4..c009e901 100644 --- a/deploy/postfix_and_dovecot.sh +++ b/deploy/postfix_and_dovecot.sh @@ -1,6 +1,6 @@ #!/usr/bin/env sh -#Here is a script to deploy cert to postfix and dovecot servers, when +#Here is a script to deploy a cert to postfix and dovecot servers, when #they use the same certificate (e.g., when both are on the same host or #use the same hostname). From dce64b0667b1f3b167d76ad313171fd5566343b5 Mon Sep 17 00:00:00 2001 From: Jordan Wiseman Date: Sun, 10 Mar 2019 21:00:49 -0700 Subject: [PATCH 8/8] remove unnecessary dual service deploy script --- deploy/postfix_and_dovecot.sh | 179 ---------------------------------- 1 file changed, 179 deletions(-) delete mode 100644 deploy/postfix_and_dovecot.sh diff --git a/deploy/postfix_and_dovecot.sh b/deploy/postfix_and_dovecot.sh deleted file mode 100644 index c009e901..00000000 --- a/deploy/postfix_and_dovecot.sh +++ /dev/null @@ -1,179 +0,0 @@ -#!/usr/bin/env sh - -#Here is a script to deploy a cert to postfix and dovecot servers, when -#they use the same certificate (e.g., when both are on the same host or -#use the same hostname). - -#returns 0 means success, otherwise error. - -#DEFAULT_POSTFIX_RELOAD="service postfix restart" -#DEFAULT_DOVECOT_RELOAD="service dovecot restart" - -#DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" -#DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" - -######## Public functions ##################### - -#domain keyfile certfile cafile fullchain -postfix_and_dovecot_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" - - _ssl_path="/etc/acme.sh/postfix_and_dovecot" - if ! mkdir -p "$_ssl_path"; then - _err "Can not create folder:$_ssl_path" - return 1 - fi - - _info "Copying key and cert" - _real_key="$_ssl_path/postfix_and_dovecot.key" - if ! cat "$_ckey" >"$_real_key"; then - _err "Error: write key file to: $_real_key" - return 1 - fi - _real_fullchain="$_ssl_path/postfix_and_dovecot.chain.pem" - if ! cat "$_cfullchain" >"$_real_fullchain"; then - _err "Error: write key file to: $_real_fullchain" - return 1 - fi - - DEFAULT_POSTFIX_RELOAD="service postfix restart" - _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}" - - DEFAULT_DOVECOT_RELOAD="service dovecot restart" - _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}" - - if [ -z "$IS_RENEW" ]; then - DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf" - DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf" - _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}" - _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}" - - # postfix first - if [ ! -f "$_postfix_conf" ]; then - if [ -z "$DEPLOY_POSTFIX_CONF" ]; then - _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF" - return 1 - else - _err "It seems that the specified postfix conf is not valid, please check." - return 1 - fi - fi - if [ ! -w "$_postfix_conf" ]; then - _err "The file $_postfix_conf is not writable, please change the permission." - return 1 - fi - _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak" - _info "Backup $_postfix_conf to $_backup_postfix_conf" - cp "$_postfix_conf" "$_backup_postfix_conf" - - _info "Modify postfix conf: $_postfix_conf" - if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \ - && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \ - && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \ - && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then - _info "Set config success!" - else - _err "Config postfix server error, please report bug to us." - _info "Restoring postfix conf" - if cat "$_backup_postfix_conf" >"$_postfix_conf"; then - _info "Restore conf success" - eval "$_reload_postfix" - else - _err "Oops, error restore postfix conf, please report bug to us." - fi - return 1 - fi - - # now dovecot - if [ ! -f "$_dovecot_conf" ]; then - if [ -z "$DEPLOY_DOVECOT_CONF" ]; then - _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF" - return 1 - else - _err "It seems that the specified dovecot conf is not valid, please check." - return 1 - fi - fi - if [ ! -w "$_dovecot_conf" ]; then - _err "The file $_dovecot_conf is not writable, please change the permission." - return 1 - fi - _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak" - _info "Backup $_dovecot_conf to $_backup_dovecot_conf" - cp "$_dovecot_conf" "$_backup_dovecot_conf" - - # dovecot needs the input redirectors ("<") before the filenames here - _info "Modify dovecot conf: $_dovecot_conf" - if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \ - && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \ - && _setopt "$_dovecot_conf" "ssl" "=" "required"; then - _info "Set config success!" - else - _err "Config dovecot server error, please report bug to us." - _info "Restoring dovecot conf" - if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then - _info "Restore conf success" - eval "$_reload_dovecot" - else - _err "Oops, error restore dovecot conf, please report bug to us." - fi - return 1 - fi - fi - - _info "Run reload: $_reload_postfix && $_reload_dovecot" - if eval "$_reload_postfix && $_reload_dovecot"; then - _info "Reload success!" - if [ "$DEPLOY_POSTFIX_CONF" ]; then - _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF" - else - _cleardomainconf DEPLOY_POSTFIX_CONF - fi - if [ "$DEPLOY_POSTFIX_RELOAD" ]; then - _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD" - else - _cleardomainconf DEPLOY_POSTFIX_RELOAD - fi - return 0 - - if [ "$DEPLOY_DOVECOT_CONF" ]; then - _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF" - else - _cleardomainconf DEPLOY_DOVECOT_CONF - fi - if [ "$DEPLOY_DOVECOT_RELOAD" ]; then - _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD" - else - _cleardomainconf DEPLOY_DOVECOT_RELOAD - fi - return 0 - else - _err "Reload error, restoring conf" - if cat "$_backup_postfix_conf" >"$_postfix_conf"; then - _info "Restore postfox conf success" - eval "$_reload_postfix" - else - _err "Oops, error restoring postfix conf, please report bug to us." - fi - return 1 - - if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then - _info "Restore dovecot conf success" - eval "$_reload_dovecot" - else - _err "Oops, error restoring dovecot conf, please report bug to us." - fi - return 1 - fi - return 0 -}