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.

176 lines
6.5 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. # ISPConfig 3.1 API
  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 txt Functions
  5. # Report bugs to https://github.com/sjau/acme.sh
  6. # Values to export:
  7. # export ISPC_User="remoteUser"
  8. # export ISPC_Password="remotePassword"
  9. # export ISPC_Api="https://ispc.domain.tld:8080/remote/json.php"
  10. # export ISPC_Api_Insecure=1 # Set 1 for insecure and 0 for secure -> difference is whether ssl cert is checked for validity (0) or whether it is just accepted (1)
  11. ######## Public functions #####################
  12. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_ispconfig_add() {
  14. fulldomain="${1}"
  15. txtvalue="${2}"
  16. _debug "Calling: dns_ispconfig_add() '${fulldomain}' '${txtvalue}'"
  17. _ISPC_credentials && _ISPC_login && _ISPC_getZoneInfo && _ISPC_addTxt
  18. }
  19. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  20. dns_ispconfig_rm() {
  21. fulldomain="${1}"
  22. _debug "Calling: dns_ispconfig_rm() '${fulldomain}'"
  23. _ISPC_credentials && _ISPC_login && _ISPC_rmTxt
  24. }
  25. #################### Private functions below ##################################
  26. _ISPC_credentials() {
  27. if [ -z "${ISPC_User}" ] || [ -z "$ISPC_Password" ] || [ -z "${ISPC_Api}" ] || [ -z "${ISPC_Api_Insecure}" ]; then
  28. ISPC_User=""
  29. ISPC_Password=""
  30. ISPC_Api=""
  31. ISPC_Api_Insecure=""
  32. _err "You haven't specified the ISPConfig Login data, URL and whether you want check the ISPC SSL cert. Please try again."
  33. return 1
  34. else
  35. _saveaccountconf ISPC_User "${ISPC_User}"
  36. _saveaccountconf ISPC_Password "${ISPC_Password}"
  37. _saveaccountconf ISPC_Api "${ISPC_Api}"
  38. _saveaccountconf ISPC_Api_Insecure "${ISPC_Api_Insecure}"
  39. # Set whether curl should use secure or insecure mode
  40. export HTTPS_INSECURE="${ISPC_Api_Insecure}"
  41. fi
  42. }
  43. _ISPC_login() {
  44. _info "Getting Session ID"
  45. curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
  46. curResult="$(_post "${curData}" "${ISPC_Api}?login")"
  47. _debug "Calling _ISPC_login: '${curData}' '${ISPC_Api}?login'"
  48. _debug "Result of _ISPC_login: '$curResult'"
  49. if _contains "${curResult}" '"code":"ok"'; then
  50. sessionID=$(echo "${curResult}" | _egrep_o "response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  51. _info "Retrieved Session ID."
  52. _debug "Session ID: '${sessionID}'"
  53. else
  54. _err "Couldn't retrieve the Session ID."
  55. return 1
  56. fi
  57. }
  58. _ISPC_getZoneInfo() {
  59. _info "Getting Zoneinfo"
  60. zoneEnd=false
  61. curZone="${fulldomain}"
  62. while [ "${zoneEnd}" = false ]; do
  63. # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  64. curZone="${curZone#*.}"
  65. # suffix . needed for zone -> domain.tld.
  66. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":{\"origin\":\"${curZone}.\"}}"
  67. curResult="$(_post "${curData}" "${ISPC_Api}?dns_zone_get")"
  68. _debug "Calling _ISPC_getZoneInfo: '${curData}' '${ISPC_Api}?login'"
  69. _debug "Result of _ISPC_getZoneInfo: '$curResult'"
  70. if _contains "${curResult}" '"id":"'; then
  71. zoneFound=true
  72. zoneEnd=true
  73. _info "Retrieved zone data."
  74. _debug "Zone data: '${curResult}'"
  75. fi
  76. if [ "${curZone#*.}" != "$curZone" ]; then
  77. _debug2 "$curZone still contains a '.' - so we can check next higher level"
  78. else
  79. zoneEnd=true
  80. _err "Couldn't retrieve zone data."
  81. return 1
  82. fi
  83. done
  84. if [ "${zoneFound}" ]; then
  85. server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  86. _debug "Server ID: '${server_id}'"
  87. case "${server_id}" in
  88. '' | *[!0-9]*)
  89. _err "Server ID is not numeric."
  90. return 1
  91. ;;
  92. *) _info "Retrieved Server ID" ;;
  93. esac
  94. zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  95. _debug "Zone: '${zone}'"
  96. case "${zone}" in
  97. '' | *[!0-9]*)
  98. _err "Zone ID is not numeric."
  99. return 1
  100. ;;
  101. *) _info "Retrieved Zone ID" ;;
  102. esac
  103. client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  104. _debug "Client ID: '${client_id}'"
  105. case "${client_id}" in
  106. '' | *[!0-9]*)
  107. _err "Client ID is not numeric."
  108. return 1
  109. ;;
  110. *) _info "Retrieved Client ID." ;;
  111. esac
  112. zoneFound=""
  113. zoneEnd=""
  114. fi
  115. }
  116. _ISPC_addTxt() {
  117. curSerial="$(date +%s)"
  118. curStamp="$(date +'%F %T')"
  119. params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}.\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"${curStamp}\",\"serial\":\"${curSerial}\""
  120. curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":{${params}},\"update_serial\":true}"
  121. curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_add")"
  122. _debug "Calling _ISPC_addTxt: '${curData}' '${ISPC_Api}?dns_txt_add'"
  123. _debug "Result of _ISPC_addTxt: '$curResult'"
  124. record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  125. _debug "Record ID: '${record_id}'"
  126. case "${record_id}" in
  127. '' | *[!0-9]*)
  128. _err "Couldn't add ACME Challenge TXT record to zone."
  129. return 1
  130. ;;
  131. *) _info "Added ACME Challenge TXT record to zone." ;;
  132. esac
  133. }
  134. _ISPC_rmTxt() {
  135. # Need to get the record ID.
  136. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":{\"name\":\"${fulldomain}.\",\"type\":\"TXT\"}}"
  137. curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_get")"
  138. _debug "Calling _ISPC_rmTxt: '${curData}' '${ISPC_Api}?dns_txt_get'"
  139. _debug "Result of _ISPC_rmTxt: '$curResult'"
  140. if _contains "${curResult}" '"code":"ok"'; then
  141. record_id=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
  142. _debug "Record ID: '${record_id}'"
  143. case "${record_id}" in
  144. '' | *[!0-9]*)
  145. _err "Record ID is not numeric."
  146. return 1
  147. ;;
  148. *)
  149. unset IFS
  150. _info "Retrieved Record ID."
  151. curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${record_id}\",\"update_serial\":true}"
  152. curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_delete")"
  153. _debug "Calling _ISPC_rmTxt: '${curData}' '${ISPC_Api}?dns_txt_delete'"
  154. _debug "Result of _ISPC_rmTxt: '$curResult'"
  155. if _contains "${curResult}" '"code":"ok"'; then
  156. _info "Removed ACME Challenge TXT record from zone."
  157. else
  158. _err "Couldn't remove ACME Challenge TXT record from zone."
  159. return 1
  160. fi
  161. ;;
  162. esac
  163. fi
  164. }