hesiod.c revision 1.10 1 /* $NetBSD: hesiod.c,v 1.10 1999/09/16 11:45:13 lukem Exp $ */
2
3 /* Copyright (c) 1996 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
17 */
18
19 /* Copyright 1996 by the Massachusetts Institute of Technology.
20 *
21 * Permission to use, copy, modify, and distribute this
22 * software and its documentation for any purpose and without
23 * fee is hereby granted, provided that the above copyright
24 * notice appear in all copies and that both that copyright
25 * notice and this permission notice appear in supporting
26 * documentation, and that the name of M.I.T. not be used in
27 * advertising or publicity pertaining to distribution of the
28 * software without specific, written prior permission.
29 * M.I.T. makes no representations about the suitability of
30 * this software for any purpose. It is provided "as is"
31 * without express or implied warranty.
32 */
33
34 /* This file is part of the hesiod library. It implements the core
35 * portion of the hesiod resolver.
36 *
37 * This file is loosely based on an interim version of hesiod.c from
38 * the BIND IRS library, which was in turn based on an earlier version
39 * of this file. Extensive changes have been made on each step of the
40 * path.
41 *
42 * This implementation is not truly thread-safe at the moment because
43 * it uses res_send() and accesses _res.
44 */
45
46 #include <sys/cdefs.h>
47
48 #if defined(LIBC_SCCS) && !defined(lint)
49 __IDSTRING(rcsid_hesiod_c,
50 "#Id: hesiod.c,v 1.18.2.1 1997/01/03 20:48:20 ghudson Exp #");
51 __IDSTRING(rcsid_hesiod_p_h,
52 "#Id: hesiod_p.h,v 1.1 1996/12/08 21:39:37 ghudson Exp #");
53 __IDSTRING(rcsid_hescompat_c,
54 "#Id: hescompat.c,v 1.1.2.1 1996/12/16 08:37:45 ghudson Exp #");
55 __RCSID("$NetBSD: hesiod.c,v 1.10 1999/09/16 11:45:13 lukem Exp $");
56 #endif /* LIBC_SCCS and not lint */
57
58 #include "namespace.h"
59
60 #include <sys/types.h>
61 #include <sys/param.h>
62 #include <netinet/in.h>
63 #include <arpa/nameser.h>
64
65 #include <assert.h>
66 #include <ctype.h>
67 #include <errno.h>
68 #include <hesiod.h>
69 #include <resolv.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73
74 #ifdef __weak_alias
75 __weak_alias(hesiod_init,_hesiod_init);
76 __weak_alias(hesiod_end,_hesiod_end);
77 __weak_alias(hesiod_to_bind,_hesiod_to_bind);
78 __weak_alias(hesiod_resolve,_hesiod_resolve);
79 __weak_alias(hesiod_free_list,_hesiod_free_list);
80 __weak_alias(hes_init,_hes_init);
81 __weak_alias(hes_to_bind,_hes_to_bind);
82 __weak_alias(hes_resolve,_hes_resolve);
83 __weak_alias(hes_error,_hes_error);
84 __weak_alias(hes_free,_hes_free);
85 #endif
86
87 struct hesiod_p {
88 char *lhs; /* normally ".ns" */
89 char *rhs; /* AKA the default hesiod domain */
90 int classes[2]; /* The class search order. */
91 };
92
93 #define MAX_HESRESP 1024
94
95 static int read_config_file __P((struct hesiod_p *, const char *));
96 static char **get_txt_records __P((int, const char *));
97 static int init_context __P((void));
98 static void translate_errors __P((void));
99
100
101 /*
102 * hesiod_init --
103 * initialize a hesiod_p.
104 */
105 int
106 hesiod_init(context)
107 void **context;
108 {
109 struct hesiod_p *ctx;
110 const char *p, *configname;
111 int serrno;
112
113 _DIAGASSERT(context != NULL);
114 #ifdef _DIAGNOSTIC
115 if (context == NULL) {
116 errno = EFAULT;
117 return (-1);
118 }
119 #endif
120
121 ctx = malloc(sizeof(struct hesiod_p));
122 if (ctx) {
123 *context = ctx;
124 configname = getenv("HESIOD_CONFIG");
125 if (!configname)
126 configname = _PATH_HESIOD_CONF;
127 if (read_config_file(ctx, configname) >= 0) {
128 /*
129 * The default rhs can be overridden by an
130 * environment variable.
131 */
132 p = getenv("HES_DOMAIN");
133 if (p) {
134 if (ctx->rhs)
135 free(ctx->rhs);
136 ctx->rhs = malloc(strlen(p) + 2);
137 if (ctx->rhs) {
138 *ctx->rhs = '.';
139 strcpy(ctx->rhs + 1,
140 (*p == '.') ? p + 1 : p);
141 return 0;
142 } else
143 errno = ENOMEM;
144 } else
145 return 0;
146 }
147 } else
148 errno = ENOMEM;
149
150 serrno = errno;
151 if (ctx->lhs)
152 free(ctx->lhs);
153 if (ctx->rhs)
154 free(ctx->rhs);
155 if (ctx)
156 free(ctx);
157 errno = serrno;
158 return -1;
159 }
160
161 /*
162 * hesiod_end --
163 * Deallocates the hesiod_p.
164 */
165 void
166 hesiod_end(context)
167 void *context;
168 {
169 struct hesiod_p *ctx = (struct hesiod_p *) context;
170
171 _DIAGASSERT(context != NULL);
172 #ifdef _DIAGNOSTIC
173 if (context == NULL)
174 return;
175 #endif
176
177 free(ctx->rhs);
178 if (ctx->lhs)
179 free(ctx->lhs);
180 free(ctx);
181 }
182
183 /*
184 * hesiod_to_bind --
185 * takes a hesiod (name, type) and returns a DNS
186 * name which is to be resolved.
187 */
188 char *
189 hesiod_to_bind(void *context, const char *name, const char *type)
190 {
191 struct hesiod_p *ctx = (struct hesiod_p *) context;
192 char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL;
193 const char *rhs;
194 int len;
195
196 _DIAGASSERT(context != NULL);
197 _DIAGASSERT(name != NULL);
198 _DIAGASSERT(type != NULL);
199 #ifdef _DIAGNOSTIC
200 if (context == NULL || name == NULL || type == NULL)
201 return (NULL);
202 #endif
203
204 strcpy(bindname, name);
205
206 /*
207 * Find the right right hand side to use, possibly
208 * truncating bindname.
209 */
210 p = strchr(bindname, '@');
211 if (p) {
212 *p++ = 0;
213 if (strchr(p, '.'))
214 rhs = name + (p - bindname);
215 else {
216 rhs_list = hesiod_resolve(context, p, "rhs-extension");
217 if (rhs_list)
218 rhs = *rhs_list;
219 else {
220 errno = ENOENT;
221 return NULL;
222 }
223 }
224 } else
225 rhs = ctx->rhs;
226
227 /* See if we have enough room. */
228 len = strlen(bindname) + 1 + strlen(type);
229 if (ctx->lhs)
230 len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
231 len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
232 if (len > sizeof(bindname) - 1) {
233 if (rhs_list)
234 hesiod_free_list(context, rhs_list);
235 errno = EMSGSIZE;
236 return NULL;
237 }
238 /* Put together the rest of the domain. */
239 strcat(bindname, ".");
240 strcat(bindname, type);
241 /* Only append lhs if it isn't empty. */
242 if (ctx->lhs && ctx->lhs[0] != '\0' ) {
243 if (ctx->lhs[0] != '.')
244 strcat(bindname, ".");
245 strcat(bindname, ctx->lhs);
246 }
247 if (rhs[0] != '.')
248 strcat(bindname, ".");
249 strcat(bindname, rhs);
250
251 /* rhs_list is no longer needed, since we're done with rhs. */
252 if (rhs_list)
253 hesiod_free_list(context, rhs_list);
254
255 /* Make a copy of the result and return it to the caller. */
256 ret = strdup(bindname);
257 if (!ret)
258 errno = ENOMEM;
259 return ret;
260 }
261
262 /*
263 * hesiod_resolve --
264 * Given a hesiod name and type, return an array of strings returned
265 * by the resolver.
266 */
267 char **
268 hesiod_resolve(context, name, type)
269 void *context;
270 const char *name;
271 const char *type;
272 {
273 struct hesiod_p *ctx = (struct hesiod_p *) context;
274 char *bindname, **retvec;
275
276 _DIAGASSERT(context != NULL);
277 _DIAGASSERT(name != NULL);
278 _DIAGASSERT(type != NULL);
279 #ifdef _DIAGNOSTIC
280 if (context == NULL || name == NULL || type == NULL)
281 return (NULL);
282 #endif
283
284 bindname = hesiod_to_bind(context, name, type);
285 if (!bindname)
286 return NULL;
287
288 retvec = get_txt_records(ctx->classes[0], bindname);
289 if (retvec == NULL && errno == ENOENT && ctx->classes[1])
290 retvec = get_txt_records(ctx->classes[1], bindname);
291
292 free(bindname);
293 return retvec;
294 }
295
296 /*ARGSUSED*/
297 void
298 hesiod_free_list(context, list)
299 void *context;
300 char **list;
301 {
302 char **p;
303
304 _DIAGASSERT(context != NULL);
305 #ifdef _DIAGNOSTIC
306 if (context == NULL)
307 return;
308 #endif
309
310 if (list == NULL)
311 return;
312 for (p = list; *p; p++)
313 free(*p);
314 free(list);
315 }
316
317
318 /* read_config_file --
319 * Parse the /etc/hesiod.conf file. Returns 0 on success,
320 * -1 on failure. On failure, it might leave values in ctx->lhs
321 * or ctx->rhs which need to be freed by the caller.
322 */
323 static int
324 read_config_file(ctx, filename)
325 struct hesiod_p *ctx;
326 const char *filename;
327 {
328 char *key, *data, *p, **which;
329 char buf[MAXDNAME + 7];
330 int n;
331 FILE *fp;
332
333 _DIAGASSERT(ctx != NULL);
334 _DIAGASSERT(filename != NULL);
335
336 /* Set default query classes. */
337 ctx->classes[0] = C_IN;
338 ctx->classes[1] = C_HS;
339
340 /* Try to open the configuration file. */
341 fp = fopen(filename, "r");
342 if (!fp) {
343 /* Use compiled in default domain names. */
344 ctx->lhs = strdup(DEF_LHS);
345 ctx->rhs = strdup(DEF_RHS);
346 if (ctx->lhs && ctx->rhs)
347 return 0;
348 else {
349 errno = ENOMEM;
350 return -1;
351 }
352 }
353 ctx->lhs = NULL;
354 ctx->rhs = NULL;
355 while (fgets(buf, sizeof(buf), fp) != NULL) {
356 p = buf;
357 if (*p == '#' || *p == '\n' || *p == '\r')
358 continue;
359 while (*p == ' ' || *p == '\t')
360 p++;
361 key = p;
362 while (*p != ' ' && *p != '\t' && *p != '=')
363 p++;
364 *p++ = 0;
365
366 while (isspace(*p) || *p == '=')
367 p++;
368 data = p;
369 while (!isspace(*p))
370 p++;
371 *p = 0;
372
373 if (strcasecmp(key, "lhs") == 0 ||
374 strcasecmp(key, "rhs") == 0) {
375 which = (strcasecmp(key, "lhs") == 0)
376 ? &ctx->lhs : &ctx->rhs;
377 *which = strdup(data);
378 if (!*which) {
379 errno = ENOMEM;
380 return -1;
381 }
382 } else {
383 if (strcasecmp(key, "classes") == 0) {
384 n = 0;
385 while (*data && n < 2) {
386 p = data;
387 while (*p && *p != ',')
388 p++;
389 if (*p)
390 *p++ = 0;
391 if (strcasecmp(data, "IN") == 0)
392 ctx->classes[n++] = C_IN;
393 else
394 if (strcasecmp(data, "HS") == 0)
395 ctx->classes[n++] =
396 C_HS;
397 data = p;
398 }
399 while (n < 2)
400 ctx->classes[n++] = 0;
401 }
402 }
403 }
404 fclose(fp);
405
406 if (!ctx->rhs || ctx->classes[0] == 0 ||
407 ctx->classes[0] == ctx->classes[1]) {
408 errno = ENOEXEC;
409 return -1;
410 }
411 return 0;
412 }
413
414 /*
415 * get_txt_records --
416 * Given a DNS class and a DNS name, do a lookup for TXT records, and
417 * return a list of them.
418 */
419 static char **
420 get_txt_records(qclass, name)
421 int qclass;
422 const char *name;
423 {
424 HEADER *hp;
425 unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
426 char *dst, **list;
427 int ancount, qdcount, i, j, n, skip, type, class, len;
428
429 _DIAGASSERT(name != NULL);
430
431 /* Make sure the resolver is initialized. */
432 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
433 return NULL;
434
435 /* Construct the query. */
436 n = res_mkquery(QUERY, name, qclass, T_TXT, NULL, 0,
437 NULL, qbuf, PACKETSZ);
438 if (n < 0)
439 return NULL;
440
441 /* Send the query. */
442 n = res_send(qbuf, n, abuf, MAX_HESRESP);
443 if (n < 0) {
444 errno = ECONNREFUSED;
445 return NULL;
446 }
447 /* Parse the header of the result. */
448 hp = (HEADER *) (void *) abuf;
449 ancount = ntohs(hp->ancount);
450 qdcount = ntohs(hp->qdcount);
451 p = abuf + sizeof(HEADER);
452 eom = abuf + n;
453
454 /*
455 * Skip questions, trying to get to the answer section
456 * which follows.
457 */
458 for (i = 0; i < qdcount; i++) {
459 skip = dn_skipname(p, eom);
460 if (skip < 0 || p + skip + QFIXEDSZ > eom) {
461 errno = EMSGSIZE;
462 return NULL;
463 }
464 p += skip + QFIXEDSZ;
465 }
466
467 /* Allocate space for the text record answers. */
468 list = malloc((ancount + 1) * sizeof(char *));
469 if (!list) {
470 errno = ENOMEM;
471 return NULL;
472 }
473 /* Parse the answers. */
474 j = 0;
475 for (i = 0; i < ancount; i++) {
476 /* Parse the header of this answer. */
477 skip = dn_skipname(p, eom);
478 if (skip < 0 || p + skip + 10 > eom)
479 break;
480 type = p[skip + 0] << 8 | p[skip + 1];
481 class = p[skip + 2] << 8 | p[skip + 3];
482 len = p[skip + 8] << 8 | p[skip + 9];
483 p += skip + 10;
484 if (p + len > eom) {
485 errno = EMSGSIZE;
486 break;
487 }
488 /* Skip entries of the wrong class and type. */
489 if (class != qclass || type != T_TXT) {
490 p += len;
491 continue;
492 }
493 /* Allocate space for this answer. */
494 list[j] = malloc((size_t)len);
495 if (!list[j]) {
496 errno = ENOMEM;
497 break;
498 }
499 dst = list[j++];
500
501 /* Copy answer data into the allocated area. */
502 eor = p + len;
503 while (p < eor) {
504 n = (unsigned char) *p++;
505 if (p + n > eor) {
506 errno = EMSGSIZE;
507 break;
508 }
509 memcpy(dst, p, (size_t)n);
510 p += n;
511 dst += n;
512 }
513 if (p < eor) {
514 errno = EMSGSIZE;
515 break;
516 }
517 *dst = 0;
518 }
519
520 /*
521 * If we didn't terminate the loop normally, something
522 * went wrong.
523 */
524 if (i < ancount) {
525 for (i = 0; i < j; i++)
526 free(list[i]);
527 free(list);
528 return NULL;
529 }
530 if (j == 0) {
531 errno = ENOENT;
532 free(list);
533 return NULL;
534 }
535 list[j] = NULL;
536 return list;
537 }
538
539 /*
540 * COMPATIBILITY FUNCTIONS
541 */
542
543 static int inited = 0;
544 static void *context;
545 static int errval = HES_ER_UNINIT;
546
547 int
548 hes_init()
549 {
550 init_context();
551 return errval;
552 }
553
554 char *
555 hes_to_bind(name, type)
556 const char *name;
557 const char *type;
558 {
559 static char *bindname;
560
561 _DIAGASSERT(name != NULL);
562 _DIAGASSERT(type != NULL);
563 #ifdef _DIAGNOSTIC
564 if (name == NULL || type == NULL)
565 return (NULL);
566 #endif
567
568 if (init_context() < 0)
569 return NULL;
570 if (bindname)
571 free(bindname);
572 bindname = hesiod_to_bind(context, name, type);
573 if (!bindname)
574 translate_errors();
575 return bindname;
576 }
577
578 char **
579 hes_resolve(name, type)
580 const char *name;
581 const char *type;
582 {
583 static char **list;
584
585 _DIAGASSERT(name != NULL);
586 _DIAGASSERT(type != NULL);
587 #ifdef _DIAGNOSTIC
588 if (name == NULL || type == NULL)
589 return (NULL);
590 #endif
591
592 if (init_context() < 0)
593 return NULL;
594
595 /*
596 * In the old Hesiod interface, the caller was responsible for
597 * freeing the returned strings but not the vector of strings itself.
598 */
599 if (list)
600 free(list);
601
602 list = hesiod_resolve(context, name, type);
603 if (!list)
604 translate_errors();
605 return list;
606 }
607
608 int
609 hes_error()
610 {
611 return errval;
612 }
613
614 void
615 hes_free(hp)
616 char **hp;
617 {
618 hesiod_free_list(context, hp);
619 }
620
621 static int
622 init_context()
623 {
624 if (!inited) {
625 inited = 1;
626 if (hesiod_init(&context) < 0) {
627 errval = HES_ER_CONFIG;
628 return -1;
629 }
630 errval = HES_ER_OK;
631 }
632 return 0;
633 }
634
635 static void
636 translate_errors()
637 {
638 switch (errno) {
639 case ENOENT:
640 errval = HES_ER_NOTFOUND;
641 break;
642 case ECONNREFUSED:
643 case EMSGSIZE:
644 errval = HES_ER_NET;
645 break;
646 case EFAULT:
647 case ENOMEM:
648 default:
649 /* Not a good match, but the best we can do. */
650 errval = HES_ER_CONFIG;
651 break;
652 }
653 }
654