3 #Rcode0 API Integration
4 #https://my.rcodezero.at/api-doc
6 # log into https://my.rcodezero.at/enableapi and get your ACME API Token (the ACME API token has limited
7 # access to the REST calls needed for acme.sh only)
9 #RCODE0_URL="https://my.rcodezero.at"
10 #RCODE0_API_TOKEN="0123456789ABCDEF"
13 DEFAULT_RCODE0_URL
="https://my.rcodezero.at"
16 ######## Public functions #####################
17 #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
24 RCODE0_API_TOKEN
="${RCODE0_API_TOKEN:-$(_readaccountconf_mutable RCODE0_API_TOKEN)}"
25 RCODE0_URL
="${RCODE0_URL:-$(_readaccountconf_mutable RCODE0_URL)}"
26 RCODE0_TTL
="${RCODE0_TTL:-$(_readaccountconf_mutable RCODE0_TTL)}"
28 if [ -z "$RCODE0_URL" ]; then
29 RCODE0_URL
="$DEFAULT_RCODE0_URL"
32 if [ -z "$RCODE0_API_TOKEN" ]; then
34 _err
"Missing Rcode0 ACME API Token."
35 _err
"Please login and create your token at httsp://my.rcodezero.at/enableapi and try again."
39 if [ -z "$RCODE0_TTL" ]; then
40 RCODE0_TTL
="$DEFAULT_RCODE0_TTL"
43 #save the token to the account conf file.
44 _saveaccountconf_mutable RCODE0_API_TOKEN
"$RCODE0_API_TOKEN"
46 if [ "$RCODE0_URL" != "$DEFAULT_RCODE0_URL" ]; then
47 _saveaccountconf_mutable RCODE0_URL
"$RCODE0_URL"
50 if [ "$RCODE0_TTL" != "$DEFAULT_RCODE0_TTL" ]; then
51 _saveaccountconf_mutable RCODE0_TTL
"$RCODE0_TTL"
54 _debug
"Detect root zone"
55 if ! _get_root
"$fulldomain"; then
56 _err
"No 'MASTER' zone for $fulldomain found at RcodeZero Anycast."
59 _debug _domain
"$_domain"
61 _debug
"Adding record"
64 _build_record_string
"$txtvalue"
65 _list_existingchallenges
66 for oldchallenge
in $_existing_challenges; do
67 _build_record_string
"$oldchallenge"
70 _debug
"Challenges: $_existing_challenges"
72 if [ -z "$_existing_challenges" ]; then
73 if ! _rcode0_rest
"PATCH" "/api/v1/acme/zones/$_domain/rrsets" "[{\"changetype\": \"add\", \"name\": \"$fulldomain.\", \"type\": \"TXT\", \"ttl\": $RCODE0_TTL, \"records\": [$_record_string]}]"; then
74 _err
"Add txt record error."
78 # try update in case a records exists (need for wildcard certs)
79 if ! _rcode0_rest
"PATCH" "/api/v1/acme/zones/$_domain/rrsets" "[{\"changetype\": \"update\", \"name\": \"$fulldomain.\", \"type\": \"TXT\", \"ttl\": $RCODE0_TTL, \"records\": [$_record_string]}]"; then
80 _err
"Set txt record error."
93 RCODE0_API_TOKEN
="${RCODE0_API_TOKEN:-$(_readaccountconf_mutable RCODE0_API_TOKEN)}"
94 RCODE0_URL
="${RCODE0_URL:-$(_readaccountconf_mutable RCODE0_URL)}"
95 RCODE0_TTL
="${RCODE0_TTL:-$(_readaccountconf_mutable RCODE0_TTL)}"
97 if [ -z "$RCODE0_URL" ]; then
98 RCODE0_URL
="$DEFAULT_RCODE0_URL"
101 if [ -z "$RCODE0_API_TOKEN" ]; then
103 _err
"Missing Rcode0 API Token."
104 _err
"Please login and create your token at httsp://my.rcodezero.at/enableapi and try again."
108 #save the api addr and key to the account conf file.
109 _saveaccountconf_mutable RCODE0_URL
"$RCODE0_URL"
110 _saveaccountconf_mutable RCODE0_API_TOKEN
"$RCODE0_API_TOKEN"
112 if [ "$RCODE0_TTL" != "$DEFAULT_RCODE0_TTL" ]; then
113 _saveaccountconf_mutable RCODE0_TTL
"$RCODE0_TTL"
116 if [ -z "$RCODE0_TTL" ]; then
117 RCODE0_TTL
="$DEFAULT_RCODE0_TTL"
120 _debug
"Detect root zone"
121 if ! _get_root
"$fulldomain"; then
122 _err
"invalid domain"
126 _debug
"Remove record"
128 #Enumerate existing acme challenges
129 _list_existingchallenges
131 if _contains
"$_existing_challenges" "$txtvalue"; then
132 #Delete all challenges (PowerDNS API does not allow to delete content)
133 if ! _rcode0_rest
"PATCH" "/api/v1/acme/zones/$_domain/rrsets" "[{\"changetype\": \"delete\", \"name\": \"$fulldomain.\", \"type\": \"TXT\"}]"; then
134 _err
"Delete txt record error."
138 #If the only existing challenge was the challenge to delete: nothing to do
139 if ! [ "$_existing_challenges" = "$txtvalue" ]; then
140 for oldchallenge
in $_existing_challenges; do
141 #Build up the challenges to re-add, ommitting the one what should be deleted
142 if ! [ "$oldchallenge" = "$txtvalue" ]; then
143 _build_record_string
"$oldchallenge"
146 #Recreate the existing challenges
147 if ! _rcode0_rest
"PATCH" "/api/v1/acme/zones/$_domain/rrsets" "[{\"changetype\": \"update\", \"name\": \"$fulldomain.\", \"type\": \"TXT\", \"ttl\": $RCODE0_TTL, \"records\": [$_record_string]}]"; then
148 _err
"Set txt record error."
153 _info
"Record not found, nothing to remove"
159 #################### Private functions below ##################################
160 #_acme-challenge.www.domain.com
168 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
170 _debug
"try to find: $h"
171 if _rcode0_rest
"GET" "/api/v1/acme/zones/$h"; then
172 if [ "$response" = "[\"found\"]" ]; then
178 elif [ "$response" = "[\"not a master domain\"]" ]; then
188 _debug
"no matching domain for $domain found"
198 export _H1
="Authorization: Bearer $RCODE0_API_TOKEN"
200 if [ ! "$method" = "GET" ]; then
202 response
="$(_post "$data" "$RCODE0_URL$ep" "" "$method")"
204 response
="$(_get "$RCODE0_URL$ep")"
207 if [ "$?" != "0" ]; then
211 _debug2 response
"$response"
216 _build_record_string
() {
217 _record_string
="${_record_string:+${_record_string}, }{\"content\": \"\\\"${1}\\\"\", \"disabled\": false}"
220 _list_existingchallenges
() {
221 _rcode0_rest
"GET" "/api/v1/acme/zones/$_domain/rrsets"
222 _existing_challenges
=$
(echo "$response" | _normalizeJson | _egrep_o
"\"name\":\"${fulldomain}[^]]*}" | _egrep_o
'content\":\"\\"[^\\]*' |
sed -n 's/^content":"\\"//p')
223 _debug2
"$_existing_challenges"