dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libresolv2 / common / irs / hesiod.c
blob0d433c88f60399f616a8d58ab0851ecdc7f427d0
1 #if defined(LIBC_SCCS) && !defined(lint)
2 static const char rcsid[] = "$Id: hesiod.c,v 1.7 2005/07/28 06:51:48 marka Exp $";
3 #endif
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996,1999 by Internet Software Consortium.
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 /*! \file
24 * \brief
25 * hesiod.c --- the core portion of the hesiod resolver.
27 * This file is derived from the hesiod library from Project Athena;
28 * It has been extensively rewritten by Theodore Ts'o to have a more
29 * thread-safe interface.
30 * \author
31 * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
34 /* Imports */
36 #include "port_before.h"
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <arpa/nameser.h>
42 #include <errno.h>
43 #include <netdb.h>
44 #include <resolv.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
49 #include "port_after.h"
51 #include "pathnames.h"
52 #include "hesiod.h"
53 #include "hesiod_p.h"
55 /* Forward */
57 int hesiod_init(void **context);
58 void hesiod_end(void *context);
59 char * hesiod_to_bind(void *context, const char *name,
60 const char *type);
61 char ** hesiod_resolve(void *context, const char *name,
62 const char *type);
63 void hesiod_free_list(void *context, char **list);
65 static int parse_config_file(struct hesiod_p *ctx, const char *filename);
66 static char ** get_txt_records(struct hesiod_p *ctx, int class,
67 const char *name);
68 static int init(struct hesiod_p *ctx);
70 /* Public */
72 /*%
73 * This function is called to initialize a hesiod_p.
75 int
76 hesiod_init(void **context) {
77 struct hesiod_p *ctx;
78 char *cp;
80 ctx = malloc(sizeof(struct hesiod_p));
81 if (ctx == 0) {
82 errno = ENOMEM;
83 return (-1);
86 memset(ctx, 0, sizeof (*ctx));
88 if (parse_config_file(ctx, _PATH_HESIOD_CONF) < 0) {
89 #ifdef DEF_RHS
91 * Use compiled in defaults.
93 ctx->LHS = malloc(strlen(DEF_LHS) + 1);
94 ctx->RHS = malloc(strlen(DEF_RHS) + 1);
95 if (ctx->LHS == NULL || ctx->RHS == NULL) {
96 errno = ENOMEM;
97 goto cleanup;
99 strcpy(ctx->LHS, DEF_LHS); /* (checked) */
100 strcpy(ctx->RHS, DEF_RHS); /* (checked) */
101 #else
102 goto cleanup;
103 #endif
106 * The default RHS can be overridden by an environment
107 * variable.
109 if ((cp = getenv("HES_DOMAIN")) != NULL) {
110 size_t RHSlen = strlen(cp) + 2;
111 free(ctx->RHS);
112 ctx->RHS = malloc(RHSlen);
113 if (!ctx->RHS) {
114 errno = ENOMEM;
115 goto cleanup;
117 if (cp[0] == '.') {
118 strcpy(ctx->RHS, cp); /* (checked) */
119 } else {
120 strcpy(ctx->RHS, "."); /* (checked) */
121 strcat(ctx->RHS, cp); /* (checked) */
126 * If there is no default hesiod realm set, we return an
127 * error.
129 if (!ctx->RHS) {
130 errno = ENOEXEC;
131 goto cleanup;
134 #if 0
135 if (res_ninit(ctx->res) < 0)
136 goto cleanup;
137 #endif
139 *context = ctx;
140 return (0);
142 cleanup:
143 hesiod_end(ctx);
144 return (-1);
148 * This function deallocates the hesiod_p
150 void
151 hesiod_end(void *context) {
152 struct hesiod_p *ctx = (struct hesiod_p *) context;
153 int save_errno = errno;
155 if (ctx->res)
156 res_nclose(ctx->res);
157 free(ctx->RHS);
158 free(ctx->LHS);
159 if (ctx->res && ctx->free_res)
160 (*ctx->free_res)(ctx->res);
161 free(ctx);
162 errno = save_errno;
166 * This function takes a hesiod (name, type) and returns a DNS
167 * name which is to be resolved.
169 char *
170 hesiod_to_bind(void *context, const char *name, const char *type) {
171 struct hesiod_p *ctx = (struct hesiod_p *) context;
172 char *bindname;
173 char **rhs_list = NULL;
174 const char *RHS, *cp;
176 /* Decide what our RHS is, and set cp to the end of the actual name. */
177 if ((cp = strchr(name, '@')) != NULL) {
178 if (strchr(cp + 1, '.'))
179 RHS = cp + 1;
180 else if ((rhs_list = hesiod_resolve(context, cp + 1,
181 "rhs-extension")) != NULL)
182 RHS = *rhs_list;
183 else {
184 errno = ENOENT;
185 return (NULL);
187 } else {
188 RHS = ctx->RHS;
189 cp = name + strlen(name);
193 * Allocate the space we need, including up to three periods and
194 * the terminating NUL.
196 if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
197 (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
198 errno = ENOMEM;
199 if (rhs_list)
200 hesiod_free_list(context, rhs_list);
201 return NULL;
204 /* Now put together the DNS name. */
205 memcpy(bindname, name, cp - name);
206 bindname[cp - name] = '\0';
207 strcat(bindname, ".");
208 strcat(bindname, type);
209 if (ctx->LHS) {
210 if (ctx->LHS[0] != '.')
211 strcat(bindname, ".");
212 strcat(bindname, ctx->LHS);
214 if (RHS[0] != '.')
215 strcat(bindname, ".");
216 strcat(bindname, RHS);
218 if (rhs_list)
219 hesiod_free_list(context, rhs_list);
221 return (bindname);
225 * This is the core function. Given a hesiod (name, type), it
226 * returns an array of strings returned by the resolver.
228 char **
229 hesiod_resolve(void *context, const char *name, const char *type) {
230 struct hesiod_p *ctx = (struct hesiod_p *) context;
231 char *bindname = hesiod_to_bind(context, name, type);
232 char **retvec;
234 if (bindname == NULL)
235 return (NULL);
236 if (init(ctx) == -1) {
237 free(bindname);
238 return (NULL);
241 if ((retvec = get_txt_records(ctx, C_IN, bindname))) {
242 free(bindname);
243 return (retvec);
246 if (errno != ENOENT)
247 return (NULL);
249 retvec = get_txt_records(ctx, C_HS, bindname);
250 free(bindname);
251 return (retvec);
254 void
255 hesiod_free_list(void *context, char **list) {
256 char **p;
258 UNUSED(context);
260 for (p = list; *p; p++)
261 free(*p);
262 free(list);
266 * This function parses the /etc/hesiod.conf file
268 static int
269 parse_config_file(struct hesiod_p *ctx, const char *filename) {
270 char *key, *data, *cp, **cpp;
271 char buf[MAXDNAME+7];
272 FILE *fp;
275 * Clear the existing configuration variable, just in case
276 * they're set.
278 free(ctx->RHS);
279 free(ctx->LHS);
280 ctx->RHS = ctx->LHS = 0;
283 * Now open and parse the file...
285 if (!(fp = fopen(filename, "r")))
286 return (-1);
288 while (fgets(buf, sizeof(buf), fp) != NULL) {
289 cp = buf;
290 if (*cp == '#' || *cp == '\n' || *cp == '\r')
291 continue;
292 while(*cp == ' ' || *cp == '\t')
293 cp++;
294 key = cp;
295 while(*cp != ' ' && *cp != '\t' && *cp != '=')
296 cp++;
297 *cp++ = '\0';
299 while(*cp == ' ' || *cp == '\t' || *cp == '=')
300 cp++;
301 data = cp;
302 while(*cp != ' ' && *cp != '\n' && *cp != '\r')
303 cp++;
304 *cp++ = '\0';
306 if (strcmp(key, "lhs") == 0)
307 cpp = &ctx->LHS;
308 else if (strcmp(key, "rhs") == 0)
309 cpp = &ctx->RHS;
310 else
311 continue;
313 *cpp = malloc(strlen(data) + 1);
314 if (!*cpp) {
315 errno = ENOMEM;
316 goto cleanup;
318 strcpy(*cpp, data);
320 fclose(fp);
321 return (0);
323 cleanup:
324 fclose(fp);
325 free(ctx->RHS);
326 free(ctx->LHS);
327 ctx->RHS = ctx->LHS = 0;
328 return (-1);
332 * Given a DNS class and a DNS name, do a lookup for TXT records, and
333 * return a list of them.
335 static char **
336 get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
337 struct {
338 int type; /*%< RR type */
339 int class; /*%< RR class */
340 int dlen; /*%< len of data section */
341 u_char *data; /*%< pointer to data */
342 } rr;
343 HEADER *hp;
344 u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
345 u_char *cp, *erdata, *eom;
346 char *dst, *edst, **list;
347 int ancount, qdcount;
348 int i, j, n, skip;
351 * Construct the query and send it.
353 n = res_nmkquery(ctx->res, QUERY, name, class, T_TXT, NULL, 0,
354 NULL, qbuf, MAX_HESRESP);
355 if (n < 0) {
356 errno = EMSGSIZE;
357 return (NULL);
359 n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP);
360 if (n < 0) {
361 errno = ECONNREFUSED;
362 return (NULL);
364 if (n < HFIXEDSZ) {
365 errno = EMSGSIZE;
366 return (NULL);
370 * OK, parse the result.
372 hp = (HEADER *) abuf;
373 ancount = ntohs(hp->ancount);
374 qdcount = ntohs(hp->qdcount);
375 cp = abuf + sizeof(HEADER);
376 eom = abuf + n;
378 /* Skip query, trying to get to the answer section which follows. */
379 for (i = 0; i < qdcount; i++) {
380 skip = dn_skipname(cp, eom);
381 if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
382 errno = EMSGSIZE;
383 return (NULL);
385 cp += skip + QFIXEDSZ;
388 list = malloc((ancount + 1) * sizeof(char *));
389 if (!list) {
390 errno = ENOMEM;
391 return (NULL);
393 j = 0;
394 for (i = 0; i < ancount; i++) {
395 skip = dn_skipname(cp, eom);
396 if (skip < 0) {
397 errno = EMSGSIZE;
398 goto cleanup;
400 cp += skip;
401 if (cp + 3 * INT16SZ + INT32SZ > eom) {
402 errno = EMSGSIZE;
403 goto cleanup;
405 rr.type = ns_get16(cp);
406 cp += INT16SZ;
407 rr.class = ns_get16(cp);
408 cp += INT16SZ + INT32SZ; /*%< skip the ttl, too */
409 rr.dlen = ns_get16(cp);
410 cp += INT16SZ;
411 if (cp + rr.dlen > eom) {
412 errno = EMSGSIZE;
413 goto cleanup;
415 rr.data = cp;
416 cp += rr.dlen;
417 if (rr.class != class || rr.type != T_TXT)
418 continue;
419 if (!(list[j] = malloc(rr.dlen)))
420 goto cleanup;
421 dst = list[j++];
422 edst = dst + rr.dlen;
423 erdata = rr.data + rr.dlen;
424 cp = rr.data;
425 while (cp < erdata) {
426 n = (unsigned char) *cp++;
427 if (cp + n > eom || dst + n > edst) {
428 errno = EMSGSIZE;
429 goto cleanup;
431 memcpy(dst, cp, n);
432 cp += n;
433 dst += n;
435 if (cp != erdata) {
436 errno = EMSGSIZE;
437 goto cleanup;
439 *dst = '\0';
441 list[j] = NULL;
442 if (j == 0) {
443 errno = ENOENT;
444 goto cleanup;
446 return (list);
448 cleanup:
449 for (i = 0; i < j; i++)
450 free(list[i]);
451 free(list);
452 return (NULL);
455 struct __res_state *
456 __hesiod_res_get(void *context) {
457 struct hesiod_p *ctx = context;
459 if (!ctx->res) {
460 struct __res_state *res;
461 res = (struct __res_state *)malloc(sizeof *res);
462 if (res == NULL) {
463 errno = ENOMEM;
464 return (NULL);
466 memset(res, 0, sizeof *res);
467 __hesiod_res_set(ctx, res, free);
470 return (ctx->res);
473 void
474 __hesiod_res_set(void *context, struct __res_state *res,
475 void (*free_res)(void *)) {
476 struct hesiod_p *ctx = context;
478 if (ctx->res && ctx->free_res) {
479 res_nclose(ctx->res);
480 (*ctx->free_res)(ctx->res);
483 ctx->res = res;
484 ctx->free_res = free_res;
487 static int
488 init(struct hesiod_p *ctx) {
490 if (!ctx->res && !__hesiod_res_get(ctx))
491 return (-1);
493 if (((ctx->res->options & RES_INIT) == 0U) &&
494 (res_ninit(ctx->res) == -1))
495 return (-1);
497 return (0);