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.

146 lines
3.6 KiB

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. }
  68. #################### Private functions below ##################################
  69. #_acme-challenge.www.domain.com
  70. #returns
  71. # _sub_domain=_acme-challenge.www
  72. # _domain=domain.com
  73. # _domain_id=sdjkglgdfewsdfg
  74. _get_root() {
  75. domain=$1
  76. i=2
  77. p=1
  78. while true; do
  79. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  80. if [ -z "$h" ]; then
  81. #not valid
  82. return 1
  83. fi
  84. if ! _me_rest GET "name?domainname=$h"; then
  85. return 1
  86. fi
  87. if _contains "$response" "\"name\":\"$h\""; then
  88. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*" | head -n 1 | cut -d : -f 2 | tr -d '}')
  89. if [ "$_domain_id" ]; then
  90. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  91. _domain="$h"
  92. return 0
  93. fi
  94. return 1
  95. fi
  96. p=$i
  97. i=$(_math "$i" + 1)
  98. done
  99. return 1
  100. }
  101. _me_rest() {
  102. m=$1
  103. ep="$2"
  104. data="$3"
  105. _debug "$ep"
  106. cdate=$(date -u +"%a, %d %b %Y %T %Z")
  107. hmac=$(printf "%s" "$cdate" | _hmac sha1 "$(_hex "$ME_Secret")" hex)
  108. export _H1="x-dnsme-apiKey: $ME_Key"
  109. export _H2="x-dnsme-requestDate: $cdate"
  110. export _H3="x-dnsme-hmac: $hmac"
  111. if [ "$data" ]; then
  112. _debug data "$data"
  113. response="$(_post "$data" "$ME_Api/$ep" "" "$m")"
  114. else
  115. response="$(_get "$ME_Api/$ep")"
  116. fi
  117. if [ "$?" != "0" ]; then
  118. _err "error $ep"
  119. return 1
  120. fi
  121. _debug2 response "$response"
  122. return 0
  123. }