2022-01-05 15:03:32 +01:00
#!/usr/bin/env sh
#
# Author: Marvin Edeler
# Report Bugs here: https://github.com/Marvo2011/acme.sh/issues/1
2022-02-17 19:02:35 +01:00
# Last Edit: 17.02.2022
2022-01-05 15:03:32 +01:00
dns_selfhost_add( ) {
2022-05-10 07:09:31 +02:00
fulldomain = $1
2022-01-05 15:03:32 +01:00
txt = $2
_info "Calling acme-dns on selfhost"
2022-05-10 07:09:31 +02:00
_debug fulldomain " $fulldomain "
2022-01-05 15:03:32 +01:00
_debug txtvalue " $txt "
SELFHOSTDNS_UPDATE_URL = "https://selfhost.de/cgi-bin/api.pl"
2022-04-29 22:26:36 +02:00
# Get values, but don't save until we successfully validated
2022-01-05 15:03:32 +01:00
SELFHOSTDNS_USERNAME = " ${ SELFHOSTDNS_USERNAME :- $( _readaccountconf_mutable SELFHOSTDNS_USERNAME) } "
SELFHOSTDNS_PASSWORD = " ${ SELFHOSTDNS_PASSWORD :- $( _readaccountconf_mutable SELFHOSTDNS_PASSWORD) } "
2022-04-29 22:26:36 +02:00
# These values are domain dependent, so read them from there
2022-08-21 23:23:47 +02:00
SELFHOSTDNS_MAP = " ${ SELFHOSTDNS_MAP :- $( _readdomainconf SELFHOSTDNS_MAP) } "
2022-08-23 00:28:52 +02:00
# Selfhost api can't dynamically add TXT record,
2022-08-23 00:04:41 +02:00
# so we have to store the last used RID of the domain to support a second RID for wildcard domains
2022-08-27 01:17:53 +02:00
# (format: 'fulldomainA:lastRid fulldomainB:lastRid ...')
2022-08-23 00:04:41 +02:00
SELFHOSTDNS_MAP_LAST_USED_INTERNAL = $( _readdomainconf SELFHOSTDNS_MAP_LAST_USED_INTERNAL)
2022-01-05 15:03:32 +01:00
2022-04-29 22:23:39 +02:00
if [ -z " ${ SELFHOSTDNS_USERNAME :- } " ] || [ -z " ${ SELFHOSTDNS_PASSWORD :- } " ] ; then
_err "SELFHOSTDNS_USERNAME and SELFHOSTDNS_PASSWORD must be set"
return 1
fi
2022-08-22 00:53:32 +02:00
# get the domain entry from SELFHOSTDNS_MAP
2022-05-10 07:09:31 +02:00
# only match full domains (at the beginning of the string or with a leading whitespace),
# e.g. don't match mytest.example.com or sub.test.example.com for test.example.com
# if the domain is defined multiple times only the last occurance will be matched
2022-08-22 00:53:32 +02:00
mapEntry = $( echo " $SELFHOSTDNS_MAP " | sed -n -E " s/(^|^.*[[:space:]])( $fulldomain )(:[[:digit:]]+)([:]?[[:digit:]]*)(.*)/\2\3\4/p " )
2022-08-23 00:04:41 +02:00
_debug2 mapEntry " $mapEntry "
2022-08-22 00:53:32 +02:00
if test -z " $mapEntry " ; then
_err "SELFHOSTDNS_MAP must contain the fulldomain incl. prefix and at least one RID"
return 1
2022-01-05 15:03:32 +01:00
fi
2022-08-22 00:53:32 +02:00
# get the RIDs from the map entry
rid1 = $( echo " $mapEntry " | cut -d: -f2)
rid2 = $( echo " $mapEntry " | cut -d: -f3)
2022-08-23 00:04:41 +02:00
# read last used rid domain
2022-08-27 01:17:53 +02:00
lastUsedRidForDomainEntry = $( echo " $SELFHOSTDNS_MAP_LAST_USED_INTERNAL " | sed -n -E " s/(^|^.*[[:space:]])( $fulldomain :[[:digit:]]+)(.*)/\2/p " )
2022-08-23 00:04:41 +02:00
_debug2 lastUsedRidForDomainEntry " $lastUsedRidForDomainEntry "
2022-08-27 01:17:53 +02:00
lastUsedRidForDomain = $( echo " $lastUsedRidForDomainEntry " | cut -d: -f2)
2022-08-23 00:04:41 +02:00
rid = " $rid1 "
if [ " $lastUsedRidForDomain " = " $rid " ] && ! test -z " $rid2 " ; then
rid = " $rid2 "
fi
2022-08-28 20:44:17 +02:00
_info " Trying to add $txt on selfhost for rid: $rid "
data = " ?username= $SELFHOSTDNS_USERNAME &password= $SELFHOSTDNS_PASSWORD &rid= $rid &content= $txt "
response = " $( _get " $SELFHOSTDNS_UPDATE_URL $data " ) "
if ! echo " $response " | grep "200 OK" >/dev/null; then
_err "Invalid response of acme-dns for selfhost"
return 1
fi
# write last used rid domain
2022-08-27 01:17:53 +02:00
newLastUsedRidForDomainEntry = " $fulldomain : $rid "
2022-08-23 00:04:41 +02:00
if ! test -z " $lastUsedRidForDomainEntry " ; then
# replace last used rid entry for domain
2022-08-27 01:17:53 +02:00
SELFHOSTDNS_MAP_LAST_USED_INTERNAL = $( echo " $SELFHOSTDNS_MAP_LAST_USED_INTERNAL " | sed -n -E " s/ $lastUsedRidForDomainEntry / $newLastUsedRidForDomainEntry /p " )
2022-08-23 00:28:52 +02:00
else
2022-08-23 00:04:41 +02:00
# add last used rid entry for domain
2022-08-27 01:17:53 +02:00
if test -z " $SELFHOSTDNS_MAP_LAST_USED_INTERNAL " ; then
SELFHOSTDNS_MAP_LAST_USED_INTERNAL = " $newLastUsedRidForDomainEntry "
else
SELFHOSTDNS_MAP_LAST_USED_INTERNAL = " $SELFHOSTDNS_MAP_LAST_USED_INTERNAL $newLastUsedRidForDomainEntry "
fi
2022-04-29 22:23:39 +02:00
fi
2022-04-29 22:26:36 +02:00
# Now that we know the values are good, save them
_saveaccountconf_mutable SELFHOSTDNS_USERNAME " $SELFHOSTDNS_USERNAME "
_saveaccountconf_mutable SELFHOSTDNS_PASSWORD " $SELFHOSTDNS_PASSWORD "
# These values are domain dependent, so store them there
2022-08-21 23:23:47 +02:00
_savedomainconf SELFHOSTDNS_MAP " $SELFHOSTDNS_MAP "
2022-08-23 00:04:41 +02:00
_savedomainconf SELFHOSTDNS_MAP_LAST_USED_INTERNAL " $SELFHOSTDNS_MAP_LAST_USED_INTERNAL "
2022-01-05 15:03:32 +01:00
}
2022-03-28 13:03:02 +02:00
dns_selfhost_rm( ) {
2022-05-10 07:09:31 +02:00
fulldomain = $1
2022-01-05 15:03:32 +01:00
txt = $2
2022-05-10 07:09:31 +02:00
_debug fulldomain " $fulldomain "
2022-01-05 15:03:32 +01:00
_debug txtvalue " $txt "
2022-03-28 13:03:02 +02:00
_info "Creating and removing of records is not supported by selfhost API, will not delete anything."
2022-01-05 15:03:32 +01:00
}