]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_rackcorp.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_rackcorp.sh
1 #!/usr/bin/env sh
2
3 # Provider: RackCorp (www.rackcorp.com)
4 # Author: Stephen Dendtler (sdendtler@rackcorp.com)
5 # Report Bugs here: https://github.com/senjoo/acme.sh
6 # Alternate email contact: support@rackcorp.com
7 #
8 # You'll need an API key (Portal: ADMINISTRATION -> API)
9 # Set the environment variables as below:
10 #
11 # export RACKCORP_APIUUID="UUIDHERE"
12 # export RACKCORP_APISECRET="SECRETHERE"
13 #
14
15 RACKCORP_API_ENDPOINT="https://api.rackcorp.net/api/rest/v2.4/json.php"
16
17 ######## Public functions #####################
18
19 dns_rackcorp_add() {
20 fulldomain="$1"
21 txtvalue="$2"
22
23 _debug fulldomain="$fulldomain"
24 _debug txtvalue="$txtvalue"
25
26 if ! _rackcorp_validate; then
27 return 1
28 fi
29
30 _debug "Searching for root zone"
31 if ! _get_root "$fulldomain"; then
32 return 1
33 fi
34 _debug _lookup "$_lookup"
35 _debug _domain "$_domain"
36
37 _info "Creating TXT record."
38
39 if ! _rackcorp_api dns.record.create "\"name\":\"$_domain\",\"type\":\"TXT\",\"lookup\":\"$_lookup\",\"data\":\"$txtvalue\",\"ttl\":300"; then
40 return 1
41 fi
42
43 return 0
44 }
45
46 #Usage: fulldomain txtvalue
47 #Remove the txt record after validation.
48 dns_rackcorp_rm() {
49 fulldomain=$1
50 txtvalue=$2
51
52 _debug fulldomain="$fulldomain"
53 _debug txtvalue="$txtvalue"
54
55 if ! _rackcorp_validate; then
56 return 1
57 fi
58
59 _debug "Searching for root zone"
60 if ! _get_root "$fulldomain"; then
61 return 1
62 fi
63 _debug _lookup "$_lookup"
64 _debug _domain "$_domain"
65
66 _info "Creating TXT record."
67
68 if ! _rackcorp_api dns.record.delete "\"name\":\"$_domain\",\"type\":\"TXT\",\"lookup\":\"$_lookup\",\"data\":\"$txtvalue\""; then
69 return 1
70 fi
71
72 return 0
73 }
74
75 #################### Private functions below ##################################
76 #_acme-challenge.domain.com
77 #returns
78 # _lookup=_acme-challenge
79 # _domain=domain.com
80 _get_root() {
81 domain=$1
82 i=1
83 p=1
84 if ! _rackcorp_api dns.domain.getall "\"name\":\"$domain\""; then
85 return 1
86 fi
87 while true; do
88 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
89 _debug searchhost "$h"
90 if [ -z "$h" ]; then
91 _err "Could not find domain for record $domain in RackCorp using the provided credentials"
92 #not valid
93 return 1
94 fi
95
96 _rackcorp_api dns.domain.getall "\"exactName\":\"$h\""
97
98 if _contains "$response" "\"matches\":1"; then
99 if _contains "$response" "\"name\":\"$h\""; then
100 _lookup=$(printf "%s" "$domain" | cut -d . -f 1-$p)
101 _domain="$h"
102 return 0
103 fi
104 fi
105 p=$i
106 i=$(_math "$i" + 1)
107 done
108
109 return 1
110 }
111
112 _rackcorp_validate() {
113 RACKCORP_APIUUID="${RACKCORP_APIUUID:-$(_readaccountconf_mutable RACKCORP_APIUUID)}"
114 if [ -z "$RACKCORP_APIUUID" ]; then
115 RACKCORP_APIUUID=""
116 _err "You require a RackCorp API UUID (export RACKCORP_APIUUID=\"<api uuid>\")"
117 _err "Please login to the portal and create an API key and try again."
118 return 1
119 fi
120
121 _saveaccountconf_mutable RACKCORP_APIUUID "$RACKCORP_APIUUID"
122
123 RACKCORP_APISECRET="${RACKCORP_APISECRET:-$(_readaccountconf_mutable RACKCORP_APISECRET)}"
124 if [ -z "$RACKCORP_APISECRET" ]; then
125 RACKCORP_APISECRET=""
126 _err "You require a RackCorp API secret (export RACKCORP_APISECRET=\"<api secret>\")"
127 _err "Please login to the portal and create an API key and try again."
128 return 1
129 fi
130
131 _saveaccountconf_mutable RACKCORP_APISECRET "$RACKCORP_APISECRET"
132
133 return 0
134 }
135 _rackcorp_api() {
136 _rackcorpcmd=$1
137 _rackcorpinputdata=$2
138 _debug cmd "$_rackcorpcmd $_rackcorpinputdata"
139
140 export _H1="Accept: application/json"
141 response="$(_post "{\"APIUUID\":\"$RACKCORP_APIUUID\",\"APISECRET\":\"$RACKCORP_APISECRET\",\"cmd\":\"$_rackcorpcmd\",$_rackcorpinputdata}" "$RACKCORP_API_ENDPOINT" "" "POST")"
142
143 if [ "$?" != "0" ]; then
144 _err "error $response"
145 return 1
146 fi
147 _debug2 response "$response"
148 if _contains "$response" "\"code\":\"OK\""; then
149 _debug code "OK"
150 else
151 _debug code "FAILED"
152 response=""
153 return 1
154 fi
155 return 0
156 }