gethostbynis.c revision 145550
1/*-
2 * Copyright (c) 1994, Garrett Wollman
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/lib/libc/net/gethostbynis.c 145550 2005-04-26 14:55:47Z ume $");
28
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <arpa/nameser.h>
34#include <netdb.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <ctype.h>
38#include <errno.h>
39#include <string.h>
40#include <stdarg.h>
41#include <nsswitch.h>
42#ifdef YP
43#include <rpc/rpc.h>
44#include <rpcsvc/yp_prot.h>
45#include <rpcsvc/ypclnt.h>
46#endif
47
48#define	MAXALIASES	35
49#define	MAXADDRS	35
50
51#ifdef YP
52static char *host_aliases[MAXALIASES];
53static uint32_t host_addr[4];		/* IPv4 or IPv6 */
54static char *host_addrs[2];
55
56static struct hostent *
57_gethostbynis(name, map, af)
58	const char *name;
59	char *map;
60	int af;
61{
62	char *cp, **q;
63	char *result;
64	int resultlen, size, addrok = 0;
65	static struct hostent h;
66	static char *domain = (char *)NULL;
67	static char ypbuf[YPMAXRECORD + 2];
68
69	switch(af) {
70	case AF_INET:
71		size = NS_INADDRSZ;
72		break;
73	case AF_INET6:
74		size = NS_IN6ADDRSZ;
75		break;
76	default:
77		errno = EAFNOSUPPORT;
78		h_errno = NETDB_INTERNAL;
79		return NULL;
80	}
81
82	if (domain == (char *)NULL)
83		if (yp_get_default_domain (&domain)) {
84			h_errno = NETDB_INTERNAL;
85			return ((struct hostent *)NULL);
86		}
87
88	if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) {
89		h_errno = HOST_NOT_FOUND;
90		return ((struct hostent *)NULL);
91	}
92
93	/* avoid potential memory leak */
94	bcopy((char *)result, (char *)&ypbuf, resultlen);
95	ypbuf[resultlen] = '\0';
96	free(result);
97	result = (char *)&ypbuf;
98
99	if ((cp = index(result, '\n')))
100		*cp = '\0';
101
102	cp = strpbrk(result, " \t");
103	*cp++ = '\0';
104	h.h_addr_list = host_addrs;
105	h.h_addr = (char *)host_addr;
106	switch (af) {
107	case AF_INET:
108		addrok = inet_aton(result, (struct in_addr *)host_addr);
109		break;
110	case AF_INET6:
111		addrok = inet_pton(af, result, host_addr);
112		break;
113	}
114	if (addrok != 1) {
115		h_errno = HOST_NOT_FOUND;
116		return NULL;
117	}
118	h.h_length = size;
119	h.h_addrtype = af;
120	while (*cp == ' ' || *cp == '\t')
121		cp++;
122	h.h_name = cp;
123	q = h.h_aliases = host_aliases;
124	cp = strpbrk(cp, " \t");
125	if (cp != NULL)
126		*cp++ = '\0';
127	while (cp && *cp) {
128		if (*cp == ' ' || *cp == '\t') {
129			cp++;
130			continue;
131		}
132		if (q < &host_aliases[MAXALIASES - 1])
133			*q++ = cp;
134		cp = strpbrk(cp, " \t");
135		if (cp != NULL)
136			*cp++ = '\0';
137	}
138	*q = NULL;
139	return (&h);
140}
141
142static struct hostent *
143_gethostbynisname_p(const char *name, int af)
144{
145	char *map;
146
147	switch (af) {
148	case AF_INET:
149		map = "hosts.byname";
150		break;
151	default:
152		map = "ipnodes.byname";
153		break;
154	}
155	return _gethostbynis(name, map, af);
156}
157
158static struct hostent *
159_gethostbynisaddr_p(const char *addr, int len, int af)
160{
161	char *map;
162
163	switch (af) {
164	case AF_INET:
165		map = "hosts.byaddr";
166		break;
167	default:
168		map = "ipnodes.byaddr";
169		break;
170	}
171	return _gethostbynis(inet_ntoa(*(struct in_addr *)addr), map, af);
172}
173#endif /* YP */
174
175/* XXX _gethostbynisname/_gethostbynisaddr only used by getipnodeby*() */
176struct hostent *
177_gethostbynisname(const char *name, int af)
178{
179#ifdef YP
180	return _gethostbynisname_p(name, af);
181#else
182	return NULL;
183#endif
184}
185
186struct hostent *
187_gethostbynisaddr(const char *addr, int len, int af)
188{
189#ifdef YP
190	return _gethostbynisaddr_p(addr, len, af);
191#else
192	return NULL;
193#endif
194}
195
196
197int
198_nis_gethostbyname(void *rval, void *cb_data, va_list ap)
199{
200#ifdef YP
201	const char *name;
202	int af;
203
204	name = va_arg(ap, const char *);
205	af = va_arg(ap, int);
206
207	*(struct hostent **)rval = _gethostbynisname_p(name, af);
208	return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
209#else
210	return NS_UNAVAIL;
211#endif
212}
213
214int
215_nis_gethostbyaddr(void *rval, void *cb_data, va_list ap)
216{
217#ifdef YP
218	const char *addr;
219	int len;
220	int af;
221
222	addr = va_arg(ap, const char *);
223	len = va_arg(ap, int);
224	af = va_arg(ap, int);
225
226	*(struct hostent **)rval =_gethostbynisaddr_p(addr, len, af);
227	return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
228#else
229	return NS_UNAVAIL;
230#endif
231}
232