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.

248 lines
9.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. result2="$(printf "%s\n" "$result" | tr '[' '\n' | grep '"name"')"
  99. result3="$(printf "%s\n" "$result2" | tr '}' '\n' | grep '"name"' | sed "s,^\,,,g" | sed "s,$,},g")"
  100. useResult=""
  101. _debug2 _get_root "Got the following root domain(s) $result"
  102. _debug2 _get_root "- JSON: $result"
  103. if [ "$(printf "%s\n" "$result" | tr '}' '\n' | grep -c '"name"')" -gt "1" ]; then
  104. checkMultiZones="true"
  105. _debug2 _get_root "- multiple zones found"
  106. else
  107. checkMultiZones="false"
  108. _debug2 _get_root "- single zone found"
  109. fi
  110. # Find/isolate the root zone to work with in createRecord() and deleteRecord()
  111. rootZone=""
  112. if [ "$checkMultiZones" = "true" ]; then
  113. #rootZone=$(for x in $(printf "%s" "${result3}" | tr ',' '\n' | sed -n 's/.*"name":"\(.*\)",.*/\1/p'); do if [ "$(echo "$domain" | grep "$x")" != "" ]; then echo "$x"; fi; done)
  114. rootZone=$(for x in $(printf "%s\n" "${result3}" | tr ',' '\n' | grep name | cut -d'"' -f4); do if [ "$(echo "$domain" | grep "$x")" != "" ]; then echo "$x"; fi; done)
  115. if [ "$rootZone" != "" ]; then
  116. _debug2 _rootZone "- root zone for $domain is $rootZone"
  117. else
  118. _err "Could not find root zone for $domain, is it correctly typed?"
  119. return 1
  120. fi
  121. else
  122. rootZone=$(echo "$result" | tr '}' '\n' | _egrep_o '"name":"[^"]*' | cut -d'"' -f4)
  123. _debug2 _get_root "- only found 1 domain in API: $rootZone"
  124. fi
  125. if [ -z "$rootZone" ]; then
  126. _err "Could not find root domain for $domain - is it correctly typed?"
  127. return 1
  128. fi
  129. # Make sure we use the correct API zone data
  130. useResult="$(printf "%s\n" "${result3}" tr ',' '\n' | grep "$rootZone")"
  131. _debug2 _useResult "useResult=$useResult"
  132. # Setup variables used by other functions to communicate with DNS.Services API
  133. #zoneInfo=$(printf "%s\n" "$useResult" | sed -E 's,.*(zones)(.*),\1\2,g' | sed -E 's,^(.*"name":")([^"]*)"(.*)$,\2,g')
  134. zoneInfo=$(printf "%s\n" "$useResult" | tr ',' '\n' | grep '"name"' | cut -d'"' -f4)
  135. rootZoneName="$rootZone"
  136. subDomainName="$(printf "%s\n" "$domain" | sed "s,\.$rootZone,,g")"
  137. subDomainNameClean="$(printf "%s\n" "$domain" | sed "s,_acme-challenge.,,g")"
  138. rootZoneDomainID=$(printf "%s\n" "$useResult" | tr ',' '\n' | grep domain_id | cut -d'"' -f4)
  139. rootZoneServiceID=$(printf "%s\n" "$useResult" | tr ',' '\n' | grep service_id | cut -d'"' -f4)
  140. _debug2 _zoneInfo "Zone info from API : $zoneInfo"
  141. _debug2 _get_root "Root zone name : $rootZoneName"
  142. _debug2 _get_root "Root zone domain ID : $rootZoneDomainID"
  143. _debug2 _get_root "Root zone service ID: $rootZoneServiceID"
  144. _debug2 _get_root "Sub domain : $subDomainName"
  145. _debug _get_root "Found valid root domain $rootZone for $subDomainNameClean"
  146. return 0
  147. }
  148. createRecord() {
  149. fulldomain="$1"
  150. txtvalue="$2"
  151. # Get root domain information - needed for DNS.Services API communication
  152. if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
  153. _get_root "$fulldomain"
  154. fi
  155. if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
  156. _err "Something happend - could not get the API zone information"
  157. return 1
  158. fi
  159. _debug2 createRecord "CNAME TXT value is: $txtvalue"
  160. # Prepare data to send to API
  161. data="{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"${txtvalue}\", \"ttl\":\"10\"}"
  162. _debug2 createRecord "data to API: $data"
  163. result=$(_post "$data" "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records" "" "POST")
  164. _debug2 createRecord "result from API: $result"
  165. if [ "$(echo "$result" | _egrep_o "\"success\":true")" = "" ]; then
  166. _err "Failed to create TXT record $fulldomain with content $txtvalue in zone $rootZoneName"
  167. _err "$result"
  168. return 1
  169. fi
  170. _info "Record \"$fulldomain TXT $txtvalue\" has been created"
  171. return 0
  172. }
  173. deleteRecord() {
  174. fulldomain="$1"
  175. txtvalue="$2"
  176. _log deleteRecord "Deleting $fulldomain TXT $txtvalue record"
  177. if [ -z "$rootZoneName" ] || [ -z "$rootZoneDomainID" ] || [ -z "$rootZoneServiceID" ]; then
  178. _get_root "$fulldomain"
  179. fi
  180. result="$(_H1="$_H1" _H2="$_H2" _get "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID")"
  181. #recordInfo="$(echo "$result" | sed -e 's/:{/:{\n/g' -e 's/},/\n},\n/g' | grep "${txtvalue}")"
  182. #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')"
  183. recordID="$(printf "%s\n" "$result" | tr '}' '\n' | grep -- "$txtvalue" | tr ',' '\n' | grep '"id"' | cut -d'"' -f4)"
  184. _debug2 _recordID "recordID used for deletion of record: $recordID"
  185. if [ -z "$recordID" ]; then
  186. _info "Record $fulldomain TXT $txtvalue not found or already deleted"
  187. return 0
  188. else
  189. _debug2 deleteRecord "Found recordID=$recordID"
  190. fi
  191. _debug2 deleteRecord "DELETE request $DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID"
  192. _log "curl DELETE request $DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID"
  193. result="$(_H1="$_H1" _H2="$_H2" _post "" "$DNSServices_API/service/$rootZoneServiceID/dns/$rootZoneDomainID/records/$recordID" "" "DELETE")"
  194. _debug2 deleteRecord "API Delete result \"$result\""
  195. _log "curl API Delete result \"$result\""
  196. # Return OK regardless
  197. return 0
  198. }