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.

196 lines
4.9 KiB

  1. #!/usr/bin/env sh
  2. #Here is a api script for MyDNS.JP.
  3. #This file name is "dns_mydnsjp.sh"
  4. #So, here must be a method dns_mydnsjp_add()
  5. #Which will be called by acme.sh to add the txt record to your api system.
  6. #returns 0 means success, otherwise error.
  7. #
  8. #Author: epgdatacapbon
  9. #Report Bugs here: https://github.com/epgdatacapbon/acme.sh
  10. #
  11. ######## Public functions #####################
  12. # Export MyDNS.JP MasterID and Password in following variables...
  13. # MYDNSJP_MasterID=MasterID
  14. # MYDNSJP_Password=Password
  15. MYDNSJP_API="https://www.mydns.jp"
  16. #Usage: dns_mydnsjp_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_mydnsjp_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. _info "Using mydnsjp"
  21. _debug fulldomain "$fulldomain"
  22. _debug txtvalue "$txtvalue"
  23. # Load the credentials from the account conf file
  24. MYDNSJP_MasterID="${MYDNSJP_MasterID:-$(_readaccountconf_mutable MYDNSJP_MasterID)}"
  25. MYDNSJP_Password="${MYDNSJP_Password:-$(_readaccountconf_mutable MYDNSJP_Password)}"
  26. if [ -z "$MYDNSJP_MasterID" ] || [ -z "$MYDNSJP_Password" ]; then
  27. MYDNSJP_MasterID=""
  28. MYDNSJP_Password=""
  29. _err "You don't specify mydnsjp api MasterID and Password yet."
  30. _err "Please export as MYDNSJP_MasterID / MYDNSJP_Password and try again."
  31. return 1
  32. fi
  33. # Save the credentials to the account conf file
  34. _saveaccountconf_mutable MYDNSJP_MasterID "$MYDNSJP_MasterID"
  35. _saveaccountconf_mutable MYDNSJP_Password "$MYDNSJP_Password"
  36. _debug "First detect the root zone."
  37. if ! _get_root "$fulldomain"; then
  38. _err "invalid domain"
  39. return 1
  40. fi
  41. _debug _sub_domain "$_sub_domain"
  42. _debug _domain "$_domain"
  43. if _mydnsjp_api "REGIST" "$_domain" "$txtvalue"; then
  44. if printf -- "%s" "$response" | grep "OK." >/dev/null; then
  45. _info "Added, OK"
  46. return 0
  47. else
  48. _err "Add txt record error."
  49. return 1
  50. fi
  51. fi
  52. _err "Add txt record error."
  53. return 1
  54. }
  55. #Usage: fulldomain txtvalue
  56. #Remove the txt record after validation.
  57. dns_mydnsjp_rm() {
  58. fulldomain=$1
  59. txtvalue=$2
  60. _info "Removing TXT record"
  61. _debug fulldomain "$fulldomain"
  62. _debug txtvalue "$txtvalue"
  63. # Load the credentials from the account conf file
  64. MYDNSJP_MasterID="${MYDNSJP_MasterID:-$(_readaccountconf_mutable MYDNSJP_MasterID)}"
  65. MYDNSJP_Password="${MYDNSJP_Password:-$(_readaccountconf_mutable MYDNSJP_Password)}"
  66. if [ -z "$MYDNSJP_MasterID" ] || [ -z "$MYDNSJP_Password" ]; then
  67. MYDNSJP_MasterID=""
  68. MYDNSJP_Password=""
  69. _err "You don't specify mydnsjp api MasterID and Password yet."
  70. _err "Please export as MYDNSJP_MasterID / MYDNSJP_Password and try again."
  71. return 1
  72. fi
  73. _debug "First detect the root zone"
  74. if ! _get_root "$fulldomain"; then
  75. _err "invalid domain"
  76. return 1
  77. fi
  78. _debug _sub_domain "$_sub_domain"
  79. _debug _domain "$_domain"
  80. if _mydnsjp_api "DELETE" "$_domain" "$txtvalue"; then
  81. if printf -- "%s" "$response" | grep "OK." >/dev/null; then
  82. _info "Deleted, OK"
  83. return 0
  84. else
  85. _err "Delete txt record error."
  86. return 1
  87. fi
  88. fi
  89. _err "Delete txt record error."
  90. return 1
  91. }
  92. #################### Private functions below ##################################
  93. # _acme-challenge.www.domain.com
  94. # returns
  95. # _sub_domain=_acme-challenge.www
  96. # _domain=domain.com
  97. _get_root() {
  98. fulldomain=$1
  99. i=2
  100. p=1
  101. # Get the root domain
  102. _mydnsjp_retrieve_domain
  103. if [ "$?" != "0" ]; then
  104. # not valid
  105. return 1
  106. fi
  107. while true; do
  108. _domain=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
  109. if [ -z "$_domain" ]; then
  110. # not valid
  111. return 1
  112. fi
  113. if [ "$_domain" = "$_root_domain" ]; then
  114. _sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-$p)
  115. return 0
  116. fi
  117. p=$i
  118. i=$(_math "$i" + 1)
  119. done
  120. return 1
  121. }
  122. # Retrieve the root domain
  123. # returns 0 success
  124. _mydnsjp_retrieve_domain() {
  125. _debug "Login to MyDNS.JP"
  126. response="$(_post "MENU=100&masterid=$MYDNSJP_MasterID&masterpwd=$MYDNSJP_Password" "$MYDNSJP_API/members/")"
  127. cookie="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2)"
  128. # If cookies is not empty then logon successful
  129. if [ -z "$cookie" ]; then
  130. _err "Fail to get a cookie."
  131. return 1
  132. fi
  133. _root_domain=$(echo "$response" | grep "DNSINFO\[domainname\]" | sed 's/^.*value="\([^"]*\)".*/\1/')
  134. _debug _root_domain "$_root_domain"
  135. if [ -z "$_root_domain" ]; then
  136. _err "Fail to get the root domain."
  137. return 1
  138. fi
  139. return 0
  140. }
  141. _mydnsjp_api() {
  142. cmd=$1
  143. domain=$2
  144. txtvalue=$3
  145. # Base64 encode the credentials
  146. credentials=$(printf "%s:%s" "$MYDNSJP_MasterID" "$MYDNSJP_Password" | _base64)
  147. # Construct the HTTP Authorization header
  148. export _H1="Content-Type: application/x-www-form-urlencoded"
  149. export _H2="Authorization: Basic ${credentials}"
  150. response="$(_post "CERTBOT_DOMAIN=$domain&CERTBOT_VALIDATION=$txtvalue&EDIT_CMD=$cmd" "$MYDNSJP_API/directedit.html")"
  151. if [ "$?" != "0" ]; then
  152. _err "error $domain"
  153. return 1
  154. fi
  155. _debug2 response "$response"
  156. return 0
  157. }