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.

231 lines
7.9 KiB

  1. #!/usr/bin/env sh
  2. #This file name is "dns_dnsservices.sh"
  3. #Script for Danish DNS registra and DNS hosting provider https://dns.services
  4. #Author: Bjarke Bruun <bbruun@gmail.com>
  5. #Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/4152
  6. # Global variable to connect to the DNS.Services API
  7. DNSServices_API=https://dns.services/api
  8. ######## Public functions #####################
  9. #Usage: dns_dnsservices_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_dnsservices_add() {
  11. fulldomain=$1
  12. txtvalue=$2
  13. _info "Using dns.services to create ACME DNS challenge"
  14. _debug2 add_fulldomain "$fulldomain"
  15. _debug2 add_txtvalue "$txtvalue"
  16. # Read username/password from environment or .acme.sh/accounts.conf
  17. DnsServices_Username="${DnsServices_Username:-$(_readaccountconf_mutable DnsServices_Username)}"
  18. DnsServices_Password="${DnsServices_Password:-$(_readaccountconf_mutable DnsServices_Password)}"
  19. if [ -z "$DnsServices_Username" ] || [ -z "$DnsServices_Password" ]; then
  20. DnsServices_Username=""
  21. DnsServices_Password=""
  22. _err "You didn't specify dns.services api username and password yet."
  23. _err "Set environment variables DnsServices_Username and DnsServices_Password"
  24. return 1
  25. fi
  26. # Setup GET/POST/DELETE headers
  27. _setup_headers
  28. #save the credentials to the account conf file.
  29. _saveaccountconf_mutable DnsServices_Username "$DnsServices_Username"
  30. _saveaccountconf_mutable DnsServices_Password "$DnsServices_Password"
  31. if ! _contains "$DnsServices_Username" "@"; then
  32. _err "It seems that the username variable DnsServices_Username has not been set/left blank"
  33. _err "or is not a valid email. Please correct and try again."
  34. return 1
  35. fi
  36. if ! _get_root "${fulldomain}"; then
  37. _err "Invalid domain ${fulldomain}"
  38. return 1
  39. fi
  40. if ! createRecord "$fulldomain" "${txtvalue}"; then
  41. _err "Error creating TXT record in domain $fulldomain in $rootZoneName"
  42. return 1
  43. fi
  44. _debug2 challenge-created "Created $fulldomain"
  45. return 0
  46. }
  47. #Usage: fulldomain txtvalue
  48. #Description: Remove the txt record after validation.
  49. dns_dnsservices_rm() {
  50. fulldomain=$1
  51. txtvalue=$2
  52. _info "Using dns.services to remove DNS record $fulldomain TXT $txtvalue"
  53. _debug rm_fulldomain "$fulldomain"
  54. _debug rm_txtvalue "$txtvalue"
  55. # Read username/password from environment or .acme.sh/accounts.conf
  56. DnsServices_Username="${DnsServices_Username:-$(_readaccountconf_mutable DnsServices_Username)}"
  57. DnsServices_Password="${DnsServices_Password:-$(_readaccountconf_mutable DnsServices_Password)}"
  58. if [ -z "$DnsServices_Username" ] || [ -z "$DnsServices_Password" ]; then
  59. DnsServices_Username=""
  60. DnsServices_Password=""
  61. _err "You didn't specify dns.services api username and password yet."
  62. _err "Set environment variables DnsServices_Username and DnsServices_Password"
  63. return 1
  64. fi
  65. # Setup GET/POST/DELETE headers
  66. _setup_headers
  67. if ! _get_root "${fulldomain}"; then
  68. _err "Invalid domain ${fulldomain}"
  69. return 1
  70. fi
  71. _debug2 rm_rootDomainInfo "found root domain $rootZoneName for $fulldomain"
  72. if ! deleteRecord "${fulldomain}" "${txtvalue}"; then
  73. _err "Error removing record: $fulldomain TXT ${txtvalue}"
  74. return 1
  75. fi
  76. return 0
  77. }
  78. #################### Private functions below ##################################
  79. _setup_headers() {
  80. # Set up API Headers for _get() and _post()
  81. # The <function>_add or <function>_rm must have been called before to work
  82. if [ -z "$DnsServices_Username" ] || [ -z "$DnsServices_Password" ]; then
  83. _err "Could not setup BASIC authentication headers, they are missing"
  84. return 1
  85. fi
  86. DnsServiceCredentials="$(printf "%s" "$DnsServices_Username:$DnsServices_Password" | _base64)"
  87. export _H1="Authorization: Basic $DnsServiceCredentials"
  88. export _H2="Content-Type: application/json"
  89. # Just return if headers are set
  90. return 0
  91. }
  92. _get_root() {
  93. domain=$1
  94. _debug2 _get_root "Get the root domain of ${domain} for DNS API"
  95. # Setup _get() and _post() headers
  96. #_setup_headers
  97. result=$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/dns")
  98. _debug2 _get_root "Got the following root domain(s) $result"
  99. _debug2 _get_root "- JSON: $result"
  100. if [ "$(echo "$result" | grep -c '"name"')" -gt "1" ]; then
  101. checkMultiZones="true"
  102. _debug2 _get_root "- multiple zones found"
  103. else
  104. checkMultiZones="false"
  105. fi
  106. # Find/isolate the root zone to work with in createRecord() and deleteRecord()
  107. rootZone=""
  108. if [ "$checkMultiZones" = "true" ]; then
  109. rootZone=$(for zone in $(echo "$result" | tr -d '\n' ' '); do
  110. if [ "$(echo "$domain" | grep "$zone")" != "" ]; then
  111. _debug2 _get_root "- trying to figure out if $zone is in $domain"
  112. echo "$zone"
  113. break
  114. fi
  115. done)
  116. else
  117. rootZone=$(echo "$result" | grep -o '"name":"[^"]*' | cut -d'"' -f4)
  118. _debug2 _get_root "- only found 1 domain in API: $rootZone"
  119. fi
  120. if [ -z "$rootZone" ]; then
  121. _err "Could not find root domain for $domain - is it correctly typed?"
  122. return 1
  123. fi
  124. # Setup variables used by other functions to communicate with DNS.Services API
  125. zoneInfo=$(echo "$result" | sed "s,\"zones,\n&,g" | grep zones | cut -d'[' -f2 | cut -d']' -f1 | tr '}' '\n' | grep "\"$rootZone\"")
  126. rootZoneName="$rootZone"
  127. subDomainName="$(echo "$domain" | sed "s,\.$rootZone,,g")"
  128. subDomainNameClean="$(echo "$domain" | sed "s,_acme-challenge.,,g")"
  129. rootZoneDomainID=$(echo "$zoneInfo" | tr ',' '\n' | grep domain_id | cut -d'"' -f4)
  130. rootZoneServiceID=$(echo "$zoneInfo" | tr ',' '\n' | grep service_id | cut -d'"' -f4)
  131. _debug2 _get_root "Root zone name : $rootZoneName"
  132. _debug2 _get_root "Root zone domain ID : $rootZoneDomainID"
  133. _debug2 _get_root "Root zone service ID: $rootZoneServiceID"
  134. _debug2 _get_root "Sub domain : $subDomainName"
  135. _debug _get_root "Found valid root domain $rootZone for $subDomainNameClean"
  136. return 0
  137. }
  138. createRecord() {
  139. fulldomain=$1
  140. txtvalue="$2"
  141. # Get root domain information - needed for DNS.Services API communication
  142. if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
  143. _get_root "$fulldomain"
  144. fi
  145. _debug2 createRecord "CNAME TXT value is: $txtvalue"
  146. # Prepare data to send to API
  147. data="{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"${txtvalue}\", \"ttl\":\"10\"}"
  148. _debug2 createRecord "data to API: $data"
  149. result=$(_post "$data" "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records" "" "POST")
  150. _debug2 createRecord "result from API: $result"
  151. if [ "$(echo "$result" | grep '"success":true')" = "" ]; then
  152. _err "Failed to create TXT record $fulldomain with content $txtvalue in zone $rootZoneName"
  153. _err "$result"
  154. return 1
  155. fi
  156. _info "Record \"$fulldomain TXT $txtvalue\" has been created"
  157. return 0
  158. }
  159. deleteRecord() {
  160. fulldomain=$1
  161. txtvalue=$2
  162. _log deleteRecord "Deleting $fulldomain TXT $txtvalue record"
  163. if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
  164. _get_root "$fulldomain"
  165. fi
  166. result="$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID")"
  167. recordInfo="$(echo "$result" | tr '}' '\n' | grep "\"name\":\"${fulldomain}" | grep "\"content\":\"" | grep "${txtvalue}")"
  168. _debug2 deleteRecord "recordInfo=$recordInfo"
  169. recordID="$(echo "$recordInfo" | tr ',' '\n' | _egrep_o "\"id\":\"[0-9]+\"" | cut -d'"' -f4)"
  170. if [ -z "$recordID" ]; then
  171. _info "Record $fulldomain TXT $txtvalue not found or already deleted"
  172. return 0
  173. else
  174. _debug2 deleteRecord "Found recordID=$recordID"
  175. fi
  176. _debug2 deleteRecord "DELETE request $DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID"
  177. result="$(_H1="$_H1" _H2="$_H2" _post "" "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID" "" "DELETE")"
  178. _debug2 deleteRecord "API Delete result \"$result\""
  179. # Return OK regardless
  180. return 0
  181. }