]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_gd.sh
Merge pull request #4776 from KincaidYang/master
[mirror_acme.sh.git] / dnsapi / dns_gd.sh
1 #!/usr/bin/env sh
2
3 #Godaddy domain api
4 # Get API key and secret from https://developer.godaddy.com/
5 #
6 # GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
7 # GD_Secret="asdfsdfsfsdfsdfdfsdf"
8 #
9 # Ex.: acme.sh --issue --staging --dns dns_gd -d "*.s.example.com" -d "s.example.com"
10
11 GD_Api="https://api.godaddy.com/v1"
12
13 ######## Public functions #####################
14
15 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 dns_gd_add() {
17 fulldomain=$1
18 txtvalue=$2
19
20 GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
21 GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
22 if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ]; then
23 GD_Key=""
24 GD_Secret=""
25 _err "You didn't specify godaddy api key and secret yet."
26 _err "Please create your key and try again."
27 return 1
28 fi
29
30 #save the api key and email to the account conf file.
31 _saveaccountconf_mutable GD_Key "$GD_Key"
32 _saveaccountconf_mutable GD_Secret "$GD_Secret"
33
34 _debug "First detect the root zone"
35 if ! _get_root "$fulldomain"; then
36 _err "invalid domain"
37 return 1
38 fi
39
40 _debug _sub_domain "$_sub_domain"
41 _debug _domain "$_domain"
42
43 _debug "Getting existing records"
44 if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
45 return 1
46 fi
47
48 if _contains "$response" "$txtvalue"; then
49 _info "This record already exists, skipping"
50 return 0
51 fi
52
53 _add_data="{\"data\":\"$txtvalue\"}"
54 for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
55 _debug2 t "$t"
56 # ignore empty (previously removed) records, to prevent useless _acme-challenge TXT entries
57 if [ "$t" ] && [ "$t" != '""' ]; then
58 _add_data="$_add_data,{\"data\":$t}"
59 fi
60 done
61 _debug2 _add_data "$_add_data"
62
63 _info "Adding record"
64 if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"; then
65 _debug "Checking updated records of '${fulldomain}'"
66
67 if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
68 _err "Validating TXT record for '${fulldomain}' with rest error [$?]." "$response"
69 return 1
70 fi
71
72 if ! _contains "$response" "$txtvalue"; then
73 _err "TXT record '${txtvalue}' for '${fulldomain}', value wasn't set!"
74 return 1
75 fi
76 else
77 _err "Add txt record error, value '${txtvalue}' for '${fulldomain}' was not set."
78 return 1
79 fi
80
81 _sleep 10
82 _info "Added TXT record '${txtvalue}' for '${fulldomain}'."
83 return 0
84 }
85
86 #fulldomain
87 dns_gd_rm() {
88 fulldomain=$1
89 txtvalue=$2
90
91 GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
92 GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
93
94 _debug "First detect the root zone"
95 if ! _get_root "$fulldomain"; then
96 _err "invalid domain"
97 return 1
98 fi
99
100 _debug _sub_domain "$_sub_domain"
101 _debug _domain "$_domain"
102
103 _debug "Getting existing records"
104 if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
105 return 1
106 fi
107
108 if ! _contains "$response" "$txtvalue"; then
109 _info "The record does not exist, skip"
110 return 0
111 fi
112
113 _add_data=""
114 for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
115 _debug2 t "$t"
116 if [ "$t" ] && [ "$t" != "\"$txtvalue\"" ]; then
117 if [ "$_add_data" ]; then
118 _add_data="$_add_data,{\"data\":$t}"
119 else
120 _add_data="{\"data\":$t}"
121 fi
122 fi
123 done
124 if [ -z "$_add_data" ]; then
125 # delete empty record
126 _debug "Delete last record for '${fulldomain}'"
127 if ! _gd_rest DELETE "domains/$_domain/records/TXT/$_sub_domain"; then
128 _err "Cannot delete empty TXT record for '$fulldomain'"
129 return 1
130 fi
131 else
132 # remove specific TXT value, keeping other entries
133 _debug2 _add_data "$_add_data"
134 if ! _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"; then
135 _err "Cannot update TXT record for '$fulldomain'"
136 return 1
137 fi
138 fi
139 }
140
141 #################### Private functions below ##################################
142 #_acme-challenge.www.domain.com
143 #returns
144 # _sub_domain=_acme-challenge.www
145 # _domain=domain.com
146 _get_root() {
147 domain=$1
148 i=2
149 p=1
150 while true; do
151 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
152 if [ -z "$h" ]; then
153 #not valid
154 return 1
155 fi
156
157 if ! _gd_rest GET "domains/$h"; then
158 return 1
159 fi
160
161 if _contains "$response" '"code":"NOT_FOUND"'; then
162 _debug "$h not found"
163 else
164 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
165 _domain="$h"
166 return 0
167 fi
168 p="$i"
169 i=$(_math "$i" + 1)
170 done
171 return 1
172 }
173
174 _gd_rest() {
175 m=$1
176 ep="$2"
177 data="$3"
178 _debug "$ep"
179
180 export _H1="Authorization: sso-key $GD_Key:$GD_Secret"
181 export _H2="Content-Type: application/json"
182
183 if [ "$data" ] || [ "$m" = "DELETE" ]; then
184 _debug "data ($m): " "$data"
185 response="$(_post "$data" "$GD_Api/$ep" "" "$m")"
186 else
187 response="$(_get "$GD_Api/$ep")"
188 fi
189
190 if [ "$?" != "0" ]; then
191 _err "error on rest call ($m): $ep"
192 return 1
193 fi
194 _debug2 response "$response"
195 if _contains "$response" "UNABLE_TO_AUTHENTICATE"; then
196 _err "It seems that your api key or secret is not correct."
197 return 1
198 fi
199 return 0
200 }