]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_leaseweb.sh
Styling, newline removed
[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 ######## Public functions #####################
7
8 LSW_API="https://api.leaseweb.com/hosting/v2/domains/"
9
10 #Usage: dns_leaseweb_add _acme-challenge.www.domain.com
11 dns_leaseweb_add() {
12 fulldomain=$1
13 txtvalue=$2
14
15 LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
16 if [ -z "$LSW_Key" ]; then
17 LSW_Key=""
18 _err "You don't specify Leaseweb api key yet."
19 _err "Please create your key and try again."
20 return 1
21 fi
22
23 #save the api key to the account conf file.
24 _saveaccountconf_mutable LSW_Key "$LSW_Key"
25
26 _debug "First detect the root zone"
27 if ! _get_root "$fulldomain"; then
28 _err "invalid domain"
29 return 1
30 fi
31
32 _debug _root_domain "$_domain"
33 _debug _domain "$fulldomain"
34
35 if _lsw_api "POST" "$_domain" "$fulldomain" "$txtvalue"; then
36 if [ "$_code" = "201" ]; then
37 _info "Added, OK"
38 return 0
39 else
40 _err "Add txt record error, invalid code. Code: $_code"
41 return 1
42 fi
43 fi
44 _err "Add txt record error."
45
46 return 1
47 }
48
49 #Usage: fulldomain txtvalue
50 #Remove the txt record after validation.
51 dns_leaseweb_rm() {
52 fulldomain=$1
53 txtvalue=$2
54
55 LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
56
57 _debug "First detect the root zone"
58 if ! _get_root "$fulldomain"; then
59 _err "invalid domain"
60 return 1
61 fi
62
63 _debug _root_domain "$_domain"
64 _debug _domain "$fulldomain"
65
66 if _lsw_api "DELETE" "$_domain" "$fulldomain" "$txtvalue"; then
67 if [ "$_code" = "204" ]; then
68 _info "Deleted, OK"
69 return 0
70 else
71 _err "Delete txt record error."
72 return 1
73 fi
74 fi
75 _err "Delete txt record error."
76
77 return 1
78 }
79
80 #################### Private functions below ##################################
81 # _acme-challenge.www.domain.com
82 # returns
83 # _domain=domain.com
84 _get_root() {
85 domain=$1
86 i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
87 i=$(_math "$i" - 1)
88
89 while true; do
90 h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
91 if [ -z "$h" ]; then
92 return 1
93 fi
94 _domain="$h"
95 return 0
96 done
97 _debug "$domain not found"
98 return 1
99 }
100
101 _lsw_api() {
102 cmd=$1
103 domain=$2
104 fulldomain=$3
105 txtvalue=$4
106
107 # Construct the HTTP Authorization header
108 export _H2="Content-Type: application/json"
109 export _H1="X-Lsw-Auth: ${LSW_Key}"
110
111 if [ "$cmd" = "POST" ]; then
112 data="{\"name\": \"$fulldomain.\",\"type\": \"TXT\",\"content\": [\"$txtvalue\"],\"ttl\": 60}"
113 response="$(_post "$data" "$LSW_API/$domain/resourceRecordSets" "$data" "POST")"
114 _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
115 _debug "http response code $_code"
116 _debug response "$response"
117 return 0
118 fi
119
120 if [ "$cmd" = "DELETE" ]; then
121 response="$(_post "" "$LSW_API/$domain/resourceRecordSets/$fulldomain/TXT" "" "DELETE")"
122 _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
123 _debug "http response code $_code"
124 _debug response "$response"
125 return 0
126 fi
127
128 return 1
129 }