]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_gd.sh
fix shellcheck warnings.
[mirror_acme.sh.git] / dnsapi / dns_gd.sh
1 #!/usr/bin/env sh
2
3 #Godaddy domain api
4 #
5 #GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
6 #
7 #GD_Secret="asdfsdfsfsdfsdfdfsdf"
8
9 GD_Api="https://api.godaddy.com/v1"
10
11 ######## Public functions #####################
12
13 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
14 dns_gd_add() {
15 fulldomain=$1
16 txtvalue=$2
17
18 if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ]; then
19 _err "You don't specify godaddy api key and secret yet."
20 _err "Please create you key and try again."
21 return 1
22 fi
23
24 #save the api key and email to the account conf file.
25 _saveaccountconf GD_Key "$GD_Key"
26 _saveaccountconf GD_Secret "$GD_Secret"
27
28 _debug "First detect the root zone"
29 if ! _get_root "$fulldomain"; then
30 _err "invalid domain"
31 return 1
32 fi
33 _debug _domain_id "$_domain_id"
34 _debug _sub_domain "$_sub_domain"
35 _debug _domain "$_domain"
36
37 _info "Adding record"
38 if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[{\"data\":\"$txtvalue\"}]"; then
39 if [ "$response" = "{}" ]; then
40 _info "Added, sleeping 10 seconds"
41 sleep 10
42 #todo: check if the record takes effect
43 return 0
44 else
45 _err "Add txt record error."
46 _err "$response"
47 return 1
48 fi
49 fi
50 _err "Add txt record error."
51
52 }
53
54 #fulldomain
55 dns_gd_rm() {
56 fulldomain=$1
57
58 }
59
60 #################### Private functions bellow ##################################
61 #_acme-challenge.www.domain.com
62 #returns
63 # _sub_domain=_acme-challenge.www
64 # _domain=domain.com
65 # _domain_id=sdjkglgdfewsdfg
66 _get_root() {
67 domain=$1
68 i=2
69 p=1
70 while true; do
71 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
72 if [ -z "$h" ]; then
73 #not valid
74 return 1
75 fi
76
77 if ! _gd_rest GET "domains/$h"; then
78 return 1
79 fi
80
81 if _contains "$response" '"code":"NOT_FOUND"'; then
82 _debug "$h not found"
83 else
84 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
85 _domain="$h"
86 return 0
87 fi
88 p="$i"
89 i=$(_math "$i" + 1)
90 done
91 return 1
92 }
93
94 _gd_rest() {
95 m=$1
96 ep="$2"
97 data="$3"
98 _debug "$ep"
99
100 _H1="Authorization: sso-key $GD_Key:$GD_Secret"
101 _H2="Content-Type: application/json"
102
103 if [ "$data" ]; then
104 _debug data "$data"
105 response="$(_post "$data" "$GD_Api/$ep" "" $m)"
106 else
107 response="$(_get "$GD_Api/$ep")"
108 fi
109
110 if [ "$?" != "0" ]; then
111 _err "error $ep"
112 return 1
113 fi
114 _debug2 response "$response"
115 return 0
116 }