xref: /titanic_51/usr/src/cmd/nscd/gethost.c (revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8)
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 gethost* calls in nscd
30  */
31 
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include "cache.h"
39 
40 #define	hnam_db	ctx->nsc_db[0]
41 #define	addr_db	ctx->nsc_db[1]
42 
43 #define	NSC_NAME_HOSTS_BYNAME	"gethostbyname"
44 #define	NSC_NAME_HOSTS_BYADDR	"gethostbyaddr"
45 
46 static int hostaddr_compar(const void *, const void *);
47 static uint_t hostaddr_gethash(nss_XbyY_key_t *, int);
48 static void hostaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
49 
50 void
51 host_init_ctx(nsc_ctx_t *ctx) {
52 	ctx->dbname = NSS_DBNAM_HOSTS;
53 	ctx->db_count = 2;
54 	ctx->file_name = "/etc/inet/hosts";
55 
56 	hnam_db = make_cache(nsc_key_cis,
57 			NSS_DBOP_HOSTS_BYNAME,
58 			NSC_NAME_HOSTS_BYNAME,
59 			NULL, NULL, NULL, nsc_ht_default, -1);
60 
61 	addr_db = make_cache(nsc_key_other,
62 			NSS_DBOP_HOSTS_BYADDR,
63 			NSC_NAME_HOSTS_BYADDR,
64 			hostaddr_compar,
65 			hostaddr_getlogstr,
66 			hostaddr_gethash, nsc_ht_default, -1);
67 }
68 
69 static void
70 hostaddr_getlogstr(char *name, char *whoami, size_t len,
71 			nss_XbyY_args_t *argp) {
72 	char addr[INET6_ADDRSTRLEN];
73 
74 	if (inet_ntop(argp->key.hostaddr.type, argp->key.hostaddr.addr, addr,
75 			sizeof (addr)) == NULL) {
76 		(void) snprintf(whoami, len, "%s", name);
77 	} else {
78 		(void) snprintf(whoami, len, "%s [key=%s, addrtype=%d]",
79 			name, addr, argp->key.hostaddr.type);
80 	}
81 }
82 
83 static int
84 hostaddr_compar(const void *n1, const void *n2) {
85 	nsc_entry_t	*e1, *e2;
86 	int		res, l1, l2;
87 
88 	e1 = (nsc_entry_t *)n1;
89 	e2 = (nsc_entry_t *)n2;
90 	l1 = e1->key.hostaddr.len;
91 	l2 = e2->key.hostaddr.len;
92 	res = memcmp(e1->key.hostaddr.addr, e2->key.hostaddr.addr,
93 		(l2 > l1)?l1:l2);
94 	return ((res) ? _NSC_INT_KEY_CMP(res, 0) : _NSC_INT_KEY_CMP(l1, l2));
95 }
96 
97 static uint_t
98 hostaddr_gethash(nss_XbyY_key_t *key, int htsize) {
99 	return (db_gethash(key->hostaddr.addr,
100 			key->hostaddr.len, htsize));
101 }
102