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.

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