xref: /illumos-gate/usr/src/cmd/nscd/getether.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 ether_*to* calls in nscd
28  */
29 
30 #include <stdlib.h>
31 #include <net/if.h>
32 #include <netinet/in.h>
33 #include <netinet/if_ether.h>
34 #include <string.h>
35 #include "cache.h"
36 
37 #define	host_db	ctx->nsc_db[0]
38 #define	addr_db	ctx->nsc_db[1]
39 
40 #define	NSC_NAME_ETHERS_HOSTTON	"ether_hostton"
41 #define	NSC_NAME_ETHERS_NTOHOST	"ether_ntohost"
42 
43 static void ether_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
44 static int ether_compar(const void *, const void *);
45 static uint_t ether_gethash(nss_XbyY_key_t *, int);
46 
47 void
48 ether_init_ctx(nsc_ctx_t *ctx) {
49 	ctx->dbname = NSS_DBNAM_ETHERS;
50 	ctx->file_name = "/etc/ethers";
51 	ctx->db_count = 2;
52 	host_db = make_cache(nsc_key_cis,
53 			NSS_DBOP_ETHERS_HOSTTON,
54 			NSC_NAME_ETHERS_HOSTTON,
55 			NULL, NULL, NULL, nsc_ht_default, -1);
56 
57 	addr_db = make_cache(nsc_key_other,
58 			NSS_DBOP_ETHERS_NTOHOST,
59 			NSC_NAME_ETHERS_NTOHOST,
60 			ether_compar,
61 			ether_getlogstr,
62 			ether_gethash, nsc_ht_default, -1);
63 }
64 
65 static int
66 ether_compar(const void *n1, const void *n2) {
67 	nsc_entry_t	*e1, *e2;
68 	int		res;
69 
70 	e1 = (nsc_entry_t *)n1;
71 	e2 = (nsc_entry_t *)n2;
72 	if (ether_cmp(e1->key.ether, e2->key.ether) != 0) {
73 		res = memcmp(e1->key.ether, e2->key.ether,
74 			sizeof (struct ether_addr));
75 		return (_NSC_INT_KEY_CMP(res, 0));
76 	}
77 	return (0);
78 }
79 
80 static uint_t
81 ether_gethash(nss_XbyY_key_t *key, int htsize) {
82 	return (db_gethash(key->ether, sizeof (struct ether_addr),
83 			htsize));
84 }
85 
86 static void
87 ether_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
88 	struct ether_addr *e;
89 	e = (struct ether_addr *)argp->key.ether;
90 	(void) snprintf(whoami, len, "%s [key=%x:%x:%x:%x:%x:%x]",
91 			name,
92 			e->ether_addr_octet[0], e->ether_addr_octet[1],
93 			e->ether_addr_octet[2], e->ether_addr_octet[3],
94 			e->ether_addr_octet[4], e->ether_addr_octet[5]);
95 }
96