]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_cf.sh
f50ab498058a0e1c9dee5454361f16d042672e3f
[mirror_acme.sh.git] / dnsapi / dns_cf.sh
1 #!/usr/bin/env sh
2
3 #
4 #CF_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
5 #
6 #CF_Email="xxxx@sss.com"
7
8 CF_Api="https://api.cloudflare.com/client/v4"
9
10 ######## Public functions #####################
11
12 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
13 dns_cf_add() {
14 fulldomain=$1
15 txtvalue=$2
16
17 CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
18 CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
19 if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
20 CF_Key=""
21 CF_Email=""
22 _err "You didn't specify a cloudflare api key and email yet."
23 _err "Please create the key and try again."
24 return 1
25 fi
26
27 if ! _contains "$CF_Email" "@"; then
28 _err "It seems that the CF_Email=$CF_Email is not a valid email address."
29 _err "Please check and retry."
30 return 1
31 fi
32
33 #save the api key and email to the account conf file.
34 _saveaccountconf_mutable CF_Key "$CF_Key"
35 _saveaccountconf_mutable CF_Email "$CF_Email"
36
37 _DOMAIN_CF_ZONES_CACHE_NAME_="$(echo "${CF_Email}_CF_ZONES_" | tr '@.' '__')"
38 _cleardomainconf "$_DOMAIN_CF_ZONES_CACHE_NAME_"
39
40 _debug "First detect the root zone"
41 if ! _get_root "$fulldomain"; then
42 _err "invalid domain"
43 return 1
44 fi
45 _debug _domain_id "$_domain_id"
46 _debug _sub_domain "$_sub_domain"
47 _debug _domain "$_domain"
48
49 _debug "Getting txt records"
50 _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
51
52 if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
53 _err "Error"
54 return 1
55 fi
56
57 # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
58 # we can not use updating anymore.
59 # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
60 # _debug count "$count"
61 # if [ "$count" = "0" ]; then
62 _info "Adding record"
63 if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
64 if _contains "$response" "$fulldomain"; then
65 _info "Added, OK"
66 return 0
67 elif _contains "$response" "The record already exists"; then
68 _info "Already exists, OK"
69 return 0
70 else
71 _err "Add txt record error."
72 return 1
73 fi
74 fi
75 _err "Add txt record error."
76 return 1
77 # else
78 # _info "Updating record"
79 # record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
80 # _debug "record_id" "$record_id"
81 #
82 # _cf_rest PUT "zones/$_domain_id/dns_records/$record_id" "{\"id\":\"$record_id\",\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"zone_id\":\"$_domain_id\",\"zone_name\":\"$_domain\"}"
83 # if [ "$?" = "0" ]; then
84 # _info "Updated, OK"
85 # return 0
86 # fi
87 # _err "Update error"
88 # return 1
89 # fi
90
91 }
92
93 #fulldomain txtvalue
94 dns_cf_rm() {
95 fulldomain=$1
96 txtvalue=$2
97
98 CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
99 CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
100 if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
101 CF_Key=""
102 CF_Email=""
103 _err "You didn't specify a cloudflare api key and email yet."
104 _err "Please create the key and try again."
105 return 1
106 fi
107
108 _DOMAIN_CF_ZONES_CACHE_NAME_="$(echo "${CF_Email}_CF_ZONES_" | tr '@.' '__')"
109
110 _debug "First detect the root zone"
111 if ! _get_root "$fulldomain"; then
112 _cleardomainconf "$_DOMAIN_CF_ZONES_CACHE_NAME_"
113 _err "invalid domain"
114 return 1
115 fi
116 _cleardomainconf "$_DOMAIN_CF_ZONES_CACHE_NAME_"
117
118 _debug _domain_id "$_domain_id"
119 _debug _sub_domain "$_sub_domain"
120 _debug _domain "$_domain"
121
122 _debug "Getting txt records"
123 _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
124
125 if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
126 _err "Error"
127 return 1
128 fi
129
130 count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
131 _debug count "$count"
132 if [ "$count" = "0" ]; then
133 _info "Don't need to remove."
134 else
135 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
136 _debug "record_id" "$record_id"
137 if [ -z "$record_id" ]; then
138 _err "Can not get record id to remove."
139 return 1
140 fi
141 if ! _cf_rest DELETE "zones/$_domain_id/dns_records/$record_id"; then
142 _err "Delete record error."
143 return 1
144 fi
145 _contains "$response" '"success":true'
146 fi
147
148 }
149
150 #################### Private functions below ##################################
151 #_acme-challenge.www.domain.com
152 #returns
153 # _sub_domain=_acme-challenge.www
154 # _domain=domain.com
155 # _domain_id=sdjkglgdfewsdfg
156 _get_root() {
157
158 _cf_zones="$(_readdomainconf "$_DOMAIN_CF_ZONES_CACHE_NAME_")"
159 _debug2 "_cf_zones" "$_cf_zones"
160 if [ -z "$_cf_zones" ]; then
161 _debug "$_DOMAIN_CF_ZONES_CACHE_NAME_ is none, so get it."
162 if ! _cf_rest GET "zones"; then
163 return 1
164 fi
165 _cf_zones="$response"
166 _savedomainconf "$_DOMAIN_CF_ZONES_CACHE_NAME_" "$(echo "$_cf_zones" | _base64)"
167 else
168 _debug "$_DOMAIN_CF_ZONES_CACHE_NAME_ found"
169 _cf_zones="$(echo "$_cf_zones" | _dbase64)"
170 fi
171
172 domain=$1
173 i=2
174 p=1
175 while true; do
176 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
177 _debug h "$h"
178 if [ -z "$h" ]; then
179 #not valid
180 return 1
181 fi
182
183 if _contains "$_cf_zones" "\"name\":\"$h\"" >/dev/null; then
184 _domain_id=$(echo "$_cf_zones" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "^\"id\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
185 if [ "$_domain_id" ]; then
186 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
187 _domain=$h
188 return 0
189 fi
190 return 1
191 fi
192 p=$i
193 i=$(_math "$i" + 1)
194 done
195 return 1
196 }
197
198 _cf_rest() {
199 m=$1
200 ep="$2"
201 data="$3"
202 _debug "$ep"
203
204 export _H1="X-Auth-Email: $CF_Email"
205 export _H2="X-Auth-Key: $CF_Key"
206 export _H3="Content-Type: application/json"
207
208 if [ "$m" != "GET" ]; then
209 _debug data "$data"
210 response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
211 else
212 response="$(_get "$CF_Api/$ep")"
213 fi
214
215 if [ "$?" != "0" ]; then
216 _err "error $ep"
217 return 1
218 fi
219 _debug2 response "$response"
220 return 0
221 }