2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * All routines necessary to deal the "netmasks" database. The sources
2N/A * contain mappings between 32 bit Internet addresses and corresponding
2N/A * 32 bit Internet address masks. The addresses are in dotted internet
2N/A * address notation.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <ctype.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <net/if.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <nss_dbdefs.h>
2N/A
2N/Aint str2addr(const char *, int, void *, char *, int);
2N/A
2N/Astatic DEFINE_NSS_DB_ROOT(db_root);
2N/A
2N/Avoid
2N/A_nss_initf_netmasks(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_NETMASKS;
2N/A p->default_config = NSS_DEFCONF_NETMASKS;
2N/A}
2N/A
2N/A/*
2N/A * Print a network number such as 129.144 as well as an IP address.
2N/A * Assumes network byte order for both IP addresses and network numbers
2N/A * (Network numbers are normally passed around in host byte order).
2N/A * to be MT safe, use a passed in buffer like otherget*_r APIs.
2N/A */
2N/Astatic char *
2N/Ainet_nettoa(struct in_addr in, char *result, int len)
2N/A{
2N/A uint32_t addr = in.s_addr;
2N/A uchar_t *up = (uchar_t *)&addr;
2N/A
2N/A if (result == NULL)
2N/A return (NULL);
2N/A
2N/A /* Omit leading zeros */
2N/A if (up[0]) {
2N/A (void) snprintf(result, len, "%d.%d.%d.%d",
2N/A up[0], up[1], up[2], up[3]);
2N/A } else if (up[1]) {
2N/A (void) snprintf(result, len, "%d.%d.%d", up[1], up[2], up[3]);
2N/A } else if (up[2]) {
2N/A (void) snprintf(result, len, "%d.%d", up[2], up[3]);
2N/A } else {
2N/A (void) snprintf(result, len, "%d", up[3]);
2N/A }
2N/A return (result);
2N/A}
2N/A
2N/A/*
2N/A * Given a 32 bit key look it up in the netmasks database
2N/A * based on the "netmasks" policy in /etc/nsswitch.conf.
2N/A * If the key is a network number with the trailing zero's removed
2N/A * (e.g. "192.9.200") this routine can't use inet_ntoa to convert
2N/A * the address to the string key.
2N/A * Returns zero if successful, non-zero otherwise.
2N/A */
2N/Astatic int
2N/Agetnetmaskbykey(const struct in_addr addr, struct in_addr *mask)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A char tmp[NSS_LINELEN_NETMASKS];
2N/A
2N/A /*
2N/A * let the backend do the allocation to store stuff for parsing.
2N/A * To simplify things, we put the dotted internet address form of
2N/A * the network address in the 'name' field as a filter to speed
2N/A * up the lookup.
2N/A */
2N/A if (inet_nettoa(addr, tmp, NSS_LINELEN_NETMASKS) == NULL)
2N/A return (NSS_NOTFOUND);
2N/A
2N/A NSS_XbyY_INIT(&arg, mask, NULL, 0, str2addr);
2N/A arg.key.name = tmp;
2N/A res = nss_search(&db_root, _nss_initf_netmasks,
2N/A NSS_DBOP_NETMASKS_BYNET, &arg);
2N/A (void) NSS_XbyY_FINI(&arg);
2N/A return (arg.status = res);
2N/A}
2N/A
2N/A/*
2N/A * Given a 32 bit internet network number, it finds the corresponding netmask
2N/A * address based on the "netmasks" policy in /etc/nsswitch.conf.
2N/A * Returns zero if successful, non-zero otherwise.
2N/A * Check both for the (masked) network number and the shifted network
2N/A * number (e.g., both "10.0.0.0" and "10").
2N/A * Assumes that the caller passes in an unshifted number (or an IP address).
2N/A */
2N/Aint
2N/Agetnetmaskbynet(const struct in_addr net, struct in_addr *mask)
2N/A{
2N/A struct in_addr net1, net2;
2N/A uint32_t i;
2N/A
2N/A i = ntohl(net.s_addr);
2N/A
2N/A /*
2N/A * Try looking for the network number both with and without
2N/A * the trailing zeros.
2N/A */
2N/A if ((i & IN_CLASSA_NET) == 0) {
2N/A /* Assume already a right-shifted network number */
2N/A net2.s_addr = htonl(i);
2N/A if ((i & IN_CLASSB_NET) != 0) {
2N/A net1.s_addr = htonl(i << IN_CLASSC_NSHIFT);
2N/A } else if ((i & IN_CLASSC_NET) != 0) {
2N/A net1.s_addr = htonl(i << IN_CLASSB_NSHIFT);
2N/A } else {
2N/A net1.s_addr = htonl(i << IN_CLASSA_NSHIFT);
2N/A }
2N/A } else if (IN_CLASSA(i)) {
2N/A net1.s_addr = htonl(i & IN_CLASSA_NET);
2N/A net2.s_addr = htonl(i >> IN_CLASSA_NSHIFT);
2N/A } else if (IN_CLASSB(i)) {
2N/A net1.s_addr = htonl(i & IN_CLASSB_NET);
2N/A net2.s_addr = htonl(i >> IN_CLASSB_NSHIFT);
2N/A } else {
2N/A net1.s_addr = htonl(i & IN_CLASSC_NET);
2N/A net2.s_addr = htonl(i >> IN_CLASSC_NSHIFT);
2N/A }
2N/A
2N/A if (getnetmaskbykey(net1, mask) == 0) {
2N/A return (0);
2N/A }
2N/A if (getnetmaskbykey(net2, mask) == 0) {
2N/A return (0);
2N/A }
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * Find the netmask used for an IP address.
2N/A * Returns zero if successful, non-zero otherwise.
2N/A *
2N/A * Support Variable Length Subnetmasks by looking for the longest
2N/A * matching subnetmask in the database.
2N/A * Start by looking for a match for the full IP address and
2N/A * mask off one rightmost bit after another until we find a match.
2N/A * Note that for a match the found netmask must match what was used
2N/A * for the lookup masking.
2N/A * As a fallback for compatibility finally lookup the network
2N/A * number with and without the trailing zeros.
2N/A * In order to suppress redundant lookups in the name service
2N/A * we keep the previous lookup key and compare against it before
2N/A * doing the lookup.
2N/A */
2N/Aint
2N/Agetnetmaskbyaddr(const struct in_addr addr, struct in_addr *mask)
2N/A{
2N/A struct in_addr prevnet, net;
2N/A uint32_t i, maskoff;
2N/A
2N/A i = ntohl(addr.s_addr);
2N/A prevnet.s_addr = 0;
2N/A mask->s_addr = 0;
2N/A
2N/A for (maskoff = 0xFFFFFFFF; maskoff != 0; maskoff = maskoff << 1) {
2N/A net.s_addr = htonl(i & maskoff);
2N/A
2N/A if (net.s_addr != prevnet.s_addr) {
2N/A if (getnetmaskbykey(net, mask) != 0) {
2N/A mask->s_addr = 0;
2N/A }
2N/A }
2N/A if (htonl(maskoff) == mask->s_addr)
2N/A return (0);
2N/A
2N/A prevnet.s_addr = net.s_addr;
2N/A }
2N/A
2N/A /*
2N/A * Non-VLSM fallback.
2N/A * Try looking for the network number with and without the trailing
2N/A * zeros.
2N/A */
2N/A return (getnetmaskbynet(addr, mask));
2N/A}
2N/A
2N/A/*
2N/A * Parse netmasks entry into its components. The network address is placed
2N/A * in buffer for use by check_addr for 'files' backend, to match the network
2N/A * address. The network address is placed in the buffer as a network order
2N/A * internet address, if buffer is non null. The network order form of the mask
2N/A * itself is placed in 'ent'.
2N/A */
2N/Aint
2N/Astr2addr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2N/A{
2N/A int retval;
2N/A struct in_addr *mask = (struct in_addr *)ent;
2N/A const char *p, *limit, *start;
2N/A struct in_addr addr;
2N/A int i;
2N/A char tmp[NSS_LINELEN_NETMASKS];
2N/A
2N/A p = instr;
2N/A limit = p + lenstr;
2N/A retval = NSS_STR_PARSE_PARSE;
2N/A
2N/A while (p < limit && isspace(*p)) /* skip leading whitespace */
2N/A p++;
2N/A
2N/A if (buffer) { /* for 'files' backend verification */
2N/A for (start = p, i = 0; p < limit && !isspace(*p); p++)
2N/A i++;
2N/A if (p < limit && i < buflen) {
2N/A (void) memcpy(tmp, start, i);
2N/A tmp[i] = '\0';
2N/A addr.s_addr = inet_addr(tmp);
2N/A /* Addr will always be an ipv4 address (32bits) */
2N/A if (addr.s_addr == 0xffffffffUL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A else {
2N/A (void) memcpy(buffer, (char *)&addr,
2N/A sizeof (struct in_addr));
2N/A }
2N/A } else
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A }
2N/A
2N/A while (p < limit && isspace(*p)) /* skip intermediate */
2N/A p++;
2N/A
2N/A if (mask) {
2N/A for (start = p, i = 0; p < limit && !isspace(*p); p++)
2N/A i++;
2N/A if (p <= limit) {
2N/A if ((i + 1) > NSS_LINELEN_NETMASKS)
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A (void) memcpy(tmp, start, i);
2N/A tmp[i] = '\0';
2N/A addr.s_addr = inet_addr(tmp);
2N/A /* Addr will always be an ipv4 address (32bits) */
2N/A if (addr.s_addr == 0xffffffffUL)
2N/A retval = NSS_STR_PARSE_PARSE;
2N/A else {
2N/A mask->s_addr = addr.s_addr;
2N/A retval = NSS_STR_PARSE_SUCCESS;
2N/A }
2N/A }
2N/A }
2N/A
2N/A return (retval);
2N/A}