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.

191 lines
5.6 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. httpmethod="$3"
  103. username="$4"
  104. password="$5"
  105. if [ -z "$httpmethod" ]; then
  106. httpmethod="POST"
  107. fi
  108. _debug $httpmethod
  109. _debug "_post_url" "$_post_url"
  110. _debug2 "body" "$body"
  111. _inithttp
  112. if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
  113. _CURL="$_ACME_CURL"
  114. if [ "$HTTPS_INSECURE" ]; then
  115. _CURL="$_CURL --insecure "
  116. fi
  117. _debug "_CURL" "$_CURL"
  118. 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")"
  119. _ret="$?"
  120. if [ "$_ret" != "0" ]; then
  121. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
  122. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  123. _err "Here is the curl dump log:"
  124. _err "$(cat "$_CURL_DUMP")"
  125. fi
  126. fi
  127. elif [ "$_ACME_WGET" ]; then
  128. _WGET="$_ACME_WGET"
  129. if [ "$HTTPS_INSECURE" ]; then
  130. _WGET="$_WGET --no-check-certificate "
  131. fi
  132. _debug "_WGET" "$_WGET"
  133. if [ "$httpmethod" = "POST" ]; then
  134. 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")"
  135. else
  136. 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")"
  137. fi
  138. _ret="$?"
  139. if [ "$_ret" = "8" ]; then
  140. _ret=0
  141. _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later."
  142. fi
  143. if [ "$_ret" != "0" ]; then
  144. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
  145. fi
  146. _sed_i "s/^ *//g" "$HTTP_HEADER"
  147. else
  148. _ret="$?"
  149. _err "Neither curl nor wget was found, cannot do $httpmethod."
  150. fi
  151. _debug "_ret" "$_ret"
  152. printf "%s" "$response"
  153. return $_ret
  154. }