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.

130 lines
4.2 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. ISPC_User=""
  4. ISPC_Password=""
  5. # Provide proper URL and port for your ISPC Installation
  6. ISPC_Api="https://ispc.domain.tld:8080/remote/json.php"
  7. ######## Public functions #####################
  8. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_ispconfig_add() {
  10. fulldomain="${1}"
  11. txtvalue="${2}"
  12. _ISPC_login
  13. if [ $? -eq 0 ]; then
  14. _ISPC_getZoneInfo
  15. fi
  16. if [ $? -eq 0 ]; then
  17. _ISPC_addTxt
  18. fi
  19. if [ $? -ne 0 ]; then
  20. return 1
  21. fi
  22. }
  23. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  24. dns_ispconfig_rm() {
  25. fulldomain="${1}"
  26. _ISPC_login
  27. if [ $? -eq 0 ]; then
  28. _ISPC_rmTxt
  29. fi
  30. if [ $? -ne 0 ]; then
  31. return 1
  32. fi
  33. }
  34. #################### Private functions bellow ##################################
  35. _ISPC_login() {
  36. _info "Getting Session ID"
  37. curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
  38. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?login")
  39. if _contains "${curResult}" '"code":"ok"'; then
  40. sessionID=$(echo "${curResult}" | _egrep_o "response.*" | cut -d ':' -f 2)
  41. sessionID=${sessionID:1:-2}
  42. _info "Successfully retrieved Session ID."
  43. else
  44. _err "Couldn't retrieve the Session ID."
  45. fi
  46. }
  47. _ISPC_getZoneInfo() {
  48. _info "Getting Zoneinfo"
  49. zoneEnd=false
  50. curZone="${fulldomain}"
  51. while [ ${zoneEnd} = false ]; do
  52. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  53. curZone="${curZone#*.}"
  54. # suffix . needed for zone -> domain.tld.
  55. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"
  56. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_zone_get")
  57. if _contains "${curResult}" '"id":"'; then
  58. zoneFound=true
  59. zoneEnd=true
  60. _info "Successfully retrieved zone data."
  61. fi
  62. if [ "${curZone#*.}" != "$curZone" ]; then
  63. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  64. else
  65. zoneEnd=true
  66. _err "Couldn't retrieve zone info."
  67. fi
  68. done
  69. if [ ${zoneFound} ]; then
  70. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2)
  71. server_id=${server_id:1:-10}
  72. case ${server_id} in
  73. ''|*[!0-9]*) _err "Server ID is not numeric." ;;
  74. *) _info "Successfully retrieved Server ID" ;;
  75. esac
  76. zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2)
  77. zone=${zone:1:-14}
  78. case ${zone} in
  79. ''|*[!0-9]*) _err "Zone ID is not numeric." ;;
  80. *) _info "Successfully retrieved Zone ID" ;;
  81. esac
  82. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2)
  83. client_id=${client_id:1:-15}
  84. case ${client_id} in
  85. ''|*[!0-9]*) _err "Client ID is not numeric." ;;
  86. *) _info "Successfully retrieved Client ID" ;;
  87. esac
  88. unset zoneFound
  89. unset 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=$(curl -k --data "${curData}" "${ISPC_Api}?dns_txt_add")
  98. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2)
  99. record_id=${record_id:1:-2}
  100. case ${record_id} in
  101. ''|*[!0-9]*) _err "Record ID is not numeric." ;;
  102. *)
  103. _info "Successfully retrieved Record ID";
  104. # Make space seperated string of record IDs for later removal.
  105. record_data="$record_data $record_id" ;;
  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=$(curl -k --data "${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. }