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