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. {
  81. # Extract TXT part, strip trailing quote sign (ACME.sh API guidelines forbid
  82. # usage of SED's GNU extensions, hence couldn't omit it via regex), strip '\'
  83. # from '\"' & turn '\n' into real LF characters.
  84. # Yup, awful API to use - but that's all we got to get this working, so... ;)
  85. _debug2 'Raw ' "$response"
  86. response="$(printf -- '%s' "$response"
  87. \ | sed 's/^\(.*TXT":"\)\([^,}]*\)\(.*\)$/\2/;s/.$//;s/\\"/"/g;s/\\n/\n/g')"
  88. _debug2 'Clean' "$response"
  89. }
  90. # Executes an HTTP GET or POST request for getting or setting DNS records,
  91. # containing given payload upon POST.
  92. # Usage: _dns [GET | SET] [payload]
  93. _dns()
  94. {
  95. action="$1"
  96. payload="$(printf -- '%s' "$2" | _url_encode)"
  97. url="$(printf -- '%s%s' "$AF_URL_DNS" "$domain"
  98. \ | sed 's/{\*}/'"$(printf -- '%s' "$action" | _lower_case)"'/')"
  99. if [ "$action" = 'SET' ]; then
  100. _debug2 'Payload' "$payload"
  101. response="$(_post '' "$url&TXT=$payload" '' 'POST' 'application/x-www-form-urlencoded')"
  102. else
  103. response="$(_get "$url" '' 10)"
  104. fi
  105. if ! _contains "$response" "$AF_API_SUCCESS"; then
  106. _err "DNS API error: $response"
  107. return 1
  108. fi
  109. _debug 'Response' "$response"
  110. return 0
  111. }
  112. # Gets the root domain zone for given domain.
  113. # Usage: _get_zone _acme-challenge.www.example.com
  114. _get_zone()
  115. {
  116. _info 'Getting domain zone...'
  117. _debug2 'Initial FQDN' "$1"
  118. fqdn="$1"
  119. fqdn="${fqdn#*.}" # Strip "_acme-challenge" right away
  120. _debug2 'Reduced FQDN' "$fqdn"
  121. domains="$(_get "$AF_URL_DOMAINS" '' 10)"
  122. while true; do
  123. if _contains "$domains" "$fqdn"; then
  124. domain="$fqdn"
  125. _info "Found root domain zone: $domain"
  126. break
  127. else
  128. fqdn="${fqdn#*.}"
  129. _debug2 'FQDN' "$fqdn"
  130. fi
  131. done
  132. if [ "$domain" = "$fqdn" ]; then
  133. return 0
  134. fi
  135. _err "Couldn't find root domain zone."
  136. return 1
  137. }
  138. # Adds the HTTP Authorization & Content-Type headers to a follow-up request.
  139. # Usage: _set_headers
  140. _set_headers()
  141. {
  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. }