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.

180 lines
4.6 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. if ! printf "%s" "$response" | grep \"success\":true >/dev/null; then
  79. _err "Error"
  80. return 1
  81. fi
  82. count=$(printf "%s\n" "$response" | _egrep_o "\"totalRecords\":[^,]*" | cut -d : -f 2)
  83. _debug count "$count"
  84. if [ "$count" = "0" ]; then
  85. _info "Don't need to remove."
  86. else
  87. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*" | cut -d : -f 2 | head -n 1)
  88. _debug "record_id" "$record_id"
  89. if [ -z "$record_id" ]; then
  90. _err "Can not get record id to remove."
  91. return 1
  92. fi
  93. if ! _me_rest DELETE "$_domain_id/records/$record_id"; then
  94. _err "Delete record error."
  95. return 1
  96. fi
  97. _contains "$response" '"success":true'
  98. fi
  99. }
  100. #################### Private functions below ##################################
  101. #_acme-challenge.www.domain.com
  102. #returns
  103. # _sub_domain=_acme-challenge.www
  104. # _domain=domain.com
  105. # _domain_id=sdjkglgdfewsdfg
  106. _get_root() {
  107. domain=$1
  108. i=2
  109. p=1
  110. while true; do
  111. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  112. if [ -z "$h" ]; then
  113. #not valid
  114. return 1
  115. fi
  116. if ! _me_rest GET "name?domainname=$h"; then
  117. return 1
  118. fi
  119. if _contains "$response" "\"name\":\"$h\""; then
  120. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*" | head -n 1 | cut -d : -f 2 | tr -d '}')
  121. if [ "$_domain_id" ]; then
  122. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  123. _domain="$h"
  124. return 0
  125. fi
  126. return 1
  127. fi
  128. p=$i
  129. i=$(_math "$i" + 1)
  130. done
  131. return 1
  132. }
  133. _me_rest() {
  134. m=$1
  135. ep="$2"
  136. data="$3"
  137. _debug "$ep"
  138. cdate=$(date -u +"%a, %d %b %Y %T %Z")
  139. hmac=$(printf "%s" "$cdate" | _hmac sha1 "$(printf "%s" "$ME_Secret" | _hex_dump | tr -d " ")" hex)
  140. export _H1="x-dnsme-apiKey: $ME_Key"
  141. export _H2="x-dnsme-requestDate: $cdate"
  142. export _H3="x-dnsme-hmac: $hmac"
  143. if [ "$m" != "GET" ]; then
  144. _debug data "$data"
  145. response="$(_post "$data" "$ME_Api/$ep" "" "$m")"
  146. else
  147. response="$(_get "$ME_Api/$ep")"
  148. fi
  149. if [ "$?" != "0" ]; then
  150. _err "error $ep"
  151. return 1
  152. fi
  153. _debug2 response "$response"
  154. return 0
  155. }