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