]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_gd.sh
Update dns_nic.sh
[mirror_acme.sh.git] / dnsapi / dns_gd.sh
1 #!/usr/bin/env sh
2
3 #Godaddy domain api
4 #
5 #GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
6 #
7 #GD_Secret="asdfsdfsfsdfsdfdfsdf"
8
9 GD_Api="https://api.godaddy.com/v1"
10
11 ######## Public functions #####################
12
13 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
14 dns_gd_add() {
15 fulldomain=$1
16 txtvalue=$2
17
18 GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
19 GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
20 if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ]; then
21 GD_Key=""
22 GD_Secret=""
23 _err "You don't specify godaddy api key and secret yet."
24 _err "Please create you key and try again."
25 return 1
26 fi
27
28 #save the api key and email to the account conf file.
29 _saveaccountconf_mutable GD_Key "$GD_Key"
30 _saveaccountconf_mutable GD_Secret "$GD_Secret"
31
32 _debug "First detect the root zone"
33 if ! _get_root "$fulldomain"; then
34 _err "invalid domain"
35 return 1
36 fi
37
38 _debug _sub_domain "$_sub_domain"
39 _debug _domain "$_domain"
40
41 _debug "Getting existing records"
42 if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
43 return 1
44 fi
45
46 if _contains "$response" "$txtvalue"; then
47 _info "The record is existing, skip"
48 return 0
49 fi
50
51 _add_data="{\"data\":\"$txtvalue\"}"
52 for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
53 _debug2 t "$t"
54 if [ "$t" ]; then
55 _add_data="$_add_data,{\"data\":$t}"
56 fi
57 done
58 _debug2 _add_data "$_add_data"
59
60 _info "Adding record"
61 if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"; then
62 _info "Added, sleeping 10 seconds"
63 _sleep 10
64 #todo: check if the record takes effect
65 return 0
66 fi
67 _err "Add txt record error."
68 return 1
69 }
70
71 #fulldomain
72 dns_gd_rm() {
73 fulldomain=$1
74 txtvalue=$2
75
76 GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
77 GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
78
79 _debug "First detect the root zone"
80 if ! _get_root "$fulldomain"; then
81 _err "invalid domain"
82 return 1
83 fi
84
85 _debug _sub_domain "$_sub_domain"
86 _debug _domain "$_domain"
87
88 _debug "Getting existing records"
89 if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
90 return 1
91 fi
92
93 if ! _contains "$response" "$txtvalue"; then
94 _info "The record is not existing, skip"
95 return 0
96 fi
97
98 _add_data=""
99 for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
100 _debug2 t "$t"
101 if [ "$t" ] && [ "$t" != "\"$txtvalue\"" ]; then
102 if [ "$_add_data" ]; then
103 _add_data="$_add_data,{\"data\":$t}"
104 else
105 _add_data="{\"data\":$t}"
106 fi
107 fi
108 done
109 if [ -z "$_add_data" ]; then
110 _add_data="{\"data\":\"\"}"
111 fi
112 _debug2 _add_data "$_add_data"
113
114 _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"
115 }
116
117 #################### Private functions below ##################################
118 #_acme-challenge.www.domain.com
119 #returns
120 # _sub_domain=_acme-challenge.www
121 # _domain=domain.com
122 _get_root() {
123 domain=$1
124 i=2
125 p=1
126 while true; do
127 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
128 if [ -z "$h" ]; then
129 #not valid
130 return 1
131 fi
132
133 if ! _gd_rest GET "domains/$h"; then
134 return 1
135 fi
136
137 if _contains "$response" '"code":"NOT_FOUND"'; then
138 _debug "$h not found"
139 else
140 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
141 _domain="$h"
142 return 0
143 fi
144 p="$i"
145 i=$(_math "$i" + 1)
146 done
147 return 1
148 }
149
150 _gd_rest() {
151 m=$1
152 ep="$2"
153 data="$3"
154 _debug "$ep"
155
156 export _H1="Authorization: sso-key $GD_Key:$GD_Secret"
157 export _H2="Content-Type: application/json"
158
159 if [ "$data" ]; then
160 _debug data "$data"
161 response="$(_post "$data" "$GD_Api/$ep" "" "$m")"
162 else
163 response="$(_get "$GD_Api/$ep")"
164 fi
165
166 if [ "$?" != "0" ]; then
167 _err "error $ep"
168 return 1
169 fi
170 _debug2 response "$response"
171 if _contains "$response" "UNABLE_TO_AUTHENTICATE"; then
172 _err "It seems that your api key or secret is not correct."
173 return 1
174 fi
175 return 0
176 }