10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
26*1219Sraf  */
27*1219Sraf 
28*1219Sraf /*
290Sstevel@tonic-gate  * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
34*1219Sraf #include "mt.h"
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <strings.h>
390Sstevel@tonic-gate #include <netdb.h>
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <arpa/inet.h>
420Sstevel@tonic-gate #include <nss_dbdefs.h>
430Sstevel@tonic-gate #include <netinet/in.h>
440Sstevel@tonic-gate #include <sys/socket.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * Still just a global.  If you want per-thread h_errno,
480Sstevel@tonic-gate  * use the reentrant interfaces (gethostbyname_r et al)
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate int h_errno;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #ifdef	NSS_INCLUDE_UNSAFE
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Don't free this, even on an endhostent(), because bitter experience shows
560Sstevel@tonic-gate  * that there's production code that does getXXXbyYYY(), then endXXXent(),
570Sstevel@tonic-gate  * and then continues to use the pointer it got back.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate static nss_XbyY_buf_t *buffer;
600Sstevel@tonic-gate #define	GETBUF()	\
610Sstevel@tonic-gate 	NSS_XbyY_ALLOC(&buffer, sizeof (struct hostent), NSS_BUFLEN_HOSTS)
620Sstevel@tonic-gate 	/* === ?? set ENOMEM on failure?  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate struct hostent *
650Sstevel@tonic-gate gethostbyname(const char *nam)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	nss_XbyY_buf_t  *b;
680Sstevel@tonic-gate 
69132Srobinson 	if ((b = GETBUF()) == 0)
70132Srobinson 		return (NULL);
71132Srobinson 	return (gethostbyname_r(nam, b->result, b->buffer, b->buflen,
72132Srobinson 		    &h_errno));
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate struct hostent *
760Sstevel@tonic-gate gethostbyaddr(const void *addr, socklen_t len, int type)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	nss_XbyY_buf_t	*b;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	h_errno = 0;
810Sstevel@tonic-gate 	if (type == AF_INET6)
820Sstevel@tonic-gate 		return (getipnodebyaddr(addr, len, type, &h_errno));
830Sstevel@tonic-gate 
84132Srobinson 	if ((b = GETBUF()) == 0)
85132Srobinson 		return (NULL);
86132Srobinson 	return (gethostbyaddr_r(addr, len, type,
87132Srobinson 		    b->result, b->buffer, b->buflen, &h_errno));
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate struct hostent *
910Sstevel@tonic-gate gethostent(void)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	nss_XbyY_buf_t	*b;
940Sstevel@tonic-gate 
95132Srobinson 	if ((b = GETBUF()) == 0)
96132Srobinson 		return (NULL);
97132Srobinson 	return (gethostent_r(b->result, b->buffer, b->buflen, &h_errno));
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
1020Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
1030Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
1040Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate int
1070Sstevel@tonic-gate __str2hostent(int af, const char *instr, int lenstr, void *ent, char *buffer,
1080Sstevel@tonic-gate     int buflen)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	struct hostent	*host	= (struct hostent *)ent;
1110Sstevel@tonic-gate 	const char	*p, *addrstart, *limit;
1120Sstevel@tonic-gate 	int		naddr, i, aliases_erange = 0;
1130Sstevel@tonic-gate 	int		addrlen, res;
1140Sstevel@tonic-gate 	char		addrbuf[100];  /* Why 100? */
1150Sstevel@tonic-gate 	struct in_addr	*addrp;
1160Sstevel@tonic-gate 	struct in6_addr	*addrp6;
1170Sstevel@tonic-gate 	char		**addrvec;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if ((instr >= buffer && (buffer + buflen) > instr) ||
120132Srobinson 	    (buffer >= instr && (instr + lenstr) > buffer))
1210Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
1220Sstevel@tonic-gate 	if (af != AF_INET && af != AF_INET6) {
1230Sstevel@tonic-gate 		/*
1240Sstevel@tonic-gate 		 * XXX - Returning ERANGE here is completely bogus.
1250Sstevel@tonic-gate 		 * Unfortunately, there's no error code identifying
1260Sstevel@tonic-gate 		 * bogus calls from the backend (and nothing the user
1270Sstevel@tonic-gate 		 * can do about our bugs anyway).
1280Sstevel@tonic-gate 		 */
1290Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/*
1330Sstevel@tonic-gate 	 * The DNS-via-YP code returns multiple lines for a key.
1340Sstevel@tonic-gate 	 * Normal YP return values do not contain newlines (nor do
1350Sstevel@tonic-gate 	 * lines from /etc/hosts or other sources)
1360Sstevel@tonic-gate 	 * We count the number of newlines; this should give us
1370Sstevel@tonic-gate 	 * the number of IP addresses specified.
1380Sstevel@tonic-gate 	 * We'll also call the aliases code and instruct it to
1390Sstevel@tonic-gate 	 * stop at the first newline as the remaining lines will
1400Sstevel@tonic-gate 	 * all contain the same hostname/aliases (no aliases, unfortunately).
1410Sstevel@tonic-gate 	 *
1420Sstevel@tonic-gate 	 * When confronted with a string with embedded newlines,
1430Sstevel@tonic-gate 	 * this code will take the hostname/aliases on the first line
1440Sstevel@tonic-gate 	 * and each of the IP addresses at the start of all lines.
1450Sstevel@tonic-gate 	 * Because the NIS protocol limits return values to 1024 bytes,
1460Sstevel@tonic-gate 	 * we still do not get all addresses.  If you want to fix
1470Sstevel@tonic-gate 	 * that problem, do not look here.
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	p = instr;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* Strip trailing newlines */
1530Sstevel@tonic-gate 	while (lenstr > 0 && p[lenstr - 1] == '\n')
1540Sstevel@tonic-gate 		lenstr--;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	naddr = 1;
1570Sstevel@tonic-gate 	limit = p + lenstr;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	for (; p < limit && (p = memchr(p, '\n', limit - p)); p++)
1600Sstevel@tonic-gate 		naddr++;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/* Allocate space for naddr addresses and h_addr_list */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (af == AF_INET6) {
1650Sstevel@tonic-gate 		addrp6 = (struct in6_addr *)ROUND_DOWN(buffer + buflen,
1660Sstevel@tonic-gate 		    sizeof (*addrp6));
1670Sstevel@tonic-gate 		addrp6 -= naddr;
1680Sstevel@tonic-gate 		addrvec = (char **)ROUND_DOWN(addrp6, sizeof (*addrvec));
1690Sstevel@tonic-gate 		addrvec -= naddr + 1;
1700Sstevel@tonic-gate 	} else {
1710Sstevel@tonic-gate 		addrp = (struct in_addr *)ROUND_DOWN(buffer + buflen,
1720Sstevel@tonic-gate 		    sizeof (*addrp));
1730Sstevel@tonic-gate 		addrp -= naddr;
1740Sstevel@tonic-gate 		addrvec = (char **)ROUND_DOWN(addrp, sizeof (*addrvec));
1750Sstevel@tonic-gate 		addrvec -= naddr + 1;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
178132Srobinson 	if ((char *)addrvec < buffer)
1790Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/* For each addr, parse and get it */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	p = instr;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	for (i = 0; i < naddr; i ++) {
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		limit = memchr(p, '\n', lenstr - (p - instr));
1880Sstevel@tonic-gate 		if (limit == NULL)
1890Sstevel@tonic-gate 			limit = instr + lenstr;
1900Sstevel@tonic-gate 
191132Srobinson 		while (p < limit && isspace(*p))
1920Sstevel@tonic-gate 			p++;
1930Sstevel@tonic-gate 		addrstart = p;
194132Srobinson 		while (p < limit && !isspace(*p))
1950Sstevel@tonic-gate 			p++;
196132Srobinson 		if (p >= limit)
1970Sstevel@tonic-gate 		    /* Syntax error - no hostname present or truncated line */
1980Sstevel@tonic-gate 		    return (NSS_STR_PARSE_PARSE);
1990Sstevel@tonic-gate 		addrlen = p - addrstart;
200132Srobinson 		if (addrlen >= sizeof (addrbuf))
2010Sstevel@tonic-gate 			/* Syntax error -- supposed IP address is too long */
2020Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
203132Srobinson 		(void) memcpy(addrbuf, addrstart, addrlen);
2040Sstevel@tonic-gate 		addrbuf[addrlen] = '\0';
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		if (addrlen > ((af == AF_INET6) ? INET6_ADDRSTRLEN
207132Srobinson 							: INET_ADDRSTRLEN))
2080Sstevel@tonic-gate 			/* Syntax error -- supposed IP address is too long */
2090Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
2100Sstevel@tonic-gate 		if (af == AF_INET) {
2110Sstevel@tonic-gate 			/*
2120Sstevel@tonic-gate 			 * inet_pton() doesn't handle d.d.d, d.d, or d formats,
2130Sstevel@tonic-gate 			 * so we must use inet_addr() for IPv4 addresses.
2140Sstevel@tonic-gate 			 */
2150Sstevel@tonic-gate 			addrvec[i] = (char *)&addrp[i];
216132Srobinson 			if ((addrp[i].s_addr = inet_addr(addrbuf)) ==
217132Srobinson 								0xffffffffU)
2180Sstevel@tonic-gate 				/* Syntax error -- bogus IPv4 address */
2190Sstevel@tonic-gate 				return (NSS_STR_PARSE_PARSE);
2200Sstevel@tonic-gate 		} else {
2210Sstevel@tonic-gate 			/*
2220Sstevel@tonic-gate 			 * In the case of AF_INET6, we can have both v4 and v6
2230Sstevel@tonic-gate 			 * addresses, so we convert v4's to v4 mapped addresses
2240Sstevel@tonic-gate 			 * and return them as such.
2250Sstevel@tonic-gate 			 */
2260Sstevel@tonic-gate 			addrvec[i] = (char *)&addrp6[i];
2270Sstevel@tonic-gate 			if (strchr(addrbuf, ':') != 0) {
228132Srobinson 				if (inet_pton(af, addrbuf, &addrp6[i]) != 1)
2290Sstevel@tonic-gate 					return (NSS_STR_PARSE_PARSE);
2300Sstevel@tonic-gate 			} else {
2310Sstevel@tonic-gate 				struct in_addr in4;
232132Srobinson 				if ((in4.s_addr = inet_addr(addrbuf)) ==
233132Srobinson 								0xffffffffU)
2340Sstevel@tonic-gate 					return (NSS_STR_PARSE_PARSE);
2350Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED(&in4, &addrp6[i]);
2360Sstevel@tonic-gate 			}
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		/* First address, this is where we get the hostname + aliases */
2400Sstevel@tonic-gate 		if (i == 0) {
2410Sstevel@tonic-gate 			while (p < limit && isspace(*p)) {
2420Sstevel@tonic-gate 				p++;
2430Sstevel@tonic-gate 			}
2440Sstevel@tonic-gate 			host->h_aliases = _nss_netdb_aliases(p, limit - p,
2450Sstevel@tonic-gate 				buffer, ((char *)addrvec) - buffer);
2460Sstevel@tonic-gate 			if (host->h_aliases == NULL)
2470Sstevel@tonic-gate 				aliases_erange = 1; /* too big for buffer */
2480Sstevel@tonic-gate 		}
2490Sstevel@tonic-gate 		if (limit >= instr + lenstr)
2500Sstevel@tonic-gate 			break;
2510Sstevel@tonic-gate 		else
2520Sstevel@tonic-gate 			p = limit + 1;		/* skip NL */
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (host->h_aliases == 0) {
2560Sstevel@tonic-gate 		if (aliases_erange)
2570Sstevel@tonic-gate 			res = NSS_STR_PARSE_ERANGE;
2580Sstevel@tonic-gate 		else
2590Sstevel@tonic-gate 			res = NSS_STR_PARSE_PARSE;
2600Sstevel@tonic-gate 	} else {
2610Sstevel@tonic-gate 		/* Success */
2620Sstevel@tonic-gate 		host->h_name = host->h_aliases[0];
2630Sstevel@tonic-gate 		host->h_aliases++;
2640Sstevel@tonic-gate 		res = NSS_STR_PARSE_SUCCESS;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	/*
2670Sstevel@tonic-gate 	 * If i < naddr, we quit the loop early and addrvec[i+1] needs NULL
2680Sstevel@tonic-gate 	 * otherwise, we ran naddr iterations and addrvec[naddr] needs NULL
2690Sstevel@tonic-gate 	 */
2700Sstevel@tonic-gate 	addrvec[i >= naddr ? naddr : i + 1] = 0;
2710Sstevel@tonic-gate 	if (af == AF_INET6) {
2720Sstevel@tonic-gate 		host->h_length    = sizeof (struct in6_addr);
2730Sstevel@tonic-gate 	} else {
2740Sstevel@tonic-gate 		host->h_length    = sizeof (struct in_addr);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 	host->h_addrtype  = af;
2770Sstevel@tonic-gate 	host->h_addr_list = addrvec;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	return (res);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate #endif	/* NSS_INCLUDE_UNSAFE */
282