]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_me.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_me.sh
1 #!/usr/bin/env sh
2
3 # bug reports to dev@1e.ca
4
5 # ME_Key=qmlkdjflmkqdjf
6 # ME_Secret=qmsdlkqmlksdvnnpae
7
8 ME_Api=https://api.dnsmadeeasy.com/V2.0/dns/managed
9
10 ######## Public functions #####################
11
12 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
13 dns_me_add() {
14 fulldomain=$1
15 txtvalue=$2
16
17 if [ -z "$ME_Key" ] || [ -z "$ME_Secret" ]; then
18 ME_Key=""
19 ME_Secret=""
20 _err "You didn't specify DNSMadeEasy api key and secret yet."
21 _err "Please create you key and try again."
22 return 1
23 fi
24
25 #save the api key and email to the account conf file.
26 _saveaccountconf ME_Key "$ME_Key"
27 _saveaccountconf ME_Secret "$ME_Secret"
28
29 _debug "First detect the root zone"
30 if ! _get_root "$fulldomain"; then
31 _err "invalid domain"
32 return 1
33 fi
34 _debug _domain_id "$_domain_id"
35 _debug _sub_domain "$_sub_domain"
36 _debug _domain "$_domain"
37
38 _debug "Getting txt records"
39 _me_rest GET "${_domain_id}/records?recordName=$_sub_domain&type=TXT"
40
41 if ! _contains "$response" "\"totalRecords\":"; then
42 _err "Error"
43 return 1
44 fi
45
46 _info "Adding record"
47 if _me_rest POST "$_domain_id/records/" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\",\"gtdLocation\":\"DEFAULT\",\"ttl\":120}"; then
48 if printf -- "%s" "$response" | grep \"id\": >/dev/null; then
49 _info "Added"
50 #todo: check if the record takes effect
51 return 0
52 else
53 _err "Add txt record error."
54 return 1
55 fi
56 fi
57
58 }
59
60 #fulldomain
61 dns_me_rm() {
62 fulldomain=$1
63 txtvalue=$2
64 _debug "First detect the root zone"
65 if ! _get_root "$fulldomain"; then
66 _err "invalid domain"
67 return 1
68 fi
69 _debug _domain_id "$_domain_id"
70 _debug _sub_domain "$_sub_domain"
71 _debug _domain "$_domain"
72
73 _debug "Getting txt records"
74 _me_rest GET "${_domain_id}/records?recordName=$_sub_domain&type=TXT"
75
76 count=$(printf "%s\n" "$response" | _egrep_o "\"totalRecords\":[^,]*" | cut -d : -f 2)
77 _debug count "$count"
78 if [ "$count" = "0" ]; then
79 _info "Don't need to remove."
80 else
81 record_id=$(printf "%s\n" "$response" | _egrep_o ",\"value\":\"..$txtvalue..\",\"id\":[^,]*" | cut -d : -f 3 | head -n 1)
82 _debug "record_id" "$record_id"
83 if [ -z "$record_id" ]; then
84 _err "Can not get record id to remove."
85 return 1
86 fi
87 if ! _me_rest DELETE "$_domain_id/records/$record_id"; then
88 _err "Delete record error."
89 return 1
90 fi
91 _contains "$response" ''
92 fi
93 }
94
95 #################### Private functions below ##################################
96 #_acme-challenge.www.domain.com
97 #returns
98 # _sub_domain=_acme-challenge.www
99 # _domain=domain.com
100 # _domain_id=sdjkglgdfewsdfg
101 _get_root() {
102 domain=$1
103 i=2
104 p=1
105 while true; do
106 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
107 if [ -z "$h" ]; then
108 #not valid
109 return 1
110 fi
111
112 if ! _me_rest GET "name?domainname=$h"; then
113 return 1
114 fi
115
116 if _contains "$response" "\"name\":\"$h\""; then
117 _domain_id=$(printf "%s\n" "$response" | sed 's/^{//; s/}$//; s/{.*}//' | sed -r 's/^.*"id":([0-9]+).*$/\1/')
118 if [ "$_domain_id" ]; then
119 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
120 _domain="$h"
121 return 0
122 fi
123 return 1
124 fi
125 p=$i
126 i=$(_math "$i" + 1)
127 done
128 return 1
129 }
130
131 _me_rest() {
132 m=$1
133 ep="$2"
134 data="$3"
135 _debug "$ep"
136
137 cdate=$(LANG=C date -u +"%a, %d %b %Y %T %Z")
138 hmac=$(printf "%s" "$cdate" | _hmac sha1 "$(printf "%s" "$ME_Secret" | _hex_dump | tr -d " ")" hex)
139
140 export _H1="x-dnsme-apiKey: $ME_Key"
141 export _H2="x-dnsme-requestDate: $cdate"
142 export _H3="x-dnsme-hmac: $hmac"
143
144 if [ "$m" != "GET" ]; then
145 _debug data "$data"
146 response="$(_post "$data" "$ME_Api/$ep" "" "$m")"
147 else
148 response="$(_get "$ME_Api/$ep")"
149 fi
150
151 if [ "$?" != "0" ]; then
152 _err "error $ep"
153 return 1
154 fi
155 _debug2 response "$response"
156 return 0
157 }