]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_selfhost.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_selfhost.sh
1 #!/usr/bin/env sh
2 #
3 # Author: Marvin Edeler
4 # Report Bugs here: https://github.com/Marvo2011/acme.sh/issues/1
5 # Last Edit: 17.02.2022
6
7 dns_selfhost_add() {
8 fulldomain=$1
9 txt=$2
10 _info "Calling acme-dns on selfhost"
11 _debug fulldomain "$fulldomain"
12 _debug txtvalue "$txt"
13
14 SELFHOSTDNS_UPDATE_URL="https://selfhost.de/cgi-bin/api.pl"
15
16 # Get values, but don't save until we successfully validated
17 SELFHOSTDNS_USERNAME="${SELFHOSTDNS_USERNAME:-$(_readaccountconf_mutable SELFHOSTDNS_USERNAME)}"
18 SELFHOSTDNS_PASSWORD="${SELFHOSTDNS_PASSWORD:-$(_readaccountconf_mutable SELFHOSTDNS_PASSWORD)}"
19 # These values are domain dependent, so read them from there
20 SELFHOSTDNS_MAP="${SELFHOSTDNS_MAP:-$(_readdomainconf SELFHOSTDNS_MAP)}"
21 # Selfhost api can't dynamically add TXT record,
22 # so we have to store the last used RID of the domain to support a second RID for wildcard domains
23 # (format: 'fulldomainA:lastRid fulldomainB:lastRid ...')
24 SELFHOSTDNS_MAP_LAST_USED_INTERNAL=$(_readdomainconf SELFHOSTDNS_MAP_LAST_USED_INTERNAL)
25
26 if [ -z "${SELFHOSTDNS_USERNAME:-}" ] || [ -z "${SELFHOSTDNS_PASSWORD:-}" ]; then
27 _err "SELFHOSTDNS_USERNAME and SELFHOSTDNS_PASSWORD must be set"
28 return 1
29 fi
30
31 # get the domain entry from SELFHOSTDNS_MAP
32 # only match full domains (at the beginning of the string or with a leading whitespace),
33 # e.g. don't match mytest.example.com or sub.test.example.com for test.example.com
34 # if the domain is defined multiple times only the last occurance will be matched
35 mapEntry=$(echo "$SELFHOSTDNS_MAP" | sed -n -E "s/(^|^.*[[:space:]])($fulldomain)(:[[:digit:]]+)([:]?[[:digit:]]*)(.*)/\2\3\4/p")
36 _debug2 mapEntry "$mapEntry"
37 if test -z "$mapEntry"; then
38 _err "SELFHOSTDNS_MAP must contain the fulldomain incl. prefix and at least one RID"
39 return 1
40 fi
41
42 # get the RIDs from the map entry
43 rid1=$(echo "$mapEntry" | cut -d: -f2)
44 rid2=$(echo "$mapEntry" | cut -d: -f3)
45
46 # read last used rid domain
47 lastUsedRidForDomainEntry=$(echo "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL" | sed -n -E "s/(^|^.*[[:space:]])($fulldomain:[[:digit:]]+)(.*)/\2/p")
48 _debug2 lastUsedRidForDomainEntry "$lastUsedRidForDomainEntry"
49 lastUsedRidForDomain=$(echo "$lastUsedRidForDomainEntry" | cut -d: -f2)
50
51 rid="$rid1"
52 if [ "$lastUsedRidForDomain" = "$rid" ] && ! test -z "$rid2"; then
53 rid="$rid2"
54 fi
55
56 _info "Trying to add $txt on selfhost for rid: $rid"
57
58 data="?username=$SELFHOSTDNS_USERNAME&password=$SELFHOSTDNS_PASSWORD&rid=$rid&content=$txt"
59 response="$(_get "$SELFHOSTDNS_UPDATE_URL$data")"
60
61 if ! echo "$response" | grep "200 OK" >/dev/null; then
62 _err "Invalid response of acme-dns for selfhost"
63 return 1
64 fi
65
66 # write last used rid domain
67 newLastUsedRidForDomainEntry="$fulldomain:$rid"
68 if ! test -z "$lastUsedRidForDomainEntry"; then
69 # replace last used rid entry for domain
70 SELFHOSTDNS_MAP_LAST_USED_INTERNAL=$(echo "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL" | sed -n -E "s/$lastUsedRidForDomainEntry/$newLastUsedRidForDomainEntry/p")
71 else
72 # add last used rid entry for domain
73 if test -z "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL"; then
74 SELFHOSTDNS_MAP_LAST_USED_INTERNAL="$newLastUsedRidForDomainEntry"
75 else
76 SELFHOSTDNS_MAP_LAST_USED_INTERNAL="$SELFHOSTDNS_MAP_LAST_USED_INTERNAL $newLastUsedRidForDomainEntry"
77 fi
78 fi
79
80 # Now that we know the values are good, save them
81 _saveaccountconf_mutable SELFHOSTDNS_USERNAME "$SELFHOSTDNS_USERNAME"
82 _saveaccountconf_mutable SELFHOSTDNS_PASSWORD "$SELFHOSTDNS_PASSWORD"
83 # These values are domain dependent, so store them there
84 _savedomainconf SELFHOSTDNS_MAP "$SELFHOSTDNS_MAP"
85 _savedomainconf SELFHOSTDNS_MAP_LAST_USED_INTERNAL "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL"
86 }
87
88 dns_selfhost_rm() {
89 fulldomain=$1
90 txt=$2
91 _debug fulldomain "$fulldomain"
92 _debug txtvalue "$txt"
93 _info "Creating and removing of records is not supported by selfhost API, will not delete anything."
94 }