10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*2830Sdjl * Common Development and Distribution License (the "License"). 6*2830Sdjl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23*2830Sdjl * Use is subject to license terms. 240Sstevel@tonic-gate * 25*2830Sdjl * files/netmasks.c -- "files" backend for nsswitch "netmasks" database 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * All routines necessary to deal with the file /etc/inet/netmasks. The file 320Sstevel@tonic-gate * contains mappings from 32 bit network internet addresses to their 330Sstevel@tonic-gate * corresponding 32 bit mask internet addresses. The addresses are in dotted 340Sstevel@tonic-gate * internet address form. 350Sstevel@tonic-gate */ 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include "files_common.h" 380Sstevel@tonic-gate #include <string.h> 390Sstevel@tonic-gate #include <sys/types.h> 400Sstevel@tonic-gate #include <sys/socket.h> 410Sstevel@tonic-gate #include <net/if.h> 420Sstevel@tonic-gate #include <netinet/in.h> 430Sstevel@tonic-gate #include <arpa/inet.h> 440Sstevel@tonic-gate #include <nss_dbdefs.h> 45*2830Sdjl #include <ctype.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* 480Sstevel@tonic-gate * Validate 'files' netmasks entry. The comparison objects are in IPv4 490Sstevel@tonic-gate * internet address format. 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate static int 52*2830Sdjl check_addr(nss_XbyY_args_t *argp, const char *line, int linelen) 530Sstevel@tonic-gate { 54*2830Sdjl const char *limit, *linep, *addrstart; 55*2830Sdjl int addrlen; 56*2830Sdjl char addrbuf[NSS_LINELEN_NETMASKS]; 57*2830Sdjl struct in_addr lineaddr, argsaddr; 58*2830Sdjl 59*2830Sdjl linep = line; 60*2830Sdjl limit = line + linelen; 61*2830Sdjl 62*2830Sdjl /* skip leading spaces */ 63*2830Sdjl while (linep < limit && isspace(*linep)) 64*2830Sdjl linep++; 650Sstevel@tonic-gate 66*2830Sdjl addrstart = linep; 67*2830Sdjl while (linep < limit && !isspace(*linep)) 68*2830Sdjl linep++; 69*2830Sdjl if (linep == limit) 70*2830Sdjl return (0); 71*2830Sdjl addrlen = linep - addrstart; 72*2830Sdjl if (addrlen < sizeof (addrbuf)) { 73*2830Sdjl (void) memcpy(addrbuf, addrstart, addrlen); 74*2830Sdjl addrbuf[addrlen] = '\0'; 75*2830Sdjl if ((lineaddr.s_addr = inet_addr(addrbuf)) == 76*2830Sdjl (in_addr_t)0xffffffffU) 77*2830Sdjl return (0); 78*2830Sdjl if ((argsaddr.s_addr = inet_addr(argp->key.name)) 79*2830Sdjl == (in_addr_t)0xffffffffU) 80*2830Sdjl return (0); 81*2830Sdjl return (lineaddr.s_addr == argsaddr.s_addr); 82*2830Sdjl } 83*2830Sdjl return (0); 840Sstevel@tonic-gate } 850Sstevel@tonic-gate 860Sstevel@tonic-gate static nss_status_t 870Sstevel@tonic-gate getbynet(be, a) 880Sstevel@tonic-gate files_backend_ptr_t be; 890Sstevel@tonic-gate void *a; 900Sstevel@tonic-gate { 91*2830Sdjl nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 920Sstevel@tonic-gate nss_status_t res; 930Sstevel@tonic-gate char tmpbuf[NSS_LINELEN_NETMASKS]; 940Sstevel@tonic-gate 95*2830Sdjl /* 96*2830Sdjl * use the buffer passed in if result is to be returned 97*2830Sdjl * in /etc file format 98*2830Sdjl */ 99*2830Sdjl if (argp->buf.result != NULL) { 100*2830Sdjl argp->buf.buffer = tmpbuf; 101*2830Sdjl argp->buf.buflen = NSS_LINELEN_NETMASKS; 102*2830Sdjl } 1030Sstevel@tonic-gate res = _nss_files_XY_all(be, argp, 0, argp->key.name, check_addr); 104*2830Sdjl if (argp->buf.result != NULL) { 105*2830Sdjl argp->buf.buffer = NULL; 106*2830Sdjl argp->buf.buflen = 0; 107*2830Sdjl } else { 108*2830Sdjl /* the frontend expects the netmask data only */ 109*2830Sdjl if (res == NSS_SUCCESS) { 110*2830Sdjl char *m; 111*2830Sdjl char *s = (char *)argp->returnval; 112*2830Sdjl int l = 0; 113*2830Sdjl 114*2830Sdjl m = s + argp->returnlen - 1; 115*2830Sdjl 116*2830Sdjl /* skip trailing spaces */ 117*2830Sdjl while (s < m && isspace(*m)) 118*2830Sdjl m--; 119*2830Sdjl 120*2830Sdjl for (; s <= m; m--) { 121*2830Sdjl if (isspace(*m)) 122*2830Sdjl break; 123*2830Sdjl l++; 124*2830Sdjl } 125*2830Sdjl m++; 126*2830Sdjl (void) memmove(argp->returnval, m, l); 127*2830Sdjl argp->returnlen = l; 128*2830Sdjl *(s + l) = '\0'; 129*2830Sdjl } 130*2830Sdjl } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate return (res); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate static files_backend_op_t netmasks_ops[] = { 1360Sstevel@tonic-gate _nss_files_destr, 1370Sstevel@tonic-gate getbynet 1380Sstevel@tonic-gate }; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /*ARGSUSED*/ 1410Sstevel@tonic-gate nss_backend_t * 1420Sstevel@tonic-gate _nss_files_netmasks_constr(dummy1, dummy2, dummy3) 1430Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3; 1440Sstevel@tonic-gate { 1450Sstevel@tonic-gate return (_nss_files_constr(netmasks_ops, 1460Sstevel@tonic-gate sizeof (netmasks_ops) / sizeof (netmasks_ops[0]), 1470Sstevel@tonic-gate _PATH_NETMASKS, NSS_LINELEN_NETMASKS, NULL)); 1480Sstevel@tonic-gate } 149