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.

146 lines
4.7 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. ######## Public functions #####################
  12. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_ispconfig_add() {
  14. HTTPS_INSECURE=1
  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. HTTPS_INSECURE=1
  22. fulldomain="${1}"
  23. _ISPC_login && _ISPC_rmTxt
  24. }
  25. #################### Private functions bellow ##################################
  26. _ISPC_credentials() {
  27. if [ -z "$ISPC_User" ] || [ -z "$ISPC_Password" ] || [ -z "$ISPC_Api" ]; then
  28. ISPC_User=""
  29. ISPC_Password=""
  30. ISPC_Api=""
  31. _err "You haven't specified the ISPConfig Login data and the URL. 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. fi
  38. }
  39. _ISPC_login() {
  40. _info "Getting Session ID"
  41. curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
  42. curResult=$(_post "${curData}" "${ISPC_Api}?login")
  43. if _contains "${curResult}" '"code":"ok"'; then
  44. sessionID=$(echo "${curResult}" | _egrep_o "response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  45. _info "Successfully retrieved Session ID."
  46. else
  47. _err "Couldn't retrieve the Session ID."
  48. return 1
  49. fi
  50. }
  51. _ISPC_getZoneInfo() {
  52. _info "Getting Zoneinfo"
  53. zoneEnd=false
  54. curZone="${fulldomain}"
  55. while [ ${zoneEnd} = false ]; do
  56. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  57. curZone="${curZone#*.}"
  58. # suffix . needed for zone -> domain.tld.
  59. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"
  60. curResult=$(_post "${curData}" "${ISPC_Api}?dns_zone_get")
  61. if _contains "${curResult}" '"id":"'; then
  62. zoneFound=true
  63. zoneEnd=true
  64. _info "Successfully retrieved zone data."
  65. fi
  66. if [ "${curZone#*.}" != "$curZone" ]; then
  67. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  68. else
  69. zoneEnd=true
  70. _err "Couldn't retrieve zone info."
  71. return 1
  72. fi
  73. done
  74. if [ ${zoneFound} ]; then
  75. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  76. case ${server_id} in
  77. '' | *[!0-9]*)
  78. _err "Server ID is not numeric."
  79. return 1
  80. ;;
  81. *) _info "Successfully retrieved Server ID" ;;
  82. esac
  83. zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  84. case ${zone} in
  85. '' | *[!0-9]*)
  86. _err "Zone ID is not numeric."
  87. return 1
  88. ;;
  89. *) _info "Successfully retrieved Zone ID" ;;
  90. esac
  91. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  92. case ${client_id} in
  93. '' | *[!0-9]*)
  94. _err "Client ID is not numeric."
  95. return 1
  96. ;;
  97. *) _info "Successfully retrieved Client ID" ;;
  98. esac
  99. zoneFound=""
  100. zoneEnd=""
  101. fi
  102. }
  103. _ISPC_addTxt() {
  104. curSerial="$(date +%s)"
  105. curStamp="$(date +'%F %T')"
  106. params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"${curStamp}\",\"serial\":\"${curSerial}\""
  107. curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":{${params}}}"
  108. curResult=$(_post "${curData}" "${ISPC_Api}?dns_txt_add")
  109. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  110. case ${record_id} in
  111. '' | *[!0-9]*)
  112. _err "Record ID is not numeric."
  113. return 1
  114. ;;
  115. *)
  116. _info "Successfully retrieved Record ID"
  117. # Make space seperated string of record IDs for later removal.
  118. record_data="$record_data $record_id"
  119. ;;
  120. esac
  121. }
  122. _ISPC_rmTxt() {
  123. IFS=" "
  124. for i in $record_data; do
  125. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${i}\"}"
  126. curResult=$(_post "${curData}" "${ISPC_Api}?dns_txt_delete")
  127. if _contains "${curResult}" '"code":"ok"'; then
  128. _info "Successfully removed ACME challenge txt record."
  129. else
  130. # Setting it to debug only because there's no harm if the txt remains
  131. _debug "Couldn't remove ACME challenge txt record."
  132. fi
  133. done
  134. }