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.

129 lines
4.6 KiB

8 years ago
  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:
  4. # - DNS zone Functions
  5. # - DNS txt Functions
  6. # Values to export:
  7. # export ISPC_User="remoteUser"
  8. # export ISPC_Password="remotePasword"
  9. # export ISPC_Api="https://ispc.domain.tld:8080/remote/json.php"
  10. ######## Public functions #####################
  11. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_ispconfig_add() {
  13. fulldomain="${1}"
  14. txtvalue="${2}"
  15. _ISPC_credentials && _ISPC_login && _ISPC_getZoneInfo && _ISPC_addTxt || return 1
  16. }
  17. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  18. dns_ispconfig_rm() {
  19. fulldomain="${1}"
  20. _ISPC_login && _ISPC_rmTxt || return 1
  21. }
  22. #################### Private functions bellow ##################################
  23. _ISPC_credentials() {
  24. if [ -z "$ISPC_User" ] || [ -z "$ISPC_Password" ] || [ -z "$ISPC_Api" ]; then
  25. ISPC_User=""
  26. ISPC_Password=""
  27. ISPC_Api=""
  28. _err "You haven't specified the ISPConfig Login data and the URL. Please try again."
  29. return 1
  30. else
  31. _saveaccountconf ISPC_User "${ISPC_User}"
  32. _saveaccountconf ISPC_Password "${ISPC_Password}"
  33. _saveaccountconf ISPC_Api "${ISPC_Api}"
  34. fi
  35. }
  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 | cut -d '"' -f 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 | cut -d '"' -f 2)
  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 | cut -d '"' -f 2)
  76. case ${zone} in
  77. '' | *[!0-9]*) _err "Zone ID is not numeric." ;;
  78. *) _info "Successfully retrieved Zone ID" ;;
  79. esac
  80. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  81. case ${client_id} in
  82. '' | *[!0-9]*) _err "Client ID is not numeric." ;;
  83. *) _info "Successfully retrieved Client ID" ;;
  84. esac
  85. zoneFound=""
  86. 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 | cut -d '"' -f 2)
  96. case ${record_id} in
  97. '' | *[!0-9]*) _err "Record ID is not numeric." ;;
  98. *)
  99. _info "Successfully retrieved Record ID"
  100. # Make space seperated string of record IDs for later removal.
  101. record_data="$record_data $record_id"
  102. ;;
  103. esac
  104. }
  105. _ISPC_rmTxt() {
  106. IFS=" "
  107. for i in $record_data; do
  108. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${i}\"}"
  109. curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_txt_delete")
  110. if _contains "${curResult}" '"code":"ok"'; then
  111. _info "Successfully removed ACME challenge txt record."
  112. else
  113. # Setting it to debug only because there's no harm if the txt remains
  114. _debug "Couldn't remove ACME challenge txt record."
  115. fi
  116. done
  117. }