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.

289 lines
7.6 KiB

9 months ago
9 months ago
9 months ago
9 months ago
4 years ago
  1. #!/usr/bin/env sh
  2. # Supports IONOS DNS API v1.0.1 and IONOS Cloud DNS API v1.15.4
  3. #
  4. # Usage:
  5. # Export IONOS_PREFIX and IONOS_SECRET or IONOS_TOKEN before calling acme.sh:
  6. #
  7. # $ export IONOS_PREFIX="..."
  8. # $ export IONOS_SECRET="..."
  9. # or
  10. # $ export IONOS_TOKEN="..."
  11. #
  12. # $ acme.sh --issue --dns dns_ionos ...
  13. #
  14. # if IONOS_PREFIX and IONOS_SECRET are set, the script will use IONOS DNS API
  15. # if IONOS_TOKEN is set, the script will use the IONOS Cloud DNS API
  16. IONOS_API="https://api.hosting.ionos.com/dns"
  17. IONOS_CLOUD_API="https://dns.de-fra.ionos.com"
  18. IONOS_ROUTE_ZONES="/v1/zones"
  19. IONOS_CLOUD_ROUTE_ZONES="/zones"
  20. IONOS_TXT_TTL=60 # minimum accepted by API
  21. IONOS_TXT_PRIO=10
  22. dns_ionos_add() {
  23. fulldomain=$1
  24. txtvalue=$2
  25. if ! _ionos_init; then
  26. return 1
  27. fi
  28. if [ $_context == "core" ];then
  29. _body="[{\"name\":\"$_sub_domain.$_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":$IONOS_TXT_TTL,\"prio\":$IONOS_TXT_PRIO,\"disabled\":false}]"
  30. if _ionos_rest POST "$IONOS_ROUTE_ZONES/$_zone_id/records" "$_body" && [ "$_code" = "201" ]; then
  31. _info "TXT record has been created successfully."
  32. return 0
  33. fi
  34. else
  35. _record_name=$(printf "%s" "$fulldomain" | cut -d . -f 1)
  36. _body="{\"properties\":{\"name\":\"$_record_name\", \"type\":\"TXT\", \"content\":\"$txtvalue\"}}"
  37. if _ionos_cloud_rest POST "$IONOS_CLOUD_ROUTE_ZONES/$_zone_id/records" "$_body" && [ "$_code" = "202" ]; then
  38. _info "TXT record has been created successfully."
  39. return 0
  40. fi
  41. fi
  42. return 1
  43. }
  44. dns_ionos_rm() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. if ! _ionos_init; then
  48. return 1
  49. fi
  50. if [ $_context == "core" ];then
  51. if ! _ionos_get_record "$fulldomain" "$_zone_id" "$txtvalue"; then
  52. _err "Could not find _acme-challenge TXT record."
  53. return 1
  54. fi
  55. if _ionos_rest DELETE "$IONOS_ROUTE_ZONES/$_zone_id/records/$_record_id" && [ "$_code" = "200" ]; then
  56. _info "TXT record has been deleted successfully."
  57. return 0
  58. fi
  59. else
  60. _record_name=$(printf "%s" "$fulldomain" | cut -d . -f 1)
  61. if ! _ionos_cloud_get_record "$_record_name" "$_zone_id" "$txtvalue"; then
  62. _err "Could not find _acme-challenge TXT record."
  63. return 1
  64. fi
  65. if _ionos_cloud_rest DELETE "$IONOS_CLOUD_ROUTE_ZONES/$_zone_id/records/$_record_id" && [ "$_code" = "200" ]; then
  66. _info "TXT record has been deleted successfully."
  67. return 0
  68. fi
  69. fi
  70. return 1
  71. }
  72. _ionos_init() {
  73. IONOS_PREFIX="${IONOS_PREFIX:-$(_readaccountconf_mutable IONOS_PREFIX)}"
  74. IONOS_SECRET="${IONOS_SECRET:-$(_readaccountconf_mutable IONOS_SECRET)}"
  75. IONOS_TOKEN="${IONOS_TOKEN:-$(_readaccountconf_mutable IONOS_TOKEN)}"
  76. if [ -n "$IONOS_PREFIX" ] && [ -n "$IONOS_SECRET" ]; then
  77. _info "You have specified an IONOS api prefix and secret."
  78. _info "The script will use the IONOS DNS API: $IONOS_API"
  79. _saveaccountconf_mutable IONOS_PREFIX "$IONOS_PREFIX"
  80. _saveaccountconf_mutable IONOS_SECRET "$IONOS_SECRET"
  81. if ! _get_root "$fulldomain"; then
  82. _err "Cannot find this domain in your IONOS account."
  83. return 1
  84. fi
  85. $_context="core"
  86. elif [ -n "$IONOS_TOKEN" ]; then
  87. _info "You have specified an IONOS token."
  88. _info "The script will use the IONOS Cloud DNS API: $IONOS_CLOUD_API"
  89. _saveaccountconf_mutable IONOS_TOKEN "$IONOS_TOKEN"
  90. if ! _get_cloud_zone "$fulldomain"; then
  91. _err "Cannot find zone $zone in your IONOS account."
  92. return 1
  93. fi
  94. $_context="cloud"
  95. else
  96. _err "You didn't specify any IONOS credentials yet."
  97. _err "If you are using the IONOS DNS API, Read https://beta.developer.hosting.ionos.de/docs/getstarted to learn how to get a prefix and secret."
  98. _err "If you are using the IONOS Cloud DNS API, Read https://api.ionos.com/docs/authentication/v1/#tag/tokens/operation/tokensGenerate to learn how to get a token."
  99. _err ""
  100. _err "Then set them before calling acme.sh:"
  101. _err "\$ export IONOS_PREFIX=\"...\""
  102. _err "\$ export IONOS_SECRET=\"...\""
  103. _err "#or"
  104. _err "\$ export IONOS_TOKEN=\"...\""
  105. _err "\$ acme.sh --issue -d ... --dns dns_ionos"
  106. return 1
  107. fi
  108. return 0
  109. }
  110. _get_root() {
  111. domain=$1
  112. i=1
  113. p=1
  114. if _ionos_rest GET "$IONOS_ROUTE_ZONES"; then
  115. _response="$(echo "$_response" | tr -d "\n")"
  116. while true; do
  117. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  118. if [ -z "$h" ]; then
  119. return 1
  120. fi
  121. _zone="$(echo "$_response" | _egrep_o "\"name\":\"$h\".*\}")"
  122. if [ "$_zone" ]; then
  123. _zone_id=$(printf "%s\n" "$_zone" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  124. if [ "$_zone_id" ]; then
  125. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  126. _domain=$h
  127. return 0
  128. fi
  129. return 1
  130. fi
  131. p=$i
  132. i=$(_math "$i" + 1)
  133. done
  134. fi
  135. return 1
  136. }
  137. _get_cloud_zone() {
  138. domain=$1
  139. zone=$(printf "%s" "$domain" | cut -d . -f 2-)
  140. if _ionos_cloud_rest GET "$IONOS_CLOUD_ROUTE_ZONES?filter.zoneName=$zone"; then
  141. _response="$(echo "$_response" | tr -d "\n")"
  142. _zone_list_items=$(echo "$_response" | _egrep_o "\"items\":.*")
  143. _zone_id=$(printf "%s\n" "$_zone_list_items" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  144. if [ "$_zone_id" ]; then
  145. return 0
  146. fi
  147. fi
  148. return 1
  149. }
  150. _ionos_get_record() {
  151. fulldomain=$1
  152. zone_id=$2
  153. txtrecord=$3
  154. if _ionos_rest GET "$IONOS_ROUTE_ZONES/$zone_id?recordName=$fulldomain&recordType=TXT"; then
  155. _response="$(echo "$_response" | tr -d "\n")"
  156. _record="$(echo "$_response" | _egrep_o "\"name\":\"$fulldomain\"[^\}]*\"type\":\"TXT\"[^\}]*\"content\":\"\\\\\"$txtrecord\\\\\"\".*\}")"
  157. if [ "$_record" ]; then
  158. _record_id=$(printf "%s\n" "$_record" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  159. return 0
  160. fi
  161. fi
  162. return 1
  163. }
  164. _ionos_cloud_get_record() {
  165. _record_name=$1
  166. zone_id=$2
  167. txtrecord=$3
  168. if _ionos_cloud_rest GET "$IONOS_ROUTE_ZONES/$zone_id/records"; then
  169. _response="$(echo "$_response" | tr -d "\n")"
  170. _record="$(echo "$_response" | _egrep_o "\"name\":\"$_record_name\"[^\}]*\"type\":\"TXT\"[^\}]*\"content\":\"\\\\\"$txtrecord\\\\\"\".*\}")"
  171. if [ "$_record" ]; then
  172. _record_id=$(printf "%s\n" "$_record" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  173. return 0
  174. fi
  175. fi
  176. return 1
  177. }
  178. _ionos_rest() {
  179. method="$1"
  180. route="$2"
  181. data="$3"
  182. IONOS_API_KEY="$(printf "%s.%s" "$IONOS_PREFIX" "$IONOS_SECRET")"
  183. export _H1="X-API-Key: $IONOS_API_KEY"
  184. # clear headers
  185. : >"$HTTP_HEADER"
  186. if [ "$method" != "GET" ]; then
  187. export _H2="Accept: application/json"
  188. export _H3="Content-Type: application/json"
  189. _response="$(_post "$data" "$IONOS_API$route" "" "$method" "application/json")"
  190. else
  191. export _H2="Accept: */*"
  192. export _H3=
  193. _response="$(_get "$IONOS_API$route")"
  194. fi
  195. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  196. if [ "$?" != "0" ]; then
  197. _err "Error $route: $_response"
  198. return 1
  199. fi
  200. _debug2 "_response" "$_response"
  201. _debug2 "_code" "$_code"
  202. return 0
  203. }
  204. _ionos_cloud_rest() {
  205. method="$1"
  206. route="$2"
  207. data="$3"
  208. export _H1="Authorization: Bearer $IONOS_TOKEN"
  209. # clear headers
  210. : >"$HTTP_HEADER"
  211. if [ "$method" != "GET" ]; then
  212. _response="$(_post "$data" "$IONOS_CLOUD_API$route" "" "$method" "application/json")"
  213. else
  214. _response="$(_get "$IONOS_CLOUD_API$route")"
  215. fi
  216. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  217. if [ "$?" != "0" ]; then
  218. _err "Error $route: $_response"
  219. return 1
  220. fi
  221. _debug2 "_response" "$_response"
  222. _debug2 "_code" "$_code"
  223. return 0
  224. }