Skip to content

Commit 4cdbdf2

Browse files
authored
Merge pull request #6391 from Lambiek12/dev
New dnsapi support for OpenProvider.eu using REST API
2 parents ec5ecab + 09d326d commit 4cdbdf2

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

dnsapi/dns_openprovider_rest.sh

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#!/usr/bin/env sh
2+
# shellcheck disable=SC2034
3+
dns_openprovider_rest_info='OpenProvider (REST)
4+
Domains: OpenProvider.com
5+
Site: OpenProvider.eu
6+
Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_openprovider_rest
7+
Options:
8+
OPENPROVIDER_REST_USERNAME Openprovider Account Username
9+
OPENPROVIDER_REST_PASSWORD Openprovider Account Password
10+
Issues: github.com/acmesh-official/acme.sh/issues/6122
11+
Author: Lambiek12
12+
'
13+
14+
OPENPROVIDER_API_URL="https://api.openprovider.eu/v1beta"
15+
16+
######## Public functions #####################
17+
18+
# Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
19+
# Used to add txt record
20+
dns_openprovider_rest_add() {
21+
fulldomain=$1
22+
txtvalue=$2
23+
24+
_openprovider_prepare_credentials || return 1
25+
26+
_debug "Try fetch OpenProvider DNS zone details"
27+
if ! _get_dns_zone "$fulldomain"; then
28+
_err "DNS zone not found within configured OpenProvider account."
29+
return 1
30+
fi
31+
32+
if [ -n "$_domain_id" ]; then
33+
addzonerecordrequestparameters="dns/zones/$_domain_name"
34+
addzonerecordrequestbody="{\"id\":$_domain_id,\"name\":\"$_domain_name\",\"records\":{\"add\":[{\"name\":\"$_sub_domain\",\"ttl\":900,\"type\":\"TXT\",\"value\":\"$txtvalue\"}]}}"
35+
36+
if _openprovider_rest PUT "$addzonerecordrequestparameters" "$addzonerecordrequestbody"; then
37+
if _contains "$response" "\"success\":true"; then
38+
return 0
39+
elif _contains "$response" "\"Duplicate record\""; then
40+
_debug "Record already existed"
41+
return 0
42+
else
43+
_err "Adding TXT record failed due to errors."
44+
return 1
45+
fi
46+
fi
47+
fi
48+
49+
_err "Adding TXT record failed due to errors."
50+
return 1
51+
}
52+
53+
# Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
54+
# Used to remove the txt record after validation
55+
dns_openprovider_rest_rm() {
56+
fulldomain=$1
57+
txtvalue=$2
58+
59+
_openprovider_prepare_credentials || return 1
60+
61+
_debug "Try fetch OpenProvider DNS zone details"
62+
if ! _get_dns_zone "$fulldomain"; then
63+
_err "DNS zone not found within configured OpenProvider account."
64+
return 1
65+
fi
66+
67+
if [ -n "$_domain_id" ]; then
68+
removezonerecordrequestparameters="dns/zones/$_domain_name"
69+
removezonerecordrequestbody="{\"id\":$_domain_id,\"name\":\"$_domain_name\",\"records\":{\"remove\":[{\"name\":\"$_sub_domain\",\"ttl\":900,\"type\":\"TXT\",\"value\":\"\\\"$txtvalue\\\"\"}]}}"
70+
71+
if _openprovider_rest PUT "$removezonerecordrequestparameters" "$removezonerecordrequestbody"; then
72+
if _contains "$response" "\"success\":true"; then
73+
return 0
74+
else
75+
_err "Removing TXT record failed due to errors."
76+
return 1
77+
fi
78+
fi
79+
fi
80+
81+
_err "Removing TXT record failed due to errors."
82+
return 1
83+
}
84+
85+
#################### OpenProvider API common functions ####################
86+
_openprovider_prepare_credentials() {
87+
OPENPROVIDER_REST_USERNAME="${OPENPROVIDER_REST_USERNAME:-$(_readaccountconf_mutable OPENPROVIDER_REST_USERNAME)}"
88+
OPENPROVIDER_REST_PASSWORD="${OPENPROVIDER_REST_PASSWORD:-$(_readaccountconf_mutable OPENPROVIDER_REST_PASSWORD)}"
89+
90+
if [ -z "$OPENPROVIDER_REST_USERNAME" ] || [ -z "$OPENPROVIDER_REST_PASSWORD" ]; then
91+
OPENPROVIDER_REST_USERNAME=""
92+
OPENPROVIDER_REST_PASSWORD=""
93+
_err "You didn't specify the Openprovider username or password yet."
94+
return 1
95+
fi
96+
97+
#save the credentials to the account conf file.
98+
_saveaccountconf_mutable OPENPROVIDER_REST_USERNAME "$OPENPROVIDER_REST_USERNAME"
99+
_saveaccountconf_mutable OPENPROVIDER_REST_PASSWORD "$OPENPROVIDER_REST_PASSWORD"
100+
}
101+
102+
_openprovider_rest() {
103+
httpmethod=$1
104+
queryparameters=$2
105+
requestbody=$3
106+
107+
_openprovider_rest_login
108+
if [ -z "$openproviderauthtoken" ]; then
109+
_err "Unable to fetch authentication token from Openprovider API."
110+
return 1
111+
fi
112+
113+
export _H1="Content-Type: application/json"
114+
export _H2="Accept: application/json"
115+
export _H3="Authorization: Bearer $openproviderauthtoken"
116+
117+
if [ "$httpmethod" != "GET" ]; then
118+
response="$(_post "$requestbody" "$OPENPROVIDER_API_URL/$queryparameters" "" "$httpmethod")"
119+
else
120+
response="$(_get "$OPENPROVIDER_API_URL/$queryparameters")"
121+
fi
122+
123+
if [ "$?" != "0" ]; then
124+
_err "No valid parameters supplied for Openprovider API: Error $queryparameters"
125+
return 1
126+
fi
127+
128+
_debug2 response "$response"
129+
130+
return 0
131+
}
132+
133+
_openprovider_rest_login() {
134+
export _H1="Content-Type: application/json"
135+
export _H2="Accept: application/json"
136+
137+
loginrequesturl="$OPENPROVIDER_API_URL/auth/login"
138+
loginrequestbody="{\"ip\":\"0.0.0.0\",\"password\":\"$OPENPROVIDER_REST_PASSWORD\",\"username\":\"$OPENPROVIDER_REST_USERNAME\"}"
139+
loginresponse="$(_post "$loginrequestbody" "$loginrequesturl" "" "POST")"
140+
141+
openproviderauthtoken="$(printf "%s\n" "$loginresponse" | _egrep_o '"token" *: *"[^"]*' | _head_n 1 | sed 's#^"token" *: *"##')"
142+
143+
export openproviderauthtoken
144+
}
145+
146+
#################### Private functions ##################################
147+
148+
# Usage: _get_dns_zone _acme-challenge.www.domain.com
149+
# Returns:
150+
# _domain_id=123456789
151+
# _domain_name=domain.com
152+
# _sub_domain=_acme-challenge.www
153+
_get_dns_zone() {
154+
domain=$1
155+
i=1
156+
p=1
157+
158+
while true; do
159+
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
160+
if [ -z "$h" ]; then
161+
# Empty value not allowed
162+
return 1
163+
fi
164+
165+
if ! _openprovider_rest GET "dns/zones/$h" ""; then
166+
return 1
167+
fi
168+
169+
if _contains "$response" "\"name\":\"$h\""; then
170+
_domain_id="$(printf "%s\n" "$response" | _egrep_o '"id" *: *[^,]*' | _head_n 1 | sed 's#^"id" *: *##')"
171+
_debug _domain_id "$_domain_id"
172+
173+
_domain_name="$h"
174+
_debug _domain_name "$_domain_name"
175+
176+
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
177+
_debug _sub_domain "$_sub_domain"
178+
return 0
179+
fi
180+
181+
p=$i
182+
i=$(_math "$i" + 1)
183+
done
184+
185+
return 1
186+
}

0 commit comments

Comments
 (0)