hesiod.c revision 1.13.2.2 1 /* $NetBSD: hesiod.c,v 1.13.2.2 2002/04/17 11:51:18 he 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.13.2.2 2002/04/17 11:51:18 he 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 #include <unistd.h>
74
75 #ifdef __weak_alias
76 __weak_alias(hesiod_init,_hesiod_init)
77 __weak_alias(hesiod_end,_hesiod_end)
78 __weak_alias(hesiod_to_bind,_hesiod_to_bind)
79 __weak_alias(hesiod_resolve,_hesiod_resolve)
80 __weak_alias(hesiod_free_list,_hesiod_free_list)
81 __weak_alias(hes_init,_hes_init)
82 __weak_alias(hes_to_bind,_hes_to_bind)
83 __weak_alias(hes_resolve,_hes_resolve)
84 __weak_alias(hes_error,_hes_error)
85 __weak_alias(hes_free,_hes_free)
86 #endif
87
88 struct hesiod_p {
89 char *lhs; /* normally ".ns" */
90 char *rhs; /* AKA the default hesiod domain */
91 int classes[2]; /* The class search order. */
92 };
93
94 #define MAX_HESRESP 1024
95
96 static int read_config_file __P((struct hesiod_p *, const char *));
97 static char **get_txt_records __P((int, const char *));
98 static int init_context __P((void));
99 static void translate_errors __P((void));
100
101
102 /*
103 * hesiod_init --
104 * initialize a hesiod_p.
105 */
106 int
107 hesiod_init(context)
108 void **context;
109 {
110 struct hesiod_p *ctx;
111 const char *p, *configname;
112 int serrno;
113
114 _DIAGASSERT(context != NULL);
115
116 ctx = malloc(sizeof(struct hesiod_p));
117 if (ctx) {
118 *context = ctx;
119 /*
120 * don't permit overrides from environment
121 * for set.id programs
122 */
123 if (issetugid())
124 configname = NULL;
125 else
126 configname = getenv("HESIOD_CONFIG");
127 if (!configname)
128 configname = _PATH_HESIOD_CONF;
129 if (read_config_file(ctx, configname) >= 0) {
130 /*
131 * The default rhs can be overridden by an
132 * environment variable, unless set.id.
133 */
134 if (issetugid())
135 p = NULL;
136 else
137 p = getenv("HES_DOMAIN");
138 if (p) {
139 if (ctx->rhs)
140 free(ctx->rhs);
141 ctx->rhs = malloc(strlen(p) + 2);
142 if (ctx->rhs) {
143 *ctx->rhs = '.';
144 strcpy(ctx->rhs + 1,
145 (*p == '.') ? p + 1 : p);
146 return 0;
147 } else
148 errno = ENOMEM;
149 } else
150 return 0;
151 }
152 } else
153 errno = ENOMEM;
154
155 serrno = errno;
156 if (ctx->lhs)
157 free(ctx->lhs);
158 if (ctx->rhs)
159 free(ctx->rhs);
160 if (ctx)
161 free(ctx);
162 errno = serrno;
163 return -1;
164 }
165
166 /*
167 * hesiod_end --
168 * Deallocates the hesiod_p.
169 */
170 void
171 hesiod_end(context)
172 void *context;
173 {
174 struct hesiod_p *ctx = (struct hesiod_p *) context;
175
176 _DIAGASSERT(context != NULL);
177
178 free(ctx->rhs);
179 if (ctx->lhs)
180 free(ctx->lhs);
181 free(ctx);
182 }
183
184 /*
185 * hesiod_to_bind --
186 * takes a hesiod (name, type) and returns a DNS
187 * name which is to be resolved.
188 */
189 char *
190 hesiod_to_bind(void *context, const char *name, const char *type)
191 {
192 struct hesiod_p *ctx = (struct hesiod_p *) context;
193 char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL;
194 const char *rhs;
195 int len;
196
197 _DIAGASSERT(context != NULL);
198 _DIAGASSERT(name != NULL);
199 _DIAGASSERT(type != NULL);
200
201 if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) {
202 errno = EMSGSIZE;
203 return NULL;
204 }
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
280 bindname = hesiod_to_bind(context, name, type);
281 if (!bindname)
282 return NULL;
283
284 retvec = get_txt_records(ctx->classes[0], bindname);
285 if (retvec == NULL && errno == ENOENT && ctx->classes[1])
286 retvec = get_txt_records(ctx->classes[1], bindname);
287
288 free(bindname);
289 return retvec;
290 }
291
292 /*ARGSUSED*/
293 void
294 hesiod_free_list(context, list)
295 void *context;
296 char **list;
297 {
298 char **p;
299
300 _DIAGASSERT(context != NULL);
301
302 if (list == NULL)
303 return;
304 for (p = list; *p; p++)
305 free(*p);
306 free(list);
307 }
308
309
310 /* read_config_file --
311 * Parse the /etc/hesiod.conf file. Returns 0 on success,
312 * -1 on failure. On failure, it might leave values in ctx->lhs
313 * or ctx->rhs which need to be freed by the caller.
314 */
315 static int
316 read_config_file(ctx, filename)
317 struct hesiod_p *ctx;
318 const char *filename;
319 {
320 char *key, *data, *p, **which;
321 char buf[MAXDNAME + 7];
322 int n;
323 FILE *fp;
324
325 _DIAGASSERT(ctx != NULL);
326 _DIAGASSERT(filename != NULL);
327
328 /* Set default query classes. */
329 ctx->classes[0] = C_IN;
330 ctx->classes[1] = C_HS;
331
332 /* Try to open the configuration file. */
333 fp = fopen(filename, "r");
334 if (!fp) {
335 /* Use compiled in default domain names. */
336 ctx->lhs = strdup(DEF_LHS);
337 ctx->rhs = strdup(DEF_RHS);
338 if (ctx->lhs && ctx->rhs)
339 return 0;
340 else {
341 errno = ENOMEM;
342 return -1;
343 }
344 }
345 ctx->lhs = NULL;
346 ctx->rhs = NULL;
347 while (fgets(buf, sizeof(buf), fp) != NULL) {
348 p = buf;
349 if (*p == '#' || *p == '\n' || *p == '\r')
350 continue;
351 while (*p == ' ' || *p == '\t')
352 p++;
353 key = p;
354 while (*p != ' ' && *p != '\t' && *p != '=')
355 p++;
356 *p++ = 0;
357
358 while (isspace(*p) || *p == '=')
359 p++;
360 data = p;
361 while (!isspace(*p))
362 p++;
363 *p = 0;
364
365 if (strcasecmp(key, "lhs") == 0 ||
366 strcasecmp(key, "rhs") == 0) {
367 which = (strcasecmp(key, "lhs") == 0)
368 ? &ctx->lhs : &ctx->rhs;
369 *which = strdup(data);
370 if (!*which) {
371 errno = ENOMEM;
372 return -1;
373 }
374 } else {
375 if (strcasecmp(key, "classes") == 0) {
376 n = 0;
377 while (*data && n < 2) {
378 p = data;
379 while (*p && *p != ',')
380 p++;
381 if (*p)
382 *p++ = 0;
383 if (strcasecmp(data, "IN") == 0)
384 ctx->classes[n++] = C_IN;
385 else
386 if (strcasecmp(data, "HS") == 0)
387 ctx->classes[n++] =
388 C_HS;
389 data = p;
390 }
391 while (n < 2)
392 ctx->classes[n++] = 0;
393 }
394 }
395 }
396 fclose(fp);
397
398 if (!ctx->rhs || ctx->classes[0] == 0 ||
399 ctx->classes[0] == ctx->classes[1]) {
400 errno = ENOEXEC;
401 return -1;
402 }
403 return 0;
404 }
405
406 /*
407 * get_txt_records --
408 * Given a DNS class and a DNS name, do a lookup for TXT records, and
409 * return a list of them.
410 */
411 static char **
412 get_txt_records(qclass, name)
413 int qclass;
414 const char *name;
415 {
416 HEADER *hp;
417 unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
418 char *dst, **list;
419 int ancount, qdcount, i, j, n, skip, type, class, len;
420
421 _DIAGASSERT(name != NULL);
422
423 /* Make sure the resolver is initialized. */
424 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
425 return NULL;
426
427 /* Construct the query. */
428 n = res_mkquery(QUERY, name, qclass, T_TXT, NULL, 0,
429 NULL, qbuf, PACKETSZ);
430 if (n < 0) {
431 errno = EMSGSIZE;
432 return NULL;
433 }
434
435 /* Send the query. */
436 n = res_send(qbuf, n, abuf, MAX_HESRESP);
437 if (n < 0) {
438 errno = ECONNREFUSED;
439 return NULL;
440 }
441 /* Parse the header of the result. */
442 hp = (HEADER *) (void *) abuf;
443 ancount = ntohs(hp->ancount);
444 qdcount = ntohs(hp->qdcount);
445 p = abuf + sizeof(HEADER);
446 eom = abuf + n;
447
448 /*
449 * Skip questions, trying to get to the answer section
450 * which follows.
451 */
452 for (i = 0; i < qdcount; i++) {
453 skip = dn_skipname(p, eom);
454 if (skip < 0 || p + skip + QFIXEDSZ > eom) {
455 errno = EMSGSIZE;
456 return NULL;
457 }
458 p += skip + QFIXEDSZ;
459 }
460
461 /* Allocate space for the text record answers. */
462 list = malloc((ancount + 1) * sizeof(char *));
463 if (!list) {
464 errno = ENOMEM;
465 return NULL;
466 }
467 /* Parse the answers. */
468 j = 0;
469 for (i = 0; i < ancount; i++) {
470 /* Parse the header of this answer. */
471 skip = dn_skipname(p, eom);
472 if (skip < 0 || p + skip + 10 > eom)
473 break;
474 type = p[skip + 0] << 8 | p[skip + 1];
475 class = p[skip + 2] << 8 | p[skip + 3];
476 len = p[skip + 8] << 8 | p[skip + 9];
477 p += skip + 10;
478 if (p + len > eom) {
479 errno = EMSGSIZE;
480 break;
481 }
482 /* Skip entries of the wrong class and type. */
483 if (class != qclass || type != T_TXT) {
484 p += len;
485 continue;
486 }
487 /* Allocate space for this answer. */
488 list[j] = malloc((size_t)len);
489 if (!list[j]) {
490 errno = ENOMEM;
491 break;
492 }
493 dst = list[j++];
494
495 /* Copy answer data into the allocated area. */
496 eor = p + len;
497 while (p < eor) {
498 n = (unsigned char) *p++;
499 if (p + n > eor) {
500 errno = EMSGSIZE;
501 break;
502 }
503 memcpy(dst, p, (size_t)n);
504 p += n;
505 dst += n;
506 }
507 if (p < eor) {
508 errno = EMSGSIZE;
509 break;
510 }
511 *dst = 0;
512 }
513
514 /*
515 * If we didn't terminate the loop normally, something
516 * went wrong.
517 */
518 if (i < ancount) {
519 for (i = 0; i < j; i++)
520 free(list[i]);
521 free(list);
522 return NULL;
523 }
524 if (j == 0) {
525 errno = ENOENT;
526 free(list);
527 return NULL;
528 }
529 list[j] = NULL;
530 return list;
531 }
532
533 /*
534 * COMPATIBILITY FUNCTIONS
535 */
536
537 static int inited = 0;
538 static void *context;
539 static int errval = HES_ER_UNINIT;
540
541 int
542 hes_init()
543 {
544 init_context();
545 return errval;
546 }
547
548 char *
549 hes_to_bind(name, type)
550 const char *name;
551 const char *type;
552 {
553 static char *bindname;
554
555 _DIAGASSERT(name != NULL);
556 _DIAGASSERT(type != NULL);
557
558 if (init_context() < 0)
559 return NULL;
560 if (bindname)
561 free(bindname);
562 bindname = hesiod_to_bind(context, name, type);
563 if (!bindname)
564 translate_errors();
565 return bindname;
566 }
567
568 char **
569 hes_resolve(name, type)
570 const char *name;
571 const char *type;
572 {
573 static char **list;
574
575 _DIAGASSERT(name != NULL);
576 _DIAGASSERT(type != NULL);
577
578 if (init_context() < 0)
579 return NULL;
580
581 /*
582 * In the old Hesiod interface, the caller was responsible for
583 * freeing the returned strings but not the vector of strings itself.
584 */
585 if (list)
586 free(list);
587
588 list = hesiod_resolve(context, name, type);
589 if (!list)
590 translate_errors();
591 return list;
592 }
593
594 int
595 hes_error()
596 {
597 return errval;
598 }
599
600 void
601 hes_free(hp)
602 char **hp;
603 {
604 hesiod_free_list(context, hp);
605 }
606
607 static int
608 init_context()
609 {
610 if (!inited) {
611 inited = 1;
612 if (hesiod_init(&context) < 0) {
613 errval = HES_ER_CONFIG;
614 return -1;
615 }
616 errval = HES_ER_OK;
617 }
618 return 0;
619 }
620
621 static void
622 translate_errors()
623 {
624 switch (errno) {
625 case ENOENT:
626 errval = HES_ER_NOTFOUND;
627 break;
628 case ECONNREFUSED:
629 case EMSGSIZE:
630 errval = HES_ER_NET;
631 break;
632 case EFAULT:
633 case ENOMEM:
634 default:
635 /* Not a good match, but the best we can do. */
636 errval = HES_ER_CONFIG;
637 break;
638 }
639 }
640