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.

200 lines
5.9 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_infomaniak_info='Infomaniak.com
  4. Site: Infomaniak.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_infomaniak
  6. Options:
  7. INFOMANIAK_API_TOKEN API Token
  8. Issues: github.com/acmesh-official/acme.sh/issues/3188
  9. '
  10. # To use this API you need visit the API dashboard of your account
  11. # once logged into https://manager.infomaniak.com add /api/dashboard to the URL
  12. #
  13. # Note: the URL looks like this:
  14. # https://manager.infomaniak.com/v3/<account_id>/api/dashboard
  15. # Then generate a token with the scope Domain
  16. # this is given as an environment variable INFOMANIAK_API_TOKEN
  17. # base variables
  18. DEFAULT_INFOMANIAK_API_URL="https://api.infomaniak.com"
  19. DEFAULT_INFOMANIAK_TTL=300
  20. ######## Public functions #####################
  21. #Usage: dns_infomaniak_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  22. dns_infomaniak_add() {
  23. INFOMANIAK_API_TOKEN="${INFOMANIAK_API_TOKEN:-$(_readaccountconf_mutable INFOMANIAK_API_TOKEN)}"
  24. INFOMANIAK_API_URL="${INFOMANIAK_API_URL:-$(_readaccountconf_mutable INFOMANIAK_API_URL)}"
  25. INFOMANIAK_TTL="${INFOMANIAK_TTL:-$(_readaccountconf_mutable INFOMANIAK_TTL)}"
  26. if [ -z "$INFOMANIAK_API_TOKEN" ]; then
  27. INFOMANIAK_API_TOKEN=""
  28. _err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
  29. return 1
  30. fi
  31. if [ -z "$INFOMANIAK_API_URL" ]; then
  32. INFOMANIAK_API_URL="$DEFAULT_INFOMANIAK_API_URL"
  33. fi
  34. if [ -z "$INFOMANIAK_TTL" ]; then
  35. INFOMANIAK_TTL="$DEFAULT_INFOMANIAK_TTL"
  36. fi
  37. #save the token to the account conf file.
  38. _saveaccountconf_mutable INFOMANIAK_API_TOKEN "$INFOMANIAK_API_TOKEN"
  39. if [ "$INFOMANIAK_API_URL" != "$DEFAULT_INFOMANIAK_API_URL" ]; then
  40. _saveaccountconf_mutable INFOMANIAK_API_URL "$INFOMANIAK_API_URL"
  41. fi
  42. if [ "$INFOMANIAK_TTL" != "$DEFAULT_INFOMANIAK_TTL" ]; then
  43. _saveaccountconf_mutable INFOMANIAK_TTL "$INFOMANIAK_TTL"
  44. fi
  45. export _H1="Authorization: Bearer $INFOMANIAK_API_TOKEN"
  46. export _H2="Content-Type: application/json"
  47. fulldomain="$1"
  48. txtvalue="$2"
  49. _info "Infomaniak DNS API"
  50. _debug fulldomain "$fulldomain"
  51. _debug txtvalue "$txtvalue"
  52. fqdn=${fulldomain#_acme-challenge.}
  53. # guess which base domain to add record to
  54. zone_and_id=$(_find_zone "$fqdn")
  55. if [ -z "$zone_and_id" ]; then
  56. _err "cannot find zone to modify"
  57. return 1
  58. fi
  59. zone=${zone_and_id% *}
  60. domain_id=${zone_and_id#* }
  61. # extract first part of domain
  62. key=${fulldomain%."$zone"}
  63. _debug "zone:$zone id:$domain_id key:$key"
  64. # payload
  65. data="{\"type\": \"TXT\", \"source\": \"$key\", \"target\": \"$txtvalue\", \"ttl\": $INFOMANIAK_TTL}"
  66. # API call
  67. response=$(_post "$data" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record")
  68. if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
  69. _info "Record added"
  70. _debug "Response: $response"
  71. return 0
  72. fi
  73. _err "could not create record"
  74. _debug "Response: $response"
  75. return 1
  76. }
  77. #Usage: fulldomain txtvalue
  78. #Remove the txt record after validation.
  79. dns_infomaniak_rm() {
  80. INFOMANIAK_API_TOKEN="${INFOMANIAK_API_TOKEN:-$(_readaccountconf_mutable INFOMANIAK_API_TOKEN)}"
  81. INFOMANIAK_API_URL="${INFOMANIAK_API_URL:-$(_readaccountconf_mutable INFOMANIAK_API_URL)}"
  82. INFOMANIAK_TTL="${INFOMANIAK_TTL:-$(_readaccountconf_mutable INFOMANIAK_TTL)}"
  83. if [ -z "$INFOMANIAK_API_TOKEN" ]; then
  84. INFOMANIAK_API_TOKEN=""
  85. _err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
  86. return 1
  87. fi
  88. if [ -z "$INFOMANIAK_API_URL" ]; then
  89. INFOMANIAK_API_URL="$DEFAULT_INFOMANIAK_API_URL"
  90. fi
  91. if [ -z "$INFOMANIAK_TTL" ]; then
  92. INFOMANIAK_TTL="$DEFAULT_INFOMANIAK_TTL"
  93. fi
  94. #save the token to the account conf file.
  95. _saveaccountconf_mutable INFOMANIAK_API_TOKEN "$INFOMANIAK_API_TOKEN"
  96. if [ "$INFOMANIAK_API_URL" != "$DEFAULT_INFOMANIAK_API_URL" ]; then
  97. _saveaccountconf_mutable INFOMANIAK_API_URL "$INFOMANIAK_API_URL"
  98. fi
  99. if [ "$INFOMANIAK_TTL" != "$DEFAULT_INFOMANIAK_TTL" ]; then
  100. _saveaccountconf_mutable INFOMANIAK_TTL "$INFOMANIAK_TTL"
  101. fi
  102. export _H1="Authorization: Bearer $INFOMANIAK_API_TOKEN"
  103. export _H2="ContentType: application/json"
  104. fulldomain=$1
  105. txtvalue=$2
  106. _info "Infomaniak DNS API"
  107. _debug fulldomain "$fulldomain"
  108. _debug txtvalue "$txtvalue"
  109. fqdn=${fulldomain#_acme-challenge.}
  110. # guess which base domain to add record to
  111. zone_and_id=$(_find_zone "$fqdn")
  112. if [ -z "$zone_and_id" ]; then
  113. _err "cannot find zone to modify"
  114. return 1
  115. fi
  116. zone=${zone_and_id% *}
  117. domain_id=${zone_and_id#* }
  118. # extract first part of domain
  119. key=${fulldomain%."$zone"}
  120. _debug "zone:$zone id:$domain_id key:$key"
  121. # find previous record
  122. # shellcheck disable=SC1004
  123. record_id=$(_get "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record" | sed 's/.*"data":\[\(.*\)\]}/\1/; s/},{/}\
  124. {/g' | sed -n 's/.*"id":"*\([0-9]*\)"*.*"source_idn":"'"$fulldomain"'".*"target_idn":"'"$txtvalue"'".*/\1/p')
  125. if [ -z "$record_id" ]; then
  126. _err "could not find record to delete"
  127. return 1
  128. fi
  129. _debug "record_id: $record_id"
  130. # API call
  131. response=$(_post "" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record/$record_id" "" DELETE)
  132. if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
  133. _info "Record deleted"
  134. return 0
  135. fi
  136. _err "could not delete record"
  137. return 1
  138. }
  139. #################### Private functions below ##################################
  140. _get_domain_id() {
  141. domain="$1"
  142. # shellcheck disable=SC1004
  143. _get "${INFOMANIAK_API_URL}/1/product?service_name=domain&customer_name=$domain" | sed 's/.*"data":\[{\(.*\)}\]}/\1/; s/,/\
  144. /g' | sed -n 's/^"id":\(.*\)/\1/p'
  145. }
  146. _find_zone() {
  147. zone="$1"
  148. # find domain in list, removing . parts sequentialy
  149. while _contains "$zone" '\.'; do
  150. _debug "testing $zone"
  151. id=$(_get_domain_id "$zone")
  152. if [ -n "$id" ]; then
  153. echo "$zone $id"
  154. return
  155. fi
  156. zone=${zone#*.}
  157. done
  158. }