xref: /illumos-gate/usr/src/cmd/nscd/getnet.c (revision 2a8bcb4e)
1*cb5caa98Sdjl /*
2*cb5caa98Sdjl  * CDDL HEADER START
3*cb5caa98Sdjl  *
4*cb5caa98Sdjl  * The contents of this file are subject to the terms of the
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
7*cb5caa98Sdjl  *
8*cb5caa98Sdjl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*cb5caa98Sdjl  * or http://www.opensolaris.org/os/licensing.
10*cb5caa98Sdjl  * See the License for the specific language governing permissions
11*cb5caa98Sdjl  * and limitations under the License.
12*cb5caa98Sdjl  *
13*cb5caa98Sdjl  * When distributing Covered Code, include this CDDL HEADER in each
14*cb5caa98Sdjl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*cb5caa98Sdjl  * If applicable, add the following below this CDDL HEADER, with the
16*cb5caa98Sdjl  * fields enclosed by brackets "[]" replaced with your own identifying
17*cb5caa98Sdjl  * information: Portions Copyright [yyyy] [name of copyright owner]
18*cb5caa98Sdjl  *
19*cb5caa98Sdjl  * CDDL HEADER END
20*cb5caa98Sdjl  */
21*cb5caa98Sdjl /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*cb5caa98Sdjl  * Use is subject to license terms.
24*cb5caa98Sdjl  */
25*cb5caa98Sdjl 
26*cb5caa98Sdjl /*
27*cb5caa98Sdjl  * Routines to handle getnet* calls in nscd
28*cb5caa98Sdjl  */
29*cb5caa98Sdjl 
30*cb5caa98Sdjl #include <string.h>
31*cb5caa98Sdjl #include <stdlib.h>
32*cb5caa98Sdjl #include <sys/types.h>
33*cb5caa98Sdjl #include <sys/socket.h>
34*cb5caa98Sdjl #include <netinet/in.h>
35*cb5caa98Sdjl #include <arpa/inet.h>
36*cb5caa98Sdjl #include "cache.h"
37*cb5caa98Sdjl 
38*cb5caa98Sdjl #define	nam_db	ctx->nsc_db[0]
39*cb5caa98Sdjl #define	addr_db	ctx->nsc_db[1]
40*cb5caa98Sdjl 
41*cb5caa98Sdjl #define	NSC_NAME_NETWORKS_BYNAME	"getnetbyname"
42*cb5caa98Sdjl #define	NSC_NAME_NETWORKS_BYADDR	"getnetbyaddr"
43*cb5caa98Sdjl 
44*cb5caa98Sdjl static int netaddr_compar(const void *, const void *);
45*cb5caa98Sdjl static uint_t netaddr_gethash(nss_XbyY_key_t *, int);
46*cb5caa98Sdjl static void netaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
47*cb5caa98Sdjl 
48*cb5caa98Sdjl void
net_init_ctx(nsc_ctx_t * ctx)49*cb5caa98Sdjl net_init_ctx(nsc_ctx_t *ctx) {
50*cb5caa98Sdjl 	ctx->dbname = NSS_DBNAM_NETWORKS;
51*cb5caa98Sdjl 	ctx->db_count = 2;
52*cb5caa98Sdjl 	ctx->file_name = "/etc/inet/networks";
53*cb5caa98Sdjl 
54*cb5caa98Sdjl 	nam_db = make_cache(nsc_key_ces,
55*cb5caa98Sdjl 			NSS_DBOP_NETWORKS_BYNAME,
56*cb5caa98Sdjl 			NSC_NAME_NETWORKS_BYNAME,
57*cb5caa98Sdjl 			NULL, NULL, NULL, nsc_ht_default, -1);
58*cb5caa98Sdjl 
59*cb5caa98Sdjl 	addr_db = make_cache(nsc_key_other,
60*cb5caa98Sdjl 			NSS_DBOP_NETWORKS_BYADDR,
61*cb5caa98Sdjl 			NSC_NAME_NETWORKS_BYADDR,
62*cb5caa98Sdjl 			netaddr_compar,
63*cb5caa98Sdjl 			netaddr_getlogstr,
64*cb5caa98Sdjl 			netaddr_gethash, nsc_ht_default, -1);
65*cb5caa98Sdjl }
66*cb5caa98Sdjl 
67*cb5caa98Sdjl static void
netaddr_getlogstr(char * name,char * whoami,size_t len,nss_XbyY_args_t * argp)68*cb5caa98Sdjl netaddr_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
69*cb5caa98Sdjl 	if (argp->key.netaddr.type == AF_INET) {
70*cb5caa98Sdjl 		uint32_t	net;
71*cb5caa98Sdjl 		uchar_t		*up;
72*cb5caa98Sdjl 
73*cb5caa98Sdjl 		net = htonl(argp->key.netaddr.net);
74*cb5caa98Sdjl 		up = (uchar_t *)&net;
75*cb5caa98Sdjl 
76*cb5caa98Sdjl 		if (up[0])
77*cb5caa98Sdjl 			(void) snprintf(whoami, len, "%s [key=%d.%d.%d.%d]",
78*cb5caa98Sdjl 				name,
79*cb5caa98Sdjl 				up[0], up[1], up[2], up[3]);
80*cb5caa98Sdjl 		else if (up[1])
81*cb5caa98Sdjl 			(void) snprintf(whoami, len, "%s [key=%d.%d.%d]",
82*cb5caa98Sdjl 				name,
83*cb5caa98Sdjl 				up[1], up[2], up[3]);
84*cb5caa98Sdjl 		else if (up[2])
85*cb5caa98Sdjl 			(void) snprintf(whoami, len, "%s [key=%d.%d]",
86*cb5caa98Sdjl 				name,
87*cb5caa98Sdjl 				up[2], up[3]);
88*cb5caa98Sdjl 		else
89*cb5caa98Sdjl 			(void) snprintf(whoami, len, "%s [key=%d]",
90*cb5caa98Sdjl 				name,
91*cb5caa98Sdjl 				up[3]);
92*cb5caa98Sdjl 	} else {
93*cb5caa98Sdjl 		(void) snprintf(whoami, len, "%s [key=%d, %d]",
94*cb5caa98Sdjl 			name,
95*cb5caa98Sdjl 			argp->key.netaddr.net, argp->key.netaddr.type);
96*cb5caa98Sdjl 	}
97*cb5caa98Sdjl }
98*cb5caa98Sdjl 
99*cb5caa98Sdjl static int
netaddr_compar(const void * n1,const void * n2)100*cb5caa98Sdjl netaddr_compar(const void *n1, const void *n2) {
101*cb5caa98Sdjl 	nsc_entry_t	*e1, *e2;
102*cb5caa98Sdjl 
103*cb5caa98Sdjl 	e1 = (nsc_entry_t *)n1;
104*cb5caa98Sdjl 	e2 = (nsc_entry_t *)n2;
105*cb5caa98Sdjl 
106*cb5caa98Sdjl 	if (e1->key.netaddr.type > e2->key.netaddr.type)
107*cb5caa98Sdjl 		return (1);
108*cb5caa98Sdjl 	else if (e1->key.netaddr.type < e2->key.netaddr.type)
109*cb5caa98Sdjl 		return (-1);
110*cb5caa98Sdjl 
111*cb5caa98Sdjl 	return (_NSC_INT_KEY_CMP(e1->key.netaddr.net, e2->key.netaddr.net));
112*cb5caa98Sdjl }
113*cb5caa98Sdjl 
114*cb5caa98Sdjl static uint_t
netaddr_gethash(nss_XbyY_key_t * key,int htsize)115*cb5caa98Sdjl netaddr_gethash(nss_XbyY_key_t *key, int htsize) {
116*cb5caa98Sdjl 	return (db_gethash(&key->netaddr.net,
117*cb5caa98Sdjl 			sizeof (key->netaddr.net), htsize));
118*cb5caa98Sdjl }
119