2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 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 ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * hesiod.c --- the core portion of the hesiod resolver.
23 * This file is derived from the hesiod library from Project Athena;
24 * It has been extensively rewritten by Theodore Ts'o to have a more
25 * thread-safe interface.
27 * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
32 #include "port_before.h"
34 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
45 #include "port_after.h"
47 #include "pathnames.h"
53 int hesiod_init(void **context
);
54 void hesiod_end(void *context
);
55 char * hesiod_to_bind(void *context
, const char *name
,
57 char ** hesiod_resolve(void *context
, const char *name
,
59 void hesiod_free_list(void *context
, char **list
);
61 static int parse_config_file(struct hesiod_p
*ctx
, const char *filename
);
62 static char ** get_txt_records(struct hesiod_p
*ctx
, int class,
64 static int init(struct hesiod_p
*ctx
);
69 * This function is called to initialize a hesiod_p.
72 hesiod_init(void **context
) {
76 ctx
= malloc(sizeof(struct hesiod_p
));
82 memset(ctx
, 0, sizeof (*ctx
));
84 if (parse_config_file(ctx
, _PATH_HESIOD_CONF
) < 0) {
87 * Use compiled in defaults.
89 ctx
->LHS
= malloc(strlen(DEF_LHS
) + 1);
90 ctx
->RHS
= malloc(strlen(DEF_RHS
) + 1);
91 if (ctx
->LHS
== NULL
|| ctx
->RHS
== NULL
) {
95 strcpy(ctx
->LHS
, DEF_LHS
); /* (checked) */
96 strcpy(ctx
->RHS
, DEF_RHS
); /* (checked) */
102 * The default RHS can be overridden by an environment
105 if ((cp
= getenv("HES_DOMAIN")) != NULL
) {
106 size_t RHSlen
= strlen(cp
) + 2;
108 ctx
->RHS
= malloc(RHSlen
);
114 strcpy(ctx
->RHS
, cp
); /* (checked) */
116 strcpy(ctx
->RHS
, "."); /* (checked) */
117 strcat(ctx
->RHS
, cp
); /* (checked) */
122 * If there is no default hesiod realm set, we return an
131 if (res_ninit(ctx
->res
) < 0)
144 * This function deallocates the hesiod_p
147 hesiod_end(void *context
) {
148 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
149 int save_errno
= errno
;
152 res_nclose(ctx
->res
);
155 if (ctx
->res
&& ctx
->free_res
)
156 (*ctx
->free_res
)(ctx
->res
);
162 * This function takes a hesiod (name, type) and returns a DNS
163 * name which is to be resolved.
166 hesiod_to_bind(void *context
, const char *name
, const char *type
) {
167 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
169 char **rhs_list
= NULL
;
170 const char *RHS
, *cp
;
172 /* Decide what our RHS is, and set cp to the end of the actual name. */
173 if ((cp
= strchr(name
, '@')) != NULL
) {
174 if (strchr(cp
+ 1, '.'))
176 else if ((rhs_list
= hesiod_resolve(context
, cp
+ 1,
177 "rhs-extension")) != NULL
)
185 cp
= name
+ strlen(name
);
189 * Allocate the space we need, including up to three periods and
190 * the terminating NUL.
192 if ((bindname
= malloc((cp
- name
) + strlen(type
) + strlen(RHS
) +
193 (ctx
->LHS
? strlen(ctx
->LHS
) : 0) + 4)) == NULL
) {
196 hesiod_free_list(context
, rhs_list
);
200 /* Now put together the DNS name. */
201 memcpy(bindname
, name
, cp
- name
);
202 bindname
[cp
- name
] = '\0';
203 strcat(bindname
, ".");
204 strcat(bindname
, type
);
206 if (ctx
->LHS
[0] != '.')
207 strcat(bindname
, ".");
208 strcat(bindname
, ctx
->LHS
);
211 strcat(bindname
, ".");
212 strcat(bindname
, RHS
);
215 hesiod_free_list(context
, rhs_list
);
221 * This is the core function. Given a hesiod (name, type), it
222 * returns an array of strings returned by the resolver.
225 hesiod_resolve(void *context
, const char *name
, const char *type
) {
226 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
227 char *bindname
= hesiod_to_bind(context
, name
, type
);
230 if (bindname
== NULL
)
232 if (init(ctx
) == -1) {
237 if ((retvec
= get_txt_records(ctx
, C_IN
, bindname
))) {
245 retvec
= get_txt_records(ctx
, C_HS
, bindname
);
251 hesiod_free_list(void *context
, char **list
) {
256 for (p
= list
; *p
; p
++)
262 * This function parses the /etc/hesiod.conf file
265 parse_config_file(struct hesiod_p
*ctx
, const char *filename
) {
266 char *key
, *data
, *cp
, **cpp
;
267 char buf
[MAXDNAME
+7];
271 * Clear the existing configuration variable, just in case
276 ctx
->RHS
= ctx
->LHS
= 0;
279 * Now open and parse the file...
281 if (!(fp
= fopen(filename
, "r")))
284 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
286 if (*cp
== '#' || *cp
== '\n' || *cp
== '\r')
288 while(*cp
== ' ' || *cp
== '\t')
291 while(*cp
!= ' ' && *cp
!= '\t' && *cp
!= '=')
295 while(*cp
== ' ' || *cp
== '\t' || *cp
== '=')
298 while(*cp
!= ' ' && *cp
!= '\n' && *cp
!= '\r')
302 if (strcmp(key
, "lhs") == 0)
304 else if (strcmp(key
, "rhs") == 0)
309 *cpp
= malloc(strlen(data
) + 1);
323 ctx
->RHS
= ctx
->LHS
= 0;
328 * Given a DNS class and a DNS name, do a lookup for TXT records, and
329 * return a list of them.
332 get_txt_records(struct hesiod_p
*ctx
, int class, const char *name
) {
334 int type
; /*%< RR type */
335 int class; /*%< RR class */
336 int dlen
; /*%< len of data section */
337 u_char
*data
; /*%< pointer to data */
340 u_char qbuf
[MAX_HESRESP
], abuf
[MAX_HESRESP
];
341 u_char
*cp
, *erdata
, *eom
;
342 char *dst
, *edst
, **list
;
343 int ancount
, qdcount
;
347 * Construct the query and send it.
349 n
= res_nmkquery(ctx
->res
, QUERY
, name
, class, T_TXT
, NULL
, 0,
350 NULL
, qbuf
, MAX_HESRESP
);
355 n
= res_nsend(ctx
->res
, qbuf
, n
, abuf
, MAX_HESRESP
);
357 errno
= ECONNREFUSED
;
366 * OK, parse the result.
368 hp
= (HEADER
*) abuf
;
369 ancount
= ntohs(hp
->ancount
);
370 qdcount
= ntohs(hp
->qdcount
);
371 cp
= abuf
+ sizeof(HEADER
);
374 /* Skip query, trying to get to the answer section which follows. */
375 for (i
= 0; i
< qdcount
; i
++) {
376 skip
= dn_skipname(cp
, eom
);
377 if (skip
< 0 || cp
+ skip
+ QFIXEDSZ
> eom
) {
381 cp
+= skip
+ QFIXEDSZ
;
384 list
= malloc((ancount
+ 1) * sizeof(char *));
390 for (i
= 0; i
< ancount
; i
++) {
391 skip
= dn_skipname(cp
, eom
);
397 if (cp
+ 3 * INT16SZ
+ INT32SZ
> eom
) {
401 rr
.type
= ns_get16(cp
);
403 rr
.class = ns_get16(cp
);
404 cp
+= INT16SZ
+ INT32SZ
; /*%< skip the ttl, too */
405 rr
.dlen
= ns_get16(cp
);
407 if (cp
+ rr
.dlen
> eom
) {
413 if (rr
.class != class || rr
.type
!= T_TXT
)
415 if (!(list
[j
] = malloc(rr
.dlen
)))
418 edst
= dst
+ rr
.dlen
;
419 erdata
= rr
.data
+ rr
.dlen
;
421 while (cp
< erdata
) {
422 n
= (unsigned char) *cp
++;
423 if (cp
+ n
> eom
|| dst
+ n
> edst
) {
445 for (i
= 0; i
< j
; i
++)
452 __hesiod_res_get(void *context
) {
453 struct hesiod_p
*ctx
= context
;
456 struct __res_state
*res
;
457 res
= (struct __res_state
*)malloc(sizeof *res
);
462 memset(res
, 0, sizeof *res
);
463 __hesiod_res_set(ctx
, res
, free
);
470 __hesiod_res_set(void *context
, struct __res_state
*res
,
471 void (*free_res
)(void *)) {
472 struct hesiod_p
*ctx
= context
;
474 if (ctx
->res
&& ctx
->free_res
) {
475 res_nclose(ctx
->res
);
476 (*ctx
->free_res
)(ctx
->res
);
480 ctx
->free_res
= free_res
;
484 init(struct hesiod_p
*ctx
) {
486 if (!ctx
->res
&& !__hesiod_res_get(ctx
))
489 if (((ctx
->res
->options
& RES_INIT
) == 0U) &&
490 (res_ninit(ctx
->res
) == -1))