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
8 years ago
  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. count=$(printf "%s\n" "$response" | _egrep_o "\"totalRecords\":[^,]*" | cut -d : -f 2)
  36. _debug count "$count"
  37. # _info "Adding record"
  38. if _me_rest POST "$_domain_id/records/" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\",\"gtdLocation\":\"DEFAULT\",\"ttl\":120}"; then
  39. if printf -- "%s" "$response" | grep \"id\": >/dev/null; then
  40. _info "Added"
  41. #todo: check if the record takes effect
  42. return 0
  43. else
  44. _err "Add txt record error."
  45. return 1
  46. fi
  47. fi
  48. _err "Add txt record error."
  49. return 1
  50. }
  51. #fulldomain
  52. dns_me_rm() {
  53. fulldomain=$1
  54. txtvalue=$2
  55. _debug "First detect the root zone"
  56. if ! _get_root "$fulldomain"; then
  57. _err "invalid domain"
  58. return 1
  59. fi
  60. _debug _domain_id "$_domain_id"
  61. _debug _sub_domain "$_sub_domain"
  62. _debug _domain "$_domain"
  63. _debug "Getting txt records"
  64. _me_rest GET "${_domain_id}/records?recordName=$_sub_domain&type=TXT"
  65. count=$(printf "%s\n" "$response" | _egrep_o "\"totalRecords\":[^,]*" | cut -d : -f 2)
  66. _debug count "$count"
  67. if [ "$count" = "0" ]; then
  68. _info "Don't need to remove."
  69. else
  70. record_id=$(printf "%s\n" "$response" | _egrep_o ",\"value\":\"..$txtvalue..\",\"id\":[^,]*" | cut -d : -f 3 | head -n 1)
  71. _debug "record_id" "$record_id"
  72. if [ -z "$record_id" ]; then
  73. _err "Can not get record id to remove."
  74. return 1
  75. fi
  76. if ! _me_rest DELETE "$_domain_id/records/$record_id"; then
  77. _err "Delete record error."
  78. return 1
  79. fi
  80. _contains "$response" ''
  81. fi
  82. }
  83. #################### Private functions below ##################################
  84. #_acme-challenge.www.domain.com
  85. #returns
  86. # _sub_domain=_acme-challenge.www
  87. # _domain=domain.com
  88. # _domain_id=sdjkglgdfewsdfg
  89. _get_root() {
  90. domain=$1
  91. i=2
  92. p=1
  93. while true; do
  94. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  95. if [ -z "$h" ]; then
  96. #not valid
  97. return 1
  98. fi
  99. if ! _me_rest GET "name?domainname=$h"; then
  100. return 1
  101. fi
  102. if _contains "$response" "\"name\":\"$h\""; then
  103. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*" | head -n 1 | cut -d : -f 2 | tr -d '}')
  104. if [ "$_domain_id" ]; then
  105. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  106. _domain="$h"
  107. return 0
  108. fi
  109. return 1
  110. fi
  111. p=$i
  112. i=$(_math "$i" + 1)
  113. done
  114. return 1
  115. }
  116. _me_rest() {
  117. m=$1
  118. ep="$2"
  119. data="$3"
  120. _debug "$ep"
  121. cdate=$(LANG=C date -u +"%a, %d %b %Y %T %Z")
  122. hmac=$(printf "%s" "$cdate" | _hmac sha1 "$(printf "%s" "$ME_Secret" | _hex_dump | tr -d " ")" hex)
  123. export _H1="x-dnsme-apiKey: $ME_Key"
  124. export _H2="x-dnsme-requestDate: $cdate"
  125. export _H3="x-dnsme-hmac: $hmac"
  126. if [ "$m" != "GET" ]; then
  127. _debug data "$data"
  128. response="$(_post "$data" "$ME_Api/$ep" "" "$m")"
  129. else
  130. response="$(_get "$ME_Api/$ep")"
  131. fi
  132. if [ "$?" != "0" ]; then
  133. _err "error $ep"
  134. return 1
  135. fi
  136. _debug2 response "$response"
  137. return 0
  138. }