]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_kinghost.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_kinghost.sh
1 #!/usr/bin/env sh
2
3 ############################################################
4 # KingHost API support #
5 # https://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 exist
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 _kinghost_rest DELETE "dns" "name=$fulldomain&content=$txtvalue"
75 if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
76 _err "Error"
77 _err "$response"
78 return 1
79 fi
80
81 return 0
82 }
83
84 #################### Private functions below ##################################
85 _kinghost_rest() {
86 method=$1
87 uri="$2"
88 data="$3"
89 _debug "$uri"
90
91 export _H1="X-Auth-Email: $KINGHOST_Username"
92 export _H2="X-Auth-Key: $KINGHOST_Password"
93
94 if [ "$method" != "GET" ]; then
95 _debug data "$data"
96 response="$(_post "$data" "$KING_Api/$uri.json" "" "$method")"
97 else
98 response="$(_get "$KING_Api/$uri.json?$data")"
99 fi
100
101 if [ "$?" != "0" ]; then
102 _err "error $uri"
103 return 1
104 fi
105 _debug2 response "$response"
106 return 0
107 }