]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_cf.sh
fix check email
[mirror_acme.sh.git] / dnsapi / dns_cf.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
175c9dec 2
175c9dec 3#
4#CF_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
5#
6#CF_Email="xxxx@sss.com"
7
a4270efa 8CF_Api="https://api.cloudflare.com/client/v4"
175c9dec 9
638b9a05 10######## Public functions #####################
11
12#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
4c2a3841 13dns_cf_add() {
175c9dec 14 fulldomain=$1
15 txtvalue=$2
4c2a3841 16
17 if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
797cbb9b 18 CF_Key=""
19 CF_Email=""
ab497961 20 _err "You don't specify cloudflare api key and email yet."
21 _err "Please create you key and try again."
22 return 1
23 fi
4c2a3841 24
4a56b240 25 if ! _contains "$CF_Email" "@"; then
26 _err "It seems that the CF_Email=$CF_Email is not a valid email address."
27 _err "Please check and retry."
28 return 1
29 fi
30
e9209938 31 #save the api key and email to the account conf file.
32 _saveaccountconf CF_Key "$CF_Key"
33 _saveaccountconf CF_Email "$CF_Email"
4c2a3841 34
1b5bd0e0 35 _debug "First detect the root zone"
c7b16249 36 if ! _get_root "$fulldomain"; then
175c9dec 37 _err "invalid domain"
38 return 1
39 fi
e6d31b4e 40 _debug _domain_id "$_domain_id"
41 _debug _sub_domain "$_sub_domain"
42 _debug _domain "$_domain"
4c2a3841 43
1b5bd0e0 44 _debug "Getting txt records"
a4270efa 45 _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
4c2a3841 46
c7b16249 47 if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
175c9dec 48 _err "Error"
49 return 1
50 fi
4c2a3841 51
e440223b 52 count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
a4270efa 53 _debug count "$count"
4c2a3841 54 if [ "$count" = "0" ]; then
175c9dec 55 _info "Adding record"
4c2a3841 56 if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
c7b16249 57 if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then
638b9a05 58 _info "Added, sleeping 10 seconds"
0ed4c939 59 sleep 10
60 #todo: check if the record takes effect
638b9a05 61 return 0
62 else
63 _err "Add txt record error."
64 return 1
65 fi
175c9dec 66 fi
67 _err "Add txt record error."
68 else
69 _info "Updating record"
e440223b 70 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
c7b16249 71 _debug "record_id" "$record_id"
4c2a3841 72
73 _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\"}"
8d5618c4 74 if [ "$?" = "0" ]; then
175c9dec 75 _info "Updated, sleeping 10 seconds"
76 sleep 10
0ed4c939 77 #todo: check if the record takes effect
4c2a3841 78 return 0
175c9dec 79 fi
80 _err "Update error"
81 return 1
82 fi
175c9dec 83
4c2a3841 84}
175c9dec 85
5d6fd809 86#fulldomain
87dns_cf_rm() {
88 fulldomain=$1
638b9a05 89
5d6fd809 90}
638b9a05 91
638b9a05 92#################### Private functions bellow ##################################
175c9dec 93#_acme-challenge.www.domain.com
1b5bd0e0 94#returns
175c9dec 95# _sub_domain=_acme-challenge.www
96# _domain=domain.com
97# _domain_id=sdjkglgdfewsdfg
98_get_root() {
99 domain=$1
100 i=2
101 p=1
c7b16249 102 while true; do
103 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
4c2a3841 104 if [ -z "$h" ]; then
175c9dec 105 #not valid
4c2a3841 106 return 1
175c9dec 107 fi
4c2a3841 108
109 if ! _cf_rest GET "zones?name=$h"; then
175c9dec 110 return 1
111 fi
4c2a3841 112
c7b16249 113 if printf "%s" "$response" | grep "\"name\":\"$h\"" >/dev/null; then
e440223b 114 _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
4c2a3841 115 if [ "$_domain_id" ]; then
c7b16249 116 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
175c9dec 117 _domain=$h
118 return 0
119 fi
120 return 1
121 fi
122 p=$i
c7b16249 123 i=$(_math "$i" + 1)
175c9dec 124 done
125 return 1
126}
127
175c9dec 128_cf_rest() {
129 m=$1
130 ep="$2"
a4270efa 131 data="$3"
c7b16249 132 _debug "$ep"
4c2a3841 133
a4270efa 134 _H1="X-Auth-Email: $CF_Email"
135 _H2="X-Auth-Key: $CF_Key"
136 _H3="Content-Type: application/json"
4c2a3841 137
138 if [ "$data" ]; then
1b5bd0e0 139 _debug data "$data"
c7b16249 140 response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
638b9a05 141 else
a4270efa 142 response="$(_get "$CF_Api/$ep")"
175c9dec 143 fi
4c2a3841 144
145 if [ "$?" != "0" ]; then
638b9a05 146 _err "error $ep"
175c9dec 147 return 1
148 fi
a63b05a9 149 _debug2 response "$response"
175c9dec 150 return 0
151}