xref: /titanic_52/usr/src/lib/nsswitch/nis/common/getnetent.c (revision 0dfdd7f38ea07ffc9a4b245c94185c923f4bb0a3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0dfdd7f3Smichen  * Common Development and Distribution License (the "License").
6*0dfdd7f3Smichen  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*0dfdd7f3Smichen 
22*0dfdd7f3Smichen /*
23*0dfdd7f3Smichen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*0dfdd7f3Smichen  * Use is subject to license terms.
25*0dfdd7f3Smichen  */
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	nis/getnetent.c -- "nis" backend for nsswitch "networks" database
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "nis_common.h"
337c478bd9Sstevel@tonic-gate #include <synch.h>
347c478bd9Sstevel@tonic-gate #include <netdb.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
377c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate 
40*0dfdd7f3Smichen static int nettoa(int anet, char *buf, int buflen, char **pnull);
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static nss_status_t
437c478bd9Sstevel@tonic-gate getbyname(be, a)
447c478bd9Sstevel@tonic-gate 	nis_backend_ptr_t	be;
457c478bd9Sstevel@tonic-gate 	void			*a;
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	return (_nss_nis_lookup(be, argp, 1, "networks.byname",
507c478bd9Sstevel@tonic-gate 		argp->key.name, 0));
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static nss_status_t
547c478bd9Sstevel@tonic-gate getbyaddr(be, a)
557c478bd9Sstevel@tonic-gate 	nis_backend_ptr_t	be;
567c478bd9Sstevel@tonic-gate 	void			*a;
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
597c478bd9Sstevel@tonic-gate 	char			addrstr[16];
60*0dfdd7f3Smichen 	char			*pnull;
61*0dfdd7f3Smichen 	nss_status_t		rc;
627c478bd9Sstevel@tonic-gate 
63*0dfdd7f3Smichen 	if (nettoa((int)argp->key.netaddr.net, addrstr, 16, &pnull) != 0)
647c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL);	/* it's really ENOMEM */
65*0dfdd7f3Smichen 	rc = _nss_nis_lookup(be, argp, 1, "networks.byaddr", addrstr, 0);
66*0dfdd7f3Smichen 
67*0dfdd7f3Smichen 	/*
68*0dfdd7f3Smichen 	 * if not found, try again with the untruncated address string
69*0dfdd7f3Smichen 	 * that has the trailing zero(s)
70*0dfdd7f3Smichen 	 */
71*0dfdd7f3Smichen 	if (rc == NSS_NOTFOUND && pnull != NULL) {
72*0dfdd7f3Smichen 		*pnull = '.';
73*0dfdd7f3Smichen 		rc = _nss_nis_lookup(be, argp, 1, "networks.byaddr",
74*0dfdd7f3Smichen 			addrstr, 0);
75*0dfdd7f3Smichen 	}
76*0dfdd7f3Smichen 	return (rc);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static nis_backend_op_t net_ops[] = {
807c478bd9Sstevel@tonic-gate 	_nss_nis_destr,
817c478bd9Sstevel@tonic-gate 	_nss_nis_endent,
827c478bd9Sstevel@tonic-gate 	_nss_nis_setent,
837c478bd9Sstevel@tonic-gate 	_nss_nis_getent_netdb,
847c478bd9Sstevel@tonic-gate 	getbyname,
857c478bd9Sstevel@tonic-gate 	getbyaddr
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
897c478bd9Sstevel@tonic-gate nss_backend_t *
907c478bd9Sstevel@tonic-gate _nss_nis_networks_constr(dummy1, dummy2, dummy3)
917c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	return (_nss_nis_constr(net_ops,
947c478bd9Sstevel@tonic-gate 				sizeof (net_ops) / sizeof (net_ops[0]),
957c478bd9Sstevel@tonic-gate 				"networks.byaddr"));
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Takes an unsigned integer in host order, and returns a printable
1007c478bd9Sstevel@tonic-gate  * string for it as a network number.  To allow for the possibility of
101*0dfdd7f3Smichen  * naming subnets, only trailing dot-zeros are truncated. The location
102*0dfdd7f3Smichen  * where the string is truncated (or set to '\0') is returned in *pnull.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate static int
105*0dfdd7f3Smichen nettoa(int anet, char *buf, int buflen, char **pnull)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	char *p;
1087c478bd9Sstevel@tonic-gate 	struct in_addr in;
1097c478bd9Sstevel@tonic-gate 	int addr;
1107c478bd9Sstevel@tonic-gate 
111*0dfdd7f3Smichen 	*pnull = NULL;
112*0dfdd7f3Smichen 
1137c478bd9Sstevel@tonic-gate 	if (buf == 0)
1147c478bd9Sstevel@tonic-gate 		return (1);
1157c478bd9Sstevel@tonic-gate 	in = inet_makeaddr(anet, INADDR_ANY);
1167c478bd9Sstevel@tonic-gate 	addr = in.s_addr;
1177c478bd9Sstevel@tonic-gate 	(void) strncpy(buf, inet_ntoa(in), buflen);
1187c478bd9Sstevel@tonic-gate 	if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
1197c478bd9Sstevel@tonic-gate 		p = strchr(buf, '.');
1207c478bd9Sstevel@tonic-gate 		if (p == NULL)
1217c478bd9Sstevel@tonic-gate 			return (1);
1227c478bd9Sstevel@tonic-gate 		*p = 0;
123*0dfdd7f3Smichen 		*pnull = p;
1247c478bd9Sstevel@tonic-gate 	} else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
1257c478bd9Sstevel@tonic-gate 		p = strchr(buf, '.');
1267c478bd9Sstevel@tonic-gate 		if (p == NULL)
1277c478bd9Sstevel@tonic-gate 			return (1);
1287c478bd9Sstevel@tonic-gate 		p = strchr(p+1, '.');
1297c478bd9Sstevel@tonic-gate 		if (p == NULL)
1307c478bd9Sstevel@tonic-gate 			return (1);
1317c478bd9Sstevel@tonic-gate 		*p = 0;
132*0dfdd7f3Smichen 		*pnull = p;
1337c478bd9Sstevel@tonic-gate 	} else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
1347c478bd9Sstevel@tonic-gate 		p = strrchr(buf, '.');
1357c478bd9Sstevel@tonic-gate 		if (p == NULL)
1367c478bd9Sstevel@tonic-gate 			return (1);
1377c478bd9Sstevel@tonic-gate 		*p = 0;
138*0dfdd7f3Smichen 		*pnull = p;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	return (0);
1417c478bd9Sstevel@tonic-gate }
142