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