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