]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_artfiles.sh
Fix retrieval of domain zone
[mirror_acme.sh.git] / dnsapi / dns_artfiles.sh
1 #!/usr/bin/env sh
2
3 ################################################################################
4 # ACME.sh 3rd party DNS API plugin for ArtFiles.de
5 ################################################################################
6 # Author: Martin Arndt, https://troublezone.net/
7 # Released: 2022-02-27
8 # Issues: https://github.com/Eagle3386/acme.sh/issues
9 ################################################################################
10 # Usage:
11 # 1. export AF_API_USERNAME="api12345678"
12 # 2. export AF_API_PASSWORD="apiPassword"
13 # 3. acme.sh --issue -d example.com --dns dns_artfiles
14 ################################################################################
15
16 ########## API configuration ###################################################
17 AF_API_SUCCESS='status":"OK'
18 AF_URL_DCP='https://dcp.c.artfiles.de/api/'
19 AF_URL_DNS=${AF_URL_DCP}'dns/{*}_dns.html?domain='
20 AF_URL_DOMAINS=${AF_URL_BASE}'domain/get_domains.html'
21
22 ########## Public functions ####################################################
23
24 # Adds a new TXT record for given ACME challenge value & domain.
25 # Usage: dns_artfiles_add _acme-challenge.www.example.com "ACME challenge value"
26 dns_artfiles_add() {
27 domain="$1"
28 txtValue="$2"
29 _info 'Using ArtFiles.de DNS addition API'
30 _debug 'Domain' "$domain"
31 _debug 'txtValue' "$txtValue"
32
33 AF_API_USERNAME="${AF_API_USERNAME:-$(_readaccountconf_mutable AF_API_USERNAME)}"
34 AF_API_PASSWORD="${AF_API_PASSWORD:-$(_readaccountconf_mutable AF_API_PASSWORD)}"
35 if [ -z "$AF_API_USERNAME" ] || [ -z "$AF_API_PASSWORD" ]; then
36 _err 'Missing ArtFiles.de username and/or password.'
37 _err 'Please ensure both are set via export command & try again.'
38
39 return 1
40 fi
41
42 _saveaccountconf_mutable 'AF_API_USERNAME' "$AF_API_USERNAME"
43 _saveaccountconf_mutable 'AF_API_PASSWORD' "$AF_API_PASSWORD"
44
45 _set_headers
46 _get_zone "$domain"
47 _dns 'GET'
48 if ! _contains "$response" 'TXT'; then
49 _err 'Retrieving TXT records failed.'
50
51 return 1
52 fi
53
54 _clean_records
55 _dns 'SET' "$(printf -- '%s\n_acme-challenge "%s"' "$response" "$txtValue")"
56 if ! _contains "$response" "$AF_API_SUCCESS"; then
57 _err 'Adding ACME challenge value failed.'
58
59 return 1
60 fi
61 }
62
63 # Removes the existing TXT record for given ACME challenge value & domain.
64 # Usage: dns_artfiles_rm _acme-challenge.www.example.com "ACME challenge value"
65 dns_artfiles_rm() {
66 domain="$1"
67 txtValue="$2"
68 _info 'Using ArtFiles.de DNS removal API'
69 _debug 'Domain' "$domain"
70 _debug 'txtValue' "$txtValue"
71
72 _set_headers
73 _get_zone "$domain"
74 if ! _dns 'GET'; then
75 return 1
76 fi
77
78 if ! _contains "$response" "$txtValue"; then
79 _err 'Retrieved TXT records are missing given ACME challenge value.'
80
81 return 1
82 fi
83
84 _clean_records
85 response="$(printf -- '%s' "$response" | sed '$d')"
86 _dns 'SET' "$response"
87 if ! _contains "$response" "$AF_API_SUCCESS"; then
88 _err 'Removing ACME challenge value failed.'
89
90 return 1
91 fi
92 }
93
94 ########## Private functions ###################################################
95
96 # Cleans awful TXT records response of ArtFiles's API & pretty prints it.
97 # Usage: _clean_records
98 _clean_records() {
99 # Extract TXT part, strip trailing quote sign (ACME.sh API guidelines forbid
100 # usage of SED's GNU extensions, hence couldn't omit it via regex), strip '\'
101 # from '\"' & turn '\n' into real LF characters.
102 # Yup, awful API to use - but that's all we got to get this working, so... ;)
103 _debug2 'Raw ' "$response"
104 response="$(
105 printf -- '%s' "$response"
106 \ | sed 's/^\(.*TXT":"\)\([^,}]*\)\(.*\)$/\2/;s/.$//;s/\\"/"/g;s/\\n/\n/g'
107 )"
108 _debug2 'Clean' "$response"
109 }
110
111 # Executes an HTTP GET or POST request for getting or setting DNS records,
112 # containing given payload upon POST.
113 # Usage: _dns [GET | SET] [payload]
114 _dns() {
115 action="$1"
116 payload="$(printf -- '%s' "$2" | _url_encode)"
117 url="$(
118 printf -- '%s%s' "$AF_URL_DNS" "$domain"
119 \ | sed 's/{\*}/'"$(printf -- '%s' "$action" | _lower_case)"'/'
120 )"
121
122 if [ "$action" = 'SET' ]; then
123 _debug2 'Payload' "$payload"
124 response="$(_post '' "$url&TXT=$payload" '' 'POST' 'application/x-www-form-urlencoded')"
125 else
126 response="$(_get "$url" '' 10)"
127 fi
128
129 if ! _contains "$response" "$AF_API_SUCCESS"; then
130 _err "DNS API error: $response"
131
132 return 1
133 fi
134
135 _debug 'Response' "$response"
136
137 return 0
138 }
139
140 # Gets the root domain zone for given domain.
141 # Usage: _get_zone _acme-challenge.www.example.com
142 _get_zone() {
143 fqdn="$1"
144 domains="$(_get "$AF_URL_DOMAINS" "" 10)"
145 _info 'Getting domain zone...'
146 _debug2 'FQDN' "$fqdn"
147 _debug2 'Domains' "$domains"
148
149 while _contains "$fqdn" "."; do
150 if _contains "$domains" "$fqdn"; then
151 domain="$fqdn"
152 _info "Found root domain zone: $domain"
153 break
154 else
155 fqdn="${fqdn#*.}"
156 _debug2 'FQDN' "$fqdn"
157 fi
158 done
159
160 if [ "$domain" = "$fqdn" ]; then
161 return 0
162 fi
163
164 _err 'Couldn\'t find root domain zone.'
165
166 return 1
167 }
168
169 # Adds the HTTP Authorization & Content-Type headers to a follow-up request.
170 # Usage: _set_headers
171 _set_headers() {
172 encoded="$(printf -- '%s:%s' "$AF_API_USERNAME" "$AF_API_PASSWORD" | _base64)"
173 export _H1="Authorization: Basic $encoded"
174 export _H2='Content-Type: application/json'
175 }