xref: /illumos-gate/usr/src/lib/nsswitch/files/common/netmasks.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 /*
29  * All routines necessary to deal with the file /etc/inet/netmasks.  The file
30  * contains mappings from 32 bit network internet addresses to their
31  * corresponding 32 bit mask internet addresses. The addresses are in dotted
32  * internet address form.
33  */
34 
35 #include "files_common.h"
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <net/if.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <nss_dbdefs.h>
43 #include <ctype.h>
44 
45 /*
46  * Validate 'files' netmasks entry. The comparison objects are in IPv4
47  * internet address format.
48  */
49 static int
check_addr(nss_XbyY_args_t * argp,const char * line,int linelen)50 check_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
51 {
52 	const char	*limit, *linep, *addrstart;
53 	int		addrlen;
54 	char		addrbuf[NSS_LINELEN_NETMASKS];
55 	struct in_addr	lineaddr, argsaddr;
56 
57 	linep = line;
58 	limit = line + linelen;
59 
60 	/* skip leading spaces */
61 	while (linep < limit && isspace(*linep))
62 		linep++;
63 
64 	addrstart = linep;
65 	while (linep < limit && !isspace(*linep))
66 		linep++;
67 	if (linep == limit)
68 		return (0);
69 	addrlen = linep - addrstart;
70 	if (addrlen < sizeof (addrbuf)) {
71 		(void) memcpy(addrbuf, addrstart, addrlen);
72 		addrbuf[addrlen] = '\0';
73 		if ((lineaddr.s_addr = inet_addr(addrbuf)) ==
74 						(in_addr_t)0xffffffffU)
75 			return (0);
76 		if ((argsaddr.s_addr = inet_addr(argp->key.name))
77 				== (in_addr_t)0xffffffffU)
78 			return (0);
79 		return (lineaddr.s_addr == argsaddr.s_addr);
80 	}
81 	return (0);
82 }
83 
84 static nss_status_t
getbynet(be,a)85 getbynet(be, a)
86 	files_backend_ptr_t	be;
87 	void			*a;
88 {
89 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
90 	nss_status_t		res;
91 	char			tmpbuf[NSS_LINELEN_NETMASKS];
92 
93 	/*
94 	 * use the buffer passed in if result is to be returned
95 	 * in /etc file format
96 	 */
97 	if (argp->buf.result != NULL) {
98 		argp->buf.buffer = tmpbuf;
99 		argp->buf.buflen = NSS_LINELEN_NETMASKS;
100 	}
101 	res = _nss_files_XY_all(be, argp, 1, argp->key.name, check_addr);
102 	if (argp->buf.result != NULL) {
103 		argp->buf.buffer = NULL;
104 		argp->buf.buflen = 0;
105 	} else {
106 		/* the frontend expects the netmask data only */
107 		if (res == NSS_SUCCESS)	 {
108 			char	*m;
109 			char	*s = (char *)argp->returnval;
110 			int	l = 0;
111 
112 			m = s + argp->returnlen - 1;
113 
114 			/* skip trailing spaces */
115 			while (s < m && isspace(*m))
116 				m--;
117 
118 			for (; s <= m; m--) {
119 				if (isspace(*m))
120 					break;
121 				l++;
122 			}
123 			m++;
124 			(void) memmove(argp->returnval, m, l);
125 			argp->returnlen = l;
126 			*(s + l) = '\0';
127 		}
128 	}
129 
130 	return (res);
131 }
132 
133 static files_backend_op_t netmasks_ops[] = {
134 	_nss_files_destr,
135 	getbynet
136 };
137 
138 /*ARGSUSED*/
139 nss_backend_t *
_nss_files_netmasks_constr(dummy1,dummy2,dummy3)140 _nss_files_netmasks_constr(dummy1, dummy2, dummy3)
141 	const char	*dummy1, *dummy2, *dummy3;
142 {
143 	return (_nss_files_constr(netmasks_ops,
144 	    sizeof (netmasks_ops) / sizeof (netmasks_ops[0]),
145 	    _PATH_NETMASKS, NSS_LINELEN_NETMASKS, NULL));
146 }
147