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.

177 lines
5.1 KiB

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