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