9 # Thanks to Neil Pang and other developers here for code reused from acme.sh from DNS-01
10 # used to communicate with the MailinaBox Custom DNS API
12 # https://github.com/billgertz/MIAB_dns_api (for dns_miab.sh)
13 # https://github.com/acmesh-official/acme.sh (for acme.sh)
15 ######## Public functions #####################
17 #Usage: dns_miab_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21 _info
"Using miab challange add"
22 _debug fulldomain
"$fulldomain"
23 _debug txtvalue
"$txtvalue"
25 #retrieve MIAB environemt vars
26 if ! _retrieve_miab_env
; then
30 #check domain and seperate into doamin and host
31 if ! _get_root
"$fulldomain"; then
32 _err
"Cannot find any part of ${fulldomain} is hosted on ${MIAB_Server}"
36 _debug2 _sub_domain
"$_sub_domain"
37 _debug2 _domain
"$_domain"
39 #add the challenge record
40 _api_path
="custom/${fulldomain}/txt"
41 _miab_rest
"$txtvalue" "$_api_path" "POST"
43 #check if result was good
44 if _contains
"$response" "updated DNS"; then
45 _info
"Successfully created the txt record"
48 _err
"Error encountered during record add"
54 #Usage: dns_miab_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
59 _info
"Using miab challage delete"
60 _debug fulldomain
"$fulldomain"
61 _debug txtvalue
"$txtvalue"
63 #retrieve MIAB environemt vars
64 if ! _retrieve_miab_env
; then
68 #check domain and seperate into doamin and host
69 if ! _get_root
"$fulldomain"; then
70 _err
"Cannot find any part of ${fulldomain} is hosted on ${MIAB_Server}"
74 _debug2 _sub_domain
"$_sub_domain"
75 _debug2 _domain
"$_domain"
77 #Remove the challenge record
78 _api_path
="custom/${fulldomain}/txt"
79 _miab_rest
"$txtvalue" "$_api_path" "DELETE"
81 #check if result was good
82 if _contains
"$response" "updated DNS"; then
83 _info
"Successfully removed the txt record"
86 _err
"Error encountered during record remove"
92 #################### Private functions below ##################################
94 #Usage: _get_root _acme-challenge.www.domain.com
96 # _sub_domain=_acme-challenge.www
100 _debug _passed_domain
"$_passed_domain"
104 #get the zones hosed on MIAB server, must be a json stream
105 _miab_rest
"" "zones" "GET"
107 if ! _is_json
"$response"; then
108 _err
"ERROR fetching domain list"
113 #cycle through the passed domain seperating out a test domain discarding
114 # the subdomain by marching thorugh the dots
116 _test_domain
=$
(printf "%s" "$_passed_domain" | cut
-d .
-f ${_i}-100)
117 _debug _test_domain
"$_test_domain"
119 if [ -z "$_test_domain" ]; then
123 #report found if the test domain is in the json response and
124 # report the subdomain
125 if _contains
"$response" "\"$_test_domain\""; then
126 _sub_domain
=$
(printf "%s" "$_passed_domain" | cut
-d .
-f 1-${_p})
127 _domain
=${_test_domain}
131 #cycle to the next dot in the passed domain
133 _i
=$
(_math
"$_i" + 1)
139 #Usage: _retrieve_miab_env
140 #Returns (from store or environment variables):
144 #retrieve MIAB environment variables, report errors and quit if problems
145 _retrieve_miab_env
() {
146 MIAB_Username
="${MIAB_Username:-$(_readaccountconf_mutable MIAB_Username)}"
147 MIAB_Password
="${MIAB_Password:-$(_readaccountconf_mutable MIAB_Password)}"
148 MIAB_Server
="${MIAB_Server:-$(_readaccountconf_mutable MIAB_Server)}"
150 #debug log the environmental variables
151 _debug MIAB_Username
"$MIAB_Username"
152 _debug MIAB_Password
"$MIAB_Password"
153 _debug MIAB_Server
"$MIAB_Server"
155 #check if MIAB environemt vars set and quit if not
156 if [ -z "$MIAB_Username" ] ||
[ -z "$MIAB_Password" ] ||
[ -z "$MIAB_Server" ]; then
157 _err
"You didn't specify one or more of MIAB_Username, MIAB_Password or MIAB_Server."
158 _err
"Please check these environment variables and try again."
162 #save the credentials to the account conf file.
163 _saveaccountconf_mutable MIAB_Username
"$MIAB_Username"
164 _saveaccountconf_mutable MIAB_Password
"$MIAB_Password"
165 _saveaccountconf_mutable MIAB_Server
"$MIAB_Server"
169 #Useage: _miab_rest "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" "custom/_acme-challenge.www.domain.com/txt "POST"
170 #Returns: "updated DNS: domain.com"
171 #rest interface MIAB dns
177 #encode username and password for basic authentication
178 _credentials
="$(printf "%s
" "$MIAB_Username:$MIAB_Password" | _base64)"
179 export _H1
="Authorization: Basic $_credentials"
180 _url
="https://${MIAB_Server}/admin/dns/${_api_path}"
182 _debug2 _data
"$_data"
183 _debug _api_path
"$_api_path"
185 _debug2 _credentails
"$_credentials"
186 _debug _httpmethod
"$_httpmethod"
188 if [ "$_httpmethod" = "GET" ]; then
189 response
="$(_get "$_url")"
191 response
="$(_post "$_data" "$_url" "" "$_httpmethod")"
196 if [ "$_retcode" != "0" ]; then
197 _err
"MIAB REST authentication failed on $_httpmethod"
201 _debug response
"$response"
205 #Usage: _is_json "\[\n "mydomain.com"\n]"
206 #Reurns "\[\n "mydomain.com"\n]"
207 #returns the string if it begins and ends with square braces
209 _str
="$(echo "$1" | _normalizeJson)"
210 echo "$_str" |
grep '^\[.*\]$' >/dev
/null
2>&1