Home | History | Annotate | Line # | Download | only in net
sethostent.c revision 1.17.2.1
      1  1.17.2.1       tls /*	$NetBSD: sethostent.c,v 1.17.2.1 2014/08/20 00:02:15 tls Exp $	*/
      2       1.4       cgd 
      3       1.1       cgd /*
      4       1.4       cgd  * Copyright (c) 1985, 1993
      5       1.4       cgd  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15      1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  */
     31       1.1       cgd 
     32       1.7  christos #include <sys/cdefs.h>
     33       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34       1.4       cgd #if 0
     35       1.4       cgd static char sccsid[] = "@(#)sethostent.c	8.1 (Berkeley) 6/4/93";
     36       1.9     perry static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp ";
     37       1.4       cgd #else
     38  1.17.2.1       tls __RCSID("$NetBSD: sethostent.c,v 1.17.2.1 2014/08/20 00:02:15 tls Exp $");
     39       1.4       cgd #endif
     40       1.1       cgd #endif /* LIBC_SCCS and not lint */
     41       1.1       cgd 
     42      1.10    kleink #include "namespace.h"
     43       1.1       cgd #include <sys/param.h>
     44       1.1       cgd #include <netinet/in.h>
     45       1.1       cgd #include <arpa/nameser.h>
     46  1.17.2.1       tls #include <arpa/inet.h>
     47  1.17.2.1       tls #include <assert.h>
     48  1.17.2.1       tls #include <string.h>
     49  1.17.2.1       tls #include <nsswitch.h>
     50       1.1       cgd #include <netdb.h>
     51       1.1       cgd #include <resolv.h>
     52  1.17.2.1       tls #include <errno.h>
     53  1.17.2.1       tls #include <stdlib.h>
     54  1.17.2.1       tls 
     55  1.17.2.1       tls #include "hostent.h"
     56      1.10    kleink 
     57      1.10    kleink #ifdef __weak_alias
     58      1.11   mycroft __weak_alias(sethostent,_sethostent)
     59      1.11   mycroft __weak_alias(endhostent,_endhostent)
     60      1.10    kleink #endif
     61       1.1       cgd 
     62      1.13  christos #ifndef _REENTRANT
     63      1.17      matt void	res_close(void);
     64      1.13  christos #endif
     65  1.17.2.1       tls 
     66  1.17.2.1       tls static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
     67       1.6       mrg 
     68       1.1       cgd void
     69      1.13  christos /*ARGSUSED*/
     70      1.17      matt sethostent(int stayopen)
     71       1.1       cgd {
     72      1.13  christos #ifndef _REENTRANT
     73       1.5       mrg 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
     74       1.5       mrg 		return;
     75       1.1       cgd 	if (stayopen)
     76       1.1       cgd 		_res.options |= RES_STAYOPEN | RES_USEVC;
     77      1.13  christos #endif
     78  1.17.2.1       tls 	sethostent_r(&_h_file);
     79       1.1       cgd }
     80       1.1       cgd 
     81       1.1       cgd void
     82      1.17      matt endhostent(void)
     83       1.1       cgd {
     84      1.13  christos #ifndef _REENTRANT
     85       1.1       cgd 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
     86       1.6       mrg 	res_close();
     87      1.13  christos #endif
     88  1.17.2.1       tls 	endhostent_r(&_h_file);
     89  1.17.2.1       tls }
     90  1.17.2.1       tls static const char *_h_hosts = _PATH_HOSTS;
     91  1.17.2.1       tls 
     92  1.17.2.1       tls void
     93  1.17.2.1       tls _hf_sethostsfile(const char *f) {
     94  1.17.2.1       tls 	_h_hosts = f;
     95  1.17.2.1       tls }
     96  1.17.2.1       tls 
     97  1.17.2.1       tls void
     98  1.17.2.1       tls sethostent_r(FILE **hf)
     99  1.17.2.1       tls {
    100  1.17.2.1       tls 	if (!*hf)
    101  1.17.2.1       tls 		*hf = fopen(_h_hosts, "re");
    102  1.17.2.1       tls 	else
    103  1.17.2.1       tls 		rewind(*hf);
    104  1.17.2.1       tls }
    105  1.17.2.1       tls 
    106  1.17.2.1       tls void
    107  1.17.2.1       tls endhostent_r(FILE **hf)
    108  1.17.2.1       tls {
    109  1.17.2.1       tls 	if (*hf) {
    110  1.17.2.1       tls 		(void)fclose(*hf);
    111  1.17.2.1       tls 		*hf = NULL;
    112  1.17.2.1       tls 	}
    113  1.17.2.1       tls }
    114  1.17.2.1       tls 
    115  1.17.2.1       tls /*ARGSUSED*/
    116  1.17.2.1       tls int
    117  1.17.2.1       tls _hf_gethtbyname(void *rv, void *cb_data, va_list ap)
    118  1.17.2.1       tls {
    119  1.17.2.1       tls 	struct hostent *hp;
    120  1.17.2.1       tls 	const char *name;
    121  1.17.2.1       tls 	int af;
    122  1.17.2.1       tls 	struct getnamaddr *info = rv;
    123  1.17.2.1       tls 
    124  1.17.2.1       tls 	_DIAGASSERT(rv != NULL);
    125  1.17.2.1       tls 
    126  1.17.2.1       tls 	name = va_arg(ap, char *);
    127  1.17.2.1       tls 	/* NOSTRICT skip string len */(void)va_arg(ap, int);
    128  1.17.2.1       tls 	af = va_arg(ap, int);
    129  1.17.2.1       tls 
    130  1.17.2.1       tls #if 0
    131  1.17.2.1       tls 	{
    132  1.17.2.1       tls 		res_state res = __res_get_state();
    133  1.17.2.1       tls 		if (res == NULL)
    134  1.17.2.1       tls 			return NS_NOTFOUND;
    135  1.17.2.1       tls 		if (res->options & RES_USE_INET6)
    136  1.17.2.1       tls 			hp = _hf_gethtbyname2(name, AF_INET6, info);
    137  1.17.2.1       tls 		else
    138  1.17.2.1       tls 			hp = NULL;
    139  1.17.2.1       tls 		if (hp == NULL)
    140  1.17.2.1       tls 			hp = _hf_gethtbyname2(name, AF_INET, info);
    141  1.17.2.1       tls 		__res_put_state(res);
    142  1.17.2.1       tls 	}
    143  1.17.2.1       tls #else
    144  1.17.2.1       tls 	hp = _hf_gethtbyname2(name, af, info);
    145  1.17.2.1       tls #endif
    146  1.17.2.1       tls 	if (hp == NULL) {
    147  1.17.2.1       tls 		*info->he = HOST_NOT_FOUND;
    148  1.17.2.1       tls 		return NS_NOTFOUND;
    149  1.17.2.1       tls 	}
    150  1.17.2.1       tls 	return NS_SUCCESS;
    151  1.17.2.1       tls }
    152  1.17.2.1       tls 
    153  1.17.2.1       tls struct hostent *
    154  1.17.2.1       tls _hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
    155  1.17.2.1       tls {
    156  1.17.2.1       tls 	struct hostent *hp, hent;
    157  1.17.2.1       tls 	char *buf, *ptr;
    158  1.17.2.1       tls 	size_t len, anum, num, i;
    159  1.17.2.1       tls 	FILE *hf;
    160  1.17.2.1       tls 	char *aliases[MAXALIASES];
    161  1.17.2.1       tls 	char *addr_ptrs[MAXADDRS];
    162  1.17.2.1       tls 
    163  1.17.2.1       tls 	_DIAGASSERT(name != NULL);
    164  1.17.2.1       tls 
    165  1.17.2.1       tls 	hf = NULL;
    166  1.17.2.1       tls 	sethostent_r(&hf);
    167  1.17.2.1       tls 	if (hf == NULL) {
    168  1.17.2.1       tls 		errno = EINVAL;
    169  1.17.2.1       tls 		*info->he = NETDB_INTERNAL;
    170  1.17.2.1       tls 		return NULL;
    171  1.17.2.1       tls 	}
    172  1.17.2.1       tls 
    173  1.17.2.1       tls 	if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
    174  1.17.2.1       tls 		*info->he = NETDB_INTERNAL;
    175  1.17.2.1       tls 		return NULL;
    176  1.17.2.1       tls 	}
    177  1.17.2.1       tls 
    178  1.17.2.1       tls 	anum = 0;		/* XXX: gcc */
    179  1.17.2.1       tls 	hent.h_name = NULL;	/* XXX: gcc */
    180  1.17.2.1       tls 	hent.h_addrtype = 0;	/* XXX: gcc */
    181  1.17.2.1       tls 	hent.h_length = 0;	/* XXX: gcc */
    182  1.17.2.1       tls 
    183  1.17.2.1       tls 	for (num = 0; num < MAXADDRS;) {
    184  1.17.2.1       tls 		info->hp->h_addrtype = af;
    185  1.17.2.1       tls 		info->hp->h_length = 0;
    186  1.17.2.1       tls 
    187  1.17.2.1       tls 		hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
    188  1.17.2.1       tls 		    info->he);
    189  1.17.2.1       tls 		if (hp == NULL)
    190  1.17.2.1       tls 			break;
    191  1.17.2.1       tls 
    192  1.17.2.1       tls 		if (strcasecmp(hp->h_name, name) != 0) {
    193  1.17.2.1       tls 			char **cp;
    194  1.17.2.1       tls 			for (cp = hp->h_aliases; *cp != NULL; cp++)
    195  1.17.2.1       tls 				if (strcasecmp(*cp, name) == 0)
    196  1.17.2.1       tls 					break;
    197  1.17.2.1       tls 			if (*cp == NULL) continue;
    198  1.17.2.1       tls 		}
    199  1.17.2.1       tls 
    200  1.17.2.1       tls 		if (num == 0) {
    201  1.17.2.1       tls 			hent.h_addrtype = af = hp->h_addrtype;
    202  1.17.2.1       tls 			hent.h_length = hp->h_length;
    203  1.17.2.1       tls 
    204  1.17.2.1       tls 			HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
    205  1.17.2.1       tls 			for (anum = 0; hp->h_aliases[anum]; anum++) {
    206  1.17.2.1       tls 				if (anum >= __arraycount(aliases))
    207  1.17.2.1       tls 					goto nospc;
    208  1.17.2.1       tls 				HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
    209  1.17.2.1       tls 				    ptr, len);
    210  1.17.2.1       tls 			}
    211  1.17.2.1       tls 			ptr = (void *)ALIGN(ptr);
    212  1.17.2.1       tls 			if ((size_t)(ptr - buf) >= info->buflen)
    213  1.17.2.1       tls 				goto nospc;
    214  1.17.2.1       tls 		}
    215  1.17.2.1       tls 
    216  1.17.2.1       tls 		if (num >= __arraycount(addr_ptrs))
    217  1.17.2.1       tls 			goto nospc;
    218  1.17.2.1       tls 		HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
    219  1.17.2.1       tls 		    len);
    220  1.17.2.1       tls 		num++;
    221  1.17.2.1       tls 	}
    222  1.17.2.1       tls 	endhostent_r(&hf);
    223  1.17.2.1       tls 
    224  1.17.2.1       tls 	if (num == 0) {
    225  1.17.2.1       tls 		*info->he = HOST_NOT_FOUND;
    226  1.17.2.1       tls 		free(buf);
    227  1.17.2.1       tls 		return NULL;
    228  1.17.2.1       tls 	}
    229  1.17.2.1       tls 
    230  1.17.2.1       tls 	hp = info->hp;
    231  1.17.2.1       tls 	ptr = info->buf;
    232  1.17.2.1       tls 	len = info->buflen;
    233  1.17.2.1       tls 
    234  1.17.2.1       tls 	hp->h_addrtype = hent.h_addrtype;
    235  1.17.2.1       tls 	hp->h_length = hent.h_length;
    236  1.17.2.1       tls 
    237  1.17.2.1       tls 	HENT_ARRAY(hp->h_aliases, anum, ptr, len);
    238  1.17.2.1       tls 	HENT_ARRAY(hp->h_addr_list, num, ptr, len);
    239  1.17.2.1       tls 
    240  1.17.2.1       tls 	for (i = 0; i < num; i++)
    241  1.17.2.1       tls 		HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
    242  1.17.2.1       tls 		    len);
    243  1.17.2.1       tls 	hp->h_addr_list[num] = NULL;
    244  1.17.2.1       tls 
    245  1.17.2.1       tls 	HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
    246  1.17.2.1       tls 
    247  1.17.2.1       tls 	for (i = 0; i < anum; i++)
    248  1.17.2.1       tls 		HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
    249  1.17.2.1       tls 	hp->h_aliases[anum] = NULL;
    250  1.17.2.1       tls 
    251  1.17.2.1       tls 	free(buf);
    252  1.17.2.1       tls 	return hp;
    253  1.17.2.1       tls nospc:
    254  1.17.2.1       tls 	*info->he = NETDB_INTERNAL;
    255  1.17.2.1       tls 	free(buf);
    256  1.17.2.1       tls 	errno = ENOSPC;
    257  1.17.2.1       tls 	return NULL;
    258  1.17.2.1       tls }
    259  1.17.2.1       tls 
    260  1.17.2.1       tls /*ARGSUSED*/
    261  1.17.2.1       tls int
    262  1.17.2.1       tls _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
    263  1.17.2.1       tls {
    264  1.17.2.1       tls 	struct hostent *hp;
    265  1.17.2.1       tls 	const unsigned char *addr;
    266  1.17.2.1       tls 	struct getnamaddr *info = rv;
    267  1.17.2.1       tls 	FILE *hf;
    268  1.17.2.1       tls 
    269  1.17.2.1       tls 	_DIAGASSERT(rv != NULL);
    270  1.17.2.1       tls 
    271  1.17.2.1       tls 	addr = va_arg(ap, unsigned char *);
    272  1.17.2.1       tls 	info->hp->h_length = va_arg(ap, int);
    273  1.17.2.1       tls 	info->hp->h_addrtype = va_arg(ap, int);
    274  1.17.2.1       tls 
    275  1.17.2.1       tls 	hf = NULL;
    276  1.17.2.1       tls 	sethostent_r(&hf);
    277  1.17.2.1       tls 	if (hf == NULL) {
    278  1.17.2.1       tls 		*info->he = NETDB_INTERNAL;
    279  1.17.2.1       tls 		return NS_UNAVAIL;
    280  1.17.2.1       tls 	}
    281  1.17.2.1       tls 	while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
    282  1.17.2.1       tls 	    info->he)) != NULL)
    283  1.17.2.1       tls 		if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
    284  1.17.2.1       tls 			break;
    285  1.17.2.1       tls 	endhostent_r(&hf);
    286  1.17.2.1       tls 
    287  1.17.2.1       tls 	if (hp == NULL) {
    288  1.17.2.1       tls 		*info->he = HOST_NOT_FOUND;
    289  1.17.2.1       tls 		return NS_NOTFOUND;
    290  1.17.2.1       tls 	}
    291  1.17.2.1       tls 	return NS_SUCCESS;
    292       1.1       cgd }
    293