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