xref: /titanic_52/usr/src/lib/nsswitch/nis/common/getnetent.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	nis/getnetent.c -- "nis" backend for nsswitch "networks" database
24  *
25  *	Copyright (c) 1988-1992 Sun Microsystems Inc
26  *	All Rights Reserved.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include "nis_common.h"
32 #include <synch.h>
33 #include <netdb.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <string.h>
38 
39 static int nettoa(int anet, char *buf, int buflen);
40 
41 static nss_status_t
42 getbyname(be, a)
43 	nis_backend_ptr_t	be;
44 	void			*a;
45 {
46 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
47 
48 	return (_nss_nis_lookup(be, argp, 1, "networks.byname",
49 			      argp->key.name, 0));
50 }
51 
52 static nss_status_t
53 getbyaddr(be, a)
54 	nis_backend_ptr_t	be;
55 	void			*a;
56 {
57 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
58 	char			addrstr[16];
59 
60 	if (nettoa((int) argp->key.netaddr.net, addrstr, 16) != 0)
61 		return (NSS_UNAVAIL);	/* it's really ENOMEM */
62 	return (_nss_nis_lookup(be, argp, 1, "networks.byaddr", addrstr, 0));
63 }
64 
65 static nis_backend_op_t net_ops[] = {
66 	_nss_nis_destr,
67 	_nss_nis_endent,
68 	_nss_nis_setent,
69 	_nss_nis_getent_netdb,
70 	getbyname,
71 	getbyaddr
72 };
73 
74 /*ARGSUSED*/
75 nss_backend_t *
76 _nss_nis_networks_constr(dummy1, dummy2, dummy3)
77 	const char	*dummy1, *dummy2, *dummy3;
78 {
79 	return (_nss_nis_constr(net_ops,
80 				sizeof (net_ops) / sizeof (net_ops[0]),
81 				"networks.byaddr"));
82 }
83 
84 /*
85  * Takes an unsigned integer in host order, and returns a printable
86  * string for it as a network number.  To allow for the possibility of
87  * naming subnets, only trailing dot-zeros are truncated.
88  */
89 static int
90 nettoa(anet, buf, buflen)
91 	int		anet;
92 	char	*buf;
93 	int		buflen;
94 {
95 	char *p;
96 	struct in_addr in;
97 	int addr;
98 
99 	if (buf == 0)
100 		return (1);
101 	in = inet_makeaddr(anet, INADDR_ANY);
102 	addr = in.s_addr;
103 	(void) strncpy(buf, inet_ntoa(in), buflen);
104 	if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
105 		p = strchr(buf, '.');
106 		if (p == NULL)
107 			return (1);
108 		*p = 0;
109 	} else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
110 		p = strchr(buf, '.');
111 		if (p == NULL)
112 			return (1);
113 		p = strchr(p+1, '.');
114 		if (p == NULL)
115 			return (1);
116 		*p = 0;
117 	} else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
118 		p = strrchr(buf, '.');
119 		if (p == NULL)
120 			return (1);
121 		*p = 0;
122 	}
123 	return (0);
124 }
125