]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_zone.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_zone.sh
CommitLineData
5048c6c2 1#!/usr/bin/env sh
2
3# Zone.ee dns API
4# https://help.zone.eu/kb/zoneid-api-v2/
5# required ZONE_Username and ZONE_Key
6
7ZONE_Api="https://api.zone.eu/v2"
8######## Public functions #####################
9
10#Usage: dns_zone_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
11dns_zone_add() {
12 fulldomain=$1
13 txtvalue=$2
14 _info "Using zone.ee dns api"
15 _debug fulldomain "$fulldomain"
16 _debug txtvalue "$txtvalue"
17 ZONE_Username="${ZONE_Username:-$(_readaccountconf_mutable ZONE_Username)}"
18 ZONE_Key="${ZONE_Key:-$(_readaccountconf_mutable ZONE_Key)}"
19 if [ -z "$ZONE_Username" ] || [ -z "$ZONE_Key" ]; then
20 ZONE_Username=""
21 ZONE_Key=""
22 _err "Zone api key and username must be present."
23 return 1
24 fi
25 _saveaccountconf_mutable ZONE_Username "$ZONE_Username"
26 _saveaccountconf_mutable ZONE_Key "$ZONE_Key"
27 _debug "First detect the root zone"
28 if ! _get_root "$fulldomain"; then
29 _err "invalid domain"
30 return 1
31 fi
32
33 _debug "Adding txt record"
34
35 if _zone_rest POST "dns/${_domain}/txt" "{\"name\": \"$fulldomain\", \"destination\": \"$txtvalue\"}"; then
36 if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then
37 _info "Added, OK"
38 return 0
39 else
40 _err "Adding txt record error."
41 return 1
42 fi
43 else
44 _err "Adding txt record error."
45 fi
46}
47
48#Usage: fulldomain txtvalue
49#Remove the txt record after validation.
50dns_zone_rm() {
51 fulldomain=$1
52 txtvalue=$2
53 _info "Using zone.ee dns api"
54 _debug fulldomain "$fulldomain"
55 _debug txtvalue "$txtvalue"
56 ZONE_Username="${ZONE_Username:-$(_readaccountconf_mutable ZONE_Username)}"
57 ZONE_Key="${ZONE_Key:-$(_readaccountconf_mutable ZONE_Key)}"
58 if [ -z "$ZONE_Username" ] || [ -z "$ZONE_Key" ]; then
59 ZONE_Username=""
60 ZONE_Key=""
61 _err "Zone api key and username must be present."
62 return 1
63 fi
64 _saveaccountconf_mutable ZONE_Username "$ZONE_Username"
65 _saveaccountconf_mutable ZONE_Key "$ZONE_Key"
66 _debug "First detect the root zone"
67 if ! _get_root "$fulldomain"; then
68 _err "invalid domain"
69 return 1
70 fi
71
72 _debug "Getting txt records"
73 _debug _domain "$_domain"
74
75 _zone_rest GET "dns/${_domain}/txt"
76
77 if printf "%s" "$response" | grep \"error\" >/dev/null; then
78 _err "Error"
79 return 1
80 fi
81
82 count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain\"" | wc -l)
83 _debug count "$count"
84 if [ "$count" = "0" ]; then
85 _info "Nothing to remove."
86 else
87 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\",\"resource_url\":\"[^\"]*\",\"name\":\"$fulldomain\"," | cut -d : -f2 | cut -d , -f1 | tr -d \" | _head_n 1)
88 if [ -z "$record_id" ]; then
89 _err "No id found to remove."
90 return 1
91 fi
92 if ! _zone_rest DELETE "dns/${_domain}/txt/$record_id"; then
93 _err "Record deleting error."
94 return 1
95 fi
96 _info "Record deleted"
97 return 0
98 fi
99
100}
101
102#################### Private functions below ##################################
103
104_zone_rest() {
105 m=$1
106 ep="$2"
107 data="$3"
108 _debug "$ep"
109
110 realm="$(printf "%s" "$ZONE_Username:$ZONE_Key" | _base64)"
111
112 export _H1="Authorization: Basic $realm"
113 export _H2="Content-Type: application/json"
114
115 if [ "$m" != "GET" ]; then
116 _debug data "$data"
117 response="$(_post "$data" "$ZONE_Api/$ep" "" "$m")"
118 else
119 response="$(_get "$ZONE_Api/$ep")"
120 fi
121
122 if [ "$?" != "0" ]; then
123 _err "error $ep"
124 return 1
125 fi
126 _debug2 response "$response"
127 return 0
128}
129
130_get_root() {
131 domain=$1
132 i=2
133 while true; do
134 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
135 _debug h "$h"
136 if [ -z "$h" ]; then
137 return 1
138 fi
b59b0f03 139 if ! _zone_rest GET "dns/$h"; then
5048c6c2 140 return 1
141 fi
b59b0f03 142 if _contains "$response" "\"identificator\":\"$h\"" >/dev/null; then
5048c6c2 143 _domain=$h
144 return 0
145 fi
146 i=$(_math "$i" + 1)
147 done
148 return 0
149}