]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_linode.sh
Merge pull request #4776 from KincaidYang/master
[mirror_acme.sh.git] / dnsapi / dns_linode.sh
1 #!/usr/bin/env sh
2
3 #Author: Philipp Grosswiler <philipp.grosswiler@swiss-design.net>
4
5 LINODE_API_URL="https://api.linode.com/?api_key=$LINODE_API_KEY&api_action="
6
7 ######## Public functions #####################
8
9 #Usage: dns_linode_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
10 dns_linode_add() {
11 fulldomain="${1}"
12 txtvalue="${2}"
13
14 if ! _Linode_API; then
15 return 1
16 fi
17
18 _info "Using Linode"
19 _debug "Calling: dns_linode_add() '${fulldomain}' '${txtvalue}'"
20
21 _debug "First detect the root zone"
22 if ! _get_root "$fulldomain"; then
23 _err "Domain does not exist."
24 return 1
25 fi
26 _debug _domain_id "$_domain_id"
27 _debug _sub_domain "$_sub_domain"
28 _debug _domain "$_domain"
29
30 _parameters="&DomainID=$_domain_id&Type=TXT&Name=$_sub_domain&Target=$txtvalue"
31
32 if _rest GET "domain.resource.create" "$_parameters" && [ -n "$response" ]; then
33 _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"ResourceID\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
34 _debug _resource_id "$_resource_id"
35
36 if [ -z "$_resource_id" ]; then
37 _err "Error adding the domain resource."
38 return 1
39 fi
40
41 _info "Domain resource successfully added."
42 return 0
43 fi
44
45 return 1
46 }
47
48 #Usage: dns_linode_rm _acme-challenge.www.domain.com
49 dns_linode_rm() {
50 fulldomain="${1}"
51
52 if ! _Linode_API; then
53 return 1
54 fi
55
56 _info "Using Linode"
57 _debug "Calling: dns_linode_rm() '${fulldomain}'"
58
59 _debug "First detect the root zone"
60 if ! _get_root "$fulldomain"; then
61 _err "Domain does not exist."
62 return 1
63 fi
64 _debug _domain_id "$_domain_id"
65 _debug _sub_domain "$_sub_domain"
66 _debug _domain "$_domain"
67
68 _parameters="&DomainID=$_domain_id"
69
70 if _rest GET "domain.resource.list" "$_parameters" && [ -n "$response" ]; then
71 response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")"
72
73 resource="$(echo "$response" | _egrep_o "{.*\"NAME\":\s*\"$_sub_domain\".*}")"
74 if [ "$resource" ]; then
75 _resource_id=$(printf "%s\n" "$resource" | _egrep_o "\"RESOURCEID\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
76 if [ "$_resource_id" ]; then
77 _debug _resource_id "$_resource_id"
78
79 _parameters="&DomainID=$_domain_id&ResourceID=$_resource_id"
80
81 if _rest GET "domain.resource.delete" "$_parameters" && [ -n "$response" ]; then
82 _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"ResourceID\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
83 _debug _resource_id "$_resource_id"
84
85 if [ -z "$_resource_id" ]; then
86 _err "Error deleting the domain resource."
87 return 1
88 fi
89
90 _info "Domain resource successfully deleted."
91 return 0
92 fi
93 fi
94
95 return 1
96 fi
97
98 return 0
99 fi
100
101 return 1
102 }
103
104 #################### Private functions below ##################################
105
106 _Linode_API() {
107 if [ -z "$LINODE_API_KEY" ]; then
108 LINODE_API_KEY=""
109
110 _err "You didn't specify the Linode API key yet."
111 _err "Please create your key and try again."
112
113 return 1
114 fi
115
116 _saveaccountconf LINODE_API_KEY "$LINODE_API_KEY"
117 }
118
119 #################### Private functions below ##################################
120 #_acme-challenge.www.domain.com
121 #returns
122 # _sub_domain=_acme-challenge.www
123 # _domain=domain.com
124 # _domain_id=12345
125 _get_root() {
126 domain=$1
127 i=2
128 p=1
129
130 if _rest GET "domain.list"; then
131 response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")"
132 while true; do
133 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
134 _debug h "$h"
135 if [ -z "$h" ]; then
136 #not valid
137 return 1
138 fi
139
140 hostedzone="$(echo "$response" | _egrep_o "{.*\"DOMAIN\":\s*\"$h\".*}")"
141 if [ "$hostedzone" ]; then
142 _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"DOMAINID\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
143 if [ "$_domain_id" ]; then
144 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
145 _domain=$h
146 return 0
147 fi
148 return 1
149 fi
150 p=$i
151 i=$(_math "$i" + 1)
152 done
153 fi
154 return 1
155 }
156
157 #method method action data
158 _rest() {
159 mtd="$1"
160 ep="$2"
161 data="$3"
162
163 _debug mtd "$mtd"
164 _debug ep "$ep"
165
166 export _H1="Accept: application/json"
167 export _H2="Content-Type: application/json"
168
169 if [ "$mtd" != "GET" ]; then
170 # both POST and DELETE.
171 _debug data "$data"
172 response="$(_post "$data" "$LINODE_API_URL$ep" "" "$mtd")"
173 else
174 response="$(_get "$LINODE_API_URL$ep$data")"
175 fi
176
177 if [ "$?" != "0" ]; then
178 _err "error $ep"
179 return 1
180 fi
181 _debug2 response "$response"
182 return 0
183 }