]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_miab.sh
inwx: Be case insensitive while searching for the cookie.
[mirror_acme.sh.git] / dnsapi / dns_miab.sh
1 #!/usr/bin/env sh
2
3 # Name: dns_miab.sh
4 #
5 # Authors:
6 # Darven Dissek 2018
7 # William Gertz 2019
8 #
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
11 # Report Bugs here:
12 # https://github.com/billgertz/MIAB_dns_api (for dns_miab.sh)
13 # https://github.com/acmesh-official/acme.sh (for acme.sh)
14 #
15 ######## Public functions #####################
16
17 #Usage: dns_miab_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
18 dns_miab_add() {
19 fulldomain=$1
20 txtvalue=$2
21 _info "Using miab challange add"
22 _debug fulldomain "$fulldomain"
23 _debug txtvalue "$txtvalue"
24
25 #retrieve MIAB environemt vars
26 if ! _retrieve_miab_env; then
27 return 1
28 fi
29
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}"
33 return 1
34 fi
35
36 _debug2 _sub_domain "$_sub_domain"
37 _debug2 _domain "$_domain"
38
39 #add the challenge record
40 _api_path="custom/${fulldomain}/txt"
41 _miab_rest "$txtvalue" "$_api_path" "POST"
42
43 #check if result was good
44 if _contains "$response" "updated DNS"; then
45 _info "Successfully created the txt record"
46 return 0
47 else
48 _err "Error encountered during record add"
49 _err "$response"
50 return 1
51 fi
52 }
53
54 #Usage: dns_miab_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
55 dns_miab_rm() {
56 fulldomain=$1
57 txtvalue=$2
58
59 _info "Using miab challage delete"
60 _debug fulldomain "$fulldomain"
61 _debug txtvalue "$txtvalue"
62
63 #retrieve MIAB environemt vars
64 if ! _retrieve_miab_env; then
65 return 1
66 fi
67
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}"
71 return 1
72 fi
73
74 _debug2 _sub_domain "$_sub_domain"
75 _debug2 _domain "$_domain"
76
77 #Remove the challenge record
78 _api_path="custom/${fulldomain}/txt"
79 _miab_rest "$txtvalue" "$_api_path" "DELETE"
80
81 #check if result was good
82 if _contains "$response" "updated DNS"; then
83 _info "Successfully removed the txt record"
84 return 0
85 else
86 _err "Error encountered during record remove"
87 _err "$response"
88 return 1
89 fi
90 }
91
92 #################### Private functions below ##################################
93 #
94 #Usage: _get_root _acme-challenge.www.domain.com
95 #Returns:
96 # _sub_domain=_acme-challenge.www
97 # _domain=domain.com
98 _get_root() {
99 _passed_domain=$1
100 _debug _passed_domain "$_passed_domain"
101 _i=2
102 _p=1
103
104 #get the zones hosed on MIAB server, must be a json stream
105 _miab_rest "" "zones" "GET"
106
107 if ! _is_json "$response"; then
108 _err "ERROR fetching domain list"
109 _err "$response"
110 return 1
111 fi
112
113 #cycle through the passed domain seperating out a test domain discarding
114 # the subdomain by marching thorugh the dots
115 while true; do
116 _test_domain=$(printf "%s" "$_passed_domain" | cut -d . -f ${_i}-100)
117 _debug _test_domain "$_test_domain"
118
119 if [ -z "$_test_domain" ]; then
120 return 1
121 fi
122
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}
128 return 0
129 fi
130
131 #cycle to the next dot in the passed domain
132 _p=${_i}
133 _i=$(_math "$_i" + 1)
134 done
135
136 return 1
137 }
138
139 #Usage: _retrieve_miab_env
140 #Returns (from store or environment variables):
141 # MIAB_Username
142 # MIAB_Password
143 # MIAB_Server
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)}"
149
150 #debug log the environmental variables
151 _debug MIAB_Username "$MIAB_Username"
152 _debug MIAB_Password "$MIAB_Password"
153 _debug MIAB_Server "$MIAB_Server"
154
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."
159 return 1
160 fi
161
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"
166 return 0
167 }
168
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
172 _miab_rest() {
173 _data="$1"
174 _api_path="$2"
175 _httpmethod="$3"
176
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}"
181
182 _debug2 _data "$_data"
183 _debug _api_path "$_api_path"
184 _debug2 _url "$_url"
185 _debug2 _credentails "$_credentials"
186 _debug _httpmethod "$_httpmethod"
187
188 if [ "$_httpmethod" = "GET" ]; then
189 response="$(_get "$_url")"
190 else
191 response="$(_post "$_data" "$_url" "" "$_httpmethod")"
192 fi
193
194 _retcode="$?"
195
196 if [ "$_retcode" != "0" ]; then
197 _err "MIAB REST authentication failed on $_httpmethod"
198 return 1
199 fi
200
201 _debug response "$response"
202 return 0
203 }
204
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
208 _is_json() {
209 _str="$(echo "$1" | _normalizeJson)"
210 echo "$_str" | grep '^\[.*\]$' >/dev/null 2>&1
211 }