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 */ 217c478bd9Sstevel@tonic-gate /* 22*cb5caa98Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * All routines necessary to deal the "netmasks" database. The sources 307c478bd9Sstevel@tonic-gate * contain mappings between 32 bit Internet addresses and corresponding 317c478bd9Sstevel@tonic-gate * 32 bit Internet address masks. The addresses are in dotted internet 327c478bd9Sstevel@tonic-gate * address notation. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <stdio.h> 367c478bd9Sstevel@tonic-gate #include <ctype.h> 377c478bd9Sstevel@tonic-gate #include <string.h> 387c478bd9Sstevel@tonic-gate #include <stdlib.h> 397c478bd9Sstevel@tonic-gate #include <sys/types.h> 407c478bd9Sstevel@tonic-gate #include <sys/socket.h> 417c478bd9Sstevel@tonic-gate #include <net/if.h> 427c478bd9Sstevel@tonic-gate #include <netinet/in.h> 437c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 447c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 457c478bd9Sstevel@tonic-gate 46*cb5caa98Sdjl int str2addr(const char *, int, void *, char *, int); 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 497c478bd9Sstevel@tonic-gate 50*cb5caa98Sdjl void 517c478bd9Sstevel@tonic-gate _nss_initf_netmasks(nss_db_params_t *p) 527c478bd9Sstevel@tonic-gate { 537c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_NETMASKS; 547c478bd9Sstevel@tonic-gate p->default_config = NSS_DEFCONF_NETMASKS; 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* 587c478bd9Sstevel@tonic-gate * Print a network number such as 129.144 as well as an IP address. 597c478bd9Sstevel@tonic-gate * Assumes network byte order for both IP addresses and network numbers 607c478bd9Sstevel@tonic-gate * (Network numbers are normally passed around in host byte order). 61*cb5caa98Sdjl * to be MT safe, use a passed in buffer like otherget*_r APIs. 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate static char * 64*cb5caa98Sdjl inet_nettoa(struct in_addr in, char *result, int len) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate uint32_t addr = in.s_addr; 677c478bd9Sstevel@tonic-gate uchar_t *up = (uchar_t *)&addr; 68*cb5caa98Sdjl 69*cb5caa98Sdjl if (result == NULL) 70*cb5caa98Sdjl return (NULL); 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* Omit leading zeros */ 737c478bd9Sstevel@tonic-gate if (up[0]) { 74*cb5caa98Sdjl (void) snprintf(result, len, "%d.%d.%d.%d", 757c478bd9Sstevel@tonic-gate up[0], up[1], up[2], up[3]); 767c478bd9Sstevel@tonic-gate } else if (up[1]) { 77*cb5caa98Sdjl (void) snprintf(result, len, "%d.%d.%d", up[1], up[2], up[3]); 787c478bd9Sstevel@tonic-gate } else if (up[2]) { 79*cb5caa98Sdjl (void) snprintf(result, len, "%d.%d", up[2], up[3]); 807c478bd9Sstevel@tonic-gate } else { 81*cb5caa98Sdjl (void) snprintf(result, len, "%d", up[3]); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate return (result); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Given a 32 bit key look it up in the netmasks database 887c478bd9Sstevel@tonic-gate * based on the "netmasks" policy in /etc/nsswitch.conf. 897c478bd9Sstevel@tonic-gate * If the key is a network number with the trailing zero's removed 907c478bd9Sstevel@tonic-gate * (e.g. "192.9.200") this routine can't use inet_ntoa to convert 917c478bd9Sstevel@tonic-gate * the address to the string key. 927c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate static int 957c478bd9Sstevel@tonic-gate getnetmaskbykey(const struct in_addr addr, struct in_addr *mask) 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 987c478bd9Sstevel@tonic-gate nss_status_t res; 997c478bd9Sstevel@tonic-gate char tmp[NSS_LINELEN_NETMASKS]; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 1037c478bd9Sstevel@tonic-gate * To simplify things, we put the dotted internet address form of 1047c478bd9Sstevel@tonic-gate * the network address in the 'name' field as a filter to speed 1057c478bd9Sstevel@tonic-gate * up the lookup. 1067c478bd9Sstevel@tonic-gate */ 107*cb5caa98Sdjl if (inet_nettoa(addr, tmp, NSS_LINELEN_NETMASKS) == NULL) 108*cb5caa98Sdjl return (NSS_NOTFOUND); 109*cb5caa98Sdjl 1107c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, mask, NULL, 0, str2addr); 1117c478bd9Sstevel@tonic-gate arg.key.name = tmp; 1127c478bd9Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_netmasks, 1137c478bd9Sstevel@tonic-gate NSS_DBOP_NETMASKS_BYNET, &arg); 1147c478bd9Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 1157c478bd9Sstevel@tonic-gate return (arg.status = res); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * Given a 32 bit internet network number, it finds the corresponding netmask 1207c478bd9Sstevel@tonic-gate * address based on the "netmasks" policy in /etc/nsswitch.conf. 1217c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1227c478bd9Sstevel@tonic-gate * Check both for the (masked) network number and the shifted network 1237c478bd9Sstevel@tonic-gate * number (e.g., both "10.0.0.0" and "10"). 1247c478bd9Sstevel@tonic-gate * Assumes that the caller passes in an unshifted number (or an IP address). 1257c478bd9Sstevel@tonic-gate */ 1267c478bd9Sstevel@tonic-gate int 1277c478bd9Sstevel@tonic-gate getnetmaskbynet(const struct in_addr net, struct in_addr *mask) 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate struct in_addr net1, net2; 1307c478bd9Sstevel@tonic-gate uint32_t i; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate i = ntohl(net.s_addr); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate /* 1357c478bd9Sstevel@tonic-gate * Try looking for the network number both with and without 1367c478bd9Sstevel@tonic-gate * the trailing zeros. 1377c478bd9Sstevel@tonic-gate */ 1387c478bd9Sstevel@tonic-gate if ((i & IN_CLASSA_NET) == 0) { 1397c478bd9Sstevel@tonic-gate /* Assume already a right-shifted network number */ 1407c478bd9Sstevel@tonic-gate net2.s_addr = htonl(i); 1417c478bd9Sstevel@tonic-gate if ((i & IN_CLASSB_NET) != 0) { 1427c478bd9Sstevel@tonic-gate net1.s_addr = htonl(i << IN_CLASSC_NSHIFT); 1437c478bd9Sstevel@tonic-gate } else if ((i & IN_CLASSC_NET) != 0) { 1447c478bd9Sstevel@tonic-gate net1.s_addr = htonl(i << IN_CLASSB_NSHIFT); 1457c478bd9Sstevel@tonic-gate } else { 1467c478bd9Sstevel@tonic-gate net1.s_addr = htonl(i << IN_CLASSA_NSHIFT); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate } else if (IN_CLASSA(i)) { 1497c478bd9Sstevel@tonic-gate net1.s_addr = htonl(i & IN_CLASSA_NET); 1507c478bd9Sstevel@tonic-gate net2.s_addr = htonl(i >> IN_CLASSA_NSHIFT); 1517c478bd9Sstevel@tonic-gate } else if (IN_CLASSB(i)) { 1527c478bd9Sstevel@tonic-gate net1.s_addr = htonl(i & IN_CLASSB_NET); 1537c478bd9Sstevel@tonic-gate net2.s_addr = htonl(i >> IN_CLASSB_NSHIFT); 1547c478bd9Sstevel@tonic-gate } else { 1557c478bd9Sstevel@tonic-gate net1.s_addr = htonl(i & IN_CLASSC_NET); 1567c478bd9Sstevel@tonic-gate net2.s_addr = htonl(i >> IN_CLASSC_NSHIFT); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (getnetmaskbykey(net1, mask) == 0) { 1607c478bd9Sstevel@tonic-gate return (0); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate if (getnetmaskbykey(net2, mask) == 0) { 1637c478bd9Sstevel@tonic-gate return (0); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate return (-1); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* 1697c478bd9Sstevel@tonic-gate * Find the netmask used for an IP address. 1707c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1717c478bd9Sstevel@tonic-gate * 1727c478bd9Sstevel@tonic-gate * Support Variable Length Subnetmasks by looking for the longest 1737c478bd9Sstevel@tonic-gate * matching subnetmask in the database. 1747c478bd9Sstevel@tonic-gate * Start by looking for a match for the full IP address and 1757c478bd9Sstevel@tonic-gate * mask off one rightmost bit after another until we find a match. 1767c478bd9Sstevel@tonic-gate * Note that for a match the found netmask must match what was used 1777c478bd9Sstevel@tonic-gate * for the lookup masking. 1787c478bd9Sstevel@tonic-gate * As a fallback for compatibility finally lookup the network 1797c478bd9Sstevel@tonic-gate * number with and without the trailing zeros. 1807c478bd9Sstevel@tonic-gate * In order to suppress redundant lookups in the name service 1817c478bd9Sstevel@tonic-gate * we keep the previous lookup key and compare against it before 1827c478bd9Sstevel@tonic-gate * doing the lookup. 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate int 1857c478bd9Sstevel@tonic-gate getnetmaskbyaddr(const struct in_addr addr, struct in_addr *mask) 1867c478bd9Sstevel@tonic-gate { 1877c478bd9Sstevel@tonic-gate struct in_addr prevnet, net; 1887c478bd9Sstevel@tonic-gate uint32_t i, maskoff; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate i = ntohl(addr.s_addr); 1917c478bd9Sstevel@tonic-gate prevnet.s_addr = 0; 1927c478bd9Sstevel@tonic-gate mask->s_addr = 0; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate for (maskoff = 0xFFFFFFFF; maskoff != 0; maskoff = maskoff << 1) { 1957c478bd9Sstevel@tonic-gate net.s_addr = htonl(i & maskoff); 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (net.s_addr != prevnet.s_addr) { 1987c478bd9Sstevel@tonic-gate if (getnetmaskbykey(net, mask) != 0) { 1997c478bd9Sstevel@tonic-gate mask->s_addr = 0; 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate if (htonl(maskoff) == mask->s_addr) 2037c478bd9Sstevel@tonic-gate return (0); 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate prevnet.s_addr = net.s_addr; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * Non-VLSM fallback. 2107c478bd9Sstevel@tonic-gate * Try looking for the network number with and without the trailing 2117c478bd9Sstevel@tonic-gate * zeros. 2127c478bd9Sstevel@tonic-gate */ 2137c478bd9Sstevel@tonic-gate return (getnetmaskbynet(addr, mask)); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* 2177c478bd9Sstevel@tonic-gate * Parse netmasks entry into its components. The network address is placed 2187c478bd9Sstevel@tonic-gate * in buffer for use by check_addr for 'files' backend, to match the network 2197c478bd9Sstevel@tonic-gate * address. The network address is placed in the buffer as a network order 2207c478bd9Sstevel@tonic-gate * internet address, if buffer is non null. The network order form of the mask 2217c478bd9Sstevel@tonic-gate * itself is placed in 'ent'. 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate int 2247c478bd9Sstevel@tonic-gate str2addr(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 2257c478bd9Sstevel@tonic-gate { 2267c478bd9Sstevel@tonic-gate int retval; 2277c478bd9Sstevel@tonic-gate struct in_addr *mask = (struct in_addr *)ent; 2287c478bd9Sstevel@tonic-gate const char *p, *limit, *start; 2297c478bd9Sstevel@tonic-gate struct in_addr addr; 2307c478bd9Sstevel@tonic-gate int i; 2317c478bd9Sstevel@tonic-gate char tmp[NSS_LINELEN_NETMASKS]; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate p = instr; 2347c478bd9Sstevel@tonic-gate limit = p + lenstr; 2357c478bd9Sstevel@tonic-gate retval = NSS_STR_PARSE_PARSE; 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) /* skip leading whitespace */ 2387c478bd9Sstevel@tonic-gate p++; 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate if (buffer) { /* for 'files' backend verification */ 2417c478bd9Sstevel@tonic-gate for (start = p, i = 0; p < limit && !isspace(*p); p++) 2427c478bd9Sstevel@tonic-gate i++; 2437c478bd9Sstevel@tonic-gate if (p < limit && i < buflen) { 2447c478bd9Sstevel@tonic-gate (void) memcpy(tmp, start, i); 2457c478bd9Sstevel@tonic-gate tmp[i] = '\0'; 2467c478bd9Sstevel@tonic-gate addr.s_addr = inet_addr(tmp); 2477c478bd9Sstevel@tonic-gate /* Addr will always be an ipv4 address (32bits) */ 2487c478bd9Sstevel@tonic-gate if (addr.s_addr == 0xffffffffUL) 2497c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2507c478bd9Sstevel@tonic-gate else { 2517c478bd9Sstevel@tonic-gate (void) memcpy(buffer, (char *)&addr, 2527c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate } else 2557c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) /* skip intermediate */ 2597c478bd9Sstevel@tonic-gate p++; 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate if (mask) { 2627c478bd9Sstevel@tonic-gate for (start = p, i = 0; p < limit && !isspace(*p); p++) 2637c478bd9Sstevel@tonic-gate i++; 2647c478bd9Sstevel@tonic-gate if (p <= limit) { 2657c478bd9Sstevel@tonic-gate if ((i + 1) > NSS_LINELEN_NETMASKS) 2667c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 2677c478bd9Sstevel@tonic-gate (void) memcpy(tmp, start, i); 2687c478bd9Sstevel@tonic-gate tmp[i] = '\0'; 2697c478bd9Sstevel@tonic-gate addr.s_addr = inet_addr(tmp); 2707c478bd9Sstevel@tonic-gate /* Addr will always be an ipv4 address (32bits) */ 2717c478bd9Sstevel@tonic-gate if (addr.s_addr == 0xffffffffUL) 2727c478bd9Sstevel@tonic-gate retval = NSS_STR_PARSE_PARSE; 2737c478bd9Sstevel@tonic-gate else { 2747c478bd9Sstevel@tonic-gate mask->s_addr = addr.s_addr; 2757c478bd9Sstevel@tonic-gate retval = NSS_STR_PARSE_SUCCESS; 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate return (retval); 2817c478bd9Sstevel@tonic-gate } 282