3 ###############################################################################
4 # Infomaniak API integration
6 # To use this API you need visit the API dashboard of your account
7 # once logged into https://manager.infomaniak.com add /api/dashboard to the URL
9 # Please report bugs to
10 # https://github.com/acmesh-official/acme.sh/issues/3188
12 # Note: the URL looks like this:
13 # https://manager.infomaniak.com/v3/<account_id>/api/dashboard
14 # Then generate a token with the scope Domain
15 # this is given as an environment variable INFOMANIAK_API_TOKEN
16 ###############################################################################
20 DEFAULT_INFOMANIAK_API_URL
="https://api.infomaniak.com"
21 DEFAULT_INFOMANIAK_TTL
=300
23 ######## Public functions #####################
25 #Usage: dns_infomaniak_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
26 dns_infomaniak_add
() {
28 INFOMANIAK_API_TOKEN
="${INFOMANIAK_API_TOKEN:-$(_readaccountconf_mutable INFOMANIAK_API_TOKEN)}"
29 INFOMANIAK_API_URL
="${INFOMANIAK_API_URL:-$(_readaccountconf_mutable INFOMANIAK_API_URL)}"
30 INFOMANIAK_TTL
="${INFOMANIAK_TTL:-$(_readaccountconf_mutable INFOMANIAK_TTL)}"
32 if [ -z "$INFOMANIAK_API_TOKEN" ]; then
33 INFOMANIAK_API_TOKEN
=""
34 _err
"Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
38 if [ -z "$INFOMANIAK_API_URL" ]; then
39 INFOMANIAK_API_URL
="$DEFAULT_INFOMANIAK_API_URL"
42 if [ -z "$INFOMANIAK_TTL" ]; then
43 INFOMANIAK_TTL
="$DEFAULT_INFOMANIAK_TTL"
46 #save the token to the account conf file.
47 _saveaccountconf_mutable INFOMANIAK_API_TOKEN
"$INFOMANIAK_API_TOKEN"
49 if [ "$INFOMANIAK_API_URL" != "$DEFAULT_INFOMANIAK_API_URL" ]; then
50 _saveaccountconf_mutable INFOMANIAK_API_URL
"$INFOMANIAK_API_URL"
53 if [ "$INFOMANIAK_TTL" != "$DEFAULT_INFOMANIAK_TTL" ]; then
54 _saveaccountconf_mutable INFOMANIAK_TTL
"$INFOMANIAK_TTL"
57 export _H1
="Authorization: Bearer $INFOMANIAK_API_TOKEN"
58 export _H2
="Content-Type: application/json"
63 _info
"Infomaniak DNS API"
64 _debug fulldomain
"$fulldomain"
65 _debug txtvalue
"$txtvalue"
67 fqdn
=${fulldomain#_acme-challenge.}
69 # guess which base domain to add record to
70 zone_and_id
=$
(_find_zone
"$fqdn")
71 if [ -z "$zone_and_id" ]; then
72 _err
"cannot find zone to modify"
75 zone
=${zone_and_id% *}
76 domain_id
=${zone_and_id#* }
78 # extract first part of domain
79 key
=${fulldomain%.$zone}
81 _debug
"zone:$zone id:$domain_id key:$key"
84 data
="{\"type\": \"TXT\", \"source\": \"$key\", \"target\": \"$txtvalue\", \"ttl\": $INFOMANIAK_TTL}"
87 response
=$
(_post
"$data" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record")
88 if [ -n "$response" ] && echo "$response" | _contains
'"result":"success"'; then
90 _debug
"Response: $response"
93 _err
"could not create record"
94 _debug
"Response: $response"
98 #Usage: fulldomain txtvalue
99 #Remove the txt record after validation.
100 dns_infomaniak_rm
() {
102 INFOMANIAK_API_TOKEN
="${INFOMANIAK_API_TOKEN:-$(_readaccountconf_mutable INFOMANIAK_API_TOKEN)}"
103 INFOMANIAK_API_URL
="${INFOMANIAK_API_URL:-$(_readaccountconf_mutable INFOMANIAK_API_URL)}"
104 INFOMANIAK_TTL
="${INFOMANIAK_TTL:-$(_readaccountconf_mutable INFOMANIAK_TTL)}"
106 if [ -z "$INFOMANIAK_API_TOKEN" ]; then
107 INFOMANIAK_API_TOKEN
=""
108 _err
"Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
112 if [ -z "$INFOMANIAK_API_URL" ]; then
113 INFOMANIAK_API_URL
="$DEFAULT_INFOMANIAK_API_URL"
116 if [ -z "$INFOMANIAK_TTL" ]; then
117 INFOMANIAK_TTL
="$DEFAULT_INFOMANIAK_TTL"
120 #save the token to the account conf file.
121 _saveaccountconf_mutable INFOMANIAK_API_TOKEN
"$INFOMANIAK_API_TOKEN"
123 if [ "$INFOMANIAK_API_URL" != "$DEFAULT_INFOMANIAK_API_URL" ]; then
124 _saveaccountconf_mutable INFOMANIAK_API_URL
"$INFOMANIAK_API_URL"
127 if [ "$INFOMANIAK_TTL" != "$DEFAULT_INFOMANIAK_TTL" ]; then
128 _saveaccountconf_mutable INFOMANIAK_TTL
"$INFOMANIAK_TTL"
131 export _H1
="Authorization: Bearer $INFOMANIAK_API_TOKEN"
132 export _H2
="ContentType: application/json"
136 _info
"Infomaniak DNS API"
137 _debug fulldomain
"$fulldomain"
138 _debug txtvalue
"$txtvalue"
140 fqdn
=${fulldomain#_acme-challenge.}
142 # guess which base domain to add record to
143 zone_and_id
=$
(_find_zone
"$fqdn")
144 if [ -z "$zone_and_id" ]; then
145 _err
"cannot find zone to modify"
148 zone
=${zone_and_id% *}
149 domain_id
=${zone_and_id#* }
151 # extract first part of domain
152 key
=${fulldomain%.$zone}
154 _debug
"zone:$zone id:$domain_id key:$key"
156 # find previous record
157 # shellcheck disable=SC1004
158 record_id
=$
(_get
"${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record" |
sed 's/.*"data":\[\(.*\)\]}/\1/; s/},{/}\
159 {/g' |
sed -n 's/.*"id":"*\([0-9]*\)"*.*"source_idn":"'"$fulldomain"'".*"target_idn":"'"$txtvalue"'".*/\1/p')
160 if [ -z "$record_id" ]; then
161 _err
"could not find record to delete"
164 _debug
"record_id: $record_id"
167 response
=$
(_post
"" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record/$record_id" "" DELETE
)
168 if [ -n "$response" ] && echo "$response" | _contains
'"result":"success"'; then
169 _info
"Record deleted"
172 _err
"could not delete record"
176 #################### Private functions below ##################################
181 # shellcheck disable=SC1004
182 _get
"${INFOMANIAK_API_URL}/1/product?service_name=domain&customer_name=$domain" |
sed 's/.*"data":\[{\(.*\)}\]}/\1/; s/,/\
183 /g' |
sed -n 's/^"id":\(.*\)/\1/p'
189 # find domain in list, removing . parts sequentialy
190 while _contains
"$zone" '\.'; do
191 _debug
"testing $zone"
192 id
=$
(_get_domain_id
"$zone")
193 if [ -n "$id" ]; then