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.

132 lines
4.6 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. fi
  49. }
  50. _ISPC_getZoneInfo() {
  51. _info "Getting Zoneinfo"
  52. zoneEnd=false
  53. curZone="${fulldomain}"
  54. while [ ${zoneEnd} = false ]; do
  55. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  56. curZone="${curZone#*.}"
  57. # suffix . needed for zone -> domain.tld.
  58. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"
  59. curResult=$(_post "${curData}" "${ISPC_Api}?dns_zone_get")
  60. if _contains "${curResult}" '"id":"'; then
  61. zoneFound=true
  62. zoneEnd=true
  63. _info "Successfully retrieved zone data."
  64. fi
  65. if [ "${curZone#*.}" != "$curZone" ]; then
  66. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  67. else
  68. zoneEnd=true
  69. _err "Couldn't retrieve zone info."
  70. fi
  71. done
  72. if [ ${zoneFound} ]; then
  73. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  74. case ${server_id} in
  75. '' | *[!0-9]*) _err "Server ID is not numeric." ;;
  76. *) _info "Successfully retrieved Server ID" ;;
  77. esac
  78. zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  79. case ${zone} in
  80. '' | *[!0-9]*) _err "Zone ID is not numeric." ;;
  81. *) _info "Successfully retrieved Zone ID" ;;
  82. esac
  83. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  84. case ${client_id} in
  85. '' | *[!0-9]*) _err "Client ID is not numeric." ;;
  86. *) _info "Successfully retrieved Client ID" ;;
  87. esac
  88. zoneFound=""
  89. zoneEnd=""
  90. fi
  91. }
  92. _ISPC_addTxt() {
  93. curSerial="$(date +%s)"
  94. curStamp="$(date +'%F %T')"
  95. params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"${curStamp}\",\"serial\":\"${curSerial}\""
  96. curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":{${params}}}"
  97. curResult=$(_post "${curData}" "${ISPC_Api}?dns_txt_add")
  98. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  99. case ${record_id} in
  100. '' | *[!0-9]*) _err "Record ID is not numeric." ;;
  101. *)
  102. _info "Successfully retrieved Record ID"
  103. # Make space seperated string of record IDs for later removal.
  104. record_data="$record_data $record_id"
  105. ;;
  106. esac
  107. }
  108. _ISPC_rmTxt() {
  109. IFS=" "
  110. for i in $record_data; do
  111. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${i}\"}"
  112. curResult=$(_post "${curData}" "${ISPC_Api}?dns_txt_delete")
  113. if _contains "${curResult}" '"code":"ok"'; then
  114. _info "Successfully removed ACME challenge txt record."
  115. else
  116. # Setting it to debug only because there's no harm if the txt remains
  117. _debug "Couldn't remove ACME challenge txt record."
  118. fi
  119. done
  120. }