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.

158 lines
3.8 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. # bug reports to dev@1e.ca
  3. #
  4. #NS1_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  5. #
  6. NS1_Api="https://api.nsone.net/v1"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_nsone_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. if [ -z "$NS1_Key" ]; then
  13. NS1_Key=""
  14. _err "You didn't specify nsone dns api key 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 NS1_Key "$NS1_Key"
  20. _debug "First detect the root zone"
  21. if ! _get_root "$fulldomain"; then
  22. _err "invalid domain"
  23. return 1
  24. fi
  25. _debug _sub_domain "$_sub_domain"
  26. _debug _domain "$_domain"
  27. _debug "Getting txt records"
  28. _nsone_rest GET "zones/${_domain}"
  29. if ! _contains "$response" "\"records\":"; then
  30. _err "Error"
  31. return 1
  32. fi
  33. count=$(printf "%s\n" "$response" | _egrep_o "\"domain\":\"$fulldomain\",[^{]*\"type\":\"TXT\"" | wc -l | tr -d " ")
  34. _debug count "$count"
  35. if [ "$count" = "0" ]; then
  36. _info "Adding record"
  37. if _nsone_rest PUT "zones/$_domain/$fulldomain/TXT" "{\"answers\":[{\"answer\":[\"$txtvalue\"]}],\"type\":\"TXT\",\"domain\":\"$fulldomain\",\"zone\":\"$_domain\"}"; then
  38. if _contains "$response" "$fulldomain"; 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 "\"domain\":\"$fulldomain.\",[^{]*\"type\":\"TXT\",\"id\":\"[^,]*\"" | _head_n 1 | cut -d: -f7 | cut -d, -f1)
  51. _debug "record_id" "$record_id"
  52. _nsone_rest POST "zones/$_domain/$fulldomain/TXT" "{\"answers\": [{\"answer\": [\"$txtvalue\"]}],\"type\": \"TXT\",\"domain\":\"$fulldomain\",\"zone\": \"$_domain\"}"
  53. if [ "$?" = "0" ] && _contains "$response" "$fulldomain"; 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_nsone_rm() {
  64. fulldomain=$1
  65. txtvalue=$2
  66. _debug "First detect the root zone"
  67. if ! _get_root "$fulldomain"; then
  68. _err "invalid domain"
  69. return 1
  70. fi
  71. _debug _sub_domain "$_sub_domain"
  72. _debug _domain "$_domain"
  73. _debug "Getting txt records"
  74. _nsone_rest GET "zones/${_domain}/$fulldomain/TXT"
  75. count=$(printf "%s\n" "$response" | _egrep_o "\"domain\":\"$fulldomain\",.*\"type\":\"TXT\"" | wc -l | tr -d " ")
  76. _debug count "$count"
  77. if [ "$count" = "0" ]; then
  78. _info "Don't need to remove."
  79. else
  80. if ! _nsone_rest DELETE "zones/${_domain}/$fulldomain/TXT"; then
  81. _err "Delete record error."
  82. return 1
  83. fi
  84. _contains "$response" ""
  85. fi
  86. }
  87. #################### Private functions below ##################################
  88. #_acme-challenge.www.domain.com
  89. #returns
  90. # _sub_domain=_acme-challenge.www
  91. # _domain=domain.com
  92. # _domain_id=sdjkglgdfewsdfg
  93. _get_root() {
  94. domain=$1
  95. i=2
  96. p=1
  97. if ! _nsone_rest GET "zones"; then
  98. return 1
  99. fi
  100. while true; do
  101. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  102. _debug h "$h"
  103. if [ -z "$h" ]; then
  104. #not valid
  105. return 1
  106. fi
  107. if _contains "$response" "\"zone\":\"$h\""; then
  108. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  109. _domain="$h"
  110. return 0
  111. fi
  112. p=$i
  113. i=$(_math "$i" + 1)
  114. done
  115. return 1
  116. }
  117. _nsone_rest() {
  118. m=$1
  119. ep="$2"
  120. data="$3"
  121. _debug "$ep"
  122. export _H1="Accept: application/json"
  123. export _H2="X-NSONE-Key: $NS1_Key"
  124. if [ "$m" != "GET" ]; then
  125. _debug data "$data"
  126. response="$(_post "$data" "$NS1_Api/$ep" "" "$m")"
  127. else
  128. response="$(_get "$NS1_Api/$ep")"
  129. fi
  130. if [ "$?" != "0" ]; then
  131. _err "error $ep"
  132. return 1
  133. fi
  134. _debug2 response "$response"
  135. return 0
  136. }