xref: /titanic_50/usr/src/lib/nsswitch/ldap/common/getether.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
32 #include <net/if_arp.h>
33 #include <netinet/if_ether.h>
34 #include "ldap_common.h"
35 
36 /* ether attributes filters */
37 #define	_E_HOSTNAME		"cn"
38 #define	_E_MACADDRESS		"macaddress"
39 #define	_F_GETETHERBYHOST	"(&(objectClass=ieee802Device)(cn=%s))"
40 #define	_F_GETETHERBYHOST_SSD	"(&(%%s)(cn=%s))"
41 #define	_F_GETETHERBYETHER	"(&(objectClass=ieee802Device)(macAddress=%s))"
42 #define	_F_GETETHERBYETHER_SSD	"(&(%%s)(macAddress=%s))"
43 
44 static const char *ethers_attrs[] = {
45 	_E_HOSTNAME,
46 	_E_MACADDRESS,
47 	(char *)NULL
48 };
49 
50 /*
51  * _nss_ldap_ethers2str is the data marshaling method for the ethers
52  * ether_hostton/ether_ntohost backend processes.
53  * This method is called after a successful ldap search has been performed.
54  * This method will parse the ldap search values into the file format.
55  * e.g.
56  *
57  * 8:0:20:8e:eb:8a8 borealis
58  *
59  * The front end marshaller str2ether uses argp->buf.result for a different
60  * purpose so a flag be->db_type is set to work around this oddity.
61  *
62  */
63 /*ARGSUSED0*/
64 static int
65 _nss_ldap_ethers2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
66 {
67 	int			nss_result;
68 	ns_ldap_result_t	*result = be->result;
69 	char			**host, **macaddress;
70 
71 	if (result == NULL)
72 		return (NSS_STR_PARSE_PARSE);
73 	nss_result = NSS_STR_PARSE_SUCCESS;
74 
75 	host = __ns_ldap_getAttr(result->entry, _E_HOSTNAME);
76 	if (host == NULL || host[0] == NULL || (strlen(host[0]) < 1)) {
77 			nss_result = NSS_STR_PARSE_PARSE;
78 			goto result_ea2str;
79 	}
80 	macaddress = __ns_ldap_getAttr(result->entry, _E_MACADDRESS);
81 	if (macaddress == NULL || macaddress[0] == NULL ||
82 				(strlen(macaddress[0]) < 1)) {
83 			nss_result = NSS_STR_PARSE_PARSE;
84 			goto result_ea2str;
85 	}
86 	be->buflen = strlen(host[0]) + strlen(macaddress[0]) + 1; /* ' ' */
87 	/* Add a trailing null for easy debug */
88 	be->buffer = calloc(1, be->buflen + 1);
89 	if (be->buffer == NULL) {
90 		nss_result = NSS_STR_PARSE_PARSE;
91 		goto result_ea2str;
92 	}
93 
94 	(void) snprintf(be->buffer, be->buflen + 1, "%s %s",
95 			macaddress[0], host[0]);
96 	be->db_type = NSS_LDAP_DB_ETHERS;
97 
98 result_ea2str:
99 
100 	(void) __ns_ldap_freeResult(&be->result);
101 	return (nss_result);
102 }
103 
104 /*
105  * getbyhost gets an ethernet address by hostname. This function
106  * constructs an ldap search filter using the hostname invocation
107  * parameter and the getetherbyhost search filter defined. Once
108  * the filter is constructed, we search for a matching entry and
109  * marshal the data results into uchar_t *ether for the frontend
110  * process. The function _nss_ldap_ethers2ent performs the data
111  * marshaling.
112  *
113  * RFC 2307, An Approach for Using LDAP as a Network Information Service,
114  * indicates that dn's be fully qualified. Host name searches will be on
115  * fully qualified host names (e.g., foo.bar.sun.com).
116  */
117 
118 static nss_status_t
119 getbyhost(ldap_backend_ptr be, void *a)
120 {
121 	char		hostname[3 * MAXHOSTNAMELEN];
122 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
123 	char		searchfilter[SEARCHFILTERLEN];
124 	char		userdata[SEARCHFILTERLEN];
125 	int		ret;
126 	nss_status_t	rc;
127 
128 	if (_ldap_filter_name(hostname, argp->key.name, sizeof (hostname)) != 0)
129 		return ((nss_status_t)NSS_NOTFOUND);
130 
131 	ret = snprintf(searchfilter, sizeof (searchfilter),
132 	    _F_GETETHERBYHOST, hostname);
133 
134 	if (ret >= sizeof (searchfilter) || ret < 0)
135 		return ((nss_status_t)NSS_NOTFOUND);
136 
137 	ret = snprintf(userdata, sizeof (userdata),
138 	    _F_GETETHERBYHOST_SSD, hostname);
139 
140 	if (ret >= sizeof (userdata) || ret < 0)
141 		return ((nss_status_t)NSS_NOTFOUND);
142 
143 	rc = (nss_status_t)_nss_ldap_lookup(be, argp,
144 		_ETHERS, searchfilter, NULL,
145 		_merge_SSD_filter, userdata);
146 
147 	return (rc);
148 }
149 
150 
151 /*
152  * getbyether gets an ethernet address by ethernet address. This
153  * function constructs an ldap search filter using the ASCII
154  * ethernet address invocation parameter and the getetherbyether
155  * search filter defined. Once the filter is constructed, we
156  * search for a matching entry and  marshal the data results into
157  * uchar_t *ether for the frontend process. The function
158  * _nss_ldap_ethers2ent performs the data marshaling.
159  */
160 
161 static nss_status_t
162 getbyether(ldap_backend_ptr be, void *a)
163 {
164 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
165 	char		etherstr[18];
166 	uchar_t	*e = argp->key.ether;
167 	char		searchfilter[SEARCHFILTERLEN];
168 	char		userdata[SEARCHFILTERLEN];
169 	int		ret;
170 
171 	ret = snprintf(etherstr, sizeof (etherstr), "%x:%x:%x:%x:%x:%x",
172 	    *e, *(e + 1), *(e + 2), *(e + 3), *(e + 4), *(e + 5));
173 	if (ret >= sizeof (etherstr) || ret < 0)
174 		return ((nss_status_t)NSS_NOTFOUND);
175 
176 	ret = snprintf(searchfilter, sizeof (searchfilter),
177 	    _F_GETETHERBYETHER, etherstr);
178 	if (ret >= sizeof (searchfilter) || ret < 0)
179 		return ((nss_status_t)NSS_NOTFOUND);
180 
181 	ret = snprintf(userdata, sizeof (userdata),
182 	    _F_GETETHERBYETHER_SSD, etherstr);
183 	if (ret >= sizeof (userdata) || ret < 0)
184 		return ((nss_status_t)NSS_NOTFOUND);
185 
186 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
187 		_ETHERS, searchfilter, NULL,
188 		_merge_SSD_filter, userdata));
189 }
190 
191 
192 static ldap_backend_op_t ethers_ops[] = {
193 	_nss_ldap_destr,
194 	getbyhost,
195 	getbyether
196 };
197 
198 
199 /*
200  * _nss_ldap_ethers_constr is where life begins. This function calls the
201  * generic ldap constructor function to define and build the abstract
202  * data types required to support ldap operations.
203  */
204 
205 /*ARGSUSED0*/
206 nss_backend_t *
207 _nss_ldap_ethers_constr(const char *dummy1, const char *dummy2,
208 			const char *dummy3)
209 {
210 
211 	return ((nss_backend_t *)_nss_ldap_constr(ethers_ops,
212 		sizeof (ethers_ops)/sizeof (ethers_ops[0]), _ETHERS,
213 		ethers_attrs, _nss_ldap_ethers2str));
214 }
215