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.

199 lines
5.9 KiB

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