1 #if defined(LIBC_SCCS) && !defined(lint)
2 static const char rcsid[] = "$Id: hesiod.c,v 1.1.2.4 2004/05/17 07:46:43 marka Exp $";
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996,1999 by Internet Software Consortium.
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
27 * hesiod.c --- the core portion of the hesiod resolver.
29 * This file is derived from the hesiod library from Project Athena;
30 * It has been extensively rewritten by Theodore Ts'o to have a more
31 * thread-safe interface.
36 #include "port_before.h"
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <arpa/nameser.h>
49 #include "port_after.h"
51 #include "pathnames.h"
57 int hesiod_init(void **context);
58 void hesiod_end(void *context);
59 char * hesiod_to_bind(void *context, const char *name,
61 char ** hesiod_resolve(void *context, const char *name,
63 void hesiod_free_list(void *context, char **list);
65 static int parse_config_file(struct hesiod_p *ctx, const char *filename);
66 static char ** get_txt_records(struct hesiod_p *ctx, int class,
68 static int init(struct hesiod_p *ctx);
73 * This function is called to initialize a hesiod_p.
76 hesiod_init(void **context) {
80 ctx = malloc(sizeof(struct hesiod_p));
90 if (parse_config_file(ctx, _PATH_HESIOD_CONF) < 0) {
93 * Use compiled in defaults.
95 ctx->LHS = malloc(strlen(DEF_LHS) + 1);
96 ctx->RHS = malloc(strlen(DEF_RHS) + 1);
97 if (ctx->LHS == NULL || ctx->RHS == NULL) {
101 strcpy(ctx->LHS, DEF_LHS); /* (checked) */
102 strcpy(ctx->RHS, DEF_RHS); /* (checked) */
108 * The default RHS can be overridden by an environment
111 if ((cp = getenv("HES_DOMAIN")) != NULL) {
112 size_t RHSlen = strlen(cp) + 2;
115 ctx->RHS = malloc(RHSlen);
121 strcpy(ctx->RHS, cp); /* (checked) */
123 strcpy(ctx->RHS, "."); /* (checked) */
124 strcat(ctx->RHS, cp); /* (checked) */
129 * If there is no default hesiod realm set, we return an
138 if (res_ninit(ctx->res) < 0)
151 * This function deallocates the hesiod_p
154 hesiod_end(void *context) {
155 struct hesiod_p *ctx = (struct hesiod_p *) context;
156 int save_errno = errno;
159 res_nclose(ctx->res);
164 if (ctx->res && ctx->free_res)
165 (*ctx->free_res)(ctx->res);
171 * This function takes a hesiod (name, type) and returns a DNS
172 * name which is to be resolved.
175 hesiod_to_bind(void *context, const char *name, const char *type) {
176 struct hesiod_p *ctx = (struct hesiod_p *) context;
178 char **rhs_list = NULL;
179 const char *RHS, *cp;
181 /* Decide what our RHS is, and set cp to the end of the actual name. */
182 if ((cp = strchr(name, '@')) != NULL) {
183 if (strchr(cp + 1, '.'))
185 else if ((rhs_list = hesiod_resolve(context, cp + 1,
186 "rhs-extension")) != NULL)
194 cp = name + strlen(name);
198 * Allocate the space we need, including up to three periods and
199 * the terminating NUL.
201 if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
202 (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
205 hesiod_free_list(context, rhs_list);
209 /* Now put together the DNS name. */
210 memcpy(bindname, name, cp - name);
211 bindname[cp - name] = '\0';
212 strcat(bindname, ".");
213 strcat(bindname, type);
215 if (ctx->LHS[0] != '.')
216 strcat(bindname, ".");
217 strcat(bindname, ctx->LHS);
220 strcat(bindname, ".");
221 strcat(bindname, RHS);
224 hesiod_free_list(context, rhs_list);
230 * This is the core function. Given a hesiod (name, type), it
231 * returns an array of strings returned by the resolver.
234 hesiod_resolve(void *context, const char *name, const char *type) {
235 struct hesiod_p *ctx = (struct hesiod_p *) context;
236 char *bindname = hesiod_to_bind(context, name, type);
239 if (bindname == NULL)
241 if (init(ctx) == -1) {
246 if ((retvec = get_txt_records(ctx, C_IN, bindname))) {
254 retvec = get_txt_records(ctx, C_HS, bindname);
260 hesiod_free_list(void *context, char **list) {
265 for (p = list; *p; p++)
271 * This function parses the /etc/hesiod.conf file
274 parse_config_file(struct hesiod_p *ctx, const char *filename) {
275 char *key, *data, *cp, **cpp;
276 char buf[MAXDNAME+7];
280 * Clear the existing configuration variable, just in case
287 ctx->RHS = ctx->LHS = 0;
290 * Now open and parse the file...
292 if (!(fp = fopen(filename, "r")))
295 while (fgets(buf, sizeof(buf), fp) != NULL) {
297 if (*cp == '#' || *cp == '\n' || *cp == '\r')
299 while(*cp == ' ' || *cp == '\t')
302 while(*cp != ' ' && *cp != '\t' && *cp != '=')
306 while(*cp == ' ' || *cp == '\t' || *cp == '=')
309 while(*cp != ' ' && *cp != '\n' && *cp != '\r')
313 if (strcmp(key, "lhs") == 0)
315 else if (strcmp(key, "rhs") == 0)
320 *cpp = malloc(strlen(data) + 1);
336 ctx->RHS = ctx->LHS = 0;
341 * Given a DNS class and a DNS name, do a lookup for TXT records, and
342 * return a list of them.
345 get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
347 int type; /* RR type */
348 int class; /* RR class */
349 int dlen; /* len of data section */
350 u_char *data; /* pointer to data */
353 u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
354 u_char *cp, *erdata, *eom;
355 char *dst, *edst, **list;
356 int ancount, qdcount;
360 * Construct the query and send it.
362 n = res_nmkquery(ctx->res, QUERY, name, class, T_TXT, NULL, 0,
363 NULL, qbuf, MAX_HESRESP);
368 n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP);
370 errno = ECONNREFUSED;
379 * OK, parse the result.
381 hp = (HEADER *) abuf;
382 ancount = ntohs(hp->ancount);
383 qdcount = ntohs(hp->qdcount);
384 cp = abuf + sizeof(HEADER);
387 /* Skip query, trying to get to the answer section which follows. */
388 for (i = 0; i < qdcount; i++) {
389 skip = dn_skipname(cp, eom);
390 if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
394 cp += skip + QFIXEDSZ;
397 list = malloc((ancount + 1) * sizeof(char *));
403 for (i = 0; i < ancount; i++) {
404 skip = dn_skipname(cp, eom);
410 if (cp + 3 * INT16SZ + INT32SZ > eom) {
414 rr.type = ns_get16(cp);
416 rr.class = ns_get16(cp);
417 cp += INT16SZ + INT32SZ; /* skip the ttl, too */
418 rr.dlen = ns_get16(cp);
420 if (cp + rr.dlen > eom) {
426 if (rr.class != class || rr.type != T_TXT)
428 if (!(list[j] = malloc(rr.dlen)))
431 edst = dst + rr.dlen;
432 erdata = rr.data + rr.dlen;
434 while (cp < erdata) {
435 n = (unsigned char) *cp++;
436 if (cp + n > eom || dst + n > edst) {
458 for (i = 0; i < j; i++)
465 __hesiod_res_get(void *context) {
466 struct hesiod_p *ctx = context;
469 struct __res_state *res;
470 res = (struct __res_state *)malloc(sizeof *res);
475 memset(res, 0, sizeof *res);
476 __hesiod_res_set(ctx, res, free);
483 __hesiod_res_set(void *context, struct __res_state *res,
484 void (*free_res)(void *)) {
485 struct hesiod_p *ctx = context;
487 if (ctx->res && ctx->free_res) {
488 res_nclose(ctx->res);
489 (*ctx->free_res)(ctx->res);
493 ctx->free_res = free_res;
497 init(struct hesiod_p *ctx) {
499 if (!ctx->res && !__hesiod_res_get(ctx))
502 if (((ctx->res->options & RES_INIT) == 0U) &&
503 (res_ninit(ctx->res) == -1))