]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_servercow.sh
Merge pull request #3261 from NerLOR/master
[mirror_acme.sh.git] / dnsapi / dns_servercow.sh
1 #!/usr/bin/env sh
2
3 ##########
4 # Custom servercow.de DNS API v1 for use with [acme.sh](https://github.com/acmesh-official/acme.sh)
5 #
6 # Usage:
7 # export SERVERCOW_API_Username=username
8 # export SERVERCOW_API_Password=password
9 # acme.sh --issue -d example.com --dns dns_servercow
10 #
11 # Issues:
12 # Any issues / questions / suggestions can be posted here:
13 # https://github.com/jhartlep/servercow-dns-api/issues
14 #
15 # Author: Jens Hartlep
16 ##########
17
18 SERVERCOW_API="https://api.servercow.de/dns/v1/domains"
19
20 # Usage dns_servercow_add _acme-challenge.www.domain.com "abcdefghijklmnopqrstuvwxyz"
21 dns_servercow_add() {
22 fulldomain=$1
23 txtvalue=$2
24
25 _info "Using servercow"
26 _debug fulldomain "$fulldomain"
27 _debug txtvalue "$txtvalue"
28
29 SERVERCOW_API_Username="${SERVERCOW_API_Username:-$(_readaccountconf_mutable SERVERCOW_API_Username)}"
30 SERVERCOW_API_Password="${SERVERCOW_API_Password:-$(_readaccountconf_mutable SERVERCOW_API_Password)}"
31 if [ -z "$SERVERCOW_API_Username" ] || [ -z "$SERVERCOW_API_Password" ]; then
32 SERVERCOW_API_Username=""
33 SERVERCOW_API_Password=""
34 _err "You don't specify servercow api username and password yet."
35 _err "Please create your username and password and try again."
36 return 1
37 fi
38
39 # save the credentials to the account conf file
40 _saveaccountconf_mutable SERVERCOW_API_Username "$SERVERCOW_API_Username"
41 _saveaccountconf_mutable SERVERCOW_API_Password "$SERVERCOW_API_Password"
42
43 _debug "First detect the root zone"
44 if ! _get_root "$fulldomain"; then
45 _err "invalid domain"
46 return 1
47 fi
48
49 _debug _sub_domain "$_sub_domain"
50 _debug _domain "$_domain"
51
52 if _servercow_api POST "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":20}"; then
53 if printf -- "%s" "$response" | grep "ok" >/dev/null; then
54 _info "Added, OK"
55 return 0
56 else
57 _err "add txt record error."
58 return 1
59 fi
60 fi
61 _err "add txt record error."
62
63 return 1
64 }
65
66 # Usage fulldomain txtvalue
67 # Remove the txt record after validation
68 dns_servercow_rm() {
69 fulldomain=$1
70 txtvalue=$2
71
72 _info "Using servercow"
73 _debug fulldomain "$fulldomain"
74 _debug txtvalue "$fulldomain"
75
76 SERVERCOW_API_Username="${SERVERCOW_API_Username:-$(_readaccountconf_mutable SERVERCOW_API_Username)}"
77 SERVERCOW_API_Password="${SERVERCOW_API_Password:-$(_readaccountconf_mutable SERVERCOW_API_Password)}"
78 if [ -z "$SERVERCOW_API_Username" ] || [ -z "$SERVERCOW_API_Password" ]; then
79 SERVERCOW_API_Username=""
80 SERVERCOW_API_Password=""
81 _err "You don't specify servercow api username and password yet."
82 _err "Please create your username and password and try again."
83 return 1
84 fi
85
86 _debug "First detect the root zone"
87 if ! _get_root "$fulldomain"; then
88 _err "invalid domain"
89 return 1
90 fi
91
92 _debug _sub_domain "$_sub_domain"
93 _debug _domain "$_domain"
94
95 if _servercow_api DELETE "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\"}"; then
96 if printf -- "%s" "$response" | grep "ok" >/dev/null; then
97 _info "Deleted, OK"
98 _contains "$response" '"message":"ok"'
99 else
100 _err "delete txt record error."
101 return 1
102 fi
103 fi
104
105 }
106
107 #################### Private functions below ##################################
108
109 # _acme-challenge.www.domain.com
110 # returns
111 # _sub_domain=_acme-challenge.www
112 # _domain=domain.com
113 _get_root() {
114 fulldomain=$1
115 i=2
116 p=1
117
118 while true; do
119 _domain=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
120
121 _debug _domain "$_domain"
122 if [ -z "$_domain" ]; then
123 # not valid
124 return 1
125 fi
126
127 if ! _servercow_api GET "$_domain"; then
128 return 1
129 fi
130
131 if ! _contains "$response" '"error":"no such domain in user context"' >/dev/null; then
132 _sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-$p)
133 if [ -z "$_sub_domain" ]; then
134 # not valid
135 return 1
136 fi
137
138 return 0
139 fi
140
141 p=$i
142 i=$(_math "$i" + 1)
143 done
144
145 return 1
146 }
147
148 _servercow_api() {
149 method=$1
150 domain=$2
151 data="$3"
152
153 export _H1="Content-Type: application/json"
154 export _H2="X-Auth-Username: $SERVERCOW_API_Username"
155 export _H3="X-Auth-Password: $SERVERCOW_API_Password"
156
157 if [ "$method" != "GET" ]; then
158 _debug data "$data"
159 response="$(_post "$data" "$SERVERCOW_API/$domain" "" "$method")"
160 else
161 response="$(_get "$SERVERCOW_API/$domain")"
162 fi
163
164 if [ "$?" != "0" ]; then
165 _err "error $domain"
166 return 1
167 fi
168 _debug2 response "$response"
169 return 0
170 }