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.

172 lines
5.2 KiB

  1. #!/usr/bin/env sh
  2. ################################################################################
  3. # ACME.sh 3rd party DNS API plugin for ArtFiles.de
  4. ################################################################################
  5. # Author: Martin Arndt, https://troublezone.net/
  6. # Released: 2022-02-27
  7. # Issues: https://github.com/acmesh-official/acme.sh/issues/XXXX
  8. ################################################################################
  9. # Usage:
  10. # 1. export AF_API_Username="api12345678"
  11. # 2. export AF_API_Password="apiPassword"
  12. # 3. acme.sh --issue -d example.com --dns dns_artfiles
  13. ################################################################################
  14. ########## API configuration ###################################################
  15. AF_API_SUCCESS='status":"OK'
  16. AF_URL_DCP='https://dcp.c.artfiles.de/api/'
  17. AF_URL_DNS=${AF_URL_DCP}'dns/{*}_dns.html?domain='
  18. AF_URL_DOMAINS=${AF_URL_BASE}'domain/get_domains.html'
  19. ########## Public functions ####################################################
  20. # Adds a new TXT record for given ACME challenge value & domain.
  21. # Usage: dns_artfiles_add _acme-challenge.www.example.com "ACME challenge value"
  22. dns_artfiles_add() {
  23. domain="$1"
  24. txtValue="$2"
  25. _info 'Using ArtFiles.de DNS addition API'
  26. _debug 'Domain' "$domain"
  27. _debug 'txtValue' "$txtValue"
  28. AF_API_USERNAME="${AF_API_USERNAME:-$(_readaccountconf_mutable AF_API_USERNAME)}"
  29. AF_API_PASSWORD="${AF_API_PASSWORD:-$(_readaccountconf_mutable AF_API_PASSWORD)}"
  30. if [ -z "$AF_API_USERNAME" ] || [ -z "$AF_API_PASSWORD" ]; then
  31. _err 'Missing ArtFiles.de username and/or password.'
  32. _err 'Please ensure both are set via export command & try again.'
  33. return 1
  34. fi
  35. _saveaccountconf_mutable 'AF_API_USERNAME' "$AF_API_USERNAME"
  36. _saveaccountconf_mutable 'AF_API_PASSWORD' "$AF_API_PASSWORD"
  37. _set_headers
  38. _get_zone "$domain"
  39. _dns 'GET'
  40. if ! _contains "$response" 'TXT'; then
  41. _err 'Retrieving TXT records failed.'
  42. return 1
  43. fi
  44. _clean_records
  45. _dns 'SET' "$(printf -- '%s\n_acme-challenge "%s"' "$response" "$txtValue")"
  46. if ! _contains "$response" "$AF_API_SUCCESS"; then
  47. _err 'Adding ACME challenge value failed.'
  48. return 1
  49. fi
  50. }
  51. # Removes the existing TXT record for given ACME challenge value & domain.
  52. # Usage: dns_artfiles_rm _acme-challenge.www.example.com "ACME challenge value"
  53. dns_artfiles_rm() {
  54. domain="$1"
  55. txtValue="$2"
  56. _info 'Using ArtFiles.de DNS removal API'
  57. _debug 'Domain' "$domain"
  58. _debug 'txtValue' "$txtValue"
  59. _set_headers
  60. _get_zone "$domain"
  61. if ! _dns 'GET'; then
  62. return 1
  63. fi
  64. if ! _contains "$response" "$txtValue"; then
  65. _err 'Retrieved TXT records are missing given ACME challenge value.'
  66. return 1
  67. fi
  68. _clean_records
  69. response="$(printf -- '%s' "$response" | sed '$d')"
  70. _dns 'SET' "$response"
  71. if ! _contains "$response" "$AF_API_SUCCESS"; then
  72. _err 'Removing ACME challenge value failed.'
  73. return 1
  74. fi
  75. }
  76. ########## Private functions ###################################################
  77. # Cleans awful TXT records response of ArtFiles's API & pretty prints it.
  78. # Usage: _clean_records
  79. _clean_records() {
  80. # Extract TXT part, strip trailing quote sign (ACME.sh API guidelines forbid
  81. # usage of SED's GNU extensions, hence couldn't omit it via regex), strip '\'
  82. # from '\"' & turn '\n' into real LF characters.
  83. # Yup, awful API to use - but that's all we got to get this working, so... ;)
  84. _debug2 'Raw ' "$response"
  85. response="$(printf -- '%s' "$response"
  86. \ | sed 's/^\(.*TXT":"\)\([^,}]*\)\(.*\)$/\2/;s/.$//;s/\\"/"/g;s/\\n/\n/g')"
  87. _debug2 'Clean' "$response"
  88. }
  89. # Executes an HTTP GET or POST request for getting or setting DNS records,
  90. # containing given payload upon POST.
  91. # Usage: _dns [GET | SET] [payload]
  92. _dns() {
  93. action="$1"
  94. payload="$(printf -- '%s' "$2" | _url_encode)"
  95. url="$(printf -- '%s%s' "$AF_URL_DNS" "$domain"
  96. \ | sed 's/{\*}/'"$(printf -- '%s' "$action" | _lower_case)"'/')"
  97. if [ "$action" = 'SET' ]; then
  98. _debug2 'Payload' "$payload"
  99. response="$(_post '' "$url&TXT=$payload" '' 'POST' 'application/x-www-form-urlencoded')"
  100. else
  101. response="$(_get "$url" '' 10)"
  102. fi
  103. if ! _contains "$response" "$AF_API_SUCCESS"; then
  104. _err "DNS API error: $response"
  105. return 1
  106. fi
  107. _debug 'Response' "$response"
  108. return 0
  109. }
  110. # Gets the root domain zone for given domain.
  111. # Usage: _get_zone _acme-challenge.www.example.com
  112. _get_zone() {
  113. _info 'Getting domain zone...'
  114. _debug2 'Initial FQDN' "$1"
  115. fqdn="$1"
  116. fqdn="${fqdn#*.}" # Strip "_acme-challenge" right away
  117. _debug2 'Reduced FQDN' "$fqdn"
  118. domains="$(_get "$AF_URL_DOMAINS" '' 10)"
  119. while true; do
  120. if _contains "$domains" "$fqdn"; then
  121. domain="$fqdn"
  122. _info "Found root domain zone: $domain"
  123. break
  124. else
  125. fqdn="${fqdn#*.}"
  126. _debug2 'FQDN' "$fqdn"
  127. fi
  128. done
  129. if [ "$domain" = "$fqdn" ]; then
  130. return 0
  131. fi
  132. _err "Couldn't find root domain zone."
  133. return 1
  134. }
  135. # Adds the HTTP Authorization & Content-Type headers to a follow-up request.
  136. # Usage: _set_headers
  137. _set_headers() {
  138. encoded="$(printf -- '%s:%s' "$AF_API_USERNAME" "$AF_API_PASSWORD" | _base64)"
  139. export _H1="Authorization: Basic $encoded"
  140. export _H2='Content-Type: application/json'
  141. }