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.

159 lines
3.9 KiB

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