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