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
5cb5caa98Sdjl * Common Development and Distribution License (the "License").
6cb5caa98Sdjl * 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*767b0abfSmichen * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23cb5caa98Sdjl * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate *
25cb5caa98Sdjl * files/netmasks.c -- "files" backend for nsswitch "netmasks" database
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * All routines necessary to deal with the file /etc/inet/netmasks. The file
307c478bd9Sstevel@tonic-gate * contains mappings from 32 bit network internet addresses to their
317c478bd9Sstevel@tonic-gate * corresponding 32 bit mask internet addresses. The addresses are in dotted
327c478bd9Sstevel@tonic-gate * internet address form.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include "files_common.h"
367c478bd9Sstevel@tonic-gate #include <string.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>
43cb5caa98Sdjl #include <ctype.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * Validate 'files' netmasks entry. The comparison objects are in IPv4
477c478bd9Sstevel@tonic-gate * internet address format.
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate static int
check_addr(nss_XbyY_args_t * argp,const char * line,int linelen)50cb5caa98Sdjl check_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
517c478bd9Sstevel@tonic-gate {
52cb5caa98Sdjl const char *limit, *linep, *addrstart;
53cb5caa98Sdjl int addrlen;
54cb5caa98Sdjl char addrbuf[NSS_LINELEN_NETMASKS];
55cb5caa98Sdjl struct in_addr lineaddr, argsaddr;
567c478bd9Sstevel@tonic-gate
57cb5caa98Sdjl linep = line;
58cb5caa98Sdjl limit = line + linelen;
59cb5caa98Sdjl
60cb5caa98Sdjl /* skip leading spaces */
61cb5caa98Sdjl while (linep < limit && isspace(*linep))
62cb5caa98Sdjl linep++;
63cb5caa98Sdjl
64cb5caa98Sdjl addrstart = linep;
65cb5caa98Sdjl while (linep < limit && !isspace(*linep))
66cb5caa98Sdjl linep++;
67cb5caa98Sdjl if (linep == limit)
68cb5caa98Sdjl return (0);
69cb5caa98Sdjl addrlen = linep - addrstart;
70cb5caa98Sdjl if (addrlen < sizeof (addrbuf)) {
71cb5caa98Sdjl (void) memcpy(addrbuf, addrstart, addrlen);
72cb5caa98Sdjl addrbuf[addrlen] = '\0';
73cb5caa98Sdjl if ((lineaddr.s_addr = inet_addr(addrbuf)) ==
74cb5caa98Sdjl (in_addr_t)0xffffffffU)
75cb5caa98Sdjl return (0);
76cb5caa98Sdjl if ((argsaddr.s_addr = inet_addr(argp->key.name))
77cb5caa98Sdjl == (in_addr_t)0xffffffffU)
78cb5caa98Sdjl return (0);
79cb5caa98Sdjl return (lineaddr.s_addr == argsaddr.s_addr);
80cb5caa98Sdjl }
81cb5caa98Sdjl return (0);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate static nss_status_t
getbynet(be,a)857c478bd9Sstevel@tonic-gate getbynet(be, a)
867c478bd9Sstevel@tonic-gate files_backend_ptr_t be;
877c478bd9Sstevel@tonic-gate void *a;
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
907c478bd9Sstevel@tonic-gate nss_status_t res;
917c478bd9Sstevel@tonic-gate char tmpbuf[NSS_LINELEN_NETMASKS];
927c478bd9Sstevel@tonic-gate
93cb5caa98Sdjl /*
94cb5caa98Sdjl * use the buffer passed in if result is to be returned
95cb5caa98Sdjl * in /etc file format
96cb5caa98Sdjl */
97cb5caa98Sdjl if (argp->buf.result != NULL) {
987c478bd9Sstevel@tonic-gate argp->buf.buffer = tmpbuf;
997c478bd9Sstevel@tonic-gate argp->buf.buflen = NSS_LINELEN_NETMASKS;
100cb5caa98Sdjl }
101*767b0abfSmichen res = _nss_files_XY_all(be, argp, 1, argp->key.name, check_addr);
102cb5caa98Sdjl if (argp->buf.result != NULL) {
1037c478bd9Sstevel@tonic-gate argp->buf.buffer = NULL;
1047c478bd9Sstevel@tonic-gate argp->buf.buflen = 0;
105cb5caa98Sdjl } else {
106cb5caa98Sdjl /* the frontend expects the netmask data only */
107cb5caa98Sdjl if (res == NSS_SUCCESS) {
108cb5caa98Sdjl char *m;
109cb5caa98Sdjl char *s = (char *)argp->returnval;
110cb5caa98Sdjl int l = 0;
111cb5caa98Sdjl
112cb5caa98Sdjl m = s + argp->returnlen - 1;
113cb5caa98Sdjl
114cb5caa98Sdjl /* skip trailing spaces */
115cb5caa98Sdjl while (s < m && isspace(*m))
116cb5caa98Sdjl m--;
117cb5caa98Sdjl
118cb5caa98Sdjl for (; s <= m; m--) {
119cb5caa98Sdjl if (isspace(*m))
120cb5caa98Sdjl break;
121cb5caa98Sdjl l++;
122cb5caa98Sdjl }
123cb5caa98Sdjl m++;
124cb5caa98Sdjl (void) memmove(argp->returnval, m, l);
125cb5caa98Sdjl argp->returnlen = l;
126cb5caa98Sdjl *(s + l) = '\0';
127cb5caa98Sdjl }
128cb5caa98Sdjl }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate return (res);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate static files_backend_op_t netmasks_ops[] = {
1347c478bd9Sstevel@tonic-gate _nss_files_destr,
1357c478bd9Sstevel@tonic-gate getbynet
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1397c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_files_netmasks_constr(dummy1,dummy2,dummy3)1407c478bd9Sstevel@tonic-gate _nss_files_netmasks_constr(dummy1, dummy2, dummy3)
1417c478bd9Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate return (_nss_files_constr(netmasks_ops,
1447c478bd9Sstevel@tonic-gate sizeof (netmasks_ops) / sizeof (netmasks_ops[0]),
1457c478bd9Sstevel@tonic-gate _PATH_NETMASKS, NSS_LINELEN_NETMASKS, NULL));
1467c478bd9Sstevel@tonic-gate }
147