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.

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