dns_mydnsjp.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "masterid=$MYDNSJP_MasterID&masterpwd=$MYDNSJP_Password" "$MYDNSJP_API/?MENU=100")"
  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. _debug "Retrieve DOMAIN INFO page"
  134. export _H1="Cookie:${cookie}"
  135. response="$(_get "$MYDNSJP_API/?MENU=300")"
  136. if [ "$?" != "0" ]; then
  137. _err "Fail to retrieve DOMAIN INFO."
  138. return 1
  139. fi
  140. _root_domain=$(echo "$response" | grep "DNSINFO\[domainname\]" | sed 's/^.*value="\([^"]*\)".*/\1/')
  141. # Logout
  142. response="$(_get "$MYDNSJP_API/?MENU=090")"
  143. _debug _root_domain "$_root_domain"
  144. if [ -z "$_root_domain" ]; then
  145. _err "Fail to get the root domain."
  146. return 1
  147. fi
  148. return 0
  149. }
  150. _mydnsjp_api() {
  151. cmd=$1
  152. domain=$2
  153. txtvalue=$3
  154. # Base64 encode the credentials
  155. credentials=$(printf "%s:%s" "$MYDNSJP_MasterID" "$MYDNSJP_Password" | _base64)
  156. # Construct the HTTP Authorization header
  157. export _H1="Content-Type: application/x-www-form-urlencoded"
  158. export _H2="Authorization: Basic ${credentials}"
  159. response="$(_post "CERTBOT_DOMAIN=$domain&CERTBOT_VALIDATION=$txtvalue&EDIT_CMD=$cmd" "$MYDNSJP_API/directedit.html")"
  160. if [ "$?" != "0" ]; then
  161. _err "error $domain"
  162. return 1
  163. fi
  164. _debug2 response "$response"
  165. return 0
  166. }