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.

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