]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_gd.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_gd.sh
CommitLineData
30de13b4 1#!/usr/bin/env sh
2
3#Godaddy domain api
2d4aa7ff 4# Get API key and secret from https://developer.godaddy.com/
30de13b4 5#
2d4aa7ff
A
6# GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
7# GD_Secret="asdfsdfsfsdfsdfdfsdf"
30de13b4 8#
2d4aa7ff 9# Ex.: acme.sh --issue --staging --dns dns_gd -d "*.s.example.com" -d "s.example.com"
30de13b4 10
30de13b4 11GD_Api="https://api.godaddy.com/v1"
12
13######## Public functions #####################
14
15#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
4c2a3841 16dns_gd_add() {
30de13b4 17 fulldomain=$1
18 txtvalue=$2
4c2a3841 19
c6f5c7f1 20 GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
21 GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
4c2a3841 22 if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ]; then
422e5026 23 GD_Key=""
24 GD_Secret=""
160513c6
ZP
25 _err "You didn't specify godaddy api key and secret yet."
26 _err "Please create your key and try again."
30de13b4 27 return 1
28 fi
4c2a3841 29
30de13b4 30 #save the api key and email to the account conf file.
c6f5c7f1 31 _saveaccountconf_mutable GD_Key "$GD_Key"
32 _saveaccountconf_mutable GD_Secret "$GD_Secret"
4c2a3841 33
30de13b4 34 _debug "First detect the root zone"
c7b16249 35 if ! _get_root "$fulldomain"; then
30de13b4 36 _err "invalid domain"
37 return 1
38 fi
e440223b 39
30de13b4 40 _debug _sub_domain "$_sub_domain"
41 _debug _domain "$_domain"
30de13b4 42
c6f5c7f1 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
160513c6 49 _info "This record already exists, skipping"
d8eb08e2 50 return 0
c6f5c7f1 51 fi
52
53 _add_data="{\"data\":\"$txtvalue\"}"
d8eb08e2 54 for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
c6f5c7f1 55 _debug2 t "$t"
5684b7c3
A
56 # ignore empty (previously removed) records, to prevent useless _acme-challenge TXT entries
57 if [ "$t" ] && [ "$t" != '""' ]; then
c6f5c7f1 58 _add_data="$_add_data,{\"data\":$t}"
59 fi
60 done
61 _debug2 _add_data "$_add_data"
62
30de13b4 63 _info "Adding record"
c6f5c7f1 64 if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"; then
5684b7c3
A
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
30de13b4 79 fi
5684b7c3
A
80
81 _sleep 10
82 _info "Added TXT record '${txtvalue}' for '${fulldomain}'."
83 return 0
4c2a3841 84}
30de13b4 85
5d6fd809 86#fulldomain
87dns_gd_rm() {
88 fulldomain=$1
c6f5c7f1 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
eca57bee 109 _info "The record does not exist, skip"
d8eb08e2 110 return 0
c6f5c7f1 111 fi
112
113 _add_data=""
d8eb08e2 114 for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
c6f5c7f1 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
5684b7c3
A
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
c6f5c7f1 138 fi
5d6fd809 139}
140
329174b6 141#################### Private functions below ##################################
30de13b4 142#_acme-challenge.www.domain.com
143#returns
144# _sub_domain=_acme-challenge.www
145# _domain=domain.com
30de13b4 146_get_root() {
147 domain=$1
148 i=2
149 p=1
c7b16249 150 while true; do
151 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
4c2a3841 152 if [ -z "$h" ]; then
30de13b4 153 #not valid
4c2a3841 154 return 1
30de13b4 155 fi
4c2a3841 156
157 if ! _gd_rest GET "domains/$h"; then
30de13b4 158 return 1
159 fi
4c2a3841 160
c7b16249 161 if _contains "$response" '"code":"NOT_FOUND"'; then
30de13b4 162 _debug "$h not found"
163 else
c7b16249 164 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
165 _domain="$h"
30de13b4 166 return 0
167 fi
e9f9f515 168 p="$i"
169 i=$(_math "$i" + 1)
30de13b4 170 done
171 return 1
172}
173
174_gd_rest() {
175 m=$1
176 ep="$2"
177 data="$3"
c7b16249 178 _debug "$ep"
4c2a3841 179
3ca93f4a
BB
180 export _H1="Authorization: sso-key $GD_Key:$GD_Secret"
181 export _H2="Content-Type: application/json"
4c2a3841 182
5684b7c3
A
183 if [ "$data" ] || [ "$m" = "DELETE" ]; then
184 _debug "data ($m): " "$data"
69925ce8 185 response="$(_post "$data" "$GD_Api/$ep" "" "$m")"
30de13b4 186 else
187 response="$(_get "$GD_Api/$ep")"
188 fi
4c2a3841 189
190 if [ "$?" != "0" ]; then
5684b7c3 191 _err "error on rest call ($m): $ep"
30de13b4 192 return 1
193 fi
194 _debug2 response "$response"
f90a2ae1 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
30de13b4 199 return 0
200}