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