(__bswap_32): Add cast to avoid invalid asm.
[glibc/pb-stable.git] / hesiod / hesiod.c
blob3a01fd076bd48042ffa62133ba2706758d640702
1 #if defined(LIBC_SCCS) && !defined(lint)
2 static const char rcsid[] = "$BINDId: hesiod.c,v 1.21 2000/02/28 14:51:08 vixie Exp $";
3 #endif
5 /*
6 * Copyright (c) 1996,1999 by Internet Software Consortium.
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 * SOFTWARE.
23 * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
27 * hesiod.c --- the core portion of the hesiod resolver.
29 * This file is derived from the hesiod library from Project Athena;
30 * It has been extensively rewritten by Theodore Ts'o to have a more
31 * thread-safe interface.
34 /* Imports */
36 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <arpa/nameser.h>
40 #include <errno.h>
41 #include <netdb.h>
42 #include <resolv.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include "hesiod.h"
48 #include "hesiod_p.h"
49 #undef DEF_RHS
51 #define _PATH_HESIOD_CONF "/etc/hesiod.conf"
53 /* Forward */
55 int hesiod_init(void **context);
56 void hesiod_end(void *context);
57 char * hesiod_to_bind(void *context, const char *name,
58 const char *type);
59 char ** hesiod_resolve(void *context, const char *name,
60 const char *type);
61 void hesiod_free_list(void *context, char **list);
63 static int parse_config_file(struct hesiod_p *ctx, const char *filename);
64 static char ** get_txt_records(struct hesiod_p *ctx, int class,
65 const char *name);
66 static int init(struct hesiod_p *ctx);
68 /* Public */
71 * This function is called to initialize a hesiod_p.
73 int
74 hesiod_init(void **context) {
75 struct hesiod_p *ctx;
76 const char *configname;
77 char *cp;
79 ctx = malloc(sizeof(struct hesiod_p));
80 if (ctx == 0) {
81 __set_errno(ENOMEM);
82 return (-1);
85 ctx->LHS = NULL;
86 ctx->RHS = NULL;
87 ctx->res = NULL;
89 configname = __secure_getenv("HESIOD_CONFIG");
90 if (!configname)
91 configname = _PATH_HESIOD_CONF;
92 if (parse_config_file(ctx, configname) < 0) {
93 #ifdef DEF_RHS
95 * Use compiled in defaults.
97 ctx->LHS = malloc(strlen(DEF_LHS)+1);
98 ctx->RHS = malloc(strlen(DEF_RHS)+1);
99 if (ctx->LHS == 0 || ctx->RHS == 0) {
100 __set_errno(ENOMEM);
101 goto cleanup;
103 strcpy(ctx->LHS, DEF_LHS);
104 strcpy(ctx->RHS, DEF_RHS);
105 #else
106 goto cleanup;
107 #endif
110 * The default RHS can be overridden by an environment
111 * variable.
113 if ((cp = __secure_getenv("HES_DOMAIN")) != NULL) {
114 if (ctx->RHS)
115 free(ctx->RHS);
116 ctx->RHS = malloc(strlen(cp)+2);
117 if (!ctx->RHS) {
118 __set_errno(ENOMEM);
119 goto cleanup;
121 if (cp[0] == '.')
122 strcpy(ctx->RHS, cp);
123 else {
124 strcpy(ctx->RHS, ".");
125 strcat(ctx->RHS, cp);
130 * If there is no default hesiod realm set, we return an
131 * error.
133 if (!ctx->RHS) {
134 __set_errno(ENOEXEC);
135 goto cleanup;
138 #if 0
139 if (res_ninit(ctx->res) < 0)
140 goto cleanup;
141 #endif
143 *context = ctx;
144 return (0);
146 cleanup:
147 hesiod_end(ctx);
148 return (-1);
152 * This function deallocates the hesiod_p
154 void
155 hesiod_end(void *context) {
156 struct hesiod_p *ctx = (struct hesiod_p *) context;
157 int save_errno = errno;
159 if (ctx->res)
160 res_nclose(ctx->res);
161 if (ctx->RHS)
162 free(ctx->RHS);
163 if (ctx->LHS)
164 free(ctx->LHS);
165 if (ctx->res && ctx->free_res)
166 (*ctx->free_res)(ctx->res);
167 free(ctx);
168 __set_errno(save_errno);
172 * This function takes a hesiod (name, type) and returns a DNS
173 * name which is to be resolved.
175 char *
176 hesiod_to_bind(void *context, const char *name, const char *type) {
177 struct hesiod_p *ctx = (struct hesiod_p *) context;
178 char *bindname;
179 char **rhs_list = NULL;
180 const char *RHS, *cp;
182 /* Decide what our RHS is, and set cp to the end of the actual name. */
183 if ((cp = strchr(name, '@')) != NULL) {
184 if (strchr(cp + 1, '.'))
185 RHS = cp + 1;
186 else if ((rhs_list = hesiod_resolve(context, cp + 1,
187 "rhs-extension")) != NULL)
188 RHS = *rhs_list;
189 else {
190 __set_errno(ENOENT);
191 return (NULL);
193 } else {
194 RHS = ctx->RHS;
195 cp = name + strlen(name);
199 * Allocate the space we need, including up to three periods and
200 * the terminating NUL.
202 if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
203 (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
204 __set_errno(ENOMEM);
205 if (rhs_list)
206 hesiod_free_list(context, rhs_list);
207 return NULL;
210 /* Now put together the DNS name. */
211 memcpy(bindname, name, cp - name);
212 bindname[cp - name] = '\0';
213 strcat(bindname, ".");
214 strcat(bindname, type);
215 if (ctx->LHS) {
216 if (ctx->LHS[0] != '.')
217 strcat(bindname, ".");
218 strcat(bindname, ctx->LHS);
220 if (RHS[0] != '.')
221 strcat(bindname, ".");
222 strcat(bindname, RHS);
224 if (rhs_list)
225 hesiod_free_list(context, rhs_list);
227 return (bindname);
231 * This is the core function. Given a hesiod (name, type), it
232 * returns an array of strings returned by the resolver.
234 char **
235 hesiod_resolve(void *context, const char *name, const char *type) {
236 struct hesiod_p *ctx = (struct hesiod_p *) context;
237 char *bindname = hesiod_to_bind(context, name, type);
238 char **retvec;
240 if (bindname == NULL)
241 return (NULL);
242 if (init(ctx) == -1) {
243 free(bindname);
244 return (NULL);
247 if ((retvec = get_txt_records(ctx, C_IN, bindname))) {
248 free(bindname);
249 return (retvec);
252 if (errno != ENOENT)
253 return (NULL);
255 retvec = get_txt_records(ctx, C_HS, bindname);
256 free(bindname);
257 return (retvec);
260 void
261 hesiod_free_list(void *context, char **list) {
262 char **p;
264 for (p = list; *p; p++)
265 free(*p);
266 free(list);
270 * This function parses the /etc/hesiod.conf file
272 static int
273 parse_config_file(struct hesiod_p *ctx, const char *filename) {
274 char *key, *data, *cp, **cpp;
275 char buf[MAXDNAME+7];
276 FILE *fp;
279 * Clear the existing configuration variable, just in case
280 * they're set.
282 if (ctx->RHS)
283 free(ctx->RHS);
284 if (ctx->LHS)
285 free(ctx->LHS);
286 ctx->RHS = ctx->LHS = 0;
289 * Now open and parse the file...
291 if (!(fp = fopen(filename, "r")))
292 return (-1);
294 while (fgets(buf, sizeof(buf), fp) != NULL) {
295 cp = buf;
296 if (*cp == '#' || *cp == '\n' || *cp == '\r')
297 continue;
298 while(*cp == ' ' || *cp == '\t')
299 cp++;
300 key = cp;
301 while(*cp != ' ' && *cp != '\t' && *cp != '=')
302 cp++;
303 *cp++ = '\0';
305 while(*cp == ' ' || *cp == '\t' || *cp == '=')
306 cp++;
307 data = cp;
308 while(*cp != ' ' && *cp != '\n' && *cp != '\r')
309 cp++;
310 *cp++ = '\0';
312 if (strcmp(key, "lhs") == 0)
313 cpp = &ctx->LHS;
314 else if (strcmp(key, "rhs") == 0)
315 cpp = &ctx->RHS;
316 else
317 continue;
319 *cpp = malloc(strlen(data) + 1);
320 if (!*cpp) {
321 __set_errno(ENOMEM);
322 goto cleanup;
324 strcpy(*cpp, data);
326 fclose(fp);
327 return (0);
329 cleanup:
330 fclose(fp);
331 if (ctx->RHS)
332 free(ctx->RHS);
333 if (ctx->LHS)
334 free(ctx->LHS);
335 ctx->RHS = ctx->LHS = 0;
336 return (-1);
340 * Given a DNS class and a DNS name, do a lookup for TXT records, and
341 * return a list of them.
343 static char **
344 get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
345 struct {
346 int type; /* RR type */
347 int class; /* RR class */
348 int dlen; /* len of data section */
349 u_char *data; /* pointer to data */
350 } rr;
351 HEADER *hp;
352 u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
353 u_char *cp, *erdata, *eom;
354 char *dst, *edst, **list;
355 int ancount, qdcount;
356 int i, j, n, skip;
359 * Construct the query and send it.
361 n = res_nmkquery(ctx->res, QUERY, name, class, T_TXT, NULL, 0,
362 NULL, qbuf, MAX_HESRESP);
363 if (n < 0) {
364 __set_errno(EMSGSIZE);
365 return (NULL);
367 n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP);
368 if (n < 0) {
369 __set_errno(ECONNREFUSED);
370 return (NULL);
372 if (n < HFIXEDSZ) {
373 __set_errno(EMSGSIZE);
374 return (NULL);
378 * OK, parse the result.
380 hp = (HEADER *) abuf;
381 ancount = ntohs(hp->ancount);
382 qdcount = ntohs(hp->qdcount);
383 cp = abuf + sizeof(HEADER);
384 eom = abuf + n;
386 /* Skip query, trying to get to the answer section which follows. */
387 for (i = 0; i < qdcount; i++) {
388 skip = dn_skipname(cp, eom);
389 if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
390 __set_errno(EMSGSIZE);
391 return (NULL);
393 cp += skip + QFIXEDSZ;
396 list = malloc((ancount + 1) * sizeof(char *));
397 if (!list) {
398 __set_errno(ENOMEM);
399 return (NULL);
401 j = 0;
402 for (i = 0; i < ancount; i++) {
403 skip = dn_skipname(cp, eom);
404 if (skip < 0) {
405 __set_errno(EMSGSIZE);
406 goto cleanup;
408 cp += skip;
409 if (cp + 3 * INT16SZ + INT32SZ > eom) {
410 __set_errno(EMSGSIZE);
411 goto cleanup;
413 rr.type = ns_get16(cp);
414 cp += INT16SZ;
415 rr.class = ns_get16(cp);
416 cp += INT16SZ + INT32SZ; /* skip the ttl, too */
417 rr.dlen = ns_get16(cp);
418 cp += INT16SZ;
419 if (cp + rr.dlen > eom) {
420 __set_errno(EMSGSIZE);
421 goto cleanup;
423 rr.data = cp;
424 cp += rr.dlen;
425 if (rr.class != class || rr.type != T_TXT)
426 continue;
427 if (!(list[j] = malloc(rr.dlen)))
428 goto cleanup;
429 dst = list[j++];
430 edst = dst + rr.dlen;
431 erdata = rr.data + rr.dlen;
432 cp = rr.data;
433 while (cp < erdata) {
434 n = (unsigned char) *cp++;
435 if (cp + n > eom || dst + n > edst) {
436 __set_errno(EMSGSIZE);
437 goto cleanup;
439 memcpy(dst, cp, n);
440 cp += n;
441 dst += n;
443 if (cp != erdata) {
444 __set_errno(EMSGSIZE);
445 goto cleanup;
447 *dst = '\0';
449 list[j] = NULL;
450 if (j == 0) {
451 __set_errno(ENOENT);
452 goto cleanup;
454 return (list);
456 cleanup:
457 for (i = 0; i < j; i++)
458 free(list[i]);
459 free(list);
460 return (NULL);
463 struct __res_state *
464 __hesiod_res_get(void *context) {
465 struct hesiod_p *ctx = context;
467 if (!ctx->res) {
468 struct __res_state *res;
469 res = (struct __res_state *)malloc(sizeof *res);
470 if (res == NULL) {
471 __set_errno(ENOMEM);
472 return (NULL);
474 memset(res, 0, sizeof *res);
475 __hesiod_res_set(ctx, res, free);
478 return (ctx->res);
481 void
482 __hesiod_res_set(void *context, struct __res_state *res,
483 void (*free_res)(void *)) {
484 struct hesiod_p *ctx = context;
486 if (ctx->res && ctx->free_res) {
487 res_nclose(ctx->res);
488 (*ctx->free_res)(ctx->res);
491 ctx->res = res;
492 ctx->free_res = free_res;
495 static int
496 init(struct hesiod_p *ctx) {
498 if (!ctx->res && !__hesiod_res_get(ctx))
499 return (-1);
501 if (((ctx->res->options & RES_INIT) == 0) &&
502 (res_ninit(ctx->res) == -1))
503 return (-1);
505 return (0);