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)}"
23 DEDYN_NAME
="${DEDYN_NAME:-$(_readaccountconf_mutable DEDYN_NAME)}"
25 if [ -z "$DEDYN_TOKEN" ] ||
[ -z "$DEDYN_NAME" ]; then
28 _err
"You don't specify DEDYN_TOKEN and DEDYN_NAME yet."
29 _err
"Please create you key and try again."
31 _err
"export DEDYN_TOKEN=d41d8cd98f00b204e9800998ecf8427e"
32 _err
"export DEDYN_NAME=foobar.dedyn.io"
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"
39 _debug
"First detect the root zone"
40 if ! _get_root
"$fulldomain" "$REST_API/"; then
44 _debug _sub_domain
"$_sub_domain"
45 _debug _domain
"$_domain"
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/"
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\\\"\""
62 _debug txtvalues
"$txtvalues"
64 body
="[{\"subname\":\"$_sub_domain\", \"type\":\"TXT\", \"records\":[$txtvalues], \"ttl\":60}]"
66 if _desec_rest PUT
"$REST_API/$DEDYN_NAME/rrsets/" "$body"; then
67 if _contains
"$response" "$txtvalue"; then
71 _err
"Add txt record error."
76 _err
"Add txt record error."
80 #Usage: fulldomain txtvalue
81 #Remove the txt record after validation.
85 _info
"Using desec.io api"
86 _debug fulldomain
"$fulldomain"
87 _debug txtvalue
"$txtvalue"
89 DEDYN_TOKEN
="${DEDYN_TOKEN:-$(_readaccountconf_mutable DEDYN_TOKEN)}"
90 DEDYN_NAME
="${DEDYN_NAME:-$(_readaccountconf_mutable DEDYN_NAME)}"
92 if [ -z "$DEDYN_TOKEN" ] ||
[ -z "$DEDYN_NAME" ]; then
95 _err
"You don't specify DEDYN_TOKEN and DEDYN_NAME yet."
96 _err
"Please create you key and try again."
98 _err
"export DEDYN_TOKEN=d41d8cd98f00b204e9800998ecf8427e"
99 _err
"export DEDYN_NAME=foobar.dedyn.io"
103 _debug
"First detect the root zone"
104 if ! _get_root
"$fulldomain" "$REST_API/"; then
105 _err
"invalid domain"
109 _debug _sub_domain
"$_sub_domain"
110 _debug _domain
"$_domain"
112 # Get existing TXT record
113 _debug
"Getting txt records"
115 _desec_rest GET
"$REST_API/$DEDYN_NAME/rrsets/$_sub_domain/TXT/"
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\\\"\""
129 txtvalues
="$(echo "$txtvalues" | cut -c3-)"
130 _debug txtvalues
"$txtvalues"
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
140 _err
"Delete txt record error."
144 #################### Private functions below ##################################
151 export _H1
="Authorization: Token $DEDYN_TOKEN"
152 export _H2
="Accept: application/json"
153 export _H3
="Content-Type: application/json"
155 if [ "$m" != "GET" ]; then
156 _secure_debug2 data
"$data"
157 response
="$(_post "$data" "$ep" "" "$m")"
159 response
="$(_get "$ep")"
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
170 response
="$(printf "%s
" "$response" | _normalizeJson)"
174 #_acme-challenge.www.domain.com
176 # _sub_domain=_acme-challenge.www
184 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
191 if ! _desec_rest GET
"$ep"; then
195 if _contains
"$response" "\"name\":\"$h\"" >/dev
/null
; then
196 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)