]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_udr.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_udr.sh
1 #!/usr/bin/env sh
2
3 # united-domains Reselling (https://www.ud-reselling.com/) DNS API
4 # Author: Andreas Scherer (https://github.com/andischerer)
5 # Created: 2021-02-01
6 #
7 # Set the environment variables as below:
8 #
9 # export UDR_USER="your_username_goes_here"
10 # export UDR_PASS="some_password_goes_here"
11 #
12
13 UDR_API="https://api.domainreselling.de/api/call.cgi"
14 UDR_TTL="30"
15
16 ######## Public functions #####################
17
18 #Usage: add _acme-challenge.www.domain.com "some_long_string_of_characters_go_here_from_lets_encrypt"
19 dns_udr_add() {
20 fulldomain=$1
21 txtvalue=$2
22
23 UDR_USER="${UDR_USER:-$(_readaccountconf_mutable UDR_USER)}"
24 UDR_PASS="${UDR_PASS:-$(_readaccountconf_mutable UDR_PASS)}"
25 if [ -z "$UDR_USER" ] || [ -z "$UDR_PASS" ]; then
26 UDR_USER=""
27 UDR_PASS=""
28 _err "You didn't specify an UD-Reselling username and password yet"
29 return 1
30 fi
31 # save the username and password to the account conf file.
32 _saveaccountconf_mutable UDR_USER "$UDR_USER"
33 _saveaccountconf_mutable UDR_PASS "$UDR_PASS"
34 _debug "First detect the root zone"
35 if ! _get_root "$fulldomain"; then
36 _err "invalid domain"
37 return 1
38 fi
39
40 _debug _dnszone "${_dnszone}"
41
42 _debug "Getting txt records"
43 if ! _udr_rest "QueryDNSZoneRRList" "dnszone=${_dnszone}"; then
44 return 1
45 fi
46
47 rr="${fulldomain}. ${UDR_TTL} IN TXT ${txtvalue}"
48 _debug resource_record "${rr}"
49 if _contains "$response" "$rr" >/dev/null; then
50 _err "Error, it would appear that this record already exists. Please review existing TXT records for this domain."
51 return 1
52 fi
53
54 _info "Adding record"
55 if ! _udr_rest "UpdateDNSZone" "dnszone=${_dnszone}&addrr0=${rr}"; then
56 _err "Adding the record did not succeed, please verify/check."
57 return 1
58 fi
59
60 _info "Added, OK"
61 return 0
62 }
63
64 dns_udr_rm() {
65 fulldomain=$1
66 txtvalue=$2
67
68 UDR_USER="${UDR_USER:-$(_readaccountconf_mutable UDR_USER)}"
69 UDR_PASS="${UDR_PASS:-$(_readaccountconf_mutable UDR_PASS)}"
70 if [ -z "$UDR_USER" ] || [ -z "$UDR_PASS" ]; then
71 UDR_USER=""
72 UDR_PASS=""
73 _err "You didn't specify an UD-Reselling username and password yet"
74 return 1
75 fi
76
77 _debug "First detect the root zone"
78 if ! _get_root "$fulldomain"; then
79 _err "invalid domain"
80 return 1
81 fi
82 _debug _dnszone "${_dnszone}"
83
84 _debug "Getting txt records"
85 if ! _udr_rest "QueryDNSZoneRRList" "dnszone=${_dnszone}"; then
86 return 1
87 fi
88
89 rr="${fulldomain}. ${UDR_TTL} IN TXT ${txtvalue}"
90 _debug resource_record "${rr}"
91 if _contains "$response" "$rr" >/dev/null; then
92 if ! _udr_rest "UpdateDNSZone" "dnszone=${_dnszone}&delrr0=${rr}"; then
93 _err "Deleting the record did not succeed, please verify/check."
94 return 1
95 fi
96 _info "Removed, OK"
97 return 0
98 else
99 _info "Text record is not present, will not delete anything."
100 return 0
101 fi
102 }
103
104 #################### Private functions below ##################################
105 #_acme-challenge.www.domain.com
106 #returns
107 # _sub_domain=_acme-challenge.www
108 # _domain=domain.com
109 _get_root() {
110 domain=$1
111 i=1
112
113 if ! _udr_rest "QueryDNSZoneList" ""; then
114 return 1
115 fi
116
117 while true; do
118 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
119 _debug h "$h"
120
121 if [ -z "$h" ]; then
122 #not valid
123 return 1
124 fi
125
126 if _contains "${response}" "${h}." >/dev/null; then
127 _dnszone=$(echo "$response" | _egrep_o "${h}")
128 if [ "$_dnszone" ]; then
129 return 0
130 fi
131 return 1
132 fi
133 i=$(_math "$i" + 1)
134 done
135 return 1
136 }
137
138 _udr_rest() {
139 if [ -n "$2" ]; then
140 data="command=$1&$2"
141 else
142 data="command=$1"
143 fi
144
145 _debug data "${data}"
146 response="$(_post "${data}" "${UDR_API}?s_login=${UDR_USER}&s_pw=${UDR_PASS}" "" "POST")"
147
148 _code=$(echo "$response" | _egrep_o "code = ([0-9]+)" | _head_n 1 | cut -d = -f 2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
149 _description=$(echo "$response" | _egrep_o "description = .*" | _head_n 1 | cut -d = -f 2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
150
151 _debug response_code "$_code"
152 _debug response_description "$_description"
153
154 if [ ! "$_code" = "200" ]; then
155 _err "DNS-API-Error: $_description"
156 return 1
157 fi
158
159 return 0
160 }