xref: /titanic_50/usr/src/lib/nsswitch/files/common/ether_addr.c (revision 36fe4a92b52649b0979d6a13212f4cea730d19c7)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	files/ether_addr.c -- "files" backend for nsswitch "ethers" database
24  *
25  * Copyright 1988-1995,2002 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * All routines necessary to deal with the file /etc/ethers.  The file
33  * contains mappings from 48 bit ethernet addresses to their corresponding
34  * hosts names.  The addresses have an ascii representation of the form
35  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
36  * bytes are always in network order.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
44 #include <nss_dbdefs.h>
45 #include "files_common.h"
46 #include <strings.h>
47 
48 #define	_PATH_ETHERS	"/etc/ethers"
49 
50 static int
51 check_host(args)
52 	nss_XbyY_args_t		*args;
53 {
54 	return (strcmp(args->buf.buffer, args->key.name) == 0);
55 }
56 
57 static nss_status_t
58 getbyhost(be, a)
59 	files_backend_ptr_t	be;
60 	void			*a;
61 {
62 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
63 	char	hostname[MAXHOSTNAMELEN];
64 	nss_status_t		res;
65 
66 	argp->buf.buffer = hostname;
67 	argp->buf.buflen = MAXHOSTNAMELEN;
68 
69 	res = _nss_files_XY_all(be, argp, 0, argp->key.name, check_host);
70 
71 	argp->buf.buffer = NULL;
72 	argp->buf.buflen = 0;
73 	return (res);
74 }
75 
76 static int
77 check_ether(args)
78 	nss_XbyY_args_t		*args;
79 {
80 	return (ether_cmp(args->buf.result, args->key.ether) == 0);
81 }
82 
83 static nss_status_t
84 getbyether(be, a)
85 	files_backend_ptr_t	be;
86 	void			*a;
87 {
88 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
89 	struct ether_addr	etheraddr;
90 	nss_status_t		res;
91 
92 	argp->buf.result	= &etheraddr;
93 
94 	res = _nss_files_XY_all(be, argp, 0, NULL, check_ether);
95 
96 	argp->buf.result	= NULL;
97 	return (res);
98 }
99 
100 static files_backend_op_t ethers_ops[] = {
101 	_nss_files_destr,
102 	getbyhost,
103 	getbyether
104 };
105 
106 /*ARGSUSED*/
107 nss_backend_t *
108 _nss_files_ethers_constr(dummy1, dummy2, dummy3)
109 	const char	*dummy1, *dummy2, *dummy3;
110 {
111 	return (_nss_files_constr(ethers_ops,
112 				sizeof (ethers_ops) / sizeof (ethers_ops[0]),
113 				_PATH_ETHERS,
114 				NSS_LINELEN_ETHERS,
115 				NULL));
116 }
117