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.

293 lines
8.2 KiB

8 months ago
8 months ago
8 months ago
8 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. if ! _ionos_cloud_get_record "$_zone_id" "$txtvalue" "$fulldomain"; then
  61. _err "Could not find _acme-challenge TXT record."
  62. return 1
  63. fi
  64. if _ionos_cloud_rest DELETE "$IONOS_CLOUD_ROUTE_ZONES/$_zone_id/records/$_record_id" && [ "$_code" = "202" ]; then
  65. _info "TXT record has been deleted successfully."
  66. return 0
  67. fi
  68. fi
  69. return 1
  70. }
  71. _ionos_init() {
  72. IONOS_PREFIX="${IONOS_PREFIX:-$(_readaccountconf_mutable IONOS_PREFIX)}"
  73. IONOS_SECRET="${IONOS_SECRET:-$(_readaccountconf_mutable IONOS_SECRET)}"
  74. IONOS_TOKEN="${IONOS_TOKEN:-$(_readaccountconf_mutable IONOS_TOKEN)}"
  75. if [ -n "$IONOS_PREFIX" ] && [ -n "$IONOS_SECRET" ]; then
  76. _info "You have specified an IONOS api prefix and secret."
  77. _info "The script will use the IONOS DNS API: $IONOS_API"
  78. _saveaccountconf_mutable IONOS_PREFIX "$IONOS_PREFIX"
  79. _saveaccountconf_mutable IONOS_SECRET "$IONOS_SECRET"
  80. if ! _get_root "$fulldomain"; then
  81. _err "Cannot find this domain in your IONOS account."
  82. return 1
  83. fi
  84. _context="core"
  85. elif [ -n "$IONOS_TOKEN" ]; then
  86. _info "You have specified an IONOS token."
  87. _info "The script will use the IONOS Cloud DNS API: $IONOS_CLOUD_API"
  88. _saveaccountconf_mutable IONOS_TOKEN "$IONOS_TOKEN"
  89. if ! _get_cloud_zone "$fulldomain"; then
  90. _err "Cannot find zone $zone in your IONOS account."
  91. return 1
  92. fi
  93. _context="cloud"
  94. else
  95. _err "You didn't specify any IONOS credentials yet."
  96. _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."
  97. _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."
  98. _err ""
  99. _err "Then set them before calling acme.sh:"
  100. _err "\$ export IONOS_PREFIX=\"...\""
  101. _err "\$ export IONOS_SECRET=\"...\""
  102. _err "#or"
  103. _err "\$ export IONOS_TOKEN=\"...\""
  104. _err "\$ acme.sh --issue -d ... --dns dns_ionos"
  105. return 1
  106. fi
  107. return 0
  108. }
  109. _get_root() {
  110. domain=$1
  111. i=1
  112. p=1
  113. if _ionos_rest GET "$IONOS_ROUTE_ZONES"; then
  114. _response="$(echo "$_response" | tr -d "\n")"
  115. while true; do
  116. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  117. if [ -z "$h" ]; then
  118. return 1
  119. fi
  120. _zone="$(echo "$_response" | _egrep_o "\"name\":\"$h\".*\}")"
  121. if [ "$_zone" ]; then
  122. _zone_id=$(printf "%s\n" "$_zone" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  123. if [ "$_zone_id" ]; then
  124. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  125. _domain=$h
  126. return 0
  127. fi
  128. return 1
  129. fi
  130. p=$i
  131. i=$(_math "$i" + 1)
  132. done
  133. fi
  134. return 1
  135. }
  136. _get_cloud_zone() {
  137. domain=$1
  138. zone=$(printf "%s" "$domain" | cut -d . -f 2-)
  139. if _ionos_cloud_rest GET "$IONOS_CLOUD_ROUTE_ZONES?filter.zoneName=$zone"; then
  140. _response="$(echo "$_response" | tr -d "\n")"
  141. _zone_list_items=$(echo "$_response" | _egrep_o "\"items\":.*")
  142. _zone_id=$(printf "%s\n" "$_zone_list_items" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  143. if [ "$_zone_id" ]; then
  144. return 0
  145. fi
  146. fi
  147. return 1
  148. }
  149. _ionos_get_record() {
  150. fulldomain=$1
  151. zone_id=$2
  152. txtrecord=$3
  153. if _ionos_rest GET "$IONOS_ROUTE_ZONES/$zone_id?recordName=$fulldomain&recordType=TXT"; then
  154. _response="$(echo "$_response" | tr -d "\n")"
  155. _record="$(echo "$_response" | _egrep_o "\"name\":\"$fulldomain\"[^\}]*\"type\":\"TXT\"[^\}]*\"content\":\"\\\\\"$txtrecord\\\\\"\".*\}")"
  156. if [ "$_record" ]; then
  157. _record_id=$(printf "%s\n" "$_record" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  158. return 0
  159. fi
  160. fi
  161. return 1
  162. }
  163. _ionos_cloud_get_record() {
  164. zone_id=$1
  165. txtrecord=$2
  166. # this is to transform the domain to lower case
  167. fulldomain=$(printf "%s" "$3" | tr "[:upper:]" "[:lower:]")
  168. # this is to transform record name to lower case
  169. # IONOS Cloud API transforms all record names to lower case
  170. _record_name=$(printf "%s" "$fulldomain" | cut -d . -f 1 | tr "[:upper:]" "[:lower:]")
  171. if _ionos_cloud_rest GET "$IONOS_CLOUD_ROUTE_ZONES/$zone_id/records"; then
  172. _response="$(echo "$_response" | tr -d "\n")"
  173. pattern="\{\"id\":\"[a-fA-F0-9\-]*\",\"type\":\"record\",\"href\":\"/zones/$zone_id/records/[a-fA-F0-9\-]*\",\"metadata\":\{\"createdDate\":\"[A-Z0-9\:\.\-]*\",\"lastModifiedDate\":\"[A-Z0-9\:\.\-]*\",\"fqdn\":\"$fulldomain\",\"state\":\"AVAILABLE\",\"zoneId\":\"$zone_id\"\},\"properties\":\{\"content\":\"$txtrecord\",\"enabled\":true,\"name\":\"$_record_name\",\"priority\":[0-9]*,\"ttl\":[0-9]*,\"type\":\"TXT\"\}\}"
  174. _record="$(echo "$_response" | _egrep_o "$pattern")"
  175. if [ "$_record" ]; then
  176. _record_id=$(printf "%s\n" "$_record" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  177. return 0
  178. fi
  179. fi
  180. return 1
  181. }
  182. _ionos_rest() {
  183. method="$1"
  184. route="$2"
  185. data="$3"
  186. IONOS_API_KEY="$(printf "%s.%s" "$IONOS_PREFIX" "$IONOS_SECRET")"
  187. export _H1="X-API-Key: $IONOS_API_KEY"
  188. # clear headers
  189. : >"$HTTP_HEADER"
  190. if [ "$method" != "GET" ]; then
  191. export _H2="Accept: application/json"
  192. export _H3="Content-Type: application/json"
  193. _response="$(_post "$data" "$IONOS_API$route" "" "$method" "application/json")"
  194. else
  195. export _H2="Accept: */*"
  196. export _H3=
  197. _response="$(_get "$IONOS_API$route")"
  198. fi
  199. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  200. if [ "$?" != "0" ]; then
  201. _err "Error $route: $_response"
  202. return 1
  203. fi
  204. _debug2 "_response" "$_response"
  205. _debug2 "_code" "$_code"
  206. return 0
  207. }
  208. _ionos_cloud_rest() {
  209. method="$1"
  210. route="$2"
  211. data="$3"
  212. export _H1="Authorization: Bearer $IONOS_TOKEN"
  213. # clear headers
  214. : >"$HTTP_HEADER"
  215. if [ "$method" != "GET" ]; then
  216. _response="$(_post "$data" "$IONOS_CLOUD_API$route" "" "$method" "application/json")"
  217. else
  218. _response="$(_get "$IONOS_CLOUD_API$route")"
  219. fi
  220. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  221. if [ "$?" != "0" ]; then
  222. _err "Error $route: $_response"
  223. return 1
  224. fi
  225. _debug2 "_response" "$_response"
  226. _debug2 "_code" "$_code"
  227. return 0
  228. }