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.

160 lines
3.9 KiB

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