hesiod.c revision 1.8 1 /* $NetBSD: hesiod.c,v 1.8 1999/01/25 22:36:19 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.8 1999/01/25 22:36:19 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 <ctype.h>
66 #include <errno.h>
67 #include <hesiod.h>
68 #include <resolv.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72
73 #ifdef __weak_alias
74 __weak_alias(hesiod_init,_hesiod_init);
75 __weak_alias(hesiod_end,_hesiod_end);
76 __weak_alias(hesiod_to_bind,_hesiod_to_bind);
77 __weak_alias(hesiod_resolve,_hesiod_resolve);
78 __weak_alias(hesiod_free_list,_hesiod_free_list);
79 __weak_alias(hes_init,_hes_init);
80 __weak_alias(hes_to_bind,_hes_to_bind);
81 __weak_alias(hes_resolve,_hes_resolve);
82 __weak_alias(hes_error,_hes_error);
83 __weak_alias(hes_free,_hes_free);
84 #endif
85
86 struct hesiod_p {
87 char *lhs; /* normally ".ns" */
88 char *rhs; /* AKA the default hesiod domain */
89 int classes[2]; /* The class search order. */
90 };
91
92 #define MAX_HESRESP 1024
93
94 static int read_config_file __P((struct hesiod_p *, const char *));
95 static char **get_txt_records __P((int, const char *));
96 static int init_context __P((void));
97 static void translate_errors __P((void));
98
99
100 /*
101 * hesiod_init --
102 * initialize a hesiod_p.
103 */
104 int
105 hesiod_init(context)
106 void **context;
107 {
108 struct hesiod_p *ctx;
109 const char *p, *configname;
110
111 ctx = malloc(sizeof(struct hesiod_p));
112 if (ctx) {
113 *context = ctx;
114 configname = getenv("HESIOD_CONFIG");
115 if (!configname)
116 configname = _PATH_HESIOD_CONF;
117 if (read_config_file(ctx, configname) >= 0) {
118 /*
119 * The default rhs can be overridden by an
120 * environment variable.
121 */
122 p = getenv("HES_DOMAIN");
123 if (p) {
124 if (ctx->rhs)
125 free(ctx->rhs);
126 ctx->rhs = malloc(strlen(p) + 2);
127 if (ctx->rhs) {
128 *ctx->rhs = '.';
129 strcpy(ctx->rhs + 1,
130 (*p == '.') ? p + 1 : p);
131 return 0;
132 } else
133 errno = ENOMEM;
134 } else
135 return 0;
136 }
137 } else
138 errno = ENOMEM;
139
140 if (ctx->lhs)
141 free(ctx->lhs);
142 if (ctx->rhs)
143 free(ctx->rhs);
144 if (ctx)
145 free(ctx);
146 return -1;
147 }
148
149 /*
150 * hesiod_end --
151 * Deallocates the hesiod_p.
152 */
153 void
154 hesiod_end(context)
155 void *context;
156 {
157 struct hesiod_p *ctx = (struct hesiod_p *) context;
158
159 free(ctx->rhs);
160 if (ctx->lhs)
161 free(ctx->lhs);
162 free(ctx);
163 }
164
165 /*
166 * hesiod_to_bind --
167 * takes a hesiod (name, type) and returns a DNS
168 * name which is to be resolved.
169 */
170 char *
171 hesiod_to_bind(void *context, const char *name, const char *type)
172 {
173 struct hesiod_p *ctx = (struct hesiod_p *) context;
174 char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL;
175 const char *rhs;
176 int len;
177
178 strcpy(bindname, name);
179
180 /*
181 * Find the right right hand side to use, possibly
182 * truncating bindname.
183 */
184 p = strchr(bindname, '@');
185 if (p) {
186 *p++ = 0;
187 if (strchr(p, '.'))
188 rhs = name + (p - bindname);
189 else {
190 rhs_list = hesiod_resolve(context, p, "rhs-extension");
191 if (rhs_list)
192 rhs = *rhs_list;
193 else {
194 errno = ENOENT;
195 return NULL;
196 }
197 }
198 } else
199 rhs = ctx->rhs;
200
201 /* See if we have enough room. */
202 len = strlen(bindname) + 1 + strlen(type);
203 if (ctx->lhs)
204 len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
205 len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
206 if (len > sizeof(bindname) - 1) {
207 if (rhs_list)
208 hesiod_free_list(context, rhs_list);
209 errno = EMSGSIZE;
210 return NULL;
211 }
212 /* Put together the rest of the domain. */
213 strcat(bindname, ".");
214 strcat(bindname, type);
215 if (ctx->lhs) {
216 if (ctx->lhs[0] != '.')
217 strcat(bindname, ".");
218 strcat(bindname, ctx->lhs);
219 }
220 if (rhs[0] != '.')
221 strcat(bindname, ".");
222 strcat(bindname, rhs);
223
224 /* rhs_list is no longer needed, since we're done with rhs. */
225 if (rhs_list)
226 hesiod_free_list(context, rhs_list);
227
228 /* Make a copy of the result and return it to the caller. */
229 ret = strdup(bindname);
230 if (!ret)
231 errno = ENOMEM;
232 return ret;
233 }
234
235 /*
236 * hesiod_resolve --
237 * Given a hesiod name and type, return an array of strings returned
238 * by the resolver.
239 */
240 char **
241 hesiod_resolve(context, name, type)
242 void *context;
243 const char *name;
244 const char *type;
245 {
246 struct hesiod_p *ctx = (struct hesiod_p *) context;
247 char *bindname, **retvec;
248
249 bindname = hesiod_to_bind(context, name, type);
250 if (!bindname)
251 return NULL;
252
253 retvec = get_txt_records(ctx->classes[0], bindname);
254 if (retvec == NULL && errno == ENOENT && ctx->classes[1])
255 retvec = get_txt_records(ctx->classes[1], bindname);
256
257 free(bindname);
258 return retvec;
259 }
260
261 /*ARGSUSED*/
262 void
263 hesiod_free_list(context, list)
264 void *context;
265 char **list;
266 {
267 char **p;
268
269 if (list == NULL)
270 return;
271 for (p = list; *p; p++)
272 free(*p);
273 free(list);
274 }
275
276
277 /* read_config_file --
278 * Parse the /etc/hesiod.conf file. Returns 0 on success,
279 * -1 on failure. On failure, it might leave values in ctx->lhs
280 * or ctx->rhs which need to be freed by the caller.
281 */
282 static int
283 read_config_file(ctx, filename)
284 struct hesiod_p *ctx;
285 const char *filename;
286 {
287 char *key, *data, *p, **which;
288 char buf[MAXDNAME + 7];
289 int n;
290 FILE *fp;
291
292 /* Set default query classes. */
293 ctx->classes[0] = C_IN;
294 ctx->classes[1] = C_HS;
295
296 /* Try to open the configuration file. */
297 fp = fopen(filename, "r");
298 if (!fp) {
299 /* Use compiled in default domain names. */
300 ctx->lhs = strdup(DEF_LHS);
301 ctx->rhs = strdup(DEF_RHS);
302 if (ctx->lhs && ctx->rhs)
303 return 0;
304 else {
305 errno = ENOMEM;
306 return -1;
307 }
308 }
309 ctx->lhs = NULL;
310 ctx->rhs = NULL;
311 while (fgets(buf, sizeof(buf), fp) != NULL) {
312 p = buf;
313 if (*p == '#' || *p == '\n' || *p == '\r')
314 continue;
315 while (*p == ' ' || *p == '\t')
316 p++;
317 key = p;
318 while (*p != ' ' && *p != '\t' && *p != '=')
319 p++;
320 *p++ = 0;
321
322 while (isspace(*p) || *p == '=')
323 p++;
324 data = p;
325 while (!isspace(*p))
326 p++;
327 *p = 0;
328
329 if (strcasecmp(key, "lhs") == 0 ||
330 strcasecmp(key, "rhs") == 0) {
331 which = (strcasecmp(key, "lhs") == 0)
332 ? &ctx->lhs : &ctx->rhs;
333 *which = strdup(data);
334 if (!*which) {
335 errno = ENOMEM;
336 return -1;
337 }
338 } else {
339 if (strcasecmp(key, "classes") == 0) {
340 n = 0;
341 while (*data && n < 2) {
342 p = data;
343 while (*p && *p != ',')
344 p++;
345 if (*p)
346 *p++ = 0;
347 if (strcasecmp(data, "IN") == 0)
348 ctx->classes[n++] = C_IN;
349 else
350 if (strcasecmp(data, "HS") == 0)
351 ctx->classes[n++] =
352 C_HS;
353 data = p;
354 }
355 while (n < 2)
356 ctx->classes[n++] = 0;
357 }
358 }
359 }
360 fclose(fp);
361
362 if (!ctx->rhs || ctx->classes[0] == 0 ||
363 ctx->classes[0] == ctx->classes[1]) {
364 errno = ENOEXEC;
365 return -1;
366 }
367 return 0;
368 }
369
370 /*
371 * get_txt_records --
372 * Given a DNS class and a DNS name, do a lookup for TXT records, and
373 * return a list of them.
374 */
375 static char **
376 get_txt_records(qclass, name)
377 int qclass;
378 const char *name;
379 {
380 HEADER *hp;
381 unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
382 char *dst, **list;
383 int ancount, qdcount, i, j, n, skip, type, class, len;
384
385 /* Make sure the resolver is initialized. */
386 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
387 return NULL;
388
389 /* Construct the query. */
390 n = res_mkquery(QUERY, name, qclass, T_TXT, NULL, 0,
391 NULL, qbuf, PACKETSZ);
392 if (n < 0)
393 return NULL;
394
395 /* Send the query. */
396 n = res_send(qbuf, n, abuf, MAX_HESRESP);
397 if (n < 0) {
398 errno = ECONNREFUSED;
399 return NULL;
400 }
401 /* Parse the header of the result. */
402 hp = (HEADER *) (void *) abuf;
403 ancount = ntohs(hp->ancount);
404 qdcount = ntohs(hp->qdcount);
405 p = abuf + sizeof(HEADER);
406 eom = abuf + n;
407
408 /*
409 * Skip questions, trying to get to the answer section
410 * which follows.
411 */
412 for (i = 0; i < qdcount; i++) {
413 skip = dn_skipname(p, eom);
414 if (skip < 0 || p + skip + QFIXEDSZ > eom) {
415 errno = EMSGSIZE;
416 return NULL;
417 }
418 p += skip + QFIXEDSZ;
419 }
420
421 /* Allocate space for the text record answers. */
422 list = malloc((ancount + 1) * sizeof(char *));
423 if (!list) {
424 errno = ENOMEM;
425 return NULL;
426 }
427 /* Parse the answers. */
428 j = 0;
429 for (i = 0; i < ancount; i++) {
430 /* Parse the header of this answer. */
431 skip = dn_skipname(p, eom);
432 if (skip < 0 || p + skip + 10 > eom)
433 break;
434 type = p[skip + 0] << 8 | p[skip + 1];
435 class = p[skip + 2] << 8 | p[skip + 3];
436 len = p[skip + 8] << 8 | p[skip + 9];
437 p += skip + 10;
438 if (p + len > eom) {
439 errno = EMSGSIZE;
440 break;
441 }
442 /* Skip entries of the wrong class and type. */
443 if (class != qclass || type != T_TXT) {
444 p += len;
445 continue;
446 }
447 /* Allocate space for this answer. */
448 list[j] = malloc((size_t)len);
449 if (!list[j]) {
450 errno = ENOMEM;
451 break;
452 }
453 dst = list[j++];
454
455 /* Copy answer data into the allocated area. */
456 eor = p + len;
457 while (p < eor) {
458 n = (unsigned char) *p++;
459 if (p + n > eor) {
460 errno = EMSGSIZE;
461 break;
462 }
463 memcpy(dst, p, (size_t)n);
464 p += n;
465 dst += n;
466 }
467 if (p < eor) {
468 errno = EMSGSIZE;
469 break;
470 }
471 *dst = 0;
472 }
473
474 /*
475 * If we didn't terminate the loop normally, something
476 * went wrong.
477 */
478 if (i < ancount) {
479 for (i = 0; i < j; i++)
480 free(list[i]);
481 free(list);
482 return NULL;
483 }
484 if (j == 0) {
485 errno = ENOENT;
486 free(list);
487 return NULL;
488 }
489 list[j] = NULL;
490 return list;
491 }
492
493 /*
494 * COMPATIBILITY FUNCTIONS
495 */
496
497 static int inited = 0;
498 static void *context;
499 static int errval = HES_ER_UNINIT;
500
501 int
502 hes_init()
503 {
504 init_context();
505 return errval;
506 }
507
508 char *
509 hes_to_bind(name, type)
510 const char *name;
511 const char *type;
512 {
513 static char *bindname;
514 if (init_context() < 0)
515 return NULL;
516 if (bindname)
517 free(bindname);
518 bindname = hesiod_to_bind(context, name, type);
519 if (!bindname)
520 translate_errors();
521 return bindname;
522 }
523
524 char **
525 hes_resolve(name, type)
526 const char *name;
527 const char *type;
528 {
529 static char **list;
530
531 if (init_context() < 0)
532 return NULL;
533
534 /*
535 * In the old Hesiod interface, the caller was responsible for
536 * freeing the returned strings but not the vector of strings itself.
537 */
538 if (list)
539 free(list);
540
541 list = hesiod_resolve(context, name, type);
542 if (!list)
543 translate_errors();
544 return list;
545 }
546
547 int
548 hes_error()
549 {
550 return errval;
551 }
552
553 void
554 hes_free(hp)
555 char **hp;
556 {
557 hesiod_free_list(context, hp);
558 }
559
560 static int
561 init_context()
562 {
563 if (!inited) {
564 inited = 1;
565 if (hesiod_init(&context) < 0) {
566 errval = HES_ER_CONFIG;
567 return -1;
568 }
569 errval = HES_ER_OK;
570 }
571 return 0;
572 }
573
574 static void
575 translate_errors()
576 {
577 switch (errno) {
578 case ENOENT:
579 errval = HES_ER_NOTFOUND;
580 break;
581 case ECONNREFUSED:
582 case EMSGSIZE:
583 errval = HES_ER_NET;
584 break;
585 case ENOMEM:
586 default:
587 /* Not a good match, but the best we can do. */
588 errval = HES_ER_CONFIG;
589 break;
590 }
591 }
592