]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_ad.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_ad.sh
1 #!/usr/bin/env sh
2
3 #
4 #AD_API_KEY="sdfsdfsdfljlbjkljlkjsdfoiwje"
5
6 #This is the Alwaysdata api wrapper for acme.sh
7 #
8 #Author: Paul Koppen
9 #Report Bugs here: https://github.com/wpk-/acme.sh
10
11 AD_API_URL="https://$AD_API_KEY:@api.alwaysdata.com/v1"
12
13 ######## Public functions #####################
14
15 #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 dns_ad_add() {
17 fulldomain=$1
18 txtvalue=$2
19
20 if [ -z "$AD_API_KEY" ]; then
21 AD_API_KEY=""
22 _err "You didn't specify the AD api key yet."
23 _err "Please create you key and try again."
24 return 1
25 fi
26
27 _saveaccountconf AD_API_KEY "$AD_API_KEY"
28
29 _debug "First detect the root zone"
30 if ! _get_root "$fulldomain"; then
31 _err "invalid domain"
32 return 1
33 fi
34 _debug _domain_id "$_domain_id"
35 _debug _sub_domain "$_sub_domain"
36 _debug _domain "$_domain"
37
38 _ad_tmpl_json="{\"domain\":$_domain_id,\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\"}"
39
40 if _ad_rest POST "record/" "$_ad_tmpl_json" && [ -z "$response" ]; then
41 _info "txt record updated success."
42 return 0
43 fi
44
45 return 1
46 }
47
48 #fulldomain txtvalue
49 dns_ad_rm() {
50 fulldomain=$1
51 txtvalue=$2
52
53 _debug "First detect the root zone"
54 if ! _get_root "$fulldomain"; then
55 _err "invalid domain"
56 return 1
57 fi
58 _debug _domain_id "$_domain_id"
59 _debug _sub_domain "$_sub_domain"
60 _debug _domain "$_domain"
61
62 _debug "Getting txt records"
63 _ad_rest GET "record/?domain=$_domain_id&name=$_sub_domain"
64
65 if [ -n "$response" ]; then
66 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
67 _debug record_id "$record_id"
68 if [ -z "$record_id" ]; then
69 _err "Can not get record id to remove."
70 return 1
71 fi
72 if _ad_rest DELETE "record/$record_id/" && [ -z "$response" ]; then
73 _info "txt record deleted success."
74 return 0
75 fi
76 _debug response "$response"
77 return 1
78 fi
79
80 return 1
81 }
82
83 #################### Private functions below ##################################
84 #_acme-challenge.www.domain.com
85 #returns
86 # _sub_domain=_acme-challenge.www
87 # _domain=domain.com
88 # _domain_id=12345
89 _get_root() {
90 domain=$1
91 i=2
92 p=1
93
94 if _ad_rest GET "domain/"; then
95 response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
96 while true; do
97 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
98 _debug h "$h"
99 if [ -z "$h" ]; then
100 #not valid
101 return 1
102 fi
103
104 hostedzone="$(echo "$response" | _egrep_o "{.*\"name\":\s*\"$h\".*}")"
105 if [ "$hostedzone" ]; then
106 _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
107 if [ "$_domain_id" ]; then
108 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
109 _domain=$h
110 return 0
111 fi
112 return 1
113 fi
114 p=$i
115 i=$(_math "$i" + 1)
116 done
117 fi
118 return 1
119 }
120
121 #method uri qstr data
122 _ad_rest() {
123 mtd="$1"
124 ep="$2"
125 data="$3"
126
127 _debug mtd "$mtd"
128 _debug ep "$ep"
129
130 export _H1="Accept: application/json"
131 export _H2="Content-Type: application/json"
132
133 if [ "$mtd" != "GET" ]; then
134 # both POST and DELETE.
135 _debug data "$data"
136 response="$(_post "$data" "$AD_API_URL/$ep" "" "$mtd")"
137 else
138 response="$(_get "$AD_API_URL/$ep")"
139 fi
140
141 if [ "$?" != "0" ]; then
142 _err "error $ep"
143 return 1
144 fi
145 _debug2 response "$response"
146 return 0
147 }