10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*2830Sdjl * Common Development and Distribution License (the "License").
6*2830Sdjl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Routines to handle getipnode* calls in nscd. Note that the
300Sstevel@tonic-gate * getnodeby* APIs were renamed getipnodeby*. The interfaces
310Sstevel@tonic-gate * related to them in the nscd will remain as getnode*.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate #include <sys/types.h>
38*2830Sdjl #include <sys/socket.h>
39*2830Sdjl #include <netinet/in.h>
40*2830Sdjl #include <arpa/inet.h>
410Sstevel@tonic-gate #include <inet/ip6.h>
42*2830Sdjl #include "cache.h"
430Sstevel@tonic-gate
44*2830Sdjl static int ipaddr_compar(const void *, const void *);
45*2830Sdjl static uint_t ipaddr_gethash(nss_XbyY_key_t *, int);
46*2830Sdjl static void ipaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
470Sstevel@tonic-gate
48*2830Sdjl static int ipname_compar(const void *, const void *);
49*2830Sdjl static uint_t ipname_gethash(nss_XbyY_key_t *, int);
50*2830Sdjl static void ipname_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
510Sstevel@tonic-gate
52*2830Sdjl #define nnam_db ctx->nsc_db[0]
53*2830Sdjl #define addr_db ctx->nsc_db[1]
540Sstevel@tonic-gate
55*2830Sdjl #define NSC_NAME_IPNODES_BYNAME "getipnodebyname"
56*2830Sdjl #define NSC_NAME_IPNODES_BYADDR "getipnodebyaddr"
570Sstevel@tonic-gate
580Sstevel@tonic-gate void
ipnode_init_ctx(nsc_ctx_t * ctx)59*2830Sdjl ipnode_init_ctx(nsc_ctx_t *ctx) {
60*2830Sdjl ctx->dbname = NSS_DBNAM_IPNODES;
61*2830Sdjl ctx->file_name = "/etc/inet/ipnodes";
62*2830Sdjl ctx->db_count = 2;
63*2830Sdjl nnam_db = make_cache(nsc_key_other,
64*2830Sdjl NSS_DBOP_IPNODES_BYNAME,
65*2830Sdjl NSC_NAME_IPNODES_BYNAME,
66*2830Sdjl ipname_compar,
67*2830Sdjl ipname_getlogstr,
68*2830Sdjl ipname_gethash, nsc_ht_default, -1);
690Sstevel@tonic-gate
70*2830Sdjl addr_db = make_cache(nsc_key_other,
71*2830Sdjl NSS_DBOP_IPNODES_BYADDR,
72*2830Sdjl NSC_NAME_IPNODES_BYADDR,
73*2830Sdjl ipaddr_compar,
74*2830Sdjl ipaddr_getlogstr,
75*2830Sdjl ipaddr_gethash, nsc_ht_default, -1);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
78*2830Sdjl static int
ipaddr_compar(const void * n1,const void * n2)79*2830Sdjl ipaddr_compar(const void *n1, const void *n2) {
80*2830Sdjl nsc_entry_t *e1, *e2;
81*2830Sdjl int res, l1, l2;
820Sstevel@tonic-gate
83*2830Sdjl e1 = (nsc_entry_t *)n1;
84*2830Sdjl e2 = (nsc_entry_t *)n2;
850Sstevel@tonic-gate
86*2830Sdjl if (e1->key.hostaddr.type > e2->key.hostaddr.type)
87*2830Sdjl return (1);
88*2830Sdjl else if (e1->key.hostaddr.type < e2->key.hostaddr.type)
89*2830Sdjl return (-1);
900Sstevel@tonic-gate
91*2830Sdjl l1 = e1->key.hostaddr.len;
92*2830Sdjl l2 = e2->key.hostaddr.len;
93*2830Sdjl res = memcmp(e1->key.hostaddr.addr, e2->key.hostaddr.addr,
94*2830Sdjl (l2 > l1)?l1:l2);
95*2830Sdjl return ((res) ? _NSC_INT_KEY_CMP(res, 0) : _NSC_INT_KEY_CMP(l1, l2));
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
98*2830Sdjl static uint_t
ipaddr_gethash(nss_XbyY_key_t * key,int htsize)99*2830Sdjl ipaddr_gethash(nss_XbyY_key_t *key, int htsize) {
100*2830Sdjl return (db_gethash(key->hostaddr.addr,
101*2830Sdjl key->hostaddr.len, htsize));
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static void
ipaddr_getlogstr(char * name,char * whoami,size_t len,nss_XbyY_args_t * argp)105*2830Sdjl ipaddr_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
106*2830Sdjl char addr[INET6_ADDRSTRLEN];
1070Sstevel@tonic-gate
108*2830Sdjl if (inet_ntop(argp->key.hostaddr.type, argp->key.hostaddr.addr, addr,
109*2830Sdjl sizeof (addr)) == NULL) {
110*2830Sdjl (void) snprintf(whoami, len, "%s", name);
111*2830Sdjl } else {
112*2830Sdjl (void) snprintf(whoami, len, "%s [key=%s addrtype=%d]",
113*2830Sdjl name,
114*2830Sdjl addr, argp->key.hostaddr.type);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
118*2830Sdjl static int
ipname_compar(const void * n1,const void * n2)119*2830Sdjl ipname_compar(const void *n1, const void *n2) {
120*2830Sdjl nsc_entry_t *e1, *e2;
121*2830Sdjl int res, l1, l2;
1220Sstevel@tonic-gate
123*2830Sdjl e1 = (nsc_entry_t *)n1;
124*2830Sdjl e2 = (nsc_entry_t *)n2;
1250Sstevel@tonic-gate
126*2830Sdjl if (e1->key.ipnode.af_family > e2->key.ipnode.af_family)
127*2830Sdjl return (1);
128*2830Sdjl else if (e1->key.ipnode.af_family < e2->key.ipnode.af_family)
129*2830Sdjl return (-1);
1300Sstevel@tonic-gate
131*2830Sdjl l1 = strlen(e1->key.ipnode.name);
132*2830Sdjl l2 = strlen(e2->key.ipnode.name);
133*2830Sdjl res = strncasecmp(e1->key.ipnode.name, e2->key.ipnode.name,
134*2830Sdjl (l1 > l2)?l1:l2);
135*2830Sdjl return (_NSC_INT_KEY_CMP(res, 0));
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
138*2830Sdjl static uint_t
ipname_gethash(nss_XbyY_key_t * key,int htsize)139*2830Sdjl ipname_gethash(nss_XbyY_key_t *key, int htsize) {
140*2830Sdjl return (cis_gethash(key->ipnode.name, htsize));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
143*2830Sdjl static void
ipname_getlogstr(char * name,char * whoami,size_t len,nss_XbyY_args_t * argp)144*2830Sdjl ipname_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
145*2830Sdjl (void) snprintf(whoami, len, "%s [key=%s:af=%d:flags=%d]",
146*2830Sdjl name,
147*2830Sdjl argp->key.ipnode.name,
148*2830Sdjl argp->key.ipnode.af_family,
149*2830Sdjl argp->key.ipnode.flags);
1500Sstevel@tonic-gate }
151