exim4.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env sh
  2. #Here is a script to deploy cert to exim4 server.
  3. #returns 0 means success, otherwise error.
  4. #DEPLOY_EXIM4_CONF="/etc/exim/exim.conf"
  5. #DEPLOY_EXIM4_RELOAD="service exim4 restart"
  6. ######## Public functions #####################
  7. #domain keyfile certfile cafile fullchain
  8. exim4_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/exim4"
  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/exim4.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/exim4.pem"
  31. if ! cat "$_cfullchain" >"$_real_fullchain"; then
  32. _err "Error: write key file to: $_real_fullchain"
  33. return 1
  34. fi
  35. DEFAULT_EXIM4_RELOAD="service exim4 restart"
  36. _reload="${DEPLOY_EXIM4_RELOAD:-$DEFAULT_EXIM4_RELOAD}"
  37. if [ -z "$IS_RENEW" ]; then
  38. DEFAULT_EXIM4_CONF="/etc/exim/exim.conf"
  39. if [ ! -f "$DEFAULT_EXIM4_CONF" ]; then
  40. DEFAULT_EXIM4_CONF="/etc/exim4/exim4.conf.template"
  41. fi
  42. _exim4_conf="${DEPLOY_EXIM4_CONF:-$DEFAULT_EXIM4_CONF}"
  43. _debug _exim4_conf "$_exim4_conf"
  44. if [ ! -f "$_exim4_conf" ]; then
  45. if [ -z "$DEPLOY_EXIM4_CONF" ]; then
  46. _err "exim4 conf is not found, please define DEPLOY_EXIM4_CONF"
  47. return 1
  48. else
  49. _err "It seems that the specified exim4 conf is not valid, please check."
  50. return 1
  51. fi
  52. fi
  53. if [ ! -w "$_exim4_conf" ]; then
  54. _err "The file $_exim4_conf is not writable, please change the permission."
  55. return 1
  56. fi
  57. _backup_conf="$DOMAIN_BACKUP_PATH/exim4.conf.bak"
  58. _info "Backup $_exim4_conf to $_backup_conf"
  59. cp "$_exim4_conf" "$_backup_conf"
  60. _info "Modify exim4 conf: $_exim4_conf"
  61. if _setopt "$_exim4_conf" "tls_certificate" "=" "$_real_fullchain" \
  62. && _setopt "$_exim4_conf" "tls_privatekey" "=" "$_real_key"; then
  63. _info "Set config success!"
  64. else
  65. _err "Config exim4 server error, please report bug to us."
  66. _info "Restoring exim4 conf"
  67. if cat "$_backup_conf" >"$_exim4_conf"; then
  68. _info "Restore conf success"
  69. eval "$_reload"
  70. else
  71. _err "Oops, error restore exim4 conf, please report bug to us."
  72. fi
  73. return 1
  74. fi
  75. fi
  76. _info "Run reload: $_reload"
  77. if eval "$_reload"; then
  78. _info "Reload success!"
  79. if [ "$DEPLOY_EXIM4_CONF" ]; then
  80. _savedomainconf DEPLOY_EXIM4_CONF "$DEPLOY_EXIM4_CONF"
  81. else
  82. _cleardomainconf DEPLOY_EXIM4_CONF
  83. fi
  84. if [ "$DEPLOY_EXIM4_RELOAD" ]; then
  85. _savedomainconf DEPLOY_EXIM4_RELOAD "$DEPLOY_EXIM4_RELOAD"
  86. else
  87. _cleardomainconf DEPLOY_EXIM4_RELOAD
  88. fi
  89. return 0
  90. else
  91. _err "Reload error, restoring"
  92. if cat "$_backup_conf" >"$_exim4_conf"; then
  93. _info "Restore conf success"
  94. eval "$_reload"
  95. else
  96. _err "Oops, error restore exim4 conf, please report bug to us."
  97. fi
  98. return 1
  99. fi
  100. return 0
  101. }