xref: /titanic_51/usr/src/cmd/nscd/getnet.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Routines to handle getnet* calls in nscd
30  */
31 
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include "cache.h"
39 
40 #define	nam_db	ctx->nsc_db[0]
41 #define	addr_db	ctx->nsc_db[1]
42 
43 #define	NSC_NAME_NETWORKS_BYNAME	"getnetbyname"
44 #define	NSC_NAME_NETWORKS_BYADDR	"getnetbyaddr"
45 
46 static int netaddr_compar(const void *, const void *);
47 static uint_t netaddr_gethash(nss_XbyY_key_t *, int);
48 static void netaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
49 
50 void
51 net_init_ctx(nsc_ctx_t *ctx) {
52 	ctx->dbname = NSS_DBNAM_NETWORKS;
53 	ctx->db_count = 2;
54 	ctx->file_name = "/etc/inet/networks";
55 
56 	nam_db = make_cache(nsc_key_ces,
57 			NSS_DBOP_NETWORKS_BYNAME,
58 			NSC_NAME_NETWORKS_BYNAME,
59 			NULL, NULL, NULL, nsc_ht_default, -1);
60 
61 	addr_db = make_cache(nsc_key_other,
62 			NSS_DBOP_NETWORKS_BYADDR,
63 			NSC_NAME_NETWORKS_BYADDR,
64 			netaddr_compar,
65 			netaddr_getlogstr,
66 			netaddr_gethash, nsc_ht_default, -1);
67 }
68 
69 static void
70 netaddr_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
71 	if (argp->key.netaddr.type == AF_INET) {
72 		uint32_t	net;
73 		uchar_t		*up;
74 
75 		net = htonl(argp->key.netaddr.net);
76 		up = (uchar_t *)&net;
77 
78 		if (up[0])
79 			(void) snprintf(whoami, len, "%s [key=%d.%d.%d.%d]",
80 				name,
81 				up[0], up[1], up[2], up[3]);
82 		else if (up[1])
83 			(void) snprintf(whoami, len, "%s [key=%d.%d.%d]",
84 				name,
85 				up[1], up[2], up[3]);
86 		else if (up[2])
87 			(void) snprintf(whoami, len, "%s [key=%d.%d]",
88 				name,
89 				up[2], up[3]);
90 		else
91 			(void) snprintf(whoami, len, "%s [key=%d]",
92 				name,
93 				up[3]);
94 	} else {
95 		(void) snprintf(whoami, len, "%s [key=%d, %d]",
96 			name,
97 			argp->key.netaddr.net, argp->key.netaddr.type);
98 	}
99 }
100 
101 static int
102 netaddr_compar(const void *n1, const void *n2) {
103 	nsc_entry_t	*e1, *e2;
104 
105 	e1 = (nsc_entry_t *)n1;
106 	e2 = (nsc_entry_t *)n2;
107 
108 	if (e1->key.netaddr.type > e2->key.netaddr.type)
109 		return (1);
110 	else if (e1->key.netaddr.type < e2->key.netaddr.type)
111 		return (-1);
112 
113 	return (_NSC_INT_KEY_CMP(e1->key.netaddr.net, e2->key.netaddr.net));
114 }
115 
116 static uint_t
117 netaddr_gethash(nss_XbyY_key_t *key, int htsize) {
118 	return (db_gethash(&key->netaddr.net,
119 			sizeof (key->netaddr.net), htsize));
120 }
121