* sysdeps/unix/sysv/linux/s390/s390-32/clone.S (__clone): Add support for
[glibc.git] / hesiod / hesiod.c
blob7fffb310f1a741733c3e2dc6cb9dd108d025be91
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 return (-1);
83 ctx->LHS = NULL;
84 ctx->RHS = NULL;
85 ctx->res = NULL;
87 configname = __secure_getenv("HESIOD_CONFIG");
88 if (!configname)
89 configname = _PATH_HESIOD_CONF;
90 if (parse_config_file(ctx, configname) < 0) {
91 #ifdef DEF_RHS
93 * Use compiled in defaults.
95 ctx->LHS = malloc(strlen(DEF_LHS)+1);
96 ctx->RHS = malloc(strlen(DEF_RHS)+1);
97 if (ctx->LHS == 0 || ctx->RHS == 0)
98 goto cleanup;
99 strcpy(ctx->LHS, DEF_LHS);
100 strcpy(ctx->RHS, DEF_RHS);
101 #else
102 goto cleanup;
103 #endif
106 * The default RHS can be overridden by an environment
107 * variable.
109 if ((cp = __secure_getenv("HES_DOMAIN")) != NULL) {
110 free(ctx->RHS);
111 ctx->RHS = malloc(strlen(cp)+2);
112 if (!ctx->RHS)
113 goto cleanup;
114 if (cp[0] == '.')
115 strcpy(ctx->RHS, cp);
116 else {
117 ctx->RHS[0] = '.';
118 strcpy(ctx->RHS + 1, cp);
123 * If there is no default hesiod realm set, we return an
124 * error.
126 if (!ctx->RHS) {
127 __set_errno(ENOEXEC);
128 goto cleanup;
131 #if 0
132 if (res_ninit(ctx->res) < 0)
133 goto cleanup;
134 #endif
136 *context = ctx;
137 return (0);
139 cleanup:
140 hesiod_end(ctx);
141 return (-1);
145 * This function deallocates the hesiod_p
147 void
148 hesiod_end(void *context) {
149 struct hesiod_p *ctx = (struct hesiod_p *) context;
150 int save_errno = errno;
152 if (ctx->res)
153 res_nclose(ctx->res);
154 free(ctx->RHS);
155 free(ctx->LHS);
156 if (ctx->res && ctx->free_res)
157 (*ctx->free_res)(ctx->res);
158 free(ctx);
159 __set_errno(save_errno);
163 * This function takes a hesiod (name, type) and returns a DNS
164 * name which is to be resolved.
166 char *
167 hesiod_to_bind(void *context, const char *name, const char *type) {
168 struct hesiod_p *ctx = (struct hesiod_p *) context;
169 char *bindname;
170 char **rhs_list = NULL;
171 const char *RHS, *cp;
172 char *endp;
174 /* Decide what our RHS is, and set cp to the end of the actual name. */
175 if ((cp = strchr(name, '@')) != NULL) {
176 if (strchr(cp + 1, '.'))
177 RHS = cp + 1;
178 else if ((rhs_list = hesiod_resolve(context, cp + 1,
179 "rhs-extension")) != NULL)
180 RHS = *rhs_list;
181 else {
182 __set_errno(ENOENT);
183 return (NULL);
185 } else {
186 RHS = ctx->RHS;
187 cp = name + strlen(name);
191 * Allocate the space we need, including up to three periods and
192 * the terminating NUL.
194 if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
195 (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
196 if (rhs_list)
197 hesiod_free_list(context, rhs_list);
198 return NULL;
201 /* Now put together the DNS name. */
202 endp = (char *) __mempcpy (bindname, name, cp - name);
203 *endp++ = '.';
204 endp = (char *) __stpcpy (endp, type);
205 if (ctx->LHS) {
206 if (ctx->LHS[0] != '.')
207 *endp++ = '.';
208 endp = __stpcpy (endp, ctx->LHS);
210 if (RHS[0] != '.')
211 *endp++ = '.';
212 strcpy (endp, 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 && errno != ECONNREFUSED)
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 for (p = list; *p; p++)
255 free(*p);
256 free(list);
260 * This function parses the /etc/hesiod.conf file
262 static int
263 parse_config_file(struct hesiod_p *ctx, const char *filename) {
264 char *key, *data, *cp, **cpp;
265 char buf[MAXDNAME+7];
266 FILE *fp;
269 * Clear the existing configuration variable, just in case
270 * they're set.
272 free(ctx->RHS);
273 free(ctx->LHS);
274 ctx->RHS = ctx->LHS = 0;
277 * Now open and parse the file...
279 if (!(fp = fopen(filename, "r")))
280 return (-1);
282 while (fgets(buf, sizeof(buf), fp) != NULL) {
283 cp = buf;
284 if (*cp == '#' || *cp == '\n' || *cp == '\r')
285 continue;
286 while(*cp == ' ' || *cp == '\t')
287 cp++;
288 key = cp;
289 while(*cp != ' ' && *cp != '\t' && *cp != '=')
290 cp++;
291 *cp++ = '\0';
293 while(*cp == ' ' || *cp == '\t' || *cp == '=')
294 cp++;
295 data = cp;
296 while(*cp != ' ' && *cp != '\n' && *cp != '\r')
297 cp++;
298 *cp++ = '\0';
300 if (strcmp(key, "lhs") == 0)
301 cpp = &ctx->LHS;
302 else if (strcmp(key, "rhs") == 0)
303 cpp = &ctx->RHS;
304 else
305 continue;
307 *cpp = malloc(strlen(data) + 1);
308 if (!*cpp)
309 goto cleanup;
310 strcpy(*cpp, data);
312 fclose(fp);
313 return (0);
315 cleanup:
316 fclose(fp);
317 free(ctx->RHS);
318 free(ctx->LHS);
319 ctx->RHS = ctx->LHS = 0;
320 return (-1);
324 * Given a DNS class and a DNS name, do a lookup for TXT records, and
325 * return a list of them.
327 static char **
328 get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
329 struct {
330 int type; /* RR type */
331 int class; /* RR class */
332 int dlen; /* len of data section */
333 u_char *data; /* pointer to data */
334 } rr;
335 HEADER *hp;
336 u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
337 u_char *cp, *erdata, *eom;
338 char *dst, *edst, **list;
339 int ancount, qdcount;
340 int i, j, n, skip;
343 * Construct the query and send it.
345 n = res_nmkquery(ctx->res, QUERY, name, class, T_TXT, NULL, 0,
346 NULL, qbuf, MAX_HESRESP);
347 if (n < 0) {
348 __set_errno(EMSGSIZE);
349 return (NULL);
351 n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP);
352 if (n < 0) {
353 __set_errno(ECONNREFUSED);
354 return (NULL);
356 if (n < HFIXEDSZ) {
357 __set_errno(EMSGSIZE);
358 return (NULL);
362 * OK, parse the result.
364 hp = (HEADER *) abuf;
365 ancount = ntohs(hp->ancount);
366 qdcount = ntohs(hp->qdcount);
367 cp = abuf + sizeof(HEADER);
368 eom = abuf + n;
370 /* Skip query, trying to get to the answer section which follows. */
371 for (i = 0; i < qdcount; i++) {
372 skip = dn_skipname(cp, eom);
373 if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
374 __set_errno(EMSGSIZE);
375 return (NULL);
377 cp += skip + QFIXEDSZ;
380 list = malloc((ancount + 1) * sizeof(char *));
381 if (!list)
382 return (NULL);
383 j = 0;
384 for (i = 0; i < ancount; i++) {
385 skip = dn_skipname(cp, eom);
386 if (skip < 0) {
387 __set_errno(EMSGSIZE);
388 goto cleanup;
390 cp += skip;
391 if (cp + 3 * INT16SZ + INT32SZ > eom) {
392 __set_errno(EMSGSIZE);
393 goto cleanup;
395 rr.type = ns_get16(cp);
396 cp += INT16SZ;
397 rr.class = ns_get16(cp);
398 cp += INT16SZ + INT32SZ; /* skip the ttl, too */
399 rr.dlen = ns_get16(cp);
400 cp += INT16SZ;
401 if (cp + rr.dlen > eom) {
402 __set_errno(EMSGSIZE);
403 goto cleanup;
405 rr.data = cp;
406 cp += rr.dlen;
407 if (rr.class != class || rr.type != T_TXT)
408 continue;
409 if (!(list[j] = malloc(rr.dlen)))
410 goto cleanup;
411 dst = list[j++];
412 edst = dst + rr.dlen;
413 erdata = rr.data + rr.dlen;
414 cp = rr.data;
415 while (cp < erdata) {
416 n = (unsigned char) *cp++;
417 if (cp + n > eom || dst + n > edst) {
418 __set_errno(EMSGSIZE);
419 goto cleanup;
421 memcpy(dst, cp, n);
422 cp += n;
423 dst += n;
425 if (cp != erdata) {
426 __set_errno(EMSGSIZE);
427 goto cleanup;
429 *dst = '\0';
431 list[j] = NULL;
432 if (j == 0) {
433 __set_errno(ENOENT);
434 goto cleanup;
436 return (list);
438 cleanup:
439 for (i = 0; i < j; i++)
440 free(list[i]);
441 free(list);
442 return (NULL);
445 struct __res_state *
446 __hesiod_res_get(void *context) {
447 struct hesiod_p *ctx = context;
449 if (!ctx->res) {
450 struct __res_state *res;
451 res = (struct __res_state *)calloc(1, sizeof *res);
452 if (res == NULL)
453 return (NULL);
454 __hesiod_res_set(ctx, res, free);
457 return (ctx->res);
460 void
461 __hesiod_res_set(void *context, struct __res_state *res,
462 void (*free_res)(void *)) {
463 struct hesiod_p *ctx = context;
465 if (ctx->res && ctx->free_res) {
466 res_nclose(ctx->res);
467 if ((ctx->res->options & RES_INIT) && ctx->res->nscount > 0) {
468 for (int ns = 0; ns < MAXNS; ns++) {
469 free (ctx->res->_u._ext.nsaddrs[ns]);
470 ctx->res->_u._ext.nsaddrs[ns] = NULL;
473 (*ctx->free_res)(ctx->res);
476 ctx->res = res;
477 ctx->free_res = free_res;
480 static int
481 init(struct hesiod_p *ctx) {
483 if (!ctx->res && !__hesiod_res_get(ctx))
484 return (-1);
486 if (__res_maybe_init (ctx->res, 0) == -1)
487 return (-1);
489 return (0);