]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_leaseweb.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_leaseweb.sh
1 #!/usr/bin/env sh
2
3 #Author: Rolph Haspers <r.haspers@global.leaseweb.com>
4 #Utilize leaseweb.com API to finish dns-01 verifications.
5 #Requires a Leaseweb API Key (export LSW_Key="Your Key")
6 #See https://developer.leaseweb.com for more information.
7 ######## Public functions #####################
8
9 LSW_API="https://api.leaseweb.com/hosting/v2/domains/"
10
11 #Usage: dns_leaseweb_add _acme-challenge.www.domain.com
12 dns_leaseweb_add() {
13 fulldomain=$1
14 txtvalue=$2
15
16 LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
17 if [ -z "$LSW_Key" ]; then
18 LSW_Key=""
19 _err "You don't specify Leaseweb api key yet."
20 _err "Please create your key and try again."
21 return 1
22 fi
23
24 #save the api key to the account conf file.
25 _saveaccountconf_mutable LSW_Key "$LSW_Key"
26
27 _debug "First detect the root zone"
28 if ! _get_root "$fulldomain"; then
29 _err "invalid domain"
30 return 1
31 fi
32
33 _debug _root_domain "$_domain"
34 _debug _domain "$fulldomain"
35
36 if _lsw_api "POST" "$_domain" "$fulldomain" "$txtvalue"; then
37 if [ "$_code" = "201" ]; then
38 _info "Added, OK"
39 return 0
40 else
41 _err "Add txt record error, invalid code. Code: $_code"
42 return 1
43 fi
44 fi
45 _err "Add txt record error."
46
47 return 1
48 }
49
50 #Usage: fulldomain txtvalue
51 #Remove the txt record after validation.
52 dns_leaseweb_rm() {
53 fulldomain=$1
54 txtvalue=$2
55
56 LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
57
58 _debug "First detect the root zone"
59 if ! _get_root "$fulldomain"; then
60 _err "invalid domain"
61 return 1
62 fi
63
64 _debug _root_domain "$_domain"
65 _debug _domain "$fulldomain"
66
67 if _lsw_api "DELETE" "$_domain" "$fulldomain" "$txtvalue"; then
68 if [ "$_code" = "204" ]; then
69 _info "Deleted, OK"
70 return 0
71 else
72 _err "Delete txt record error."
73 return 1
74 fi
75 fi
76 _err "Delete txt record error."
77
78 return 1
79 }
80
81 #################### Private functions below ##################################
82 # _acme-challenge.www.domain.com
83 # returns
84 # _domain=domain.com
85 _get_root() {
86 rdomain=$1
87 i="$(echo "$rdomain" | tr '.' ' ' | wc -w)"
88 i=$(_math "$i" - 1)
89
90 while true; do
91 h=$(printf "%s" "$rdomain" | cut -d . -f "$i"-100)
92 _debug h "$h"
93 if [ -z "$h" ]; then
94 return 1 #not valid domain
95 fi
96
97 #Check API if domain exists
98 if _lsw_api "GET" "$h"; then
99 if [ "$_code" = "200" ]; then
100 _domain="$h"
101 return 0
102 fi
103 fi
104 i=$(_math "$i" - 1)
105 if [ "$i" -lt 2 ]; then
106 return 1 #not found, no need to check _acme-challenge.sub.domain in leaseweb api.
107 fi
108 done
109
110 return 1
111 }
112
113 _lsw_api() {
114 cmd=$1
115 d=$2
116 fd=$3
117 tvalue=$4
118
119 # Construct the HTTP Authorization header
120 export _H2="Content-Type: application/json"
121 export _H1="X-Lsw-Auth: ${LSW_Key}"
122
123 if [ "$cmd" = "GET" ]; then
124 response="$(_get "$LSW_API/$d")"
125 _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
126 _debug "http response code $_code"
127 _debug response "$response"
128 return 0
129 fi
130
131 if [ "$cmd" = "POST" ]; then
132 data="{\"name\": \"$fd.\",\"type\": \"TXT\",\"content\": [\"$tvalue\"],\"ttl\": 60}"
133 response="$(_post "$data" "$LSW_API/$d/resourceRecordSets" "$data" "POST")"
134 _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
135 _debug "http response code $_code"
136 _debug response "$response"
137 return 0
138 fi
139
140 if [ "$cmd" = "DELETE" ]; then
141 response="$(_post "" "$LSW_API/$d/resourceRecordSets/$fd/TXT" "" "DELETE")"
142 _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
143 _debug "http response code $_code"
144 _debug response "$response"
145 return 0
146 fi
147
148 return 1
149 }