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.

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