You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.3 KiB

  1. #!/usr/bin/env sh
  2. #Here is a script to deploy cert to dovecot servers.
  3. #returns 0 means success, otherwise error.
  4. #DEFAULT_DOVECOT_RELOAD="service dovecot restart"
  5. #DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf"
  6. ######## Public functions #####################
  7. #domain keyfile certfile cafile fullchain
  8. dovecot_deploy() {
  9. _cdomain="$1"
  10. _ckey="$2"
  11. _ccert="$3"
  12. _cca="$4"
  13. _cfullchain="$5"
  14. _debug _cdomain "$_cdomain"
  15. _debug _ckey "$_ckey"
  16. _debug _ccert "$_ccert"
  17. _debug _cca "$_cca"
  18. _debug _cfullchain "$_cfullchain"
  19. _ssl_path="/etc/acme.sh/dovecot"
  20. if ! mkdir -p "$_ssl_path"; then
  21. _err "Can not create folder:$_ssl_path"
  22. return 1
  23. fi
  24. _info "Copying key and cert"
  25. _real_key="$_ssl_path/dovecot.key"
  26. if ! cat "$_ckey" >"$_real_key"; then
  27. _err "Error: write key file to: $_real_key"
  28. return 1
  29. fi
  30. _real_fullchain="$_ssl_path/dovecot.chain.pem"
  31. if ! cat "$_cfullchain" >"$_real_fullchain"; then
  32. _err "Error: write key file to: $_real_fullchain"
  33. return 1
  34. fi
  35. DEFAULT_DOVECOT_RELOAD="service dovecot restart"
  36. _reload_dovecot="${DEPLOY_DOVECOT_RELOAD:-$DEFAULT_DOVECOT_RELOAD}"
  37. if [ -z "$IS_RENEW" ]; then
  38. DEFAULT_DOVECOT_CONF="/etc/dovecot/dovecot.conf"
  39. _dovecot_conf="${DEPLOY_DOVECOT_CONF:-$DEFAULT_DOVECOT_CONF}"
  40. if [ ! -f "$_dovecot_conf" ]; then
  41. if [ -z "$DEPLOY_DOVECOT_CONF" ]; then
  42. _err "dovecot conf is not found, please define DEPLOY_DOVECOT_CONF"
  43. return 1
  44. else
  45. _err "It seems that the specified dovecot conf is not valid, please check."
  46. return 1
  47. fi
  48. fi
  49. if [ ! -w "$_dovecot_conf" ]; then
  50. _err "The file $_dovecot_conf is not writable, please change the permission."
  51. return 1
  52. fi
  53. _backup_dovecot_conf="$DOMAIN_BACKUP_PATH/dovecot.conf.bak"
  54. _info "Backup $_dovecot_conf to $_backup_dovecot_conf"
  55. cp "$_dovecot_conf" "$_backup_dovecot_conf"
  56. # dovecot needs the input redirectors ("<") before the filenames here
  57. _info "Modify dovecot conf: $_dovecot_conf"
  58. if _setopt "$_dovecot_conf" "ssl_cert" "=" "<$_real_fullchain" \
  59. && _setopt "$_dovecot_conf" "ssl_key" "=" "<$_real_key" \
  60. && _setopt "$_dovecot_conf" "ssl" "=" "required"; then
  61. _info "Set config success!"
  62. else
  63. _err "Config dovecot server error, please report bug to us."
  64. _info "Restoring dovecot conf"
  65. if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then
  66. _info "Restore conf success"
  67. eval "$_reload_dovecot"
  68. else
  69. _err "Oops, error restore dovecot conf, please report bug to us."
  70. fi
  71. return 1
  72. fi
  73. fi
  74. _info "Run reload: $_reload_dovecot"
  75. if eval "$_reload_dovecot"; then
  76. _info "Reload success!"
  77. if [ "$DEPLOY_DOVECOT_CONF" ]; then
  78. _savedomainconf DEPLOY_DOVECOT_CONF "$DEPLOY_DOVECOT_CONF"
  79. else
  80. _cleardomainconf DEPLOY_DOVECOT_CONF
  81. fi
  82. if [ "$DEPLOY_DOVECOT_RELOAD" ]; then
  83. _savedomainconf DEPLOY_DOVECOT_RELOAD "$DEPLOY_DOVECOT_RELOAD"
  84. else
  85. _cleardomainconf DEPLOY_DOVECOT_RELOAD
  86. fi
  87. return 0
  88. else
  89. _err "Reload error, restoring conf"
  90. if cat "$_backup_dovecot_conf" >"$_dovecot_conf"; then
  91. _info "Restore dovecot conf success"
  92. eval "$_reload_dovecot"
  93. else
  94. _err "Oops, error restoring dovecot conf, please report bug to us."
  95. fi
  96. return 1
  97. fi
  98. return 0
  99. }