]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_ultra.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_ultra.sh
1 #!/usr/bin/env sh
2
3 #
4 # ULTRA_USR="your_user_goes_here"
5 #
6 # ULTRA_PWD="some_password_goes_here"
7
8 ULTRA_API="https://api.ultradns.com/v3/"
9 ULTRA_AUTH_API="https://api.ultradns.com/v2/"
10
11 #Usage: add _acme-challenge.www.domain.com "some_long_string_of_characters_go_here_from_lets_encrypt"
12 dns_ultra_add() {
13 fulldomain=$1
14 txtvalue=$2
15 export txtvalue
16 ULTRA_USR="${ULTRA_USR:-$(_readaccountconf_mutable ULTRA_USR)}"
17 ULTRA_PWD="${ULTRA_PWD:-$(_readaccountconf_mutable ULTRA_PWD)}"
18 if [ -z "$ULTRA_USR" ] || [ -z "$ULTRA_PWD" ]; then
19 ULTRA_USR=""
20 ULTRA_PWD=""
21 _err "You didn't specify an UltraDNS username and password yet"
22 return 1
23 fi
24 # save the username and password to the account conf file.
25 _saveaccountconf_mutable ULTRA_USR "$ULTRA_USR"
26 _saveaccountconf_mutable ULTRA_PWD "$ULTRA_PWD"
27 _debug "First detect the root zone"
28 if ! _get_root "$fulldomain"; then
29 _err "invalid domain"
30 return 1
31 fi
32 _debug _domain_id "${_domain_id}"
33 _debug _sub_domain "${_sub_domain}"
34 _debug _domain "${_domain}"
35 _debug "Getting txt records"
36 _ultra_rest GET "zones/${_domain_id}/rrsets/TXT?q=value:${fulldomain}"
37 if printf "%s" "$response" | grep \"totalCount\" >/dev/null; then
38 _err "Error, it would appear that this record already exists. Please review existing TXT records for this domain."
39 return 1
40 fi
41
42 _info "Adding record"
43 if _ultra_rest POST "zones/$_domain_id/rrsets/TXT/${_sub_domain}" '{"ttl":300,"rdata":["'"${txtvalue}"'"]}'; then
44 if _contains "$response" "Successful"; then
45 _info "Added, OK"
46 return 0
47 elif _contains "$response" "Resource Record of type 16 with these attributes already exists"; then
48 _info "Already exists, OK"
49 return 0
50 else
51 _err "Add txt record error."
52 return 1
53 fi
54 fi
55 _err "Add txt record error."
56
57 }
58
59 dns_ultra_rm() {
60 fulldomain=$1
61 txtvalue=$2
62 export txtvalue
63 ULTRA_USR="${ULTRA_USR:-$(_readaccountconf_mutable ULTRA_USR)}"
64 ULTRA_PWD="${ULTRA_PWD:-$(_readaccountconf_mutable ULTRA_PWD)}"
65 if [ -z "$ULTRA_USR" ] || [ -z "$ULTRA_PWD" ]; then
66 ULTRA_USR=""
67 ULTRA_PWD=""
68 _err "You didn't specify an UltraDNS username and password yet"
69 return 1
70 fi
71
72 _debug "First detect the root zone"
73 if ! _get_root "$fulldomain"; then
74 _err "invalid domain"
75 return 1
76 fi
77 _debug _domain_id "${_domain_id}"
78 _debug _sub_domain "${_sub_domain}"
79 _debug _domain "${domain}"
80
81 _debug "Getting TXT records"
82 _ultra_rest GET "zones/${_domain_id}/rrsets?q=kind:RECORDS+owner:${_sub_domain}"
83
84 if ! printf "%s" "$response" | grep \"resultInfo\" >/dev/null; then
85 _err "There was an error in obtaining the resource records for ${_domain_id}"
86 return 1
87 fi
88
89 count=$(echo "$response" | _egrep_o "\"returnedCount\":[^,]*" | cut -d: -f2 | cut -d'}' -f1)
90 _debug count "${count}"
91 if [ "${count}" = "" ]; then
92 _info "Text record is not present, will not delete anything."
93 else
94 if ! _ultra_rest DELETE "zones/$_domain_id/rrsets/TXT/${_sub_domain}" '{"ttl":300,"rdata":["'"${txtvalue}"'"]}'; then
95 _err "Deleting the record did not succeed, please verify/check."
96 return 1
97 fi
98 _contains "$response" ""
99 fi
100
101 }
102
103 #################### Private functions below ##################################
104 #_acme-challenge.www.domain.com
105 #returns
106 # _sub_domain=_acme-challenge.www
107 # _domain=domain.com
108 # _domain_id=sdjkglgdfewsdfg
109 _get_root() {
110 domain=$1
111 i=2
112 p=1
113 while true; do
114 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
115 _debug h "$h"
116 _debug response "$response"
117 if [ -z "$h" ]; then
118 #not valid
119 return 1
120 fi
121 if ! _ultra_rest GET "zones"; then
122 return 1
123 fi
124 if _contains "${response}" "${h}." >/dev/null; then
125 _domain_id=$(echo "$response" | _egrep_o "${h}" | head -1)
126 if [ "$_domain_id" ]; then
127 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
128 _domain="${h}"
129 _debug sub_domain "${_sub_domain}"
130 _debug domain "${_domain}"
131 return 0
132 fi
133 return 1
134 fi
135 p=$i
136 i=$(_math "$i" + 1)
137 done
138 return 1
139 }
140
141 _ultra_rest() {
142 m=$1
143 ep="$2"
144 data="$3"
145 _debug "$ep"
146 if [ -z "$AUTH_TOKEN" ]; then
147 _ultra_login
148 fi
149 _debug TOKEN "$AUTH_TOKEN"
150
151 export _H1="Content-Type: application/json"
152 export _H2="Authorization: Bearer $AUTH_TOKEN"
153
154 if [ "$m" != "GET" ]; then
155 _debug data "$data"
156 response="$(_post "$data" "$ULTRA_API$ep" "" "$m")"
157 else
158 response="$(_get "$ULTRA_API$ep")"
159 fi
160 }
161
162 _ultra_login() {
163 export _H1=""
164 export _H2=""
165 AUTH_TOKEN=$(_post "grant_type=password&username=${ULTRA_USR}&password=${ULTRA_PWD}" "${ULTRA_AUTH_API}authorization/token" | cut -d, -f3 | cut -d\" -f4)
166 export AUTH_TOKEN
167 }