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