xref: /titanic_44/usr/src/cmd/nscd/getnode.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
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 getipnode* calls in nscd. Note that the
30  * getnodeby* APIs were renamed getipnodeby*. The interfaces
31  * related to them in the nscd will remain as getnode*.
32  */
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <inet/ip6.h>
42 #include "cache.h"
43 
44 static int ipaddr_compar(const void *, const void *);
45 static uint_t ipaddr_gethash(nss_XbyY_key_t *, int);
46 static void ipaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
47 
48 static int ipname_compar(const void *, const void *);
49 static uint_t ipname_gethash(nss_XbyY_key_t *, int);
50 static void ipname_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
51 
52 #define	nnam_db	ctx->nsc_db[0]
53 #define	addr_db	ctx->nsc_db[1]
54 
55 #define	NSC_NAME_IPNODES_BYNAME	"getipnodebyname"
56 #define	NSC_NAME_IPNODES_BYADDR	"getipnodebyaddr"
57 
58 void
59 ipnode_init_ctx(nsc_ctx_t *ctx) {
60 	ctx->dbname = NSS_DBNAM_IPNODES;
61 	ctx->file_name = "/etc/inet/ipnodes";
62 	ctx->db_count = 2;
63 	nnam_db = make_cache(nsc_key_other,
64 			NSS_DBOP_IPNODES_BYNAME,
65 			NSC_NAME_IPNODES_BYNAME,
66 			ipname_compar,
67 			ipname_getlogstr,
68 			ipname_gethash, nsc_ht_default, -1);
69 
70 	addr_db = make_cache(nsc_key_other,
71 			NSS_DBOP_IPNODES_BYADDR,
72 			NSC_NAME_IPNODES_BYADDR,
73 			ipaddr_compar,
74 			ipaddr_getlogstr,
75 			ipaddr_gethash, nsc_ht_default, -1);
76 }
77 
78 static int
79 ipaddr_compar(const void *n1, const void *n2) {
80 	nsc_entry_t	*e1, *e2;
81 	int		res, l1, l2;
82 
83 	e1 = (nsc_entry_t *)n1;
84 	e2 = (nsc_entry_t *)n2;
85 
86 	if (e1->key.hostaddr.type > e2->key.hostaddr.type)
87 		return (1);
88 	else if (e1->key.hostaddr.type < e2->key.hostaddr.type)
89 		return (-1);
90 
91 	l1 = e1->key.hostaddr.len;
92 	l2 = e2->key.hostaddr.len;
93 	res = memcmp(e1->key.hostaddr.addr, e2->key.hostaddr.addr,
94 		(l2 > l1)?l1:l2);
95 	return ((res) ? _NSC_INT_KEY_CMP(res, 0) : _NSC_INT_KEY_CMP(l1, l2));
96 }
97 
98 static uint_t
99 ipaddr_gethash(nss_XbyY_key_t *key, int htsize) {
100 	return (db_gethash(key->hostaddr.addr,
101 		key->hostaddr.len, htsize));
102 }
103 
104 static void
105 ipaddr_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
106 	char addr[INET6_ADDRSTRLEN];
107 
108 	if (inet_ntop(argp->key.hostaddr.type, argp->key.hostaddr.addr, addr,
109 			sizeof (addr)) == NULL) {
110 		(void) snprintf(whoami, len, "%s", name);
111 	} else {
112 		(void) snprintf(whoami, len, "%s [key=%s addrtype=%d]",
113 			name,
114 			addr, argp->key.hostaddr.type);
115 	}
116 }
117 
118 static int
119 ipname_compar(const void *n1, const void *n2) {
120 	nsc_entry_t	*e1, *e2;
121 	int		res, l1, l2;
122 
123 	e1 = (nsc_entry_t *)n1;
124 	e2 = (nsc_entry_t *)n2;
125 
126 	if (e1->key.ipnode.af_family > e2->key.ipnode.af_family)
127 		return (1);
128 	else if (e1->key.ipnode.af_family < e2->key.ipnode.af_family)
129 		return (-1);
130 
131 	l1 = strlen(e1->key.ipnode.name);
132 	l2 = strlen(e2->key.ipnode.name);
133 	res = strncasecmp(e1->key.ipnode.name, e2->key.ipnode.name,
134 		(l1 > l2)?l1:l2);
135 	return (_NSC_INT_KEY_CMP(res, 0));
136 }
137 
138 static uint_t
139 ipname_gethash(nss_XbyY_key_t *key, int htsize) {
140 	return (cis_gethash(key->ipnode.name, htsize));
141 }
142 
143 static void
144 ipname_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
145 	(void) snprintf(whoami, len, "%s [key=%s:af=%d:flags=%d]",
146 			name,
147 			argp->key.ipnode.name,
148 			argp->key.ipnode.af_family,
149 			argp->key.ipnode.flags);
150 }
151