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.

236 lines
8.3 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 for the code reused from acme.sh from HTTP-01 validation
  9. # used to communicate with the MailintheBox 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"
  20. _debug fulldomain "$fulldomain"
  21. _debug txtvalue "$txtvalue"
  22. MIAB_Username="${MIAB_Username:-$(_readaccountconf_mutable MIAB_Username)}"
  23. MIAB_Password="${MIAB_Password:-$(_readaccountconf_mutable MIAB_Password)}"
  24. MIAB_Server="${MIAB_Server:-$(_readaccountconf_mutable MIAB_Server)}"
  25. #debug log the environmental variables
  26. _debug MIAB_Username "$MIAB_Username"
  27. _debug MIAB_Password "$MIAB_Password"
  28. _debug MIAB_Server "$MIAB_Server"
  29. if [ -z "$MIAB_Username" ] || [ -z "$MIAB_Password" ] || [ -z "$MIAB_Server" ]; then
  30. MIAB_Username=""
  31. MIAB_Password=""
  32. MIAB_Server=""
  33. _err "You didn't specify MIAB_Username or MIAB_Password or MIAB_Server."
  34. _err "Please try again."
  35. return 1
  36. fi
  37. #save the credentials to the account conf file.
  38. _saveaccountconf_mutable MIAB_Username "$MIAB_Username"
  39. _saveaccountconf_mutable MIAB_Password "$MIAB_Password"
  40. _saveaccountconf_mutable MIAB_Server "$MIAB_Server"
  41. baseurl="https://$MIAB_Server/admin/dns/custom/$fulldomain/txt"
  42. #Add the challenge record
  43. result="$(_miab_post "$txtvalue" "$baseurl" "" "POST" "" "$MIAB_Username" "$MIAB_Password")"
  44. _debug result "$result"
  45. #check if result was good
  46. if _contains "$result" "updated DNS"; then
  47. _info "Successfully created the txt record"
  48. return 0
  49. else
  50. _err "Error encountered during record addition"
  51. _err "$result"
  52. return 1
  53. fi
  54. }
  55. #Usage: fulldomain txtvalue
  56. #Remove the txt record after validation.
  57. dns_miab_rm() {
  58. fulldomain=$1
  59. txtvalue=$2
  60. _info "Using miab"
  61. _debug fulldomain "$fulldomain"
  62. _debug txtvalue "$txtvalue"
  63. MIAB_Username="${MIAB_Username:-$(_readaccountconf_mutable MIAB_Username)}"
  64. MIAB_Password="${MIAB_Password:-$(_readaccountconf_mutable MIAB_Password)}"
  65. MIAB_Server="${MIAB_Server:-$(_readaccountconf_mutable MIAB_Server)}"
  66. #debug log the environmental variables
  67. _debug MIAB_Username "$MIAB_Username"
  68. _debug MIAB_Password "$MIAB_Password"
  69. _debug MIAB_Server "$MIAB_Server"
  70. if [ -z "$MIAB_Username" ] || [ -z "$MIAB_Password" ] || [ -z "$MIAB_Server" ]; then
  71. MIAB_Username=""
  72. MIAB_Password=""
  73. MIAB_Server=""
  74. _err "You didn't specify MIAB_Username or MIAB_Password or MIAB_Server."
  75. _err "Please try again."
  76. return 1
  77. fi
  78. #save the credentials to the account conf file.
  79. _saveaccountconf_mutable MIAB_Username "$MIAB_Username"
  80. _saveaccountconf_mutable MIAB_Password "$MIAB_Password"
  81. _saveaccountconf_mutable MIAB_Server "$MIAB_Server"
  82. baseurl="https://$MIAB_Server/admin/dns/custom/$fulldomain/txt"
  83. #Remove the challenge record
  84. result="$(_miab_post "$txtvalue" "$baseurl" "" "DELETE" "" "$MIAB_Username" "$MIAB_Password")"
  85. _debug result "$result"
  86. #check if result was good
  87. if _contains "$result" "updated DNS"; then
  88. _info "Successfully created the txt record"
  89. return 0
  90. else
  91. _err "Error encountered during record addition"
  92. _err "$result"
  93. return 1
  94. fi
  95. }
  96. #################### Private functions below ##################################
  97. #
  98. # post changes to MIAB dns (taken from acme.sh)
  99. _miab_post() {
  100. body="$1"
  101. _post_url="$2"
  102. needbase64="$3"
  103. httpmethod="$4"
  104. _postContentType="$5"
  105. username="$6"
  106. password="$7"
  107. if [ -z "$httpmethod" ]; then
  108. httpmethod="POST"
  109. fi
  110. _debug $httpmethod
  111. _debug "_post_url" "$_post_url"
  112. _debug2 "body" "$body"
  113. _debug2 "_postContentType" "$_postContentType"
  114. _inithttp
  115. if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
  116. _CURL="$_ACME_CURL"
  117. if [ "$HTTPS_INSECURE" ]; then
  118. _CURL="$_CURL --insecure "
  119. fi
  120. _debug "_CURL" "$_CURL"
  121. if [ "$needbase64" ]; then
  122. if [ "$_postContentType" ]; then
  123. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod --user "$username:$password" -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)"
  124. else
  125. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod --user "$username:$password" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)"
  126. fi
  127. else
  128. if [ "$_postContentType" ]; then
  129. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod --user "$username:$password" -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
  130. else
  131. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod --user "$username:$password" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
  132. fi
  133. fi
  134. _ret="$?"
  135. if [ "$_ret" != "0" ]; then
  136. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
  137. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  138. _err "Here is the curl dump log:"
  139. _err "$(cat "$_CURL_DUMP")"
  140. fi
  141. fi
  142. elif [ "$_ACME_WGET" ]; then
  143. _WGET="$_ACME_WGET"
  144. if [ "$HTTPS_INSECURE" ]; then
  145. _WGET="$_WGET --no-check-certificate "
  146. fi
  147. _debug "_WGET" "$_WGET"
  148. if [ "$needbase64" ]; then
  149. if [ "$httpmethod" = "POST" ]; then
  150. if [ "$_postContentType" ]; then
  151. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  152. else
  153. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  154. fi
  155. else
  156. if [ "$_postContentType" ]; then
  157. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  158. else
  159. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  160. fi
  161. fi
  162. else
  163. if [ "$httpmethod" = "POST" ]; then
  164. if [ "$_postContentType" ]; then
  165. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  166. else
  167. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  168. fi
  169. else
  170. if [ "$_postContentType" ]; then
  171. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  172. else
  173. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  174. fi
  175. fi
  176. fi
  177. _ret="$?"
  178. if [ "$_ret" = "8" ]; then
  179. _ret=0
  180. _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later."
  181. fi
  182. if [ "$_ret" != "0" ]; then
  183. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
  184. fi
  185. _sed_i "s/^ *//g" "$HTTP_HEADER"
  186. else
  187. _ret="$?"
  188. _err "Neither curl nor wget was found, cannot do $httpmethod."
  189. fi
  190. _debug "_ret" "$_ret"
  191. printf "%s" "$response"
  192. return $_ret
  193. }