You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.8 KiB

  1. #!/usr/bin/env sh
  2. # Name: dns_miab.sh
  3. #
  4. # Authors:
  5. # Darven Dissek 2018
  6. # William Gertz 2019
  7. #
  8. # Thanks to Neil Pang and other developers here for code reused from acme.sh from DNS-01
  9. # used to communicate with the MailinaBox Custom DNS API
  10. # Report Bugs here:
  11. # https://github.com/billgertz/MIAB_dns_api (for dns_miab.sh)
  12. # https://github.com/Neilpang/acme.sh (for acme.sh)
  13. #
  14. ######## Public functions #####################
  15. #Usage: dns_miab_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_miab_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. _info "Using miab challange add"
  20. _debug fulldomain "$fulldomain"
  21. _debug txtvalue "$txtvalue"
  22. #retrieve MIAB environemt vars
  23. if ! _retrieve_miab_env; then
  24. return 1
  25. fi
  26. #check domain and seperate into doamin and host
  27. if ! _get_root "$fulldomain"; then
  28. _err "Cannot find any part of ${fulldomain} is hosted on ${MIAB_Server}"
  29. return 1
  30. fi
  31. _debug2 _sub_domain "$_sub_domain"
  32. _debug2 _domain "$_domain"
  33. #add the challenge record
  34. _api_path="custom/${fulldomain}/txt"
  35. _miab_rest "$txtvalue" "$_api_path" "POST"
  36. #check if result was good
  37. if _contains "$response" "updated DNS"; then
  38. _info "Successfully created the txt record"
  39. return 0
  40. else
  41. _err "Error encountered during record add"
  42. _err "$response"
  43. return 1
  44. fi
  45. }
  46. #Usage: dns_miab_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  47. dns_miab_rm() {
  48. fulldomain=$1
  49. txtvalue=$2
  50. _info "Using miab challage delete"
  51. _debug fulldomain "$fulldomain"
  52. _debug txtvalue "$txtvalue"
  53. #retrieve MIAB environemt vars
  54. if ! _retrieve_miab_env; then
  55. return 1
  56. fi
  57. #check domain and seperate into doamin and host
  58. if ! _get_root "$fulldomain"; then
  59. _err "Cannot find any part of ${fulldomain} is hosted on ${MIAB_Server}"
  60. return 1
  61. fi
  62. _debug2 _sub_domain "$_sub_domain"
  63. _debug2 _domain "$_domain"
  64. #Remove the challenge record
  65. _api_path="custom/${fulldomain}/txt"
  66. _miab_rest "$txtvalue" "$_api_path" "DELETE"
  67. #check if result was good
  68. if _contains "$response" "updated DNS"; then
  69. _info "Successfully removed the txt record"
  70. return 0
  71. else
  72. _err "Error encountered during record remove"
  73. _err "$response"
  74. return 1
  75. fi
  76. }
  77. #################### Private functions below ##################################
  78. #
  79. #Usage: _get_root _acme-challenge.www.domain.com
  80. #Returns:
  81. # _sub_domain=_acme-challenge.www
  82. # _domain=domain.com
  83. _get_root() {
  84. _passed_domain=$1
  85. _debug _passed_domain "$_passed_domain"
  86. _i=2
  87. _p=1
  88. #get the zones hosed on MIAB server, must be a json stream
  89. _miab_rest "" "zones" "GET"
  90. _info "_startswith test:$(_startswith "test" "t")"
  91. _info "_endstest test:$(_endswith "test" "t")"
  92. if ! _is_json "$response"; then
  93. _err "ERROR fetching domain list"
  94. _err "$response"
  95. return 1
  96. fi
  97. #cycle through the passed domain seperating out a test domain discarding
  98. # the subdomain by marching thorugh the dots
  99. while true; do
  100. _test_domain=$(printf "%s" "$_passed_domain" | cut -d . -f ${_i}-100)
  101. _debug _test_domain "$_test_domain"
  102. if [ -z "$_test_domain" ]; then
  103. return 1
  104. fi
  105. #report found if the test domain is in the json response and
  106. # report the subdomain
  107. if _contains "$response" "\"$_test_domain\""; then
  108. _sub_domain=$(printf "%s" "$_passed_domain" | cut -d . -f 1-${_p})
  109. _domain=${_test_domain}
  110. return 0
  111. fi
  112. #cycle to the next dot in the passed domain
  113. _p=${_i}
  114. _i=$(_math "$_i" + 1)
  115. done
  116. return 1
  117. }
  118. #Usage: _retrieve_miab_env
  119. #Returns (from store or environment variables):
  120. # MIAB_Username
  121. # MIAB_Password
  122. # MIAB_Server
  123. #retrieve MIAB environment variables, report errors and quit if problems
  124. _retrieve_miab_env() {
  125. MIAB_Username="${MIAB_Username:-$(_readaccountconf_mutable MIAB_Username)}"
  126. MIAB_Password="${MIAB_Password:-$(_readaccountconf_mutable MIAB_Password)}"
  127. MIAB_Server="${MIAB_Server:-$(_readaccountconf_mutable MIAB_Server)}"
  128. #debug log the environmental variables
  129. _debug MIAB_Username "$MIAB_Username"
  130. _debug MIAB_Password "$MIAB_Password"
  131. _debug MIAB_Server "$MIAB_Server"
  132. #check if MIAB environemt vars set and quit if not
  133. if [ -z "$MIAB_Username" ] || [ -z "$MIAB_Password" ] || [ -z "$MIAB_Server" ]; then
  134. _err "You didn't specify one or more of MIAB_Username, MIAB_Password or MIAB_Server."
  135. _err "Please check these environment variables and try again."
  136. return 1
  137. fi
  138. #save the credentials to the account conf file.
  139. _saveaccountconf_mutable MIAB_Username "$MIAB_Username"
  140. _saveaccountconf_mutable MIAB_Password "$MIAB_Password"
  141. _saveaccountconf_mutable MIAB_Server "$MIAB_Server"
  142. }
  143. #Useage: _miab_rest "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" "custom/_acme-challenge.www.domain.com/txt "POST"
  144. #Returns: "updated DNS: domain.com"
  145. #rest interface MIAB dns
  146. _miab_rest() {
  147. _data="$1"
  148. _api_path="$2"
  149. _httpmethod="$3"
  150. #encode username and password for url
  151. _username="$(printf "%s" "$MIAB_Username" | _url_encode)"
  152. _password="$(printf "%s" "$MIAB_Password" | _url_encode)"
  153. _url="https://${_username}:${_password}@${MIAB_Server}/admin/dns/${_api_path}"
  154. _debug2 _data "$_data"
  155. _debug _api_path "$_api_path"
  156. _debug2 _url "$_url"
  157. _debug _httpmethod "$_httpmethod"
  158. if [ "$_httpmethod" = "GET" ]; then
  159. response="$(_get "$_url")"
  160. else
  161. response="$(_post "$_data" "$_url" "" "$_httpmethod")"
  162. fi
  163. _retcode="$?"
  164. if [ "$_retcode" != "0" ]; then
  165. _err "MAAB REST authentication failed on $_httpmethod"
  166. return 1
  167. fi
  168. _debug response "$response"
  169. return 0
  170. }
  171. #Usage: _is_json "\[\n "mydomain.com"\n]"
  172. #Reurns "\[\n "mydomain.com"\n]"
  173. #returns the string if it begins and ends with square braces
  174. _is_json() {
  175. _str="$(echo "$1" | _normalizeJson)"
  176. echo "$_str" | grep '^\[.*\]$' >/dev/null 2>&1
  177. }