]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_ad.sh
Merge pull request #3734 from acmesh-official/dev
[mirror_acme.sh.git] / dnsapi / dns_ad.sh
CommitLineData
e5079b9d
PK
1#!/usr/bin/env sh
2
3#
4#AD_API_KEY="sdfsdfsdfljlbjkljlkjsdfoiwje"
5
6#This is the Alwaysdata api wrapper for acme.sh
831b95f1
PK
7#
8#Author: Paul Koppen
9#Report Bugs here: https://github.com/wpk-/acme.sh
e5079b9d 10
331b599a 11AD_API_URL="https://$AD_API_KEY:@api.alwaysdata.com/v1"
e5079b9d
PK
12
13######## Public functions #####################
14
15#Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16dns_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\"}"
7b4be7be 39
331b599a 40 if _ad_rest POST "record/" "$_ad_tmpl_json" && [ -z "$response" ]; then
e5079b9d
PK
41 _info "txt record updated success."
42 return 0
43 fi
7b4be7be 44
e5079b9d
PK
45 return 1
46}
47
48#fulldomain txtvalue
49dns_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
331b599a
PK
62 _debug "Getting txt records"
63 _ad_rest GET "record/?domain=$_domain_id&name=$_sub_domain"
7b4be7be 64
331b599a 65 if [ -n "$response" ]; then
b90917a5 66 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
331b599a
PK
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
e5079b9d 78 fi
e5079b9d
PK
79
80 return 1
81}
82
83#################### Private functions below ##################################
331b599a
PK
84#_acme-challenge.www.domain.com
85#returns
86# _sub_domain=_acme-challenge.www
87# _domain=domain.com
88# _domain_id=12345
e5079b9d
PK
89_get_root() {
90 domain=$1
91 i=2
92 p=1
93
331b599a
PK
94 if _ad_rest GET "domain/"; then
95 response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
e5079b9d
PK
96 while true; do
97 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
331b599a 98 _debug h "$h"
e5079b9d
PK
99 if [ -z "$h" ]; then
100 #not valid
101 return 1
102 fi
103
331b599a
PK
104 hostedzone="$(echo "$response" | _egrep_o "{.*\"name\":\s*\"$h\".*}")"
105 if [ "$hostedzone" ]; then
831b95f1 106 _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
e5079b9d
PK
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
331b599a 122_ad_rest() {
e5079b9d
PK
123 mtd="$1"
124 ep="$2"
331b599a 125 data="$3"
e5079b9d
PK
126
127 _debug mtd "$mtd"
128 _debug ep "$ep"
e5079b9d 129
3ca93f4a
BB
130 export _H1="Accept: application/json"
131 export _H2="Content-Type: application/json"
7b4be7be 132
331b599a
PK
133 if [ "$mtd" != "GET" ]; then
134 # both POST and DELETE.
135 _debug data "$data"
136 response="$(_post "$data" "$AD_API_URL/$ep" "" "$mtd")"
e5079b9d 137 else
331b599a 138 response="$(_get "$AD_API_URL/$ep")"
e5079b9d
PK
139 fi
140
331b599a
PK
141 if [ "$?" != "0" ]; then
142 _err "error $ep"
143 return 1
e5079b9d 144 fi
331b599a
PK
145 _debug2 response "$response"
146 return 0
e5079b9d 147}