xref: /titanic_50/usr/src/cmd/idmap/idmapd/nldaputils.c (revision 148c5f43199ca0b43fc8e3b643aab11cd66ea327)
1e8c27ec8Sbaban /*
2e8c27ec8Sbaban  * CDDL HEADER START
3e8c27ec8Sbaban  *
4e8c27ec8Sbaban  * The contents of this file are subject to the terms of the
5e8c27ec8Sbaban  * Common Development and Distribution License (the "License").
6e8c27ec8Sbaban  * You may not use this file except in compliance with the License.
7e8c27ec8Sbaban  *
8e8c27ec8Sbaban  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e8c27ec8Sbaban  * or http://www.opensolaris.org/os/licensing.
10e8c27ec8Sbaban  * See the License for the specific language governing permissions
11e8c27ec8Sbaban  * and limitations under the License.
12e8c27ec8Sbaban  *
13e8c27ec8Sbaban  * When distributing Covered Code, include this CDDL HEADER in each
14e8c27ec8Sbaban  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e8c27ec8Sbaban  * If applicable, add the following below this CDDL HEADER, with the
16e8c27ec8Sbaban  * fields enclosed by brackets "[]" replaced with your own identifying
17e8c27ec8Sbaban  * information: Portions Copyright [yyyy] [name of copyright owner]
18e8c27ec8Sbaban  *
19e8c27ec8Sbaban  * CDDL HEADER END
20e8c27ec8Sbaban  */
21e8c27ec8Sbaban 
22e8c27ec8Sbaban /*
23*148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24e8c27ec8Sbaban  */
25e8c27ec8Sbaban 
26e8c27ec8Sbaban /*
27e8c27ec8Sbaban  * native LDAP related utility routines
28e8c27ec8Sbaban  */
29e8c27ec8Sbaban 
30e8c27ec8Sbaban #include "idmapd.h"
31479ac375Sdm199847 #include "idmap_priv.h"
32479ac375Sdm199847 #include "ns_sldap.h"
33479ac375Sdm199847 #include "nldaputils.h"
34479ac375Sdm199847 #include <assert.h>
35479ac375Sdm199847 
36479ac375Sdm199847 /*
37479ac375Sdm199847  * The following are format strings used to construct LDAP search filters
38479ac375Sdm199847  * when looking up Native LDAP directory service. The _F_XXX_SSD format
39479ac375Sdm199847  * is used by the libsldap API if a corresponding SSD is defined in
40479ac375Sdm199847  * Native LDAP configuration. The SSD contains a string that replaces
41479ac375Sdm199847  * the first %s in _F_XXX_SSD. If no SSD is defined then the regular
42479ac375Sdm199847  * _F_XXX format is used.
43479ac375Sdm199847  *
44479ac375Sdm199847  * Note that '\\' needs to be represented as "\\5c" in LDAP filters.
45479ac375Sdm199847  */
46479ac375Sdm199847 
47479ac375Sdm199847 /* Native LDAP lookup using UNIX username */
48479ac375Sdm199847 #define	_F_GETPWNAM		"(&(objectClass=posixAccount)(uid=%s))"
49479ac375Sdm199847 #define	_F_GETPWNAM_SSD		"(&(%%s)(uid=%s))"
50479ac375Sdm199847 
51479ac375Sdm199847 /*
52479ac375Sdm199847  * Native LDAP user lookup using names of well-known SIDs
53479ac375Sdm199847  * Note the use of 1$, 2$ in the format string which basically
54479ac375Sdm199847  * allows snprintf to re-use its first two arguments.
55479ac375Sdm199847  */
56479ac375Sdm199847 #define	_F_GETPWWNAMWK \
57479ac375Sdm199847 		"(&(objectClass=posixAccount)(|(%s=%s)(%1$s=BUILTIN\\5c%2$s)))"
58479ac375Sdm199847 #define	_F_GETPWWNAMWK_SSD	"(&(%%s)(|(%s=%s)(%1$s=BUILTIN\\5c%2$s)))"
59479ac375Sdm199847 
60479ac375Sdm199847 /* Native LDAP user lookup using winname@windomain OR windomain\winname */
61479ac375Sdm199847 #define	_F_GETPWWNAMDOM \
62479ac375Sdm199847 	"(&(objectClass=posixAccount)(|(%s=%s@%s)(%1$s=%3$s\\5c%2$s)))"
63479ac375Sdm199847 #define	_F_GETPWWNAMDOM_SSD	"(&(%%s)(|(%s=%s@%s)(%1$s=%3$s\\5c%2$s)))"
64479ac375Sdm199847 
65479ac375Sdm199847 /* Native LDAP lookup using UID */
66479ac375Sdm199847 #define	_F_GETPWUID		"(&(objectClass=posixAccount)(uidNumber=%u))"
67479ac375Sdm199847 #define	_F_GETPWUID_SSD		"(&(%%s)(uidNumber=%u))"
68479ac375Sdm199847 
69479ac375Sdm199847 /* Native LDAP lookup using UNIX groupname */
70479ac375Sdm199847 #define	_F_GETGRNAM		"(&(objectClass=posixGroup)(cn=%s))"
71479ac375Sdm199847 #define	_F_GETGRNAM_SSD		"(&(%%s)(cn=%s))"
72479ac375Sdm199847 
73479ac375Sdm199847 /* Native LDAP group lookup using names of well-known SIDs */
74479ac375Sdm199847 #define	_F_GETGRWNAMWK \
75479ac375Sdm199847 		"(&(objectClass=posixGroup)(|(%s=%s)(%1$s=BUILTIN\\5c%2$s)))"
76479ac375Sdm199847 #define	_F_GETGRWNAMWK_SSD	"(&(%%s)(|(%s=%s)(%1$s=BUILTIN\\5c%2$s)))"
77479ac375Sdm199847 
78479ac375Sdm199847 /* Native LDAP group lookup using winname@windomain OR windomain\winname */
79479ac375Sdm199847 #define	_F_GETGRWNAMDOM \
80479ac375Sdm199847 		"(&(objectClass=posixGroup)(|(%s=%s@%s)(%1$s=%3$s\\5c%2$s)))"
81479ac375Sdm199847 #define	_F_GETGRWNAMDOM_SSD	"(&(%%s)(|(%s=%s@%s)(%1$s=%3$s\\5c%2$s)))"
82479ac375Sdm199847 
83479ac375Sdm199847 /* Native LDAP lookup using GID */
84479ac375Sdm199847 #define	_F_GETGRGID		"(&(objectClass=posixGroup)(gidNumber=%u))"
85479ac375Sdm199847 #define	_F_GETGRGID_SSD		"(&(%%s)(gidNumber=%u))"
86479ac375Sdm199847 
87479ac375Sdm199847 /* Native LDAP attribute names */
88479ac375Sdm199847 #define	UID			"uid"
89479ac375Sdm199847 #define	CN			"cn"
90479ac375Sdm199847 #define	UIDNUMBER		"uidnumber"
91479ac375Sdm199847 #define	GIDNUMBER		"gidnumber"
92479ac375Sdm199847 #define	DN			"dn"
93479ac375Sdm199847 
94479ac375Sdm199847 #define	IS_NLDAP_RC_FATAL(x)	((x == NS_LDAP_MEMORY) ? 1 : 0)
95479ac375Sdm199847 
96479ac375Sdm199847 typedef struct idmap_nldap_q {
97479ac375Sdm199847 	char			**winname;
98479ac375Sdm199847 	char			**windomain;
99479ac375Sdm199847 	char			**unixname;
100479ac375Sdm199847 	uid_t			*pid;
101479ac375Sdm199847 	char			**dn;
102479ac375Sdm199847 	char			**attr;
103479ac375Sdm199847 	char			**value;
104479ac375Sdm199847 	int			is_user;
105479ac375Sdm199847 	idmap_retcode		*rc;
106479ac375Sdm199847 	int			lrc;
107479ac375Sdm199847 	ns_ldap_result_t	*result;
108479ac375Sdm199847 	ns_ldap_error_t		*errorp;
109479ac375Sdm199847 	char			*filter;
110479ac375Sdm199847 	char			*udata;
111479ac375Sdm199847 } idmap_nldap_q_t;
112479ac375Sdm199847 
113479ac375Sdm199847 typedef struct idmap_nldap_query_state {
114479ac375Sdm199847 	const char		*nldap_winname_attr;
115479ac375Sdm199847 	const char		*defdom;
116479ac375Sdm199847 	int			nqueries;
117479ac375Sdm199847 	int			qid;
118479ac375Sdm199847 	int			flag;
119479ac375Sdm199847 	ns_ldap_list_batch_t	*batch;
120479ac375Sdm199847 	idmap_nldap_q_t		queries[1];
121479ac375Sdm199847 } idmap_nldap_query_state_t;
122479ac375Sdm199847 
123479ac375Sdm199847 /*
124479ac375Sdm199847  * This routine has been copied from lib/nsswitch/ldap/common/ldap_utils.c
125479ac375Sdm199847  * after removing the debug statements.
126479ac375Sdm199847  *
127479ac375Sdm199847  * This is a generic filter callback function for merging the filter
128479ac375Sdm199847  * from service search descriptor with an existing search filter. This
129479ac375Sdm199847  * routine expects userdata to contain a format string with a single %s
130479ac375Sdm199847  * in it, and will use the format string with sprintf() to insert the
131479ac375Sdm199847  * SSD filter.
132479ac375Sdm199847  *
133479ac375Sdm199847  * This routine and userdata are passed to the __ns_ldap_list_batch_add()
134479ac375Sdm199847  * API.
135479ac375Sdm199847  *
136479ac375Sdm199847  * Consider an example that uses __ns_ldap_list_batch_add() to lookup
137479ac375Sdm199847  * native LDAP directory using a given userid 'xy12345'. In this
138479ac375Sdm199847  * example the userdata will contain the filter "(&(%s)(cn=xy1234))".
139479ac375Sdm199847  * If a SSD is defined to replace the rfc2307bis specified filter
140479ac375Sdm199847  * i.e. (objectClass=posixAccount) by a site-specific filter
141479ac375Sdm199847  * say (department=sds) then this routine when called will produce
142479ac375Sdm199847  * "(&(department=sds)(uid=xy1234))" as the real search filter.
143479ac375Sdm199847  */
144479ac375Sdm199847 static
145479ac375Sdm199847 int
146479ac375Sdm199847 merge_SSD_filter(const ns_ldap_search_desc_t *desc,
147479ac375Sdm199847 	char **realfilter, const void *userdata)
148479ac375Sdm199847 {
149479ac375Sdm199847 	int	len;
150479ac375Sdm199847 	if (realfilter == NULL)
151479ac375Sdm199847 		return (NS_LDAP_INVALID_PARAM);
152479ac375Sdm199847 	*realfilter = NULL;
153479ac375Sdm199847 	if (desc == NULL || desc->filter == NULL || userdata == NULL)
154479ac375Sdm199847 		return (NS_LDAP_INVALID_PARAM);
155479ac375Sdm199847 	len = strlen(userdata) + strlen(desc->filter) + 1;
156479ac375Sdm199847 	*realfilter = (char *)malloc(len);
157479ac375Sdm199847 	if (*realfilter == NULL)
158479ac375Sdm199847 		return (NS_LDAP_MEMORY);
159479ac375Sdm199847 	(void) sprintf(*realfilter, (char *)userdata, desc->filter);
160479ac375Sdm199847 	return (NS_LDAP_SUCCESS);
161479ac375Sdm199847 }
162479ac375Sdm199847 
163479ac375Sdm199847 static
164479ac375Sdm199847 char
165479ac375Sdm199847 hex_char(int n)
166479ac375Sdm199847 {
167479ac375Sdm199847 	return ("0123456789abcdef"[n & 0xf]);
168479ac375Sdm199847 }
169479ac375Sdm199847 
170479ac375Sdm199847 /*
171479ac375Sdm199847  * If the input string contains special characters that needs to be
172479ac375Sdm199847  * escaped before the string can be used in a LDAP filter then this
173479ac375Sdm199847  * function will return a new sanitized string. Otherwise this function
174479ac375Sdm199847  * returns the input string (This saves us un-necessary memory allocations
175479ac375Sdm199847  * especially when processing a batch of requests). The caller must free
176479ac375Sdm199847  * the returned string if it isn't the input string.
177479ac375Sdm199847  *
178479ac375Sdm199847  * The escape mechanism for LDAP filter is described in RFC2254 basically
179479ac375Sdm199847  * it's \hh where hh are the two hexadecimal digits representing the ASCII
180479ac375Sdm199847  * value of the encoded character (case of hh is not significant).
181479ac375Sdm199847  * Example: * -> \2a, ( -> \28, ) -> \29, \ -> \5c,
182479ac375Sdm199847  *
183479ac375Sdm199847  * outstring = sanitize_for_ldap_filter(instring);
184479ac375Sdm199847  * if (outstring == NULL)
185479ac375Sdm199847  *	Out of memory
186479ac375Sdm199847  * else
187479ac375Sdm199847  *	Use outstring
188479ac375Sdm199847  *	if (outstring != instring)
189479ac375Sdm199847  *		free(outstring);
190479ac375Sdm199847  * done
191479ac375Sdm199847  */
192479ac375Sdm199847 char *
193479ac375Sdm199847 sanitize_for_ldap_filter(const char *str)
194479ac375Sdm199847 {
195479ac375Sdm199847 	const char	*p;
196479ac375Sdm199847 	char		*q, *s_str = NULL;
197479ac375Sdm199847 	int		n;
198479ac375Sdm199847 
199479ac375Sdm199847 	/* Get a count of special characters */
200479ac375Sdm199847 	for (p = str, n = 0; *p; p++)
201479ac375Sdm199847 		if (*p == '*' || *p == '(' || *p == ')' ||
202479ac375Sdm199847 		    *p == '\\' || *p == '%')
203479ac375Sdm199847 			n++;
204479ac375Sdm199847 	/* If count is zero then no need to sanitize */
205479ac375Sdm199847 	if (n == 0)
206479ac375Sdm199847 		return ((char *)str);
207479ac375Sdm199847 	/* Create output buffer that will contain the sanitized value */
208479ac375Sdm199847 	s_str = calloc(1, n * 2 + strlen(str) + 1);
209479ac375Sdm199847 	if (s_str == NULL)
210479ac375Sdm199847 		return (NULL);
211479ac375Sdm199847 	for (p = str, q = s_str; *p; p++) {
212479ac375Sdm199847 		if (*p == '*' || *p == '(' || *p == ')' ||
213479ac375Sdm199847 		    *p == '\\' || *p == '%') {
214479ac375Sdm199847 			*q++ = '\\';
215479ac375Sdm199847 			*q++ = hex_char(*p >> 4);
216479ac375Sdm199847 			*q++ = hex_char(*p & 0xf);
217479ac375Sdm199847 		} else
218479ac375Sdm199847 			*q++ = *p;
219479ac375Sdm199847 	}
220479ac375Sdm199847 	return (s_str);
221479ac375Sdm199847 }
222479ac375Sdm199847 
223479ac375Sdm199847 /*
224479ac375Sdm199847  * Map libsldap status to idmap  status
225479ac375Sdm199847  */
226479ac375Sdm199847 static
227479ac375Sdm199847 idmap_retcode
228479ac375Sdm199847 nldaprc2retcode(int rc)
229479ac375Sdm199847 {
230479ac375Sdm199847 	switch (rc) {
231479ac375Sdm199847 	case NS_LDAP_SUCCESS:
232479ac375Sdm199847 	case NS_LDAP_SUCCESS_WITH_INFO:
233479ac375Sdm199847 		return (IDMAP_SUCCESS);
234479ac375Sdm199847 	case NS_LDAP_NOTFOUND:
235479ac375Sdm199847 		return (IDMAP_ERR_NOTFOUND);
236479ac375Sdm199847 	case NS_LDAP_MEMORY:
237479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
238479ac375Sdm199847 	case NS_LDAP_CONFIG:
239479ac375Sdm199847 		return (IDMAP_ERR_NS_LDAP_CFG);
240479ac375Sdm199847 	case NS_LDAP_OP_FAILED:
241479ac375Sdm199847 		return (IDMAP_ERR_NS_LDAP_OP_FAILED);
242479ac375Sdm199847 	case NS_LDAP_PARTIAL:
243479ac375Sdm199847 		return (IDMAP_ERR_NS_LDAP_PARTIAL);
244479ac375Sdm199847 	case NS_LDAP_INTERNAL:
245479ac375Sdm199847 		return (IDMAP_ERR_INTERNAL);
246479ac375Sdm199847 	case NS_LDAP_INVALID_PARAM:
247479ac375Sdm199847 		return (IDMAP_ERR_ARG);
248479ac375Sdm199847 	default:
249479ac375Sdm199847 		return (IDMAP_ERR_OTHER);
250479ac375Sdm199847 	}
251479ac375Sdm199847 	/*NOTREACHED*/
252479ac375Sdm199847 }
253479ac375Sdm199847 
254479ac375Sdm199847 /*
255479ac375Sdm199847  * Create a batch for native LDAP lookup.
256479ac375Sdm199847  */
257479ac375Sdm199847 static
258479ac375Sdm199847 idmap_retcode
259479ac375Sdm199847 idmap_nldap_lookup_batch_start(int nqueries, idmap_nldap_query_state_t **qs)
260479ac375Sdm199847 {
261479ac375Sdm199847 	idmap_nldap_query_state_t	*s;
262479ac375Sdm199847 
263479ac375Sdm199847 	s = calloc(1, sizeof (*s) +
264479ac375Sdm199847 	    (nqueries - 1) * sizeof (idmap_nldap_q_t));
265479ac375Sdm199847 	if (s == NULL)
266479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
267479ac375Sdm199847 	if (__ns_ldap_list_batch_start(&s->batch) != NS_LDAP_SUCCESS) {
268479ac375Sdm199847 		free(s);
269479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
270479ac375Sdm199847 	}
271479ac375Sdm199847 	s->nqueries = nqueries;
272479ac375Sdm199847 	s->flag = NS_LDAP_KEEP_CONN;
273479ac375Sdm199847 	*qs = s;
274479ac375Sdm199847 	return (IDMAP_SUCCESS);
275479ac375Sdm199847 }
276479ac375Sdm199847 
277479ac375Sdm199847 /*
278479ac375Sdm199847  * Add a lookup by winname request to the batch.
279479ac375Sdm199847  */
280479ac375Sdm199847 static
281479ac375Sdm199847 idmap_retcode
282479ac375Sdm199847 idmap_nldap_bywinname_batch_add(idmap_nldap_query_state_t *qs,
283479ac375Sdm199847 	const char *winname, const char *windomain, int is_user,
284479ac375Sdm199847 	char **dn, char **attr, char **value,
285479ac375Sdm199847 	char **unixname, uid_t *pid, idmap_retcode *rc)
286479ac375Sdm199847 {
287479ac375Sdm199847 	idmap_nldap_q_t		*q;
288479ac375Sdm199847 	const char		*db, *filter, *udata;
289479ac375Sdm199847 	int			flen, ulen, wksid = 0;
290479ac375Sdm199847 	char			*s_winname, *s_windomain;
291479ac375Sdm199847 	const char		**attrs;
292479ac375Sdm199847 	const char		*pwd_attrs[] = {UID, UIDNUMBER, NULL, NULL};
293479ac375Sdm199847 	const char		*grp_attrs[] = {CN, GIDNUMBER, NULL, NULL};
294479ac375Sdm199847 
295479ac375Sdm199847 	s_winname = s_windomain = NULL;
296479ac375Sdm199847 	q = &(qs->queries[qs->qid++]);
297479ac375Sdm199847 	q->unixname = unixname;
298479ac375Sdm199847 	q->pid = pid;
299479ac375Sdm199847 	q->rc = rc;
300479ac375Sdm199847 	q->is_user = is_user;
301479ac375Sdm199847 	q->dn = dn;
302479ac375Sdm199847 	q->attr = attr;
303479ac375Sdm199847 	q->value = value;
304479ac375Sdm199847 
305479ac375Sdm199847 	if (is_user) {
306479ac375Sdm199847 		db = "passwd";
30708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (lookup_wksids_name2sid(winname, NULL, NULL, NULL, NULL,
30808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, NULL) == IDMAP_SUCCESS) {
309479ac375Sdm199847 			filter = _F_GETPWWNAMWK;
310479ac375Sdm199847 			udata = _F_GETPWWNAMWK_SSD;
311479ac375Sdm199847 			wksid = 1;
312479ac375Sdm199847 		} else if (windomain != NULL) {
313479ac375Sdm199847 			filter = _F_GETPWWNAMDOM;
314479ac375Sdm199847 			udata = _F_GETPWWNAMDOM_SSD;
315479ac375Sdm199847 		} else {
316479ac375Sdm199847 			*q->rc = IDMAP_ERR_DOMAIN_NOTFOUND;
317479ac375Sdm199847 			goto errout;
318479ac375Sdm199847 		}
319479ac375Sdm199847 		pwd_attrs[2] = qs->nldap_winname_attr;
320479ac375Sdm199847 		attrs = pwd_attrs;
321479ac375Sdm199847 	} else {
322479ac375Sdm199847 		db = "group";
32308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (lookup_wksids_name2sid(winname, NULL, NULL, NULL, NULL,
32408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, NULL) == IDMAP_SUCCESS) {
325479ac375Sdm199847 			filter = _F_GETGRWNAMWK;
326479ac375Sdm199847 			udata = _F_GETGRWNAMWK_SSD;
327479ac375Sdm199847 			wksid = 1;
328479ac375Sdm199847 		} else if (windomain != NULL) {
329479ac375Sdm199847 			filter = _F_GETGRWNAMDOM;
330479ac375Sdm199847 			udata = _F_GETGRWNAMDOM_SSD;
331479ac375Sdm199847 		} else {
332479ac375Sdm199847 			*q->rc = IDMAP_ERR_DOMAIN_NOTFOUND;
333479ac375Sdm199847 			goto errout;
334479ac375Sdm199847 		}
335479ac375Sdm199847 		grp_attrs[2] = qs->nldap_winname_attr;
336479ac375Sdm199847 		attrs = grp_attrs;
337479ac375Sdm199847 	}
338479ac375Sdm199847 
339479ac375Sdm199847 	/*
340479ac375Sdm199847 	 * Sanitize names. No need to sanitize qs->nldap_winname_attr
341479ac375Sdm199847 	 * because if it contained any of the special characters then
342479ac375Sdm199847 	 * it would have been rejected by the function that reads it
343479ac375Sdm199847 	 * from the SMF config. LDAP attribute names can only contain
344479ac375Sdm199847 	 * letters, digits or hyphens.
345479ac375Sdm199847 	 */
346479ac375Sdm199847 	s_winname = sanitize_for_ldap_filter(winname);
347479ac375Sdm199847 	if (s_winname == NULL) {
348479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
349479ac375Sdm199847 		goto errout;
350479ac375Sdm199847 	}
351479ac375Sdm199847 	/* windomain could be NULL for names of well-known SIDs */
352479ac375Sdm199847 	if (windomain != NULL) {
353479ac375Sdm199847 		s_windomain = sanitize_for_ldap_filter(windomain);
354479ac375Sdm199847 		if (s_windomain == NULL) {
355479ac375Sdm199847 			*q->rc = IDMAP_ERR_MEMORY;
356479ac375Sdm199847 			goto errout;
357479ac375Sdm199847 		}
358479ac375Sdm199847 	}
359479ac375Sdm199847 
360479ac375Sdm199847 	/* Construct the filter and udata using snprintf. */
361479ac375Sdm199847 	if (wksid) {
362479ac375Sdm199847 		flen = snprintf(NULL, 0, filter, qs->nldap_winname_attr,
363479ac375Sdm199847 		    s_winname) + 1;
364479ac375Sdm199847 		ulen = snprintf(NULL, 0, udata, qs->nldap_winname_attr,
365479ac375Sdm199847 		    s_winname) + 1;
366479ac375Sdm199847 	} else {
367479ac375Sdm199847 		flen = snprintf(NULL, 0, filter, qs->nldap_winname_attr,
368479ac375Sdm199847 		    s_winname, s_windomain) + 1;
369479ac375Sdm199847 		ulen = snprintf(NULL, 0, udata, qs->nldap_winname_attr,
370479ac375Sdm199847 		    s_winname, s_windomain) + 1;
371479ac375Sdm199847 	}
372479ac375Sdm199847 
373479ac375Sdm199847 	q->filter = malloc(flen);
374479ac375Sdm199847 	if (q->filter == NULL) {
375479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
376479ac375Sdm199847 		goto errout;
377479ac375Sdm199847 	}
378479ac375Sdm199847 	q->udata = malloc(ulen);
379479ac375Sdm199847 	if (q->udata == NULL) {
380479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
381479ac375Sdm199847 		goto errout;
382479ac375Sdm199847 	}
383479ac375Sdm199847 
384479ac375Sdm199847 	if (wksid) {
385479ac375Sdm199847 		(void) snprintf(q->filter, flen, filter,
386479ac375Sdm199847 		    qs->nldap_winname_attr, s_winname);
387479ac375Sdm199847 		(void) snprintf(q->udata, ulen, udata,
388479ac375Sdm199847 		    qs->nldap_winname_attr, s_winname);
389479ac375Sdm199847 	} else {
390479ac375Sdm199847 		(void) snprintf(q->filter, flen, filter,
391479ac375Sdm199847 		    qs->nldap_winname_attr, s_winname, s_windomain);
392479ac375Sdm199847 		(void) snprintf(q->udata, ulen, udata,
393479ac375Sdm199847 		    qs->nldap_winname_attr, s_winname, s_windomain);
394479ac375Sdm199847 	}
395479ac375Sdm199847 
396479ac375Sdm199847 	if (s_winname != winname)
397479ac375Sdm199847 		free(s_winname);
398479ac375Sdm199847 	if (s_windomain != windomain)
399479ac375Sdm199847 		free(s_windomain);
400479ac375Sdm199847 
401479ac375Sdm199847 	q->lrc = __ns_ldap_list_batch_add(qs->batch, db, q->filter,
402479ac375Sdm199847 	    merge_SSD_filter, attrs, NULL, qs->flag, &q->result,
403479ac375Sdm199847 	    &q->errorp, &q->lrc, NULL, q->udata);
404479ac375Sdm199847 
405479ac375Sdm199847 	if (IS_NLDAP_RC_FATAL(q->lrc))
406479ac375Sdm199847 		return (nldaprc2retcode(q->lrc));
407479ac375Sdm199847 	return (IDMAP_SUCCESS);
408479ac375Sdm199847 
409479ac375Sdm199847 errout:
410479ac375Sdm199847 	/* query q and its content will be freed by batch_release */
411479ac375Sdm199847 	if (s_winname != winname)
412479ac375Sdm199847 		free(s_winname);
413479ac375Sdm199847 	if (s_windomain != windomain)
414479ac375Sdm199847 		free(s_windomain);
415479ac375Sdm199847 	return (*q->rc);
416479ac375Sdm199847 }
417479ac375Sdm199847 
418479ac375Sdm199847 /*
419479ac375Sdm199847  * Add a lookup by uid/gid request to the batch.
420479ac375Sdm199847  */
421479ac375Sdm199847 static
422479ac375Sdm199847 idmap_retcode
423479ac375Sdm199847 idmap_nldap_bypid_batch_add(idmap_nldap_query_state_t *qs,
424479ac375Sdm199847 	uid_t pid, int is_user, char **dn, char **attr, char **value,
425479ac375Sdm199847 	char **winname, char **windomain,
426479ac375Sdm199847 	char **unixname, idmap_retcode *rc)
427479ac375Sdm199847 {
428479ac375Sdm199847 	idmap_nldap_q_t		*q;
429479ac375Sdm199847 	const char		*db, *filter, *udata;
430479ac375Sdm199847 	int			len;
431479ac375Sdm199847 	const char		**attrs;
432479ac375Sdm199847 	const char		*pwd_attrs[] = {UID, NULL, NULL};
433479ac375Sdm199847 	const char		*grp_attrs[] = {CN, NULL, NULL};
434479ac375Sdm199847 
435479ac375Sdm199847 	q = &(qs->queries[qs->qid++]);
436479ac375Sdm199847 	q->winname = winname;
437479ac375Sdm199847 	q->windomain = windomain;
438479ac375Sdm199847 	q->unixname = unixname;
439479ac375Sdm199847 	q->rc = rc;
440479ac375Sdm199847 	q->is_user = is_user;
441479ac375Sdm199847 	q->dn = dn;
442479ac375Sdm199847 	q->attr = attr;
443479ac375Sdm199847 	q->value = value;
444479ac375Sdm199847 
445479ac375Sdm199847 	if (is_user) {
446479ac375Sdm199847 		db = "passwd";
447479ac375Sdm199847 		filter = _F_GETPWUID;
448479ac375Sdm199847 		udata = _F_GETPWUID_SSD;
449479ac375Sdm199847 		pwd_attrs[1] = qs->nldap_winname_attr;
450479ac375Sdm199847 		attrs = pwd_attrs;
451479ac375Sdm199847 	} else {
452479ac375Sdm199847 		db = "group";
453479ac375Sdm199847 		filter = _F_GETGRGID;
454479ac375Sdm199847 		udata = _F_GETGRGID_SSD;
455479ac375Sdm199847 		grp_attrs[1] = qs->nldap_winname_attr;
456479ac375Sdm199847 		attrs = grp_attrs;
457479ac375Sdm199847 	}
458479ac375Sdm199847 
459479ac375Sdm199847 	len = snprintf(NULL, 0, filter, pid) + 1;
460479ac375Sdm199847 	q->filter = malloc(len);
461479ac375Sdm199847 	if (q->filter == NULL) {
462479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
463479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
464479ac375Sdm199847 	}
465479ac375Sdm199847 	(void) snprintf(q->filter, len, filter, pid);
466479ac375Sdm199847 
467479ac375Sdm199847 	len = snprintf(NULL, 0, udata, pid) + 1;
468479ac375Sdm199847 	q->udata = malloc(len);
469479ac375Sdm199847 	if (q->udata == NULL) {
470479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
471479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
472479ac375Sdm199847 	}
473479ac375Sdm199847 	(void) snprintf(q->udata, len, udata, pid);
474479ac375Sdm199847 
475479ac375Sdm199847 	q->lrc = __ns_ldap_list_batch_add(qs->batch, db, q->filter,
476479ac375Sdm199847 	    merge_SSD_filter, attrs, NULL, qs->flag, &q->result,
477479ac375Sdm199847 	    &q->errorp, &q->lrc, NULL, q->udata);
478479ac375Sdm199847 
479479ac375Sdm199847 	if (IS_NLDAP_RC_FATAL(q->lrc))
480479ac375Sdm199847 		return (nldaprc2retcode(q->lrc));
481479ac375Sdm199847 	return (IDMAP_SUCCESS);
482479ac375Sdm199847 }
483479ac375Sdm199847 
484479ac375Sdm199847 /*
485479ac375Sdm199847  * Add a lookup by user/group name request to the batch.
486479ac375Sdm199847  */
487479ac375Sdm199847 static
488479ac375Sdm199847 idmap_retcode
489479ac375Sdm199847 idmap_nldap_byunixname_batch_add(idmap_nldap_query_state_t *qs,
490479ac375Sdm199847 	const char *unixname, int is_user,
491479ac375Sdm199847 	char **dn, char **attr, char **value,
492479ac375Sdm199847 	char **winname, char **windomain, uid_t *pid, idmap_retcode *rc)
493479ac375Sdm199847 {
494479ac375Sdm199847 	idmap_nldap_q_t		*q;
495479ac375Sdm199847 	const char		*db, *filter, *udata;
496479ac375Sdm199847 	int			len;
497479ac375Sdm199847 	char			*s_unixname = NULL;
498479ac375Sdm199847 	const char		**attrs;
499479ac375Sdm199847 	const char		*pwd_attrs[] = {UIDNUMBER, NULL, NULL};
500479ac375Sdm199847 	const char		*grp_attrs[] = {GIDNUMBER, NULL, NULL};
501479ac375Sdm199847 
502479ac375Sdm199847 	q = &(qs->queries[qs->qid++]);
503479ac375Sdm199847 	q->winname = winname;
504479ac375Sdm199847 	q->windomain = windomain;
505479ac375Sdm199847 	q->pid = pid;
506479ac375Sdm199847 	q->rc = rc;
507479ac375Sdm199847 	q->is_user = is_user;
508479ac375Sdm199847 	q->dn = dn;
509479ac375Sdm199847 	q->attr = attr;
510479ac375Sdm199847 	q->value = value;
511479ac375Sdm199847 
512479ac375Sdm199847 	if (is_user) {
513479ac375Sdm199847 		db = "passwd";
514479ac375Sdm199847 		filter = _F_GETPWNAM;
515479ac375Sdm199847 		udata = _F_GETPWNAM_SSD;
516479ac375Sdm199847 		pwd_attrs[1] = qs->nldap_winname_attr;
517479ac375Sdm199847 		attrs = pwd_attrs;
518479ac375Sdm199847 	} else {
519479ac375Sdm199847 		db = "group";
520479ac375Sdm199847 		filter = _F_GETGRNAM;
521479ac375Sdm199847 		udata = _F_GETGRNAM_SSD;
522479ac375Sdm199847 		grp_attrs[1] = qs->nldap_winname_attr;
523479ac375Sdm199847 		attrs = grp_attrs;
524479ac375Sdm199847 	}
525479ac375Sdm199847 
526479ac375Sdm199847 	s_unixname = sanitize_for_ldap_filter(unixname);
527479ac375Sdm199847 	if (s_unixname == NULL) {
528479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
529479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
530479ac375Sdm199847 	}
531479ac375Sdm199847 
532479ac375Sdm199847 	len = snprintf(NULL, 0, filter, s_unixname) + 1;
533479ac375Sdm199847 	q->filter = malloc(len);
534479ac375Sdm199847 	if (q->filter == NULL) {
535479ac375Sdm199847 		if (s_unixname != unixname)
536479ac375Sdm199847 			free(s_unixname);
537479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
538479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
539479ac375Sdm199847 	}
540479ac375Sdm199847 	(void) snprintf(q->filter, len, filter, s_unixname);
541479ac375Sdm199847 
542479ac375Sdm199847 	len = snprintf(NULL, 0, udata, s_unixname) + 1;
543479ac375Sdm199847 	q->udata = malloc(len);
544479ac375Sdm199847 	if (q->udata == NULL) {
545479ac375Sdm199847 		if (s_unixname != unixname)
546479ac375Sdm199847 			free(s_unixname);
547479ac375Sdm199847 		*q->rc = IDMAP_ERR_MEMORY;
548479ac375Sdm199847 		return (IDMAP_ERR_MEMORY);
549479ac375Sdm199847 	}
550479ac375Sdm199847 	(void) snprintf(q->udata, len, udata, s_unixname);
551479ac375Sdm199847 
552479ac375Sdm199847 	if (s_unixname != unixname)
553479ac375Sdm199847 		free(s_unixname);
554479ac375Sdm199847 
555479ac375Sdm199847 	q->lrc = __ns_ldap_list_batch_add(qs->batch, db, q->filter,
556479ac375Sdm199847 	    merge_SSD_filter, attrs, NULL, qs->flag, &q->result,
557479ac375Sdm199847 	    &q->errorp, &q->lrc, NULL, q->udata);
558479ac375Sdm199847 
559479ac375Sdm199847 	if (IS_NLDAP_RC_FATAL(q->lrc))
560479ac375Sdm199847 		return (nldaprc2retcode(q->lrc));
561479ac375Sdm199847 	return (IDMAP_SUCCESS);
562479ac375Sdm199847 }
563479ac375Sdm199847 
564479ac375Sdm199847 /*
565479ac375Sdm199847  * Free the batch
566479ac375Sdm199847  */
567479ac375Sdm199847 static
568479ac375Sdm199847 void
569479ac375Sdm199847 idmap_nldap_lookup_batch_release(idmap_nldap_query_state_t *qs)
570479ac375Sdm199847 {
571479ac375Sdm199847 	idmap_nldap_q_t		*q;
572479ac375Sdm199847 	int			i;
573479ac375Sdm199847 
574479ac375Sdm199847 	if (qs->batch != NULL)
575479ac375Sdm199847 		(void) __ns_ldap_list_batch_release(qs->batch);
576479ac375Sdm199847 	for (i = 0; i < qs->qid; i++) {
577479ac375Sdm199847 		q = &(qs->queries[i]);
578479ac375Sdm199847 		free(q->filter);
579479ac375Sdm199847 		free(q->udata);
580479ac375Sdm199847 		if (q->errorp != NULL)
581479ac375Sdm199847 			(void) __ns_ldap_freeError(&q->errorp);
582479ac375Sdm199847 		if (q->result != NULL)
583479ac375Sdm199847 			(void) __ns_ldap_freeResult(&q->result);
584479ac375Sdm199847 	}
585479ac375Sdm199847 	free(qs);
586479ac375Sdm199847 }
587479ac375Sdm199847 
588479ac375Sdm199847 /*
589479ac375Sdm199847  * Process all requests added to the batch and then free the batch.
590479ac375Sdm199847  * The results for individual requests will be accessible using the
591479ac375Sdm199847  * pointers passed during idmap_nldap_lookup_batch_end.
592479ac375Sdm199847  */
593479ac375Sdm199847 static
594479ac375Sdm199847 idmap_retcode
595479ac375Sdm199847 idmap_nldap_lookup_batch_end(idmap_nldap_query_state_t *qs)
596479ac375Sdm199847 {
597479ac375Sdm199847 	idmap_nldap_q_t		*q;
598479ac375Sdm199847 	int			i;
599479ac375Sdm199847 	ns_ldap_entry_t		*entry;
600479ac375Sdm199847 	char			**val, *end, *str, *name, *dom;
601479ac375Sdm199847 	idmap_retcode		rc = IDMAP_SUCCESS;
602479ac375Sdm199847 
603479ac375Sdm199847 	(void) __ns_ldap_list_batch_end(qs->batch);
604479ac375Sdm199847 	qs->batch = NULL;
605479ac375Sdm199847 	for (i = 0; i < qs->qid; i++) {
606479ac375Sdm199847 		q = &(qs->queries[i]);
607479ac375Sdm199847 		*q->rc = nldaprc2retcode(q->lrc);
608479ac375Sdm199847 		if (*q->rc != IDMAP_SUCCESS)
609479ac375Sdm199847 			continue;
610479ac375Sdm199847 		if (q->result == NULL ||
611479ac375Sdm199847 		    !q->result->entries_count ||
612479ac375Sdm199847 		    (entry = q->result->entry) == NULL ||
613479ac375Sdm199847 		    !entry->attr_count) {
614479ac375Sdm199847 			*q->rc = IDMAP_ERR_NOTFOUND;
615479ac375Sdm199847 			continue;
616479ac375Sdm199847 		}
617479ac375Sdm199847 		/* Get uid/gid */
618479ac375Sdm199847 		if (q->pid != NULL) {
619479ac375Sdm199847 			val = __ns_ldap_getAttr(entry,
620479ac375Sdm199847 			    (q->is_user) ? UIDNUMBER : GIDNUMBER);
621479ac375Sdm199847 			if (val != NULL && *val != NULL)
622479ac375Sdm199847 				*q->pid = strtoul(*val, &end, 10);
623479ac375Sdm199847 		}
624479ac375Sdm199847 		/* Get unixname */
625479ac375Sdm199847 		if (q->unixname != NULL) {
626479ac375Sdm199847 			val = __ns_ldap_getAttr(entry,
627479ac375Sdm199847 			    (q->is_user) ? UID : CN);
628479ac375Sdm199847 			if (val != NULL && *val != NULL) {
629479ac375Sdm199847 				*q->unixname = strdup(*val);
630479ac375Sdm199847 				if (*q->unixname == NULL) {
631479ac375Sdm199847 					rc = *q->rc = IDMAP_ERR_MEMORY;
632479ac375Sdm199847 					goto out;
633479ac375Sdm199847 				}
634479ac375Sdm199847 			}
635479ac375Sdm199847 		}
636479ac375Sdm199847 		/* Get DN for how info */
637479ac375Sdm199847 		if (q->dn != NULL) {
638479ac375Sdm199847 			val = __ns_ldap_getAttr(entry, DN);
639479ac375Sdm199847 			if (val != NULL && *val != NULL) {
640479ac375Sdm199847 				*q->dn = strdup(*val);
641479ac375Sdm199847 				if (*q->dn == NULL) {
642479ac375Sdm199847 					rc = *q->rc = IDMAP_ERR_MEMORY;
643479ac375Sdm199847 					goto out;
644479ac375Sdm199847 				}
645479ac375Sdm199847 			}
646479ac375Sdm199847 		}
647479ac375Sdm199847 		/* Get nldap name mapping attr name for how info */
648479ac375Sdm199847 		if (q->attr != NULL) {
649479ac375Sdm199847 			*q->attr = strdup(qs->nldap_winname_attr);
650479ac375Sdm199847 			if (*q->attr == NULL) {
651479ac375Sdm199847 				rc = *q->rc = IDMAP_ERR_MEMORY;
652479ac375Sdm199847 				goto out;
653479ac375Sdm199847 			}
654479ac375Sdm199847 		}
655479ac375Sdm199847 		/* Get nldap name mapping attr value for how info */
656479ac375Sdm199847 		val =  __ns_ldap_getAttr(entry, qs->nldap_winname_attr);
657479ac375Sdm199847 		if (val == NULL || *val == NULL)
658479ac375Sdm199847 			continue;
659479ac375Sdm199847 		if (q->value != NULL) {
660479ac375Sdm199847 			*q->value = strdup(*val);
661479ac375Sdm199847 			if (*q->value == NULL) {
662479ac375Sdm199847 				rc = *q->rc = IDMAP_ERR_MEMORY;
663479ac375Sdm199847 				goto out;
664479ac375Sdm199847 			}
665479ac375Sdm199847 		}
666479ac375Sdm199847 
667479ac375Sdm199847 		/* Get winname and windomain */
668479ac375Sdm199847 		if (q->winname == NULL && q->windomain == NULL)
669479ac375Sdm199847 			continue;
670479ac375Sdm199847 		/*
671479ac375Sdm199847 		 * We need to split the value into winname and
672479ac375Sdm199847 		 * windomain. The value could be either in NT4
673479ac375Sdm199847 		 * style (i.e. dom\name) or AD-style (i.e. name@dom).
674479ac375Sdm199847 		 * We choose the first '\\' if it's in NT4 style and
675479ac375Sdm199847 		 * the last '@' if it's in AD-style for the split.
676479ac375Sdm199847 		 */
677479ac375Sdm199847 		name = dom = NULL;
67808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (lookup_wksids_name2sid(*val, NULL, NULL, NULL, NULL, NULL,
67908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL) == IDMAP_SUCCESS) {
680479ac375Sdm199847 			name = *val;
681479ac375Sdm199847 			dom = NULL;
682479ac375Sdm199847 		} else if ((str = strchr(*val, '\\')) != NULL) {
683479ac375Sdm199847 			*str = '\0';
684479ac375Sdm199847 			name = str + 1;
685479ac375Sdm199847 			dom = *val;
686479ac375Sdm199847 		} else if ((str = strrchr(*val, '@')) != NULL) {
687479ac375Sdm199847 			*str = '\0';
688479ac375Sdm199847 			name = *val;
689479ac375Sdm199847 			dom = str + 1;
690479ac375Sdm199847 		} else {
691479ac375Sdm199847 			idmapdlog(LOG_INFO, "Domain-less "
692479ac375Sdm199847 			    "winname (%s) found in Native LDAP", *val);
693479ac375Sdm199847 			*q->rc = IDMAP_ERR_NS_LDAP_BAD_WINNAME;
694479ac375Sdm199847 			continue;
695479ac375Sdm199847 		}
696479ac375Sdm199847 		if (q->winname != NULL) {
697479ac375Sdm199847 			*q->winname = strdup(name);
698479ac375Sdm199847 			if (*q->winname == NULL) {
699479ac375Sdm199847 				rc = *q->rc = IDMAP_ERR_MEMORY;
700479ac375Sdm199847 				goto out;
701479ac375Sdm199847 			}
702479ac375Sdm199847 		}
703479ac375Sdm199847 		if (q->windomain != NULL && dom != NULL) {
704479ac375Sdm199847 			*q->windomain = strdup(dom);
705479ac375Sdm199847 			if (*q->windomain == NULL) {
706479ac375Sdm199847 				rc = *q->rc = IDMAP_ERR_MEMORY;
707479ac375Sdm199847 				goto out;
708479ac375Sdm199847 			}
709479ac375Sdm199847 		}
710479ac375Sdm199847 	}
711479ac375Sdm199847 
712479ac375Sdm199847 out:
713479ac375Sdm199847 	(void) idmap_nldap_lookup_batch_release(qs);
714479ac375Sdm199847 	return (rc);
715479ac375Sdm199847 }
716e8c27ec8Sbaban 
717e8c27ec8Sbaban /* ARGSUSED */
718e8c27ec8Sbaban idmap_retcode
719e8c27ec8Sbaban nldap_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch,
720e8c27ec8Sbaban 		idmap_ids_res *result)
721e8c27ec8Sbaban {
722479ac375Sdm199847 	idmap_retcode			retcode, rc1;
723*148c5f43SAlan Wright 	int				i, add;
724479ac375Sdm199847 	idmap_mapping			*req;
725479ac375Sdm199847 	idmap_id_res			*res;
726479ac375Sdm199847 	idmap_nldap_query_state_t	*qs = NULL;
727479ac375Sdm199847 	idmap_how			*how;
728479ac375Sdm199847 
729479ac375Sdm199847 	if (state->nldap_nqueries == 0)
730479ac375Sdm199847 		return (IDMAP_SUCCESS);
731479ac375Sdm199847 
732479ac375Sdm199847 	/* Create nldap lookup batch */
733479ac375Sdm199847 	retcode = idmap_nldap_lookup_batch_start(state->nldap_nqueries, &qs);
734479ac375Sdm199847 	if (retcode != IDMAP_SUCCESS) {
735479ac375Sdm199847 		idmapdlog(LOG_ERR,
736479ac375Sdm199847 		    "Failed to create batch for native LDAP lookup");
737479ac375Sdm199847 		goto out;
738479ac375Sdm199847 	}
739479ac375Sdm199847 
740479ac375Sdm199847 	qs->nldap_winname_attr = state->nldap_winname_attr;
741479ac375Sdm199847 	qs->defdom = state->defdom;
742479ac375Sdm199847 
743479ac375Sdm199847 	/* Add requests to the batch */
744479ac375Sdm199847 	for (i = 0, add = 0; i < batch->idmap_mapping_batch_len; i++) {
745479ac375Sdm199847 		req = &batch->idmap_mapping_batch_val[i];
746479ac375Sdm199847 		res = &result->ids.ids_val[i];
747479ac375Sdm199847 		retcode = IDMAP_SUCCESS;
748479ac375Sdm199847 
749479ac375Sdm199847 		/* Skip if not marked for nldap lookup */
750479ac375Sdm199847 		if (!(req->direction & _IDMAP_F_LOOKUP_NLDAP))
751479ac375Sdm199847 			continue;
752479ac375Sdm199847 
753*148c5f43SAlan Wright 		if (IS_ID_SID(req->id1)) {
754479ac375Sdm199847 
755479ac375Sdm199847 			/* win2unix request: */
756479ac375Sdm199847 
757e8c27ec8Sbaban 			/*
758479ac375Sdm199847 			 * When processing a win2unix request, nldap lookup
759479ac375Sdm199847 			 * is performed after AD lookup or a successful
760479ac375Sdm199847 			 * name-cache lookup. Therefore we should already
761479ac375Sdm199847 			 * have sid, winname and sidtype. Note that
762479ac375Sdm199847 			 * windomain could be NULL e.g. well-known SIDs.
763e8c27ec8Sbaban 			 */
764479ac375Sdm199847 			assert(req->id1name != NULL &&
765479ac375Sdm199847 			    (res->id.idtype == IDMAP_UID ||
766479ac375Sdm199847 			    res->id.idtype == IDMAP_GID));
767479ac375Sdm199847 
768479ac375Sdm199847 			/* Skip if we already have pid and unixname */
769479ac375Sdm199847 			if (req->id2name != NULL &&
7709fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			    res->id.idmap_id_u.uid != IDMAP_SENTINEL_PID) {
771479ac375Sdm199847 				res->retcode = IDMAP_SUCCESS;
772479ac375Sdm199847 				continue;
773479ac375Sdm199847 			}
774479ac375Sdm199847 
775479ac375Sdm199847 			/* Clear leftover value */
776479ac375Sdm199847 			free(req->id2name);
777479ac375Sdm199847 			req->id2name = NULL;
778479ac375Sdm199847 
779479ac375Sdm199847 			/* Lookup nldap by winname to get pid and unixname */
780479ac375Sdm199847 			add = 1;
781*148c5f43SAlan Wright 			idmap_how_clear(&res->info.how);
782479ac375Sdm199847 			res->info.src = IDMAP_MAP_SRC_NEW;
783479ac375Sdm199847 			how = &res->info.how;
784479ac375Sdm199847 			how->map_type = IDMAP_MAP_TYPE_DS_NLDAP;
785479ac375Sdm199847 			retcode = idmap_nldap_bywinname_batch_add(
786479ac375Sdm199847 			    qs, req->id1name, req->id1domain,
787479ac375Sdm199847 			    (res->id.idtype == IDMAP_UID) ? 1 : 0,
788479ac375Sdm199847 			    &how->idmap_how_u.nldap.dn,
789479ac375Sdm199847 			    &how->idmap_how_u.nldap.attr,
790479ac375Sdm199847 			    &how->idmap_how_u.nldap.value,
791479ac375Sdm199847 			    &req->id2name, &res->id.idmap_id_u.uid,
792479ac375Sdm199847 			    &res->retcode);
793479ac375Sdm199847 
794*148c5f43SAlan Wright 		} else if (IS_ID_UID(req->id1) || IS_ID_GID(req->id1)) {
795479ac375Sdm199847 
796479ac375Sdm199847 			/* unix2win request: */
797479ac375Sdm199847 
798479ac375Sdm199847 			/* Skip if we already have winname */
799479ac375Sdm199847 			if (req->id2name != NULL) {
800479ac375Sdm199847 				res->retcode = IDMAP_SUCCESS;
801479ac375Sdm199847 				continue;
802479ac375Sdm199847 			}
803479ac375Sdm199847 
804479ac375Sdm199847 			/* Clear old value */
805479ac375Sdm199847 			free(req->id2domain);
806479ac375Sdm199847 			req->id2domain = NULL;
807479ac375Sdm199847 
808479ac375Sdm199847 			/* Set how info */
809*148c5f43SAlan Wright 			idmap_how_clear(&res->info.how);
810479ac375Sdm199847 			res->info.src = IDMAP_MAP_SRC_NEW;
811479ac375Sdm199847 			how = &res->info.how;
812479ac375Sdm199847 			how->map_type = IDMAP_MAP_TYPE_DS_NLDAP;
813479ac375Sdm199847 
814479ac375Sdm199847 			/* Lookup nldap by pid or unixname to get winname */
8159fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			if (req->id1.idmap_id_u.uid != IDMAP_SENTINEL_PID) {
816479ac375Sdm199847 				add = 1;
817479ac375Sdm199847 				retcode = idmap_nldap_bypid_batch_add(
818479ac375Sdm199847 				    qs, req->id1.idmap_id_u.uid,
819479ac375Sdm199847 				    (req->id1.idtype == IDMAP_UID) ? 1 : 0,
820479ac375Sdm199847 				    &how->idmap_how_u.nldap.dn,
821479ac375Sdm199847 				    &how->idmap_how_u.nldap.attr,
822479ac375Sdm199847 				    &how->idmap_how_u.nldap.value,
823479ac375Sdm199847 				    &req->id2name, &req->id2domain,
824479ac375Sdm199847 				    (req->id1name == NULL) ?
825479ac375Sdm199847 				    &req->id1name : NULL,
826479ac375Sdm199847 				    &res->retcode);
827479ac375Sdm199847 			} else if (req->id1name != NULL) {
828479ac375Sdm199847 				add = 1;
829479ac375Sdm199847 				retcode = idmap_nldap_byunixname_batch_add(
830479ac375Sdm199847 				    qs, req->id1name,
831479ac375Sdm199847 				    (req->id1.idtype == IDMAP_UID) ? 1 : 0,
832479ac375Sdm199847 				    &how->idmap_how_u.nldap.dn,
833479ac375Sdm199847 				    &how->idmap_how_u.nldap.attr,
834479ac375Sdm199847 				    &how->idmap_how_u.nldap.value,
835479ac375Sdm199847 				    &req->id2name, &req->id2domain,
836479ac375Sdm199847 				    &req->id1.idmap_id_u.uid, &res->retcode);
837479ac375Sdm199847 			}
838479ac375Sdm199847 
839479ac375Sdm199847 		}
840479ac375Sdm199847 
841479ac375Sdm199847 		/*
842479ac375Sdm199847 		 * nldap_batch_add API returns error only on fatal failures
843479ac375Sdm199847 		 * otherwise it returns success and the actual status
844479ac375Sdm199847 		 * is stored in the individual request (res->retcode).
845479ac375Sdm199847 		 * Stop adding requests to this batch on fatal failures
846479ac375Sdm199847 		 * (i.e. if retcode != success)
847479ac375Sdm199847 		 */
848479ac375Sdm199847 		if (retcode != IDMAP_SUCCESS)
849479ac375Sdm199847 			break;
850479ac375Sdm199847 	}
851479ac375Sdm199847 
852479ac375Sdm199847 	if (!add)
853479ac375Sdm199847 		idmap_nldap_lookup_batch_release(qs);
854479ac375Sdm199847 	else if (retcode != IDMAP_SUCCESS)
855479ac375Sdm199847 		idmap_nldap_lookup_batch_release(qs);
856479ac375Sdm199847 	else
857479ac375Sdm199847 		retcode = idmap_nldap_lookup_batch_end(qs);
858479ac375Sdm199847 
859479ac375Sdm199847 out:
860479ac375Sdm199847 	for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
861479ac375Sdm199847 		req = &batch->idmap_mapping_batch_val[i];
862479ac375Sdm199847 		res = &result->ids.ids_val[i];
863479ac375Sdm199847 		if (!(req->direction & _IDMAP_F_LOOKUP_NLDAP))
864479ac375Sdm199847 			continue;
865479ac375Sdm199847 
866479ac375Sdm199847 		/* Reset nldap flag */
867479ac375Sdm199847 		req->direction &= ~(_IDMAP_F_LOOKUP_NLDAP);
868479ac375Sdm199847 
869479ac375Sdm199847 		/*
870479ac375Sdm199847 		 * As noted earlier retcode != success if there were fatal
871479ac375Sdm199847 		 * errors during batch_start and batch_adds. If so then set
872479ac375Sdm199847 		 * the status of each nldap request to that error.
873479ac375Sdm199847 		 */
874479ac375Sdm199847 		if (retcode != IDMAP_SUCCESS) {
875479ac375Sdm199847 			res->retcode = retcode;
876479ac375Sdm199847 			continue;
877479ac375Sdm199847 		}
878479ac375Sdm199847 		if (!add)
879479ac375Sdm199847 			continue;
880479ac375Sdm199847 
881479ac375Sdm199847 		/*
882479ac375Sdm199847 		 * If we successfully retrieved winname from nldap entry
883479ac375Sdm199847 		 * then lookup winname2sid locally. If not found locally
884479ac375Sdm199847 		 * then mark this request for AD lookup.
885479ac375Sdm199847 		 */
886479ac375Sdm199847 		if (res->retcode == IDMAP_SUCCESS &&
887479ac375Sdm199847 		    req->id2name != NULL &&
888479ac375Sdm199847 		    res->id.idmap_id_u.sid.prefix == NULL &&
889*148c5f43SAlan Wright 		    (IS_ID_UID(req->id1) || IS_ID_GID(req->id1))) {
890479ac375Sdm199847 
891479ac375Sdm199847 			rc1 = lookup_name2sid(state->cache,
892*148c5f43SAlan Wright 			    req->id2name, req->id2domain, -1,
89308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			    NULL, NULL,
894479ac375Sdm199847 			    &res->id.idmap_id_u.sid.prefix,
895*148c5f43SAlan Wright 			    &res->id.idmap_id_u.sid.rid,
896*148c5f43SAlan Wright 			    &res->id.idtype,
897*148c5f43SAlan Wright 			    req, 1);
898*148c5f43SAlan Wright 			if (rc1 == IDMAP_ERR_NOTFOUND) {
899479ac375Sdm199847 				req->direction |= _IDMAP_F_LOOKUP_AD;
900479ac375Sdm199847 				state->ad_nqueries++;
901479ac375Sdm199847 			} else
902479ac375Sdm199847 				res->retcode = rc1;
903479ac375Sdm199847 		}
904479ac375Sdm199847 
905479ac375Sdm199847 		/*
906479ac375Sdm199847 		 * Unset non-fatal errors in individual request. This allows
907479ac375Sdm199847 		 * the next pass to process other mapping mechanisms for
908479ac375Sdm199847 		 * this request.
909479ac375Sdm199847 		 */
910479ac375Sdm199847 		if (res->retcode != IDMAP_SUCCESS &&
911479ac375Sdm199847 		    res->retcode != IDMAP_ERR_NS_LDAP_BAD_WINNAME &&
912479ac375Sdm199847 		    !(IDMAP_FATAL_ERROR(res->retcode))) {
913*148c5f43SAlan Wright 			idmap_how_clear(&res->info.how);
914479ac375Sdm199847 			res->retcode = IDMAP_SUCCESS;
915479ac375Sdm199847 		}
916479ac375Sdm199847 	}
917479ac375Sdm199847 
918479ac375Sdm199847 	state->nldap_nqueries = 0;
919479ac375Sdm199847 	return (retcode);
920e8c27ec8Sbaban }
921