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.

118 lines
4.3 KiB

  1. #!/usr/bin/env sh
  2. #ISPConfig 3.1 API - Add remote user and give him access to at least the "DNS txt functions"
  3. # User must provide login data and URL to the ISPConfig installation incl. port. The remote user in ISPConfig must have access to the dns_txt_function
  4. # Values to export:
  5. # export ISPC_User="remoteUser"
  6. # export ISPC_Password="remotePasword"
  7. # export ISPC_Api="https://ispc.domain.tld:8080/remote/json.php"
  8. ######## Public functions #####################
  9. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_ispconfig_add() {
  11. fulldomain="${1}"
  12. txtvalue="${2}"
  13. _ISPC_login && _ISPC_getZoneInfo && _ISPC_addTxt || return 1
  14. }
  15. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  16. dns_ispconfig_rm() {
  17. fulldomain="${1}"
  18. _ISPC_login && _ISPC_rmTxt || return 1
  19. }
  20. #################### Private functions bellow ##################################
  21. _ISPC_login() {
  22. _info "Getting Session ID"
  23. curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
  24. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?login")
  25. if _contains "${curResult}" '"code":"ok"'; then
  26. sessionID=$(echo "${curResult}" | _egrep_o "response.*" | cut -d ':' -f 2)
  27. sessionID=${sessionID:1:-2}
  28. _info "Successfully retrieved Session ID."
  29. else
  30. _err "Couldn't retrieve the Session ID."
  31. fi
  32. }
  33. _ISPC_getZoneInfo() {
  34. _info "Getting Zoneinfo"
  35. zoneEnd=false
  36. curZone="${fulldomain}"
  37. while [ ${zoneEnd} = false ]; do
  38. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  39. curZone="${curZone#*.}"
  40. # suffix . needed for zone -> domain.tld.
  41. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"
  42. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_zone_get")
  43. if _contains "${curResult}" '"id":"'; then
  44. zoneFound=true
  45. zoneEnd=true
  46. _info "Successfully retrieved zone data."
  47. fi
  48. if [ "${curZone#*.}" != "$curZone" ]; then
  49. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  50. else
  51. zoneEnd=true
  52. _err "Couldn't retrieve zone info."
  53. fi
  54. done
  55. if [ ${zoneFound} ]; then
  56. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2)
  57. server_id=${server_id:1:-10}
  58. case ${server_id} in
  59. '' | *[!0-9]*) _err "Server ID is not numeric." ;;
  60. *) _info "Successfully retrieved Server ID" ;;
  61. esac
  62. zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2)
  63. zone=${zone:1:-14}
  64. case ${zone} in
  65. '' | *[!0-9]*) _err "Zone ID is not numeric." ;;
  66. *) _info "Successfully retrieved Zone ID" ;;
  67. esac
  68. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2)
  69. client_id=${client_id:1:-15}
  70. case ${client_id} in
  71. '' | *[!0-9]*) _err "Client ID is not numeric." ;;
  72. *) _info "Successfully retrieved Client ID" ;;
  73. esac
  74. unset zoneFound
  75. unset zoneEnd
  76. fi
  77. }
  78. _ISPC_addTxt() {
  79. curSerial="$(date +%s)"
  80. curStamp="$(date +'%F %T')"
  81. params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"${curStamp}\",\"serial\":\"${curSerial}\""
  82. curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":{${params}}}"
  83. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_txt_add")
  84. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2)
  85. record_id=${record_id:1:-2}
  86. case ${record_id} in
  87. '' | *[!0-9]*) _err "Record ID is not numeric." ;;
  88. *)
  89. _info "Successfully retrieved Record ID"
  90. # Make space seperated string of record IDs for later removal.
  91. record_data="$record_data $record_id"
  92. ;;
  93. esac
  94. }
  95. _ISPC_rmTxt() {
  96. IFS=" "
  97. for i in $record_data; do
  98. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${i}\"}"
  99. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_txt_delete")
  100. if _contains "${curResult}" '"code":"ok"'; then
  101. _info "Successfully removed ACME challenge txt record."
  102. else
  103. # Setting it to debug only because there's no harm if the txt remains
  104. _debug "Couldn't remove ACME challenge txt record."
  105. fi
  106. done
  107. }