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;
109 ctx
->RHS
= malloc(RHSlen
);
115 strcpy(ctx
->RHS
, cp
); /* (checked) */
117 strcpy(ctx
->RHS
, "."); /* (checked) */
118 strcat(ctx
->RHS
, cp
); /* (checked) */
123 * If there is no default hesiod realm set, we return an
132 if (res_ninit(ctx
->res
) < 0)
145 * This function deallocates the hesiod_p
148 hesiod_end(void *context
) {
149 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
150 int save_errno
= errno
;
153 res_nclose(ctx
->res
);
158 if (ctx
->res
&& ctx
->free_res
)
159 (*ctx
->free_res
)(ctx
->res
);
165 * This function takes a hesiod (name, type) and returns a DNS
166 * name which is to be resolved.
169 hesiod_to_bind(void *context
, const char *name
, const char *type
) {
170 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
172 char **rhs_list
= NULL
;
173 const char *RHS
, *cp
;
175 /* Decide what our RHS is, and set cp to the end of the actual name. */
176 if ((cp
= strchr(name
, '@')) != NULL
) {
177 if (strchr(cp
+ 1, '.'))
179 else if ((rhs_list
= hesiod_resolve(context
, cp
+ 1,
180 "rhs-extension")) != NULL
)
188 cp
= name
+ strlen(name
);
192 * Allocate the space we need, including up to three periods and
193 * the terminating NUL.
195 if ((bindname
= malloc((cp
- name
) + strlen(type
) + strlen(RHS
) +
196 (ctx
->LHS
? strlen(ctx
->LHS
) : 0) + 4)) == NULL
) {
199 hesiod_free_list(context
, rhs_list
);
203 /* Now put together the DNS name. */
204 memcpy(bindname
, name
, cp
- name
);
205 bindname
[cp
- name
] = '\0';
206 strcat(bindname
, ".");
207 strcat(bindname
, type
);
209 if (ctx
->LHS
[0] != '.')
210 strcat(bindname
, ".");
211 strcat(bindname
, ctx
->LHS
);
214 strcat(bindname
, ".");
215 strcat(bindname
, RHS
);
218 hesiod_free_list(context
, rhs_list
);
224 * This is the core function. Given a hesiod (name, type), it
225 * returns an array of strings returned by the resolver.
228 hesiod_resolve(void *context
, const char *name
, const char *type
) {
229 struct hesiod_p
*ctx
= (struct hesiod_p
*) context
;
230 char *bindname
= hesiod_to_bind(context
, name
, type
);
233 if (bindname
== NULL
)
235 if (init(ctx
) == -1) {
240 if ((retvec
= get_txt_records(ctx
, C_IN
, bindname
))) {
248 retvec
= get_txt_records(ctx
, C_HS
, bindname
);
254 hesiod_free_list(void *context
, char **list
) {
259 for (p
= list
; *p
; p
++)
265 * This function parses the /etc/hesiod.conf file
268 parse_config_file(struct hesiod_p
*ctx
, const char *filename
) {
269 char *key
, *data
, *cp
, **cpp
;
270 char buf
[MAXDNAME
+7];
274 * Clear the existing configuration variable, just in case
281 ctx
->RHS
= ctx
->LHS
= 0;
284 * Now open and parse the file...
286 if (!(fp
= fopen(filename
, "r")))
289 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
291 if (*cp
== '#' || *cp
== '\n' || *cp
== '\r')
293 while(*cp
== ' ' || *cp
== '\t')
296 while(*cp
!= ' ' && *cp
!= '\t' && *cp
!= '=')
300 while(*cp
== ' ' || *cp
== '\t' || *cp
== '=')
303 while(*cp
!= ' ' && *cp
!= '\n' && *cp
!= '\r')
307 if (strcmp(key
, "lhs") == 0)
309 else if (strcmp(key
, "rhs") == 0)
314 *cpp
= malloc(strlen(data
) + 1);
330 ctx
->RHS
= ctx
->LHS
= 0;
335 * Given a DNS class and a DNS name, do a lookup for TXT records, and
336 * return a list of them.
339 get_txt_records(struct hesiod_p
*ctx
, int class, const char *name
) {
341 int type
; /*%< RR type */
342 int class; /*%< RR class */
343 int dlen
; /*%< len of data section */
344 u_char
*data
; /*%< pointer to data */
347 u_char qbuf
[MAX_HESRESP
], abuf
[MAX_HESRESP
];
348 u_char
*cp
, *erdata
, *eom
;
349 char *dst
, *edst
, **list
;
350 int ancount
, qdcount
;
354 * Construct the query and send it.
356 n
= res_nmkquery(ctx
->res
, QUERY
, name
, class, T_TXT
, NULL
, 0,
357 NULL
, qbuf
, MAX_HESRESP
);
362 n
= res_nsend(ctx
->res
, qbuf
, n
, abuf
, MAX_HESRESP
);
364 errno
= ECONNREFUSED
;
373 * OK, parse the result.
375 hp
= (HEADER
*) abuf
;
376 ancount
= ntohs(hp
->ancount
);
377 qdcount
= ntohs(hp
->qdcount
);
378 cp
= abuf
+ sizeof(HEADER
);
381 /* Skip query, trying to get to the answer section which follows. */
382 for (i
= 0; i
< qdcount
; i
++) {
383 skip
= dn_skipname(cp
, eom
);
384 if (skip
< 0 || cp
+ skip
+ QFIXEDSZ
> eom
) {
388 cp
+= skip
+ QFIXEDSZ
;
391 list
= malloc((ancount
+ 1) * sizeof(char *));
397 for (i
= 0; i
< ancount
; i
++) {
398 skip
= dn_skipname(cp
, eom
);
404 if (cp
+ 3 * INT16SZ
+ INT32SZ
> eom
) {
408 rr
.type
= ns_get16(cp
);
410 rr
.class = ns_get16(cp
);
411 cp
+= INT16SZ
+ INT32SZ
; /*%< skip the ttl, too */
412 rr
.dlen
= ns_get16(cp
);
414 if (cp
+ rr
.dlen
> eom
) {
420 if (rr
.class != class || rr
.type
!= T_TXT
)
422 if (!(list
[j
] = malloc(rr
.dlen
)))
425 edst
= dst
+ rr
.dlen
;
426 erdata
= rr
.data
+ rr
.dlen
;
428 while (cp
< erdata
) {
429 n
= (unsigned char) *cp
++;
430 if (cp
+ n
> eom
|| dst
+ n
> edst
) {
452 for (i
= 0; i
< j
; i
++)
459 __hesiod_res_get(void *context
) {
460 struct hesiod_p
*ctx
= context
;
463 struct __res_state
*res
;
464 res
= (struct __res_state
*)malloc(sizeof *res
);
469 memset(res
, 0, sizeof *res
);
470 __hesiod_res_set(ctx
, res
, free
);
477 __hesiod_res_set(void *context
, struct __res_state
*res
,
478 void (*free_res
)(void *)) {
479 struct hesiod_p
*ctx
= context
;
481 if (ctx
->res
&& ctx
->free_res
) {
482 res_nclose(ctx
->res
);
483 (*ctx
->free_res
)(ctx
->res
);
487 ctx
->free_res
= free_res
;
491 init(struct hesiod_p
*ctx
) {
493 if (!ctx
->res
&& !__hesiod_res_get(ctx
))
496 if (((ctx
->res
->options
& RES_INIT
) == 0U) &&
497 (res_ninit(ctx
->res
) == -1))