keyutils: create a common DNS interface
Subsystems (3)
cover keyutils: create a common DNS interface
Hi David, list,
In the following patches, I moved all the common DNS functionality in keyutils into key.dns.{h,c} with the aim to:
- Improve code readability
- ... by restructuring the DNS functions
- Use libresolv for any query, and not only AFSDB/SRV types. This allows A/AAAA queries to also have extra data fetched (such as TTL, for example)
- Add flexibility to add/remove/update any DNS query type
Patch 1/3: moves the common DNS parts into key.dns.{h.c}, rework on the
query part to have it as generic as possible, adapt callers to the new structure.
Patch 2/3: refactor read_config() to make it more structured, hence more
flexible (idea is to have it ready for new config options).
Patch 3/3: adds a "ns=ADDR" callout option to allow callers to specify
custom nameservers for their queries. Custom as in, different from the system's default (usually resolv.conf).
I've been using most of this code on my day-to-day to aid my CIFS debugging activities and I thought it'd be good to have it upstream.
Reviews, feedback, testing are all welcome.
Cheers,
Enzo Matsumiya (3):
key.dns: create a common DNS interface
key.dns_resolver: refactor read_config()
key.dns: allow to use custom nameservers
Makefile | 6 +- dns.afsdb.c | 358 +++++------------- key.dns.c | 891 +++++++++++++++++++++++++++++++++++++++++++++ key.dns.h | 260 +++++++++++-- key.dns_resolver.c | 683 +++++++++++++--------------------- 5 files changed, 1457 insertions(+), 741 deletions(-) create mode 100644 key.dns.c
Signature · 1 line
2.35.3
PATCH 1/3 key.dns: create a common DNS interface
This commit merges all the common DNS functionalities into key.dns.{h,c}.
The main function exposed by this API is the dns_resolver() function, that will resolve the desired hostname. There's a second exposed function afs_lookup_VL_servers() for querying AFSDB records, but will use dns_resolver() internally as well.
Some refactor to the imported exsiting code was also done:
- dns_resolver() now uses only libresolv functions, pulling away getaddrinfo() as it didn't provid no other information aside from the resolved IP address (e.g. it's now possible to get the TTL from the DNS record for any of query type)
- better error handling and rework on error logging
- reduced number of globals, such as key expiry, payload buffer, etc
- simplify the code from the caller modules
--- Makefile | 6 +- dns.afsdb.c | 348 +++++--------------- key.dns.c | 794 +++++++++++++++++++++++++++++++++++++++++++++ key.dns.h | 255 ++++++++++++--- key.dns_resolver.c | 490 +++++++--------------------- 5 files changed, 1207 insertions(+), 686 deletions(-) create mode 100644 key.dns.c
Makefile +4 -2
@@ -159,12 +159,14 @@ keyctl_watch.o: watch_queue.h request-key: request-key.o $(LIB_DEPENDENCY) $(CC) -L. $(CFLAGS) $(LDFLAGS) $(RPATH) -o $@ $< -lkeyutils -key.dns_resolver: key.dns_resolver.o dns.afsdb.o $(LIB_DEPENDENCY) +key.dns_resolver: key.dns_resolver.o dns.afsdb.o key.dns.h key.dns.o $(LIB_DEPENDENCY) $(CC) -L. $(CFLAGS) $(LDFLAGS) $(RPATH) -o $@ \ - key.dns_resolver.o dns.afsdb.o -lkeyutils -lresolv + key.dns_resolver.o dns.afsdb.o key.dns.o \ + -lkeyutils -lresolv key.dns_resolver.o: key.dns_resolver.c key.dns.h dns.afsdb.o: dns.afsdb.c key.dns.h +key.dns.o: key.dns.c key.dns.h ############################################################################### #
dns.afsdb.c +76 -272
372 lines not on this page · raw message
key.dns.c new file +794 -0
794 lines not on this page · raw message
key.dns.h +216 -39
280 lines not on this page · raw message
key.dns_resolver.c +117 -373
621 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH 2/3 key.dns_resolver: refactor read_config()
In reply to Enzo MatsumiyaCreate a key_dns_conf_t struct to hold config values (*). Rework read_config() to take a config file path and return a key_dns_conf_t. This avoids globals and create a structured way to map between config file values and code.
(*) The only config option is still default_ttl
--- dns.afsdb.c | 40 ++++---- key.dns.h | 2 +- key.dns_resolver.c | 222 ++++++++++++++++++++++++++++----------------- 3 files changed, 161 insertions(+), 103 deletions(-)
dns.afsdb.c +19 -21
@@ -63,11 +63,12 @@ static int afs_instantiate(payload_t *payload, unsigned int ttl)* Lookup VL servers for AFS. */ __attribute__((noreturn)) -void afs_lookup_VL_servers (const char *cell,char *options) +void afs_lookup_VL_servers (const char *cell,char *options,long config_ttl) {payload_t *payload =NULL ;struct hostinfo host = {0 };char *vlsrv_name =NULL ; +unsigned int ttl;int ret =0 ;if (!cell)
@@ -107,28 +108,25 @@ void afs_lookup_VL_servers(const char *cell, char *options) }dump_payload (payload); -info ("Key timeout will be %ld seconds" , host.ttl); - -if (!debug_mode) { -unsigned int ttl; - -/* -* If TTL was set through the config file (key_expiry), -* it takes precedence over the one from the DNS record (stored -* in host.ttl). -*/ -if (key_expiry >-1 ) - ttl = (unsigned int )key_expiry; -else if (host.ttl >-1 ) - ttl = (unsigned int )host.ttl; -else -/* Fallback to default value if dns_resolver() couldn't -* get TTL for some reason */ - ttl =DEFAULT_KEY_TTL ; +/* +* If TTL was set through the config file (@config_ttl), +* it takes precedence over the one from the DNS record (stored +* in host.ttl). +*/ +if (config_ttl >0 ) + ttl = (unsigned int )config_ttl; +else if (host.ttl >0 ) + ttl = (unsigned int )host.ttl; +else +/* Fallback to default value if dns_resolver() couldn't +* get TTL for some reason */ + ttl =DEFAULT_KEY_TTL ; + +info ("Key timeout will be %u seconds" , ttl); + +if (!debug_mode) ret =afs_instantiate (payload, ttl); - } - out_free:free_hostinfo (&host);free (payload);
key.dns.h +1 -1
@@ -246,6 +246,6 @@ void dump_payload(payload_t *payload);int dns_resolver (hostinfo_t *host,payload_t *payload);/* AFS-specific DNS query function */ -void afs_lookup_VL_servers (const char *cell,char *options); +void afs_lookup_VL_servers (const char *cell,char *options,long config_ttl);#endif /* _KEY_DNS_H */
key.dns_resolver.c +141 -81
374 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH 3/3 key.dns: allow to use custom nameservers
In reply to Enzo MatsumiyaThis commit allows the user to use custom nameservers for their key.dns_resolver queries.
It can be achieved by passing the "ns=ADDR" option as the callout info for dns_resolver(). Both IPv4 and IPv6 are supported. A maximum of 3 nameservers is allowed, as a libresolv limitation.
Each nameserver will be queried only once for the hostname specified. Upon success (hostname was resolved), the information is returned and no further queries are made to the remaining nameservers.
If all specified nameservers fail (either unreachable or can't resolve the hostname), there's no fallback to using system's default. Callers must adapt their calls to remove the failing nameservers from their callout info and query again.
If no nameservers are passed, the system's default will be used (just like before this patch).
--- dns.afsdb.c | 2 + key.dns.c | 107 ++++++++++++++++++++++++++++++++++++++++++--- key.dns.h | 5 +++ key.dns_resolver.c | 41 +++++++++++++++-- 4 files changed, 146 insertions(+), 9 deletions(-)
dns.afsdb.c +2 -0
@@ -129,6 +129,8 @@ void afs_lookup_VL_servers(const char *cell, char *options, long config_ttl) ret =afs_instantiate (payload, ttl); out_free:free_hostinfo (&host); +while (host.nslen-- >0 ) +free (host.nameservers[host.nslen]);free (payload);exit (ret);
key.dns.c +102 -5
166 lines not on this page · raw message
key.dns.h +5 -0
@@ -182,6 +182,8 @@ struct hostinfo {long ttl;ns_msg *handle;bool single_addr; +char *nameservers[MAXNS ]; +int nslen; };typedef struct hostinfo hostinfo_t ;
@@ -204,6 +206,9 @@ static inline void dump_host(hostinfo_t *host)/* skip af; it's used only internally for resolving and could contain * incoherent information */ debug (" ttl: %ld" , host->ttl); +if (host->nslen >0 ) +for (i =0 ; i < host->nslen; i++) +debug (" custom ns[%d]: %s" , i, host->nameservers[i]); }static inline void free_hostinfo (hostinfo_t *host)
key.dns_resolver.c +37 -4
74 lines not on this page · raw message
Signature · 1 line
2.35.3