1 /* Copyright (c) 1996 by Internet Software Consortium.
3 * Permission to use, copy, modify, and distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
8 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
9 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
10 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 /* Copyright 1996 by the Massachusetts Institute of Technology.
19 * Permission to use, copy, modify, and distribute this
20 * software and its documentation for any purpose and without
21 * fee is hereby granted, provided that the above copyright
22 * notice appear in all copies and that both that copyright
23 * notice and this permission notice appear in supporting
24 * documentation, and that the name of M.I.T. not be used in
25 * advertising or publicity pertaining to distribution of the
26 * software without specific, written prior permission.
27 * M.I.T. makes no representations about the suitability of
28 * this software for any purpose. It is provided "as is"
29 * without express or implied warranty.
32 /* This file is part of the hesiod library. It implements the core
33 * portion of the hesiod resolver.
35 * This file is loosely based on an interim version of hesiod.c from
36 * the BIND IRS library, which was in turn based on an earlier version
37 * of this file. Extensive changes have been made on each step of the
40 * This implementation is not truly thread-safe at the moment because
41 * it uses res_send() and accesses _res.
43 * $NetBSD: hesiod.c,v 1.9 1999/02/11 06:16:38 simonb Exp $
44 * $FreeBSD: src/lib/libc/net/hesiod.c,v 1.9 2003/05/01 19:03:14 nectar Exp $
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <netinet/in.h>
50 #include <arpa/nameser.h>
62 char *lhs; /* normally ".ns" */
63 char *rhs; /* AKA the default hesiod domain */
64 int classes[2]; /* The class search order. */
67 #define MAX_HESRESP 1024
69 static int read_config_file(struct hesiod_p *, const char *);
70 static char **get_txt_records(int, const char *);
71 static int init_context(void);
72 static void translate_errors(void);
77 * initialize a hesiod_p.
80 hesiod_init(void **context)
83 const char *p, *configname;
85 ctx = malloc(sizeof(struct hesiod_p));
89 configname = getenv("HESIOD_CONFIG");
93 configname = _PATH_HESIOD_CONF;
94 if (read_config_file(ctx, configname) >= 0) {
96 * The default rhs can be overridden by an
97 * environment variable.
100 p = getenv("HES_DOMAIN");
106 ctx->rhs = malloc(strlen(p) + 2);
110 (*p == '.') ? p + 1 : p);
131 * Deallocates the hesiod_p.
134 hesiod_end(void *context)
136 struct hesiod_p *ctx = (struct hesiod_p *) context;
146 * takes a hesiod (name, type) and returns a DNS
147 * name which is to be resolved.
150 hesiod_to_bind(void *context, const char *name, const char *type)
152 struct hesiod_p *ctx = (struct hesiod_p *) context;
153 char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL;
157 if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) {
163 * Find the right right hand side to use, possibly
164 * truncating bindname.
166 p = strchr(bindname, '@');
170 rhs = name + (p - bindname);
172 rhs_list = hesiod_resolve(context, p, "rhs-extension");
183 /* See if we have enough room. */
184 len = strlen(bindname) + 1 + strlen(type);
186 len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
187 len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
188 if (len > sizeof(bindname) - 1) {
190 hesiod_free_list(context, rhs_list);
194 /* Put together the rest of the domain. */
195 strcat(bindname, ".");
196 strcat(bindname, type);
197 /* Only append lhs if it isn't empty. */
198 if (ctx->lhs && ctx->lhs[0] != '\0' ) {
199 if (ctx->lhs[0] != '.')
200 strcat(bindname, ".");
201 strcat(bindname, ctx->lhs);
204 strcat(bindname, ".");
205 strcat(bindname, rhs);
207 /* rhs_list is no longer needed, since we're done with rhs. */
209 hesiod_free_list(context, rhs_list);
211 /* Make a copy of the result and return it to the caller. */
212 ret = strdup(bindname);
220 * Given a hesiod name and type, return an array of strings returned
224 hesiod_resolve(void *context, const char *name, const char *type)
226 struct hesiod_p *ctx = (struct hesiod_p *) context;
227 char *bindname, **retvec;
229 bindname = hesiod_to_bind(context, name, type);
233 retvec = get_txt_records(ctx->classes[0], bindname);
234 if (retvec == NULL && errno == ENOENT && ctx->classes[1])
235 retvec = get_txt_records(ctx->classes[1], bindname);
243 hesiod_free_list(void *context, char **list)
249 for (p = list; *p; p++)
255 /* read_config_file --
256 * Parse the /etc/hesiod.conf file. Returns 0 on success,
257 * -1 on failure. On failure, it might leave values in ctx->lhs
258 * or ctx->rhs which need to be freed by the caller.
261 read_config_file(struct hesiod_p *ctx, const char *filename)
263 char *key, *data, *p, **which;
264 char buf[MAXDNAME + 7];
268 /* Set default query classes. */
269 ctx->classes[0] = C_IN;
270 ctx->classes[1] = C_HS;
272 /* Try to open the configuration file. */
273 fp = fopen(filename, "r");
275 /* Use compiled in default domain names. */
276 ctx->lhs = strdup(DEF_LHS);
277 ctx->rhs = strdup(DEF_RHS);
278 if (ctx->lhs && ctx->rhs)
287 while (fgets(buf, sizeof(buf), fp) != NULL) {
289 if (*p == '#' || *p == '\n' || *p == '\r')
291 while (*p == ' ' || *p == '\t')
294 while (*p != ' ' && *p != '\t' && *p != '=')
298 while (isspace(*p) || *p == '=')
305 if (strcasecmp(key, "lhs") == 0 ||
306 strcasecmp(key, "rhs") == 0) {
307 which = (strcasecmp(key, "lhs") == 0)
308 ? &ctx->lhs : &ctx->rhs;
309 *which = strdup(data);
315 if (strcasecmp(key, "classes") == 0) {
317 while (*data && n < 2) {
319 while (*p && *p != ',')
323 if (strcasecmp(data, "IN") == 0)
324 ctx->classes[n++] = C_IN;
326 if (strcasecmp(data, "HS") == 0)
332 ctx->classes[n++] = 0;
338 if (!ctx->rhs || ctx->classes[0] == 0 ||
339 ctx->classes[0] == ctx->classes[1]) {
348 * Given a DNS class and a DNS name, do a lookup for TXT records, and
349 * return a list of them.
352 get_txt_records(int qclass, const char *name)
355 unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
357 int ancount, qdcount, i, j, n, skip, type, class, len;
359 /* Make sure the resolver is initialized. */
360 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
363 /* Construct the query. */
364 n = res_mkquery(QUERY, name, qclass, T_TXT, NULL, 0,
365 NULL, qbuf, PACKETSZ);
369 /* Send the query. */
370 n = res_send(qbuf, n, abuf, MAX_HESRESP);
371 if (n < 0 || n > MAX_HESRESP) {
372 errno = ECONNREFUSED; /* XXX */
375 /* Parse the header of the result. */
376 hp = (HEADER *) (void *) abuf;
377 ancount = ntohs(hp->ancount);
378 qdcount = ntohs(hp->qdcount);
379 p = abuf + sizeof(HEADER);
383 * Skip questions, trying to get to the answer section
386 for (i = 0; i < qdcount; i++) {
387 skip = dn_skipname(p, eom);
388 if (skip < 0 || p + skip + QFIXEDSZ > eom) {
392 p += skip + QFIXEDSZ;
395 /* Allocate space for the text record answers. */
396 list = malloc((ancount + 1) * sizeof(char *));
401 /* Parse the answers. */
403 for (i = 0; i < ancount; i++) {
404 /* Parse the header of this answer. */
405 skip = dn_skipname(p, eom);
406 if (skip < 0 || p + skip + 10 > eom)
408 type = p[skip + 0] << 8 | p[skip + 1];
409 class = p[skip + 2] << 8 | p[skip + 3];
410 len = p[skip + 8] << 8 | p[skip + 9];
416 /* Skip entries of the wrong class and type. */
417 if (class != qclass || type != T_TXT) {
421 /* Allocate space for this answer. */
422 list[j] = malloc((size_t)len);
429 /* Copy answer data into the allocated area. */
432 n = (unsigned char) *p++;
437 memcpy(dst, p, (size_t)n);
449 * If we didn't terminate the loop normally, something
453 for (i = 0; i < j; i++)
468 * COMPATIBILITY FUNCTIONS
471 static int inited = 0;
472 static void *context;
473 static int errval = HES_ER_UNINIT;
483 hes_to_bind(const char *name, const char *type)
485 static char *bindname;
486 if (init_context() < 0)
490 bindname = hesiod_to_bind(context, name, type);
497 hes_resolve(const char *name, const char *type)
501 if (init_context() < 0)
505 * In the old Hesiod interface, the caller was responsible for
506 * freeing the returned strings but not the vector of strings itself.
511 list = hesiod_resolve(context, name, type);
526 hesiod_free_list(context, hp);
534 if (hesiod_init(&context) < 0) {
535 errval = HES_ER_CONFIG;
544 translate_errors(void)
548 errval = HES_ER_NOTFOUND;
556 /* Not a good match, but the best we can do. */
557 errval = HES_ER_CONFIG;