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