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.

88 lines
3.6 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/.*(;$fulldomain:[[:digit:]]+;).*/\1/p")
  41. _debug2 lastUsedRidForDomainEntry "$lastUsedRidForDomainEntry"
  42. lastUsedRidForDomain=$(echo "$lastUsedRidForDomainEntry" | tr -d ";" | cut -d: -f2)
  43. rid="$rid1"
  44. if [ "$lastUsedRidForDomain" = "$rid" ] && ! test -z "$rid2"; then
  45. rid="$rid2"
  46. fi
  47. if ! test -z "$lastUsedRidForDomainEntry"; then
  48. # replace last used rid entry for domain
  49. SELFHOSTDNS_MAP_LAST_USED_INTERNAL=$(echo "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL" | sed -n -E "s/$lastUsedRidForDomainEntry/;$fulldomain:$rid;/p")
  50. else
  51. # add last used rid entry for domain
  52. SELFHOSTDNS_MAP_LAST_USED_INTERNAL="$SELFHOSTDNS_MAP_LAST_USED_INTERNAL"";$fulldomain:$rid;"
  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. # Now that we know the values are good, save them
  62. _saveaccountconf_mutable SELFHOSTDNS_USERNAME "$SELFHOSTDNS_USERNAME"
  63. _saveaccountconf_mutable SELFHOSTDNS_PASSWORD "$SELFHOSTDNS_PASSWORD"
  64. # These values are domain dependent, so store them there
  65. _savedomainconf SELFHOSTDNS_MAP "$SELFHOSTDNS_MAP"
  66. _savedomainconf SELFHOSTDNS_MAP_LAST_USED_INTERNAL "$SELFHOSTDNS_MAP_LAST_USED_INTERNAL"
  67. }
  68. dns_selfhost_rm() {
  69. fulldomain=$1
  70. txt=$2
  71. _debug fulldomain "$fulldomain"
  72. _debug txtvalue "$txt"
  73. _info "Creating and removing of records is not supported by selfhost API, will not delete anything."
  74. }