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.

290 lines
7.5 KiB

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