Skip to content

Commit bff1064

Browse files
committed
Add new dnsapi support for OpenProvider.eu using new REST API
1 parent 3d8b682 commit bff1064

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

dnsapi/dns_openprovider_rest.sh

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
_debug httpmethod "$httpmethod"
118+
_debug requestfullurl "$OPENPROVIDER_API_URL/$queryparameters"
119+
_debug queryparameters "$queryparameters"
120+
121+
if [ "$httpmethod" != "GET" ]; then
122+
_debug requestbody "$requestbody"
123+
124+
response="$(_post "$requestbody" "$OPENPROVIDER_API_URL/$queryparameters" "" "$httpmethod")"
125+
else
126+
response="$(_get "$OPENPROVIDER_API_URL/$queryparameters")"
127+
fi
128+
129+
if [ "$?" != "0" ]; then
130+
_err "No valid parameters supplied for Openprovider API: Error $queryparameters"
131+
return 1
132+
fi
133+
134+
_debug2 response "$response"
135+
136+
return 0
137+
}
138+
139+
_openprovider_rest_login() {
140+
export _H1="Content-Type: application/json"
141+
export _H2="Accept: application/json"
142+
143+
loginrequesturl="$OPENPROVIDER_API_URL/auth/login"
144+
loginrequestbody="{\"ip\":\"0.0.0.0\",\"password\":\"$OPENPROVIDER_REST_PASSWORD\",\"username\":\"$OPENPROVIDER_REST_USERNAME\"}"
145+
loginresponse="$(_post "$loginrequestbody" "$loginrequesturl" "" "POST")"
146+
147+
openproviderauthtoken="$(printf "%s\n" "$loginresponse" | _egrep_o '"token" *: *"[^"]*' | _head_n 1 | sed 's#^"token" *: *"##')"
148+
_debug openproviderauthtoken "$openproviderauthtoken"
149+
150+
export openproviderauthtoken
151+
}
152+
153+
#################### Private functions ##################################
154+
155+
# Usage: _get_dns_zone _acme-challenge.www.domain.com
156+
# Returns:
157+
# _domain_id=123456789
158+
# _domain_name=domain.com
159+
# _sub_domain=_acme-challenge.www
160+
_get_dns_zone() {
161+
domain=$1
162+
i=1
163+
p=1
164+
165+
while true; do
166+
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
167+
if [ -z "$h" ]; then
168+
# Empty value not allowed
169+
return 1
170+
fi
171+
172+
if ! _openprovider_rest GET "dns/zones/$h" ""; then
173+
return 1
174+
fi
175+
176+
if _contains "$response" "\"name\":\"$h\""; then
177+
_domain_id="$(printf "%s\n" "$response" | _egrep_o '"id" *: *[^,]*' | _head_n 1 | sed 's#^"id" *: *##')"
178+
_debug _domain_id "$_domain_id"
179+
180+
_domain_name="$h"
181+
_debug _domain_name "$_domain_name"
182+
183+
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
184+
_debug _sub_domain "$_sub_domain"
185+
return 0
186+
fi
187+
188+
p=$i
189+
i=$(_math "$i" + 1)
190+
done
191+
192+
return 1
193+
}

0 commit comments

Comments
 (0)