8 # https://desec.readthedocs.io/en/latest/
10 REST_API
="https://desec.io/api/v1/domains"
12 ######## Public functions #####################
14 #Usage: dns_desec_add _acme-challenge.foobar.dedyn.io "d41d8cd98f00b204e9800998ecf8427e"
18 _info
"Using desec.io api"
19 _debug fulldomain
"$fulldomain"
20 _debug txtvalue
"$txtvalue"
22 DEDYN_TOKEN
="${DEDYN_TOKEN:-$(_readaccountconf_mutable DEDYN_TOKEN)}"
24 if [ -z "$DEDYN_TOKEN" ]; then
26 _err
"You did not specify DEDYN_TOKEN yet."
27 _err
"Please create your key and try again."
29 _err
"export DEDYN_TOKEN=d41d8cd98f00b204e9800998ecf8427e"
32 #save the api token to the account conf file.
33 _saveaccountconf_mutable DEDYN_TOKEN
"$DEDYN_TOKEN"
35 _debug
"First detect the root zone"
36 if ! _get_root
"$fulldomain" "$REST_API/"; then
40 _debug _sub_domain
"$_sub_domain"
41 _debug _domain
"$_domain"
43 # Get existing TXT record
44 _debug
"Getting txt records"
45 txtvalues
="\"\\\"$txtvalue\\\"\""
46 _desec_rest GET
"$REST_API/$_domain/rrsets/$_sub_domain/TXT/"
48 if [ "$_code" = "200" ]; then
49 oldtxtvalues
="$(echo "$response" | _egrep_o "\"records
\":\\[\"\\S
*\"\\]" | cut -d : -f 2 | tr -d "[]\\\\\"" | sed "s
/,/ /g
")"
50 _debug
"existing TXT found"
51 _debug oldtxtvalues
"$oldtxtvalues"
52 if [ -n "$oldtxtvalues" ]; then
53 for oldtxtvalue
in $oldtxtvalues; do
54 txtvalues
="$txtvalues, \"\\\"$oldtxtvalue\\\"\""
58 _debug txtvalues
"$txtvalues"
60 body
="[{\"subname\":\"$_sub_domain\", \"type\":\"TXT\", \"records\":[$txtvalues], \"ttl\":3600}]"
62 if _desec_rest PUT
"$REST_API/$_domain/rrsets/" "$body"; then
63 if _contains
"$response" "$txtvalue"; then
67 _err
"Add txt record error."
72 _err
"Add txt record error."
76 #Usage: fulldomain txtvalue
77 #Remove the txt record after validation.
81 _info
"Using desec.io api"
82 _debug fulldomain
"$fulldomain"
83 _debug txtvalue
"$txtvalue"
85 DEDYN_TOKEN
="${DEDYN_TOKEN:-$(_readaccountconf_mutable DEDYN_TOKEN)}"
87 if [ -z "$DEDYN_TOKEN" ]; then
89 _err
"You did not specify DEDYN_TOKEN yet."
90 _err
"Please create your key and try again."
92 _err
"export DEDYN_TOKEN=d41d8cd98f00b204e9800998ecf8427e"
96 _debug
"First detect the root zone"
97 if ! _get_root
"$fulldomain" "$REST_API/"; then
102 _debug _sub_domain
"$_sub_domain"
103 _debug _domain
"$_domain"
105 # Get existing TXT record
106 _debug
"Getting txt records"
108 _desec_rest GET
"$REST_API/$_domain/rrsets/$_sub_domain/TXT/"
110 if [ "$_code" = "200" ]; then
111 oldtxtvalues
="$(echo "$response" | _egrep_o "\"records
\":\\[\"\\S
*\"\\]" | cut -d : -f 2 | tr -d "[]\\\\\"" | sed "s
/,/ /g
")"
112 _debug
"existing TXT found"
113 _debug oldtxtvalues
"$oldtxtvalues"
114 if [ -n "$oldtxtvalues" ]; then
115 for oldtxtvalue
in $oldtxtvalues; do
116 if [ "$txtvalue" != "$oldtxtvalue" ]; then
117 txtvalues
="$txtvalues, \"\\\"$oldtxtvalue\\\"\""
122 txtvalues
="$(echo "$txtvalues" | cut -c3-)"
123 _debug txtvalues
"$txtvalues"
125 _info
"Deleting record"
126 body
="[{\"subname\":\"$_sub_domain\", \"type\":\"TXT\", \"records\":[$txtvalues], \"ttl\":3600}]"
127 _desec_rest PUT
"$REST_API/$_domain/rrsets/" "$body"
128 if [ "$_code" = "200" ]; then
133 _err
"Delete txt record error."
137 #################### Private functions below ##################################
144 export _H1
="Authorization: Token $DEDYN_TOKEN"
145 export _H2
="Accept: application/json"
146 export _H3
="Content-Type: application/json"
148 if [ "$m" != "GET" ]; then
149 _secure_debug2 data
"$data"
150 response
="$(_post "$data" "$ep" "" "$m")"
152 response
="$(_get "$ep")"
155 _code
="$(grep "^HTTP
" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r
\\n
")"
156 _debug
"http response code $_code"
157 _secure_debug2 response
"$response"
158 if [ "$_ret" != "0" ]; then
163 response
="$(printf "%s
" "$response" | _normalizeJson)"
167 #_acme-challenge.www.domain.com
169 # _sub_domain=_acme-challenge.www
177 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
184 if ! _desec_rest GET
"$ep"; then
188 if _contains
"$response" "\"name\":\"$h\"" >/dev
/null
; then
189 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)