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.

148 lines
3.5 KiB

8 years ago
  1. #!/bin/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. _err "You didn't specify DNSMadeEasy api key and secret yet."
  13. _err "Please create you key and try again."
  14. return 1
  15. fi
  16. #save the api key and email to the account conf file.
  17. _saveaccountconf ME_Key "$ME_Key"
  18. _saveaccountconf ME_Secret "$ME_Secret"
  19. _debug "First detect the root zone"
  20. if ! _get_root $fulldomain ; then
  21. _err "invalid domain"
  22. return 1
  23. fi
  24. _debug _domain_id "$_domain_id"
  25. _debug _sub_domain "$_sub_domain"
  26. _debug _domain "$_domain"
  27. _debug "Getting txt records"
  28. _me_rest GET "${_domain_id}/records?recordName=$_sub_domain&type=TXT"
  29. if ! printf "$response" | grep \"totalRecords\": > /dev/null ; then
  30. _err "Error"
  31. return 1
  32. fi
  33. count=$(printf "%s\n" "$response" | _egrep_o \"totalRecords\":[^,]* | cut -d : -f 2)
  34. _debug count "$count"
  35. if [ "$count" = "0" ] ; then
  36. _info "Adding record"
  37. if _me_rest POST "$_domain_id/records/" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\",\"gtdLocation\":\"DEFAULT\",\"ttl\":120}"; then
  38. if [ "$?" = "0" ]; then
  39. _info "Added"
  40. #todo: check if the record takes effect
  41. return 0
  42. else
  43. _err "Add txt record error."
  44. return 1
  45. fi
  46. fi
  47. _err "Add txt record error."
  48. else
  49. _info "Updating record"
  50. record_id=$(printf "%s\n" "$response" | _egrep_o \"id\":[^,]* | cut -d : -f 2 | head -n 1)
  51. _debug "record_id" $record_id
  52. _me_rest PUT "$_domain_id/records/$record_id/" "{\"id\":\"$record_id\",\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\",\"gtdLocation\":\"DEFAULT\",\"ttl\":120}"
  53. if [ "$?" = "0" ]; then
  54. _info "Updated"
  55. #todo: check if the record takes effect
  56. return 0;
  57. fi
  58. _err "Update error"
  59. return 1
  60. fi
  61. }
  62. #fulldomain
  63. dns_me_rm() {
  64. fulldomain=$1
  65. }
  66. #################### Private functions bellow ##################################
  67. #_acme-challenge.www.domain.com
  68. #returns
  69. # _sub_domain=_acme-challenge.www
  70. # _domain=domain.com
  71. # _domain_id=sdjkglgdfewsdfg
  72. _get_root() {
  73. domain=$1
  74. i=2
  75. p=1
  76. while [ '1' ] ; do
  77. h=$(printf $domain | cut -d . -f $i-100)
  78. if [ -z "$h" ] ; then
  79. #not valid
  80. return 1;
  81. fi
  82. if ! _me_rest GET "name?domainname=$h" ; then
  83. return 1
  84. fi
  85. if printf $response | grep \"name\":\"$h\" >/dev/null ; then
  86. _domain_id=$(printf "%s\n" "$response" | _egrep_o \"id\":[^,]* | head -n 1 | cut -d : -f 2 )
  87. if [ "$_domain_id" ] ; then
  88. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  89. _domain=$h
  90. return 0
  91. fi
  92. return 1
  93. fi
  94. p=$i
  95. i=$(expr $i + 1)
  96. done
  97. return 1
  98. }
  99. _me_rest() {
  100. m=$1
  101. ep="$2"
  102. data="$3"
  103. _debug $ep
  104. cdate=$(date -Ru)
  105. hmac=$(printf "$cdate" | openssl dgst -sha1 -hmac $ME_Secret | cut -d = -f 2 | tr -d ' ')
  106. _H1="x-dnsme-apiKey: $ME_Key"
  107. _H2="x-dnsme-requestDate: $cdate"
  108. _H3="x-dnsme-hmac: $hmac"
  109. if [ "$data" ] ; then
  110. _debug data "$data"
  111. response="$(_post "$data" "$ME_Api/$ep" "" $m)"
  112. else
  113. response="$(_get "$ME_Api/$ep")"
  114. fi
  115. if [ "$?" != "0" ] ; then
  116. _err "error $ep"
  117. return 1
  118. fi
  119. _debug2 response "$response"
  120. return 0
  121. }