xref: /titanic_50/usr/src/lib/nsswitch/files/common/getnetent.c (revision 407eb7cc0bcf1bc14f8cd498d68af0782e777f74)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * files/getnetent.c -- "files" backend for nsswitch "networks" database
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36 #include "files_common.h"
37 #include <strings.h>
38 #include <ctype.h>
39 
40 static nss_status_t
41 getbyname(be, a)
42 	files_backend_ptr_t	be;
43 	void		*a;
44 {
45 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
46 
47 	return (_nss_files_XY_all(be, argp, 1, argp->key.name,
48 			_nss_files_check_name_aliases));
49 }
50 
51 static int
52 check_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
53 {
54 
55 	const char	*limit, *linep, *addrstart;
56 	int		addrlen;
57 	char		addrbuf[NSS_LINELEN_NETWORKS];
58 	in_addr_t	linenet;
59 
60 	linep = line;
61 	limit = line + linelen;
62 
63 	/* skip network name */
64 	while (linep < limit && !isspace(*linep))
65 		linep++;
66 	/* skip the delimiting spaces */
67 	while (linep < limit && isspace(*linep))
68 		linep++;
69 	if (linep == limit)
70 		return (0);
71 
72 	addrstart = linep;
73 	while (linep < limit && !isspace(*linep))
74 		linep++;
75 	addrlen = linep - addrstart;
76 	if (addrlen < sizeof (addrbuf)) {
77 		(void) memcpy(addrbuf, addrstart, addrlen);
78 		addrbuf[addrlen] = '\0';
79 		if ((linenet = inet_network(addrbuf)) ==
80 						(in_addr_t)0xffffffffU)
81 			return (0);
82 		return (AF_INET == argp->key.netaddr.type &&
83 			linenet == argp->key.netaddr.net);
84 	}
85 	return (0);
86 }
87 
88 static nss_status_t
89 getbyaddr(be, a)
90 	files_backend_ptr_t	be;
91 	void		*a;
92 {
93 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
94 
95 	return (_nss_files_XY_all(be, argp, 1, 0, check_addr));
96 }
97 
98 static files_backend_op_t net_ops[] = {
99 	_nss_files_destr,
100 	_nss_files_endent,
101 	_nss_files_setent,
102 	_nss_files_getent_netdb,
103 	getbyname,
104 	getbyaddr
105 };
106 
107 /*ARGSUSED*/
108 nss_backend_t *
109 _nss_files_networks_constr(dummy1, dummy2, dummy3)
110 	const char  *dummy1, *dummy2, *dummy3;
111 {
112 	return (_nss_files_constr(net_ops,
113 				sizeof (net_ops) / sizeof (net_ops[0]),
114 				_PATH_NETWORKS,
115 				NSS_LINELEN_NETWORKS,
116 				NULL));
117 }
118