hesiod.c revision 1.22 1 1.22 christos /* $NetBSD: hesiod.c,v 1.22 2004/05/23 16:54:13 christos Exp $ */
2 1.2 lukem
3 1.6 lukem /* Copyright (c) 1996 by Internet Software Consortium.
4 1.2 lukem *
5 1.6 lukem * Permission to use, copy, modify, and distribute this software for any
6 1.6 lukem * purpose with or without fee is hereby granted, provided that the above
7 1.6 lukem * copyright notice and this permission notice appear in all copies.
8 1.6 lukem *
9 1.6 lukem * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 1.6 lukem * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 1.6 lukem * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 1.6 lukem * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 1.6 lukem * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 1.6 lukem * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 1.6 lukem * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 1.6 lukem * SOFTWARE.
17 1.6 lukem */
18 1.6 lukem
19 1.6 lukem /* Copyright 1996 by the Massachusetts Institute of Technology.
20 1.6 lukem *
21 1.6 lukem * Permission to use, copy, modify, and distribute this
22 1.6 lukem * software and its documentation for any purpose and without
23 1.6 lukem * fee is hereby granted, provided that the above copyright
24 1.6 lukem * notice appear in all copies and that both that copyright
25 1.6 lukem * notice and this permission notice appear in supporting
26 1.6 lukem * documentation, and that the name of M.I.T. not be used in
27 1.6 lukem * advertising or publicity pertaining to distribution of the
28 1.6 lukem * software without specific, written prior permission.
29 1.6 lukem * M.I.T. makes no representations about the suitability of
30 1.6 lukem * this software for any purpose. It is provided "as is"
31 1.6 lukem * without express or implied warranty.
32 1.6 lukem */
33 1.6 lukem
34 1.6 lukem /* This file is part of the hesiod library. It implements the core
35 1.6 lukem * portion of the hesiod resolver.
36 1.6 lukem *
37 1.6 lukem * This file is loosely based on an interim version of hesiod.c from
38 1.6 lukem * the BIND IRS library, which was in turn based on an earlier version
39 1.6 lukem * of this file. Extensive changes have been made on each step of the
40 1.6 lukem * path.
41 1.6 lukem *
42 1.21 christos * This implementation is thread-safe because it uses res_nsend().
43 1.2 lukem */
44 1.2 lukem
45 1.2 lukem #include <sys/cdefs.h>
46 1.2 lukem
47 1.6 lukem #if defined(LIBC_SCCS) && !defined(lint)
48 1.6 lukem __IDSTRING(rcsid_hesiod_c,
49 1.6 lukem "#Id: hesiod.c,v 1.18.2.1 1997/01/03 20:48:20 ghudson Exp #");
50 1.6 lukem __IDSTRING(rcsid_hesiod_p_h,
51 1.6 lukem "#Id: hesiod_p.h,v 1.1 1996/12/08 21:39:37 ghudson Exp #");
52 1.6 lukem __IDSTRING(rcsid_hescompat_c,
53 1.6 lukem "#Id: hescompat.c,v 1.1.2.1 1996/12/16 08:37:45 ghudson Exp #");
54 1.22 christos __RCSID("$NetBSD: hesiod.c,v 1.22 2004/05/23 16:54:13 christos Exp $");
55 1.6 lukem #endif /* LIBC_SCCS and not lint */
56 1.6 lukem
57 1.6 lukem #include "namespace.h"
58 1.2 lukem
59 1.2 lukem #include <sys/types.h>
60 1.2 lukem #include <sys/param.h>
61 1.2 lukem #include <netinet/in.h>
62 1.2 lukem #include <arpa/nameser.h>
63 1.2 lukem
64 1.10 lukem #include <assert.h>
65 1.6 lukem #include <ctype.h>
66 1.2 lukem #include <errno.h>
67 1.2 lukem #include <hesiod.h>
68 1.2 lukem #include <resolv.h>
69 1.2 lukem #include <stdio.h>
70 1.2 lukem #include <stdlib.h>
71 1.2 lukem #include <string.h>
72 1.15 lukem #include <unistd.h>
73 1.2 lukem
74 1.6 lukem #ifdef __weak_alias
75 1.12 mycroft __weak_alias(hesiod_init,_hesiod_init)
76 1.12 mycroft __weak_alias(hesiod_end,_hesiod_end)
77 1.12 mycroft __weak_alias(hesiod_to_bind,_hesiod_to_bind)
78 1.12 mycroft __weak_alias(hesiod_resolve,_hesiod_resolve)
79 1.12 mycroft __weak_alias(hesiod_free_list,_hesiod_free_list)
80 1.12 mycroft __weak_alias(hes_init,_hes_init)
81 1.12 mycroft __weak_alias(hes_to_bind,_hes_to_bind)
82 1.12 mycroft __weak_alias(hes_resolve,_hes_resolve)
83 1.12 mycroft __weak_alias(hes_error,_hes_error)
84 1.12 mycroft __weak_alias(hes_free,_hes_free)
85 1.6 lukem #endif
86 1.2 lukem
87 1.6 lukem struct hesiod_p {
88 1.6 lukem char *lhs; /* normally ".ns" */
89 1.6 lukem char *rhs; /* AKA the default hesiod domain */
90 1.6 lukem int classes[2]; /* The class search order. */
91 1.6 lukem };
92 1.6 lukem
93 1.6 lukem #define MAX_HESRESP 1024
94 1.6 lukem
95 1.6 lukem static int read_config_file __P((struct hesiod_p *, const char *));
96 1.6 lukem static char **get_txt_records __P((int, const char *));
97 1.6 lukem static int init_context __P((void));
98 1.6 lukem static void translate_errors __P((void));
99 1.2 lukem
100 1.2 lukem
101 1.6 lukem /*
102 1.6 lukem * hesiod_init --
103 1.6 lukem * initialize a hesiod_p.
104 1.6 lukem */
105 1.6 lukem int
106 1.6 lukem hesiod_init(context)
107 1.6 lukem void **context;
108 1.6 lukem {
109 1.6 lukem struct hesiod_p *ctx;
110 1.6 lukem const char *p, *configname;
111 1.10 lukem int serrno;
112 1.10 lukem
113 1.10 lukem _DIAGASSERT(context != NULL);
114 1.2 lukem
115 1.6 lukem ctx = malloc(sizeof(struct hesiod_p));
116 1.6 lukem if (ctx) {
117 1.6 lukem *context = ctx;
118 1.18 itojun /*
119 1.18 itojun * don't permit overrides from environment
120 1.18 itojun * for set.id programs
121 1.18 itojun */
122 1.15 lukem if (issetugid())
123 1.15 lukem configname = NULL;
124 1.15 lukem else
125 1.15 lukem configname = getenv("HESIOD_CONFIG");
126 1.6 lukem if (!configname)
127 1.6 lukem configname = _PATH_HESIOD_CONF;
128 1.6 lukem if (read_config_file(ctx, configname) >= 0) {
129 1.6 lukem /*
130 1.6 lukem * The default rhs can be overridden by an
131 1.15 lukem * environment variable, unless set.id.
132 1.6 lukem */
133 1.15 lukem if (issetugid())
134 1.15 lukem p = NULL;
135 1.15 lukem else
136 1.15 lukem p = getenv("HES_DOMAIN");
137 1.6 lukem if (p) {
138 1.6 lukem if (ctx->rhs)
139 1.6 lukem free(ctx->rhs);
140 1.6 lukem ctx->rhs = malloc(strlen(p) + 2);
141 1.6 lukem if (ctx->rhs) {
142 1.6 lukem *ctx->rhs = '.';
143 1.6 lukem strcpy(ctx->rhs + 1,
144 1.6 lukem (*p == '.') ? p + 1 : p);
145 1.6 lukem return 0;
146 1.6 lukem } else
147 1.6 lukem errno = ENOMEM;
148 1.6 lukem } else
149 1.6 lukem return 0;
150 1.6 lukem }
151 1.6 lukem } else
152 1.6 lukem errno = ENOMEM;
153 1.2 lukem
154 1.10 lukem serrno = errno;
155 1.6 lukem if (ctx->lhs)
156 1.6 lukem free(ctx->lhs);
157 1.6 lukem if (ctx->rhs)
158 1.6 lukem free(ctx->rhs);
159 1.6 lukem if (ctx)
160 1.6 lukem free(ctx);
161 1.10 lukem errno = serrno;
162 1.6 lukem return -1;
163 1.2 lukem }
164 1.2 lukem
165 1.6 lukem /*
166 1.6 lukem * hesiod_end --
167 1.6 lukem * Deallocates the hesiod_p.
168 1.6 lukem */
169 1.6 lukem void
170 1.6 lukem hesiod_end(context)
171 1.6 lukem void *context;
172 1.6 lukem {
173 1.6 lukem struct hesiod_p *ctx = (struct hesiod_p *) context;
174 1.6 lukem
175 1.10 lukem _DIAGASSERT(context != NULL);
176 1.10 lukem
177 1.6 lukem free(ctx->rhs);
178 1.6 lukem if (ctx->lhs)
179 1.6 lukem free(ctx->lhs);
180 1.6 lukem free(ctx);
181 1.6 lukem }
182 1.2 lukem
183 1.6 lukem /*
184 1.6 lukem * hesiod_to_bind --
185 1.6 lukem * takes a hesiod (name, type) and returns a DNS
186 1.6 lukem * name which is to be resolved.
187 1.6 lukem */
188 1.6 lukem char *
189 1.6 lukem hesiod_to_bind(void *context, const char *name, const char *type)
190 1.2 lukem {
191 1.6 lukem struct hesiod_p *ctx = (struct hesiod_p *) context;
192 1.6 lukem char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL;
193 1.6 lukem const char *rhs;
194 1.20 thorpej size_t len;
195 1.6 lukem
196 1.10 lukem _DIAGASSERT(context != NULL);
197 1.10 lukem _DIAGASSERT(name != NULL);
198 1.10 lukem _DIAGASSERT(type != NULL);
199 1.10 lukem
200 1.16 sommerfe if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) {
201 1.16 sommerfe errno = EMSGSIZE;
202 1.16 sommerfe return NULL;
203 1.16 sommerfe }
204 1.6 lukem
205 1.18 itojun /*
206 1.18 itojun * Find the right right hand side to use, possibly
207 1.18 itojun * truncating bindname.
208 1.18 itojun */
209 1.6 lukem p = strchr(bindname, '@');
210 1.6 lukem if (p) {
211 1.6 lukem *p++ = 0;
212 1.6 lukem if (strchr(p, '.'))
213 1.6 lukem rhs = name + (p - bindname);
214 1.6 lukem else {
215 1.6 lukem rhs_list = hesiod_resolve(context, p, "rhs-extension");
216 1.6 lukem if (rhs_list)
217 1.6 lukem rhs = *rhs_list;
218 1.6 lukem else {
219 1.6 lukem errno = ENOENT;
220 1.4 christos return NULL;
221 1.6 lukem }
222 1.2 lukem }
223 1.6 lukem } else
224 1.6 lukem rhs = ctx->rhs;
225 1.6 lukem
226 1.18 itojun /* See if we have enough room. */
227 1.6 lukem len = strlen(bindname) + 1 + strlen(type);
228 1.6 lukem if (ctx->lhs)
229 1.6 lukem len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
230 1.6 lukem len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
231 1.6 lukem if (len > sizeof(bindname) - 1) {
232 1.6 lukem if (rhs_list)
233 1.6 lukem hesiod_free_list(context, rhs_list);
234 1.6 lukem errno = EMSGSIZE;
235 1.6 lukem return NULL;
236 1.2 lukem }
237 1.18 itojun /* Put together the rest of the domain. */
238 1.19 itojun strlcat(bindname, ".", sizeof(bindname));
239 1.19 itojun strlcat(bindname, type, sizeof(bindname));
240 1.18 itojun /* Only append lhs if it isn't empty. */
241 1.9 simonb if (ctx->lhs && ctx->lhs[0] != '\0' ) {
242 1.6 lukem if (ctx->lhs[0] != '.')
243 1.19 itojun strlcat(bindname, ".", sizeof(bindname));
244 1.19 itojun strlcat(bindname, ctx->lhs, sizeof(bindname));
245 1.6 lukem }
246 1.6 lukem if (rhs[0] != '.')
247 1.19 itojun strlcat(bindname, ".", sizeof(bindname));
248 1.19 itojun strlcat(bindname, rhs, sizeof(bindname));
249 1.6 lukem
250 1.18 itojun /* rhs_list is no longer needed, since we're done with rhs. */
251 1.6 lukem if (rhs_list)
252 1.6 lukem hesiod_free_list(context, rhs_list);
253 1.6 lukem
254 1.18 itojun /* Make a copy of the result and return it to the caller. */
255 1.6 lukem ret = strdup(bindname);
256 1.17 groo if (ret == NULL)
257 1.6 lukem errno = ENOMEM;
258 1.6 lukem return ret;
259 1.6 lukem }
260 1.6 lukem
261 1.6 lukem /*
262 1.6 lukem * hesiod_resolve --
263 1.6 lukem * Given a hesiod name and type, return an array of strings returned
264 1.6 lukem * by the resolver.
265 1.6 lukem */
266 1.6 lukem char **
267 1.6 lukem hesiod_resolve(context, name, type)
268 1.6 lukem void *context;
269 1.6 lukem const char *name;
270 1.6 lukem const char *type;
271 1.6 lukem {
272 1.6 lukem struct hesiod_p *ctx = (struct hesiod_p *) context;
273 1.6 lukem char *bindname, **retvec;
274 1.2 lukem
275 1.10 lukem _DIAGASSERT(context != NULL);
276 1.10 lukem _DIAGASSERT(name != NULL);
277 1.10 lukem _DIAGASSERT(type != NULL);
278 1.10 lukem
279 1.6 lukem bindname = hesiod_to_bind(context, name, type);
280 1.6 lukem if (!bindname)
281 1.6 lukem return NULL;
282 1.2 lukem
283 1.6 lukem retvec = get_txt_records(ctx->classes[0], bindname);
284 1.6 lukem if (retvec == NULL && errno == ENOENT && ctx->classes[1])
285 1.6 lukem retvec = get_txt_records(ctx->classes[1], bindname);
286 1.2 lukem
287 1.6 lukem free(bindname);
288 1.6 lukem return retvec;
289 1.6 lukem }
290 1.2 lukem
291 1.6 lukem /*ARGSUSED*/
292 1.6 lukem void
293 1.6 lukem hesiod_free_list(context, list)
294 1.6 lukem void *context;
295 1.6 lukem char **list;
296 1.6 lukem {
297 1.6 lukem char **p;
298 1.2 lukem
299 1.10 lukem _DIAGASSERT(context != NULL);
300 1.10 lukem
301 1.7 lukem if (list == NULL)
302 1.7 lukem return;
303 1.6 lukem for (p = list; *p; p++)
304 1.6 lukem free(*p);
305 1.6 lukem free(list);
306 1.2 lukem }
307 1.2 lukem
308 1.6 lukem
309 1.6 lukem /* read_config_file --
310 1.6 lukem * Parse the /etc/hesiod.conf file. Returns 0 on success,
311 1.6 lukem * -1 on failure. On failure, it might leave values in ctx->lhs
312 1.6 lukem * or ctx->rhs which need to be freed by the caller.
313 1.2 lukem */
314 1.6 lukem static int
315 1.6 lukem read_config_file(ctx, filename)
316 1.6 lukem struct hesiod_p *ctx;
317 1.6 lukem const char *filename;
318 1.6 lukem {
319 1.6 lukem char *key, *data, *p, **which;
320 1.6 lukem char buf[MAXDNAME + 7];
321 1.6 lukem int n;
322 1.6 lukem FILE *fp;
323 1.2 lukem
324 1.10 lukem _DIAGASSERT(ctx != NULL);
325 1.10 lukem _DIAGASSERT(filename != NULL);
326 1.10 lukem
327 1.18 itojun /* Set default query classes. */
328 1.8 lukem ctx->classes[0] = C_IN;
329 1.8 lukem ctx->classes[1] = C_HS;
330 1.6 lukem
331 1.18 itojun /* Try to open the configuration file. */
332 1.6 lukem fp = fopen(filename, "r");
333 1.6 lukem if (!fp) {
334 1.6 lukem /* Use compiled in default domain names. */
335 1.6 lukem ctx->lhs = strdup(DEF_LHS);
336 1.6 lukem ctx->rhs = strdup(DEF_RHS);
337 1.6 lukem if (ctx->lhs && ctx->rhs)
338 1.6 lukem return 0;
339 1.6 lukem else {
340 1.6 lukem errno = ENOMEM;
341 1.6 lukem return -1;
342 1.6 lukem }
343 1.6 lukem }
344 1.6 lukem ctx->lhs = NULL;
345 1.6 lukem ctx->rhs = NULL;
346 1.6 lukem while (fgets(buf, sizeof(buf), fp) != NULL) {
347 1.6 lukem p = buf;
348 1.6 lukem if (*p == '#' || *p == '\n' || *p == '\r')
349 1.6 lukem continue;
350 1.6 lukem while (*p == ' ' || *p == '\t')
351 1.6 lukem p++;
352 1.6 lukem key = p;
353 1.17 groo while (*p != ' ' && *p != '\t' && *p != '=' && *p)
354 1.6 lukem p++;
355 1.17 groo
356 1.17 groo if (*p == '\0')
357 1.17 groo continue;
358 1.17 groo
359 1.6 lukem *p++ = 0;
360 1.6 lukem
361 1.14 itohy while (isspace((u_char) *p) || *p == '=')
362 1.6 lukem p++;
363 1.17 groo
364 1.17 groo if (*p == '\0')
365 1.17 groo continue;
366 1.17 groo
367 1.6 lukem data = p;
368 1.17 groo while (!isspace((u_char) *p) && *p)
369 1.6 lukem p++;
370 1.17 groo
371 1.6 lukem *p = 0;
372 1.6 lukem
373 1.6 lukem if (strcasecmp(key, "lhs") == 0 ||
374 1.6 lukem strcasecmp(key, "rhs") == 0) {
375 1.6 lukem which = (strcasecmp(key, "lhs") == 0)
376 1.6 lukem ? &ctx->lhs : &ctx->rhs;
377 1.6 lukem *which = strdup(data);
378 1.6 lukem if (!*which) {
379 1.6 lukem errno = ENOMEM;
380 1.6 lukem return -1;
381 1.6 lukem }
382 1.6 lukem } else {
383 1.6 lukem if (strcasecmp(key, "classes") == 0) {
384 1.6 lukem n = 0;
385 1.6 lukem while (*data && n < 2) {
386 1.6 lukem p = data;
387 1.6 lukem while (*p && *p != ',')
388 1.6 lukem p++;
389 1.6 lukem if (*p)
390 1.6 lukem *p++ = 0;
391 1.6 lukem if (strcasecmp(data, "IN") == 0)
392 1.6 lukem ctx->classes[n++] = C_IN;
393 1.6 lukem else
394 1.6 lukem if (strcasecmp(data, "HS") == 0)
395 1.6 lukem ctx->classes[n++] =
396 1.6 lukem C_HS;
397 1.6 lukem data = p;
398 1.6 lukem }
399 1.6 lukem while (n < 2)
400 1.6 lukem ctx->classes[n++] = 0;
401 1.6 lukem }
402 1.6 lukem }
403 1.6 lukem }
404 1.6 lukem fclose(fp);
405 1.2 lukem
406 1.6 lukem if (!ctx->rhs || ctx->classes[0] == 0 ||
407 1.6 lukem ctx->classes[0] == ctx->classes[1]) {
408 1.6 lukem errno = ENOEXEC;
409 1.6 lukem return -1;
410 1.2 lukem }
411 1.6 lukem return 0;
412 1.6 lukem }
413 1.2 lukem
414 1.6 lukem /*
415 1.6 lukem * get_txt_records --
416 1.6 lukem * Given a DNS class and a DNS name, do a lookup for TXT records, and
417 1.6 lukem * return a list of them.
418 1.6 lukem */
419 1.6 lukem static char **
420 1.6 lukem get_txt_records(qclass, name)
421 1.6 lukem int qclass;
422 1.6 lukem const char *name;
423 1.6 lukem {
424 1.6 lukem HEADER *hp;
425 1.6 lukem unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
426 1.6 lukem char *dst, **list;
427 1.6 lukem int ancount, qdcount, i, j, n, skip, type, class, len;
428 1.21 christos res_state res = __res_get_state();
429 1.6 lukem
430 1.22 christos if (res == NULL)
431 1.22 christos return NULL;
432 1.22 christos
433 1.10 lukem _DIAGASSERT(name != NULL);
434 1.10 lukem
435 1.18 itojun /* Construct the query. */
436 1.21 christos n = res_nmkquery(res, QUERY, name, qclass, T_TXT, NULL, 0,
437 1.2 lukem NULL, qbuf, PACKETSZ);
438 1.13 ghudson if (n < 0) {
439 1.13 ghudson errno = EMSGSIZE;
440 1.21 christos __res_put_state(res);
441 1.6 lukem return NULL;
442 1.13 ghudson }
443 1.6 lukem
444 1.18 itojun /* Send the query. */
445 1.21 christos n = res_nsend(res, qbuf, n, abuf, MAX_HESRESP);
446 1.21 christos __res_put_state(res);
447 1.2 lukem if (n < 0) {
448 1.6 lukem errno = ECONNREFUSED;
449 1.6 lukem return NULL;
450 1.6 lukem }
451 1.18 itojun /* Parse the header of the result. */
452 1.6 lukem hp = (HEADER *) (void *) abuf;
453 1.6 lukem ancount = ntohs(hp->ancount);
454 1.6 lukem qdcount = ntohs(hp->qdcount);
455 1.6 lukem p = abuf + sizeof(HEADER);
456 1.6 lukem eom = abuf + n;
457 1.6 lukem
458 1.18 itojun /*
459 1.18 itojun * Skip questions, trying to get to the answer section
460 1.18 itojun * which follows.
461 1.18 itojun */
462 1.6 lukem for (i = 0; i < qdcount; i++) {
463 1.6 lukem skip = dn_skipname(p, eom);
464 1.6 lukem if (skip < 0 || p + skip + QFIXEDSZ > eom) {
465 1.6 lukem errno = EMSGSIZE;
466 1.6 lukem return NULL;
467 1.6 lukem }
468 1.6 lukem p += skip + QFIXEDSZ;
469 1.2 lukem }
470 1.2 lukem
471 1.18 itojun /* Allocate space for the text record answers. */
472 1.6 lukem list = malloc((ancount + 1) * sizeof(char *));
473 1.6 lukem if (!list) {
474 1.6 lukem errno = ENOMEM;
475 1.6 lukem return NULL;
476 1.6 lukem }
477 1.18 itojun /* Parse the answers. */
478 1.6 lukem j = 0;
479 1.6 lukem for (i = 0; i < ancount; i++) {
480 1.6 lukem /* Parse the header of this answer. */
481 1.6 lukem skip = dn_skipname(p, eom);
482 1.6 lukem if (skip < 0 || p + skip + 10 > eom)
483 1.6 lukem break;
484 1.6 lukem type = p[skip + 0] << 8 | p[skip + 1];
485 1.6 lukem class = p[skip + 2] << 8 | p[skip + 3];
486 1.6 lukem len = p[skip + 8] << 8 | p[skip + 9];
487 1.6 lukem p += skip + 10;
488 1.6 lukem if (p + len > eom) {
489 1.6 lukem errno = EMSGSIZE;
490 1.6 lukem break;
491 1.6 lukem }
492 1.6 lukem /* Skip entries of the wrong class and type. */
493 1.6 lukem if (class != qclass || type != T_TXT) {
494 1.6 lukem p += len;
495 1.6 lukem continue;
496 1.6 lukem }
497 1.6 lukem /* Allocate space for this answer. */
498 1.6 lukem list[j] = malloc((size_t)len);
499 1.6 lukem if (!list[j]) {
500 1.6 lukem errno = ENOMEM;
501 1.6 lukem break;
502 1.6 lukem }
503 1.6 lukem dst = list[j++];
504 1.2 lukem
505 1.6 lukem /* Copy answer data into the allocated area. */
506 1.6 lukem eor = p + len;
507 1.6 lukem while (p < eor) {
508 1.6 lukem n = (unsigned char) *p++;
509 1.6 lukem if (p + n > eor) {
510 1.6 lukem errno = EMSGSIZE;
511 1.6 lukem break;
512 1.6 lukem }
513 1.6 lukem memcpy(dst, p, (size_t)n);
514 1.6 lukem p += n;
515 1.6 lukem dst += n;
516 1.6 lukem }
517 1.6 lukem if (p < eor) {
518 1.6 lukem errno = EMSGSIZE;
519 1.6 lukem break;
520 1.6 lukem }
521 1.6 lukem *dst = 0;
522 1.6 lukem }
523 1.2 lukem
524 1.18 itojun /*
525 1.18 itojun * If we didn't terminate the loop normally, something
526 1.18 itojun * went wrong.
527 1.18 itojun */
528 1.6 lukem if (i < ancount) {
529 1.6 lukem for (i = 0; i < j; i++)
530 1.6 lukem free(list[i]);
531 1.6 lukem free(list);
532 1.6 lukem return NULL;
533 1.6 lukem }
534 1.6 lukem if (j == 0) {
535 1.6 lukem errno = ENOENT;
536 1.6 lukem free(list);
537 1.6 lukem return NULL;
538 1.2 lukem }
539 1.6 lukem list[j] = NULL;
540 1.6 lukem return list;
541 1.6 lukem }
542 1.2 lukem
543 1.18 itojun /*
544 1.18 itojun * COMPATIBILITY FUNCTIONS
545 1.18 itojun */
546 1.6 lukem
547 1.6 lukem static int inited = 0;
548 1.6 lukem static void *context;
549 1.6 lukem static int errval = HES_ER_UNINIT;
550 1.2 lukem
551 1.2 lukem int
552 1.2 lukem hes_init()
553 1.2 lukem {
554 1.6 lukem init_context();
555 1.6 lukem return errval;
556 1.2 lukem }
557 1.2 lukem
558 1.2 lukem char *
559 1.6 lukem hes_to_bind(name, type)
560 1.6 lukem const char *name;
561 1.6 lukem const char *type;
562 1.2 lukem {
563 1.6 lukem static char *bindname;
564 1.10 lukem
565 1.10 lukem _DIAGASSERT(name != NULL);
566 1.10 lukem _DIAGASSERT(type != NULL);
567 1.10 lukem
568 1.6 lukem if (init_context() < 0)
569 1.6 lukem return NULL;
570 1.6 lukem if (bindname)
571 1.6 lukem free(bindname);
572 1.6 lukem bindname = hesiod_to_bind(context, name, type);
573 1.6 lukem if (!bindname)
574 1.6 lukem translate_errors();
575 1.6 lukem return bindname;
576 1.2 lukem }
577 1.2 lukem
578 1.2 lukem char **
579 1.6 lukem hes_resolve(name, type)
580 1.6 lukem const char *name;
581 1.6 lukem const char *type;
582 1.2 lukem {
583 1.6 lukem static char **list;
584 1.6 lukem
585 1.10 lukem _DIAGASSERT(name != NULL);
586 1.10 lukem _DIAGASSERT(type != NULL);
587 1.10 lukem
588 1.6 lukem if (init_context() < 0)
589 1.6 lukem return NULL;
590 1.6 lukem
591 1.6 lukem /*
592 1.6 lukem * In the old Hesiod interface, the caller was responsible for
593 1.6 lukem * freeing the returned strings but not the vector of strings itself.
594 1.6 lukem */
595 1.6 lukem if (list)
596 1.6 lukem free(list);
597 1.6 lukem
598 1.6 lukem list = hesiod_resolve(context, name, type);
599 1.6 lukem if (!list)
600 1.6 lukem translate_errors();
601 1.6 lukem return list;
602 1.2 lukem }
603 1.2 lukem
604 1.2 lukem int
605 1.2 lukem hes_error()
606 1.2 lukem {
607 1.6 lukem return errval;
608 1.2 lukem }
609 1.2 lukem
610 1.2 lukem void
611 1.2 lukem hes_free(hp)
612 1.2 lukem char **hp;
613 1.2 lukem {
614 1.7 lukem hesiod_free_list(context, hp);
615 1.6 lukem }
616 1.6 lukem
617 1.6 lukem static int
618 1.6 lukem init_context()
619 1.6 lukem {
620 1.6 lukem if (!inited) {
621 1.6 lukem inited = 1;
622 1.6 lukem if (hesiod_init(&context) < 0) {
623 1.6 lukem errval = HES_ER_CONFIG;
624 1.6 lukem return -1;
625 1.6 lukem }
626 1.6 lukem errval = HES_ER_OK;
627 1.6 lukem }
628 1.6 lukem return 0;
629 1.6 lukem }
630 1.6 lukem
631 1.6 lukem static void
632 1.6 lukem translate_errors()
633 1.6 lukem {
634 1.6 lukem switch (errno) {
635 1.6 lukem case ENOENT:
636 1.6 lukem errval = HES_ER_NOTFOUND;
637 1.6 lukem break;
638 1.6 lukem case ECONNREFUSED:
639 1.6 lukem case EMSGSIZE:
640 1.6 lukem errval = HES_ER_NET;
641 1.6 lukem break;
642 1.10 lukem case EFAULT:
643 1.6 lukem case ENOMEM:
644 1.6 lukem default:
645 1.6 lukem /* Not a good match, but the best we can do. */
646 1.6 lukem errval = HES_ER_CONFIG;
647 1.6 lukem break;
648 1.6 lukem }
649 1.2 lukem }
650