1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  * lib/libnsl/nss/gethostent.c
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <ctype.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <strings.h>
37*0Sstevel@tonic-gate #include <netdb.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <arpa/inet.h>
40*0Sstevel@tonic-gate #include <nss_dbdefs.h>
41*0Sstevel@tonic-gate #include <rpc/trace.h>
42*0Sstevel@tonic-gate #include <netinet/in.h>
43*0Sstevel@tonic-gate #include <sys/socket.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * Still just a global.  If you want per-thread h_errno,
47*0Sstevel@tonic-gate  * use the reentrant interfaces (gethostbyname_r et al)
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate int h_errno;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #ifdef	NSS_INCLUDE_UNSAFE
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * Don't free this, even on an endhostent(), because bitter experience shows
55*0Sstevel@tonic-gate  * that there's production code that does getXXXbyYYY(), then endXXXent(),
56*0Sstevel@tonic-gate  * and then continues to use the pointer it got back.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate static nss_XbyY_buf_t *buffer;
59*0Sstevel@tonic-gate #define	GETBUF()	\
60*0Sstevel@tonic-gate 	NSS_XbyY_ALLOC(&buffer, sizeof (struct hostent), NSS_BUFLEN_HOSTS)
61*0Sstevel@tonic-gate 	/* === ?? set ENOMEM on failure?  */
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate struct hostent *
64*0Sstevel@tonic-gate gethostbyname(const char *nam)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	nss_XbyY_buf_t  *b;
67*0Sstevel@tonic-gate 	struct hostent  *res = 0;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	trace1(TR_gethostbyname, 0);
70*0Sstevel@tonic-gate 	if ((b = GETBUF()) != 0) {
71*0Sstevel@tonic-gate 		res = gethostbyname_r(nam,
72*0Sstevel@tonic-gate 		    b->result, b->buffer, b->buflen,
73*0Sstevel@tonic-gate 		    &h_errno);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	trace1(TR_gethostbyname, 1);
76*0Sstevel@tonic-gate 	return (res);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate struct hostent *
80*0Sstevel@tonic-gate gethostbyaddr(const void *addr, socklen_t len, int type)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	nss_XbyY_buf_t	*b;
83*0Sstevel@tonic-gate 	struct hostent	*res = 0;
84*0Sstevel@tonic-gate 	char *c;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	trace2(TR_gethostbyaddr, 0, len);
87*0Sstevel@tonic-gate 	h_errno = 0;
88*0Sstevel@tonic-gate 	if (type == AF_INET6)
89*0Sstevel@tonic-gate 		return (getipnodebyaddr(addr, len, type, &h_errno));
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if ((b = GETBUF()) != 0) {
92*0Sstevel@tonic-gate 		res = gethostbyaddr_r(addr, len, type,
93*0Sstevel@tonic-gate 		    b->result, b->buffer, b->buflen,
94*0Sstevel@tonic-gate 		    &h_errno);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 	trace2(TR_gethostbyaddr, 1, len);
97*0Sstevel@tonic-gate 	return (res);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate struct hostent *
101*0Sstevel@tonic-gate gethostent(void)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	nss_XbyY_buf_t	*b;
104*0Sstevel@tonic-gate 	struct hostent	*res = 0;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	trace1(TR_gethostent, 0);
107*0Sstevel@tonic-gate 	if ((b = GETBUF()) != 0) {
108*0Sstevel@tonic-gate 		res = gethostent_r(b->result, b->buffer, b->buflen, &h_errno);
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 	trace1(TR_gethostent, 1);
111*0Sstevel@tonic-gate 	return (res);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
116*0Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
117*0Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
118*0Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
119*0Sstevel@tonic-gate  */
120*0Sstevel@tonic-gate int
121*0Sstevel@tonic-gate __str2hostent(int af, const char *instr, int lenstr, void *ent, char *buffer,
122*0Sstevel@tonic-gate     int buflen)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	struct hostent	*host	= (struct hostent *)ent;
125*0Sstevel@tonic-gate 	const char	*p, *addrstart, *limit;
126*0Sstevel@tonic-gate 	int		naddr, i, aliases_erange = 0;
127*0Sstevel@tonic-gate 	int		addrlen, res;
128*0Sstevel@tonic-gate 	char		addrbuf[100];  /* Why 100? */
129*0Sstevel@tonic-gate 	struct in_addr	*addrp;
130*0Sstevel@tonic-gate 	struct in6_addr	*addrp6;
131*0Sstevel@tonic-gate 	char		**addrvec;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	trace3(TR_str2hostent, 0, lenstr, buflen);
134*0Sstevel@tonic-gate 	if ((instr >= buffer && (buffer + buflen) > instr) ||
135*0Sstevel@tonic-gate 	    (buffer >= instr && (instr + lenstr) > buffer)) {
136*0Sstevel@tonic-gate 		trace3(TR_str2hostent, 1, lenstr, buflen);
137*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 	if (af != AF_INET && af != AF_INET6) {
140*0Sstevel@tonic-gate 		trace3(TR_str2hostent, 1, lenstr, buflen);
141*0Sstevel@tonic-gate 		/*
142*0Sstevel@tonic-gate 		 * XXX - Returning ERANGE here is completely bogus.
143*0Sstevel@tonic-gate 		 * Unfortunately, there's no error code identifying
144*0Sstevel@tonic-gate 		 * bogus calls from the backend (and nothing the user
145*0Sstevel@tonic-gate 		 * can do about our bugs anyway).
146*0Sstevel@tonic-gate 		 */
147*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/*
151*0Sstevel@tonic-gate 	 * The DNS-via-YP code returns multiple lines for a key.
152*0Sstevel@tonic-gate 	 * Normal YP return values do not contain newlines (nor do
153*0Sstevel@tonic-gate 	 * lines from /etc/hosts or other sources)
154*0Sstevel@tonic-gate 	 * We count the number of newlines; this should give us
155*0Sstevel@tonic-gate 	 * the number of IP addresses specified.
156*0Sstevel@tonic-gate 	 * We'll also call the aliases code and instruct it to
157*0Sstevel@tonic-gate 	 * stop at the first newline as the remaining lines will
158*0Sstevel@tonic-gate 	 * all contain the same hostname/aliases (no aliases, unfortunately).
159*0Sstevel@tonic-gate 	 *
160*0Sstevel@tonic-gate 	 * When confronted with a string with embedded newlines,
161*0Sstevel@tonic-gate 	 * this code will take the hostname/aliases on the first line
162*0Sstevel@tonic-gate 	 * and each of the IP addresses at the start of all lines.
163*0Sstevel@tonic-gate 	 * Because the NIS protocol limits return values to 1024 bytes,
164*0Sstevel@tonic-gate 	 * we still do not get all addresses.  If you want to fix
165*0Sstevel@tonic-gate 	 * that problem, do not look here.
166*0Sstevel@tonic-gate 	 */
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	p = instr;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/* Strip trailing newlines */
171*0Sstevel@tonic-gate 	while (lenstr > 0 && p[lenstr - 1] == '\n')
172*0Sstevel@tonic-gate 		lenstr--;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	naddr = 1;
175*0Sstevel@tonic-gate 	limit = p + lenstr;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	for (; p < limit && (p = memchr(p, '\n', limit - p)); p++)
178*0Sstevel@tonic-gate 		naddr++;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	/* Allocate space for naddr addresses and h_addr_list */
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	if (af == AF_INET6) {
183*0Sstevel@tonic-gate 		addrp6 = (struct in6_addr *)ROUND_DOWN(buffer + buflen,
184*0Sstevel@tonic-gate 		    sizeof (*addrp6));
185*0Sstevel@tonic-gate 		addrp6 -= naddr;
186*0Sstevel@tonic-gate 		addrvec = (char **)ROUND_DOWN(addrp6, sizeof (*addrvec));
187*0Sstevel@tonic-gate 		addrvec -= naddr + 1;
188*0Sstevel@tonic-gate 	} else {
189*0Sstevel@tonic-gate 		addrp = (struct in_addr *)ROUND_DOWN(buffer + buflen,
190*0Sstevel@tonic-gate 		    sizeof (*addrp));
191*0Sstevel@tonic-gate 		addrp -= naddr;
192*0Sstevel@tonic-gate 		addrvec = (char **)ROUND_DOWN(addrp, sizeof (*addrvec));
193*0Sstevel@tonic-gate 		addrvec -= naddr + 1;
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if ((char *)addrvec < buffer) {
197*0Sstevel@tonic-gate 		trace3(TR_str2hostent, 1, lenstr, buflen);
198*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	/* For each addr, parse and get it */
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	p = instr;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	for (i = 0; i < naddr; i ++) {
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 		limit = memchr(p, '\n', lenstr - (p - instr));
208*0Sstevel@tonic-gate 		if (limit == NULL)
209*0Sstevel@tonic-gate 			limit = instr + lenstr;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
212*0Sstevel@tonic-gate 			p++;
213*0Sstevel@tonic-gate 		}
214*0Sstevel@tonic-gate 		addrstart = p;
215*0Sstevel@tonic-gate 		while (p < limit && !isspace(*p)) {
216*0Sstevel@tonic-gate 			p++;
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate 		if (p >= limit) {
219*0Sstevel@tonic-gate 		    /* Syntax error - no hostname present or truncated line */
220*0Sstevel@tonic-gate 		    trace3(TR_str2hostent, 1, lenstr, buflen);
221*0Sstevel@tonic-gate 		    return (NSS_STR_PARSE_PARSE);
222*0Sstevel@tonic-gate 		}
223*0Sstevel@tonic-gate 		addrlen = p - addrstart;
224*0Sstevel@tonic-gate 		if (addrlen >= sizeof (addrbuf)) {
225*0Sstevel@tonic-gate 			/* Syntax error -- supposed IP address is too long */
226*0Sstevel@tonic-gate 			trace3(TR_str2hostent, 1, lenstr, buflen);
227*0Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 		memcpy(addrbuf, addrstart, addrlen);
230*0Sstevel@tonic-gate 		addrbuf[addrlen] = '\0';
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 		if (addrlen > ((af == AF_INET6) ? INET6_ADDRSTRLEN
233*0Sstevel@tonic-gate 							: INET_ADDRSTRLEN)) {
234*0Sstevel@tonic-gate 			/* Syntax error -- supposed IP address is too long */
235*0Sstevel@tonic-gate 			trace3(TR_str2hostent, 4, lenstr, buflen);
236*0Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 		if (af == AF_INET) {
239*0Sstevel@tonic-gate 			/*
240*0Sstevel@tonic-gate 			 * inet_pton() doesn't handle d.d.d, d.d, or d formats,
241*0Sstevel@tonic-gate 			 * so we must use inet_addr() for IPv4 addresses.
242*0Sstevel@tonic-gate 			 */
243*0Sstevel@tonic-gate 			addrvec[i] = (char *)&addrp[i];
244*0Sstevel@tonic-gate 			if ((addrp[i].s_addr = inet_addr(addrbuf)) == -1) {
245*0Sstevel@tonic-gate 				/* Syntax error -- bogus IPv4 address */
246*0Sstevel@tonic-gate 				trace3(TR_str2hostent, 4, lenstr, buflen);
247*0Sstevel@tonic-gate 				return (NSS_STR_PARSE_PARSE);
248*0Sstevel@tonic-gate 			}
249*0Sstevel@tonic-gate 		} else {
250*0Sstevel@tonic-gate 			/*
251*0Sstevel@tonic-gate 			 * In the case of AF_INET6, we can have both v4 and v6
252*0Sstevel@tonic-gate 			 * addresses, so we convert v4's to v4 mapped addresses
253*0Sstevel@tonic-gate 			 * and return them as such.
254*0Sstevel@tonic-gate 			 */
255*0Sstevel@tonic-gate 			addrvec[i] = (char *)&addrp6[i];
256*0Sstevel@tonic-gate 			if (strchr(addrbuf, ':') != 0) {
257*0Sstevel@tonic-gate 				if (inet_pton(af, addrbuf, &addrp6[i]) != 1) {
258*0Sstevel@tonic-gate 					trace3(TR_str2hostent, 4, lenstr,
259*0Sstevel@tonic-gate 					    buflen);
260*0Sstevel@tonic-gate 					return (NSS_STR_PARSE_PARSE);
261*0Sstevel@tonic-gate 				}
262*0Sstevel@tonic-gate 			} else {
263*0Sstevel@tonic-gate 				struct in_addr in4;
264*0Sstevel@tonic-gate 				if ((in4.s_addr = inet_addr(addrbuf)) == -1) {
265*0Sstevel@tonic-gate 					trace3(TR_str2hostent, 4, lenstr,
266*0Sstevel@tonic-gate 					    buflen);
267*0Sstevel@tonic-gate 					return (NSS_STR_PARSE_PARSE);
268*0Sstevel@tonic-gate 				}
269*0Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED(&in4, &addrp6[i]);
270*0Sstevel@tonic-gate 			}
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 		/* First address, this is where we get the hostname + aliases */
274*0Sstevel@tonic-gate 		if (i == 0) {
275*0Sstevel@tonic-gate 			while (p < limit && isspace(*p)) {
276*0Sstevel@tonic-gate 				p++;
277*0Sstevel@tonic-gate 			}
278*0Sstevel@tonic-gate 			host->h_aliases = _nss_netdb_aliases(p, limit - p,
279*0Sstevel@tonic-gate 				buffer, ((char *)addrvec) - buffer);
280*0Sstevel@tonic-gate 			if (host->h_aliases == NULL)
281*0Sstevel@tonic-gate 				aliases_erange = 1; /* too big for buffer */
282*0Sstevel@tonic-gate 		}
283*0Sstevel@tonic-gate 		if (limit >= instr + lenstr)
284*0Sstevel@tonic-gate 			break;
285*0Sstevel@tonic-gate 		else
286*0Sstevel@tonic-gate 			p = limit + 1;		/* skip NL */
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	if (host->h_aliases == 0) {
290*0Sstevel@tonic-gate 		if (aliases_erange)
291*0Sstevel@tonic-gate 			res = NSS_STR_PARSE_ERANGE;
292*0Sstevel@tonic-gate 		else
293*0Sstevel@tonic-gate 			res = NSS_STR_PARSE_PARSE;
294*0Sstevel@tonic-gate 	} else {
295*0Sstevel@tonic-gate 		/* Success */
296*0Sstevel@tonic-gate 		host->h_name = host->h_aliases[0];
297*0Sstevel@tonic-gate 		host->h_aliases++;
298*0Sstevel@tonic-gate 		res = NSS_STR_PARSE_SUCCESS;
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 	/*
301*0Sstevel@tonic-gate 	 * If i < naddr, we quit the loop early and addrvec[i+1] needs NULL
302*0Sstevel@tonic-gate 	 * otherwise, we ran naddr iterations and addrvec[naddr] needs NULL
303*0Sstevel@tonic-gate 	 */
304*0Sstevel@tonic-gate 	addrvec[i >= naddr ? naddr : i + 1] = 0;
305*0Sstevel@tonic-gate 	if (af == AF_INET6) {
306*0Sstevel@tonic-gate 		host->h_length    = sizeof (struct in6_addr);
307*0Sstevel@tonic-gate 	} else {
308*0Sstevel@tonic-gate 		host->h_length    = sizeof (struct in_addr);
309*0Sstevel@tonic-gate 	}
310*0Sstevel@tonic-gate 	host->h_addrtype  = af;
311*0Sstevel@tonic-gate 	host->h_addr_list = addrvec;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	trace3(TR_str2hostent, 1, lenstr, buflen);
314*0Sstevel@tonic-gate 	return (res);
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate #endif	/* NSS_INCLUDE_UNSAFE */
318