You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
4.1 KiB

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