1 /* $NetBSD: hesiod.c,v 1.23 2005/02/28 01:25:24 lukem Exp $ */
3 /* Copyright (c) 1996 by Internet Software Consortium.
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.
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
19 /* Copyright 1996 by the Massachusetts Institute of Technology.
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.
34 /* This file is part of the hesiod library. It implements the core
35 * portion of the hesiod resolver.
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
42 * This implementation is thread-safe because it uses res_nsend().
45 #include <sys/cdefs.h>
47 #if defined(LIBC_SCCS) && !defined(lint)
48 __IDSTRING(rcsid_hesiod_c
,
49 "#Id: hesiod.c,v 1.18.2.1 1997/01/03 20:48:20 ghudson Exp #");
50 __IDSTRING(rcsid_hesiod_p_h
,
51 "#Id: hesiod_p.h,v 1.1 1996/12/08 21:39:37 ghudson Exp #");
52 __IDSTRING(rcsid_hescompat_c
,
53 "#Id: hescompat.c,v 1.1.2.1 1996/12/16 08:37:45 ghudson Exp #");
54 __RCSID("$NetBSD: hesiod.c,v 1.23 2005/02/28 01:25:24 lukem Exp $");
55 #endif /* LIBC_SCCS and not lint */
57 #include "namespace.h"
59 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <netinet/in.h>
62 #include <arpa/nameser.h>
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
)
88 char *lhs
; /* normally ".ns" */
89 char *rhs
; /* AKA the default hesiod domain */
90 int classes
[2]; /* The class search order. */
93 #define MAX_HESRESP 1024
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));
103 * initialize a hesiod_p.
109 struct hesiod_p
*ctx
;
110 const char *p
, *configname
;
113 _DIAGASSERT(context
!= NULL
);
115 ctx
= calloc(1, sizeof(struct hesiod_p
));
119 * don't permit overrides from environment
120 * for set.id programs
125 configname
= getenv("HESIOD_CONFIG");
127 configname
= _PATH_HESIOD_CONF
;
128 if (read_config_file(ctx
, configname
) >= 0) {
130 * The default rhs can be overridden by an
131 * environment variable, unless set.id.
136 p
= getenv("HES_DOMAIN");
140 ctx
->rhs
= malloc(strlen(p
) + 2);
144 (*p
== '.') ? p
+ 1 : p
);
168 * Deallocates the hesiod_p.
174 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
176 _DIAGASSERT(context
!= NULL
);
186 * takes a hesiod (name, type) and returns a DNS
187 * name which is to be resolved.
190 hesiod_to_bind(void *context
, const char *name
, const char *type
)
192 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
193 char bindname
[MAXDNAME
], *p
, *ret
, **rhs_list
= NULL
;
197 _DIAGASSERT(context
!= NULL
);
198 _DIAGASSERT(name
!= NULL
);
199 _DIAGASSERT(type
!= NULL
);
201 if (strlcpy(bindname
, name
, sizeof(bindname
)) >= sizeof(bindname
)) {
207 * Find the right right hand side to use, possibly
208 * truncating bindname.
210 p
= strchr(bindname
, '@');
214 rhs
= name
+ (p
- bindname
);
216 rhs_list
= hesiod_resolve(context
, p
, "rhs-extension");
227 /* See if we have enough room. */
228 len
= strlen(bindname
) + 1 + strlen(type
);
230 len
+= strlen(ctx
->lhs
) + ((ctx
->lhs
[0] != '.') ? 1 : 0);
231 len
+= strlen(rhs
) + ((rhs
[0] != '.') ? 1 : 0);
232 if (len
> sizeof(bindname
) - 1) {
234 hesiod_free_list(context
, rhs_list
);
238 /* Put together the rest of the domain. */
239 strlcat(bindname
, ".", sizeof(bindname
));
240 strlcat(bindname
, type
, sizeof(bindname
));
241 /* Only append lhs if it isn't empty. */
242 if (ctx
->lhs
&& ctx
->lhs
[0] != '\0' ) {
243 if (ctx
->lhs
[0] != '.')
244 strlcat(bindname
, ".", sizeof(bindname
));
245 strlcat(bindname
, ctx
->lhs
, sizeof(bindname
));
248 strlcat(bindname
, ".", sizeof(bindname
));
249 strlcat(bindname
, rhs
, sizeof(bindname
));
251 /* rhs_list is no longer needed, since we're done with rhs. */
253 hesiod_free_list(context
, rhs_list
);
255 /* Make a copy of the result and return it to the caller. */
256 ret
= strdup(bindname
);
264 * Given a hesiod name and type, return an array of strings returned
268 hesiod_resolve(context
, name
, type
)
273 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
274 char *bindname
, **retvec
;
276 _DIAGASSERT(context
!= NULL
);
277 _DIAGASSERT(name
!= NULL
);
278 _DIAGASSERT(type
!= NULL
);
280 bindname
= hesiod_to_bind(context
, name
, type
);
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
);
294 hesiod_free_list(context
, list
)
300 _DIAGASSERT(context
!= NULL
);
304 for (p
= list
; *p
; p
++)
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.
316 read_config_file(ctx
, filename
)
317 struct hesiod_p
*ctx
;
318 const char *filename
;
320 char *key
, *data
, *p
, **which
;
321 char buf
[MAXDNAME
+ 7];
325 _DIAGASSERT(ctx
!= NULL
);
326 _DIAGASSERT(filename
!= NULL
);
328 /* Set default query classes. */
329 ctx
->classes
[0] = C_IN
;
330 ctx
->classes
[1] = C_HS
;
332 /* Try to open the configuration file. */
333 fp
= fopen(filename
, "r");
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
)
347 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
349 if (*p
== '#' || *p
== '\n' || *p
== '\r')
351 while (*p
== ' ' || *p
== '\t')
354 while (*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
)
362 while (isspace((u_char
) *p
) || *p
== '=')
369 while (!isspace((u_char
) *p
) && *p
)
374 if (strcasecmp(key
, "lhs") == 0 ||
375 strcasecmp(key
, "rhs") == 0) {
376 which
= (strcasecmp(key
, "lhs") == 0)
377 ? &ctx
->lhs
: &ctx
->rhs
;
378 *which
= strdup(data
);
384 if (strcasecmp(key
, "classes") == 0) {
386 while (*data
&& n
< 2) {
388 while (*p
&& *p
!= ',')
392 if (strcasecmp(data
, "IN") == 0)
393 ctx
->classes
[n
++] = C_IN
;
395 if (strcasecmp(data
, "HS") == 0)
401 ctx
->classes
[n
++] = 0;
407 if (!ctx
->rhs
|| ctx
->classes
[0] == 0 ||
408 ctx
->classes
[0] == ctx
->classes
[1]) {
417 * Given a DNS class and a DNS name, do a lookup for TXT records, and
418 * return a list of them.
421 get_txt_records(qclass
, name
)
426 unsigned char qbuf
[PACKETSZ
], abuf
[MAX_HESRESP
], *p
, *eom
, *eor
;
428 int ancount
, qdcount
, i
, j
, n
, skip
, type
, class, len
;
429 res_state res
= __res_get_state();
434 _DIAGASSERT(name
!= NULL
);
436 /* Construct the query. */
437 n
= res_nmkquery(res
, QUERY
, name
, qclass
, T_TXT
, NULL
, 0,
438 NULL
, qbuf
, PACKETSZ
);
441 __res_put_state(res
);
445 /* Send the query. */
446 n
= res_nsend(res
, qbuf
, n
, abuf
, MAX_HESRESP
);
447 __res_put_state(res
);
449 errno
= ECONNREFUSED
;
452 /* Parse the header of the result. */
453 hp
= (HEADER
*) (void *) abuf
;
454 ancount
= ntohs(hp
->ancount
);
455 qdcount
= ntohs(hp
->qdcount
);
456 p
= abuf
+ sizeof(HEADER
);
460 * Skip questions, trying to get to the answer section
463 for (i
= 0; i
< qdcount
; i
++) {
464 skip
= dn_skipname(p
, eom
);
465 if (skip
< 0 || p
+ skip
+ QFIXEDSZ
> eom
) {
469 p
+= skip
+ QFIXEDSZ
;
472 /* Allocate space for the text record answers. */
473 list
= malloc((ancount
+ 1) * sizeof(char *));
478 /* Parse the answers. */
480 for (i
= 0; i
< ancount
; i
++) {
481 /* Parse the header of this answer. */
482 skip
= dn_skipname(p
, eom
);
483 if (skip
< 0 || p
+ skip
+ 10 > eom
)
485 type
= p
[skip
+ 0] << 8 | p
[skip
+ 1];
486 class = p
[skip
+ 2] << 8 | p
[skip
+ 3];
487 len
= p
[skip
+ 8] << 8 | p
[skip
+ 9];
493 /* Skip entries of the wrong class and type. */
494 if (class != qclass
|| type
!= T_TXT
) {
498 /* Allocate space for this answer. */
499 list
[j
] = malloc((size_t)len
);
506 /* Copy answer data into the allocated area. */
509 n
= (unsigned char) *p
++;
514 memcpy(dst
, p
, (size_t)n
);
526 * If we didn't terminate the loop normally, something
530 for (i
= 0; i
< j
; i
++)
545 * COMPATIBILITY FUNCTIONS
548 static int inited
= 0;
549 static void *context
;
550 static int errval
= HES_ER_UNINIT
;
560 hes_to_bind(name
, type
)
564 static char *bindname
;
566 _DIAGASSERT(name
!= NULL
);
567 _DIAGASSERT(type
!= NULL
);
569 if (init_context() < 0)
573 bindname
= hesiod_to_bind(context
, name
, type
);
580 hes_resolve(name
, type
)
586 _DIAGASSERT(name
!= NULL
);
587 _DIAGASSERT(type
!= NULL
);
589 if (init_context() < 0)
593 * In the old Hesiod interface, the caller was responsible for
594 * freeing the returned strings but not the vector of strings itself.
599 list
= hesiod_resolve(context
, name
, type
);
615 hesiod_free_list(context
, hp
);
623 if (hesiod_init(&context
) < 0) {
624 errval
= HES_ER_CONFIG
;
637 errval
= HES_ER_NOTFOUND
;
644 /* Not a good match, but the best we can do. */
645 errval
= HES_ER_CONFIG
;