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.

118 lines
3.2 KiB

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