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 (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * files/netmasks.c -- "files" backend for nsswitch "netmasks" database
2N/A */
2N/A
2N/A/*
2N/A * All routines necessary to deal with the file /etc/inet/netmasks. The file
2N/A * contains mappings from 32 bit network internet addresses to their
2N/A * corresponding 32 bit mask internet addresses. The addresses are in dotted
2N/A * internet address form.
2N/A */
2N/A
2N/A#include "files_common.h"
2N/A#include <string.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#include <ctype.h>
2N/A
2N/A/*
2N/A * Validate 'files' netmasks entry. The comparison objects are in IPv4
2N/A * internet address format.
2N/A */
2N/Astatic int
2N/Acheck_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
2N/A{
2N/A const char *limit, *linep, *addrstart;
2N/A int addrlen;
2N/A char addrbuf[NSS_LINELEN_NETMASKS];
2N/A struct in_addr lineaddr, argsaddr;
2N/A
2N/A linep = line;
2N/A limit = line + linelen;
2N/A
2N/A /* skip leading spaces */
2N/A while (linep < limit && isspace(*linep))
2N/A linep++;
2N/A
2N/A addrstart = linep;
2N/A while (linep < limit && !isspace(*linep))
2N/A linep++;
2N/A if (linep == limit)
2N/A return (0);
2N/A addrlen = linep - addrstart;
2N/A if (addrlen < sizeof (addrbuf)) {
2N/A (void) memcpy(addrbuf, addrstart, addrlen);
2N/A addrbuf[addrlen] = '\0';
2N/A if ((lineaddr.s_addr = inet_addr(addrbuf)) ==
2N/A (in_addr_t)0xffffffffU)
2N/A return (0);
2N/A if ((argsaddr.s_addr = inet_addr(argp->key.name))
2N/A == (in_addr_t)0xffffffffU)
2N/A return (0);
2N/A return (lineaddr.s_addr == argsaddr.s_addr);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Astatic nss_status_t
2N/Agetbynet(be, a)
2N/A files_backend_ptr_t be;
2N/A void *a;
2N/A{
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A nss_status_t res;
2N/A char tmpbuf[NSS_LINELEN_NETMASKS];
2N/A
2N/A /*
2N/A * use the buffer passed in if result is to be returned
2N/A * in /etc file format
2N/A */
2N/A if (argp->buf.result != NULL) {
2N/A argp->buf.buffer = tmpbuf;
2N/A argp->buf.buflen = NSS_LINELEN_NETMASKS;
2N/A }
2N/A res = _nss_files_XY_all(be, argp, 1, argp->key.name, check_addr);
2N/A if (argp->buf.result != NULL) {
2N/A argp->buf.buffer = NULL;
2N/A argp->buf.buflen = 0;
2N/A } else {
2N/A /* the frontend expects the netmask data only */
2N/A if (res == NSS_SUCCESS) {
2N/A char *m;
2N/A char *s = (char *)argp->returnval;
2N/A int l = 0;
2N/A
2N/A m = s + argp->returnlen - 1;
2N/A
2N/A /* skip trailing spaces */
2N/A while (s < m && isspace(*m))
2N/A m--;
2N/A
2N/A for (; s <= m; m--) {
2N/A if (isspace(*m))
2N/A break;
2N/A l++;
2N/A }
2N/A m++;
2N/A (void) memmove(argp->returnval, m, l);
2N/A argp->returnlen = l;
2N/A *(s + l) = '\0';
2N/A }
2N/A }
2N/A
2N/A return (res);
2N/A}
2N/A
2N/Astatic files_backend_op_t netmasks_ops[] = {
2N/A _nss_files_destr,
2N/A getbynet
2N/A};
2N/A
2N/A/*ARGSUSED*/
2N/Anss_backend_t *
2N/A_nss_files_netmasks_constr(dummy1, dummy2, dummy3)
2N/A const char *dummy1, *dummy2, *dummy3;
2N/A{
2N/A return (_nss_files_constr(netmasks_ops,
2N/A sizeof (netmasks_ops) / sizeof (netmasks_ops[0]),
2N/A _PATH_NETMASKS, NSS_LINELEN_NETMASKS, NULL, 0));
2N/A}