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