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.

98 lines
2.6 KiB

  1. #!/usr/bin/env sh
  2. #Here is a sample custom api script.
  3. #This file name is "myapi.sh"
  4. #So, here must be a method myapi_deploy()
  5. #Which will be called by acme.sh to deploy the cert
  6. #returns 0 means success, otherwise error.
  7. ######## Public functions #####################
  8. #domain keyfile certfile cafile fullchain
  9. strongswan_deploy() {
  10. _cdomain="${1}"
  11. _ckey="${2}"
  12. _ccert="${3}"
  13. _cca="${4}"
  14. _cfullchain="${5}"
  15. _info "Using strongswan"
  16. if _exists ipsec; then
  17. _ipsec=ipsec
  18. elif _exists strongswan; then
  19. _ipsec=strongswan
  20. fi
  21. if _exists swanctl; then
  22. _swanctl=swanctl
  23. fi
  24. # For legacy stroke mode
  25. if [ -n "${_ipsec}" ]; then
  26. _info "${_ipsec} command detected"
  27. _confdir=$(${_ipsec} --confdir)
  28. if [ -z "${_confdir}" ]; then
  29. _err "no strongswan --confdir is detected"
  30. return 1
  31. fi
  32. _info _confdir "${_confdir}"
  33. __deploy_cert "$@" "stroke" "${_confdir}"
  34. ${_ipsec} reload
  35. fi
  36. # For modern vici mode
  37. if [ -n "${_swanctl}" ]; then
  38. _info "${_swanctl} command detected"
  39. for _dir in /usr/local/etc/swanctl /etc/swanctl /etc/strongswan/swanctl; do
  40. if [ -d ${_dir} ]; then
  41. _confdir=${_dir}
  42. _info _confdir "${_confdir}"
  43. break
  44. fi
  45. done
  46. if [ -z "${_confdir}" ]; then
  47. _err "no swanctl config dir is found"
  48. return 1
  49. fi
  50. __deploy_cert "$@" "vici" "${_confdir}"
  51. ${_swanctl} --load-creds
  52. fi
  53. if [ -z "${_swanctl}" ] && [ -z "${_ipsec}" ]; then
  54. _err "no strongswan or ipsec command is detected"
  55. _err "no swanctl is detected"
  56. return 1
  57. fi
  58. }
  59. #################### Private functions below ##################################
  60. __deploy_cert() {
  61. _cdomain="${1}"
  62. _ckey="${2}"
  63. _ccert="${3}"
  64. _cca="${4}"
  65. _cfullchain="${5}"
  66. _swan_mode="${6}"
  67. _confdir="${7}"
  68. _debug _cdomain "${_cdomain}"
  69. _debug _ckey "${_ckey}"
  70. _debug _ccert "${_ccert}"
  71. _debug _cca "${_cca}"
  72. _debug _cfullchain "${_cfullchain}"
  73. _debug _swan_mode "${_swan_mode}"
  74. _debug _confdir "${_confdir}"
  75. if [ "${_swan_mode}" = "vici" ]; then
  76. _dir_private="private"
  77. _dir_cert="x509"
  78. _dir_ca="x509ca"
  79. elif [ "${_swan_mode}" = "stroke" ]; then
  80. _dir_private="ipsec.d/private"
  81. _dir_cert="ipsec.d/certs"
  82. _dir_ca="ipsec.d/cacerts"
  83. else
  84. _err "unknown StrongSwan mode ${_swan_mode}"
  85. return 1
  86. fi
  87. cat "${_ckey}" >"${_confdir}/${_dir_private}/$(basename "${_ckey}")"
  88. cat "${_ccert}" >"${_confdir}/${_dir_cert}/$(basename "${_ccert}")"
  89. cat "${_cca}" >"${_confdir}/${_dir_ca}/$(basename "${_cca}")"
  90. if [ "${_swan_mode}" = "stroke" ]; then
  91. cat "${_cfullchain}" >"${_confdir}/${_dir_ca}/$(basename "${_cfullchain}")"
  92. fi
  93. }