dns_me.sh 3.8 KB

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