Home | History | Annotate | Line # | Download | only in net
sethostent.c revision 1.16.18.1
      1  1.16.18.1    bouyer /*	$NetBSD: sethostent.c,v 1.16.18.1 2013/12/18 20:23:36 bouyer 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.16.18.1    bouyer __RCSID("$NetBSD: sethostent.c,v 1.16.18.1 2013/12/18 20:23:36 bouyer 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.16.18.1    bouyer #include <arpa/inet.h>
     47  1.16.18.1    bouyer #include <assert.h>
     48  1.16.18.1    bouyer #include <string.h>
     49  1.16.18.1    bouyer #include <nsswitch.h>
     50        1.1       cgd #include <netdb.h>
     51        1.1       cgd #include <resolv.h>
     52  1.16.18.1    bouyer #include <errno.h>
     53  1.16.18.1    bouyer #include <stdlib.h>
     54  1.16.18.1    bouyer 
     55  1.16.18.1    bouyer #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.16.18.1    bouyer void	res_close(void);
     64       1.13  christos #endif
     65  1.16.18.1    bouyer 
     66  1.16.18.1    bouyer 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.16.18.1    bouyer 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.16.18.1    bouyer 	sethostent_r(&_h_file);
     79        1.1       cgd }
     80        1.1       cgd 
     81        1.1       cgd void
     82  1.16.18.1    bouyer 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.16.18.1    bouyer 	endhostent_r(&_h_file);
     89  1.16.18.1    bouyer }
     90  1.16.18.1    bouyer static const char *_h_hosts = _PATH_HOSTS;
     91  1.16.18.1    bouyer 
     92  1.16.18.1    bouyer void
     93  1.16.18.1    bouyer _hf_sethostsfile(const char *f) {
     94  1.16.18.1    bouyer 	_h_hosts = f;
     95  1.16.18.1    bouyer }
     96  1.16.18.1    bouyer 
     97  1.16.18.1    bouyer void
     98  1.16.18.1    bouyer sethostent_r(FILE **hf)
     99  1.16.18.1    bouyer {
    100  1.16.18.1    bouyer 	if (!*hf)
    101  1.16.18.1    bouyer 		*hf = fopen(_h_hosts, "re");
    102  1.16.18.1    bouyer 	else
    103  1.16.18.1    bouyer 		rewind(*hf);
    104  1.16.18.1    bouyer }
    105  1.16.18.1    bouyer 
    106  1.16.18.1    bouyer void
    107  1.16.18.1    bouyer endhostent_r(FILE **hf)
    108  1.16.18.1    bouyer {
    109  1.16.18.1    bouyer 	if (*hf) {
    110  1.16.18.1    bouyer 		(void)fclose(*hf);
    111  1.16.18.1    bouyer 		*hf = NULL;
    112  1.16.18.1    bouyer 	}
    113  1.16.18.1    bouyer }
    114  1.16.18.1    bouyer 
    115  1.16.18.1    bouyer /*ARGSUSED*/
    116  1.16.18.1    bouyer int
    117  1.16.18.1    bouyer _hf_gethtbyname(void *rv, void *cb_data, va_list ap)
    118  1.16.18.1    bouyer {
    119  1.16.18.1    bouyer 	struct hostent *hp;
    120  1.16.18.1    bouyer 	const char *name;
    121  1.16.18.1    bouyer 	int af;
    122  1.16.18.1    bouyer 	struct getnamaddr *info = rv;
    123  1.16.18.1    bouyer 
    124  1.16.18.1    bouyer 	_DIAGASSERT(rv != NULL);
    125  1.16.18.1    bouyer 
    126  1.16.18.1    bouyer 	name = va_arg(ap, char *);
    127  1.16.18.1    bouyer 	/* NOSTRICT skip string len */(void)va_arg(ap, int);
    128  1.16.18.1    bouyer 	af = va_arg(ap, int);
    129  1.16.18.1    bouyer 
    130  1.16.18.1    bouyer #if 0
    131  1.16.18.1    bouyer 	{
    132  1.16.18.1    bouyer 		res_state res = __res_get_state();
    133  1.16.18.1    bouyer 		if (res == NULL)
    134  1.16.18.1    bouyer 			return NS_NOTFOUND;
    135  1.16.18.1    bouyer 		if (res->options & RES_USE_INET6)
    136  1.16.18.1    bouyer 			hp = _hf_gethtbyname2(name, AF_INET6, info);
    137  1.16.18.1    bouyer 		else
    138  1.16.18.1    bouyer 			hp = NULL;
    139  1.16.18.1    bouyer 		if (hp == NULL)
    140  1.16.18.1    bouyer 			hp = _hf_gethtbyname2(name, AF_INET, info);
    141  1.16.18.1    bouyer 		__res_put_state(res);
    142  1.16.18.1    bouyer 	}
    143  1.16.18.1    bouyer #else
    144  1.16.18.1    bouyer 	hp = _hf_gethtbyname2(name, af, info);
    145  1.16.18.1    bouyer #endif
    146  1.16.18.1    bouyer 	if (hp == NULL) {
    147  1.16.18.1    bouyer 		*info->he = HOST_NOT_FOUND;
    148  1.16.18.1    bouyer 		return NS_NOTFOUND;
    149  1.16.18.1    bouyer 	}
    150  1.16.18.1    bouyer 	return NS_SUCCESS;
    151  1.16.18.1    bouyer }
    152  1.16.18.1    bouyer 
    153  1.16.18.1    bouyer struct hostent *
    154  1.16.18.1    bouyer _hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
    155  1.16.18.1    bouyer {
    156  1.16.18.1    bouyer 	struct hostent *hp, hent;
    157  1.16.18.1    bouyer 	char *buf, *ptr;
    158  1.16.18.1    bouyer 	size_t len, anum, num, i;
    159  1.16.18.1    bouyer 	FILE *hf;
    160  1.16.18.1    bouyer 	char *aliases[MAXALIASES];
    161  1.16.18.1    bouyer 	char *addr_ptrs[MAXADDRS];
    162  1.16.18.1    bouyer 
    163  1.16.18.1    bouyer 	_DIAGASSERT(name != NULL);
    164  1.16.18.1    bouyer 
    165  1.16.18.1    bouyer 	hf = NULL;
    166  1.16.18.1    bouyer 	sethostent_r(&hf);
    167  1.16.18.1    bouyer 	if (hf == NULL) {
    168  1.16.18.1    bouyer 		errno = EINVAL;
    169  1.16.18.1    bouyer 		*info->he = NETDB_INTERNAL;
    170  1.16.18.1    bouyer 		return NULL;
    171  1.16.18.1    bouyer 	}
    172  1.16.18.1    bouyer 
    173  1.16.18.1    bouyer 	if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
    174  1.16.18.1    bouyer 		*info->he = NETDB_INTERNAL;
    175  1.16.18.1    bouyer 		return NULL;
    176  1.16.18.1    bouyer 	}
    177  1.16.18.1    bouyer 
    178  1.16.18.1    bouyer 	anum = 0;		/* XXX: gcc */
    179  1.16.18.1    bouyer 	hent.h_name = NULL;	/* XXX: gcc */
    180  1.16.18.1    bouyer 	hent.h_addrtype = 0;	/* XXX: gcc */
    181  1.16.18.1    bouyer 	hent.h_length = 0;	/* XXX: gcc */
    182  1.16.18.1    bouyer 
    183  1.16.18.1    bouyer 	for (num = 0; num < MAXADDRS;) {
    184  1.16.18.1    bouyer 		info->hp->h_addrtype = af;
    185  1.16.18.1    bouyer 		info->hp->h_length = 0;
    186  1.16.18.1    bouyer 
    187  1.16.18.1    bouyer 		hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
    188  1.16.18.1    bouyer 		    info->he);
    189  1.16.18.1    bouyer 		if (hp == NULL)
    190  1.16.18.1    bouyer 			break;
    191  1.16.18.1    bouyer 
    192  1.16.18.1    bouyer 		if (strcasecmp(hp->h_name, name) != 0) {
    193  1.16.18.1    bouyer 			char **cp;
    194  1.16.18.1    bouyer 			for (cp = hp->h_aliases; *cp != NULL; cp++)
    195  1.16.18.1    bouyer 				if (strcasecmp(*cp, name) == 0)
    196  1.16.18.1    bouyer 					break;
    197  1.16.18.1    bouyer 			if (*cp == NULL) continue;
    198  1.16.18.1    bouyer 		}
    199  1.16.18.1    bouyer 
    200  1.16.18.1    bouyer 		if (num == 0) {
    201  1.16.18.1    bouyer 			hent.h_addrtype = af = hp->h_addrtype;
    202  1.16.18.1    bouyer 			hent.h_length = hp->h_length;
    203  1.16.18.1    bouyer 
    204  1.16.18.1    bouyer 			HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
    205  1.16.18.1    bouyer 			for (anum = 0; hp->h_aliases[anum]; anum++) {
    206  1.16.18.1    bouyer 				if (anum >= __arraycount(aliases))
    207  1.16.18.1    bouyer 					goto nospc;
    208  1.16.18.1    bouyer 				HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
    209  1.16.18.1    bouyer 				    ptr, len);
    210  1.16.18.1    bouyer 			}
    211  1.16.18.1    bouyer 			ptr = (void *)ALIGN(ptr);
    212  1.16.18.1    bouyer 			if ((size_t)(ptr - buf) >= info->buflen)
    213  1.16.18.1    bouyer 				goto nospc;
    214  1.16.18.1    bouyer 		}
    215  1.16.18.1    bouyer 
    216  1.16.18.1    bouyer 		if (num >= __arraycount(addr_ptrs))
    217  1.16.18.1    bouyer 			goto nospc;
    218  1.16.18.1    bouyer 		HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
    219  1.16.18.1    bouyer 		    len);
    220  1.16.18.1    bouyer 		num++;
    221  1.16.18.1    bouyer 	}
    222  1.16.18.1    bouyer 	endhostent_r(&hf);
    223  1.16.18.1    bouyer 
    224  1.16.18.1    bouyer 	if (num == 0) {
    225  1.16.18.1    bouyer 		*info->he = HOST_NOT_FOUND;
    226  1.16.18.1    bouyer 		return NULL;
    227  1.16.18.1    bouyer 	}
    228  1.16.18.1    bouyer 
    229  1.16.18.1    bouyer 	hp = info->hp;
    230  1.16.18.1    bouyer 	ptr = info->buf;
    231  1.16.18.1    bouyer 	len = info->buflen;
    232  1.16.18.1    bouyer 
    233  1.16.18.1    bouyer 	hp->h_addrtype = hent.h_addrtype;
    234  1.16.18.1    bouyer 	hp->h_length = hent.h_length;
    235  1.16.18.1    bouyer 
    236  1.16.18.1    bouyer 	HENT_ARRAY(hp->h_aliases, anum, ptr, len);
    237  1.16.18.1    bouyer 	HENT_ARRAY(hp->h_addr_list, num, ptr, len);
    238  1.16.18.1    bouyer 
    239  1.16.18.1    bouyer 	for (i = 0; i < num; i++)
    240  1.16.18.1    bouyer 		HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
    241  1.16.18.1    bouyer 		    len);
    242  1.16.18.1    bouyer 	hp->h_addr_list[num] = NULL;
    243  1.16.18.1    bouyer 
    244  1.16.18.1    bouyer 	HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
    245  1.16.18.1    bouyer 
    246  1.16.18.1    bouyer 	for (i = 0; i < anum; i++)
    247  1.16.18.1    bouyer 		HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
    248  1.16.18.1    bouyer 	hp->h_aliases[anum] = NULL;
    249  1.16.18.1    bouyer 
    250  1.16.18.1    bouyer 	return hp;
    251  1.16.18.1    bouyer nospc:
    252  1.16.18.1    bouyer 	*info->he = NETDB_INTERNAL;
    253  1.16.18.1    bouyer 	errno = ENOSPC;
    254  1.16.18.1    bouyer 	return NULL;
    255  1.16.18.1    bouyer }
    256  1.16.18.1    bouyer 
    257  1.16.18.1    bouyer /*ARGSUSED*/
    258  1.16.18.1    bouyer int
    259  1.16.18.1    bouyer _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
    260  1.16.18.1    bouyer {
    261  1.16.18.1    bouyer 	struct hostent *hp;
    262  1.16.18.1    bouyer 	const unsigned char *addr;
    263  1.16.18.1    bouyer 	struct getnamaddr *info = rv;
    264  1.16.18.1    bouyer 	FILE *hf;
    265  1.16.18.1    bouyer 
    266  1.16.18.1    bouyer 	_DIAGASSERT(rv != NULL);
    267  1.16.18.1    bouyer 
    268  1.16.18.1    bouyer 	addr = va_arg(ap, unsigned char *);
    269  1.16.18.1    bouyer 	info->hp->h_length = va_arg(ap, int);
    270  1.16.18.1    bouyer 	info->hp->h_addrtype = va_arg(ap, int);
    271  1.16.18.1    bouyer 
    272  1.16.18.1    bouyer 	hf = NULL;
    273  1.16.18.1    bouyer 	sethostent_r(&hf);
    274  1.16.18.1    bouyer 	if (hf == NULL) {
    275  1.16.18.1    bouyer 		*info->he = NETDB_INTERNAL;
    276  1.16.18.1    bouyer 		return NS_UNAVAIL;
    277  1.16.18.1    bouyer 	}
    278  1.16.18.1    bouyer 	while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
    279  1.16.18.1    bouyer 	    info->he)) != NULL)
    280  1.16.18.1    bouyer 		if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
    281  1.16.18.1    bouyer 			break;
    282  1.16.18.1    bouyer 	endhostent_r(&hf);
    283  1.16.18.1    bouyer 
    284  1.16.18.1    bouyer 	if (hp == NULL) {
    285  1.16.18.1    bouyer 		*info->he = HOST_NOT_FOUND;
    286  1.16.18.1    bouyer 		return NS_NOTFOUND;
    287  1.16.18.1    bouyer 	}
    288  1.16.18.1    bouyer 	return NS_SUCCESS;
    289        1.1       cgd }
    290