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