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.

191 lines
7.1 KiB

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