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.

110 lines
3.1 KiB

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