]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_selfhost.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_selfhost.sh
CommitLineData
cc5cfc75
M
1#!/usr/bin/env sh
2#
3# Author: Marvin Edeler
4# Report Bugs here: https://github.com/Marvo2011/acme.sh/issues/1
2982e994 5# Last Edit: 17.02.2022
cc5cfc75
M
6
7dns_selfhost_add() {
2cf72bad 8 fulldomain=$1
cc5cfc75
M
9 txt=$2
10 _info "Calling acme-dns on selfhost"
2cf72bad 11 _debug fulldomain "$fulldomain"
cc5cfc75
M
12 _debug txtvalue "$txt"
13
14 SELFHOSTDNS_UPDATE_URL="https://selfhost.de/cgi-bin/api.pl"
610c3cf6
A
15
16 # Get values, but don't save until we successfully validated
cc5cfc75
M
17 SELFHOSTDNS_USERNAME="${SELFHOSTDNS_USERNAME:-$(_readaccountconf_mutable SELFHOSTDNS_USERNAME)}"
18 SELFHOSTDNS_PASSWORD="${SELFHOSTDNS_PASSWORD:-$(_readaccountconf_mutable SELFHOSTDNS_PASSWORD)}"
610c3cf6 19 # These values are domain dependent, so read them from there
b9256a1b 20 SELFHOSTDNS_MAP="${SELFHOSTDNS_MAP:-$(_readdomainconf SELFHOSTDNS_MAP)}"
c94f9f21 21 # Selfhost api can't dynamically add TXT record,
fc336e37 22 # so we have to store the last used RID of the domain to support a second RID for wildcard domains
f9320fff 23 # (format: 'fulldomainA:lastRid fulldomainB:lastRid ...')
fc336e37 24 SELFHOSTDNS_MAP_LAST_USED_INTERNAL=$(_readdomainconf SELFHOSTDNS_MAP_LAST_USED_INTERNAL)
cc5cfc75 25
96d45cc3
A
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
35ec3ada 31 # get the domain entry from SELFHOSTDNS_MAP
2cf72bad
A
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
2cf72bad 34 # if the domain is defined multiple times only the last occurance will be matched
35ec3ada 35 mapEntry=$(echo "$SELFHOSTDNS_MAP" | sed -n -E "s/(^|^.*[[:space:]])($fulldomain)(:[[:digit:]]+)([:]?[[:digit:]]*)(.*)/\2\3\4/p")
fc336e37 36 _debug2 mapEntry "$mapEntry"
35ec3ada
A
37 if test -z "$mapEntry"; then
38 _err "SELFHOSTDNS_MAP must contain the fulldomain incl. prefix and at least one RID"
39 return 1
cc5cfc75
M
40 fi
41
35ec3ada
A
42 # get the RIDs from the map entry
43 rid1=$(echo "$mapEntry" | cut -d: -f2)
35ec3ada 44 rid2=$(echo "$mapEntry" | cut -d: -f3)
35ec3ada 45
fc336e37 46 # read last used rid domain
f9320fff 47 lastUsedRidForDomainEntry=$(echo "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL" | sed -n -E "s/(^|^.*[[:space:]])($fulldomain:[[:digit:]]+)(.*)/\2/p")
fc336e37 48 _debug2 lastUsedRidForDomainEntry "$lastUsedRidForDomainEntry"
f9320fff 49 lastUsedRidForDomain=$(echo "$lastUsedRidForDomainEntry" | cut -d: -f2)
fc336e37
A
50
51 rid="$rid1"
52 if [ "$lastUsedRidForDomain" = "$rid" ] && ! test -z "$rid2"; then
53 rid="$rid2"
54 fi
55
7a1f94bc
A
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
f9320fff 67 newLastUsedRidForDomainEntry="$fulldomain:$rid"
fc336e37
A
68 if ! test -z "$lastUsedRidForDomainEntry"; then
69 # replace last used rid entry for domain
f9320fff 70 SELFHOSTDNS_MAP_LAST_USED_INTERNAL=$(echo "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL" | sed -n -E "s/$lastUsedRidForDomainEntry/$newLastUsedRidForDomainEntry/p")
c94f9f21 71 else
fc336e37 72 # add last used rid entry for domain
f9320fff
A
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
96d45cc3
A
78 fi
79
610c3cf6
A
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
b9256a1b 84 _savedomainconf SELFHOSTDNS_MAP "$SELFHOSTDNS_MAP"
fc336e37 85 _savedomainconf SELFHOSTDNS_MAP_LAST_USED_INTERNAL "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL"
cc5cfc75
M
86}
87
d6c68f1a 88dns_selfhost_rm() {
2cf72bad 89 fulldomain=$1
cc5cfc75 90 txt=$2
2cf72bad 91 _debug fulldomain "$fulldomain"
cc5cfc75 92 _debug txtvalue "$txt"
d6c68f1a 93 _info "Creating and removing of records is not supported by selfhost API, will not delete anything."
cc5cfc75 94}