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 postfix servers.
  3. #returns 0 means success, otherwise error.
  4. #DEFAULT_POSTFIX_RELOAD="service postfix restart"
  5. #DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf"
  6. ######## Public functions #####################
  7. #domain keyfile certfile cafile fullchain
  8. postfix_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/postfix"
  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/postfix.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/postfix.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_POSTFIX_RELOAD="service postfix restart"
  36. _reload_postfix="${DEPLOY_POSTFIX_RELOAD:-$DEFAULT_POSTFIX_RELOAD}"
  37. if [ -z "$IS_RENEW" ]; then
  38. DEFAULT_POSTFIX_CONF="/etc/postfix/main.cf"
  39. _postfix_conf="${DEPLOY_POSTFIX_CONF:-$DEFAULT_POSTFIX_CONF}"
  40. if [ ! -f "$_postfix_conf" ]; then
  41. if [ -z "$DEPLOY_POSTFIX_CONF" ]; then
  42. _err "postfix conf is not found, please define DEPLOY_POSTFIX_CONF"
  43. return 1
  44. else
  45. _err "It seems that the specified postfix conf is not valid, please check."
  46. return 1
  47. fi
  48. fi
  49. if [ ! -w "$_postfix_conf" ]; then
  50. _err "The file $_postfix_conf is not writable, please change the permission."
  51. return 1
  52. fi
  53. _backup_postfix_conf="$DOMAIN_BACKUP_PATH/postfix.conf.bak"
  54. _info "Backup $_postfix_conf to $_backup_postfix_conf"
  55. cp "$_postfix_conf" "$_backup_postfix_conf"
  56. _info "Modify postfix conf: $_postfix_conf"
  57. if _setopt "$_postfix_conf" "smtpd_tls_cert_file" "=" "$_real_fullchain" \
  58. && _setopt "$_postfix_conf" "smtpd_tls_key_file" "=" "$_real_key" \
  59. && _setopt "$_postfix_conf" "smtpd_use_tls" "=" "yes" \
  60. && _setopt "$_postfix_conf" "smtpd_tls_security_level" "=" "may"; then
  61. _info "Set config success!"
  62. else
  63. _err "Config postfix server error, please report bug to us."
  64. _info "Restoring postfix conf"
  65. if cat "$_backup_postfix_conf" >"$_postfix_conf"; then
  66. _info "Restore conf success"
  67. eval "$_reload_postfix"
  68. else
  69. _err "Oops, error restore postfix conf, please report bug to us."
  70. fi
  71. return 1
  72. fi
  73. fi
  74. _info "Run reload: $_reload_postfix"
  75. if eval "$_reload_postfix"; then
  76. _info "Reload success!"
  77. if [ "$DEPLOY_POSTFIX_CONF" ]; then
  78. _savedomainconf DEPLOY_POSTFIX_CONF "$DEPLOY_POSTFIX_CONF"
  79. else
  80. _cleardomainconf DEPLOY_POSTFIX_CONF
  81. fi
  82. if [ "$DEPLOY_POSTFIX_RELOAD" ]; then
  83. _savedomainconf DEPLOY_POSTFIX_RELOAD "$DEPLOY_POSTFIX_RELOAD"
  84. else
  85. _cleardomainconf DEPLOY_POSTFIX_RELOAD
  86. fi
  87. return 0
  88. else
  89. _err "Reload error, restoring conf"
  90. if cat "$_backup_postfix_conf" >"$_postfix_conf"; then
  91. _info "Restore postfox conf success"
  92. eval "$_reload_postfix"
  93. else
  94. _err "Oops, error restoring postfix conf, please report bug to us."
  95. fi
  96. return 1
  97. fi
  98. return 0
  99. }