Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / hesiod.c
blob8d41eac00f2bfe7897a1d1141cc25af6fb471975
1 /*
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.
19 /*! \file
20 * \brief
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.
26 * \author
27 * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
30 /* Imports */
32 #include "port_before.h"
34 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
38 #include <errno.h>
39 #include <netdb.h>
40 #include <resolv.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 #include "port_after.h"
47 #include "pathnames.h"
48 #include "hesiod.h"
49 #include "hesiod_p.h"
51 /* Forward */
53 int hesiod_init(void **context);
54 void hesiod_end(void *context);
55 char * hesiod_to_bind(void *context, const char *name,
56 const char *type);
57 char ** hesiod_resolve(void *context, const char *name,
58 const char *type);
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,
63 const char *name);
64 static int init(struct hesiod_p *ctx);
66 /* Public */
68 /*%
69 * This function is called to initialize a hesiod_p.
71 int
72 hesiod_init(void **context) {
73 struct hesiod_p *ctx;
74 char *cp;
76 ctx = malloc(sizeof(struct hesiod_p));
77 if (ctx == 0) {
78 errno = ENOMEM;
79 return (-1);
82 memset(ctx, 0, sizeof (*ctx));
84 if (parse_config_file(ctx, _PATH_HESIOD_CONF) < 0) {
85 #ifdef DEF_RHS
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) {
92 errno = ENOMEM;
93 goto cleanup;
95 strcpy(ctx->LHS, DEF_LHS); /* (checked) */
96 strcpy(ctx->RHS, DEF_RHS); /* (checked) */
97 #else
98 goto cleanup;
99 #endif
102 * The default RHS can be overridden by an environment
103 * variable.
105 if ((cp = getenv("HES_DOMAIN")) != NULL) {
106 size_t RHSlen = strlen(cp) + 2;
107 free(ctx->RHS);
108 ctx->RHS = malloc(RHSlen);
109 if (!ctx->RHS) {
110 errno = ENOMEM;
111 goto cleanup;
113 if (cp[0] == '.') {
114 strcpy(ctx->RHS, cp); /* (checked) */
115 } else {
116 strcpy(ctx->RHS, "."); /* (checked) */
117 strcat(ctx->RHS, cp); /* (checked) */
122 * If there is no default hesiod realm set, we return an
123 * error.
125 if (!ctx->RHS) {
126 errno = ENOEXEC;
127 goto cleanup;
130 #if 0
131 if (res_ninit(ctx->res) < 0)
132 goto cleanup;
133 #endif
135 *context = ctx;
136 return (0);
138 cleanup:
139 hesiod_end(ctx);
140 return (-1);
144 * This function deallocates the hesiod_p
146 void
147 hesiod_end(void *context) {
148 struct hesiod_p *ctx = (struct hesiod_p *) context;
149 int save_errno = errno;
151 if (ctx->res)
152 res_nclose(ctx->res);
153 free(ctx->RHS);
154 free(ctx->LHS);
155 if (ctx->res && ctx->free_res)
156 (*ctx->free_res)(ctx->res);
157 free(ctx);
158 errno = save_errno;
162 * This function takes a hesiod (name, type) and returns a DNS
163 * name which is to be resolved.
165 char *
166 hesiod_to_bind(void *context, const char *name, const char *type) {
167 struct hesiod_p *ctx = (struct hesiod_p *) context;
168 char *bindname;
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, '.'))
175 RHS = cp + 1;
176 else if ((rhs_list = hesiod_resolve(context, cp + 1,
177 "rhs-extension")) != NULL)
178 RHS = *rhs_list;
179 else {
180 errno = ENOENT;
181 return (NULL);
183 } else {
184 RHS = ctx->RHS;
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) {
194 errno = ENOMEM;
195 if (rhs_list)
196 hesiod_free_list(context, rhs_list);
197 return NULL;
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);
205 if (ctx->LHS) {
206 if (ctx->LHS[0] != '.')
207 strcat(bindname, ".");
208 strcat(bindname, ctx->LHS);
210 if (RHS[0] != '.')
211 strcat(bindname, ".");
212 strcat(bindname, RHS);
214 if (rhs_list)
215 hesiod_free_list(context, rhs_list);
217 return (bindname);
221 * This is the core function. Given a hesiod (name, type), it
222 * returns an array of strings returned by the resolver.
224 char **
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);
228 char **retvec;
230 if (bindname == NULL)
231 return (NULL);
232 if (init(ctx) == -1) {
233 free(bindname);
234 return (NULL);
237 if ((retvec = get_txt_records(ctx, C_IN, bindname))) {
238 free(bindname);
239 return (retvec);
242 if (errno != ENOENT)
243 return (NULL);
245 retvec = get_txt_records(ctx, C_HS, bindname);
246 free(bindname);
247 return (retvec);
250 void
251 hesiod_free_list(void *context, char **list) {
252 char **p;
254 UNUSED(context);
256 for (p = list; *p; p++)
257 free(*p);
258 free(list);
262 * This function parses the /etc/hesiod.conf file
264 static int
265 parse_config_file(struct hesiod_p *ctx, const char *filename) {
266 char *key, *data, *cp, **cpp;
267 char buf[MAXDNAME+7];
268 FILE *fp;
271 * Clear the existing configuration variable, just in case
272 * they're set.
274 free(ctx->RHS);
275 free(ctx->LHS);
276 ctx->RHS = ctx->LHS = 0;
279 * Now open and parse the file...
281 if (!(fp = fopen(filename, "r")))
282 return (-1);
284 while (fgets(buf, sizeof(buf), fp) != NULL) {
285 cp = buf;
286 if (*cp == '#' || *cp == '\n' || *cp == '\r')
287 continue;
288 while(*cp == ' ' || *cp == '\t')
289 cp++;
290 key = cp;
291 while(*cp != ' ' && *cp != '\t' && *cp != '=')
292 cp++;
293 *cp++ = '\0';
295 while(*cp == ' ' || *cp == '\t' || *cp == '=')
296 cp++;
297 data = cp;
298 while(*cp != ' ' && *cp != '\n' && *cp != '\r')
299 cp++;
300 *cp++ = '\0';
302 if (strcmp(key, "lhs") == 0)
303 cpp = &ctx->LHS;
304 else if (strcmp(key, "rhs") == 0)
305 cpp = &ctx->RHS;
306 else
307 continue;
309 *cpp = malloc(strlen(data) + 1);
310 if (!*cpp) {
311 errno = ENOMEM;
312 goto cleanup;
314 strcpy(*cpp, data);
316 fclose(fp);
317 return (0);
319 cleanup:
320 fclose(fp);
321 free(ctx->RHS);
322 free(ctx->LHS);
323 ctx->RHS = ctx->LHS = 0;
324 return (-1);
328 * Given a DNS class and a DNS name, do a lookup for TXT records, and
329 * return a list of them.
331 static char **
332 get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
333 struct {
334 int type; /*%< RR type */
335 int class; /*%< RR class */
336 int dlen; /*%< len of data section */
337 u_char *data; /*%< pointer to data */
338 } rr;
339 HEADER *hp;
340 u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
341 u_char *cp, *erdata, *eom;
342 char *dst, *edst, **list;
343 int ancount, qdcount;
344 int i, j, n, skip;
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);
351 if (n < 0) {
352 errno = EMSGSIZE;
353 return (NULL);
355 n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP);
356 if (n < 0) {
357 errno = ECONNREFUSED;
358 return (NULL);
360 if (n < HFIXEDSZ) {
361 errno = EMSGSIZE;
362 return (NULL);
366 * OK, parse the result.
368 hp = (HEADER *) abuf;
369 ancount = ntohs(hp->ancount);
370 qdcount = ntohs(hp->qdcount);
371 cp = abuf + sizeof(HEADER);
372 eom = abuf + n;
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) {
378 errno = EMSGSIZE;
379 return (NULL);
381 cp += skip + QFIXEDSZ;
384 list = malloc((ancount + 1) * sizeof(char *));
385 if (!list) {
386 errno = ENOMEM;
387 return (NULL);
389 j = 0;
390 for (i = 0; i < ancount; i++) {
391 skip = dn_skipname(cp, eom);
392 if (skip < 0) {
393 errno = EMSGSIZE;
394 goto cleanup;
396 cp += skip;
397 if (cp + 3 * INT16SZ + INT32SZ > eom) {
398 errno = EMSGSIZE;
399 goto cleanup;
401 rr.type = ns_get16(cp);
402 cp += INT16SZ;
403 rr.class = ns_get16(cp);
404 cp += INT16SZ + INT32SZ; /*%< skip the ttl, too */
405 rr.dlen = ns_get16(cp);
406 cp += INT16SZ;
407 if (cp + rr.dlen > eom) {
408 errno = EMSGSIZE;
409 goto cleanup;
411 rr.data = cp;
412 cp += rr.dlen;
413 if (rr.class != class || rr.type != T_TXT)
414 continue;
415 if (!(list[j] = malloc(rr.dlen)))
416 goto cleanup;
417 dst = list[j++];
418 edst = dst + rr.dlen;
419 erdata = rr.data + rr.dlen;
420 cp = rr.data;
421 while (cp < erdata) {
422 n = (unsigned char) *cp++;
423 if (cp + n > eom || dst + n > edst) {
424 errno = EMSGSIZE;
425 goto cleanup;
427 memcpy(dst, cp, n);
428 cp += n;
429 dst += n;
431 if (cp != erdata) {
432 errno = EMSGSIZE;
433 goto cleanup;
435 *dst = '\0';
437 list[j] = NULL;
438 if (j == 0) {
439 errno = ENOENT;
440 goto cleanup;
442 return (list);
444 cleanup:
445 for (i = 0; i < j; i++)
446 free(list[i]);
447 free(list);
448 return (NULL);
451 struct __res_state *
452 __hesiod_res_get(void *context) {
453 struct hesiod_p *ctx = context;
455 if (!ctx->res) {
456 struct __res_state *res;
457 res = (struct __res_state *)malloc(sizeof *res);
458 if (res == NULL) {
459 errno = ENOMEM;
460 return (NULL);
462 memset(res, 0, sizeof *res);
463 __hesiod_res_set(ctx, res, free);
466 return (ctx->res);
469 void
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);
479 ctx->res = res;
480 ctx->free_res = free_res;
483 static int
484 init(struct hesiod_p *ctx) {
486 if (!ctx->res && !__hesiod_res_get(ctx))
487 return (-1);
489 if (((ctx->res->options & RES_INIT) == 0U) &&
490 (res_ninit(ctx->res) == -1))
491 return (-1);
493 return (0);