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.

234 lines
8.3 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" | _egrep_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. zoneInfo=$(echo "$result" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"name":")([^"]*)"(.*)$,\2,g' | grep "\"$rootZone\"")
  127. rootZoneName="$rootZone"
  128. subDomainName="$(echo "$domain" | sed "s,\.$rootZone,,g")"
  129. subDomainNameClean="$(echo "$domain" | sed "s,_acme-challenge.,,g")"
  130. rootZoneDomainID=$(echo "$result" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"domain_id":")([^"]*)"(.*)$,\2,g')
  131. rootZoneServiceID=$(echo "$result" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"service_id":")([^"]*)"(.*)$,\2,g')
  132. _debug2 _zoneInfo "Zone info from API : $zoneInfo"
  133. _debug2 _get_root "Root zone name : $rootZoneName"
  134. _debug2 _get_root "Root zone domain ID : $rootZoneDomainID"
  135. _debug2 _get_root "Root zone service ID: $rootZoneServiceID"
  136. _debug2 _get_root "Sub domain : $subDomainName"
  137. _debug _get_root "Found valid root domain $rootZone for $subDomainNameClean"
  138. return 0
  139. }
  140. createRecord() {
  141. fulldomain=$1
  142. txtvalue="$2"
  143. # Get root domain information - needed for DNS.Services API communication
  144. if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
  145. _get_root "$fulldomain"
  146. fi
  147. _debug2 createRecord "CNAME TXT value is: $txtvalue"
  148. # Prepare data to send to API
  149. data="{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"${txtvalue}\", \"ttl\":\"10\"}"
  150. _debug2 createRecord "data to API: $data"
  151. result=$(_post "$data" "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records" "" "POST")
  152. _debug2 createRecord "result from API: $result"
  153. if [ "$(echo "$result" | _egrep_o "\"success\":true")" = "" ]; then
  154. _err "Failed to create TXT record $fulldomain with content $txtvalue in zone $rootZoneName"
  155. _err "$result"
  156. return 1
  157. fi
  158. _info "Record \"$fulldomain TXT $txtvalue\" has been created"
  159. return 0
  160. }
  161. deleteRecord() {
  162. fulldomain=$1
  163. txtvalue=$2
  164. _log deleteRecord "Deleting $fulldomain TXT $txtvalue record"
  165. if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
  166. _get_root "$fulldomain"
  167. fi
  168. result="$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID")"
  169. recordInfo="$(echo "$result" | sed -e 's/:{/:{\n/g' -e 's/},/\n},\n/g' | grep "${txtvalue}")"
  170. recordID="$(echo "$recordInfo" | sed -e 's/:{/:{\n/g' -e 's/},/\n},\n/g' | grep "${txtvalue}" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"id":")([^"]*)"(.*)$,\2,g')"
  171. if [ -z "$recordID" ]; then
  172. _info "Record $fulldomain TXT $txtvalue not found or already deleted"
  173. return 0
  174. else
  175. _debug2 deleteRecord "Found recordID=$recordID"
  176. fi
  177. _debug2 deleteRecord "DELETE request $DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID"
  178. _log "curl DELETE request $DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID"
  179. result="$(_H1="$_H1" _H2="$_H2" _post "" "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID" "" "DELETE")"
  180. _debug2 deleteRecord "API Delete result \"$result\""
  181. _log "curl API Delete result \"$result\""
  182. # Return OK regardless
  183. return 0
  184. }