]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_me.sh
DNSMadeEasy ACMEv2 support
[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 count=$(printf "%s\n" "$response" | _egrep_o "\"totalRecords\":[^,]*" | cut -d : -f 2)
47 _debug count "$count"
48 # _info "Adding record"
49 if _me_rest POST "$_domain_id/records/" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\",\"gtdLocation\":\"DEFAULT\",\"ttl\":120}"; then
50 if printf -- "%s" "$response" | grep \"id\": >/dev/null; then
51 _info "Added"
52 #todo: check if the record takes effect
53 return 0
54 else
55 _err "Add txt record error."
56 return 1
57 fi
58 fi
59 _err "Add txt record error."
60 return 1
61
62 }
63
64 #fulldomain
65 dns_me_rm() {
66 fulldomain=$1
67 txtvalue=$2
68 _debug "First detect the root zone"
69 if ! _get_root "$fulldomain"; then
70 _err "invalid domain"
71 return 1
72 fi
73 _debug _domain_id "$_domain_id"
74 _debug _sub_domain "$_sub_domain"
75 _debug _domain "$_domain"
76
77 _debug "Getting txt records"
78 _me_rest GET "${_domain_id}/records?recordName=$_sub_domain&type=TXT"
79
80 count=$(printf "%s\n" "$response" | _egrep_o "\"totalRecords\":[^,]*" | cut -d : -f 2)
81 _debug count "$count"
82 if [ "$count" = "0" ]; then
83 _info "Don't need to remove."
84 else
85 record_id=$(printf "%s\n" "$response" | _egrep_o ",\"value\":\"..$txtvalue..\",\"id\":[^,]*" | cut -d : -f 3 | head -n 1)
86 _debug "record_id" "$record_id"
87 if [ -z "$record_id" ]; then
88 _err "Can not get record id to remove."
89 return 1
90 fi
91 if ! _me_rest DELETE "$_domain_id/records/$record_id"; then
92 _err "Delete record error."
93 return 1
94 fi
95 _contains "$response" ''
96 fi
97 }
98
99 #################### Private functions below ##################################
100 #_acme-challenge.www.domain.com
101 #returns
102 # _sub_domain=_acme-challenge.www
103 # _domain=domain.com
104 # _domain_id=sdjkglgdfewsdfg
105 _get_root() {
106 domain=$1
107 i=2
108 p=1
109 while true; do
110 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
111 if [ -z "$h" ]; then
112 #not valid
113 return 1
114 fi
115
116 if ! _me_rest GET "name?domainname=$h"; then
117 return 1
118 fi
119
120 if _contains "$response" "\"name\":\"$h\""; then
121 _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*" | head -n 1 | cut -d : -f 2 | tr -d '}')
122 if [ "$_domain_id" ]; then
123 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
124 _domain="$h"
125 return 0
126 fi
127 return 1
128 fi
129 p=$i
130 i=$(_math "$i" + 1)
131 done
132 return 1
133 }
134
135 _me_rest() {
136 m=$1
137 ep="$2"
138 data="$3"
139 _debug "$ep"
140
141 cdate=$(LANG=C date -u +"%a, %d %b %Y %T %Z")
142 hmac=$(printf "%s" "$cdate" | _hmac sha1 "$(printf "%s" "$ME_Secret" | _hex_dump | tr -d " ")" hex)
143
144 export _H1="x-dnsme-apiKey: $ME_Key"
145 export _H2="x-dnsme-requestDate: $cdate"
146 export _H3="x-dnsme-hmac: $hmac"
147
148 if [ "$m" != "GET" ]; then
149 _debug data "$data"
150 response="$(_post "$data" "$ME_Api/$ep" "" "$m")"
151 else
152 response="$(_get "$ME_Api/$ep")"
153 fi
154
155 if [ "$?" != "0" ]; then
156 _err "error $ep"
157 return 1
158 fi
159 _debug2 response "$response"
160 return 0
161 }