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