]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_kinghost.sh
fixes on dnsapi/dns_kinghost.sh and dnsapi/README.md
[mirror_acme.sh.git] / dnsapi / dns_kinghost.sh
1 #!/usr/bin/env sh
2
3 ############################################################
4 # KingHost API support #
5 # http://api.kinghost.net/doc/ #
6 # #
7 # Author: Felipe Keller Braz <felipebraz@kinghost.com.br> #
8 # Report Bugs here: https://github.com/kinghost/acme.sh #
9 # #
10 # Values to export: #
11 # export KINGHOST_Username="email@provider.com" #
12 # export KINGHOST_Password="xxxxxxxxxx" #
13 ############################################################
14
15 KING_Api="https://api.kinghost.net/acme"
16
17 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
18 # Used to add txt record
19 dns_kinghost_add() {
20 fulldomain=$1
21 txtvalue=$2
22
23 KINGHOST_Username="${KINGHOST_Username:-$(_readaccountconf_mutable KINGHOST_Username)}"
24 KINGHOST_Password="${KINGHOST_Password:-$(_readaccountconf_mutable KINGHOST_Password)}"
25 if [ -z "$KINGHOST_Username" ] || [ -z "$KINGHOST_Password" ]; then
26 KINGHOST_Username=""
27 KINGHOST_Password=""
28 _err "You don't specify KingHost api password and email yet."
29 _err "Please create you key and try again."
30 return 1
31 fi
32
33 #save the credentials to the account conf file.
34 _saveaccountconf_mutable KINGHOST_Username "$KINGHOST_Username"
35 _saveaccountconf_mutable KINGHOST_Password "$KINGHOST_Password"
36
37 _debug "Getting txt records"
38 _kinghost_rest GET "dns" "name=$fulldomain&content=$txtvalue"
39
40 #This API call returns "status":"ok" if dns record does not exists
41 #We are creating a new txt record here, so we expect the "ok" status
42 if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
43 _err "Error"
44 _err "$response"
45 return 1
46 fi
47
48 _kinghost_rest POST "dns" "name=$fulldomain&content=$txtvalue"
49 if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
50 _err "Error"
51 _err "$response"
52 return 1
53 fi
54
55 return 0
56 }
57
58 # Usage: fulldomain txtvalue
59 # Used to remove the txt record after validation
60 dns_kinghost_rm() {
61 fulldomain=$1
62 txtvalue=$2
63
64 KINGHOST_Password="${KINGHOST_Password:-$(_readaccountconf_mutable KINGHOST_Password)}"
65 KINGHOST_Username="${KINGHOST_Username:-$(_readaccountconf_mutable KINGHOST_Username)}"
66 if [ -z "$KINGHOST_Password" ] || [ -z "$KINGHOST_Username" ]; then
67 KINGHOST_Password=""
68 KINGHOST_Username=""
69 _err "You don't specify KingHost api key and email yet."
70 _err "Please create you key and try again."
71 return 1
72 fi
73
74 _debug "Getting txt records"
75 _kinghost_rest GET "dns" "name=$fulldomain&content=$txtvalue"
76
77 #This API call returns "status":"ok" if dns record does not exists
78 #We are removing a txt record here, so the record must exists
79 if echo "$response" | grep '"status":"ok"' >/dev/null; then
80 _err "Error"
81 _err "$response"
82 return 1
83 fi
84
85 _kinghost_rest DELETE "dns" "name=$fulldomain&content=$txtvalue"
86 if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
87 _err "Error"
88 _err "$response"
89 return 1
90 fi
91
92 return 0
93 }
94
95 #################### Private functions below ##################################
96 _kinghost_rest() {
97 method=$1
98 uri="$2"
99 data="$3"
100 _debug "$uri"
101
102 export _H1="X-Auth-Email: $KINGHOST_Username"
103 export _H2="X-Auth-Key: $KINGHOST_Password"
104
105 if [ "$method" != "GET" ]; then
106 _debug data "$data"
107 response="$(_post "$data" "$KING_Api/$uri.json" "" "$method")"
108 else
109 response="$(_get "$KING_Api/$uri.json?$data")"
110 fi
111
112 if [ "$?" != "0" ]; then
113 _err "error $uri"
114 return 1
115 fi
116 _debug2 response "$response"
117 return 0
118 }