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.

158 lines
5.5 KiB

  1. #!/usr/bin/env sh
  2. # ISPConfig 3.1 API
  3. # User must provide login data and URL to the ISPConfig installation incl. port. The remote user in ISPConfig must have access to:
  4. # - DNS zone Functions
  5. # - DNS txt Functions
  6. # Report bugs to https://github.com/sjau/acme.sh
  7. # Values to export:
  8. # export ISPC_User="remoteUser"
  9. # export ISPC_Password="remotePasword"
  10. # export ISPC_Api="https://ispc.domain.tld:8080/remote/json.php"
  11. # export ISPC_Api_Insecure=1 # Set 1 for insecure and 0 for secure -> difference is whether ssl cert is checked for validity (0) or whether it is just accepted (1)
  12. ######## Public functions #####################
  13. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_ispconfig_add() {
  15. fulldomain="${1}"
  16. txtvalue="${2}"
  17. _ISPC_credentials && _ISPC_login && _ISPC_getZoneInfo && _ISPC_addTxt
  18. }
  19. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  20. dns_ispconfig_rm() {
  21. fulldomain="${1}"
  22. _ISPC_credentials && _ISPC_login && _ISPC_rmTxt
  23. }
  24. #################### Private functions bellow ##################################
  25. _ISPC_credentials() {
  26. if [ -z "${ISPC_User}" ] || [ -z "$ISPC_Password" ] || [ -z "${ISPC_Api}" ] || [ -z "${ISPC_Api_Insecure}" ]; then
  27. ISPC_User=""
  28. ISPC_Password=""
  29. ISPC_Api=""
  30. ISPC_Api_Insecure=""
  31. _err "You haven't specified the ISPConfig Login data, URL and whether you want check the ISPC SSL cert. Please try again."
  32. return 1
  33. else
  34. _saveaccountconf ISPC_User "${ISPC_User}"
  35. _saveaccountconf ISPC_Password "${ISPC_Password}"
  36. _saveaccountconf ISPC_Api "${ISPC_Api}"
  37. _saveaccountconf ISPC_Api_Insecure "${ISPC_Api_Insecure}"
  38. # Set whether curl should use secure or insecure mode
  39. HTTPS_INSECURE="${ISPC_Api_Insecure}"
  40. fi
  41. }
  42. _ISPC_login() {
  43. _info "Getting Session ID"
  44. curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
  45. curResult="$(_post "${curData}" "${ISPC_Api}?login")"
  46. if _contains "${curResult}" '"code":"ok"'; then
  47. sessionID=$(echo "${curResult}" | _egrep_o "response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  48. _info "Retrieved Session ID."
  49. else
  50. _err "Couldn't retrieve the Session ID."
  51. return 1
  52. fi
  53. }
  54. _ISPC_getZoneInfo() {
  55. _info "Getting Zoneinfo"
  56. zoneEnd=false
  57. curZone="${fulldomain}"
  58. while [ "${zoneEnd}" = false ]; do
  59. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  60. curZone="${curZone#*.}"
  61. # suffix . needed for zone -> domain.tld.
  62. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"
  63. curResult="$(_post "${curData}" "${ISPC_Api}?dns_zone_get")"
  64. if _contains "${curResult}" '"id":"'; then
  65. zoneFound=true
  66. zoneEnd=true
  67. _info "Retrieved zone data."
  68. fi
  69. if [ "${curZone#*.}" != "$curZone" ]; then
  70. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  71. else
  72. zoneEnd=true
  73. _err "Couldn't retrieve zone data."
  74. return 1
  75. fi
  76. done
  77. if [ "${zoneFound}" ]; then
  78. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  79. case "${server_id}" in
  80. '' | *[!0-9]*)
  81. _err "Server ID is not numeric."
  82. return 1
  83. ;;
  84. *) _info "Retrieved Server ID" ;;
  85. esac
  86. zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  87. case "${zone}" in
  88. '' | *[!0-9]*)
  89. _err "Zone ID is not numeric."
  90. return 1
  91. ;;
  92. *) _info "Retrieved Zone ID" ;;
  93. esac
  94. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  95. case "${client_id}" in
  96. '' | *[!0-9]*)
  97. _err "Client ID is not numeric."
  98. return 1
  99. ;;
  100. *) _info "Retrieved Client ID." ;;
  101. esac
  102. zoneFound=""
  103. zoneEnd=""
  104. fi
  105. }
  106. _ISPC_addTxt() {
  107. curSerial="$(date +%s)"
  108. curStamp="$(date +'%F %T')"
  109. params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}.\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"${curStamp}\",\"serial\":\"${curSerial}\""
  110. curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":{${params}}}"
  111. curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_add")"
  112. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  113. case "${record_id}" in
  114. '' | *[!0-9]*)
  115. _err "Couldn't add ACME Challenge TXT record to zone."
  116. return 1
  117. ;;
  118. *) _info "Added ACME Challenge TXT record to zone." ;;
  119. esac
  120. }
  121. _ISPC_rmTxt() {
  122. # Need to get the record ID.
  123. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":{\"name\":\"${fulldomain}.\",\"type\":\"TXT\"}}"
  124. curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_get")"
  125. if _contains "${curResult}" '"code":"ok"'; then
  126. record_id=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  127. case "${record_id}" in
  128. '' | *[!0-9]*)
  129. _err "Record ID is not numeric."
  130. return 1
  131. ;;
  132. *)
  133. unset IFS
  134. _info "Retrieved Record ID."
  135. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${record_id}\"}"
  136. curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_delete")"
  137. if _contains "${curResult}" '"code":"ok"'; then
  138. _info "Removed ACME Challenge TXT record from zone."
  139. else
  140. _err "Couldn't remove ACME Challenge TXT record from zone."
  141. return 1
  142. fi
  143. ;;
  144. esac
  145. fi
  146. }