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.7 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_Credentials && _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_credentials() {
  22. if [ -z "$ISPC_User" ] || [ -z "$ISPC_Password" ] || [ -z "$ISPC_Api" ]; then
  23. ISPC_User=""
  24. ISPC_Password=""
  25. ISPC_Api=""
  26. _err "You haven't specified the ISPConfig Login data and the URL. Please try again."
  27. return 1
  28. else
  29. _saveaccountconf ISPC_User "${ISPC_User}"
  30. _saveaccountconf ISPC_Password "${ISPC_Password}"
  31. _saveaccountconf ISPC_Api "${ISPC_Api}"
  32. fi
  33. }
  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. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  52. curZone="${curZone#*.}"
  53. # suffix . needed for zone -> domain.tld.
  54. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"
  55. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_zone_get")
  56. if _contains "${curResult}" '"id":"'; then
  57. zoneFound=true
  58. zoneEnd=true
  59. _info "Successfully retrieved zone data."
  60. fi
  61. if [ "${curZone#*.}" != "$curZone" ]; then
  62. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  63. else
  64. zoneEnd=true
  65. _err "Couldn't retrieve zone info."
  66. fi
  67. done
  68. if [ ${zoneFound} ]; then
  69. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2)
  70. server_id=${server_id:1:-10}
  71. case ${server_id} in
  72. '' | *[!0-9]*) _err "Server ID is not numeric." ;;
  73. *) _info "Successfully retrieved Server ID" ;;
  74. esac
  75. zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2)
  76. zone=${zone:1:-14}
  77. case ${zone} in
  78. '' | *[!0-9]*) _err "Zone ID is not numeric." ;;
  79. *) _info "Successfully retrieved Zone ID" ;;
  80. esac
  81. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2)
  82. client_id=${client_id:1:-15}
  83. case ${client_id} in
  84. '' | *[!0-9]*) _err "Client ID is not numeric." ;;
  85. *) _info "Successfully retrieved Client ID" ;;
  86. esac
  87. zoneFound=""
  88. zoneEnd=""
  89. fi
  90. }
  91. _ISPC_addTxt() {
  92. curSerial="$(date +%s)"
  93. curStamp="$(date +'%F %T')"
  94. params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"${curStamp}\",\"serial\":\"${curSerial}\""
  95. curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":{${params}}}"
  96. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_txt_add")
  97. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2)
  98. record_id=${record_id:1:-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=$(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. }