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.

161 lines
4.0 KiB

8 years ago
8 years ago
3 months ago
3 months ago
8 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_me_info='DnsMadeEasy.com
  4. Site: DnsMadeEasy.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_me
  6. Options:
  7. ME_Key API Key
  8. ME_Secret API Secret
  9. Author: <dev@1e.ca>
  10. '
  11. ME_Api=https://api.dnsmadeeasy.com/V2.0/dns/managed
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_me_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if [ -z "$ME_Key" ] || [ -z "$ME_Secret" ]; then
  18. ME_Key=""
  19. ME_Secret=""
  20. _err "You didn't specify DNSMadeEasy api key and secret yet."
  21. _err "Please create you key and try again."
  22. return 1
  23. fi
  24. #save the api key and email to the account conf file.
  25. _saveaccountconf ME_Key "$ME_Key"
  26. _saveaccountconf ME_Secret "$ME_Secret"
  27. _debug "First detect the root zone"
  28. if ! _get_root "$fulldomain"; then
  29. _err "invalid domain"
  30. return 1
  31. fi
  32. _debug _domain_id "$_domain_id"
  33. _debug _sub_domain "$_sub_domain"
  34. _debug _domain "$_domain"
  35. _debug "Getting txt records"
  36. _me_rest GET "${_domain_id}/records?recordName=$_sub_domain&type=TXT"
  37. if ! _contains "$response" "\"totalRecords\":"; then
  38. _err "Error"
  39. return 1
  40. fi
  41. _info "Adding record"
  42. if _me_rest POST "$_domain_id/records/" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\",\"gtdLocation\":\"DEFAULT\",\"ttl\":120}"; then
  43. if printf -- "%s" "$response" | grep \"id\": >/dev/null; then
  44. _info "Added"
  45. #todo: check if the record takes effect
  46. return 0
  47. else
  48. _err "Add txt record error."
  49. return 1
  50. fi
  51. fi
  52. }
  53. #fulldomain
  54. dns_me_rm() {
  55. fulldomain=$1
  56. txtvalue=$2
  57. _debug "First detect the root zone"
  58. if ! _get_root "$fulldomain"; then
  59. _err "invalid domain"
  60. return 1
  61. fi
  62. _debug _domain_id "$_domain_id"
  63. _debug _sub_domain "$_sub_domain"
  64. _debug _domain "$_domain"
  65. _debug "Getting txt records"
  66. _me_rest GET "${_domain_id}/records?recordName=$_sub_domain&type=TXT"
  67. count=$(printf "%s\n" "$response" | _egrep_o "\"totalRecords\":[^,]*" | cut -d : -f 2)
  68. _debug count "$count"
  69. if [ "$count" = "0" ]; then
  70. _info "Don't need to remove."
  71. else
  72. record_id=$(printf "%s\n" "$response" | _egrep_o ",\"value\":\"..$txtvalue..\",\"id\":[^,]*" | cut -d : -f 3 | head -n 1)
  73. _debug "record_id" "$record_id"
  74. if [ -z "$record_id" ]; then
  75. _err "Can not get record id to remove."
  76. return 1
  77. fi
  78. if ! _me_rest DELETE "$_domain_id/records/$record_id"; then
  79. _err "Delete record error."
  80. return 1
  81. fi
  82. _contains "$response" ''
  83. fi
  84. }
  85. #################### Private functions below ##################################
  86. #_acme-challenge.www.domain.com
  87. #returns
  88. # _sub_domain=_acme-challenge.www
  89. # _domain=domain.com
  90. # _domain_id=sdjkglgdfewsdfg
  91. _get_root() {
  92. domain=$1
  93. i=2
  94. p=1
  95. while true; do
  96. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  97. if [ -z "$h" ]; then
  98. #not valid
  99. return 1
  100. fi
  101. if ! _me_rest GET "name?domainname=$h"; then
  102. return 1
  103. fi
  104. if _contains "$response" "\"name\":\"$h\""; then
  105. _domain_id=$(printf "%s\n" "$response" | sed 's/^{//; s/}$//; s/{.*}//' | sed -r 's/^.*"id":([0-9]+).*$/\1/')
  106. if [ "$_domain_id" ]; then
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  108. _domain="$h"
  109. return 0
  110. fi
  111. return 1
  112. fi
  113. p=$i
  114. i=$(_math "$i" + 1)
  115. done
  116. return 1
  117. }
  118. _me_rest() {
  119. m=$1
  120. ep="$2"
  121. data="$3"
  122. _debug "$ep"
  123. cdate=$(LANG=C date -u +"%a, %d %b %Y %T %Z")
  124. hmac=$(printf "%s" "$cdate" | _hmac sha1 "$(printf "%s" "$ME_Secret" | _hex_dump | tr -d " ")" hex)
  125. export _H1="x-dnsme-apiKey: $ME_Key"
  126. export _H2="x-dnsme-requestDate: $cdate"
  127. export _H3="x-dnsme-hmac: $hmac"
  128. if [ "$m" != "GET" ]; then
  129. _debug data "$data"
  130. response="$(_post "$data" "$ME_Api/$ep" "" "$m")"
  131. else
  132. response="$(_get "$ME_Api/$ep")"
  133. fi
  134. if [ "$?" != "0" ]; then
  135. _err "error $ep"
  136. return 1
  137. fi
  138. _debug2 response "$response"
  139. return 0
  140. }