Perform Secure-by-default or Woefully Insecure ‘DNS’ Queries
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.

resolver.R 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #' Create a gdns DNS over TLS context and populate it with a resolver
  2. #' for use in resolution functions
  3. #'
  4. #' @param resolvers character vector of valid DNS over TLS resolvers;
  5. #' Defaults to Quad9 (`9.9.9.9`).
  6. #' @export
  7. #' @examples
  8. #' x <- gdns_resolver()
  9. #' x <- gdns_resolver("1.1.1.1")
  10. gdns_resolver <- function(resolvers = "9.9.9.9") {
  11. int_gdns_resolver(resolvers)
  12. }
  13. #' Arbitrary DNS queries
  14. #'
  15. #' Perform any valid resource record inquiry for a given name. See `Details`.
  16. #'
  17. #' This returns a fairly complex result object but that is the nature
  18. #' of DNS queries. You're likely going to want what is in `$replies_tree$answer`
  19. #' but the rest of the structure contains lovely metadata about the query and
  20. #' remote query environment. There will eventually be "as data frame"-ish helpers
  21. #' for this object.
  22. #'
  23. #' Valid values for `rr_type`:
  24. #' - `a`
  25. #' - `a6`
  26. #' - `aaaa`
  27. #' - `afsdb`
  28. #' - `any`
  29. #' - `apl`
  30. #' - `atma`
  31. #' - `avc`
  32. #' - `axfr`
  33. #' - `caa`
  34. #' - `cdnskey`
  35. #' - `cds`
  36. #' - `cert`
  37. #' - `cname`
  38. #' - `csync`
  39. #' - `dhcid`
  40. #' - `dlv`
  41. #' - `dname`
  42. #' - `dnskey`
  43. #' - `doa`
  44. #' - `ds`
  45. #' - `eid`
  46. #' - `eui48`
  47. #' - `eui64`
  48. #' - `gid`
  49. #' - `gpos`
  50. #' - `hinfo`
  51. #' - `hip`
  52. #' - `ipseckey`
  53. #' - `isdn`
  54. #' - `ixfr`
  55. #' - `key`
  56. #' - `kx`
  57. #' - `l32`
  58. #' - `l64`
  59. #' - `loc`
  60. #' - `lp`
  61. #' - `maila`
  62. #' - `mailb`
  63. #' - `mb`
  64. #' - `md`
  65. #' - `mf`
  66. #' - `mg`
  67. #' - `minfo`
  68. #' - `mr`
  69. #' - `mx`
  70. #' - `naptr`
  71. #' - `nid`
  72. #' - `nimloc`
  73. #' - `ninfo`
  74. #' - `ns`
  75. #' - `nsap`
  76. #' - `nsap_ptr`
  77. #' - `nsec`
  78. #' - `nsec3`
  79. #' - `nsec3param`
  80. #' - `null`
  81. #' - `nxt`
  82. #' - `openpgpkey`
  83. #' - `opt`
  84. #' - `ptr`
  85. #' - `px`
  86. #' - `rkey`
  87. #' - `rp`
  88. #' - `rrsig`
  89. #' - `rt`
  90. #' - `sig`
  91. #' - `sink`
  92. #' - `smimea`
  93. #' - `soa`
  94. #' - `spf`
  95. #' - `srv`
  96. #' - `sshfp`
  97. #' - `ta`
  98. #' - `talink`
  99. #' - `tkey`
  100. #' - `tlsa`
  101. #' - `tsig`
  102. #' - `txt`
  103. #' - `uid`
  104. #' - `uinfo`
  105. #' - `unspec`
  106. #' - `uri`
  107. #' - `wks`
  108. #' - `x25`
  109. #' - `zonemd`
  110. #'
  111. #' @param gctx gdns resolver context created with [gdns_resolver()]
  112. #' @param name an entity to query for
  113. #' @param rr_type what resource record type do you want to queyr for? See `Details`.
  114. #' @references <https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml>
  115. #' @export
  116. #' @examples
  117. #' x <- gdns_resolver()
  118. #' gdns_query(x, "example.com")
  119. gdns_query <- function(gctx, name, rr_type = "txt") {
  120. rr_type <- match.arg(trimws(tolower(rr_type[1])), names(rr_types))
  121. res <- int_gdns_query(gctx, name, unname(as.integer(rr_types[rr_type])))
  122. if (length(res)) {
  123. out <- jsonlite::fromJSON(res)
  124. class(out) <- c("gdns_response", "list")
  125. out
  126. } else {
  127. NULL
  128. }
  129. }
  130. list(`1` = "ipv4_address", `2` = "nsdname", `6` = c("expire",
  131. "minimum", "mname", "refresh", "retry", "rname", "serial"), `16` = "txt_strings",
  132. `28` = "ipv6_address", `43` = c("algorithm", "digest", "digest_type",
  133. "key_tag"), `46` = c("algorithm", "key_tag", "labels", "original_ttl",
  134. "signature", "signature_expiration", "signature_inception",
  135. "signers_name", "type_covered"), `47` = c("next_domain_name",
  136. "type_bit_maps"), `48` = c("algorithm", "flags", "protocol",
  137. "public_key")) -> rr_fields
  138. #' Printer for gdns_response objects
  139. #'
  140. #' @param x a `gdns_response` object
  141. #' @param ... ignored
  142. #' @keywords internal
  143. #' @export
  144. print.gdns_response <- function(x, ...) {
  145. cat(
  146. "Query: ",
  147. x$replies_tree$question$qname[1], " ",
  148. toupper(rr_types_rev[x$replies_tree$question$qtype[1]]),
  149. "\n", sep=""
  150. )
  151. print(str(
  152. x$replies_tree$answer[[1]]$rdata[
  153. rr_fields[[as.character(unique(x$replies_tree$answer[[1]]$type))]]
  154. ], 1
  155. ))
  156. }