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.

94 lines
3.9 KiB

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