xref: /minix3/lib/libc/net/sethostent.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1985, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
342fe8fb19SBen Gras #if 0
352fe8fb19SBen Gras static char sccsid[] = "@(#)sethostent.c	8.1 (Berkeley) 6/4/93";
362fe8fb19SBen Gras static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp ";
372fe8fb19SBen Gras #else
38*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $");
392fe8fb19SBen Gras #endif
402fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
412fe8fb19SBen Gras 
422fe8fb19SBen Gras #include "namespace.h"
432fe8fb19SBen Gras #include <sys/param.h>
442fe8fb19SBen Gras #include <netinet/in.h>
452fe8fb19SBen Gras #include <arpa/nameser.h>
4684d9c625SLionel Sambuc #include <arpa/inet.h>
4784d9c625SLionel Sambuc #include <assert.h>
4884d9c625SLionel Sambuc #include <string.h>
4984d9c625SLionel Sambuc #include <nsswitch.h>
502fe8fb19SBen Gras #include <netdb.h>
512fe8fb19SBen Gras #include <resolv.h>
5284d9c625SLionel Sambuc #include <errno.h>
5384d9c625SLionel Sambuc #include <stdlib.h>
5484d9c625SLionel Sambuc 
5584d9c625SLionel Sambuc #include "hostent.h"
562fe8fb19SBen Gras 
572fe8fb19SBen Gras #ifdef __weak_alias
582fe8fb19SBen Gras __weak_alias(sethostent,_sethostent)
592fe8fb19SBen Gras __weak_alias(endhostent,_endhostent)
602fe8fb19SBen Gras #endif
612fe8fb19SBen Gras 
622fe8fb19SBen Gras #ifndef _REENTRANT
63f14fb602SLionel Sambuc void	res_close(void);
642fe8fb19SBen Gras #endif
6584d9c625SLionel Sambuc 
6684d9c625SLionel Sambuc static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
672fe8fb19SBen Gras 
682fe8fb19SBen Gras void
692fe8fb19SBen Gras /*ARGSUSED*/
sethostent(int stayopen)70f14fb602SLionel Sambuc sethostent(int stayopen)
712fe8fb19SBen Gras {
722fe8fb19SBen Gras #ifndef _REENTRANT
732fe8fb19SBen Gras 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
742fe8fb19SBen Gras 		return;
752fe8fb19SBen Gras 	if (stayopen)
762fe8fb19SBen Gras 		_res.options |= RES_STAYOPEN | RES_USEVC;
772fe8fb19SBen Gras #endif
7884d9c625SLionel Sambuc 	sethostent_r(&_h_file);
792fe8fb19SBen Gras }
802fe8fb19SBen Gras 
812fe8fb19SBen Gras void
endhostent(void)82f14fb602SLionel Sambuc endhostent(void)
832fe8fb19SBen Gras {
842fe8fb19SBen Gras #ifndef _REENTRANT
852fe8fb19SBen Gras 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
862fe8fb19SBen Gras 	res_close();
872fe8fb19SBen Gras #endif
8884d9c625SLionel Sambuc 	endhostent_r(&_h_file);
8984d9c625SLionel Sambuc }
9084d9c625SLionel Sambuc static const char *_h_hosts = _PATH_HOSTS;
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc void
_hf_sethostsfile(const char * f)9384d9c625SLionel Sambuc _hf_sethostsfile(const char *f) {
9484d9c625SLionel Sambuc 	_h_hosts = f;
9584d9c625SLionel Sambuc }
9684d9c625SLionel Sambuc 
9784d9c625SLionel Sambuc void
sethostent_r(FILE ** hf)9884d9c625SLionel Sambuc sethostent_r(FILE **hf)
9984d9c625SLionel Sambuc {
10084d9c625SLionel Sambuc 	if (!*hf)
10184d9c625SLionel Sambuc 		*hf = fopen(_h_hosts, "re");
10284d9c625SLionel Sambuc 	else
10384d9c625SLionel Sambuc 		rewind(*hf);
10484d9c625SLionel Sambuc }
10584d9c625SLionel Sambuc 
10684d9c625SLionel Sambuc void
endhostent_r(FILE ** hf)10784d9c625SLionel Sambuc endhostent_r(FILE **hf)
10884d9c625SLionel Sambuc {
10984d9c625SLionel Sambuc 	if (*hf) {
11084d9c625SLionel Sambuc 		(void)fclose(*hf);
11184d9c625SLionel Sambuc 		*hf = NULL;
11284d9c625SLionel Sambuc 	}
11384d9c625SLionel Sambuc }
11484d9c625SLionel Sambuc 
11584d9c625SLionel Sambuc /*ARGSUSED*/
11684d9c625SLionel Sambuc int
_hf_gethtbyname(void * rv,void * cb_data,va_list ap)11784d9c625SLionel Sambuc _hf_gethtbyname(void *rv, void *cb_data, va_list ap)
11884d9c625SLionel Sambuc {
11984d9c625SLionel Sambuc 	struct hostent *hp;
12084d9c625SLionel Sambuc 	const char *name;
12184d9c625SLionel Sambuc 	int af;
12284d9c625SLionel Sambuc 	struct getnamaddr *info = rv;
12384d9c625SLionel Sambuc 
12484d9c625SLionel Sambuc 	_DIAGASSERT(rv != NULL);
12584d9c625SLionel Sambuc 
12684d9c625SLionel Sambuc 	name = va_arg(ap, char *);
12784d9c625SLionel Sambuc 	/* NOSTRICT skip string len */(void)va_arg(ap, int);
12884d9c625SLionel Sambuc 	af = va_arg(ap, int);
12984d9c625SLionel Sambuc 
13084d9c625SLionel Sambuc #if 0
13184d9c625SLionel Sambuc 	{
13284d9c625SLionel Sambuc 		res_state res = __res_get_state();
13384d9c625SLionel Sambuc 		if (res == NULL)
13484d9c625SLionel Sambuc 			return NS_NOTFOUND;
13584d9c625SLionel Sambuc 		if (res->options & RES_USE_INET6)
13684d9c625SLionel Sambuc 			hp = _hf_gethtbyname2(name, AF_INET6, info);
13784d9c625SLionel Sambuc 		else
13884d9c625SLionel Sambuc 			hp = NULL;
13984d9c625SLionel Sambuc 		if (hp == NULL)
14084d9c625SLionel Sambuc 			hp = _hf_gethtbyname2(name, AF_INET, info);
14184d9c625SLionel Sambuc 		__res_put_state(res);
14284d9c625SLionel Sambuc 	}
14384d9c625SLionel Sambuc #else
14484d9c625SLionel Sambuc 	hp = _hf_gethtbyname2(name, af, info);
14584d9c625SLionel Sambuc #endif
14684d9c625SLionel Sambuc 	if (hp == NULL) {
14784d9c625SLionel Sambuc 		*info->he = HOST_NOT_FOUND;
14884d9c625SLionel Sambuc 		return NS_NOTFOUND;
14984d9c625SLionel Sambuc 	}
15084d9c625SLionel Sambuc 	return NS_SUCCESS;
15184d9c625SLionel Sambuc }
15284d9c625SLionel Sambuc 
15384d9c625SLionel Sambuc struct hostent *
_hf_gethtbyname2(const char * name,int af,struct getnamaddr * info)15484d9c625SLionel Sambuc _hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
15584d9c625SLionel Sambuc {
15684d9c625SLionel Sambuc 	struct hostent *hp, hent;
15784d9c625SLionel Sambuc 	char *buf, *ptr;
15884d9c625SLionel Sambuc 	size_t len, anum, num, i;
15984d9c625SLionel Sambuc 	FILE *hf;
16084d9c625SLionel Sambuc 	char *aliases[MAXALIASES];
16184d9c625SLionel Sambuc 	char *addr_ptrs[MAXADDRS];
16284d9c625SLionel Sambuc 
16384d9c625SLionel Sambuc 	_DIAGASSERT(name != NULL);
16484d9c625SLionel Sambuc 
16584d9c625SLionel Sambuc 	hf = NULL;
16684d9c625SLionel Sambuc 	sethostent_r(&hf);
16784d9c625SLionel Sambuc 	if (hf == NULL) {
16884d9c625SLionel Sambuc 		errno = EINVAL;
16984d9c625SLionel Sambuc 		*info->he = NETDB_INTERNAL;
17084d9c625SLionel Sambuc 		return NULL;
17184d9c625SLionel Sambuc 	}
17284d9c625SLionel Sambuc 
17384d9c625SLionel Sambuc 	if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
17484d9c625SLionel Sambuc 		*info->he = NETDB_INTERNAL;
17584d9c625SLionel Sambuc 		return NULL;
17684d9c625SLionel Sambuc 	}
17784d9c625SLionel Sambuc 
17884d9c625SLionel Sambuc 	anum = 0;		/* XXX: gcc */
17984d9c625SLionel Sambuc 	hent.h_name = NULL;	/* XXX: gcc */
18084d9c625SLionel Sambuc 	hent.h_addrtype = 0;	/* XXX: gcc */
18184d9c625SLionel Sambuc 	hent.h_length = 0;	/* XXX: gcc */
18284d9c625SLionel Sambuc 
18384d9c625SLionel Sambuc 	for (num = 0; num < MAXADDRS;) {
18484d9c625SLionel Sambuc 		info->hp->h_addrtype = af;
18584d9c625SLionel Sambuc 		info->hp->h_length = 0;
18684d9c625SLionel Sambuc 
18784d9c625SLionel Sambuc 		hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
18884d9c625SLionel Sambuc 		    info->he);
18984d9c625SLionel Sambuc 		if (hp == NULL)
19084d9c625SLionel Sambuc 			break;
19184d9c625SLionel Sambuc 
19284d9c625SLionel Sambuc 		if (strcasecmp(hp->h_name, name) != 0) {
19384d9c625SLionel Sambuc 			char **cp;
19484d9c625SLionel Sambuc 			for (cp = hp->h_aliases; *cp != NULL; cp++)
19584d9c625SLionel Sambuc 				if (strcasecmp(*cp, name) == 0)
19684d9c625SLionel Sambuc 					break;
19784d9c625SLionel Sambuc 			if (*cp == NULL) continue;
19884d9c625SLionel Sambuc 		}
19984d9c625SLionel Sambuc 
20084d9c625SLionel Sambuc 		if (num == 0) {
20184d9c625SLionel Sambuc 			hent.h_addrtype = af = hp->h_addrtype;
20284d9c625SLionel Sambuc 			hent.h_length = hp->h_length;
20384d9c625SLionel Sambuc 
20484d9c625SLionel Sambuc 			HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
20584d9c625SLionel Sambuc 			for (anum = 0; hp->h_aliases[anum]; anum++) {
20684d9c625SLionel Sambuc 				if (anum >= __arraycount(aliases))
20784d9c625SLionel Sambuc 					goto nospc;
20884d9c625SLionel Sambuc 				HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
20984d9c625SLionel Sambuc 				    ptr, len);
21084d9c625SLionel Sambuc 			}
21184d9c625SLionel Sambuc 			ptr = (void *)ALIGN(ptr);
21284d9c625SLionel Sambuc 			if ((size_t)(ptr - buf) >= info->buflen)
21384d9c625SLionel Sambuc 				goto nospc;
21484d9c625SLionel Sambuc 		}
21584d9c625SLionel Sambuc 
21684d9c625SLionel Sambuc 		if (num >= __arraycount(addr_ptrs))
21784d9c625SLionel Sambuc 			goto nospc;
21884d9c625SLionel Sambuc 		HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
21984d9c625SLionel Sambuc 		    len);
22084d9c625SLionel Sambuc 		num++;
22184d9c625SLionel Sambuc 	}
22284d9c625SLionel Sambuc 	endhostent_r(&hf);
22384d9c625SLionel Sambuc 
22484d9c625SLionel Sambuc 	if (num == 0) {
22584d9c625SLionel Sambuc 		*info->he = HOST_NOT_FOUND;
226*0a6a1f1dSLionel Sambuc 		free(buf);
22784d9c625SLionel Sambuc 		return NULL;
22884d9c625SLionel Sambuc 	}
22984d9c625SLionel Sambuc 
23084d9c625SLionel Sambuc 	hp = info->hp;
23184d9c625SLionel Sambuc 	ptr = info->buf;
23284d9c625SLionel Sambuc 	len = info->buflen;
23384d9c625SLionel Sambuc 
23484d9c625SLionel Sambuc 	hp->h_addrtype = hent.h_addrtype;
23584d9c625SLionel Sambuc 	hp->h_length = hent.h_length;
23684d9c625SLionel Sambuc 
23784d9c625SLionel Sambuc 	HENT_ARRAY(hp->h_aliases, anum, ptr, len);
23884d9c625SLionel Sambuc 	HENT_ARRAY(hp->h_addr_list, num, ptr, len);
23984d9c625SLionel Sambuc 
24084d9c625SLionel Sambuc 	for (i = 0; i < num; i++)
24184d9c625SLionel Sambuc 		HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
24284d9c625SLionel Sambuc 		    len);
24384d9c625SLionel Sambuc 	hp->h_addr_list[num] = NULL;
24484d9c625SLionel Sambuc 
24584d9c625SLionel Sambuc 	HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
24684d9c625SLionel Sambuc 
24784d9c625SLionel Sambuc 	for (i = 0; i < anum; i++)
24884d9c625SLionel Sambuc 		HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
24984d9c625SLionel Sambuc 	hp->h_aliases[anum] = NULL;
25084d9c625SLionel Sambuc 
251*0a6a1f1dSLionel Sambuc 	free(buf);
25284d9c625SLionel Sambuc 	return hp;
25384d9c625SLionel Sambuc nospc:
25484d9c625SLionel Sambuc 	*info->he = NETDB_INTERNAL;
255*0a6a1f1dSLionel Sambuc 	free(buf);
25684d9c625SLionel Sambuc 	errno = ENOSPC;
25784d9c625SLionel Sambuc 	return NULL;
25884d9c625SLionel Sambuc }
25984d9c625SLionel Sambuc 
26084d9c625SLionel Sambuc /*ARGSUSED*/
26184d9c625SLionel Sambuc int
_hf_gethtbyaddr(void * rv,void * cb_data,va_list ap)26284d9c625SLionel Sambuc _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
26384d9c625SLionel Sambuc {
26484d9c625SLionel Sambuc 	struct hostent *hp;
26584d9c625SLionel Sambuc 	const unsigned char *addr;
26684d9c625SLionel Sambuc 	struct getnamaddr *info = rv;
26784d9c625SLionel Sambuc 	FILE *hf;
26884d9c625SLionel Sambuc 
26984d9c625SLionel Sambuc 	_DIAGASSERT(rv != NULL);
27084d9c625SLionel Sambuc 
27184d9c625SLionel Sambuc 	addr = va_arg(ap, unsigned char *);
27284d9c625SLionel Sambuc 	info->hp->h_length = va_arg(ap, int);
27384d9c625SLionel Sambuc 	info->hp->h_addrtype = va_arg(ap, int);
27484d9c625SLionel Sambuc 
27584d9c625SLionel Sambuc 	hf = NULL;
27684d9c625SLionel Sambuc 	sethostent_r(&hf);
27784d9c625SLionel Sambuc 	if (hf == NULL) {
27884d9c625SLionel Sambuc 		*info->he = NETDB_INTERNAL;
27984d9c625SLionel Sambuc 		return NS_UNAVAIL;
28084d9c625SLionel Sambuc 	}
28184d9c625SLionel Sambuc 	while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
28284d9c625SLionel Sambuc 	    info->he)) != NULL)
28384d9c625SLionel Sambuc 		if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
28484d9c625SLionel Sambuc 			break;
28584d9c625SLionel Sambuc 	endhostent_r(&hf);
28684d9c625SLionel Sambuc 
28784d9c625SLionel Sambuc 	if (hp == NULL) {
28884d9c625SLionel Sambuc 		*info->he = HOST_NOT_FOUND;
28984d9c625SLionel Sambuc 		return NS_NOTFOUND;
29084d9c625SLionel Sambuc 	}
29184d9c625SLionel Sambuc 	return NS_SUCCESS;
2922fe8fb19SBen Gras }
293