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