]>
Commit | Line | Data |
---|---|---|
e0deca33 RH |
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") | |
f0d6d467 | 6 | #See http://developer.leaseweb.com for more information. |
e0deca33 RH |
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 | ||
54b38086 | 36 | if _lsw_api "POST" "$_domain" "$fulldomain" "$txtvalue"; then |
e0deca33 RH |
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 | |
4a81205e | 63 | |
e0deca33 RH |
64 | _debug _root_domain "$_domain" |
65 | _debug _domain "$fulldomain" | |
66 | ||
54b38086 | 67 | if _lsw_api "DELETE" "$_domain" "$fulldomain" "$txtvalue"; then |
e0deca33 RH |
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 | ||
e0deca33 RH |
81 | #################### Private functions below ################################## |
82 | # _acme-challenge.www.domain.com | |
83 | # returns | |
84 | # _domain=domain.com | |
85 | _get_root() { | |
e10f447b RH |
86 | rdomain=$1 |
87 | i="$(echo "$rdomain" | tr '.' ' ' | wc -w)" | |
e0deca33 RH |
88 | i=$(_math "$i" - 1) |
89 | ||
90 | while true; do | |
58642286 | 91 | h=$(printf "%s" "$rdomain" | cut -d . -f "$i"-100) |
e10f447b | 92 | _debug h "$h" |
e0deca33 | 93 | if [ -z "$h" ]; then |
1d1f6161 RH |
94 | return 1 #not valid domain |
95 | fi | |
96 | ||
97 | #Check API if domain exists | |
98 | if _lsw_api "GET" "$h"; then | |
e10f447b | 99 | if [ "$_code" = "200" ]; then |
1d1f6161 RH |
100 | _domain="$h" |
101 | return 0 | |
102 | fi | |
103 | fi | |
104 | i=$(_math "$i" - 1) | |
e48daffa | 105 | if [ "$i" -lt 2 ]; then |
6d62ae22 | 106 | return 1 #not found, no need to check _acme-challenge.sub.domain in leaseweb api. |
e0deca33 | 107 | fi |
e0deca33 | 108 | done |
1d1f6161 | 109 | |
e0deca33 RH |
110 | return 1 |
111 | } | |
112 | ||
113 | _lsw_api() { | |
114 | cmd=$1 | |
14f6f9ec | 115 | d=$2 |
e10f447b RH |
116 | fd=$3 |
117 | tvalue=$4 | |
e0deca33 RH |
118 | |
119 | # Construct the HTTP Authorization header | |
120 | export _H2="Content-Type: application/json" | |
121 | export _H1="X-Lsw-Auth: ${LSW_Key}" | |
122 | ||
1d1f6161 | 123 | if [ "$cmd" = "GET" ]; then |
e10f447b | 124 | response="$(_get "$LSW_API/$d")" |
1d1f6161 RH |
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 | ||
54b38086 | 131 | if [ "$cmd" = "POST" ]; then |
e10f447b RH |
132 | data="{\"name\": \"$fd.\",\"type\": \"TXT\",\"content\": [\"$tvalue\"],\"ttl\": 60}" |
133 | response="$(_post "$data" "$LSW_API/$d/resourceRecordSets" "$data" "POST")" | |
e0deca33 | 134 | _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")" |
54b38086 RH |
135 | _debug "http response code $_code" |
136 | _debug response "$response" | |
e0deca33 RH |
137 | return 0 |
138 | fi | |
139 | ||
54b38086 | 140 | if [ "$cmd" = "DELETE" ]; then |
e10f447b | 141 | response="$(_post "" "$LSW_API/$d/resourceRecordSets/$fd/TXT" "" "DELETE")" |
e0deca33 RH |
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 | |
54b38086 | 149 | } |