]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_cf.sh
Merge branch 'master' of github.com:Neilpang/acme.sh
[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 _debug "First detect the root zone"
38 if ! _get_root "$fulldomain"; then
39 _err "invalid domain"
40 return 1
41 fi
42 _debug _domain_id "$_domain_id"
43 _debug _sub_domain "$_sub_domain"
44 _debug _domain "$_domain"
45
46 _debug "Getting txt records"
47 _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
48
49 if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
50 _err "Error"
51 return 1
52 fi
53
54 # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
55 # we can not use updating anymore.
56 # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
57 # _debug count "$count"
58 # if [ "$count" = "0" ]; then
59 _info "Adding record"
60 if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
61 if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then
62 _info "Added, OK"
63 return 0
64 else
65 _err "Add txt record error."
66 return 1
67 fi
68 fi
69 _err "Add txt record error."
70 return 1
71 # else
72 # _info "Updating record"
73 # record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
74 # _debug "record_id" "$record_id"
75 #
76 # _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\"}"
77 # if [ "$?" = "0" ]; then
78 # _info "Updated, OK"
79 # return 0
80 # fi
81 # _err "Update error"
82 # return 1
83 # fi
84
85 }
86
87 #fulldomain txtvalue
88 dns_cf_rm() {
89 fulldomain=$1
90 txtvalue=$2
91
92 CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
93 CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
94 if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
95 CF_Key=""
96 CF_Email=""
97 _err "You didn't specify a cloudflare api key and email yet."
98 _err "Please create the key and try again."
99 return 1
100 fi
101
102 _debug "First detect the root zone"
103 if ! _get_root "$fulldomain"; then
104 _err "invalid domain"
105 return 1
106 fi
107 _debug _domain_id "$_domain_id"
108 _debug _sub_domain "$_sub_domain"
109 _debug _domain "$_domain"
110
111 _debug "Getting txt records"
112 _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
113
114 if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
115 _err "Error"
116 return 1
117 fi
118
119 count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
120 _debug count "$count"
121 if [ "$count" = "0" ]; then
122 _info "Don't need to remove."
123 else
124 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
125 _debug "record_id" "$record_id"
126 if [ -z "$record_id" ]; then
127 _err "Can not get record id to remove."
128 return 1
129 fi
130 if ! _cf_rest DELETE "zones/$_domain_id/dns_records/$record_id"; then
131 _err "Delete record error."
132 return 1
133 fi
134 _contains "$response" '"success":true'
135 fi
136
137 }
138
139 #################### Private functions below ##################################
140 #_acme-challenge.www.domain.com
141 #returns
142 # _sub_domain=_acme-challenge.www
143 # _domain=domain.com
144 # _domain_id=sdjkglgdfewsdfg
145 _get_root() {
146 domain=$1
147 i=2
148 p=1
149 while true; do
150 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
151 _debug h "$h"
152 if [ -z "$h" ]; then
153 #not valid
154 return 1
155 fi
156
157 if ! _cf_rest GET "zones?name=$h"; then
158 return 1
159 fi
160
161 if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
162 _domain_id=$(printf "%s\n" "$response" | _egrep_o "\[.\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
163 if [ "$_domain_id" ]; then
164 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
165 _domain=$h
166 return 0
167 fi
168 return 1
169 fi
170 p=$i
171 i=$(_math "$i" + 1)
172 done
173 return 1
174 }
175
176 _cf_rest() {
177 m=$1
178 ep="$2"
179 data="$3"
180 _debug "$ep"
181
182 export _H1="X-Auth-Email: $CF_Email"
183 export _H2="X-Auth-Key: $CF_Key"
184 export _H3="Content-Type: application/json"
185
186 if [ "$m" != "GET" ]; then
187 _debug data "$data"
188 response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
189 else
190 response="$(_get "$CF_Api/$ep")"
191 fi
192
193 if [ "$?" != "0" ]; then
194 _err "error $ep"
195 return 1
196 fi
197 _debug2 response "$response"
198 return 0
199 }