]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_desec.sh
fix https://github.com/Neilpang/acme.sh/issues/2417
[mirror_acme.sh.git] / dnsapi / dns_desec.sh
CommitLineData
3bb97b81
JQ
1#!/usr/bin/env sh
2#
3# deSEC.io Domain API
4#
5# Author: Zheng Qian
6#
7# deSEC API doc
8# https://desec.readthedocs.io/en/latest/
9
10REST_API="https://desec.io/api/v1/domains"
11
12######## Public functions #####################
13
14#Usage: dns_desec_add _acme-challenge.foobar.dedyn.io "d41d8cd98f00b204e9800998ecf8427e"
15dns_desec_add() {
16 fulldomain=$1
17 txtvalue=$2
18 _info "Using desec.io api"
19 _debug fulldomain "$fulldomain"
20 _debug txtvalue "$txtvalue"
21
22 DEDYN_TOKEN="${DEDYN_TOKEN:-$(_readaccountconf_mutable DEDYN_TOKEN)}"
23 DEDYN_NAME="${DEDYN_NAME:-$(_readaccountconf_mutable DEDYN_NAME)}"
24
25 if [ -z "$DEDYN_TOKEN" ] || [ -z "$DEDYN_NAME" ]; then
26 DEDYN_TOKEN=""
27 DEDYN_NAME=""
28 _err "You don't specify DEDYN_TOKEN and DEDYN_NAME yet."
29 _err "Please create you key and try again."
30 _err "e.g."
31 _err "export DEDYN_TOKEN=d41d8cd98f00b204e9800998ecf8427e"
32 _err "export DEDYN_NAME=foobar.dedyn.io"
33 return 1
34 fi
35 #save the api token and name to the account conf file.
36 _saveaccountconf_mutable DEDYN_TOKEN "$DEDYN_TOKEN"
37 _saveaccountconf_mutable DEDYN_NAME "$DEDYN_NAME"
38
39 _debug "First detect the root zone"
40 if ! _get_root "$fulldomain" "$REST_API/"; then
41 _err "invalid domain"
42 return 1
43 fi
44 _debug _sub_domain "$_sub_domain"
45 _debug _domain "$_domain"
46
47 # Get existing TXT record
48 _debug "Getting txt records"
49 txtvalues="\"\\\"$txtvalue\\\"\""
50 _desec_rest GET "$REST_API/$DEDYN_NAME/rrsets/$_sub_domain/TXT/"
51
52 if [ "$_code" = "200" ]; then
53 oldtxtvalues="$(echo "$response" | _egrep_o "\"records\":\\[\"\\S*\"\\]" | cut -d : -f 2 | tr -d "[]\\\\\"" | sed "s/,/ /g")"
54 _debug "existing TXT found"
55 _debug oldtxtvalues "$oldtxtvalues"
56 if [ -n "$oldtxtvalues" ]; then
57 for oldtxtvalue in $oldtxtvalues; do
58 txtvalues="$txtvalues, \"\\\"$oldtxtvalue\\\"\""
59 done
60 fi
61 fi
62 _debug txtvalues "$txtvalues"
63 _info "Adding record"
64 body="[{\"subname\":\"$_sub_domain\", \"type\":\"TXT\", \"records\":[$txtvalues], \"ttl\":60}]"
65
66 if _desec_rest PUT "$REST_API/$DEDYN_NAME/rrsets/" "$body"; then
67 if _contains "$response" "$txtvalue"; then
68 _info "Added, OK"
69 return 0
70 else
71 _err "Add txt record error."
72 return 1
73 fi
74 fi
75
76 _err "Add txt record error."
77 return 1
78}
79
80#Usage: fulldomain txtvalue
81#Remove the txt record after validation.
82dns_desec_rm() {
83 fulldomain=$1
84 txtvalue=$2
85 _info "Using desec.io api"
86 _debug fulldomain "$fulldomain"
87 _debug txtvalue "$txtvalue"
88
89 DEDYN_TOKEN="${DEDYN_TOKEN:-$(_readaccountconf_mutable DEDYN_TOKEN)}"
90 DEDYN_NAME="${DEDYN_NAME:-$(_readaccountconf_mutable DEDYN_NAME)}"
91
92 if [ -z "$DEDYN_TOKEN" ] || [ -z "$DEDYN_NAME" ]; then
93 DEDYN_TOKEN=""
94 DEDYN_NAME=""
95 _err "You don't specify DEDYN_TOKEN and DEDYN_NAME yet."
96 _err "Please create you key and try again."
97 _err "e.g."
98 _err "export DEDYN_TOKEN=d41d8cd98f00b204e9800998ecf8427e"
99 _err "export DEDYN_NAME=foobar.dedyn.io"
100 return 1
101 fi
102
103 _debug "First detect the root zone"
104 if ! _get_root "$fulldomain" "$REST_API/"; then
105 _err "invalid domain"
106 return 1
107 fi
108
109 _debug _sub_domain "$_sub_domain"
110 _debug _domain "$_domain"
111
112 # Get existing TXT record
113 _debug "Getting txt records"
114 txtvalues=""
115 _desec_rest GET "$REST_API/$DEDYN_NAME/rrsets/$_sub_domain/TXT/"
116
117 if [ "$_code" = "200" ]; then
118 oldtxtvalues="$(echo "$response" | _egrep_o "\"records\":\\[\"\\S*\"\\]" | cut -d : -f 2 | tr -d "[]\\\\\"" | sed "s/,/ /g")"
119 _debug "existing TXT found"
120 _debug oldtxtvalues "$oldtxtvalues"
121 if [ -n "$oldtxtvalues" ]; then
122 for oldtxtvalue in $oldtxtvalues; do
123 if [ "$txtvalue" != "$oldtxtvalue" ]; then
124 txtvalues="$txtvalues, \"\\\"$oldtxtvalue\\\"\""
125 fi
126 done
127 fi
128 fi
129 txtvalues="$(echo "$txtvalues" | cut -c3-)"
130 _debug txtvalues "$txtvalues"
131
132 _info "Deleting record"
133 body="[{\"subname\":\"$_sub_domain\", \"type\":\"TXT\", \"records\":[$txtvalues], \"ttl\":60}]"
134 _desec_rest PUT "$REST_API/$DEDYN_NAME/rrsets/" "$body"
135 if [ "$_code" = "200" ]; then
136 _info "Deleted, OK"
137 return 0
138 fi
139
140 _err "Delete txt record error."
141 return 1
142}
143
144#################### Private functions below ##################################
145
146_desec_rest() {
147 m="$1"
148 ep="$2"
149 data="$3"
150
151 export _H1="Authorization: Token $DEDYN_TOKEN"
152 export _H2="Accept: application/json"
153 export _H3="Content-Type: application/json"
154
155 if [ "$m" != "GET" ]; then
156 _secure_debug2 data "$data"
157 response="$(_post "$data" "$ep" "" "$m")"
158 else
159 response="$(_get "$ep")"
160 fi
161 _ret="$?"
162 _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
163 _debug "http response code $_code"
164 _secure_debug2 response "$response"
165 if [ "$_ret" != "0" ]; then
166 _err "error $ep"
167 return 1
168 fi
169
170 response="$(printf "%s" "$response" | _normalizeJson)"
171 return 0
172}
173
174#_acme-challenge.www.domain.com
175#returns
176# _sub_domain=_acme-challenge.www
177# _domain=domain.com
178_get_root() {
179 domain="$1"
180 ep="$2"
181 i=2
182 p=1
183 while true; do
184 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
185 _debug h "$h"
186 if [ -z "$h" ]; then
187 #not valid
188 return 1
189 fi
190
191 if ! _desec_rest GET "$ep"; then
192 return 1
193 fi
194
195 if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
196 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
197 _domain=$h
198 return 0
199 fi
200 p=$i
201 i=$(_math "$i" + 1)
202 done
203 return 1
204}