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 #pragma ident "%Z%%M% %I% %E% SMI"
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * All routines necessary to deal with the file /etc/inet/netmasks. The file
327c478bd9Sstevel@tonic-gate * contains mappings from 32 bit network internet addresses to their
337c478bd9Sstevel@tonic-gate * corresponding 32 bit mask internet addresses. The addresses are in dotted
347c478bd9Sstevel@tonic-gate * internet address form.
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include "files_common.h"
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/socket.h>
417c478bd9Sstevel@tonic-gate #include <net/if.h>
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
447c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
45cb5caa98Sdjl #include <ctype.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * Validate 'files' netmasks entry. The comparison objects are in IPv4
497c478bd9Sstevel@tonic-gate * internet address format.
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate static int
check_addr(nss_XbyY_args_t * argp,const char * line,int linelen)52cb5caa98Sdjl check_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
537c478bd9Sstevel@tonic-gate {
54cb5caa98Sdjl const char *limit, *linep, *addrstart;
55cb5caa98Sdjl int addrlen;
56cb5caa98Sdjl char addrbuf[NSS_LINELEN_NETMASKS];
57cb5caa98Sdjl struct in_addr lineaddr, argsaddr;
587c478bd9Sstevel@tonic-gate
59cb5caa98Sdjl linep = line;
60cb5caa98Sdjl limit = line + linelen;
61cb5caa98Sdjl
62cb5caa98Sdjl /* skip leading spaces */
63cb5caa98Sdjl while (linep < limit && isspace(*linep))
64cb5caa98Sdjl linep++;
65cb5caa98Sdjl
66cb5caa98Sdjl addrstart = linep;
67cb5caa98Sdjl while (linep < limit && !isspace(*linep))
68cb5caa98Sdjl linep++;
69cb5caa98Sdjl if (linep == limit)
70cb5caa98Sdjl return (0);
71cb5caa98Sdjl addrlen = linep - addrstart;
72cb5caa98Sdjl if (addrlen < sizeof (addrbuf)) {
73cb5caa98Sdjl (void) memcpy(addrbuf, addrstart, addrlen);
74cb5caa98Sdjl addrbuf[addrlen] = '\0';
75cb5caa98Sdjl if ((lineaddr.s_addr = inet_addr(addrbuf)) ==
76cb5caa98Sdjl (in_addr_t)0xffffffffU)
77cb5caa98Sdjl return (0);
78cb5caa98Sdjl if ((argsaddr.s_addr = inet_addr(argp->key.name))
79cb5caa98Sdjl == (in_addr_t)0xffffffffU)
80cb5caa98Sdjl return (0);
81cb5caa98Sdjl return (lineaddr.s_addr == argsaddr.s_addr);
82cb5caa98Sdjl }
83cb5caa98Sdjl return (0);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate static nss_status_t
getbynet(be,a)877c478bd9Sstevel@tonic-gate getbynet(be, a)
887c478bd9Sstevel@tonic-gate files_backend_ptr_t be;
897c478bd9Sstevel@tonic-gate void *a;
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
927c478bd9Sstevel@tonic-gate nss_status_t res;
937c478bd9Sstevel@tonic-gate char tmpbuf[NSS_LINELEN_NETMASKS];
947c478bd9Sstevel@tonic-gate
95cb5caa98Sdjl /*
96cb5caa98Sdjl * use the buffer passed in if result is to be returned
97cb5caa98Sdjl * in /etc file format
98cb5caa98Sdjl */
99cb5caa98Sdjl if (argp->buf.result != NULL) {
1007c478bd9Sstevel@tonic-gate argp->buf.buffer = tmpbuf;
1017c478bd9Sstevel@tonic-gate argp->buf.buflen = NSS_LINELEN_NETMASKS;
102cb5caa98Sdjl }
103*767b0abfSmichen res = _nss_files_XY_all(be, argp, 1, argp->key.name, check_addr);
104cb5caa98Sdjl if (argp->buf.result != NULL) {
1057c478bd9Sstevel@tonic-gate argp->buf.buffer = NULL;
1067c478bd9Sstevel@tonic-gate argp->buf.buflen = 0;
107cb5caa98Sdjl } else {
108cb5caa98Sdjl /* the frontend expects the netmask data only */
109cb5caa98Sdjl if (res == NSS_SUCCESS) {
110cb5caa98Sdjl char *m;
111cb5caa98Sdjl char *s = (char *)argp->returnval;
112cb5caa98Sdjl int l = 0;
113cb5caa98Sdjl
114cb5caa98Sdjl m = s + argp->returnlen - 1;
115cb5caa98Sdjl
116cb5caa98Sdjl /* skip trailing spaces */
117cb5caa98Sdjl while (s < m && isspace(*m))
118cb5caa98Sdjl m--;
119cb5caa98Sdjl
120cb5caa98Sdjl for (; s <= m; m--) {
121cb5caa98Sdjl if (isspace(*m))
122cb5caa98Sdjl break;
123cb5caa98Sdjl l++;
124cb5caa98Sdjl }
125cb5caa98Sdjl m++;
126cb5caa98Sdjl (void) memmove(argp->returnval, m, l);
127cb5caa98Sdjl argp->returnlen = l;
128cb5caa98Sdjl *(s + l) = '\0';
129cb5caa98Sdjl }
130cb5caa98Sdjl }
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate return (res);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate static files_backend_op_t netmasks_ops[] = {
1367c478bd9Sstevel@tonic-gate _nss_files_destr,
1377c478bd9Sstevel@tonic-gate getbynet
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1417c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_files_netmasks_constr(dummy1,dummy2,dummy3)1427c478bd9Sstevel@tonic-gate _nss_files_netmasks_constr(dummy1, dummy2, dummy3)
1437c478bd9Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate return (_nss_files_constr(netmasks_ops,
1467c478bd9Sstevel@tonic-gate sizeof (netmasks_ops) / sizeof (netmasks_ops[0]),
1477c478bd9Sstevel@tonic-gate _PATH_NETMASKS, NSS_LINELEN_NETMASKS, NULL));
1487c478bd9Sstevel@tonic-gate }
149