xref: /titanic_51/usr/src/lib/nsswitch/ldap/common/getether.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 /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/socket.h>
307c478bd9Sstevel@tonic-gate #include <net/if.h>
317c478bd9Sstevel@tonic-gate #include <netinet/in.h>
327c478bd9Sstevel@tonic-gate #include <net/if_arp.h>
337c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
347c478bd9Sstevel@tonic-gate #include "ldap_common.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* ether attributes filters */
377c478bd9Sstevel@tonic-gate #define	_E_HOSTNAME		"cn"
387c478bd9Sstevel@tonic-gate #define	_E_MACADDRESS		"macaddress"
397c478bd9Sstevel@tonic-gate #define	_F_GETETHERBYHOST	"(&(objectClass=ieee802Device)(cn=%s))"
407c478bd9Sstevel@tonic-gate #define	_F_GETETHERBYHOST_SSD	"(&(%%s)(cn=%s))"
417c478bd9Sstevel@tonic-gate #define	_F_GETETHERBYETHER	"(&(objectClass=ieee802Device)(macAddress=%s))"
427c478bd9Sstevel@tonic-gate #define	_F_GETETHERBYETHER_SSD	"(&(%%s)(macAddress=%s))"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static const char *ethers_attrs[] = {
457c478bd9Sstevel@tonic-gate 	_E_HOSTNAME,
467c478bd9Sstevel@tonic-gate 	_E_MACADDRESS,
477c478bd9Sstevel@tonic-gate 	(char *)NULL
487c478bd9Sstevel@tonic-gate };
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
51*cb5caa98Sdjl  * _nss_ldap_ethers2str is the data marshaling method for the ethers
52*cb5caa98Sdjl  * ether_hostton/ether_ntohost backend processes.
53*cb5caa98Sdjl  * This method is called after a successful ldap search has been performed.
54*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
55*cb5caa98Sdjl  * e.g.
567c478bd9Sstevel@tonic-gate  *
57*cb5caa98Sdjl  * 8:0:20:8e:eb:8a8 borealis
587c478bd9Sstevel@tonic-gate  *
59*cb5caa98Sdjl  * The front end marshaller str2ether uses argp->buf.result for a different
60*cb5caa98Sdjl  * purpose so a flag be->db_type is set to work around this oddity.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  */
63*cb5caa98Sdjl /*ARGSUSED0*/
647c478bd9Sstevel@tonic-gate static int
65*cb5caa98Sdjl _nss_ldap_ethers2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	int			nss_result;
687c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
69*cb5caa98Sdjl 	char			**host, **macaddress;
707c478bd9Sstevel@tonic-gate 
71*cb5caa98Sdjl 	if (result == NULL)
72*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
73*cb5caa98Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
74*cb5caa98Sdjl 
75*cb5caa98Sdjl 	host = __ns_ldap_getAttr(result->entry, _E_HOSTNAME);
76*cb5caa98Sdjl 	if (host == NULL || host[0] == NULL || (strlen(host[0]) < 1)) {
77*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
78*cb5caa98Sdjl 			goto result_ea2str;
79*cb5caa98Sdjl 	}
80*cb5caa98Sdjl 	macaddress = __ns_ldap_getAttr(result->entry, _E_MACADDRESS);
81*cb5caa98Sdjl 	if (macaddress == NULL || macaddress[0] == NULL ||
82*cb5caa98Sdjl 				(strlen(macaddress[0]) < 1)) {
83*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
84*cb5caa98Sdjl 			goto result_ea2str;
85*cb5caa98Sdjl 	}
86*cb5caa98Sdjl 	be->buflen = strlen(host[0]) + strlen(macaddress[0]) + 1; /* ' ' */
87*cb5caa98Sdjl 	/* Add a trailing null for easy debug */
88*cb5caa98Sdjl 	be->buffer = calloc(1, be->buflen + 1);
89*cb5caa98Sdjl 	if (be->buffer == NULL) {
90*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
91*cb5caa98Sdjl 		goto result_ea2str;
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
94*cb5caa98Sdjl 	(void) snprintf(be->buffer, be->buflen + 1, "%s %s",
95*cb5caa98Sdjl 			macaddress[0], host[0]);
96*cb5caa98Sdjl 	be->db_type = NSS_LDAP_DB_ETHERS;
977c478bd9Sstevel@tonic-gate 
98*cb5caa98Sdjl result_ea2str:
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
101*cb5caa98Sdjl 	return (nss_result);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * getbyhost gets an ethernet address by hostname. This function
1067c478bd9Sstevel@tonic-gate  * constructs an ldap search filter using the hostname invocation
1077c478bd9Sstevel@tonic-gate  * parameter and the getetherbyhost search filter defined. Once
1087c478bd9Sstevel@tonic-gate  * the filter is constructed, we search for a matching entry and
1097c478bd9Sstevel@tonic-gate  * marshal the data results into uchar_t *ether for the frontend
1107c478bd9Sstevel@tonic-gate  * process. The function _nss_ldap_ethers2ent performs the data
1117c478bd9Sstevel@tonic-gate  * marshaling.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * RFC 2307, An Approach for Using LDAP as a Network Information Service,
1147c478bd9Sstevel@tonic-gate  * indicates that dn's be fully qualified. Host name searches will be on
1157c478bd9Sstevel@tonic-gate  * fully qualified host names (e.g., foo.bar.sun.com).
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static nss_status_t
1197c478bd9Sstevel@tonic-gate getbyhost(ldap_backend_ptr be, void *a)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	char		hostname[3 * MAXHOSTNAMELEN];
1227c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1237c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1247c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1257c478bd9Sstevel@tonic-gate 	int		ret;
126*cb5caa98Sdjl 	nss_status_t	rc;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(hostname, argp->key.name, sizeof (hostname)) != 0)
1297c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1327c478bd9Sstevel@tonic-gate 	    _F_GETETHERBYHOST, hostname);
133*cb5caa98Sdjl 
1347c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
1357c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1387c478bd9Sstevel@tonic-gate 	    _F_GETETHERBYHOST_SSD, hostname);
139*cb5caa98Sdjl 
1407c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1417c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1427c478bd9Sstevel@tonic-gate 
143*cb5caa98Sdjl 	rc = (nss_status_t)_nss_ldap_lookup(be, argp,
1447c478bd9Sstevel@tonic-gate 		_ETHERS, searchfilter, NULL,
145*cb5caa98Sdjl 		_merge_SSD_filter, userdata);
146*cb5caa98Sdjl 
147*cb5caa98Sdjl 	return (rc);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * getbyether gets an ethernet address by ethernet address. This
1537c478bd9Sstevel@tonic-gate  * function constructs an ldap search filter using the ASCII
1547c478bd9Sstevel@tonic-gate  * ethernet address invocation parameter and the getetherbyether
1557c478bd9Sstevel@tonic-gate  * search filter defined. Once the filter is constructed, we
1567c478bd9Sstevel@tonic-gate  * search for a matching entry and  marshal the data results into
1577c478bd9Sstevel@tonic-gate  * uchar_t *ether for the frontend process. The function
1587c478bd9Sstevel@tonic-gate  * _nss_ldap_ethers2ent performs the data marshaling.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static nss_status_t
1627c478bd9Sstevel@tonic-gate getbyether(ldap_backend_ptr be, void *a)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1657c478bd9Sstevel@tonic-gate 	char		etherstr[18];
1667c478bd9Sstevel@tonic-gate 	uchar_t	*e = argp->key.ether;
1677c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1687c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1697c478bd9Sstevel@tonic-gate 	int		ret;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	ret = snprintf(etherstr, sizeof (etherstr), "%x:%x:%x:%x:%x:%x",
1727c478bd9Sstevel@tonic-gate 	    *e, *(e + 1), *(e + 2), *(e + 3), *(e + 4), *(e + 5));
1737c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (etherstr) || ret < 0)
1747c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1777c478bd9Sstevel@tonic-gate 	    _F_GETETHERBYETHER, etherstr);
1787c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
1797c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1827c478bd9Sstevel@tonic-gate 	    _F_GETETHERBYETHER_SSD, etherstr);
1837c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1847c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
1877c478bd9Sstevel@tonic-gate 		_ETHERS, searchfilter, NULL,
1887c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static ldap_backend_op_t ethers_ops[] = {
1937c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
1947c478bd9Sstevel@tonic-gate 	getbyhost,
1957c478bd9Sstevel@tonic-gate 	getbyether
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * _nss_ldap_ethers_constr is where life begins. This function calls the
2017c478bd9Sstevel@tonic-gate  * generic ldap constructor function to define and build the abstract
2027c478bd9Sstevel@tonic-gate  * data types required to support ldap operations.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
2067c478bd9Sstevel@tonic-gate nss_backend_t *
2077c478bd9Sstevel@tonic-gate _nss_ldap_ethers_constr(const char *dummy1, const char *dummy2,
2087c478bd9Sstevel@tonic-gate 			const char *dummy3)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(ethers_ops,
2127c478bd9Sstevel@tonic-gate 		sizeof (ethers_ops)/sizeof (ethers_ops[0]), _ETHERS,
213*cb5caa98Sdjl 		ethers_attrs, _nss_ldap_ethers2str));
2147c478bd9Sstevel@tonic-gate }
215