xref: /illumos-gate/usr/src/lib/nsswitch/files/common/netmasks.c (revision 3bfb48feb84bb78924286a801c68f80198912fa7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * files/netmasks.c -- "files" backend for nsswitch "netmasks" database
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * All routines necessary to deal with the file /etc/inet/netmasks.  The file
32  * contains mappings from 32 bit network internet addresses to their
33  * corresponding 32 bit mask internet addresses. The addresses are in dotted
34  * internet address form.
35  */
36 
37 #include "files_common.h"
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <nss_dbdefs.h>
45 #include <ctype.h>
46 
47 /*
48  * Validate 'files' netmasks entry. The comparison objects are in IPv4
49  * internet address format.
50  */
51 static int
52 check_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
53 {
54 	const char	*limit, *linep, *addrstart;
55 	int		addrlen;
56 	char		addrbuf[NSS_LINELEN_NETMASKS];
57 	struct in_addr	lineaddr, argsaddr;
58 
59 	linep = line;
60 	limit = line + linelen;
61 
62 	/* skip leading spaces */
63 	while (linep < limit && isspace(*linep))
64 		linep++;
65 
66 	addrstart = linep;
67 	while (linep < limit && !isspace(*linep))
68 		linep++;
69 	if (linep == limit)
70 		return (0);
71 	addrlen = linep - addrstart;
72 	if (addrlen < sizeof (addrbuf)) {
73 		(void) memcpy(addrbuf, addrstart, addrlen);
74 		addrbuf[addrlen] = '\0';
75 		if ((lineaddr.s_addr = inet_addr(addrbuf)) ==
76 						(in_addr_t)0xffffffffU)
77 			return (0);
78 		if ((argsaddr.s_addr = inet_addr(argp->key.name))
79 				== (in_addr_t)0xffffffffU)
80 			return (0);
81 		return (lineaddr.s_addr == argsaddr.s_addr);
82 	}
83 	return (0);
84 }
85 
86 static nss_status_t
87 getbynet(be, a)
88 	files_backend_ptr_t	be;
89 	void			*a;
90 {
91 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
92 	nss_status_t		res;
93 	char			tmpbuf[NSS_LINELEN_NETMASKS];
94 
95 	/*
96 	 * use the buffer passed in if result is to be returned
97 	 * in /etc file format
98 	 */
99 	if (argp->buf.result != NULL) {
100 		argp->buf.buffer = tmpbuf;
101 		argp->buf.buflen = NSS_LINELEN_NETMASKS;
102 	}
103 	res = _nss_files_XY_all(be, argp, 1, argp->key.name, check_addr);
104 	if (argp->buf.result != NULL) {
105 		argp->buf.buffer = NULL;
106 		argp->buf.buflen = 0;
107 	} else {
108 		/* the frontend expects the netmask data only */
109 		if (res == NSS_SUCCESS)	 {
110 			char	*m;
111 			char	*s = (char *)argp->returnval;
112 			int	l = 0;
113 
114 			m = s + argp->returnlen - 1;
115 
116 			/* skip trailing spaces */
117 			while (s < m && isspace(*m))
118 				m--;
119 
120 			for (; s <= m; m--) {
121 				if (isspace(*m))
122 					break;
123 				l++;
124 			}
125 			m++;
126 			(void) memmove(argp->returnval, m, l);
127 			argp->returnlen = l;
128 			*(s + l) = '\0';
129 		}
130 	}
131 
132 	return (res);
133 }
134 
135 static files_backend_op_t netmasks_ops[] = {
136 	_nss_files_destr,
137 	getbynet
138 };
139 
140 /*ARGSUSED*/
141 nss_backend_t *
142 _nss_files_netmasks_constr(dummy1, dummy2, dummy3)
143 	const char	*dummy1, *dummy2, *dummy3;
144 {
145 	return (_nss_files_constr(netmasks_ops,
146 	    sizeof (netmasks_ops) / sizeof (netmasks_ops[0]),
147 	    _PATH_NETMASKS, NSS_LINELEN_NETMASKS, NULL));
148 }
149