]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_pdns.sh
Merge branch 'master' of github.com:Neilpang/acme.sh
[mirror_acme.sh.git] / dnsapi / dns_pdns.sh
1 #!/usr/bin/env sh
2
3 #PowerDNS Embedded API
4 #https://doc.powerdns.com/md/httpapi/api_spec/
5 #
6 #PDNS_Url="http://ns.example.com:8081"
7 #PDNS_ServerId="localhost"
8 #PDNS_Token="0123456789ABCDEF"
9 #PDNS_Ttl=60
10
11 DEFAULT_PDNS_TTL=60
12
13 ######## Public functions #####################
14 #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
15 #fulldomain
16 #txtvalue
17 dns_pdns_add() {
18 fulldomain=$1
19 txtvalue=$2
20
21 if [ -z "$PDNS_Url" ]; then
22 PDNS_Url=""
23 _err "You don't specify PowerDNS address."
24 _err "Please set PDNS_Url and try again."
25 return 1
26 fi
27
28 if [ -z "$PDNS_ServerId" ]; then
29 PDNS_ServerId=""
30 _err "You don't specify PowerDNS server id."
31 _err "Please set you PDNS_ServerId and try again."
32 return 1
33 fi
34
35 if [ -z "$PDNS_Token" ]; then
36 PDNS_Token=""
37 _err "You don't specify PowerDNS token."
38 _err "Please create you PDNS_Token and try again."
39 return 1
40 fi
41
42 if [ -z "$PDNS_Ttl" ]; then
43 PDNS_Ttl="$DEFAULT_PDNS_TTL"
44 fi
45
46 #save the api addr and key to the account conf file.
47 _saveaccountconf PDNS_Url "$PDNS_Url"
48 _saveaccountconf PDNS_ServerId "$PDNS_ServerId"
49 _saveaccountconf PDNS_Token "$PDNS_Token"
50
51 if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
52 _saveaccountconf PDNS_Ttl "$PDNS_Ttl"
53 fi
54
55 _debug "Detect root zone"
56 if ! _get_root "$fulldomain"; then
57 _err "invalid domain"
58 return 1
59 fi
60 _debug _domain "$_domain"
61
62 if ! set_record "$_domain" "$fulldomain" "$txtvalue"; then
63 return 1
64 fi
65
66 return 0
67 }
68
69 #fulldomain
70 dns_pdns_rm() {
71 fulldomain=$1
72
73 _debug "Detect root zone"
74 if ! _get_root "$fulldomain"; then
75 _err "invalid domain"
76 return 1
77 fi
78 _debug _domain "$_domain"
79
80 if ! rm_record "$_domain" "$fulldomain"; then
81 return 1
82 fi
83
84 return 0
85 }
86
87 set_record() {
88 _info "Adding record"
89 root=$1
90 full=$2
91 txtvalue=$3
92
93 if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"REPLACE\", \"name\": \"$full.\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [{\"name\": \"$full.\", \"type\": \"TXT\", \"content\": \"\\\"$txtvalue\\\"\", \"disabled\": false, \"ttl\": $PDNS_Ttl}]}]}"; then
94 _err "Set txt record error."
95 return 1
96 fi
97
98 if ! notify_slaves "$root"; then
99 return 1
100 fi
101
102 return 0
103 }
104
105 rm_record() {
106 _info "Remove record"
107 root=$1
108 full=$2
109
110 if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}"; then
111 _err "Delete txt record error."
112 return 1
113 fi
114
115 if ! notify_slaves "$root"; then
116 return 1
117 fi
118
119 return 0
120 }
121
122 notify_slaves() {
123 root=$1
124
125 if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root/notify"; then
126 _err "Notify slaves error."
127 return 1
128 fi
129
130 return 0
131 }
132
133 #################### Private functions below ##################################
134 #_acme-challenge.www.domain.com
135 #returns
136 # _domain=domain.com
137 _get_root() {
138 domain=$1
139 i=1
140
141 if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
142 _zones_response="$response"
143 fi
144
145 while true; do
146 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
147
148 if _contains "$_zones_response" "\"name\": \"$h.\""; then
149 _domain="$h."
150 if [ -z "$h" ]; then
151 _domain="=2E"
152 fi
153 return 0
154 fi
155
156 if [ -z "$h" ]; then
157 return 1
158 fi
159 i=$(_math $i + 1)
160 done
161 _debug "$domain not found"
162
163 return 1
164 }
165
166 _pdns_rest() {
167 method=$1
168 ep=$2
169 data=$3
170
171 export _H1="X-API-Key: $PDNS_Token"
172
173 if [ ! "$method" = "GET" ]; then
174 _debug data "$data"
175 response="$(_post "$data" "$PDNS_Url$ep" "" "$method")"
176 else
177 response="$(_get "$PDNS_Url$ep")"
178 fi
179
180 if [ "$?" != "0" ]; then
181 _err "error $ep"
182 return 1
183 fi
184 _debug2 response "$response"
185
186 return 0
187 }