xref: /illumos-gate/usr/src/cmd/nscd/gettnrhdb.c (revision 6bc074b1c1f7d3014541f4c3e3152dcf2b19eed6)
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 tsol_getrhbyaddr calls in nscd
28  */
29 
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <libtsnet.h>
34 #include "cache.h"
35 
36 #define	tsol_rh_db	ctx->nsc_db[0]
37 
38 #define	NSC_NAME_TSOL_RH_BYADDR	"tsol_getrhbyaddr"
39 
40 static int tsol_rh_compar(const void *, const void *);
41 static uint_t tsol_rh_gethash(nss_XbyY_key_t *, int);
42 static void tsol_rh_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
43 
44 void
45 tnrhdb_init_ctx(nsc_ctx_t *ctx) {
46 	ctx->dbname = NSS_DBNAM_TSOL_RH;
47 	ctx->db_count = 1;
48 	ctx->file_name = TNRHDB_PATH;
49 
50 	tsol_rh_db = make_cache(nsc_key_other,
51 			NSS_DBOP_TSOL_RH_BYADDR,
52 			NSC_NAME_TSOL_RH_BYADDR,
53 			tsol_rh_compar,
54 			tsol_rh_getlogstr,
55 			tsol_rh_gethash, nsc_ht_default, -1);
56 }
57 
58 static void
59 tsol_rh_getlogstr(char *name, char *whoami, size_t len,
60 			nss_XbyY_args_t *argp) {
61 
62 	(void) snprintf(whoami, len, "%s [key=%s, len=%d, addrtype=%d]",
63 		name, argp->key.hostaddr.addr, argp->key.hostaddr.len,
64 		argp->key.hostaddr.type);
65 }
66 
67 static int
68 tsol_rh_compar(const void *n1, const void *n2) {
69 	nsc_entry_t	*e1, *e2;
70 	int		res, l1, l2;
71 
72 	e1 = (nsc_entry_t *)n1;
73 	e2 = (nsc_entry_t *)n2;
74 
75 	if (e1->key.hostaddr.type > e2->key.hostaddr.type)
76 		return (1);
77 	else if (e1->key.hostaddr.type < e2->key.hostaddr.type)
78 		return (-1);
79 
80 	l1 = strlen(e1->key.hostaddr.addr);
81 	l2 = strlen(e2->key.hostaddr.addr);
82 	res = strncasecmp(e1->key.hostaddr.addr, e2->key.hostaddr.addr,
83 		(l1 > l2)?l1:l2);
84 	return (_NSC_INT_KEY_CMP(res, 0));
85 }
86 
87 static uint_t
88 tsol_rh_gethash(nss_XbyY_key_t *key, int htsize) {
89 	return (db_gethash(key->hostaddr.addr,
90 			strlen(key->hostaddr.addr), htsize));
91 }
92