xref: /titanic_41/usr/src/lib/nsswitch/files/common/ether_addr.c (revision eb0cc229f19c437a6b538d3ac0d0443268290b7e)
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  * files/ether_addr.c -- "files" backend for nsswitch "ethers" database
23  *
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * All routines necessary to deal with the file /etc/ethers.  The file
32  * contains mappings from 48 bit ethernet addresses to their corresponding
33  * hosts names.  The addresses have an ascii representation of the form
34  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
35  * bytes are always in network order.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <net/if.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 #include <nss_dbdefs.h>
44 #include "files_common.h"
45 #include <strings.h>
46 #include <ctype.h>
47 
48 #define	_PATH_ETHERS	"/etc/ethers"
49 #define	DIGIT(x)	\
50 	(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
51 
52 static int
53 check_host(nss_XbyY_args_t *argp, const char *line, int linelen)
54 {
55 	const char	*limit, *linep, *keyp;
56 	linep = line;
57 	limit = line + linelen;
58 
59 	/* skip leading spaces */
60 	while (linep < limit && isspace(*linep))
61 		linep++;
62 	/* skip mac address */
63 	while (linep < limit && !isspace(*linep))
64 		linep++;
65 	/* skip the delimiting spaces */
66 	while (linep < limit && isspace(*linep))
67 		linep++;
68 	if (linep == limit)
69 		return (0);
70 
71 	/* compare the host name */
72 	keyp = argp->key.name;
73 	while (*keyp != '\0' && linep < limit && *keyp == *linep) {
74 		keyp++;
75 		linep++;
76 	}
77 	return (*keyp == '\0' && linep == limit);
78 }
79 
80 static nss_status_t
81 getbyhost(be, a)
82 	files_backend_ptr_t	be;
83 	void			*a;
84 {
85 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
86 	char	hostname[MAXHOSTNAMELEN];
87 	nss_status_t		res;
88 
89 	/*
90 	 * use the buffer passed in if result is to be returned
91 	 * in /etc file format
92 	 */
93 	if (argp->buf.result != NULL) {
94 		argp->buf.buffer = hostname;
95 		argp->buf.buflen = MAXHOSTNAMELEN;
96 	}
97 
98 	res = _nss_files_XY_all(be, argp, 0, argp->key.name, check_host);
99 
100 	if (argp->buf.result != NULL) {
101 		argp->buf.buffer = NULL;
102 		argp->buf.buflen = 0;
103 	}
104 
105 	return (res);
106 }
107 
108 static int
109 check_ether(nss_XbyY_args_t *argp, const char *line, int linelen)
110 {
111 
112 	const char	*limit, *linep;
113 	uchar_t		ether[6];
114 	ptrdiff_t	i;
115 	int		n;
116 
117 	linep = line;
118 	limit = line + linelen;
119 
120 	/* skip leading spaces */
121 	while (linep < limit && isspace(*linep))
122 		linep++;
123 
124 	for (i = 0; i < 6; i++) {
125 		n = 0;
126 		while (linep < limit && isxdigit(*linep)) {
127 			n = 16 * n + DIGIT(*linep);
128 			linep++;
129 		}
130 		if (*linep != ':' && i < 5) {
131 			return (0);
132 		} else if (*linep == ':' && i == 5) {
133 			return (0);
134 		} else {
135 			linep++;
136 			ether[i] = (uchar_t)n;
137 		}
138 	}
139 	return (ether_cmp((void *)ether, (void *)argp->key.ether) == 0);
140 }
141 
142 static nss_status_t
143 getbyether(be, a)
144 	files_backend_ptr_t	be;
145 	void			*a;
146 {
147 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
148 	struct ether_addr	etheraddr;
149 	nss_status_t		res;
150 
151 	argp->buf.result	= &etheraddr;
152 
153 	res = _nss_files_XY_all(be, argp, 0, NULL, check_ether);
154 
155 	argp->buf.result	= NULL;
156 	return (res);
157 }
158 
159 static files_backend_op_t ethers_ops[] = {
160 	_nss_files_destr,
161 	getbyhost,
162 	getbyether
163 };
164 
165 /*ARGSUSED*/
166 nss_backend_t *
167 _nss_files_ethers_constr(dummy1, dummy2, dummy3)
168 	const char	*dummy1, *dummy2, *dummy3;
169 {
170 	return (_nss_files_constr(ethers_ops,
171 				sizeof (ethers_ops) / sizeof (ethers_ops[0]),
172 				_PATH_ETHERS,
173 				NSS_LINELEN_ETHERS,
174 				NULL));
175 }
176