17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*cb5caa98Sdjl * Common Development and Distribution License (the "License"). 6*cb5caa98Sdjl * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2161961e0fSrobinson 227c478bd9Sstevel@tonic-gate /* 23e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25e8031f0aSraf */ 26e8031f0aSraf 27e8031f0aSraf /* 287c478bd9Sstevel@tonic-gate * Ye olde non-reentrant interface (MT-unsafe, caveat utor) 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 33e8031f0aSraf #include "mt.h" 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <ctype.h> 367c478bd9Sstevel@tonic-gate #include <string.h> 377c478bd9Sstevel@tonic-gate #include <strings.h> 387c478bd9Sstevel@tonic-gate #include <netdb.h> 397c478bd9Sstevel@tonic-gate #include <stdio.h> 407c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 417c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 427c478bd9Sstevel@tonic-gate #include <netinet/in.h> 437c478bd9Sstevel@tonic-gate #include <sys/socket.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * Still just a global. If you want per-thread h_errno, 477c478bd9Sstevel@tonic-gate * use the reentrant interfaces (gethostbyname_r et al) 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate int h_errno; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #ifdef NSS_INCLUDE_UNSAFE 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * Don't free this, even on an endhostent(), because bitter experience shows 557c478bd9Sstevel@tonic-gate * that there's production code that does getXXXbyYYY(), then endXXXent(), 567c478bd9Sstevel@tonic-gate * and then continues to use the pointer it got back. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *buffer; 597c478bd9Sstevel@tonic-gate #define GETBUF() \ 607c478bd9Sstevel@tonic-gate NSS_XbyY_ALLOC(&buffer, sizeof (struct hostent), NSS_BUFLEN_HOSTS) 617c478bd9Sstevel@tonic-gate /* === ?? set ENOMEM on failure? */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate struct hostent * 647c478bd9Sstevel@tonic-gate gethostbyname(const char *nam) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *b; 677c478bd9Sstevel@tonic-gate 6861961e0fSrobinson if ((b = GETBUF()) == 0) 6961961e0fSrobinson return (NULL); 7061961e0fSrobinson return (gethostbyname_r(nam, b->result, b->buffer, b->buflen, 7161961e0fSrobinson &h_errno)); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate struct hostent * 757c478bd9Sstevel@tonic-gate gethostbyaddr(const void *addr, socklen_t len, int type) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *b; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate h_errno = 0; 807c478bd9Sstevel@tonic-gate if (type == AF_INET6) 817c478bd9Sstevel@tonic-gate return (getipnodebyaddr(addr, len, type, &h_errno)); 827c478bd9Sstevel@tonic-gate 8361961e0fSrobinson if ((b = GETBUF()) == 0) 8461961e0fSrobinson return (NULL); 8561961e0fSrobinson return (gethostbyaddr_r(addr, len, type, 8661961e0fSrobinson b->result, b->buffer, b->buflen, &h_errno)); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate struct hostent * 907c478bd9Sstevel@tonic-gate gethostent(void) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *b; 937c478bd9Sstevel@tonic-gate 9461961e0fSrobinson if ((b = GETBUF()) == 0) 9561961e0fSrobinson return (NULL); 9661961e0fSrobinson return (gethostent_r(b->result, b->buffer, b->buflen, &h_errno)); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 1017c478bd9Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 1027c478bd9Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 1037c478bd9Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate int 1067c478bd9Sstevel@tonic-gate __str2hostent(int af, const char *instr, int lenstr, void *ent, char *buffer, 1077c478bd9Sstevel@tonic-gate int buflen) 1087c478bd9Sstevel@tonic-gate { 1097c478bd9Sstevel@tonic-gate struct hostent *host = (struct hostent *)ent; 1107c478bd9Sstevel@tonic-gate const char *p, *addrstart, *limit; 1117c478bd9Sstevel@tonic-gate int naddr, i, aliases_erange = 0; 1127c478bd9Sstevel@tonic-gate int addrlen, res; 1137c478bd9Sstevel@tonic-gate char addrbuf[100]; /* Why 100? */ 1147c478bd9Sstevel@tonic-gate struct in_addr *addrp; 1157c478bd9Sstevel@tonic-gate struct in6_addr *addrp6; 1167c478bd9Sstevel@tonic-gate char **addrvec; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if ((instr >= buffer && (buffer + buflen) > instr) || 11961961e0fSrobinson (buffer >= instr && (instr + lenstr) > buffer)) 1207c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 1217c478bd9Sstevel@tonic-gate if (af != AF_INET && af != AF_INET6) { 1227c478bd9Sstevel@tonic-gate /* 1237c478bd9Sstevel@tonic-gate * XXX - Returning ERANGE here is completely bogus. 1247c478bd9Sstevel@tonic-gate * Unfortunately, there's no error code identifying 1257c478bd9Sstevel@tonic-gate * bogus calls from the backend (and nothing the user 1267c478bd9Sstevel@tonic-gate * can do about our bugs anyway). 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * The DNS-via-YP code returns multiple lines for a key. 1337c478bd9Sstevel@tonic-gate * Normal YP return values do not contain newlines (nor do 1347c478bd9Sstevel@tonic-gate * lines from /etc/hosts or other sources) 1357c478bd9Sstevel@tonic-gate * We count the number of newlines; this should give us 1367c478bd9Sstevel@tonic-gate * the number of IP addresses specified. 1377c478bd9Sstevel@tonic-gate * We'll also call the aliases code and instruct it to 1387c478bd9Sstevel@tonic-gate * stop at the first newline as the remaining lines will 1397c478bd9Sstevel@tonic-gate * all contain the same hostname/aliases (no aliases, unfortunately). 1407c478bd9Sstevel@tonic-gate * 1417c478bd9Sstevel@tonic-gate * When confronted with a string with embedded newlines, 1427c478bd9Sstevel@tonic-gate * this code will take the hostname/aliases on the first line 1437c478bd9Sstevel@tonic-gate * and each of the IP addresses at the start of all lines. 1447c478bd9Sstevel@tonic-gate * Because the NIS protocol limits return values to 1024 bytes, 1457c478bd9Sstevel@tonic-gate * we still do not get all addresses. If you want to fix 1467c478bd9Sstevel@tonic-gate * that problem, do not look here. 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate p = instr; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* Strip trailing newlines */ 1527c478bd9Sstevel@tonic-gate while (lenstr > 0 && p[lenstr - 1] == '\n') 1537c478bd9Sstevel@tonic-gate lenstr--; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate naddr = 1; 1567c478bd9Sstevel@tonic-gate limit = p + lenstr; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate for (; p < limit && (p = memchr(p, '\n', limit - p)); p++) 1597c478bd9Sstevel@tonic-gate naddr++; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate /* Allocate space for naddr addresses and h_addr_list */ 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (af == AF_INET6) { 1647c478bd9Sstevel@tonic-gate addrp6 = (struct in6_addr *)ROUND_DOWN(buffer + buflen, 1657c478bd9Sstevel@tonic-gate sizeof (*addrp6)); 1667c478bd9Sstevel@tonic-gate addrp6 -= naddr; 1677c478bd9Sstevel@tonic-gate addrvec = (char **)ROUND_DOWN(addrp6, sizeof (*addrvec)); 1687c478bd9Sstevel@tonic-gate addrvec -= naddr + 1; 1697c478bd9Sstevel@tonic-gate } else { 1707c478bd9Sstevel@tonic-gate addrp = (struct in_addr *)ROUND_DOWN(buffer + buflen, 1717c478bd9Sstevel@tonic-gate sizeof (*addrp)); 1727c478bd9Sstevel@tonic-gate addrp -= naddr; 1737c478bd9Sstevel@tonic-gate addrvec = (char **)ROUND_DOWN(addrp, sizeof (*addrvec)); 1747c478bd9Sstevel@tonic-gate addrvec -= naddr + 1; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 17761961e0fSrobinson if ((char *)addrvec < buffer) 1787c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* For each addr, parse and get it */ 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate p = instr; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate for (i = 0; i < naddr; i ++) { 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate limit = memchr(p, '\n', lenstr - (p - instr)); 1877c478bd9Sstevel@tonic-gate if (limit == NULL) 1887c478bd9Sstevel@tonic-gate limit = instr + lenstr; 1897c478bd9Sstevel@tonic-gate 19061961e0fSrobinson while (p < limit && isspace(*p)) 1917c478bd9Sstevel@tonic-gate p++; 1927c478bd9Sstevel@tonic-gate addrstart = p; 19361961e0fSrobinson while (p < limit && !isspace(*p)) 1947c478bd9Sstevel@tonic-gate p++; 19561961e0fSrobinson if (p >= limit) 1967c478bd9Sstevel@tonic-gate /* Syntax error - no hostname present or truncated line */ 1977c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 1987c478bd9Sstevel@tonic-gate addrlen = p - addrstart; 19961961e0fSrobinson if (addrlen >= sizeof (addrbuf)) 2007c478bd9Sstevel@tonic-gate /* Syntax error -- supposed IP address is too long */ 2017c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 20261961e0fSrobinson (void) memcpy(addrbuf, addrstart, addrlen); 2037c478bd9Sstevel@tonic-gate addrbuf[addrlen] = '\0'; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate if (addrlen > ((af == AF_INET6) ? INET6_ADDRSTRLEN 20661961e0fSrobinson : INET_ADDRSTRLEN)) 2077c478bd9Sstevel@tonic-gate /* Syntax error -- supposed IP address is too long */ 2087c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2097c478bd9Sstevel@tonic-gate if (af == AF_INET) { 2107c478bd9Sstevel@tonic-gate /* 2117c478bd9Sstevel@tonic-gate * inet_pton() doesn't handle d.d.d, d.d, or d formats, 2127c478bd9Sstevel@tonic-gate * so we must use inet_addr() for IPv4 addresses. 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate addrvec[i] = (char *)&addrp[i]; 21561961e0fSrobinson if ((addrp[i].s_addr = inet_addr(addrbuf)) == 21661961e0fSrobinson 0xffffffffU) 2177c478bd9Sstevel@tonic-gate /* Syntax error -- bogus IPv4 address */ 2187c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2197c478bd9Sstevel@tonic-gate } else { 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * In the case of AF_INET6, we can have both v4 and v6 2227c478bd9Sstevel@tonic-gate * addresses, so we convert v4's to v4 mapped addresses 2237c478bd9Sstevel@tonic-gate * and return them as such. 2247c478bd9Sstevel@tonic-gate */ 2257c478bd9Sstevel@tonic-gate addrvec[i] = (char *)&addrp6[i]; 2267c478bd9Sstevel@tonic-gate if (strchr(addrbuf, ':') != 0) { 22761961e0fSrobinson if (inet_pton(af, addrbuf, &addrp6[i]) != 1) 2287c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2297c478bd9Sstevel@tonic-gate } else { 2307c478bd9Sstevel@tonic-gate struct in_addr in4; 23161961e0fSrobinson if ((in4.s_addr = inet_addr(addrbuf)) == 23261961e0fSrobinson 0xffffffffU) 2337c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2347c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(&in4, &addrp6[i]); 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* First address, this is where we get the hostname + aliases */ 2397c478bd9Sstevel@tonic-gate if (i == 0) { 2407c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) { 2417c478bd9Sstevel@tonic-gate p++; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate host->h_aliases = _nss_netdb_aliases(p, limit - p, 2447c478bd9Sstevel@tonic-gate buffer, ((char *)addrvec) - buffer); 2457c478bd9Sstevel@tonic-gate if (host->h_aliases == NULL) 2467c478bd9Sstevel@tonic-gate aliases_erange = 1; /* too big for buffer */ 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate if (limit >= instr + lenstr) 2497c478bd9Sstevel@tonic-gate break; 2507c478bd9Sstevel@tonic-gate else 2517c478bd9Sstevel@tonic-gate p = limit + 1; /* skip NL */ 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if (host->h_aliases == 0) { 2557c478bd9Sstevel@tonic-gate if (aliases_erange) 2567c478bd9Sstevel@tonic-gate res = NSS_STR_PARSE_ERANGE; 2577c478bd9Sstevel@tonic-gate else 2587c478bd9Sstevel@tonic-gate res = NSS_STR_PARSE_PARSE; 2597c478bd9Sstevel@tonic-gate } else { 2607c478bd9Sstevel@tonic-gate /* Success */ 2617c478bd9Sstevel@tonic-gate host->h_name = host->h_aliases[0]; 2627c478bd9Sstevel@tonic-gate host->h_aliases++; 2637c478bd9Sstevel@tonic-gate res = NSS_STR_PARSE_SUCCESS; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate /* 2667c478bd9Sstevel@tonic-gate * If i < naddr, we quit the loop early and addrvec[i+1] needs NULL 2677c478bd9Sstevel@tonic-gate * otherwise, we ran naddr iterations and addrvec[naddr] needs NULL 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate addrvec[i >= naddr ? naddr : i + 1] = 0; 2707c478bd9Sstevel@tonic-gate if (af == AF_INET6) { 2717c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in6_addr); 2727c478bd9Sstevel@tonic-gate } else { 2737c478bd9Sstevel@tonic-gate host->h_length = sizeof (struct in_addr); 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate host->h_addrtype = af; 2767c478bd9Sstevel@tonic-gate host->h_addr_list = addrvec; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate return (res); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate #endif /* NSS_INCLUDE_UNSAFE */ 281