dns_lexicon.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env sh
  2. # dns api wrapper of lexicon for acme.sh
  3. # https://github.com/AnalogJ/lexicon
  4. lexicon_cmd="lexicon"
  5. wiki="https://github.com/Neilpang/acme.sh/wiki/How-to-use-lexicon-dns-api"
  6. _lexicon_init() {
  7. if ! _exists "$lexicon_cmd"; then
  8. _err "Please install $lexicon_cmd first: $wiki"
  9. return 1
  10. fi
  11. PROVIDER="${PROVIDER:-$(_readdomainconf PROVIDER)}"
  12. if [ -z "$PROVIDER" ]; then
  13. PROVIDER=""
  14. _err "Please define env PROVIDER first: $wiki"
  15. return 1
  16. fi
  17. _savedomainconf PROVIDER "$PROVIDER"
  18. export PROVIDER
  19. # e.g. busybox-ash does not know [:upper:]
  20. # shellcheck disable=SC2018,SC2019
  21. Lx_name=$(echo LEXICON_"${PROVIDER}"_USERNAME | tr 'a-z' 'A-Z')
  22. eval "$Lx_name=\${$Lx_name:-$(_readaccountconf_mutable "$Lx_name")}"
  23. Lx_name_v=$(eval echo \$"$Lx_name")
  24. _secure_debug "$Lx_name" "$Lx_name_v"
  25. if [ "$Lx_name_v" ]; then
  26. _saveaccountconf_mutable "$Lx_name" "$Lx_name_v"
  27. eval export "$Lx_name"
  28. fi
  29. # shellcheck disable=SC2018,SC2019
  30. Lx_token=$(echo LEXICON_"${PROVIDER}"_TOKEN | tr 'a-z' 'A-Z')
  31. eval "$Lx_token=\${$Lx_token:-$(_readaccountconf_mutable "$Lx_token")}"
  32. Lx_token_v=$(eval echo \$"$Lx_token")
  33. _secure_debug "$Lx_token" "$Lx_token_v"
  34. if [ "$Lx_token_v" ]; then
  35. _saveaccountconf_mutable "$Lx_token" "$Lx_token_v"
  36. eval export "$Lx_token"
  37. fi
  38. # shellcheck disable=SC2018,SC2019
  39. Lx_password=$(echo LEXICON_"${PROVIDER}"_PASSWORD | tr 'a-z' 'A-Z')
  40. eval "$Lx_password=\${$Lx_password:-$(_readaccountconf_mutable "$Lx_password")}"
  41. Lx_password_v=$(eval echo \$"$Lx_password")
  42. _secure_debug "$Lx_password" "$Lx_password_v"
  43. if [ "$Lx_password_v" ]; then
  44. _saveaccountconf_mutable "$Lx_password" "$Lx_password_v"
  45. eval export "$Lx_password"
  46. fi
  47. # shellcheck disable=SC2018,SC2019
  48. Lx_domaintoken=$(echo LEXICON_"${PROVIDER}"_DOMAINTOKEN | tr 'a-z' 'A-Z')
  49. eval "$Lx_domaintoken=\${$Lx_domaintoken:-$(_readaccountconf_mutable "$Lx_domaintoken")}"
  50. Lx_domaintoken_v=$(eval echo \$"$Lx_domaintoken")
  51. _secure_debug "$Lx_domaintoken" "$Lx_domaintoken_v"
  52. if [ "$Lx_domaintoken_v" ]; then
  53. _saveaccountconf_mutable "$Lx_domaintoken" "$Lx_domaintoken_v"
  54. eval export "$Lx_domaintoken"
  55. fi
  56. }
  57. ######## Public functions #####################
  58. #Usage: dns_lexicon_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  59. dns_lexicon_add() {
  60. fulldomain=$1
  61. txtvalue=$2
  62. if ! _lexicon_init; then
  63. return 1
  64. fi
  65. domain=$(printf "%s" "$fulldomain" | cut -d . -f 2-999)
  66. _secure_debug LEXICON_OPTS "$LEXICON_OPTS"
  67. _savedomainconf LEXICON_OPTS "$LEXICON_OPTS"
  68. # shellcheck disable=SC2086
  69. $lexicon_cmd "$PROVIDER" $LEXICON_OPTS create "${domain}" TXT --name="_acme-challenge.${domain}." --content="${txtvalue}"
  70. }
  71. #Usage: dns_lexicon_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  72. dns_lexicon_rm() {
  73. fulldomain=$1
  74. txtvalue=$2
  75. if ! _lexicon_init; then
  76. return 1
  77. fi
  78. domain=$(printf "%s" "$fulldomain" | cut -d . -f 2-999)
  79. # shellcheck disable=SC2086
  80. $lexicon_cmd "$PROVIDER" $LEXICON_OPTS delete "${domain}" TXT --name="_acme-challenge.${domain}." --content="${txtvalue}"
  81. }