Home | History | Annotate | Line # | Download | only in net
hesiod.c revision 1.27
      1  1.27      matt /*	$NetBSD: hesiod.c,v 1.27 2012/03/20 17:44:18 matt 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.27      matt __RCSID("$NetBSD: hesiod.c,v 1.27 2012/03/20 17:44:18 matt 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.27      matt static int	  read_config_file(struct hesiod_p *, const char *);
     96  1.27      matt static char	**get_txt_records(int, const char *);
     97  1.27      matt static int	  init_context(void);
     98  1.27      matt static void	  translate_errors(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.27      matt hesiod_init(void **context)
    107   1.6     lukem {
    108   1.6     lukem 	struct hesiod_p	*ctx;
    109   1.6     lukem 	const char	*p, *configname;
    110  1.10     lukem 	int serrno;
    111  1.10     lukem 
    112  1.10     lukem 	_DIAGASSERT(context != NULL);
    113   1.2     lukem 
    114  1.23     lukem 	ctx = calloc(1, sizeof(struct hesiod_p));
    115   1.6     lukem 	if (ctx) {
    116   1.6     lukem 		*context = ctx;
    117  1.18    itojun 		/*
    118  1.18    itojun 		 * don't permit overrides from environment
    119  1.18    itojun 		 * for set.id programs
    120  1.18    itojun 		 */
    121  1.15     lukem 		if (issetugid())
    122  1.15     lukem 			configname = NULL;
    123  1.15     lukem 		else
    124  1.15     lukem 			configname = getenv("HESIOD_CONFIG");
    125   1.6     lukem 		if (!configname)
    126   1.6     lukem 			configname = _PATH_HESIOD_CONF;
    127   1.6     lukem 		if (read_config_file(ctx, configname) >= 0) {
    128   1.6     lukem 			/*
    129   1.6     lukem 			 * The default rhs can be overridden by an
    130  1.15     lukem 			 * environment variable, unless set.id.
    131   1.6     lukem 			 */
    132  1.15     lukem 			if (issetugid())
    133  1.15     lukem 				p = NULL;
    134  1.15     lukem 			else
    135  1.15     lukem 				p = getenv("HES_DOMAIN");
    136   1.6     lukem 			if (p) {
    137   1.6     lukem 				if (ctx->rhs)
    138   1.6     lukem 					free(ctx->rhs);
    139   1.6     lukem 				ctx->rhs = malloc(strlen(p) + 2);
    140   1.6     lukem 				if (ctx->rhs) {
    141   1.6     lukem 					*ctx->rhs = '.';
    142   1.6     lukem 					strcpy(ctx->rhs + 1,
    143   1.6     lukem 					    (*p == '.') ? p + 1 : p);
    144   1.6     lukem 					return 0;
    145   1.6     lukem 				} else
    146   1.6     lukem 					errno = ENOMEM;
    147   1.6     lukem 			} else
    148   1.6     lukem 				return 0;
    149   1.6     lukem 		}
    150   1.6     lukem 	} else
    151   1.6     lukem 		errno = ENOMEM;
    152   1.2     lukem 
    153  1.10     lukem 	serrno = errno;
    154  1.23     lukem 	if (ctx) {
    155  1.23     lukem 		if (ctx->lhs)
    156  1.23     lukem 			free(ctx->lhs);
    157  1.23     lukem 		if (ctx->rhs)
    158  1.23     lukem 			free(ctx->rhs);
    159   1.6     lukem 		free(ctx);
    160  1.23     lukem 	}
    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.27      matt hesiod_end(void *context)
    171   1.6     lukem {
    172   1.6     lukem 	struct hesiod_p *ctx = (struct hesiod_p *) context;
    173   1.6     lukem 
    174  1.10     lukem 	_DIAGASSERT(context != NULL);
    175  1.10     lukem 
    176   1.6     lukem 	free(ctx->rhs);
    177   1.6     lukem 	if (ctx->lhs)
    178   1.6     lukem 		free(ctx->lhs);
    179   1.6     lukem 	free(ctx);
    180   1.6     lukem }
    181   1.2     lukem 
    182   1.6     lukem /*
    183   1.6     lukem  * hesiod_to_bind --
    184   1.6     lukem  * 	takes a hesiod (name, type) and returns a DNS
    185   1.6     lukem  *	name which is to be resolved.
    186   1.6     lukem  */
    187   1.6     lukem char *
    188   1.6     lukem hesiod_to_bind(void *context, const char *name, const char *type)
    189   1.2     lukem {
    190   1.6     lukem 	struct hesiod_p *ctx = (struct hesiod_p *) context;
    191   1.6     lukem 	char		 bindname[MAXDNAME], *p, *ret, **rhs_list = NULL;
    192   1.6     lukem 	const char	*rhs;
    193  1.20   thorpej 	size_t		 len;
    194   1.6     lukem 
    195  1.10     lukem 	_DIAGASSERT(context != NULL);
    196  1.10     lukem 	_DIAGASSERT(name != NULL);
    197  1.10     lukem 	_DIAGASSERT(type != NULL);
    198  1.10     lukem 
    199  1.16  sommerfe         if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) {
    200  1.16  sommerfe                 errno = EMSGSIZE;
    201  1.16  sommerfe                 return NULL;
    202  1.16  sommerfe         }
    203   1.6     lukem 
    204  1.18    itojun 	/*
    205  1.18    itojun 	 * Find the right right hand side to use, possibly
    206  1.18    itojun 	 * truncating bindname.
    207  1.18    itojun 	 */
    208   1.6     lukem 	p = strchr(bindname, '@');
    209   1.6     lukem 	if (p) {
    210   1.6     lukem 		*p++ = 0;
    211   1.6     lukem 		if (strchr(p, '.'))
    212   1.6     lukem 			rhs = name + (p - bindname);
    213   1.6     lukem 		else {
    214   1.6     lukem 			rhs_list = hesiod_resolve(context, p, "rhs-extension");
    215   1.6     lukem 			if (rhs_list)
    216   1.6     lukem 				rhs = *rhs_list;
    217   1.6     lukem 			else {
    218   1.6     lukem 				errno = ENOENT;
    219   1.4  christos 				return NULL;
    220   1.6     lukem 			}
    221   1.2     lukem 		}
    222   1.6     lukem 	} else
    223   1.6     lukem 		rhs = ctx->rhs;
    224   1.6     lukem 
    225  1.18    itojun 	/* See if we have enough room. */
    226   1.6     lukem 	len = strlen(bindname) + 1 + strlen(type);
    227   1.6     lukem 	if (ctx->lhs)
    228   1.6     lukem 		len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
    229   1.6     lukem 	len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
    230   1.6     lukem 	if (len > sizeof(bindname) - 1) {
    231   1.6     lukem 		if (rhs_list)
    232   1.6     lukem 			hesiod_free_list(context, rhs_list);
    233   1.6     lukem 		errno = EMSGSIZE;
    234   1.6     lukem 		return NULL;
    235   1.2     lukem 	}
    236  1.18    itojun 	/* Put together the rest of the domain. */
    237  1.19    itojun 	strlcat(bindname, ".", sizeof(bindname));
    238  1.19    itojun 	strlcat(bindname, type, sizeof(bindname));
    239  1.18    itojun 	/* Only append lhs if it isn't empty. */
    240   1.9    simonb 	if (ctx->lhs && ctx->lhs[0] != '\0' ) {
    241   1.6     lukem 		if (ctx->lhs[0] != '.')
    242  1.19    itojun 			strlcat(bindname, ".", sizeof(bindname));
    243  1.19    itojun 		strlcat(bindname, ctx->lhs, sizeof(bindname));
    244   1.6     lukem 	}
    245   1.6     lukem 	if (rhs[0] != '.')
    246  1.19    itojun 		strlcat(bindname, ".", sizeof(bindname));
    247  1.19    itojun 	strlcat(bindname, rhs, sizeof(bindname));
    248   1.6     lukem 
    249  1.18    itojun 	/* rhs_list is no longer needed, since we're done with rhs. */
    250   1.6     lukem 	if (rhs_list)
    251   1.6     lukem 		hesiod_free_list(context, rhs_list);
    252   1.6     lukem 
    253  1.18    itojun 	/* Make a copy of the result and return it to the caller. */
    254   1.6     lukem 	ret = strdup(bindname);
    255  1.17      groo 	if (ret == NULL)
    256   1.6     lukem 		errno = ENOMEM;
    257   1.6     lukem 	return ret;
    258   1.6     lukem }
    259   1.6     lukem 
    260   1.6     lukem /*
    261   1.6     lukem  * hesiod_resolve --
    262   1.6     lukem  *	Given a hesiod name and type, return an array of strings returned
    263   1.6     lukem  *	by the resolver.
    264   1.6     lukem  */
    265   1.6     lukem char **
    266  1.27      matt hesiod_resolve(void *context, const char *name, const char *type)
    267   1.6     lukem {
    268   1.6     lukem 	struct hesiod_p	*ctx = (struct hesiod_p *) context;
    269   1.6     lukem 	char		*bindname, **retvec;
    270   1.2     lukem 
    271  1.10     lukem 	_DIAGASSERT(context != NULL);
    272  1.10     lukem 	_DIAGASSERT(name != NULL);
    273  1.10     lukem 	_DIAGASSERT(type != NULL);
    274  1.10     lukem 
    275   1.6     lukem 	bindname = hesiod_to_bind(context, name, type);
    276   1.6     lukem 	if (!bindname)
    277   1.6     lukem 		return NULL;
    278   1.2     lukem 
    279   1.6     lukem 	retvec = get_txt_records(ctx->classes[0], bindname);
    280   1.6     lukem 	if (retvec == NULL && errno == ENOENT && ctx->classes[1])
    281   1.6     lukem 		retvec = get_txt_records(ctx->classes[1], bindname);
    282   1.2     lukem 
    283   1.6     lukem 	free(bindname);
    284   1.6     lukem 	return retvec;
    285   1.6     lukem }
    286   1.2     lukem 
    287   1.6     lukem /*ARGSUSED*/
    288   1.6     lukem void
    289  1.27      matt hesiod_free_list(void *context, char **list)
    290   1.6     lukem {
    291   1.6     lukem 	char  **p;
    292   1.2     lukem 
    293  1.10     lukem 	_DIAGASSERT(context != NULL);
    294  1.10     lukem 
    295   1.7     lukem 	if (list == NULL)
    296   1.7     lukem 		return;
    297   1.6     lukem 	for (p = list; *p; p++)
    298   1.6     lukem 		free(*p);
    299   1.6     lukem 	free(list);
    300   1.2     lukem }
    301   1.2     lukem 
    302   1.6     lukem 
    303   1.6     lukem /* read_config_file --
    304   1.6     lukem  *	Parse the /etc/hesiod.conf file.  Returns 0 on success,
    305   1.6     lukem  *	-1 on failure.  On failure, it might leave values in ctx->lhs
    306   1.6     lukem  *	or ctx->rhs which need to be freed by the caller.
    307   1.2     lukem  */
    308   1.6     lukem static int
    309  1.27      matt read_config_file(struct hesiod_p *ctx, const char *filename)
    310   1.6     lukem {
    311  1.26  christos 	char	*buf, *key, *data, *p, **which;
    312   1.6     lukem 	int	 n;
    313   1.6     lukem 	FILE	*fp;
    314   1.2     lukem 
    315  1.10     lukem 	_DIAGASSERT(ctx != NULL);
    316  1.10     lukem 	_DIAGASSERT(filename != NULL);
    317  1.10     lukem 
    318  1.18    itojun 	/* Set default query classes. */
    319   1.8     lukem 	ctx->classes[0] = C_IN;
    320   1.8     lukem 	ctx->classes[1] = C_HS;
    321   1.6     lukem 
    322  1.18    itojun 	/* Try to open the configuration file. */
    323   1.6     lukem 	fp = fopen(filename, "r");
    324   1.6     lukem 	if (!fp) {
    325   1.6     lukem 		/* Use compiled in default domain names. */
    326   1.6     lukem 		ctx->lhs = strdup(DEF_LHS);
    327   1.6     lukem 		ctx->rhs = strdup(DEF_RHS);
    328   1.6     lukem 		if (ctx->lhs && ctx->rhs)
    329   1.6     lukem 			return 0;
    330   1.6     lukem 		else {
    331   1.6     lukem 			errno = ENOMEM;
    332   1.6     lukem 			return -1;
    333   1.6     lukem 		}
    334   1.6     lukem 	}
    335   1.6     lukem 	ctx->lhs = NULL;
    336   1.6     lukem 	ctx->rhs = NULL;
    337  1.26  christos 	for (; (buf = fparseln(fp, NULL, NULL, NULL, FPARSELN_UNESCALL))
    338  1.26  christos 	    != NULL; free(buf)) {
    339   1.6     lukem 		p = buf;
    340   1.6     lukem 		while (*p == ' ' || *p == '\t')
    341   1.6     lukem 			p++;
    342   1.6     lukem 		key = p;
    343  1.17      groo 		while (*p != ' ' && *p != '\t' && *p != '=' && *p)
    344   1.6     lukem 			p++;
    345  1.17      groo 
    346  1.17      groo 		if (*p == '\0')
    347  1.17      groo 			continue;
    348  1.17      groo 
    349   1.6     lukem 		*p++ = 0;
    350   1.6     lukem 
    351  1.14     itohy 		while (isspace((u_char) *p) || *p == '=')
    352   1.6     lukem 			p++;
    353  1.17      groo 
    354  1.17      groo 		if (*p == '\0')
    355  1.17      groo 			continue;
    356  1.17      groo 
    357   1.6     lukem 		data = p;
    358  1.17      groo 		while (!isspace((u_char) *p) && *p)
    359   1.6     lukem 			p++;
    360  1.17      groo 
    361   1.6     lukem 		*p = 0;
    362   1.6     lukem 
    363   1.6     lukem 		if (strcasecmp(key, "lhs") == 0 ||
    364   1.6     lukem 		    strcasecmp(key, "rhs") == 0) {
    365   1.6     lukem 			which = (strcasecmp(key, "lhs") == 0)
    366   1.6     lukem 			    ? &ctx->lhs : &ctx->rhs;
    367   1.6     lukem 			*which = strdup(data);
    368   1.6     lukem 			if (!*which) {
    369   1.6     lukem 				errno = ENOMEM;
    370  1.26  christos 				free(buf);
    371  1.25       wiz 				(void)fclose(fp);
    372   1.6     lukem 				return -1;
    373   1.6     lukem 			}
    374   1.6     lukem 		} else {
    375   1.6     lukem 			if (strcasecmp(key, "classes") == 0) {
    376   1.6     lukem 				n = 0;
    377   1.6     lukem 				while (*data && n < 2) {
    378   1.6     lukem 					p = data;
    379   1.6     lukem 					while (*p && *p != ',')
    380   1.6     lukem 						p++;
    381   1.6     lukem 					if (*p)
    382   1.6     lukem 						*p++ = 0;
    383   1.6     lukem 					if (strcasecmp(data, "IN") == 0)
    384   1.6     lukem 						ctx->classes[n++] = C_IN;
    385   1.6     lukem 					else
    386   1.6     lukem 						if (strcasecmp(data, "HS") == 0)
    387   1.6     lukem 							ctx->classes[n++] =
    388   1.6     lukem 							    C_HS;
    389   1.6     lukem 					data = p;
    390   1.6     lukem 				}
    391   1.6     lukem 				while (n < 2)
    392   1.6     lukem 					ctx->classes[n++] = 0;
    393   1.6     lukem 			}
    394   1.6     lukem 		}
    395   1.6     lukem 	}
    396   1.6     lukem 	fclose(fp);
    397   1.2     lukem 
    398   1.6     lukem 	if (!ctx->rhs || ctx->classes[0] == 0 ||
    399   1.6     lukem 	    ctx->classes[0] == ctx->classes[1]) {
    400   1.6     lukem 		errno = ENOEXEC;
    401   1.6     lukem 		return -1;
    402   1.2     lukem 	}
    403   1.6     lukem 	return 0;
    404   1.6     lukem }
    405   1.2     lukem 
    406   1.6     lukem /*
    407   1.6     lukem  * get_txt_records --
    408   1.6     lukem  *	Given a DNS class and a DNS name, do a lookup for TXT records, and
    409   1.6     lukem  *	return a list of them.
    410   1.6     lukem  */
    411   1.6     lukem static char **
    412  1.27      matt get_txt_records(int qclass, const char *name)
    413   1.6     lukem {
    414   1.6     lukem 	HEADER		*hp;
    415   1.6     lukem 	unsigned char	 qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
    416   1.6     lukem 	char		*dst, **list;
    417   1.6     lukem 	int		 ancount, qdcount, i, j, n, skip, type, class, len;
    418  1.21  christos 	res_state	 res = __res_get_state();
    419   1.6     lukem 
    420  1.22  christos 	if (res == NULL)
    421  1.22  christos 		return NULL;
    422  1.22  christos 
    423  1.10     lukem 	_DIAGASSERT(name != NULL);
    424  1.10     lukem 
    425  1.18    itojun 	/* Construct the query. */
    426  1.21  christos 	n = res_nmkquery(res, QUERY, name, qclass, T_TXT, NULL, 0,
    427   1.2     lukem 	    NULL, qbuf, PACKETSZ);
    428  1.13   ghudson 	if (n < 0) {
    429  1.13   ghudson 		errno = EMSGSIZE;
    430  1.21  christos 		__res_put_state(res);
    431   1.6     lukem 		return NULL;
    432  1.13   ghudson 	}
    433   1.6     lukem 
    434  1.18    itojun 	/* Send the query. */
    435  1.21  christos 	n = res_nsend(res, qbuf, n, abuf, MAX_HESRESP);
    436  1.21  christos 	__res_put_state(res);
    437   1.2     lukem 	if (n < 0) {
    438   1.6     lukem 		errno = ECONNREFUSED;
    439   1.6     lukem 		return NULL;
    440   1.6     lukem 	}
    441  1.18    itojun 	/* Parse the header of the result. */
    442   1.6     lukem 	hp = (HEADER *) (void *) abuf;
    443   1.6     lukem 	ancount = ntohs(hp->ancount);
    444   1.6     lukem 	qdcount = ntohs(hp->qdcount);
    445   1.6     lukem 	p = abuf + sizeof(HEADER);
    446   1.6     lukem 	eom = abuf + n;
    447   1.6     lukem 
    448  1.18    itojun 	/*
    449  1.18    itojun 	 * Skip questions, trying to get to the answer section
    450  1.18    itojun 	 * which follows.
    451  1.18    itojun 	 */
    452   1.6     lukem 	for (i = 0; i < qdcount; i++) {
    453   1.6     lukem 		skip = dn_skipname(p, eom);
    454   1.6     lukem 		if (skip < 0 || p + skip + QFIXEDSZ > eom) {
    455   1.6     lukem 			errno = EMSGSIZE;
    456   1.6     lukem 			return NULL;
    457   1.6     lukem 		}
    458   1.6     lukem 		p += skip + QFIXEDSZ;
    459   1.2     lukem 	}
    460   1.2     lukem 
    461  1.18    itojun 	/* Allocate space for the text record answers. */
    462   1.6     lukem 	list = malloc((ancount + 1) * sizeof(char *));
    463   1.6     lukem 	if (!list) {
    464   1.6     lukem 		errno = ENOMEM;
    465   1.6     lukem 		return NULL;
    466   1.6     lukem 	}
    467  1.18    itojun 	/* Parse the answers. */
    468   1.6     lukem 	j = 0;
    469   1.6     lukem 	for (i = 0; i < ancount; i++) {
    470   1.6     lukem 		/* Parse the header of this answer. */
    471   1.6     lukem 		skip = dn_skipname(p, eom);
    472   1.6     lukem 		if (skip < 0 || p + skip + 10 > eom)
    473   1.6     lukem 			break;
    474   1.6     lukem 		type = p[skip + 0] << 8 | p[skip + 1];
    475   1.6     lukem 		class = p[skip + 2] << 8 | p[skip + 3];
    476   1.6     lukem 		len = p[skip + 8] << 8 | p[skip + 9];
    477   1.6     lukem 		p += skip + 10;
    478   1.6     lukem 		if (p + len > eom) {
    479   1.6     lukem 			errno = EMSGSIZE;
    480   1.6     lukem 			break;
    481   1.6     lukem 		}
    482   1.6     lukem 		/* Skip entries of the wrong class and type. */
    483   1.6     lukem 		if (class != qclass || type != T_TXT) {
    484   1.6     lukem 			p += len;
    485   1.6     lukem 			continue;
    486   1.6     lukem 		}
    487   1.6     lukem 		/* Allocate space for this answer. */
    488   1.6     lukem 		list[j] = malloc((size_t)len);
    489   1.6     lukem 		if (!list[j]) {
    490   1.6     lukem 			errno = ENOMEM;
    491   1.6     lukem 			break;
    492   1.6     lukem 		}
    493   1.6     lukem 		dst = list[j++];
    494   1.2     lukem 
    495   1.6     lukem 		/* Copy answer data into the allocated area. */
    496   1.6     lukem 		eor = p + len;
    497   1.6     lukem 		while (p < eor) {
    498   1.6     lukem 			n = (unsigned char) *p++;
    499   1.6     lukem 			if (p + n > eor) {
    500   1.6     lukem 				errno = EMSGSIZE;
    501   1.6     lukem 				break;
    502   1.6     lukem 			}
    503   1.6     lukem 			memcpy(dst, p, (size_t)n);
    504   1.6     lukem 			p += n;
    505   1.6     lukem 			dst += n;
    506   1.6     lukem 		}
    507   1.6     lukem 		if (p < eor) {
    508   1.6     lukem 			errno = EMSGSIZE;
    509   1.6     lukem 			break;
    510   1.6     lukem 		}
    511   1.6     lukem 		*dst = 0;
    512   1.6     lukem 	}
    513   1.2     lukem 
    514  1.18    itojun 	/*
    515  1.18    itojun 	 * If we didn't terminate the loop normally, something
    516  1.18    itojun 	 * went wrong.
    517  1.18    itojun 	 */
    518   1.6     lukem 	if (i < ancount) {
    519   1.6     lukem 		for (i = 0; i < j; i++)
    520   1.6     lukem 			free(list[i]);
    521   1.6     lukem 		free(list);
    522   1.6     lukem 		return NULL;
    523   1.6     lukem 	}
    524   1.6     lukem 	if (j == 0) {
    525   1.6     lukem 		errno = ENOENT;
    526   1.6     lukem 		free(list);
    527   1.6     lukem 		return NULL;
    528   1.2     lukem 	}
    529   1.6     lukem 	list[j] = NULL;
    530   1.6     lukem 	return list;
    531   1.6     lukem }
    532   1.2     lukem 
    533  1.18    itojun /*
    534  1.18    itojun  * COMPATIBILITY FUNCTIONS
    535  1.18    itojun  */
    536   1.6     lukem 
    537   1.6     lukem static int	  inited = 0;
    538   1.6     lukem static void	 *context;
    539   1.6     lukem static int	  errval = HES_ER_UNINIT;
    540   1.2     lukem 
    541   1.2     lukem int
    542  1.27      matt hes_init(void)
    543   1.2     lukem {
    544   1.6     lukem 	init_context();
    545   1.6     lukem 	return errval;
    546   1.2     lukem }
    547   1.2     lukem 
    548   1.2     lukem char *
    549  1.27      matt hes_to_bind(const char *name, const char *type)
    550   1.2     lukem {
    551   1.6     lukem 	static	char	*bindname;
    552  1.10     lukem 
    553  1.10     lukem 	_DIAGASSERT(name != NULL);
    554  1.10     lukem 	_DIAGASSERT(type != NULL);
    555  1.10     lukem 
    556   1.6     lukem 	if (init_context() < 0)
    557   1.6     lukem 		return NULL;
    558   1.6     lukem 	if (bindname)
    559   1.6     lukem 		free(bindname);
    560   1.6     lukem 	bindname = hesiod_to_bind(context, name, type);
    561   1.6     lukem 	if (!bindname)
    562   1.6     lukem 		translate_errors();
    563   1.6     lukem 	return bindname;
    564   1.2     lukem }
    565   1.2     lukem 
    566   1.2     lukem char **
    567  1.27      matt hes_resolve(const char *name, const char *type)
    568   1.2     lukem {
    569   1.6     lukem 	static char	**list;
    570   1.6     lukem 
    571  1.10     lukem 	_DIAGASSERT(name != NULL);
    572  1.10     lukem 	_DIAGASSERT(type != NULL);
    573  1.10     lukem 
    574   1.6     lukem 	if (init_context() < 0)
    575   1.6     lukem 		return NULL;
    576   1.6     lukem 
    577   1.6     lukem 	/*
    578   1.6     lukem 	 * In the old Hesiod interface, the caller was responsible for
    579   1.6     lukem 	 * freeing the returned strings but not the vector of strings itself.
    580   1.6     lukem 	 */
    581   1.6     lukem 	if (list)
    582   1.6     lukem 		free(list);
    583   1.6     lukem 
    584   1.6     lukem 	list = hesiod_resolve(context, name, type);
    585   1.6     lukem 	if (!list)
    586   1.6     lukem 		translate_errors();
    587   1.6     lukem 	return list;
    588   1.2     lukem }
    589   1.2     lukem 
    590   1.2     lukem int
    591  1.27      matt hes_error(void)
    592   1.2     lukem {
    593   1.6     lukem 	return errval;
    594   1.2     lukem }
    595   1.2     lukem 
    596   1.2     lukem void
    597  1.27      matt hes_free(char **hp)
    598   1.2     lukem {
    599   1.7     lukem 	hesiod_free_list(context, hp);
    600   1.6     lukem }
    601   1.6     lukem 
    602   1.6     lukem static int
    603  1.27      matt init_context(void)
    604   1.6     lukem {
    605   1.6     lukem 	if (!inited) {
    606   1.6     lukem 		inited = 1;
    607   1.6     lukem 		if (hesiod_init(&context) < 0) {
    608   1.6     lukem 			errval = HES_ER_CONFIG;
    609   1.6     lukem 			return -1;
    610   1.6     lukem 		}
    611   1.6     lukem 		errval = HES_ER_OK;
    612   1.6     lukem 	}
    613   1.6     lukem 	return 0;
    614   1.6     lukem }
    615   1.6     lukem 
    616   1.6     lukem static void
    617  1.27      matt translate_errors(void)
    618   1.6     lukem {
    619   1.6     lukem 	switch (errno) {
    620   1.6     lukem 	case ENOENT:
    621   1.6     lukem 		errval = HES_ER_NOTFOUND;
    622   1.6     lukem 		break;
    623   1.6     lukem 	case ECONNREFUSED:
    624   1.6     lukem 	case EMSGSIZE:
    625   1.6     lukem 		errval = HES_ER_NET;
    626   1.6     lukem 		break;
    627   1.6     lukem 	default:
    628   1.6     lukem 		/* Not a good match, but the best we can do. */
    629   1.6     lukem 		errval = HES_ER_CONFIG;
    630   1.6     lukem 		break;
    631   1.6     lukem 	}
    632   1.2     lukem }
    633