dns_resolver.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===================
  3. DNS Resolver Module
  4. ===================
  5. .. Contents:
  6. - Overview.
  7. - Compilation.
  8. - Setting up.
  9. - Usage.
  10. - Mechanism.
  11. - Debugging.
  12. Overview
  13. ========
  14. The DNS resolver module provides a way for kernel services to make DNS queries
  15. by way of requesting a key of key type dns_resolver. These queries are
  16. upcalled to userspace through /sbin/request-key.
  17. These routines must be supported by userspace tools dns.upcall, cifs.upcall and
  18. request-key. It is under development and does not yet provide the full feature
  19. set. The features it does support include:
  20. * Implements the dns_resolver key_type to contact userspace.
  21. It does not yet support the following AFS features:
  22. * DNS query support for AFSDB resource record.
  23. This code is extracted from the CIFS filesystem.
  24. Compilation
  25. ===========
  26. The module should be enabled by turning on the kernel configuration options::
  27. CONFIG_DNS_RESOLVER - tristate "DNS Resolver support"
  28. Setting up
  29. ==========
  30. To set up this facility, the /etc/request-key.conf file must be altered so that
  31. /sbin/request-key can appropriately direct the upcalls. For example, to handle
  32. basic dname to IPv4/IPv6 address resolution, the following line should be
  33. added::
  34. #OP TYPE DESC CO-INFO PROGRAM ARG1 ARG2 ARG3 ...
  35. #====== ============ ======= ======= ==========================
  36. create dns_resolver * * /usr/sbin/cifs.upcall %k
  37. To direct a query for query type 'foo', a line of the following should be added
  38. before the more general line given above as the first match is the one taken::
  39. create dns_resolver foo:* * /usr/sbin/dns.foo %k
  40. Usage
  41. =====
  42. To make use of this facility, first ``dns_resolver.h`` must be included::
  43. #include <linux/dns_resolver.h>
  44. Then queries may be made by calling::
  45. int dns_query(const char *type, const char *name, size_t namelen,
  46. const char *options, char **_result, time_t *_expiry);
  47. This is the basic access function. It looks for a cached DNS query and if
  48. it doesn't find it, it upcalls to userspace to make a new DNS query, which
  49. may then be cached. The key description is constructed as a string of the
  50. form::
  51. [<type>:]<name>
  52. where <type> optionally specifies the particular upcall program to invoke,
  53. and thus the type of query, and <name> specifies the string to be looked up.
  54. The default query type is a straight hostname to IP address set lookup.
  55. The name parameter is not required to be a NUL-terminated string, and its
  56. length should be given by the namelen argument.
  57. The options parameter may be NULL or it may be a set of options
  58. appropriate to the query type.
  59. The return value is a string appropriate to the query type. For instance,
  60. for the default query type it is just a list of comma-separated IPv4 and
  61. IPv6 addresses. The caller must free the result.
  62. The length of the result string is returned on success, and a negative
  63. error code is returned otherwise. -EKEYREJECTED will be returned if the
  64. DNS lookup failed.
  65. If _expiry is non-NULL, the expiry time (TTL) of the result will be
  66. returned also.
  67. The kernel maintains an internal keyring in which it caches looked up keys.
  68. This can be cleared by any process that has the CAP_SYS_ADMIN capability by
  69. the use of KEYCTL_KEYRING_CLEAR on the keyring ID.
  70. Reading DNS Keys from Userspace
  71. ===============================
  72. Keys of dns_resolver type can be read from userspace using keyctl_read() or
  73. "keyctl read/print/pipe".
  74. Mechanism
  75. =========
  76. The dns_resolver module registers a key type called "dns_resolver". Keys of
  77. this type are used to transport and cache DNS lookup results from userspace.
  78. When dns_query() is invoked, it calls request_key() to search the local
  79. keyrings for a cached DNS result. If that fails to find one, it upcalls to
  80. userspace to get a new result.
  81. Upcalls to userspace are made through the request_key() upcall vector, and are
  82. directed by means of configuration lines in /etc/request-key.conf that tell
  83. /sbin/request-key what program to run to instantiate the key.
  84. The upcall handler program is responsible for querying the DNS, processing the
  85. result into a form suitable for passing to the keyctl_instantiate_key()
  86. routine. This then passes the data to dns_resolver_instantiate() which strips
  87. off and processes any options included in the data, and then attaches the
  88. remainder of the string to the key as its payload.
  89. The upcall handler program should set the expiry time on the key to that of the
  90. lowest TTL of all the records it has extracted a result from. This means that
  91. the key will be discarded and recreated when the data it holds has expired.
  92. dns_query() returns a copy of the value attached to the key, or an error if
  93. that is indicated instead.
  94. See Documentation/security/keys/request-key.rst for further information about
  95. request-key function.
  96. Debugging
  97. =========
  98. Debugging messages can be turned on dynamically by writing a 1 into the
  99. following file::
  100. /sys/module/dns_resolver/parameters/debug