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.9 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_pmiab_info='Power-Mail-in-a-Box
  4. Site: github.com/ddavness/power-mailinabox
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_pmiab
  6. Options:
  7. PMIAB_Username Admin username
  8. PMIAB_Password Admin password
  9. PMIAB_Server Server hostname. FQDN of your_PMIAB Server
  10. Issues: github.com/acmesh-official/acme.sh/issues/2550
  11. Author: Roland Giesler (lifeboy)
  12. Cloned from dns_miab by Darven Dissek, William Gertz
  13. '
  14. ######## Public functions #####################
  15. #Usage: dns_pmiab_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_pmiab_add() {
  17. fulldomain=$1
  18. txtvalue="$2"
  19. _info "Using pmiab challenge add"
  20. _debug fulldomain "$fulldomain"
  21. _debug txtvalue "$txtvalue"
  22. #retrieve pmiab environemt vars
  23. if ! _retrieve_pmiab_env; then
  24. return 1
  25. fi
  26. #check domain and seperate into domain and host
  27. if ! _get_root "$fulldomain"; then
  28. _err "Cannot find any part of ${fulldomain} is hosted on ${PMIAB_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. # Added "value=" and "&ttl=300" to accomodate the new TXT record format used by the PMIAB API
  36. _pmiab_rest "value=$txtvalue&ttl=300" "$_api_path" "POST"
  37. #check if result was good
  38. if _contains "$response" "updated DNS"; then
  39. _info "Successfully created the txt record"
  40. return 0
  41. else
  42. _err "Error encountered during record add"
  43. _err "$response"
  44. return 1
  45. fi
  46. }
  47. #Usage: dns_pmiab_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  48. dns_pmiab_rm() {
  49. fulldomain=$1
  50. txtvalue=$2
  51. _info "Using pmiab challenge delete"
  52. _debug fulldomain "$fulldomain"
  53. _debug txtvalue "$txtvalue"
  54. #retrieve PMIAB environemt vars
  55. if ! _retrieve_pmiab_env; then
  56. return 1
  57. fi
  58. #check domain and seperate into doamin and host
  59. if ! _get_root "$fulldomain"; then
  60. _err "Cannot find any part of ${fulldomain} is hosted on ${PMIAB_Server}"
  61. return 1
  62. fi
  63. _debug2 _sub_domain "$_sub_domain"
  64. _debug2 _domain "$_domain"
  65. #Remove the challenge record
  66. _api_path="custom/${fulldomain}/txt"
  67. _pmiab_rest "$txtvalue" "$_api_path" "DELETE"
  68. #check if result was good
  69. if _contains "$response" "updated DNS"; then
  70. _info "Successfully removed the txt record"
  71. return 0
  72. else
  73. _err "Error encountered during record remove"
  74. _err "$response"
  75. return 1
  76. fi
  77. }
  78. #################### Private functions below ##################################
  79. #
  80. #Usage: _get_root _acme-challenge.www.domain.com
  81. #Returns:
  82. # _sub_domain=_acme-challenge.www
  83. # _domain=domain.com
  84. _get_root() {
  85. _passed_domain=$1
  86. _debug _passed_domain "$_passed_domain"
  87. _i=2
  88. _p=1
  89. #get the zones hosed on PMIAB server, must be a json stream
  90. _pmiab_rest "" "zones" "GET"
  91. if ! _is_json "$response"; then
  92. _err "ERROR fetching domain list"
  93. _err "$response"
  94. return 1
  95. fi
  96. #cycle through the passed domain seperating out a test domain discarding
  97. # the subdomain by marching thorugh the dots
  98. while true; do
  99. _test_domain=$(printf "%s" "$_passed_domain" | cut -d . -f "${_i}"-100)
  100. _debug _test_domain "$_test_domain"
  101. if [ -z "$_test_domain" ]; then
  102. return 1
  103. fi
  104. #report found if the test domain is in the json response and
  105. # report the subdomain
  106. if _contains "$response" "\"$_test_domain\""; then
  107. _sub_domain=$(printf "%s" "$_passed_domain" | cut -d . -f 1-"${_p}")
  108. _domain=${_test_domain}
  109. return 0
  110. fi
  111. #cycle to the next dot in the passed domain
  112. _p=${_i}
  113. _i=$(_math "$_i" + 1)
  114. done
  115. return 1
  116. }
  117. #Usage: _retrieve_pmiab_env
  118. #Returns (from store or environment variables):
  119. # PMIAB_Username
  120. # PMIAB_Password
  121. # PMIAB_Server
  122. #retrieve PMIAB environment variables, report errors and quit if problems
  123. _retrieve_pmiab_env() {
  124. PMIAB_Username="${PMIAB_Username:-$(_readaccountconf_mutable PMIAB_Username)}"
  125. PMIAB_Password="${PMIAB_Password:-$(_readaccountconf_mutable PMIAB_Password)}"
  126. PMIAB_Server="${PMIAB_Server:-$(_readaccountconf_mutable PMIAB_Server)}"
  127. #debug log the environmental variables
  128. _debug PMIAB_Username "$PMIAB_Username"
  129. _debug PMIAB_Password "$PMIAB_Password"
  130. _debug PMIAB_Server "$PMIAB_Server"
  131. #check if PMIAB environemt vars set and quit if not
  132. if [ -z "$PMIAB_Username" ] || [ -z "$PMIAB_Password" ] || [ -z "$PMIAB_Server" ]; then
  133. _err "You didn't specify one or more of PMIAB_Username, PMIAB_Password or PMIAB_Server."
  134. _err "Please check these environment variables and try again."
  135. return 1
  136. fi
  137. #save the credentials to the account conf file.
  138. _saveaccountconf_mutable PMIAB_Username "$PMIAB_Username"
  139. _saveaccountconf_mutable PMIAB_Password "$PMIAB_Password"
  140. _saveaccountconf_mutable PMIAB_Server "$PMIAB_Server"
  141. return 0
  142. }
  143. #Useage: _pmiab_rest "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" "custom/_acme-challenge.www.domain.com/txt "POST"
  144. #Returns: "updated DNS: domain.com"
  145. #rest interface PMIAB dns
  146. _pmiab_rest() {
  147. _data="$1"
  148. _api_path="$2"
  149. _httpmethod="$3"
  150. #encode username and password for basic authentication
  151. _credentials="$(printf "%s" "$PMIAB_Username:$PMIAB_Password" | _base64)"
  152. export _H1="Authorization: Basic $_credentials"
  153. _url="https://${PMIAB_Server}/admin/dns/${_api_path}"
  154. _debug2 _data "$_data"
  155. _debug _api_path "$_api_path"
  156. _debug2 _url "$_url"
  157. _debug2 _credentails "$_credentials"
  158. _debug _httpmethod "$_httpmethod"
  159. if [ "$_httpmethod" = "GET" ]; then
  160. response="$(_get "$_url")"
  161. else
  162. response="$(_post "$_data" "$_url" "" "$_httpmethod")"
  163. fi
  164. _retcode="$?"
  165. if [ "$_retcode" != "0" ]; then
  166. _err "PMIAB REST authentication failed on $_httpmethod"
  167. return 1
  168. fi
  169. _debug response "$response"
  170. return 0
  171. }
  172. #Usage: _is_json "\[\n "mydomain.com"\n]"
  173. #Reurns "\[\n "mydomain.com"\n]"
  174. #returns the string if it begins and ends with square braces
  175. _is_json() {
  176. _str="$(echo "$1" | _normalizeJson)"
  177. echo "$_str" | grep '^\[.*\]$' >/dev/null 2>&1
  178. }