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.

297 lines
8.3 KiB

10 months ago
10 months ago
10 months ago
10 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. fulldomain=$3
  167. _record_name=$(printf "%s" "$fulldomain" | cut -d . -f 1)
  168. # this is to transform record name to lower case
  169. # IONOS Cloud API transforms all record names to lower case
  170. _record_name=${_record_name@L}
  171. _info "grepping with the following args: zone_id=$zone_id txtrecord=$txtrecord fulldomain=$fulldomain _record_name=$_record_name"
  172. if _ionos_cloud_rest GET "$IONOS_CLOUD_ROUTE_ZONES/$zone_id/records"; then
  173. _response="$(echo "$_response" | tr -d "\n")"
  174. 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\"}}"
  175. _record="$(echo "$_response" | _egrep_o $pattern)"
  176. _info "the found record after grep: $_record"
  177. if [ "$_record" ]; then
  178. _record_id=$(printf "%s\n" "$_record" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  179. _info "the record id after the search is: $_record_id"
  180. return 0
  181. fi
  182. fi
  183. return 1
  184. }
  185. _ionos_rest() {
  186. method="$1"
  187. route="$2"
  188. data="$3"
  189. IONOS_API_KEY="$(printf "%s.%s" "$IONOS_PREFIX" "$IONOS_SECRET")"
  190. export _H1="X-API-Key: $IONOS_API_KEY"
  191. # clear headers
  192. : >"$HTTP_HEADER"
  193. if [ "$method" != "GET" ]; then
  194. export _H2="Accept: application/json"
  195. export _H3="Content-Type: application/json"
  196. _response="$(_post "$data" "$IONOS_API$route" "" "$method" "application/json")"
  197. else
  198. export _H2="Accept: */*"
  199. export _H3=
  200. _response="$(_get "$IONOS_API$route")"
  201. fi
  202. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  203. if [ "$?" != "0" ]; then
  204. _err "Error $route: $_response"
  205. return 1
  206. fi
  207. _debug2 "_response" "$_response"
  208. _debug2 "_code" "$_code"
  209. return 0
  210. }
  211. _ionos_cloud_rest() {
  212. method="$1"
  213. route="$2"
  214. data="$3"
  215. export _H1="Authorization: Bearer $IONOS_TOKEN"
  216. # clear headers
  217. : >"$HTTP_HEADER"
  218. if [ "$method" != "GET" ]; then
  219. _response="$(_post "$data" "$IONOS_CLOUD_API$route" "" "$method" "application/json")"
  220. else
  221. _response="$(_get "$IONOS_CLOUD_API$route")"
  222. fi
  223. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  224. if [ "$?" != "0" ]; then
  225. _err "Error $route: $_response"
  226. return 1
  227. fi
  228. _debug2 "_response" "$_response"
  229. _debug2 "_code" "$_code"
  230. return 0
  231. }