xref: /titanic_44/usr/src/lib/nsswitch/files/common/ether_addr.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * files/ether_addr.c -- "files" backend for nsswitch "ethers" database
237c478bd9Sstevel@tonic-gate  *
24*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * All routines necessary to deal with the file /etc/ethers.  The file
327c478bd9Sstevel@tonic-gate  * contains mappings from 48 bit ethernet addresses to their corresponding
337c478bd9Sstevel@tonic-gate  * hosts names.  The addresses have an ascii representation of the form
347c478bd9Sstevel@tonic-gate  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
357c478bd9Sstevel@tonic-gate  * bytes are always in network order.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <net/if.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
437c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
447c478bd9Sstevel@tonic-gate #include "files_common.h"
457c478bd9Sstevel@tonic-gate #include <strings.h>
46*cb5caa98Sdjl #include <ctype.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	_PATH_ETHERS	"/etc/ethers"
49*cb5caa98Sdjl #define	DIGIT(x)	\
50*cb5caa98Sdjl 	(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static int
check_host(nss_XbyY_args_t * argp,const char * line,int linelen)53*cb5caa98Sdjl check_host(nss_XbyY_args_t *argp, const char *line, int linelen)
547c478bd9Sstevel@tonic-gate {
55*cb5caa98Sdjl 	const char	*limit, *linep, *keyp;
56*cb5caa98Sdjl 	linep = line;
57*cb5caa98Sdjl 	limit = line + linelen;
58*cb5caa98Sdjl 
59*cb5caa98Sdjl 	/* skip leading spaces */
60*cb5caa98Sdjl 	while (linep < limit && isspace(*linep))
61*cb5caa98Sdjl 		linep++;
62*cb5caa98Sdjl 	/* skip mac address */
63*cb5caa98Sdjl 	while (linep < limit && !isspace(*linep))
64*cb5caa98Sdjl 		linep++;
65*cb5caa98Sdjl 	/* skip the delimiting spaces */
66*cb5caa98Sdjl 	while (linep < limit && isspace(*linep))
67*cb5caa98Sdjl 		linep++;
68*cb5caa98Sdjl 	if (linep == limit)
69*cb5caa98Sdjl 		return (0);
70*cb5caa98Sdjl 
71*cb5caa98Sdjl 	/* compare the host name */
72*cb5caa98Sdjl 	keyp = argp->key.name;
73*cb5caa98Sdjl 	while (*keyp != '\0' && linep < limit && *keyp == *linep) {
74*cb5caa98Sdjl 		keyp++;
75*cb5caa98Sdjl 		linep++;
76*cb5caa98Sdjl 	}
77*cb5caa98Sdjl 	return (*keyp == '\0' && linep == limit);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static nss_status_t
getbyhost(be,a)817c478bd9Sstevel@tonic-gate getbyhost(be, a)
827c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
837c478bd9Sstevel@tonic-gate 	void			*a;
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
867c478bd9Sstevel@tonic-gate 	char	hostname[MAXHOSTNAMELEN];
877c478bd9Sstevel@tonic-gate 	nss_status_t		res;
887c478bd9Sstevel@tonic-gate 
89*cb5caa98Sdjl 	/*
90*cb5caa98Sdjl 	 * use the buffer passed in if result is to be returned
91*cb5caa98Sdjl 	 * in /etc file format
92*cb5caa98Sdjl 	 */
93*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
947c478bd9Sstevel@tonic-gate 		argp->buf.buffer = hostname;
957c478bd9Sstevel@tonic-gate 		argp->buf.buflen = MAXHOSTNAMELEN;
96*cb5caa98Sdjl 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	res = _nss_files_XY_all(be, argp, 0, argp->key.name, check_host);
997c478bd9Sstevel@tonic-gate 
100*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
1017c478bd9Sstevel@tonic-gate 		argp->buf.buffer = NULL;
1027c478bd9Sstevel@tonic-gate 		argp->buf.buflen = 0;
103*cb5caa98Sdjl 	}
104*cb5caa98Sdjl 
1057c478bd9Sstevel@tonic-gate 	return (res);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static int
check_ether(nss_XbyY_args_t * argp,const char * line,int linelen)109*cb5caa98Sdjl check_ether(nss_XbyY_args_t *argp, const char *line, int linelen)
1107c478bd9Sstevel@tonic-gate {
111*cb5caa98Sdjl 
112*cb5caa98Sdjl 	const char	*limit, *linep;
113*cb5caa98Sdjl 	uchar_t		ether[6];
114*cb5caa98Sdjl 	ptrdiff_t	i;
115*cb5caa98Sdjl 	int		n;
116*cb5caa98Sdjl 
117*cb5caa98Sdjl 	linep = line;
118*cb5caa98Sdjl 	limit = line + linelen;
119*cb5caa98Sdjl 
120*cb5caa98Sdjl 	/* skip leading spaces */
121*cb5caa98Sdjl 	while (linep < limit && isspace(*linep))
122*cb5caa98Sdjl 		linep++;
123*cb5caa98Sdjl 
124*cb5caa98Sdjl 	for (i = 0; i < 6; i++) {
125*cb5caa98Sdjl 		n = 0;
126*cb5caa98Sdjl 		while (linep < limit && isxdigit(*linep)) {
127*cb5caa98Sdjl 			n = 16 * n + DIGIT(*linep);
128*cb5caa98Sdjl 			linep++;
129*cb5caa98Sdjl 		}
130*cb5caa98Sdjl 		if (*linep != ':' && i < 5) {
131*cb5caa98Sdjl 			return (0);
132*cb5caa98Sdjl 		} else if (*linep == ':' && i == 5) {
133*cb5caa98Sdjl 			return (0);
134*cb5caa98Sdjl 		} else {
135*cb5caa98Sdjl 			linep++;
136*cb5caa98Sdjl 			ether[i] = (uchar_t)n;
137*cb5caa98Sdjl 		}
138*cb5caa98Sdjl 	}
139*cb5caa98Sdjl 	return (ether_cmp((void *)ether, (void *)argp->key.ether) == 0);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static nss_status_t
getbyether(be,a)1437c478bd9Sstevel@tonic-gate getbyether(be, a)
1447c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
1457c478bd9Sstevel@tonic-gate 	void			*a;
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
1487c478bd9Sstevel@tonic-gate 	struct ether_addr	etheraddr;
1497c478bd9Sstevel@tonic-gate 	nss_status_t		res;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	argp->buf.result	= &etheraddr;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	res = _nss_files_XY_all(be, argp, 0, NULL, check_ether);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	argp->buf.result	= NULL;
1567c478bd9Sstevel@tonic-gate 	return (res);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static files_backend_op_t ethers_ops[] = {
1607c478bd9Sstevel@tonic-gate 	_nss_files_destr,
1617c478bd9Sstevel@tonic-gate 	getbyhost,
1627c478bd9Sstevel@tonic-gate 	getbyether
1637c478bd9Sstevel@tonic-gate };
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1667c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_files_ethers_constr(dummy1,dummy2,dummy3)1677c478bd9Sstevel@tonic-gate _nss_files_ethers_constr(dummy1, dummy2, dummy3)
1687c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	return (_nss_files_constr(ethers_ops,
1717c478bd9Sstevel@tonic-gate 				sizeof (ethers_ops) / sizeof (ethers_ops[0]),
1727c478bd9Sstevel@tonic-gate 				_PATH_ETHERS,
1737c478bd9Sstevel@tonic-gate 				NSS_LINELEN_ETHERS,
1747c478bd9Sstevel@tonic-gate 				NULL));
1757c478bd9Sstevel@tonic-gate }
176