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