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.

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