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.

131 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. HTTPS_INSECURE=1
  14. fulldomain="${1}"
  15. txtvalue="${2}"
  16. _ISPC_credentials && _ISPC_login && _ISPC_getZoneInfo && _ISPC_addTxt || return 1
  17. }
  18. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  19. dns_ispconfig_rm() {
  20. HTTPS_INSECURE=1
  21. fulldomain="${1}"
  22. _ISPC_login && _ISPC_rmTxt || return 1
  23. }
  24. #################### Private functions bellow ##################################
  25. _ISPC_credentials() {
  26. if [ -z "$ISPC_User" ] || [ -z "$ISPC_Password" ] || [ -z "$ISPC_Api" ]; then
  27. ISPC_User=""
  28. ISPC_Password=""
  29. ISPC_Api=""
  30. _err "You haven't specified the ISPConfig Login data and the URL. Please try again."
  31. return 1
  32. else
  33. _saveaccountconf ISPC_User "${ISPC_User}"
  34. _saveaccountconf ISPC_Password "${ISPC_Password}"
  35. _saveaccountconf ISPC_Api "${ISPC_Api}"
  36. fi
  37. }
  38. _ISPC_login() {
  39. _info "Getting Session ID"
  40. curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
  41. curResult=$(_post "${curData}" "${ISPC_Api}?login")
  42. if _contains "${curResult}" '"code":"ok"'; then
  43. sessionID=$(echo "${curResult}" | _egrep_o "response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  44. _info "Successfully retrieved Session ID."
  45. else
  46. _err "Couldn't retrieve the Session ID."
  47. fi
  48. }
  49. _ISPC_getZoneInfo() {
  50. _info "Getting Zoneinfo"
  51. zoneEnd=false
  52. curZone="${fulldomain}"
  53. while [ ${zoneEnd} = false ]; do
  54. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  55. curZone="${curZone#*.}"
  56. # suffix . needed for zone -> domain.tld.
  57. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"
  58. curResult=$(_post "${curData}" "${ISPC_Api}?dns_zone_get")
  59. if _contains "${curResult}" '"id":"'; then
  60. zoneFound=true
  61. zoneEnd=true
  62. _info "Successfully retrieved zone data."
  63. fi
  64. if [ "${curZone#*.}" != "$curZone" ]; then
  65. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  66. else
  67. zoneEnd=true
  68. _err "Couldn't retrieve zone info."
  69. fi
  70. done
  71. if [ ${zoneFound} ]; then
  72. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  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 | cut -d '"' -f 2)
  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 | cut -d '"' -f 2)
  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=$(_post "${curData}" "${ISPC_Api}?dns_txt_add")
  97. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  98. case ${record_id} in
  99. '' | *[!0-9]*) _err "Record ID is not numeric." ;;
  100. *)
  101. _info "Successfully retrieved Record ID"
  102. # Make space seperated string of record IDs for later removal.
  103. record_data="$record_data $record_id"
  104. ;;
  105. esac
  106. }
  107. _ISPC_rmTxt() {
  108. IFS=" "
  109. for i in $record_data; do
  110. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${i}\"}"
  111. curResult=$(_post "${curData}" "${ISPC_Api}?dns_txt_delete")
  112. if _contains "${curResult}" '"code":"ok"'; then
  113. _info "Successfully removed ACME challenge txt record."
  114. else
  115. # Setting it to debug only because there's no harm if the txt remains
  116. _debug "Couldn't remove ACME challenge txt record."
  117. fi
  118. done
  119. }