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 */ 22*132Srobinson 230Sstevel@tonic-gate /* 24*132Srobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate * 270Sstevel@tonic-gate * Ye olde non-reentrant interface (MT-unsafe, caveat utor) 280Sstevel@tonic-gate * 290Sstevel@tonic-gate * lib/libnsl/nss/gethostent.c 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <stdlib.h> 350Sstevel@tonic-gate #include <ctype.h> 360Sstevel@tonic-gate #include <string.h> 370Sstevel@tonic-gate #include <strings.h> 380Sstevel@tonic-gate #include <netdb.h> 390Sstevel@tonic-gate #include <stdio.h> 400Sstevel@tonic-gate #include <arpa/inet.h> 410Sstevel@tonic-gate #include <nss_dbdefs.h> 420Sstevel@tonic-gate #include <netinet/in.h> 430Sstevel@tonic-gate #include <sys/socket.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * Still just a global. If you want per-thread h_errno, 470Sstevel@tonic-gate * use the reentrant interfaces (gethostbyname_r et al) 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate int h_errno; 500Sstevel@tonic-gate 510Sstevel@tonic-gate #ifdef NSS_INCLUDE_UNSAFE 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * Don't free this, even on an endhostent(), because bitter experience shows 550Sstevel@tonic-gate * that there's production code that does getXXXbyYYY(), then endXXXent(), 560Sstevel@tonic-gate * and then continues to use the pointer it got back. 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate static nss_XbyY_buf_t *buffer; 590Sstevel@tonic-gate #define GETBUF() \ 600Sstevel@tonic-gate NSS_XbyY_ALLOC(&buffer, sizeof (struct hostent), NSS_BUFLEN_HOSTS) 610Sstevel@tonic-gate /* === ?? set ENOMEM on failure? */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate struct hostent * 640Sstevel@tonic-gate gethostbyname(const char *nam) 650Sstevel@tonic-gate { 660Sstevel@tonic-gate nss_XbyY_buf_t *b; 670Sstevel@tonic-gate 68*132Srobinson if ((b = GETBUF()) == 0) 69*132Srobinson return (NULL); 70*132Srobinson return (gethostbyname_r(nam, b->result, b->buffer, b->buflen, 71*132Srobinson &h_errno)); 720Sstevel@tonic-gate } 730Sstevel@tonic-gate 740Sstevel@tonic-gate struct hostent * 750Sstevel@tonic-gate gethostbyaddr(const void *addr, socklen_t len, int type) 760Sstevel@tonic-gate { 770Sstevel@tonic-gate nss_XbyY_buf_t *b; 780Sstevel@tonic-gate 790Sstevel@tonic-gate h_errno = 0; 800Sstevel@tonic-gate if (type == AF_INET6) 810Sstevel@tonic-gate return (getipnodebyaddr(addr, len, type, &h_errno)); 820Sstevel@tonic-gate 83*132Srobinson if ((b = GETBUF()) == 0) 84*132Srobinson return (NULL); 85*132Srobinson return (gethostbyaddr_r(addr, len, type, 86*132Srobinson b->result, b->buffer, b->buflen, &h_errno)); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate 890Sstevel@tonic-gate struct hostent * 900Sstevel@tonic-gate gethostent(void) 910Sstevel@tonic-gate { 920Sstevel@tonic-gate nss_XbyY_buf_t *b; 930Sstevel@tonic-gate 94*132Srobinson if ((b = GETBUF()) == 0) 95*132Srobinson return (NULL); 96*132Srobinson return (gethostent_r(b->result, b->buffer, b->buflen, &h_errno)); 970Sstevel@tonic-gate } 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 1010Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 1020Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 1030Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate int 1060Sstevel@tonic-gate __str2hostent(int af, const char *instr, int lenstr, void *ent, char *buffer, 1070Sstevel@tonic-gate int buflen) 1080Sstevel@tonic-gate { 1090Sstevel@tonic-gate struct hostent *host = (struct hostent *)ent; 1100Sstevel@tonic-gate const char *p, *addrstart, *limit; 1110Sstevel@tonic-gate int naddr, i, aliases_erange = 0; 1120Sstevel@tonic-gate int addrlen, res; 1130Sstevel@tonic-gate char addrbuf[100]; /* Why 100? */ 1140Sstevel@tonic-gate struct in_addr *addrp; 1150Sstevel@tonic-gate struct in6_addr *addrp6; 1160Sstevel@tonic-gate char **addrvec; 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate if ((instr >= buffer && (buffer + buflen) > instr) || 119*132Srobinson (buffer >= instr && (instr + lenstr) > buffer)) 1200Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 1210Sstevel@tonic-gate if (af != AF_INET && af != AF_INET6) { 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * XXX - Returning ERANGE here is completely bogus. 1240Sstevel@tonic-gate * Unfortunately, there's no error code identifying 1250Sstevel@tonic-gate * bogus calls from the backend (and nothing the user 1260Sstevel@tonic-gate * can do about our bugs anyway). 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* 1320Sstevel@tonic-gate * The DNS-via-YP code returns multiple lines for a key. 1330Sstevel@tonic-gate * Normal YP return values do not contain newlines (nor do 1340Sstevel@tonic-gate * lines from /etc/hosts or other sources) 1350Sstevel@tonic-gate * We count the number of newlines; this should give us 1360Sstevel@tonic-gate * the number of IP addresses specified. 1370Sstevel@tonic-gate * We'll also call the aliases code and instruct it to 1380Sstevel@tonic-gate * stop at the first newline as the remaining lines will 1390Sstevel@tonic-gate * all contain the same hostname/aliases (no aliases, unfortunately). 1400Sstevel@tonic-gate * 1410Sstevel@tonic-gate * When confronted with a string with embedded newlines, 1420Sstevel@tonic-gate * this code will take the hostname/aliases on the first line 1430Sstevel@tonic-gate * and each of the IP addresses at the start of all lines. 1440Sstevel@tonic-gate * Because the NIS protocol limits return values to 1024 bytes, 1450Sstevel@tonic-gate * we still do not get all addresses. If you want to fix 1460Sstevel@tonic-gate * that problem, do not look here. 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate p = instr; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* Strip trailing newlines */ 1520Sstevel@tonic-gate while (lenstr > 0 && p[lenstr - 1] == '\n') 1530Sstevel@tonic-gate lenstr--; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate naddr = 1; 1560Sstevel@tonic-gate limit = p + lenstr; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate for (; p < limit && (p = memchr(p, '\n', limit - p)); p++) 1590Sstevel@tonic-gate naddr++; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* Allocate space for naddr addresses and h_addr_list */ 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if (af == AF_INET6) { 1640Sstevel@tonic-gate addrp6 = (struct in6_addr *)ROUND_DOWN(buffer + buflen, 1650Sstevel@tonic-gate sizeof (*addrp6)); 1660Sstevel@tonic-gate addrp6 -= naddr; 1670Sstevel@tonic-gate addrvec = (char **)ROUND_DOWN(addrp6, sizeof (*addrvec)); 1680Sstevel@tonic-gate addrvec -= naddr + 1; 1690Sstevel@tonic-gate } else { 1700Sstevel@tonic-gate addrp = (struct in_addr *)ROUND_DOWN(buffer + buflen, 1710Sstevel@tonic-gate sizeof (*addrp)); 1720Sstevel@tonic-gate addrp -= naddr; 1730Sstevel@tonic-gate addrvec = (char **)ROUND_DOWN(addrp, sizeof (*addrvec)); 1740Sstevel@tonic-gate addrvec -= naddr + 1; 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 177*132Srobinson if ((char *)addrvec < buffer) 1780Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* For each addr, parse and get it */ 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate p = instr; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate for (i = 0; i < naddr; i ++) { 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate limit = memchr(p, '\n', lenstr - (p - instr)); 1870Sstevel@tonic-gate if (limit == NULL) 1880Sstevel@tonic-gate limit = instr + lenstr; 1890Sstevel@tonic-gate 190*132Srobinson while (p < limit && isspace(*p)) 1910Sstevel@tonic-gate p++; 1920Sstevel@tonic-gate addrstart = p; 193*132Srobinson while (p < limit && !isspace(*p)) 1940Sstevel@tonic-gate p++; 195*132Srobinson if (p >= limit) 1960Sstevel@tonic-gate /* Syntax error - no hostname present or truncated line */ 1970Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 1980Sstevel@tonic-gate addrlen = p - addrstart; 199*132Srobinson if (addrlen >= sizeof (addrbuf)) 2000Sstevel@tonic-gate /* Syntax error -- supposed IP address is too long */ 2010Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 202*132Srobinson (void) memcpy(addrbuf, addrstart, addrlen); 2030Sstevel@tonic-gate addrbuf[addrlen] = '\0'; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate if (addrlen > ((af == AF_INET6) ? INET6_ADDRSTRLEN 206*132Srobinson : INET_ADDRSTRLEN)) 2070Sstevel@tonic-gate /* Syntax error -- supposed IP address is too long */ 2080Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2090Sstevel@tonic-gate if (af == AF_INET) { 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * inet_pton() doesn't handle d.d.d, d.d, or d formats, 2120Sstevel@tonic-gate * so we must use inet_addr() for IPv4 addresses. 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate addrvec[i] = (char *)&addrp[i]; 215*132Srobinson if ((addrp[i].s_addr = inet_addr(addrbuf)) == 216*132Srobinson 0xffffffffU) 2170Sstevel@tonic-gate /* Syntax error -- bogus IPv4 address */ 2180Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2190Sstevel@tonic-gate } else { 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * In the case of AF_INET6, we can have both v4 and v6 2220Sstevel@tonic-gate * addresses, so we convert v4's to v4 mapped addresses 2230Sstevel@tonic-gate * and return them as such. 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate addrvec[i] = (char *)&addrp6[i]; 2260Sstevel@tonic-gate if (strchr(addrbuf, ':') != 0) { 227*132Srobinson if (inet_pton(af, addrbuf, &addrp6[i]) != 1) 2280Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2290Sstevel@tonic-gate } else { 2300Sstevel@tonic-gate struct in_addr in4; 231*132Srobinson if ((in4.s_addr = inet_addr(addrbuf)) == 232*132Srobinson 0xffffffffU) 2330Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2340Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(&in4, &addrp6[i]); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate /* First address, this is where we get the hostname + aliases */ 2390Sstevel@tonic-gate if (i == 0) { 2400Sstevel@tonic-gate while (p < limit && isspace(*p)) { 2410Sstevel@tonic-gate p++; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate host->h_aliases = _nss_netdb_aliases(p, limit - p, 2440Sstevel@tonic-gate buffer, ((char *)addrvec) - buffer); 2450Sstevel@tonic-gate if (host->h_aliases == NULL) 2460Sstevel@tonic-gate aliases_erange = 1; /* too big for buffer */ 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate if (limit >= instr + lenstr) 2490Sstevel@tonic-gate break; 2500Sstevel@tonic-gate else 2510Sstevel@tonic-gate p = limit + 1; /* skip NL */ 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate if (host->h_aliases == 0) { 2550Sstevel@tonic-gate if (aliases_erange) 2560Sstevel@tonic-gate res = NSS_STR_PARSE_ERANGE; 2570Sstevel@tonic-gate else 2580Sstevel@tonic-gate res = NSS_STR_PARSE_PARSE; 2590Sstevel@tonic-gate } else { 2600Sstevel@tonic-gate /* Success */ 2610Sstevel@tonic-gate host->h_name = host->h_aliases[0]; 2620Sstevel@tonic-gate host->h_aliases++; 2630Sstevel@tonic-gate res = NSS_STR_PARSE_SUCCESS; 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate /* 2660Sstevel@tonic-gate * If i < naddr, we quit the loop early and addrvec[i+1] needs NULL 2670Sstevel@tonic-gate * otherwise, we ran naddr iterations and addrvec[naddr] needs NULL 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate addrvec[i >= naddr ? naddr : i + 1] = 0; 2700Sstevel@tonic-gate if (af == AF_INET6) { 2710Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 2720Sstevel@tonic-gate } else { 2730Sstevel@tonic-gate host->h_length = sizeof (struct in_addr); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate host->h_addrtype = af; 2760Sstevel@tonic-gate host->h_addr_list = addrvec; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate return (res); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate #endif /* NSS_INCLUDE_UNSAFE */ 281