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.

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