1 /* $NetBSD: hesiod.c,v 1.27 2012/03/20 17:44:18 matt 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.27 2012/03/20 17:44:18 matt Exp $");
55 #endif /* LIBC_SCCS and not lint */
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <netinet/in.h>
60 #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(struct hesiod_p
*, const char *);
96 static char **get_txt_records(int, const char *);
97 static int init_context(void);
98 static void translate_errors(void);
103 * initialize a hesiod_p.
106 hesiod_init(void **context
)
108 struct hesiod_p
*ctx
;
109 const char *p
, *configname
;
112 assert(context
!= NULL
);
114 ctx
= calloc(1, sizeof(struct hesiod_p
));
118 * don't permit overrides from environment
119 * for set.id programs
126 configname
= getenv("HESIOD_CONFIG");
128 configname
= _PATH_HESIOD_CONF
;
129 if (read_config_file(ctx
, configname
) >= 0) {
131 * The default rhs can be overridden by an
132 * environment variable, unless set.id.
139 p
= getenv("HES_DOMAIN");
143 ctx
->rhs
= malloc(strlen(p
) + 2);
147 (*p
== '.') ? p
+ 1 : p
);
171 * Deallocates the hesiod_p.
174 hesiod_end(void *context
)
176 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
178 assert(context
!= NULL
);
188 * takes a hesiod (name, type) and returns a DNS
189 * name which is to be resolved.
192 hesiod_to_bind(void *context
, const char *name
, const char *type
)
194 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
195 char bindname
[MAXDNAME
], *p
, *ret
, **rhs_list
= NULL
;
199 assert(context
!= NULL
);
200 assert(name
!= NULL
);
201 assert(type
!= NULL
);
203 if (strlcpy(bindname
, name
, sizeof(bindname
)) >= sizeof(bindname
)) {
209 * Find the right right hand side to use, possibly
210 * truncating bindname.
212 p
= strchr(bindname
, '@');
216 rhs
= name
+ (p
- bindname
);
218 rhs_list
= hesiod_resolve(context
, p
, "rhs-extension");
229 /* See if we have enough room. */
230 len
= strlen(bindname
) + 1 + strlen(type
);
232 len
+= strlen(ctx
->lhs
) + ((ctx
->lhs
[0] != '.') ? 1 : 0);
233 len
+= strlen(rhs
) + ((rhs
[0] != '.') ? 1 : 0);
234 if (len
> sizeof(bindname
) - 1) {
236 hesiod_free_list(context
, rhs_list
);
240 /* Put together the rest of the domain. */
241 strlcat(bindname
, ".", sizeof(bindname
));
242 strlcat(bindname
, type
, sizeof(bindname
));
243 /* Only append lhs if it isn't empty. */
244 if (ctx
->lhs
&& ctx
->lhs
[0] != '\0' ) {
245 if (ctx
->lhs
[0] != '.')
246 strlcat(bindname
, ".", sizeof(bindname
));
247 strlcat(bindname
, ctx
->lhs
, sizeof(bindname
));
250 strlcat(bindname
, ".", sizeof(bindname
));
251 strlcat(bindname
, rhs
, sizeof(bindname
));
253 /* rhs_list is no longer needed, since we're done with rhs. */
255 hesiod_free_list(context
, rhs_list
);
257 /* Make a copy of the result and return it to the caller. */
258 ret
= strdup(bindname
);
266 * Given a hesiod name and type, return an array of strings returned
270 hesiod_resolve(void *context
, const char *name
, const char *type
)
272 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
273 char *bindname
, **retvec
;
275 assert(context
!= NULL
);
276 assert(name
!= NULL
);
277 assert(type
!= NULL
);
279 bindname
= hesiod_to_bind(context
, name
, type
);
283 retvec
= get_txt_records(ctx
->classes
[0], bindname
);
284 if (retvec
== NULL
&& errno
== ENOENT
&& ctx
->classes
[1])
285 retvec
= get_txt_records(ctx
->classes
[1], bindname
);
293 hesiod_free_list(void *context
, char **list
)
297 assert(context
!= NULL
);
301 for (p
= list
; *p
; p
++)
307 /* read_config_file --
308 * Parse the /etc/hesiod.conf file. Returns 0 on success,
309 * -1 on failure. On failure, it might leave values in ctx->lhs
310 * or ctx->rhs which need to be freed by the caller.
313 read_config_file(struct hesiod_p
*ctx
, const char *filename
)
315 char *buf
, *key
, *data
, *p
, **which
;
320 assert(filename
!= NULL
);
322 /* Set default query classes. */
323 ctx
->classes
[0] = C_IN
;
324 ctx
->classes
[1] = C_HS
;
326 /* Try to open the configuration file. */
327 fp
= fopen(filename
, "r");
329 /* Use compiled in default domain names. */
330 ctx
->lhs
= strdup(DEF_LHS
);
331 ctx
->rhs
= strdup(DEF_RHS
);
332 if (ctx
->lhs
&& ctx
->rhs
)
341 for (; (buf
= fparseln(fp
, NULL
, NULL
, NULL
, FPARSELN_UNESCALL
))
342 != NULL
; free(buf
)) {
344 while (*p
== ' ' || *p
== '\t')
347 while (*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
)
355 while (isspace((u_char
) *p
) || *p
== '=')
362 while (!isspace((u_char
) *p
) && *p
)
367 if (strcasecmp(key
, "lhs") == 0 ||
368 strcasecmp(key
, "rhs") == 0) {
369 which
= (strcasecmp(key
, "lhs") == 0)
370 ? &ctx
->lhs
: &ctx
->rhs
;
371 *which
= strdup(data
);
379 if (strcasecmp(key
, "classes") == 0) {
381 while (*data
&& n
< 2) {
383 while (*p
&& *p
!= ',')
387 if (strcasecmp(data
, "IN") == 0)
388 ctx
->classes
[n
++] = C_IN
;
390 if (strcasecmp(data
, "HS") == 0)
396 ctx
->classes
[n
++] = 0;
402 if (!ctx
->rhs
|| ctx
->classes
[0] == 0 ||
403 ctx
->classes
[0] == ctx
->classes
[1]) {
412 * Given a DNS class and a DNS name, do a lookup for TXT records, and
413 * return a list of them.
416 get_txt_records(int qclass
, const char *name
)
419 unsigned char qbuf
[PACKETSZ
], abuf
[MAX_HESRESP
], *p
, *eom
, *eor
;
421 int ancount
, qdcount
, i
, j
, n
, skip
, type
, class, len
;
422 res_state res
= __res_get_state();
427 assert(name
!= NULL
);
429 /* Construct the query. */
430 n
= res_nmkquery(res
, QUERY
, name
, qclass
, T_TXT
, NULL
, 0,
431 NULL
, qbuf
, PACKETSZ
);
434 __res_put_state(res
);
438 /* Send the query. */
439 n
= res_nsend(res
, qbuf
, n
, abuf
, MAX_HESRESP
);
440 __res_put_state(res
);
442 errno
= ECONNREFUSED
;
445 /* Parse the header of the result. */
446 hp
= (HEADER
*) (void *) abuf
;
447 ancount
= ntohs(hp
->ancount
);
448 qdcount
= ntohs(hp
->qdcount
);
449 p
= abuf
+ sizeof(HEADER
);
453 * Skip questions, trying to get to the answer section
456 for (i
= 0; i
< qdcount
; i
++) {
457 skip
= dn_skipname(p
, eom
);
458 if (skip
< 0 || p
+ skip
+ QFIXEDSZ
> eom
) {
462 p
+= skip
+ QFIXEDSZ
;
465 /* Allocate space for the text record answers. */
466 list
= malloc((ancount
+ 1) * sizeof(char *));
471 /* Parse the answers. */
473 for (i
= 0; i
< ancount
; i
++) {
474 /* Parse the header of this answer. */
475 skip
= dn_skipname(p
, eom
);
476 if (skip
< 0 || p
+ skip
+ 10 > eom
)
478 type
= p
[skip
+ 0] << 8 | p
[skip
+ 1];
479 class = p
[skip
+ 2] << 8 | p
[skip
+ 3];
480 len
= p
[skip
+ 8] << 8 | p
[skip
+ 9];
486 /* Skip entries of the wrong class and type. */
487 if (class != qclass
|| type
!= T_TXT
) {
491 /* Allocate space for this answer. */
492 list
[j
] = malloc((size_t)len
);
499 /* Copy answer data into the allocated area. */
502 n
= (unsigned char) *p
++;
507 memcpy(dst
, p
, (size_t)n
);
519 * If we didn't terminate the loop normally, something
523 for (i
= 0; i
< j
; i
++)
538 * COMPATIBILITY FUNCTIONS
541 static int inited
= 0;
542 static void *context
;
543 static int errval
= HES_ER_UNINIT
;
553 hes_to_bind(const char *name
, const char *type
)
555 static char *bindname
;
557 assert(name
!= NULL
);
558 assert(type
!= NULL
);
560 if (init_context() < 0)
564 bindname
= hesiod_to_bind(context
, name
, type
);
571 hes_resolve(const char *name
, const char *type
)
575 assert(name
!= NULL
);
576 assert(type
!= NULL
);
578 if (init_context() < 0)
582 * In the old Hesiod interface, the caller was responsible for
583 * freeing the returned strings but not the vector of strings itself.
588 list
= hesiod_resolve(context
, name
, type
);
603 hesiod_free_list(context
, hp
);
611 if (hesiod_init(&context
) < 0) {
612 errval
= HES_ER_CONFIG
;
621 translate_errors(void)
625 errval
= HES_ER_NOTFOUND
;
632 /* Not a good match, but the best we can do. */
633 errval
= HES_ER_CONFIG
;