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.

176 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="$(
  86. printf -- '%s' "$response"
  87. \ | sed 's/^\(.*TXT":"\)\([^,}]*\)\(.*\)$/\2/;s/.$//;s/\\"/"/g;s/\\n/\n/g'
  88. )"
  89. _debug2 'Clean' "$response"
  90. }
  91. # Executes an HTTP GET or POST request for getting or setting DNS records,
  92. # containing given payload upon POST.
  93. # Usage: _dns [GET | SET] [payload]
  94. _dns() {
  95. action="$1"
  96. payload="$(printf -- '%s' "$2" | _url_encode)"
  97. url="$(
  98. printf -- '%s%s' "$AF_URL_DNS" "$domain"
  99. \ | sed 's/{\*}/'"$(printf -- '%s' "$action" | _lower_case)"'/'
  100. )"
  101. if [ "$action" = 'SET' ]; then
  102. _debug2 'Payload' "$payload"
  103. response="$(_post '' "$url&TXT=$payload" '' 'POST' 'application/x-www-form-urlencoded')"
  104. else
  105. response="$(_get "$url" '' 10)"
  106. fi
  107. if ! _contains "$response" "$AF_API_SUCCESS"; then
  108. _err "DNS API error: $response"
  109. return 1
  110. fi
  111. _debug 'Response' "$response"
  112. return 0
  113. }
  114. # Gets the root domain zone for given domain.
  115. # Usage: _get_zone _acme-challenge.www.example.com
  116. _get_zone() {
  117. _info 'Getting domain zone...'
  118. _debug2 'Initial FQDN' "$1"
  119. fqdn="$1"
  120. fqdn="${fqdn#*.}" # Strip "_acme-challenge" right away
  121. _debug2 'Reduced FQDN' "$fqdn"
  122. domains="$(_get "$AF_URL_DOMAINS" '' 10)"
  123. while true; do
  124. if _contains "$domains" "$fqdn"; then
  125. domain="$fqdn"
  126. _info "Found root domain zone: $domain"
  127. break
  128. else
  129. fqdn="${fqdn#*.}"
  130. _debug2 'FQDN' "$fqdn"
  131. fi
  132. done
  133. if [ "$domain" = "$fqdn" ]; then
  134. return 0
  135. fi
  136. _err "Couldn't find root domain zone."
  137. return 1
  138. }
  139. # Adds the HTTP Authorization & Content-Type headers to a follow-up request.
  140. # Usage: _set_headers
  141. _set_headers() {
  142. encoded="$(printf -- '%s:%s' "$AF_API_USERNAME" "$AF_API_PASSWORD" | _base64)"
  143. export _H1="Authorization: Basic $encoded"
  144. export _H2='Content-Type: application/json'
  145. }