xref: /titanic_52/usr/src/cmd/idmap/idmapd/adutils.c (revision cd37da7426f0c49c14ad9a8a07638ca971477566)
1c5c4113dSnw141292 /*
2c5c4113dSnw141292  * CDDL HEADER START
3c5c4113dSnw141292  *
4c5c4113dSnw141292  * The contents of this file are subject to the terms of the
5c5c4113dSnw141292  * Common Development and Distribution License (the "License").
6c5c4113dSnw141292  * You may not use this file except in compliance with the License.
7c5c4113dSnw141292  *
8c5c4113dSnw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c5c4113dSnw141292  * or http://www.opensolaris.org/os/licensing.
10c5c4113dSnw141292  * See the License for the specific language governing permissions
11c5c4113dSnw141292  * and limitations under the License.
12c5c4113dSnw141292  *
13c5c4113dSnw141292  * When distributing Covered Code, include this CDDL HEADER in each
14c5c4113dSnw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c5c4113dSnw141292  * If applicable, add the following below this CDDL HEADER, with the
16c5c4113dSnw141292  * fields enclosed by brackets "[]" replaced with your own identifying
17c5c4113dSnw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
18c5c4113dSnw141292  *
19c5c4113dSnw141292  * CDDL HEADER END
20c5c4113dSnw141292  */
21c5c4113dSnw141292 
22c5c4113dSnw141292 /*
23c5c4113dSnw141292  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24c5c4113dSnw141292  * Use is subject to license terms.
25c5c4113dSnw141292  */
26c5c4113dSnw141292 
27c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28c5c4113dSnw141292 
29c5c4113dSnw141292 /*
30c5c4113dSnw141292  * Processes name2sid & sid2name batched lookups for a given user or
31c5c4113dSnw141292  * computer from an AD Directory server using GSSAPI authentication
32c5c4113dSnw141292  */
33c5c4113dSnw141292 
34c5c4113dSnw141292 #include <stdio.h>
35c5c4113dSnw141292 #include <stdlib.h>
36c5c4113dSnw141292 #include <alloca.h>
37c5c4113dSnw141292 #include <string.h>
38c5c4113dSnw141292 #include <strings.h>
39c5c4113dSnw141292 #include <lber.h>
40c5c4113dSnw141292 #include <ldap.h>
41c5c4113dSnw141292 #include <sasl/sasl.h>
42c5c4113dSnw141292 #include <string.h>
43c5c4113dSnw141292 #include <ctype.h>
44c5c4113dSnw141292 #include <pthread.h>
45c5c4113dSnw141292 #include <synch.h>
46c5c4113dSnw141292 #include <atomic.h>
47c5c4113dSnw141292 #include <errno.h>
48c5c4113dSnw141292 #include <assert.h>
49c5c4113dSnw141292 #include <limits.h>
50*cd37da74Snw141292 #include <sys/u8_textprep.h>
51c5c4113dSnw141292 #include "idmapd.h"
52c5c4113dSnw141292 
53c5c4113dSnw141292 /*
54c5c4113dSnw141292  * Internal data structures for this code
55c5c4113dSnw141292  */
56c5c4113dSnw141292 
57c5c4113dSnw141292 /* Attribute names and filter format strings */
58e3c2d6aaSnw141292 #define	SAN		"sAMAccountName"
59e3c2d6aaSnw141292 #define	OBJSID		"objectSid"
60e3c2d6aaSnw141292 #define	OBJCLASS	"objectClass"
61c5c4113dSnw141292 #define	SANFILTER	"(sAMAccountName=%.*s)"
62e3c2d6aaSnw141292 #define	OBJSIDFILTER	"(objectSid=%s)"
63c5c4113dSnw141292 
64c5c4113dSnw141292 /*
65c5c4113dSnw141292  * This should really be in some <sys/sid.h> file or so; we have a
66c5c4113dSnw141292  * private version of sid_t, and so must other components of ON until we
67c5c4113dSnw141292  * rationalize this.
68c5c4113dSnw141292  */
69c5c4113dSnw141292 typedef struct sid {
70c5c4113dSnw141292 	uchar_t		version;
71c5c4113dSnw141292 	uchar_t		sub_authority_count;
72c5c4113dSnw141292 	uint64_t	authority;  /* really, 48-bits */
73c5c4113dSnw141292 	rid_t		sub_authorities[SID_MAX_SUB_AUTHORITIES];
74c5c4113dSnw141292 } sid_t;
75c5c4113dSnw141292 
76c5c4113dSnw141292 /* A single DS */
77c5c4113dSnw141292 typedef struct ad_host {
78c5c4113dSnw141292 	struct ad_host		*next;
79c5c4113dSnw141292 	ad_t			*owner;		/* ad_t to which this belongs */
80c5c4113dSnw141292 	pthread_mutex_t		lock;
81c5c4113dSnw141292 	LDAP			*ld;		/* LDAP connection */
82c5c4113dSnw141292 	uint32_t		ref;		/* ref count */
83c5c4113dSnw141292 	time_t			idletime;	/* time since last activity */
84c5c4113dSnw141292 	int			dead;		/* error on LDAP connection */
85c5c4113dSnw141292 	/*
86c5c4113dSnw141292 	 * Used to distinguish between different instances of LDAP
87c5c4113dSnw141292 	 * connections to this same DS.  We need this so we never mix up
88c5c4113dSnw141292 	 * results for a given msgID from one connection with those of
89c5c4113dSnw141292 	 * another earlier connection where two batch state structures
90c5c4113dSnw141292 	 * share this ad_host object but used different LDAP connections
91c5c4113dSnw141292 	 * to send their LDAP searches.
92c5c4113dSnw141292 	 */
93c5c4113dSnw141292 	uint64_t		generation;
94c5c4113dSnw141292 
95c5c4113dSnw141292 	/* LDAP DS info */
96c5c4113dSnw141292 	char			*host;
97c5c4113dSnw141292 	int			port;
98c5c4113dSnw141292 
99c5c4113dSnw141292 	/* hardwired to SASL GSSAPI only for now */
100c5c4113dSnw141292 	char			*saslmech;
101c5c4113dSnw141292 	unsigned		saslflags;
102c5c4113dSnw141292 } ad_host_t;
103c5c4113dSnw141292 
104c5c4113dSnw141292 /* A set of DSs for a given AD partition; ad_t typedef comes from  adutil.h */
105c5c4113dSnw141292 struct ad {
106c5c4113dSnw141292 	char			*dflt_w2k_dom;	/* used to qualify bare names */
107c5c4113dSnw141292 	pthread_mutex_t		lock;
108c5c4113dSnw141292 	uint32_t		ref;
109c8e26105Sjp151216 	ad_host_t		*last_adh;
110c5c4113dSnw141292 	idmap_ad_partition_t	partition;	/* Data or global catalog? */
111c5c4113dSnw141292 };
112c5c4113dSnw141292 
113c5c4113dSnw141292 /*
114c5c4113dSnw141292  * A place to put the results of a batched (async) query
115c5c4113dSnw141292  *
116c5c4113dSnw141292  * There is one of these for every query added to a batch object
117c5c4113dSnw141292  * (idmap_query_state, see below).
118c5c4113dSnw141292  */
119c5c4113dSnw141292 typedef struct idmap_q {
120e3c2d6aaSnw141292 	/*
121e3c2d6aaSnw141292 	 * data used for validating search result entries for name->SID
122e3c2d6aaSnw141292 	 * loopups
123e3c2d6aaSnw141292 	 */
124*cd37da74Snw141292 	char			*ecanonname;	/* expected canon name */
125*cd37da74Snw141292 	char			*edomain;	/* expected domain name */
126e3c2d6aaSnw141292 	/* results */
127*cd37da74Snw141292 	char			**canonname;	/* actual canon name */
128c5c4113dSnw141292 	char			**result;	/* name or stringified SID */
129c5c4113dSnw141292 	char			**domain;	/* name of domain of object */
130c5c4113dSnw141292 	rid_t			*rid;		/* for n2s, if not NULL */
131c5c4113dSnw141292 	int			*sid_type;	/* if not NULL */
132c5c4113dSnw141292 	idmap_retcode		*rc;
133e3c2d6aaSnw141292 
134e3c2d6aaSnw141292 	/* lookup state */
135c5c4113dSnw141292 	int			msgid;		/* LDAP message ID */
136c5c4113dSnw141292 } idmap_q_t;
137c5c4113dSnw141292 
138c5c4113dSnw141292 /* Batch context structure; typedef is in header file */
139c5c4113dSnw141292 struct idmap_query_state {
140c5c4113dSnw141292 	idmap_query_state_t	*next;
141c5c4113dSnw141292 	int			qcount;		/* how many queries */
14284decf41Sjp151216 	int			ref_cnt;	/* reference count */
14384decf41Sjp151216 	pthread_cond_t		cv;		/* Condition wait variable */
144c5c4113dSnw141292 	uint32_t		qlastsent;
145c5c4113dSnw141292 	uint32_t		qinflight;	/* how many queries in flight */
146c5c4113dSnw141292 	uint16_t		qdead;		/* oops, lost LDAP connection */
147c5c4113dSnw141292 	ad_host_t		*qadh;		/* LDAP connection */
148c5c4113dSnw141292 	uint64_t		qadh_gen;	/* same as qadh->generation */
149c5c4113dSnw141292 	idmap_q_t		queries[1];	/* array of query results */
150c5c4113dSnw141292 };
151c5c4113dSnw141292 
152c5c4113dSnw141292 /*
153c5c4113dSnw141292  * List of query state structs -- needed so we can "route" LDAP results
154c5c4113dSnw141292  * to the right context if multiple threads should be using the same
155c5c4113dSnw141292  * connection concurrently
156c5c4113dSnw141292  */
157c5c4113dSnw141292 static idmap_query_state_t	*qstatehead = NULL;
158c5c4113dSnw141292 static pthread_mutex_t		qstatelock = PTHREAD_MUTEX_INITIALIZER;
159c5c4113dSnw141292 
160c5c4113dSnw141292 /*
161c5c4113dSnw141292  * List of DSs, needed by the idle connection reaper thread
162c5c4113dSnw141292  */
163c5c4113dSnw141292 static ad_host_t	*host_head = NULL;
164651c0131Sbaban static pthread_t	reaperid = 0;
165c5c4113dSnw141292 static pthread_mutex_t	adhostlock = PTHREAD_MUTEX_INITIALIZER;
166c5c4113dSnw141292 
16784decf41Sjp151216 
16884decf41Sjp151216 static void
16984decf41Sjp151216 idmap_lookup_unlock_batch(idmap_query_state_t **state);
17084decf41Sjp151216 
171c8e26105Sjp151216 static void
172c8e26105Sjp151216 delete_ds(ad_t *ad, const char *host, int port);
17384decf41Sjp151216 
174c5c4113dSnw141292 /*ARGSUSED*/
175c5c4113dSnw141292 static int
176c5c4113dSnw141292 idmap_saslcallback(LDAP *ld, unsigned flags, void *defaults, void *prompts) {
177c5c4113dSnw141292 	sasl_interact_t	*interact;
178c5c4113dSnw141292 
179c5c4113dSnw141292 	if (prompts == NULL || flags != LDAP_SASL_INTERACTIVE)
180c5c4113dSnw141292 		return (LDAP_PARAM_ERROR);
181c5c4113dSnw141292 
182c5c4113dSnw141292 	/* There should be no extra arguemnts for SASL/GSSAPI authentication */
183c5c4113dSnw141292 	for (interact = prompts; interact->id != SASL_CB_LIST_END;
184c5c4113dSnw141292 			interact++) {
185c5c4113dSnw141292 		interact->result = NULL;
186c5c4113dSnw141292 		interact->len = 0;
187c5c4113dSnw141292 	}
188c5c4113dSnw141292 	return (LDAP_SUCCESS);
189c5c4113dSnw141292 }
190c5c4113dSnw141292 
191c5c4113dSnw141292 
192c5c4113dSnw141292 /*
193c5c4113dSnw141292  * Turn "dc=foo,dc=bar,dc=com" into "foo.bar.com"; ignores any other
194e3c2d6aaSnw141292  * attributes (CN, etc...).  We don't need the reverse, for now.
195c5c4113dSnw141292  */
196c5c4113dSnw141292 static
197c5c4113dSnw141292 char *
198c5c4113dSnw141292 dn2dns(const char *dn)
199c5c4113dSnw141292 {
200c5c4113dSnw141292 	char **rdns = NULL;
201c5c4113dSnw141292 	char **attrs = NULL;
202c5c4113dSnw141292 	char **labels = NULL;
203c5c4113dSnw141292 	char *dns = NULL;
204c5c4113dSnw141292 	char **rdn, **attr, **label;
205c5c4113dSnw141292 	int maxlabels = 5;
206c5c4113dSnw141292 	int nlabels = 0;
207c5c4113dSnw141292 	int dnslen;
208c5c4113dSnw141292 
209c5c4113dSnw141292 	/*
210c5c4113dSnw141292 	 * There is no reverse of ldap_dns_to_dn() in our libldap, so we
211c5c4113dSnw141292 	 * have to do the hard work here for now.
212c5c4113dSnw141292 	 */
213c5c4113dSnw141292 
214c5c4113dSnw141292 	/*
215c5c4113dSnw141292 	 * This code is much too liberal: it looks for "dc" attributes
216c5c4113dSnw141292 	 * in all RDNs of the DN.  In theory this could cause problems
217c5c4113dSnw141292 	 * if people were to use "dc" in nodes other than the root of
218c5c4113dSnw141292 	 * the tree, but in practice noone, least of all Active
219c5c4113dSnw141292 	 * Directory, does that.
220c5c4113dSnw141292 	 *
221c5c4113dSnw141292 	 * On the other hand, this code is much too conservative: it
222c5c4113dSnw141292 	 * does not make assumptions about ldap_explode_dn(), and _that_
223c5c4113dSnw141292 	 * is the true for looking at every attr of every RDN.
224c5c4113dSnw141292 	 *
225c5c4113dSnw141292 	 * Since we only ever look at dc and those must be DNS labels,
226c5c4113dSnw141292 	 * at least until we get around to supporting IDN here we
227c5c4113dSnw141292 	 * shouldn't see escaped labels from AD nor from libldap, though
228c5c4113dSnw141292 	 * the spec (RFC2253) does allow libldap to escape things that
229c5c4113dSnw141292 	 * don't need escaping -- if that should ever happen then
230c5c4113dSnw141292 	 * libldap will need a spanking, and we can take care of that.
231c5c4113dSnw141292 	 */
232c5c4113dSnw141292 
233c5c4113dSnw141292 	/* Explode a DN into RDNs */
234c5c4113dSnw141292 	if ((rdns = ldap_explode_dn(dn, 0)) == NULL)
235c5c4113dSnw141292 		return (NULL);
236c5c4113dSnw141292 
237c5c4113dSnw141292 	labels = calloc(maxlabels + 1, sizeof (char *));
238c5c4113dSnw141292 	label = labels;
239c5c4113dSnw141292 
240c5c4113dSnw141292 	for (rdn = rdns; *rdn != NULL; rdn++) {
241c5c4113dSnw141292 		if (attrs != NULL)
242c5c4113dSnw141292 			ldap_value_free(attrs);
243c5c4113dSnw141292 
244c5c4113dSnw141292 		/* Explode each RDN, look for DC attr, save val as DNS label */
245c5c4113dSnw141292 		if ((attrs = ldap_explode_rdn(rdn[0], 0)) == NULL)
246c5c4113dSnw141292 			goto done;
247c5c4113dSnw141292 
248c5c4113dSnw141292 		for (attr = attrs; *attr != NULL; attr++) {
249c5c4113dSnw141292 			if (strncasecmp(*attr, "dc=", 3) != 0)
250c5c4113dSnw141292 				continue;
251c5c4113dSnw141292 
252c5c4113dSnw141292 			/* Found a DNS label */
253c5c4113dSnw141292 			labels[nlabels++] = strdup((*attr) + 3);
254c5c4113dSnw141292 
255c5c4113dSnw141292 			if (nlabels == maxlabels) {
256c5c4113dSnw141292 				char **tmp;
257c5c4113dSnw141292 				tmp = realloc(labels,
258c5c4113dSnw141292 				    sizeof (char *) * (maxlabels + 1));
259c5c4113dSnw141292 
260c5c4113dSnw141292 				if (tmp == NULL)
261c5c4113dSnw141292 					goto done;
262c5c4113dSnw141292 
263c5c4113dSnw141292 				labels = tmp;
264c5c4113dSnw141292 				labels[nlabels] = NULL;
265c5c4113dSnw141292 			}
266c5c4113dSnw141292 
267c5c4113dSnw141292 			/* There should be just one DC= attr per-RDN */
268c5c4113dSnw141292 			break;
269c5c4113dSnw141292 		}
270c5c4113dSnw141292 	}
271c5c4113dSnw141292 
272c5c4113dSnw141292 	/*
273c5c4113dSnw141292 	 * Got all the labels, now join with '.'
274c5c4113dSnw141292 	 *
275c5c4113dSnw141292 	 * We need room for nlabels - 1 periods ('.'), one nul
276c5c4113dSnw141292 	 * terminator, and the strlen() of each label.
277c5c4113dSnw141292 	 */
278c5c4113dSnw141292 	dnslen = nlabels;
279c5c4113dSnw141292 	for (label = labels; *label != NULL; label++)
280c5c4113dSnw141292 		dnslen += strlen(*label);
281c5c4113dSnw141292 
282c5c4113dSnw141292 	if ((dns = malloc(dnslen)) == NULL)
283c5c4113dSnw141292 		goto done;
284c5c4113dSnw141292 
285c5c4113dSnw141292 	*dns = '\0';
286c5c4113dSnw141292 
287c5c4113dSnw141292 	for (label = labels; *label != NULL; label++) {
288c5c4113dSnw141292 		(void) strlcat(dns, *label, dnslen);
289c5c4113dSnw141292 		/*
290c5c4113dSnw141292 		 * NOTE: the last '.' won't be appended -- there's no room
291c5c4113dSnw141292 		 * for it!
292c5c4113dSnw141292 		 */
293c5c4113dSnw141292 		(void) strlcat(dns, ".", dnslen);
294c5c4113dSnw141292 	}
295c5c4113dSnw141292 
296c5c4113dSnw141292 done:
297c5c4113dSnw141292 	if (labels != NULL) {
298c5c4113dSnw141292 		for (label = labels; *label != NULL; label++)
299c5c4113dSnw141292 			free(*label);
300c5c4113dSnw141292 		free(labels);
301c5c4113dSnw141292 	}
302c5c4113dSnw141292 	if (attrs != NULL)
303c5c4113dSnw141292 		ldap_value_free(attrs);
304c5c4113dSnw141292 	if (rdns != NULL)
305c5c4113dSnw141292 		ldap_value_free(rdns);
306c5c4113dSnw141292 
307c5c4113dSnw141292 	return (dns);
308c5c4113dSnw141292 }
309c5c4113dSnw141292 
310c5c4113dSnw141292 /*
311c5c4113dSnw141292  * Keep connection management simple for now, extend or replace later
312c5c4113dSnw141292  * with updated libsldap code.
313c5c4113dSnw141292  */
314c5c4113dSnw141292 #define	ADREAPERSLEEP	60
315c5c4113dSnw141292 #define	ADCONN_TIME	300
316c5c4113dSnw141292 
317c5c4113dSnw141292 /*
318c5c4113dSnw141292  * Idle connection reaping side of connection management
319c5c4113dSnw141292  *
320c5c4113dSnw141292  * Every minute wake up and look for connections that have been idle for
321c5c4113dSnw141292  * five minutes or more and close them.
322c5c4113dSnw141292  */
323c5c4113dSnw141292 /*ARGSUSED*/
324c5c4113dSnw141292 static
325c5c4113dSnw141292 void
326c5c4113dSnw141292 adreaper(void *arg)
327c5c4113dSnw141292 {
328c5c4113dSnw141292 	ad_host_t	*adh;
329c5c4113dSnw141292 	time_t		now;
330c5c4113dSnw141292 	timespec_t	ts;
331c5c4113dSnw141292 
332c5c4113dSnw141292 	ts.tv_sec = ADREAPERSLEEP;
333c5c4113dSnw141292 	ts.tv_nsec = 0;
334c5c4113dSnw141292 
335c5c4113dSnw141292 	for (;;) {
336c5c4113dSnw141292 		/*
337c5c4113dSnw141292 		 * nanosleep(3RT) is thead-safe (no SIGALRM) and more
338c5c4113dSnw141292 		 * portable than usleep(3C)
339c5c4113dSnw141292 		 */
340c5c4113dSnw141292 		(void) nanosleep(&ts, NULL);
341c5c4113dSnw141292 		(void) pthread_mutex_lock(&adhostlock);
342c5c4113dSnw141292 		now = time(NULL);
343c5c4113dSnw141292 		for (adh = host_head; adh != NULL; adh = adh->next) {
344c5c4113dSnw141292 			(void) pthread_mutex_lock(&adh->lock);
345c5c4113dSnw141292 			if (adh->ref == 0 && adh->idletime != 0 &&
346c5c4113dSnw141292 			    adh->idletime + ADCONN_TIME < now) {
347c5c4113dSnw141292 				if (adh->ld) {
348c5c4113dSnw141292 					(void) ldap_unbind(adh->ld);
349c5c4113dSnw141292 					adh->ld = NULL;
350c5c4113dSnw141292 					adh->idletime = 0;
351c5c4113dSnw141292 					adh->ref = 0;
352c5c4113dSnw141292 				}
353c5c4113dSnw141292 			}
354c5c4113dSnw141292 			(void) pthread_mutex_unlock(&adh->lock);
355c5c4113dSnw141292 		}
356c5c4113dSnw141292 		(void) pthread_mutex_unlock(&adhostlock);
357c5c4113dSnw141292 	}
358c5c4113dSnw141292 }
359c5c4113dSnw141292 
360c5c4113dSnw141292 int
361c5c4113dSnw141292 idmap_ad_alloc(ad_t **new_ad, const char *default_domain,
362c5c4113dSnw141292 		idmap_ad_partition_t part)
363c5c4113dSnw141292 {
364c5c4113dSnw141292 	ad_t *ad;
365c5c4113dSnw141292 
366c5c4113dSnw141292 	*new_ad = NULL;
367c5c4113dSnw141292 
368c5c4113dSnw141292 	if ((default_domain == NULL || *default_domain == '\0') &&
369c5c4113dSnw141292 	    part != IDMAP_AD_GLOBAL_CATALOG)
370c5c4113dSnw141292 		return (-1);
371c5c4113dSnw141292 
372c5c4113dSnw141292 	if ((ad = calloc(1, sizeof (ad_t))) == NULL)
373c5c4113dSnw141292 		return (-1);
374c5c4113dSnw141292 
375c5c4113dSnw141292 	ad->ref = 1;
376c5c4113dSnw141292 	ad->partition = part;
377c5c4113dSnw141292 
378c5c4113dSnw141292 	/*
379c5c4113dSnw141292 	 * If default_domain is NULL, deal, deferring errors until
380c5c4113dSnw141292 	 * idmap_lookup_batch_start() -- this makes it easier on the
381c5c4113dSnw141292 	 * caller, who can simply observe lookups failing as opposed to
382c5c4113dSnw141292 	 * having to conditionalize calls to lookups according to
383c5c4113dSnw141292 	 * whether it has a non-NULL ad_t *.
384c5c4113dSnw141292 	 */
385c5c4113dSnw141292 	if (default_domain == NULL)
386c5c4113dSnw141292 		default_domain = "";
387c5c4113dSnw141292 
388c5c4113dSnw141292 	if ((ad->dflt_w2k_dom = strdup(default_domain)) == NULL)
389c5c4113dSnw141292 		goto err;
390c5c4113dSnw141292 
391c5c4113dSnw141292 	if (pthread_mutex_init(&ad->lock, NULL) != 0)
392c5c4113dSnw141292 		goto err;
393c5c4113dSnw141292 
394c5c4113dSnw141292 	*new_ad = ad;
395c5c4113dSnw141292 
396c5c4113dSnw141292 	return (0);
397c5c4113dSnw141292 err:
398c5c4113dSnw141292 	if (ad->dflt_w2k_dom != NULL)
399c5c4113dSnw141292 		free(ad->dflt_w2k_dom);
400c5c4113dSnw141292 	free(ad);
401c5c4113dSnw141292 	return (-1);
402c5c4113dSnw141292 }
403c5c4113dSnw141292 
404c5c4113dSnw141292 
405c5c4113dSnw141292 void
406c5c4113dSnw141292 idmap_ad_free(ad_t **ad)
407c5c4113dSnw141292 {
408c5c4113dSnw141292 	ad_host_t *p;
409c8e26105Sjp151216 	ad_host_t *prev;
410c5c4113dSnw141292 
411c5c4113dSnw141292 	if (ad == NULL || *ad == NULL)
412c5c4113dSnw141292 		return;
413c5c4113dSnw141292 
414c5c4113dSnw141292 	(void) pthread_mutex_lock(&(*ad)->lock);
415c5c4113dSnw141292 
416c5c4113dSnw141292 	if (atomic_dec_32_nv(&(*ad)->ref) > 0) {
417c5c4113dSnw141292 		(void) pthread_mutex_unlock(&(*ad)->lock);
418c5c4113dSnw141292 		*ad = NULL;
419c5c4113dSnw141292 		return;
420c5c4113dSnw141292 	}
421c5c4113dSnw141292 
422c8e26105Sjp151216 	(void) pthread_mutex_lock(&adhostlock);
423c8e26105Sjp151216 	prev = NULL;
424c8e26105Sjp151216 	p = host_head;
425c8e26105Sjp151216 	while (p != NULL) {
426c8e26105Sjp151216 		if (p->owner != (*ad)) {
427c8e26105Sjp151216 			prev = p;
428c8e26105Sjp151216 			p = p->next;
429c5c4113dSnw141292 			continue;
430c8e26105Sjp151216 		} else {
431c8e26105Sjp151216 			delete_ds((*ad), p->host, p->port);
432c8e26105Sjp151216 			if (prev == NULL)
433c8e26105Sjp151216 				p = host_head;
434c8e26105Sjp151216 			else
435c8e26105Sjp151216 				p = prev->next;
436c5c4113dSnw141292 		}
437c8e26105Sjp151216 	}
438c8e26105Sjp151216 	(void) pthread_mutex_unlock(&adhostlock);
439c5c4113dSnw141292 
440c5c4113dSnw141292 	(void) pthread_mutex_unlock(&(*ad)->lock);
441c5c4113dSnw141292 	(void) pthread_mutex_destroy(&(*ad)->lock);
442c5c4113dSnw141292 
443e3c2d6aaSnw141292 	free((*ad)->dflt_w2k_dom);
444c5c4113dSnw141292 	free(*ad);
445c5c4113dSnw141292 
446c5c4113dSnw141292 	*ad = NULL;
447c5c4113dSnw141292 }
448c5c4113dSnw141292 
449c8e26105Sjp151216 
450c5c4113dSnw141292 static
451c5c4113dSnw141292 int
452c5c4113dSnw141292 idmap_open_conn(ad_host_t *adh)
453c5c4113dSnw141292 {
454c5c4113dSnw141292 	int zero = 0;
455c5c4113dSnw141292 	int timeoutms = 30 * 1000;
456c8e26105Sjp151216 	int ldversion, rc;
457c8e26105Sjp151216 
458c8e26105Sjp151216 	if (adh == NULL)
459c8e26105Sjp151216 		return (0);
460c8e26105Sjp151216 
461c8e26105Sjp151216 	(void) pthread_mutex_lock(&adh->lock);
462c8e26105Sjp151216 
463c8e26105Sjp151216 	if (!adh->dead && adh->ld != NULL)
464c8e26105Sjp151216 		/* done! */
465c8e26105Sjp151216 		goto out;
466c8e26105Sjp151216 
467c8e26105Sjp151216 	if (adh->ld != NULL) {
468c8e26105Sjp151216 		(void) ldap_unbind(adh->ld);
469c8e26105Sjp151216 		adh->ld = NULL;
470c8e26105Sjp151216 	}
471c5c4113dSnw141292 
472c5c4113dSnw141292 	atomic_inc_64(&adh->generation);
473c8e26105Sjp151216 
474c8e26105Sjp151216 	/* Open and bind an LDAP connection */
475c5c4113dSnw141292 	adh->ld = ldap_init(adh->host, adh->port);
476c8e26105Sjp151216 	if (adh->ld == NULL) {
477c8e26105Sjp151216 		idmapdlog(LOG_INFO, "ldap_init() to server "
478c8e26105Sjp151216 		    "%s port %d failed. (%s)", adh->host,
479c8e26105Sjp151216 		    adh->port, strerror(errno));
480c8e26105Sjp151216 		goto out;
481c8e26105Sjp151216 	}
482c5c4113dSnw141292 	ldversion = LDAP_VERSION3;
483c8e26105Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_OPT_PROTOCOL_VERSION, &ldversion);
484c8e26105Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
485c5c4113dSnw141292 	(void) ldap_set_option(adh->ld, LDAP_OPT_TIMELIMIT, &zero);
486c5c4113dSnw141292 	(void) ldap_set_option(adh->ld, LDAP_OPT_SIZELIMIT, &zero);
487c8e26105Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_X_OPT_CONNECT_TIMEOUT, &timeoutms);
488c5c4113dSnw141292 	(void) ldap_set_option(adh->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
489c8e26105Sjp151216 	rc = ldap_sasl_interactive_bind_s(adh->ld, "" /* binddn */,
490c8e26105Sjp151216 	    adh->saslmech, NULL, NULL, adh->saslflags, &idmap_saslcallback,
491c8e26105Sjp151216 	    NULL);
492c5c4113dSnw141292 
493c5c4113dSnw141292 	if (rc != LDAP_SUCCESS) {
494c8e26105Sjp151216 		(void) ldap_unbind(adh->ld);
495c8e26105Sjp151216 		adh->ld = NULL;
496c8e26105Sjp151216 		idmapdlog(LOG_INFO, "ldap_sasl_interactive_bind_s() to server "
497c8e26105Sjp151216 		    "%s port %d failed. (%s)", adh->host, adh->port,
498c8e26105Sjp151216 		    ldap_err2string(rc));
499c5c4113dSnw141292 	}
500c5c4113dSnw141292 
501c8e26105Sjp151216 	idmapdlog(LOG_DEBUG, "Using global catalog server %s:%d",
502c8e26105Sjp151216 	    adh->host, adh->port);
503c8e26105Sjp151216 
504c8e26105Sjp151216 out:
505c8e26105Sjp151216 	if (adh->ld != NULL) {
506c8e26105Sjp151216 		atomic_inc_32(&adh->ref);
507c5c4113dSnw141292 		adh->idletime = time(NULL);
508c8e26105Sjp151216 		adh->dead = 0;
509c8e26105Sjp151216 		(void) pthread_mutex_unlock(&adh->lock);
510c8e26105Sjp151216 		return (1);
511c8e26105Sjp151216 	}
512c5c4113dSnw141292 
513c8e26105Sjp151216 	(void) pthread_mutex_unlock(&adh->lock);
514c8e26105Sjp151216 	return (0);
515c5c4113dSnw141292 }
516c5c4113dSnw141292 
517c5c4113dSnw141292 
518c5c4113dSnw141292 /*
519c5c4113dSnw141292  * Connection management: find an open connection or open one
520c5c4113dSnw141292  */
521c5c4113dSnw141292 static
522c5c4113dSnw141292 ad_host_t *
523c8e26105Sjp151216 idmap_get_conn(ad_t *ad)
524c5c4113dSnw141292 {
525c5c4113dSnw141292 	ad_host_t	*adh = NULL;
526c8e26105Sjp151216 	ad_host_t	*first_adh, *next_adh;
527c8e26105Sjp151216 	int		seen_last;
528c8e26105Sjp151216 	int		tries = -1;
529c5c4113dSnw141292 
530c8e26105Sjp151216 retry:
531c5c4113dSnw141292 	(void) pthread_mutex_lock(&adhostlock);
532c5c4113dSnw141292 
533c8e26105Sjp151216 	if (host_head == NULL)
534c8e26105Sjp151216 		goto out;
535c8e26105Sjp151216 
536c8e26105Sjp151216 	/* Try as many DSs as we have, once each; count them once */
537c8e26105Sjp151216 	if (tries < 0) {
538c8e26105Sjp151216 		for (adh = host_head, tries = 0; adh != NULL; adh = adh->next)
539c8e26105Sjp151216 			tries++;
540c8e26105Sjp151216 	}
541c8e26105Sjp151216 
542c5c4113dSnw141292 	/*
543c8e26105Sjp151216 	 * Find a suitable ad_host_t (one associated with this ad_t,
544c8e26105Sjp151216 	 * preferably one that's already connected and not dead),
545c8e26105Sjp151216 	 * possibly round-robining through the ad_host_t list.
546c8e26105Sjp151216 	 *
547c8e26105Sjp151216 	 * If we can't find a non-dead ad_host_t and we've had one
548c8e26105Sjp151216 	 * before (ad->last_adh != NULL) then pick the next one or, if
549c8e26105Sjp151216 	 * there is no next one, the first one (i.e., round-robin).
550c8e26105Sjp151216 	 *
551c8e26105Sjp151216 	 * If we've never had one then (ad->last_adh == NULL) then pick
552c8e26105Sjp151216 	 * the first one.
553c8e26105Sjp151216 	 *
554c8e26105Sjp151216 	 * If we ever want to be more clever, such as preferring DSes
555c8e26105Sjp151216 	 * with better average response times, adjusting for weights
556c8e26105Sjp151216 	 * from SRV RRs, and so on, then this is the place to do it.
557c5c4113dSnw141292 	 */
558c8e26105Sjp151216 
559c8e26105Sjp151216 	for (adh = host_head, first_adh = NULL, next_adh = NULL, seen_last = 0;
560c8e26105Sjp151216 	    adh != NULL; adh = adh->next) {
561c8e26105Sjp151216 		if (adh->owner != ad)
562c8e26105Sjp151216 			continue;
563c8e26105Sjp151216 		if (first_adh == NULL)
564c8e26105Sjp151216 			first_adh = adh;
565c8e26105Sjp151216 		if (adh == ad->last_adh)
566c8e26105Sjp151216 			seen_last++;
567c8e26105Sjp151216 		else if (seen_last)
568c8e26105Sjp151216 			next_adh = adh;
569c8e26105Sjp151216 
570c8e26105Sjp151216 		/* First time or current adh is live -> done */
571c8e26105Sjp151216 		if (ad->last_adh == NULL || (!adh->dead && adh->ld != NULL))
572c5c4113dSnw141292 			break;
573c5c4113dSnw141292 	}
574c8e26105Sjp151216 
575c8e26105Sjp151216 	/* Round-robin */
576c8e26105Sjp151216 	if (adh == NULL)
577c8e26105Sjp151216 		adh = (next_adh != NULL) ? next_adh : first_adh;
578c5c4113dSnw141292 
579c5c4113dSnw141292 	if (adh != NULL)
580c8e26105Sjp151216 		ad->last_adh = adh;
581c5c4113dSnw141292 
582c8e26105Sjp151216 out:
583c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adhostlock);
584c5c4113dSnw141292 
585c8e26105Sjp151216 	/* Found suitable DS, open it if not already opened */
586c8e26105Sjp151216 	if (idmap_open_conn(adh))
587c5c4113dSnw141292 		return (adh);
588c8e26105Sjp151216 
589c8e26105Sjp151216 	if (tries-- > 0)
590c8e26105Sjp151216 		goto retry;
591c8e26105Sjp151216 
592c8e26105Sjp151216 	idmapdlog(LOG_ERR, "Couldn't open an LDAP connection to any global "
593c8e26105Sjp151216 	    "catalog server!");
594c8e26105Sjp151216 
595c8e26105Sjp151216 	return (NULL);
596c5c4113dSnw141292 }
597c5c4113dSnw141292 
598c5c4113dSnw141292 static
599c5c4113dSnw141292 void
600c5c4113dSnw141292 idmap_release_conn(ad_host_t *adh)
601c5c4113dSnw141292 {
602c5c4113dSnw141292 	(void) pthread_mutex_lock(&adh->lock);
603c5c4113dSnw141292 	if (atomic_dec_32_nv(&adh->ref) == 0)
604c5c4113dSnw141292 		adh->idletime = time(NULL);
605c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adh->lock);
606c5c4113dSnw141292 }
607c5c4113dSnw141292 
608c5c4113dSnw141292 /*
609c5c4113dSnw141292  * Take ad_host_config_t information, create a ad_host_t,
610c5c4113dSnw141292  * populate it and add it to the list of hosts.
611c5c4113dSnw141292  */
612c5c4113dSnw141292 
613c5c4113dSnw141292 int
614c5c4113dSnw141292 idmap_add_ds(ad_t *ad, const char *host, int port)
615c5c4113dSnw141292 {
616c5c4113dSnw141292 	ad_host_t	*p;
617c5c4113dSnw141292 	ad_host_t	*new = NULL;
618c5c4113dSnw141292 	int		ret = -1;
619c5c4113dSnw141292 
620c5c4113dSnw141292 	if (port == 0)
621c5c4113dSnw141292 		port = (int)ad->partition;
622c5c4113dSnw141292 
623c5c4113dSnw141292 	(void) pthread_mutex_lock(&adhostlock);
624c5c4113dSnw141292 	for (p = host_head; p != NULL; p = p->next) {
625c5c4113dSnw141292 		if (p->owner != ad)
626c5c4113dSnw141292 			continue;
627c5c4113dSnw141292 
628c5c4113dSnw141292 		if (strcmp(host, p->host) == 0 && p->port == port) {
629c5c4113dSnw141292 			/* already added */
630c8e26105Sjp151216 			ret = 0;
631c5c4113dSnw141292 			goto err;
632c5c4113dSnw141292 		}
633c5c4113dSnw141292 	}
634c5c4113dSnw141292 
635c5c4113dSnw141292 	/* add new entry */
636c5c4113dSnw141292 	new = (ad_host_t *)calloc(1, sizeof (ad_host_t));
637c5c4113dSnw141292 	if (new == NULL)
638c5c4113dSnw141292 		goto err;
639c5c4113dSnw141292 	new->owner = ad;
640c5c4113dSnw141292 	new->port = port;
641c5c4113dSnw141292 	new->dead = 0;
642c5c4113dSnw141292 	if ((new->host = strdup(host)) == NULL)
643c5c4113dSnw141292 		goto err;
644c5c4113dSnw141292 
645c5c4113dSnw141292 	/* default to SASL GSSAPI only for now */
646c5c4113dSnw141292 	new->saslflags = LDAP_SASL_INTERACTIVE;
647c5c4113dSnw141292 	new->saslmech = "GSSAPI";
648c5c4113dSnw141292 
649c5c4113dSnw141292 	if ((ret = pthread_mutex_init(&new->lock, NULL)) != 0) {
650c5c4113dSnw141292 		free(new->host);
651c5c4113dSnw141292 		new->host = NULL;
652c5c4113dSnw141292 		errno = ret;
653c5c4113dSnw141292 		ret = -1;
654c5c4113dSnw141292 		goto err;
655c5c4113dSnw141292 	}
656c5c4113dSnw141292 
657c5c4113dSnw141292 	/* link in */
658c5c4113dSnw141292 	new->next = host_head;
659c5c4113dSnw141292 	host_head = new;
660c5c4113dSnw141292 
661c5c4113dSnw141292 	/* Start reaper if it doesn't exist */
662c5c4113dSnw141292 	if (reaperid == 0)
663c5c4113dSnw141292 		(void) pthread_create(&reaperid, NULL,
664c5c4113dSnw141292 		    (void *(*)(void *))adreaper, (void *)NULL);
665c5c4113dSnw141292 
666c5c4113dSnw141292 err:
667c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adhostlock);
668c5c4113dSnw141292 
669c5c4113dSnw141292 	if (ret != 0 && new != NULL) {
670c5c4113dSnw141292 		if (new->host != NULL) {
671c5c4113dSnw141292 			(void) pthread_mutex_destroy(&new->lock);
672c5c4113dSnw141292 			free(new->host);
673c5c4113dSnw141292 		}
674c5c4113dSnw141292 		free(new);
675c5c4113dSnw141292 	}
676c5c4113dSnw141292 
677c5c4113dSnw141292 	return (ret);
678c5c4113dSnw141292 }
679c5c4113dSnw141292 
680c5c4113dSnw141292 /*
681c8e26105Sjp151216  * Free a DS configuration.
682c8e26105Sjp151216  * Caller must lock the adhostlock mutex
683c5c4113dSnw141292  */
684c8e26105Sjp151216 static void
685c8e26105Sjp151216 delete_ds(ad_t *ad, const char *host, int port)
686c5c4113dSnw141292 {
687c5c4113dSnw141292 	ad_host_t	**p, *q;
688c5c4113dSnw141292 
689c5c4113dSnw141292 	for (p = &host_head; *p != NULL; p = &((*p)->next)) {
690c5c4113dSnw141292 		if ((*p)->owner != ad || strcmp(host, (*p)->host) != 0 ||
691c5c4113dSnw141292 		    (*p)->port != port)
692c5c4113dSnw141292 			continue;
693c5c4113dSnw141292 		/* found */
694c8e26105Sjp151216 		if ((*p)->ref > 0)
695c5c4113dSnw141292 			break;	/* still in use */
696c5c4113dSnw141292 
697c5c4113dSnw141292 		q = *p;
698c5c4113dSnw141292 		*p = (*p)->next;
699c5c4113dSnw141292 
700c5c4113dSnw141292 		(void) pthread_mutex_destroy(&q->lock);
701c5c4113dSnw141292 
702c5c4113dSnw141292 		if (q->ld)
703c5c4113dSnw141292 			(void) ldap_unbind(q->ld);
704c5c4113dSnw141292 		if (q->host)
705c5c4113dSnw141292 			free(q->host);
706c5c4113dSnw141292 		free(q);
707c5c4113dSnw141292 		break;
708c5c4113dSnw141292 	}
709c8e26105Sjp151216 
710c5c4113dSnw141292 }
711c5c4113dSnw141292 
712c8e26105Sjp151216 
713c5c4113dSnw141292 /*
714c5c4113dSnw141292  * Convert a binary SID in a BerValue to a sid_t
715c5c4113dSnw141292  */
716c5c4113dSnw141292 static
717c5c4113dSnw141292 int
718c5c4113dSnw141292 idmap_getsid(BerValue *bval, sid_t *sidp)
719c5c4113dSnw141292 {
720c5c4113dSnw141292 	int		i, j;
721c5c4113dSnw141292 	uchar_t		*v;
722c5c4113dSnw141292 	uint32_t	a;
723c5c4113dSnw141292 
724c5c4113dSnw141292 	/*
725c5c4113dSnw141292 	 * The binary format of a SID is as follows:
726c5c4113dSnw141292 	 *
727c5c4113dSnw141292 	 * byte #0: version, always 0x01
728c5c4113dSnw141292 	 * byte #1: RID count, always <= 0x0f
729c5c4113dSnw141292 	 * bytes #2-#7: SID authority, big-endian 48-bit unsigned int
730c5c4113dSnw141292 	 *
731c5c4113dSnw141292 	 * followed by RID count RIDs, each a little-endian, unsigned
732c5c4113dSnw141292 	 * 32-bit int.
733c5c4113dSnw141292 	 */
734c5c4113dSnw141292 	/*
735c5c4113dSnw141292 	 * Sanity checks: must have at least one RID, version must be
736c5c4113dSnw141292 	 * 0x01, and the length must be 8 + rid count * 4
737c5c4113dSnw141292 	 */
738c5c4113dSnw141292 	if (bval->bv_len > 8 && bval->bv_val[0] == 0x01 &&
739c5c4113dSnw141292 	    bval->bv_len == 1 + 1 + 6 + bval->bv_val[1] * 4) {
740c5c4113dSnw141292 		v = (uchar_t *)bval->bv_val;
741c5c4113dSnw141292 		sidp->version = v[0];
742c5c4113dSnw141292 		sidp->sub_authority_count = v[1];
743c5c4113dSnw141292 		sidp->authority =
744c5c4113dSnw141292 		    /* big endian -- so start from the left */
745c5c4113dSnw141292 		    ((u_longlong_t)v[2] << 40) |
746c5c4113dSnw141292 		    ((u_longlong_t)v[3] << 32) |
747c5c4113dSnw141292 		    ((u_longlong_t)v[4] << 24) |
748c5c4113dSnw141292 		    ((u_longlong_t)v[5] << 16) |
749c5c4113dSnw141292 		    ((u_longlong_t)v[6] << 8) |
750c5c4113dSnw141292 		    (u_longlong_t)v[7];
751c5c4113dSnw141292 		for (i = 0; i < sidp->sub_authority_count; i++) {
752c5c4113dSnw141292 			j = 8 + (i * 4);
753c5c4113dSnw141292 			/* little endian -- so start from the right */
754c5c4113dSnw141292 			a = (v[j + 3] << 24) | (v[j + 2] << 16) |
755c5c4113dSnw141292 			    (v[j + 1] << 8) | (v[j]);
756c5c4113dSnw141292 			sidp->sub_authorities[i] = a;
757c5c4113dSnw141292 		}
758c5c4113dSnw141292 		return (0);
759c5c4113dSnw141292 	}
760c5c4113dSnw141292 	return (-1);
761c5c4113dSnw141292 }
762c5c4113dSnw141292 
763c5c4113dSnw141292 /*
764c5c4113dSnw141292  * Convert a sid_t to S-1-...
765c5c4113dSnw141292  */
766c5c4113dSnw141292 static
767c5c4113dSnw141292 char *
768c5c4113dSnw141292 idmap_sid2txt(sid_t *sidp)
769c5c4113dSnw141292 {
770c5c4113dSnw141292 	int	rlen, i, len;
771c5c4113dSnw141292 	char	*str, *cp;
772c5c4113dSnw141292 
773c5c4113dSnw141292 	if (sidp->version != 1)
774c5c4113dSnw141292 		return (NULL);
775c5c4113dSnw141292 
776c5c4113dSnw141292 	len = sizeof ("S-1-") - 1;
777c5c4113dSnw141292 
778c5c4113dSnw141292 	/*
779c5c4113dSnw141292 	 * We could optimize like so, but, why?
780c5c4113dSnw141292 	 *	if (sidp->authority < 10)
781c5c4113dSnw141292 	 *		len += 2;
782c5c4113dSnw141292 	 *	else if (sidp->authority < 100)
783c5c4113dSnw141292 	 *		len += 3;
784c5c4113dSnw141292 	 *	else
785c5c4113dSnw141292 	 *		len += snprintf(NULL, 0"%llu", sidp->authority);
786c5c4113dSnw141292 	 */
787c5c4113dSnw141292 	len += snprintf(NULL, 0, "%llu", sidp->authority);
788c5c4113dSnw141292 
789c5c4113dSnw141292 	/* Max length of a uint32_t printed out in ASCII is 10 bytes */
790c5c4113dSnw141292 	len += 1 + (sidp->sub_authority_count + 1) * 10;
791c5c4113dSnw141292 
792c5c4113dSnw141292 	if ((cp = str = malloc(len)) == NULL)
793c5c4113dSnw141292 		return (NULL);
794c5c4113dSnw141292 
795c5c4113dSnw141292 	rlen = snprintf(str, len, "S-1-%llu", sidp->authority);
796c5c4113dSnw141292 
797c5c4113dSnw141292 	cp += rlen;
798c5c4113dSnw141292 	len -= rlen;
799c5c4113dSnw141292 
800c5c4113dSnw141292 	for (i = 0; i < sidp->sub_authority_count; i++) {
801c5c4113dSnw141292 		assert(len > 0);
802c5c4113dSnw141292 		rlen = snprintf(cp, len, "-%u", sidp->sub_authorities[i]);
803c5c4113dSnw141292 		cp += rlen;
804c5c4113dSnw141292 		len -= rlen;
805c5c4113dSnw141292 		assert(len >= 0);
806c5c4113dSnw141292 	}
807c5c4113dSnw141292 
808c5c4113dSnw141292 	return (str);
809c5c4113dSnw141292 }
810c5c4113dSnw141292 
811c5c4113dSnw141292 /*
812c5c4113dSnw141292  * Convert a sid_t to on-the-wire encoding
813c5c4113dSnw141292  */
814c5c4113dSnw141292 static
815c5c4113dSnw141292 int
816c5c4113dSnw141292 idmap_sid2binsid(sid_t *sid, uchar_t *binsid, int binsidlen)
817c5c4113dSnw141292 {
818c5c4113dSnw141292 	uchar_t		*p;
819c5c4113dSnw141292 	int		i;
820c5c4113dSnw141292 	uint64_t	a;
821c5c4113dSnw141292 	uint32_t	r;
822c5c4113dSnw141292 
823c5c4113dSnw141292 	if (sid->version != 1 ||
824c5c4113dSnw141292 	    binsidlen != (1 + 1 + 6 + sid->sub_authority_count * 4))
825c5c4113dSnw141292 		return (-1);
826c5c4113dSnw141292 
827c5c4113dSnw141292 	p = binsid;
828c5c4113dSnw141292 	*p++ = 0x01;		/* version */
829c5c4113dSnw141292 	/* sub authority count */
830c5c4113dSnw141292 	*p++ = sid->sub_authority_count;
831c5c4113dSnw141292 	/* Authority */
832c5c4113dSnw141292 	a = sid->authority;
833c5c4113dSnw141292 	/* big-endian -- start from left */
834c5c4113dSnw141292 	*p++ = (a >> 40) & 0xFF;
835c5c4113dSnw141292 	*p++ = (a >> 32) & 0xFF;
836c5c4113dSnw141292 	*p++ = (a >> 24) & 0xFF;
837c5c4113dSnw141292 	*p++ = (a >> 16) & 0xFF;
838c5c4113dSnw141292 	*p++ = (a >> 8) & 0xFF;
839c5c4113dSnw141292 	*p++ = a & 0xFF;
840c5c4113dSnw141292 
841c5c4113dSnw141292 	/* sub-authorities */
842c5c4113dSnw141292 	for (i = 0; i < sid->sub_authority_count; i++) {
843c5c4113dSnw141292 		r = sid->sub_authorities[i];
844c5c4113dSnw141292 		/* little-endian -- start from right */
845c5c4113dSnw141292 		*p++ = (r & 0x000000FF);
846c5c4113dSnw141292 		*p++ = (r & 0x0000FF00) >> 8;
847c5c4113dSnw141292 		*p++ = (r & 0x00FF0000) >> 16;
848c5c4113dSnw141292 		*p++ = (r & 0xFF000000) >> 24;
849c5c4113dSnw141292 	}
850c5c4113dSnw141292 
851c5c4113dSnw141292 	return (0);
852c5c4113dSnw141292 }
853c5c4113dSnw141292 
854c5c4113dSnw141292 /*
855c5c4113dSnw141292  * Convert a stringified SID (S-1-...) into a hex-encoded version of the
856c5c4113dSnw141292  * on-the-wire encoding, but with each pair of hex digits pre-pended
857c5c4113dSnw141292  * with a '\', so we can pass this to libldap.
858c5c4113dSnw141292  */
859c5c4113dSnw141292 static
860c5c4113dSnw141292 int
861c5c4113dSnw141292 idmap_txtsid2hexbinsid(const char *txt, const rid_t *rid,
862c5c4113dSnw141292 	char *hexbinsid, int hexbinsidlen)
863c5c4113dSnw141292 {
864c5c4113dSnw141292 	sid_t		sid = { 0 };
865c5c4113dSnw141292 	int		i, j;
866c5c4113dSnw141292 	const char	*cp;
867c5c4113dSnw141292 	char		*ecp;
868c5c4113dSnw141292 	u_longlong_t	a;
869c5c4113dSnw141292 	unsigned long	r;
870c5c4113dSnw141292 	uchar_t		*binsid, b, hb;
871c5c4113dSnw141292 
872c5c4113dSnw141292 	/* Only version 1 SIDs please */
873c5c4113dSnw141292 	if (strncmp(txt, "S-1-", strlen("S-1-")) != 0)
874c5c4113dSnw141292 		return (-1);
875c5c4113dSnw141292 
876c5c4113dSnw141292 	if (strlen(txt) < (strlen("S-1-") + 1))
877c5c4113dSnw141292 		return (-1);
878c5c4113dSnw141292 
879c5c4113dSnw141292 	/* count '-'s */
880c5c4113dSnw141292 	for (j = 0, cp = strchr(txt, '-');
881c5c4113dSnw141292 	    cp != NULL && *cp != '\0';
882c5c4113dSnw141292 	    j++, cp = strchr(cp + 1, '-')) {
883c5c4113dSnw141292 		/* can't end on a '-' */
884c5c4113dSnw141292 		if (*(cp + 1) == '\0')
885c5c4113dSnw141292 			return (-1);
886c5c4113dSnw141292 	}
887c5c4113dSnw141292 
88862c60062Sbaban 	/* Adjust count for version and authority */
88962c60062Sbaban 	j -= 2;
89062c60062Sbaban 
89162c60062Sbaban 	/* we know the version number and RID count */
89262c60062Sbaban 	sid.version = 1;
89362c60062Sbaban 	sid.sub_authority_count = (rid != NULL) ? j + 1 : j;
89462c60062Sbaban 
895c5c4113dSnw141292 	/* must have at least one RID, but not too many */
89662c60062Sbaban 	if (sid.sub_authority_count < 1 ||
89762c60062Sbaban 	    sid.sub_authority_count > SID_MAX_SUB_AUTHORITIES)
898c5c4113dSnw141292 		return (-1);
899c5c4113dSnw141292 
900c5c4113dSnw141292 	/* check that we only have digits and '-' */
901c5c4113dSnw141292 	if (strspn(txt + 1, "0123456789-") < (strlen(txt) - 1))
902c5c4113dSnw141292 		return (-1);
903c5c4113dSnw141292 
904c5c4113dSnw141292 	cp = txt + strlen("S-1-");
905c5c4113dSnw141292 
906c5c4113dSnw141292 	/* 64-bit safe parsing of unsigned 48-bit authority value */
907c5c4113dSnw141292 	errno = 0;
908c5c4113dSnw141292 	a = strtoull(cp, &ecp, 10);
909c5c4113dSnw141292 
910c5c4113dSnw141292 	/* errors parsing the authority or too many bits */
911c5c4113dSnw141292 	if (cp == ecp || (a == 0 && errno == EINVAL) ||
912c5c4113dSnw141292 	    (a == ULLONG_MAX && errno == ERANGE) ||
913c5c4113dSnw141292 	    (a & 0x0000ffffffffffffULL) != a)
914c5c4113dSnw141292 		return (-1);
915c5c4113dSnw141292 
916c5c4113dSnw141292 	cp = ecp;
917c5c4113dSnw141292 
918c5c4113dSnw141292 	sid.authority = (uint64_t)a;
919c5c4113dSnw141292 
92062c60062Sbaban 	for (i = 0; i < j; i++) {
921c5c4113dSnw141292 		if (*cp++ != '-')
922c5c4113dSnw141292 			return (-1);
923c5c4113dSnw141292 		/* 64-bit safe parsing of unsigned 32-bit RID */
924c5c4113dSnw141292 		errno = 0;
925c5c4113dSnw141292 		r = strtoul(cp, &ecp, 10);
926c5c4113dSnw141292 		/* errors parsing the RID or too many bits */
927c5c4113dSnw141292 		if (cp == ecp || (r == 0 && errno == EINVAL) ||
928c5c4113dSnw141292 		    (r == ULONG_MAX && errno == ERANGE) ||
929c5c4113dSnw141292 		    (r & 0xffffffffUL) != r)
930c5c4113dSnw141292 			return (-1);
931c5c4113dSnw141292 		sid.sub_authorities[i] = (uint32_t)r;
932c5c4113dSnw141292 		cp = ecp;
933c5c4113dSnw141292 	}
934c5c4113dSnw141292 
935c5c4113dSnw141292 	/* check that all of the string SID has been consumed */
936c5c4113dSnw141292 	if (*cp != '\0')
937c5c4113dSnw141292 		return (-1);
938c5c4113dSnw141292 
93962c60062Sbaban 	if (rid != NULL)
94062c60062Sbaban 		sid.sub_authorities[j] = *rid;
941c5c4113dSnw141292 
942c5c4113dSnw141292 	j = 1 + 1 + 6 + sid.sub_authority_count * 4;
943c5c4113dSnw141292 
944c5c4113dSnw141292 	if (hexbinsidlen < (j * 3))
945c5c4113dSnw141292 		return (-2);
946c5c4113dSnw141292 
947c5c4113dSnw141292 	/* binary encode the SID */
948c5c4113dSnw141292 	binsid = (uchar_t *)alloca(j);
949c5c4113dSnw141292 	(void) idmap_sid2binsid(&sid, binsid, j);
950c5c4113dSnw141292 
951c5c4113dSnw141292 	/* hex encode, with a backslash before each byte */
952c5c4113dSnw141292 	for (ecp = hexbinsid, i = 0; i < j; i++) {
953c5c4113dSnw141292 		b = binsid[i];
954c5c4113dSnw141292 		*ecp++ = '\\';
955c5c4113dSnw141292 		hb = (b >> 4) & 0xF;
956c5c4113dSnw141292 		*ecp++ = (hb <= 0x9 ? hb + '0' : hb - 10 + 'A');
957c5c4113dSnw141292 		hb = b & 0xF;
958c5c4113dSnw141292 		*ecp++ = (hb <= 0x9 ? hb + '0' : hb - 10 + 'A');
959c5c4113dSnw141292 	}
960c5c4113dSnw141292 	*ecp = '\0';
961c5c4113dSnw141292 
962c5c4113dSnw141292 	return (0);
963c5c4113dSnw141292 }
964c5c4113dSnw141292 
965c5c4113dSnw141292 static
966c5c4113dSnw141292 char *
967c5c4113dSnw141292 convert_bval2sid(BerValue *bval, rid_t *rid)
968c5c4113dSnw141292 {
969c5c4113dSnw141292 	sid_t	sid;
970c5c4113dSnw141292 
971c5c4113dSnw141292 	if (idmap_getsid(bval, &sid) < 0)
972c5c4113dSnw141292 		return (NULL);
973c5c4113dSnw141292 
974c5c4113dSnw141292 	/*
975c5c4113dSnw141292 	 * If desired and if the SID is what should be a domain/computer
976c5c4113dSnw141292 	 * user or group SID (i.e., S-1-5-w-x-y-z-<user/group RID>) then
977c5c4113dSnw141292 	 * save the last RID and truncate the SID
978c5c4113dSnw141292 	 */
979c5c4113dSnw141292 	if (rid != NULL && sid.authority == 5 && sid.sub_authority_count == 5)
980c5c4113dSnw141292 		*rid = sid.sub_authorities[--sid.sub_authority_count];
981c5c4113dSnw141292 	return (idmap_sid2txt(&sid));
982c5c4113dSnw141292 }
983c5c4113dSnw141292 
984c5c4113dSnw141292 
985c5c4113dSnw141292 idmap_retcode
986c5c4113dSnw141292 idmap_lookup_batch_start(ad_t *ad, int nqueries, idmap_query_state_t **state)
987c5c4113dSnw141292 {
988c5c4113dSnw141292 	idmap_query_state_t *new_state;
989c5c4113dSnw141292 	ad_host_t	*adh = NULL;
990c5c4113dSnw141292 
991c5c4113dSnw141292 	*state = NULL;
992c5c4113dSnw141292 
993*cd37da74Snw141292 	/* Note: ad->dflt_w2k_dom cannot be NULL - see idmap_ad_alloc() */
994*cd37da74Snw141292 	if (ad == NULL || *ad->dflt_w2k_dom == '\0')
995c5c4113dSnw141292 		return (-1);
996c5c4113dSnw141292 
997c5c4113dSnw141292 	adh = idmap_get_conn(ad);
998c5c4113dSnw141292 	if (adh == NULL)
999c5c4113dSnw141292 		return (IDMAP_ERR_OTHER);
1000c5c4113dSnw141292 
1001c5c4113dSnw141292 	new_state = calloc(1, sizeof (idmap_query_state_t) +
1002c5c4113dSnw141292 	    (nqueries - 1) * sizeof (idmap_q_t));
1003c5c4113dSnw141292 
1004c5c4113dSnw141292 	if (new_state == NULL)
1005c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
1006c5c4113dSnw141292 
100784decf41Sjp151216 	new_state->ref_cnt = 1;
1008c5c4113dSnw141292 	new_state->qadh = adh;
1009c5c4113dSnw141292 	new_state->qcount = nqueries;
1010c5c4113dSnw141292 	new_state->qadh_gen = adh->generation;
1011c5c4113dSnw141292 	/* should be -1, but the atomic routines want unsigned */
1012c5c4113dSnw141292 	new_state->qlastsent = 0;
101384decf41Sjp151216 	(void) pthread_cond_init(&new_state->cv, NULL);
1014c5c4113dSnw141292 
1015c5c4113dSnw141292 	(void) pthread_mutex_lock(&qstatelock);
1016c5c4113dSnw141292 	new_state->next = qstatehead;
1017c5c4113dSnw141292 	qstatehead = new_state;
1018c5c4113dSnw141292 	(void) pthread_mutex_unlock(&qstatelock);
1019c5c4113dSnw141292 
1020c5c4113dSnw141292 	*state = new_state;
1021c5c4113dSnw141292 
1022c5c4113dSnw141292 	return (IDMAP_SUCCESS);
1023c5c4113dSnw141292 }
1024c5c4113dSnw141292 
1025c5c4113dSnw141292 /*
1026c5c4113dSnw141292  * Find the idmap_query_state_t to which a given LDAP result msgid on a
102784decf41Sjp151216  * given connection belongs. This routine increaments the reference count
102884decf41Sjp151216  * so that the object can not be freed. idmap_lookup_unlock_batch()
102984decf41Sjp151216  * must be called to decreament the reference count.
1030c5c4113dSnw141292  */
1031c5c4113dSnw141292 static
1032c5c4113dSnw141292 int
1033c5c4113dSnw141292 idmap_msgid2query(ad_host_t *adh, int msgid,
1034c5c4113dSnw141292 	idmap_query_state_t **state, int *qid)
1035c5c4113dSnw141292 {
1036c5c4113dSnw141292 	idmap_query_state_t *p;
1037c5c4113dSnw141292 	int		    i;
1038c5c4113dSnw141292 
1039c5c4113dSnw141292 	(void) pthread_mutex_lock(&qstatelock);
1040c5c4113dSnw141292 	for (p = qstatehead; p != NULL; p = p->next) {
1041c5c4113dSnw141292 		if (p->qadh != adh || adh->generation != p->qadh_gen)
1042c5c4113dSnw141292 			continue;
1043c5c4113dSnw141292 		for (i = 0; i < p->qcount; i++) {
1044c5c4113dSnw141292 			if ((p->queries[i]).msgid == msgid) {
104584decf41Sjp151216 				p->ref_cnt++;
1046c5c4113dSnw141292 				*state = p;
1047c5c4113dSnw141292 				*qid = i;
1048c5c4113dSnw141292 				(void) pthread_mutex_unlock(&qstatelock);
1049c5c4113dSnw141292 				return (1);
1050c5c4113dSnw141292 			}
1051c5c4113dSnw141292 		}
1052c5c4113dSnw141292 	}
1053c5c4113dSnw141292 	(void) pthread_mutex_unlock(&qstatelock);
1054c5c4113dSnw141292 	return (0);
1055c5c4113dSnw141292 }
1056c5c4113dSnw141292 
1057c5c4113dSnw141292 /*
1058*cd37da74Snw141292  * Take parsed attribute values from a search result entry and check if
1059*cd37da74Snw141292  * it is the result that was desired and, if so, set the result fields
1060*cd37da74Snw141292  * of the given idmap_q_t.
1061*cd37da74Snw141292  *
1062*cd37da74Snw141292  * Frees the unused char * values, and returns 1 on success, 0 if the
1063*cd37da74Snw141292  * result entry was not applicable or if ENOMEM.
1064c5c4113dSnw141292  */
1065c5c4113dSnw141292 static
1066*cd37da74Snw141292 void
1067*cd37da74Snw141292 idmap_setqresults(idmap_q_t *q, char *san, char *dn, char *sid,
1068*cd37da74Snw141292 	rid_t rid, int sid_type)
1069c5c4113dSnw141292 {
1070*cd37da74Snw141292 	char *domain;
1071*cd37da74Snw141292 	int n2s, err1, err2;
1072*cd37da74Snw141292 
1073*cd37da74Snw141292 	assert(dn != NULL);
1074*cd37da74Snw141292 	assert(san != NULL || sid != NULL);
1075*cd37da74Snw141292 
1076*cd37da74Snw141292 	/*
1077*cd37da74Snw141292 	 * Name->SID lookups need a canonname output in addition to the
1078*cd37da74Snw141292 	 * SID, whereas SID->name lookups always put the canonical name
1079*cd37da74Snw141292 	 * in the result field.  So if q->canonname != NULL then this
1080*cd37da74Snw141292 	 * must be a name->SID lookup.
1081*cd37da74Snw141292 	 */
1082*cd37da74Snw141292 	n2s = (q->canonname != NULL);
1083*cd37da74Snw141292 
1084*cd37da74Snw141292 	if ((domain = dn2dns(dn)) == NULL)
1085*cd37da74Snw141292 		goto out;
1086*cd37da74Snw141292 
1087*cd37da74Snw141292 	if (n2s) {
1088*cd37da74Snw141292 		assert(q->edomain != NULL);
1089*cd37da74Snw141292 
1090*cd37da74Snw141292 		/* Check that this is the entry that we were looking for */
1091*cd37da74Snw141292 		if (u8_strcmp(q->ecanonname, san, 0,
1092*cd37da74Snw141292 		    U8_STRCMP_CI_LOWER, /* no normalization, for now */
1093*cd37da74Snw141292 		    U8_UNICODE_LATEST, &err1) != 0 || err1 != 0 ||
1094*cd37da74Snw141292 		    u8_strcmp(q->edomain, domain, 0, U8_STRCMP_CI_LOWER,
1095*cd37da74Snw141292 		    U8_UNICODE_LATEST, &err2) != 0 || err2 != 0)
1096*cd37da74Snw141292 			goto out;
1097*cd37da74Snw141292 
1098*cd37da74Snw141292 		/* Set name->SID results */
1099*cd37da74Snw141292 		*q->result = sid;
1100*cd37da74Snw141292 		*q->rid = rid;
1101*cd37da74Snw141292 		*q->sid_type = sid_type;
1102*cd37da74Snw141292 		/* We need the canonical SAN for case-insensitivity */
1103*cd37da74Snw141292 		*q->canonname = san;
1104*cd37da74Snw141292 
1105*cd37da74Snw141292 		/* Don't free these now that q references them */
1106*cd37da74Snw141292 		sid = NULL;
1107*cd37da74Snw141292 		san = NULL;
1108*cd37da74Snw141292 	} else {
1109*cd37da74Snw141292 		*q->sid_type = sid_type;
1110*cd37da74Snw141292 
1111*cd37da74Snw141292 		/* SID->name search result entry */
1112*cd37da74Snw141292 		if (q->domain != NULL) {
1113*cd37da74Snw141292 			/* name and domain in separate slots */
1114*cd37da74Snw141292 			*q->result = san;
1115*cd37da74Snw141292 			*q->domain = domain;
1116*cd37da74Snw141292 			/* Don't free these now that q references them */
1117*cd37da74Snw141292 			domain = NULL;
1118*cd37da74Snw141292 			san = NULL;
1119*cd37da74Snw141292 		} else {
1120*cd37da74Snw141292 			char *s;
1121*cd37da74Snw141292 			int len;
1122*cd37da74Snw141292 
1123*cd37da74Snw141292 			/* name@domain in one slot */
1124*cd37da74Snw141292 			len = strlen(san) + strlen(domain) + 2;
1125*cd37da74Snw141292 			if ((s = malloc(len)) == NULL) {
1126*cd37da74Snw141292 				idmapdlog(LOG_ERR, "%s", strerror(errno));
1127*cd37da74Snw141292 				goto out;
1128*cd37da74Snw141292 			}
1129*cd37da74Snw141292 			(void) snprintf(s, len, "%s@%s", san, domain);
1130*cd37da74Snw141292 			*q->result = s;
1131*cd37da74Snw141292 		}
1132*cd37da74Snw141292 	}
1133*cd37da74Snw141292 
1134*cd37da74Snw141292 	*q->rc = IDMAP_SUCCESS;
1135*cd37da74Snw141292 
1136*cd37da74Snw141292 out:
1137*cd37da74Snw141292 	/* Free unused attribute values */
1138*cd37da74Snw141292 	free(san);
1139*cd37da74Snw141292 	free(sid);
1140*cd37da74Snw141292 	free(domain);
1141c5c4113dSnw141292 }
1142c5c4113dSnw141292 
1143c5c4113dSnw141292 /*
1144*cd37da74Snw141292  * The following three functions extract objectSid, sAMAccountName and
1145*cd37da74Snw141292  * objectClass attribute values and, in the case of objectSid and
1146*cd37da74Snw141292  * objectClass, parse them.
1147*cd37da74Snw141292  *
1148*cd37da74Snw141292  * idmap_setqresults() takes care of dealing with the result entry's DN.
1149*cd37da74Snw141292  */
1150*cd37da74Snw141292 
1151*cd37da74Snw141292 /*
1152*cd37da74Snw141292  * Return a NUL-terminated stringified SID from the value of an
1153*cd37da74Snw141292  * objectSid attribute and put the last RID in *rid.
1154c5c4113dSnw141292  */
1155c5c4113dSnw141292 static
1156*cd37da74Snw141292 char *
1157*cd37da74Snw141292 idmap_bv_objsid2sidstr(BerValue **bvalues, rid_t *rid)
1158c5c4113dSnw141292 {
1159*cd37da74Snw141292 	char *sid;
1160*cd37da74Snw141292 
1161*cd37da74Snw141292 	if (bvalues == NULL)
1162*cd37da74Snw141292 		return (NULL);
1163*cd37da74Snw141292 	/* objectSid is single valued */
1164*cd37da74Snw141292 	if ((sid = convert_bval2sid(bvalues[0], rid)) == NULL)
1165*cd37da74Snw141292 		return (NULL);
1166*cd37da74Snw141292 	return (sid);
1167*cd37da74Snw141292 }
1168*cd37da74Snw141292 
1169*cd37da74Snw141292 /*
1170*cd37da74Snw141292  * Return a NUL-terminated string from the value of a sAMAccountName
1171*cd37da74Snw141292  * attribute.
1172*cd37da74Snw141292  */
1173*cd37da74Snw141292 static
1174*cd37da74Snw141292 char *
1175*cd37da74Snw141292 idmap_bv_samaccountname2name(BerValue **bvalues)
1176*cd37da74Snw141292 {
1177*cd37da74Snw141292 	char *s;
1178c5c4113dSnw141292 
1179c5c4113dSnw141292 	if (bvalues == NULL || bvalues[0] == NULL ||
1180c5c4113dSnw141292 	    bvalues[0]->bv_val == NULL)
1181*cd37da74Snw141292 		return (NULL);
1182e3c2d6aaSnw141292 
1183*cd37da74Snw141292 	if ((s = malloc(bvalues[0]->bv_len + 1)) == NULL)
1184*cd37da74Snw141292 		return (NULL);
1185e3c2d6aaSnw141292 
1186*cd37da74Snw141292 	(void) snprintf(s, bvalues[0]->bv_len + 1, "%.*s", bvalues[0]->bv_len,
1187*cd37da74Snw141292 	    bvalues[0]->bv_val);
1188e3c2d6aaSnw141292 
1189*cd37da74Snw141292 	return (s);
1190c5c4113dSnw141292 }
1191c5c4113dSnw141292 
1192c5c4113dSnw141292 
1193c5c4113dSnw141292 #define	BVAL_CASEEQ(bv, str) \
1194c5c4113dSnw141292 		(((*(bv))->bv_len == (sizeof (str) - 1)) && \
1195c5c4113dSnw141292 		    strncasecmp((*(bv))->bv_val, str, (*(bv))->bv_len) == 0)
1196c5c4113dSnw141292 
1197c5c4113dSnw141292 /*
1198*cd37da74Snw141292  * Extract the class of the result entry.  Returns 1 on success, 0 on
1199*cd37da74Snw141292  * failure.
1200c5c4113dSnw141292  */
1201c5c4113dSnw141292 static
1202e3c2d6aaSnw141292 int
1203*cd37da74Snw141292 idmap_bv_objclass2sidtype(BerValue **bvalues, int *sid_type)
1204c5c4113dSnw141292 {
1205c5c4113dSnw141292 	BerValue	**cbval;
1206c5c4113dSnw141292 
1207*cd37da74Snw141292 	*sid_type = _IDMAP_T_OTHER;
1208c5c4113dSnw141292 	if (bvalues == NULL)
1209e3c2d6aaSnw141292 		return (0);
1210c5c4113dSnw141292 
1211*cd37da74Snw141292 	/*
1212*cd37da74Snw141292 	 * We iterate over all the values because computer is a
1213*cd37da74Snw141292 	 * sub-class of user.
1214*cd37da74Snw141292 	 */
1215c5c4113dSnw141292 	for (cbval = bvalues; *cbval != NULL; cbval++) {
1216c5c4113dSnw141292 		if (BVAL_CASEEQ(cbval, "Computer")) {
1217*cd37da74Snw141292 			*sid_type = _IDMAP_T_COMPUTER;
1218*cd37da74Snw141292 			break;
1219c5c4113dSnw141292 		} else if (BVAL_CASEEQ(cbval, "Group")) {
1220*cd37da74Snw141292 			*sid_type = _IDMAP_T_GROUP;
1221*cd37da74Snw141292 			break;
1222c5c4113dSnw141292 		} else if (BVAL_CASEEQ(cbval, "USER")) {
1223*cd37da74Snw141292 			*sid_type = _IDMAP_T_USER;
1224*cd37da74Snw141292 			/* Continue looping -- this may be a computer yet */
1225*cd37da74Snw141292 		}
1226*cd37da74Snw141292 		/*
1227*cd37da74Snw141292 		 * "else if (*sid_type = _IDMAP_T_USER)" then this is a
1228*cd37da74Snw141292 		 * new sub-class of user -- what to do with it??
1229*cd37da74Snw141292 		 */
1230c5c4113dSnw141292 	}
1231e3c2d6aaSnw141292 
1232e3c2d6aaSnw141292 	return (1);
1233c5c4113dSnw141292 }
1234c5c4113dSnw141292 
1235c5c4113dSnw141292 /*
1236c5c4113dSnw141292  * Handle a given search result entry
1237c5c4113dSnw141292  */
1238c5c4113dSnw141292 static
1239c5c4113dSnw141292 void
1240c5c4113dSnw141292 idmap_extract_object(idmap_query_state_t *state, int qid, LDAPMessage *res)
1241c5c4113dSnw141292 {
1242c5c4113dSnw141292 	BerElement		*ber = NULL;
1243c5c4113dSnw141292 	BerValue		**bvalues;
1244c5c4113dSnw141292 	ad_host_t		*adh;
1245c5c4113dSnw141292 	idmap_q_t		*q;
1246*cd37da74Snw141292 	char			*attr;
1247*cd37da74Snw141292 	char			*dn = NULL;
1248*cd37da74Snw141292 	char			*san = NULL;
1249*cd37da74Snw141292 	char			*sid = NULL;
1250*cd37da74Snw141292 	rid_t			rid = 0;
1251*cd37da74Snw141292 	int			sid_type;
1252e3c2d6aaSnw141292 	int			has_class, has_san, has_sid;
1253c5c4113dSnw141292 
1254c5c4113dSnw141292 	adh = state->qadh;
1255c5c4113dSnw141292 
1256c5c4113dSnw141292 	(void) pthread_mutex_lock(&adh->lock);
1257c5c4113dSnw141292 
1258e3c2d6aaSnw141292 	q = &(state->queries[qid]);
1259e3c2d6aaSnw141292 
1260e3c2d6aaSnw141292 	if (*q->rc == IDMAP_SUCCESS || adh->dead ||
1261e3c2d6aaSnw141292 	    (dn = ldap_get_dn(adh->ld, res)) == NULL) {
1262c5c4113dSnw141292 		(void) pthread_mutex_unlock(&adh->lock);
1263c5c4113dSnw141292 		return;
1264c5c4113dSnw141292 	}
1265c5c4113dSnw141292 
1266e3c2d6aaSnw141292 	assert(*q->result == NULL);
1267e3c2d6aaSnw141292 	assert(q->domain == NULL || *q->domain == NULL);
1268c5c4113dSnw141292 
1269e3c2d6aaSnw141292 	has_class = has_san = has_sid = 0;
1270c5c4113dSnw141292 	for (attr = ldap_first_attribute(adh->ld, res, &ber); attr != NULL;
1271c5c4113dSnw141292 	    attr = ldap_next_attribute(adh->ld, res, ber)) {
1272c5c4113dSnw141292 		bvalues = NULL;	/* for memory management below */
1273c5c4113dSnw141292 
1274c5c4113dSnw141292 		/*
1275c5c4113dSnw141292 		 * If this is an attribute we are looking for and
1276c5c4113dSnw141292 		 * haven't seen it yet, parse it
1277c5c4113dSnw141292 		 */
1278*cd37da74Snw141292 		if (q->ecanonname != NULL && !has_sid &&
1279e3c2d6aaSnw141292 		    strcasecmp(attr, OBJSID) == 0) {
1280c5c4113dSnw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
1281*cd37da74Snw141292 			sid = idmap_bv_objsid2sidstr(bvalues, &rid);
1282*cd37da74Snw141292 			has_sid = (sid != NULL);
1283e3c2d6aaSnw141292 		} else if (!has_san && strcasecmp(attr, SAN) == 0) {
1284c5c4113dSnw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
1285*cd37da74Snw141292 			san = idmap_bv_samaccountname2name(bvalues);
1286*cd37da74Snw141292 			has_san = (san != NULL);
1287e3c2d6aaSnw141292 		} else if (!has_class && strcasecmp(attr, OBJCLASS) == 0) {
1288c5c4113dSnw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
1289*cd37da74Snw141292 			has_class = idmap_bv_objclass2sidtype(bvalues,
1290*cd37da74Snw141292 			    &sid_type);
1291c5c4113dSnw141292 		}
1292c5c4113dSnw141292 
1293c5c4113dSnw141292 		if (bvalues != NULL)
1294c5c4113dSnw141292 			ldap_value_free_len(bvalues);
1295c5c4113dSnw141292 		ldap_memfree(attr);
1296c5c4113dSnw141292 
1297*cd37da74Snw141292 		if (has_class && has_san &&
1298*cd37da74Snw141292 		    (q->ecanonname == NULL || has_sid)) {
1299*cd37da74Snw141292 			/* Got what we need, set results if relevant */
1300*cd37da74Snw141292 			idmap_setqresults(q, san, dn, sid, rid, sid_type);
1301e3c2d6aaSnw141292 			break;
1302c5c4113dSnw141292 		}
1303e3c2d6aaSnw141292 	}
1304e3c2d6aaSnw141292 
1305e3c2d6aaSnw141292 	(void) pthread_mutex_unlock(&adh->lock);
1306c5c4113dSnw141292 
1307c5c4113dSnw141292 	if (ber != NULL)
1308c5c4113dSnw141292 		ber_free(ber, 0);
1309c5c4113dSnw141292 
1310c5c4113dSnw141292 	ldap_memfree(dn);
1311c5c4113dSnw141292 }
1312c5c4113dSnw141292 
1313c5c4113dSnw141292 /*
1314c5c4113dSnw141292  * Try to get a result; if there is one, find the corresponding
1315c5c4113dSnw141292  * idmap_q_t and process the result.
1316c5c4113dSnw141292  */
1317c5c4113dSnw141292 static
1318c5c4113dSnw141292 int
1319c5c4113dSnw141292 idmap_get_adobject_batch(ad_host_t *adh, struct timeval *timeout)
1320c5c4113dSnw141292 {
1321c5c4113dSnw141292 	idmap_query_state_t	*query_state;
1322c5c4113dSnw141292 	LDAPMessage		*res = NULL;
1323c5c4113dSnw141292 	int			rc, ret, msgid, qid;
1324c5c4113dSnw141292 
1325c5c4113dSnw141292 	(void) pthread_mutex_lock(&adh->lock);
1326c5c4113dSnw141292 	if (adh->dead) {
1327c5c4113dSnw141292 		(void) pthread_mutex_unlock(&adh->lock);
1328c5c4113dSnw141292 		return (-1);
1329c5c4113dSnw141292 	}
1330c5c4113dSnw141292 
1331c5c4113dSnw141292 	/* Get one result */
1332c5c4113dSnw141292 	rc = ldap_result(adh->ld, LDAP_RES_ANY, 0,
1333c5c4113dSnw141292 	    timeout, &res);
1334c5c4113dSnw141292 	if (rc == LDAP_UNAVAILABLE || rc == LDAP_UNWILLING_TO_PERFORM ||
1335c5c4113dSnw141292 	    rc == LDAP_CONNECT_ERROR || rc == LDAP_SERVER_DOWN ||
1336c5c4113dSnw141292 	    rc == LDAP_BUSY)
1337c5c4113dSnw141292 		adh->dead = 1;
1338c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adh->lock);
1339c5c4113dSnw141292 
1340c5c4113dSnw141292 	if (adh->dead)
1341c5c4113dSnw141292 		return (-1);
1342c5c4113dSnw141292 
1343c5c4113dSnw141292 	switch (rc) {
1344c5c4113dSnw141292 	case LDAP_RES_SEARCH_RESULT:
1345c5c4113dSnw141292 		/* We have all the LDAP replies for some search... */
1346c5c4113dSnw141292 		msgid = ldap_msgid(res);
1347c5c4113dSnw141292 		if (idmap_msgid2query(adh, msgid,
1348c5c4113dSnw141292 		    &query_state, &qid)) {
1349c5c4113dSnw141292 			/* ...so we can decrement qinflight */
1350c5c4113dSnw141292 			atomic_dec_32(&query_state->qinflight);
1351e3c2d6aaSnw141292 			/* We've seen all the result entries we'll see */
1352e3c2d6aaSnw141292 			if (*query_state->queries[qid].rc != IDMAP_SUCCESS)
1353e3c2d6aaSnw141292 				*query_state->queries[qid].rc =
1354e3c2d6aaSnw141292 				    IDMAP_ERR_NOTFOUND;
135584decf41Sjp151216 			idmap_lookup_unlock_batch(&query_state);
1356c5c4113dSnw141292 		}
1357c5c4113dSnw141292 		(void) ldap_msgfree(res);
1358c5c4113dSnw141292 		ret = 0;
1359c5c4113dSnw141292 		break;
1360c5c4113dSnw141292 	case LDAP_RES_SEARCH_REFERENCE:
1361c5c4113dSnw141292 		/*
1362c5c4113dSnw141292 		 * We have no need for these at the moment.  Eventually,
1363c5c4113dSnw141292 		 * when we query things that we can't expect to find in
1364c5c4113dSnw141292 		 * the Global Catalog then we'll need to learn to follow
1365c5c4113dSnw141292 		 * references.
1366c5c4113dSnw141292 		 */
1367c5c4113dSnw141292 		(void) ldap_msgfree(res);
1368c5c4113dSnw141292 		ret = 0;
1369c5c4113dSnw141292 		break;
1370c5c4113dSnw141292 	case LDAP_RES_SEARCH_ENTRY:
1371c5c4113dSnw141292 		/* Got a result */
1372c5c4113dSnw141292 		msgid = ldap_msgid(res);
1373c5c4113dSnw141292 		if (idmap_msgid2query(adh, msgid,
1374c5c4113dSnw141292 		    &query_state, &qid)) {
1375c5c4113dSnw141292 			idmap_extract_object(query_state, qid, res);
1376c5c4113dSnw141292 			/* we saw at least one result */
137784decf41Sjp151216 			idmap_lookup_unlock_batch(&query_state);
1378c5c4113dSnw141292 		}
1379c5c4113dSnw141292 		(void) ldap_msgfree(res);
1380c5c4113dSnw141292 		ret = 0;
1381c5c4113dSnw141292 		break;
1382c5c4113dSnw141292 	default:
1383c5c4113dSnw141292 		/* timeout or error; treat the same */
1384c5c4113dSnw141292 		ret = -1;
1385c5c4113dSnw141292 		break;
1386c5c4113dSnw141292 	}
1387c5c4113dSnw141292 
1388c5c4113dSnw141292 	return (ret);
1389c5c4113dSnw141292 }
1390c5c4113dSnw141292 
139184decf41Sjp151216 /*
139284decf41Sjp151216  * This routine decreament the reference count of the
139384decf41Sjp151216  * idmap_query_state_t
139484decf41Sjp151216  */
139584decf41Sjp151216 static void
139684decf41Sjp151216 idmap_lookup_unlock_batch(idmap_query_state_t **state)
139784decf41Sjp151216 {
139884decf41Sjp151216 	/*
139984decf41Sjp151216 	 * Decrement reference count with qstatelock locked
140084decf41Sjp151216 	 */
140184decf41Sjp151216 	(void) pthread_mutex_lock(&qstatelock);
140284decf41Sjp151216 	(*state)->ref_cnt--;
140384decf41Sjp151216 	/*
140484decf41Sjp151216 	 * If there are no references wakup the allocating thread
140584decf41Sjp151216 	 */
140684decf41Sjp151216 	if ((*state)->ref_cnt == 0)
140784decf41Sjp151216 		(void) pthread_cond_signal(&(*state)->cv);
140884decf41Sjp151216 	(void) pthread_mutex_unlock(&qstatelock);
140984decf41Sjp151216 	*state = NULL;
141084decf41Sjp151216 }
141184decf41Sjp151216 
1412e3c2d6aaSnw141292 static
1413e3c2d6aaSnw141292 void
1414e3c2d6aaSnw141292 idmap_cleanup_batch(idmap_query_state_t *batch)
1415e3c2d6aaSnw141292 {
1416e3c2d6aaSnw141292 	int i;
1417e3c2d6aaSnw141292 
1418e3c2d6aaSnw141292 	for (i = 0; i < batch->qcount; i++) {
1419*cd37da74Snw141292 		if (batch->queries[i].ecanonname != NULL)
1420*cd37da74Snw141292 			free(batch->queries[i].ecanonname);
1421*cd37da74Snw141292 		batch->queries[i].ecanonname = NULL;
1422*cd37da74Snw141292 		if (batch->queries[i].edomain != NULL)
1423*cd37da74Snw141292 			free(batch->queries[i].edomain);
1424*cd37da74Snw141292 		batch->queries[i].edomain = NULL;
1425e3c2d6aaSnw141292 	}
1426e3c2d6aaSnw141292 }
1427e3c2d6aaSnw141292 
142884decf41Sjp151216 /*
142984decf41Sjp151216  * This routine frees the idmap_query_state_t structure
143084decf41Sjp151216  * If the reference count is greater than 1 it waits
143184decf41Sjp151216  * for the other threads to finish using it.
143284decf41Sjp151216  */
1433c5c4113dSnw141292 void
143484decf41Sjp151216 idmap_lookup_release_batch(idmap_query_state_t **state)
1435c5c4113dSnw141292 {
1436c5c4113dSnw141292 	idmap_query_state_t **p;
1437c5c4113dSnw141292 
143884decf41Sjp151216 	/*
143984decf41Sjp151216 	 * Decrement reference count with qstatelock locked
144084decf41Sjp151216 	 * and wait for reference count to get to zero
144184decf41Sjp151216 	 */
144284decf41Sjp151216 	(void) pthread_mutex_lock(&qstatelock);
144384decf41Sjp151216 	(*state)->ref_cnt--;
144484decf41Sjp151216 	while ((*state)->ref_cnt > 0) {
144584decf41Sjp151216 		(void) pthread_cond_wait(&(*state)->cv, &qstatelock);
144684decf41Sjp151216 	}
1447c5c4113dSnw141292 
1448c5c4113dSnw141292 	/* Remove this state struct from the list of state structs */
1449c5c4113dSnw141292 	for (p = &qstatehead; *p != NULL; p = &(*p)->next) {
1450c5c4113dSnw141292 		if (*p == (*state)) {
1451c5c4113dSnw141292 			*p = (*state)->next;
1452c5c4113dSnw141292 			break;
1453c5c4113dSnw141292 		}
1454c5c4113dSnw141292 	}
1455e3c2d6aaSnw141292 
1456e3c2d6aaSnw141292 	idmap_cleanup_batch(*state);
1457e3c2d6aaSnw141292 
1458c5c4113dSnw141292 	(void) pthread_mutex_unlock(&qstatelock);
1459c5c4113dSnw141292 
146084decf41Sjp151216 	(void) pthread_cond_destroy(&(*state)->cv);
146184decf41Sjp151216 
146284decf41Sjp151216 	idmap_release_conn((*state)->qadh);
146384decf41Sjp151216 
1464c5c4113dSnw141292 	free(*state);
1465c5c4113dSnw141292 	*state = NULL;
1466c5c4113dSnw141292 }
1467c5c4113dSnw141292 
1468c5c4113dSnw141292 idmap_retcode
1469c5c4113dSnw141292 idmap_lookup_batch_end(idmap_query_state_t **state,
1470c5c4113dSnw141292 	struct timeval *timeout)
1471c5c4113dSnw141292 {
1472c5c4113dSnw141292 	int		    rc = LDAP_SUCCESS;
1473c5c4113dSnw141292 	idmap_retcode	    retcode = IDMAP_SUCCESS;
1474c5c4113dSnw141292 
1475c5c4113dSnw141292 	(*state)->qdead = 1;
1476c5c4113dSnw141292 
1477c5c4113dSnw141292 	/* Process results until done or until timeout, if given */
1478c5c4113dSnw141292 	while ((*state)->qinflight > 0) {
1479c5c4113dSnw141292 		if ((rc = idmap_get_adobject_batch((*state)->qadh,
1480c5c4113dSnw141292 		    timeout)) != 0)
1481c5c4113dSnw141292 			break;
1482c5c4113dSnw141292 	}
1483c5c4113dSnw141292 
1484c5c4113dSnw141292 	if (rc == LDAP_UNAVAILABLE || rc == LDAP_UNWILLING_TO_PERFORM ||
1485c5c4113dSnw141292 	    rc == LDAP_CONNECT_ERROR || rc == LDAP_SERVER_DOWN ||
1486c5c4113dSnw141292 	    rc == LDAP_BUSY) {
1487c5c4113dSnw141292 		retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
1488c5c4113dSnw141292 		(*state)->qadh->dead = 1;
1489c5c4113dSnw141292 	}
1490c5c4113dSnw141292 
149184decf41Sjp151216 	idmap_lookup_release_batch(state);
1492c5c4113dSnw141292 
1493c5c4113dSnw141292 	return (retcode);
1494c5c4113dSnw141292 }
1495c5c4113dSnw141292 
1496c5c4113dSnw141292 /*
1497c5c4113dSnw141292  * Send one prepared search, queue up msgid, process what results are
1498c5c4113dSnw141292  * available
1499c5c4113dSnw141292  */
1500c5c4113dSnw141292 static
1501c5c4113dSnw141292 idmap_retcode
1502e3c2d6aaSnw141292 idmap_batch_add1(idmap_query_state_t *state,
1503*cd37da74Snw141292 	const char *filter, char *ecanonname, char *edomain,
1504*cd37da74Snw141292 	char **canonname, char **result, char **dname, rid_t *rid,
1505*cd37da74Snw141292 	int *sid_type, idmap_retcode *rc)
1506c5c4113dSnw141292 {
1507c5c4113dSnw141292 	idmap_retcode	retcode = IDMAP_SUCCESS;
1508c5c4113dSnw141292 	int		lrc, qid;
1509c5c4113dSnw141292 	struct timeval	tv;
1510c5c4113dSnw141292 	idmap_q_t	*q;
1511*cd37da74Snw141292 	static char	*attrs[] = {
1512*cd37da74Snw141292 		SAN,
1513*cd37da74Snw141292 		OBJSID,
1514*cd37da74Snw141292 		OBJCLASS,
1515*cd37da74Snw141292 		NULL
1516*cd37da74Snw141292 	};
1517c5c4113dSnw141292 
1518c5c4113dSnw141292 	qid = atomic_inc_32_nv(&state->qlastsent) - 1;
1519c5c4113dSnw141292 
1520c5c4113dSnw141292 	q = &(state->queries[qid]);
1521c5c4113dSnw141292 
1522*cd37da74Snw141292 	/*
1523*cd37da74Snw141292 	 * Remember the expected canonname so we can check the results
1524*cd37da74Snw141292 	 * agains it
1525*cd37da74Snw141292 	 */
1526*cd37da74Snw141292 	q->ecanonname = ecanonname;
1527*cd37da74Snw141292 	q->edomain = edomain;
1528e3c2d6aaSnw141292 
1529c5c4113dSnw141292 	/* Remember where to put the results */
1530*cd37da74Snw141292 	q->canonname = canonname;
1531c5c4113dSnw141292 	q->result = result;
1532c5c4113dSnw141292 	q->domain = dname;
1533c5c4113dSnw141292 	q->rid = rid;
1534c5c4113dSnw141292 	q->sid_type = sid_type;
1535c5c4113dSnw141292 	q->rc = rc;
1536c5c4113dSnw141292 
1537c5c4113dSnw141292 	/*
1538c5c4113dSnw141292 	 * Provide sane defaults for the results in case we never hear
1539c5c4113dSnw141292 	 * back from the DS before closing the connection.
1540e3c2d6aaSnw141292 	 *
1541e3c2d6aaSnw141292 	 * In particular we default the result to indicate a retriable
1542e3c2d6aaSnw141292 	 * error.  The first complete matching result entry will cause
1543e3c2d6aaSnw141292 	 * this to be set to IDMAP_SUCCESS, and the end of the results
1544e3c2d6aaSnw141292 	 * for this search will cause this to indicate "not found" if no
1545e3c2d6aaSnw141292 	 * result entries arrived or no complete ones matched the lookup
1546e3c2d6aaSnw141292 	 * we were doing.
1547c5c4113dSnw141292 	 */
1548c5c4113dSnw141292 	*rc = IDMAP_ERR_RETRIABLE_NET_ERR;
1549c5c4113dSnw141292 	*sid_type = _IDMAP_T_OTHER;
1550c5c4113dSnw141292 	*result = NULL;
1551*cd37da74Snw141292 	if (canonname != NULL)
1552*cd37da74Snw141292 		*canonname = NULL;
1553c5c4113dSnw141292 	if (dname != NULL)
1554c5c4113dSnw141292 		*dname = NULL;
1555c5c4113dSnw141292 	if (rid != NULL)
1556c5c4113dSnw141292 		*rid = 0;
1557c5c4113dSnw141292 
1558c5c4113dSnw141292 	/* Send this lookup, don't wait for a result here */
1559c5c4113dSnw141292 	(void) pthread_mutex_lock(&state->qadh->lock);
1560c5c4113dSnw141292 
1561c5c4113dSnw141292 	if (!state->qadh->dead) {
1562c5c4113dSnw141292 		state->qadh->idletime = time(NULL);
1563e3c2d6aaSnw141292 		lrc = ldap_search_ext(state->qadh->ld, "",
1564*cd37da74Snw141292 		    LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL,
1565c5c4113dSnw141292 		    NULL, -1, &q->msgid);
1566c5c4113dSnw141292 		if (lrc == LDAP_BUSY || lrc == LDAP_UNAVAILABLE ||
1567c5c4113dSnw141292 		    lrc == LDAP_CONNECT_ERROR || lrc == LDAP_SERVER_DOWN ||
1568c5c4113dSnw141292 		    lrc == LDAP_UNWILLING_TO_PERFORM) {
1569c5c4113dSnw141292 			retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
1570c5c4113dSnw141292 			state->qadh->dead = 1;
1571c5c4113dSnw141292 		} else if (lrc != LDAP_SUCCESS) {
1572c5c4113dSnw141292 			retcode = IDMAP_ERR_OTHER;
1573c5c4113dSnw141292 			state->qadh->dead = 1;
1574c5c4113dSnw141292 		}
1575c5c4113dSnw141292 	}
1576c5c4113dSnw141292 	(void) pthread_mutex_unlock(&state->qadh->lock);
1577c5c4113dSnw141292 
1578c5c4113dSnw141292 	if (state->qadh->dead)
1579c5c4113dSnw141292 		return (retcode);
1580c5c4113dSnw141292 
1581c5c4113dSnw141292 	atomic_inc_32(&state->qinflight);
1582c5c4113dSnw141292 
1583c5c4113dSnw141292 	/*
1584c5c4113dSnw141292 	 * Reap as many requests as we can _without_ waiting
1585c5c4113dSnw141292 	 *
1586c5c4113dSnw141292 	 * We do this to prevent any possible TCP socket buffer
1587c5c4113dSnw141292 	 * starvation deadlocks.
1588c5c4113dSnw141292 	 */
1589c5c4113dSnw141292 	(void) memset(&tv, 0, sizeof (tv));
1590c5c4113dSnw141292 	while (idmap_get_adobject_batch(state->qadh, &tv) == 0)
1591c5c4113dSnw141292 		;
1592c5c4113dSnw141292 
1593c5c4113dSnw141292 	return (IDMAP_SUCCESS);
1594c5c4113dSnw141292 }
1595c5c4113dSnw141292 
1596c5c4113dSnw141292 idmap_retcode
1597c5c4113dSnw141292 idmap_name2sid_batch_add1(idmap_query_state_t *state,
1598c5c4113dSnw141292 	const char *name, const char *dname,
1599*cd37da74Snw141292 	char **canonname, char **sid, rid_t *rid, int *sid_type,
1600*cd37da74Snw141292 	idmap_retcode *rc)
1601c5c4113dSnw141292 {
1602c5c4113dSnw141292 	idmap_retcode	retcode;
1603e3c2d6aaSnw141292 	int		len, samAcctNameLen;
1604c5c4113dSnw141292 	char		*filter = NULL;
1605*cd37da74Snw141292 	char		*ecanonname, *edomain; /* expected canonname */
1606c5c4113dSnw141292 
1607c5c4113dSnw141292 	/*
1608e3c2d6aaSnw141292 	 * Strategy: search the global catalog for user/group by
1609e3c2d6aaSnw141292 	 * sAMAccountName = user/groupname with "" as the base DN and by
1610e3c2d6aaSnw141292 	 * userPrincipalName = user/groupname@domain.  The result
1611e3c2d6aaSnw141292 	 * entries will be checked to conform to the name and domain
1612e3c2d6aaSnw141292 	 * name given here.  The DN, sAMAccountName, userPrincipalName,
1613e3c2d6aaSnw141292 	 * objectSid and objectClass of the result entries are all we
1614e3c2d6aaSnw141292 	 * need to figure out which entries match the lookup, the SID of
1615e3c2d6aaSnw141292 	 * the user/group and whether it is a user or a group.
1616c5c4113dSnw141292 	 */
1617c5c4113dSnw141292 
1618c5c4113dSnw141292 	/*
1619e3c2d6aaSnw141292 	 * We need the name and the domain name separately and as
1620e3c2d6aaSnw141292 	 * name@domain.  We also allow the domain to be provided
1621e3c2d6aaSnw141292 	 * separately.
1622c5c4113dSnw141292 	 */
1623d3a612caSnw141292 	samAcctNameLen = strlen(name);
1624e3c2d6aaSnw141292 
1625*cd37da74Snw141292 	if ((ecanonname = strdup(name)) == NULL)
1626e3c2d6aaSnw141292 		return (IDMAP_ERR_MEMORY);
1627*cd37da74Snw141292 
1628*cd37da74Snw141292 	if (dname == NULL || *dname == '\0') {
1629*cd37da74Snw141292 		if ((dname = strchr(name, '@')) != NULL) {
1630*cd37da74Snw141292 			/* 'name' is qualified with a domain name */
1631*cd37da74Snw141292 			if ((edomain = strdup(dname + 1)) == NULL) {
1632*cd37da74Snw141292 				free(ecanonname);
1633*cd37da74Snw141292 				return (IDMAP_ERR_MEMORY);
1634*cd37da74Snw141292 			}
1635*cd37da74Snw141292 			*strchr(ecanonname, '@') = '\0';
1636c5c4113dSnw141292 		} else {
1637e3c2d6aaSnw141292 			/* 'name' not qualified and dname not given */
1638*cd37da74Snw141292 			if (state->qadh->owner->dflt_w2k_dom == NULL ||
1639*cd37da74Snw141292 			    *state->qadh->owner->dflt_w2k_dom == '\0') {
1640*cd37da74Snw141292 				free(ecanonname);
1641e3c2d6aaSnw141292 				return (IDMAP_ERR_DOMAIN);
1642e3c2d6aaSnw141292 			}
1643*cd37da74Snw141292 			edomain = strdup(state->qadh->owner->dflt_w2k_dom);
1644*cd37da74Snw141292 			if (edomain == NULL) {
1645*cd37da74Snw141292 				free(ecanonname);
1646e3c2d6aaSnw141292 				return (IDMAP_ERR_MEMORY);
1647*cd37da74Snw141292 			}
1648*cd37da74Snw141292 		}
1649*cd37da74Snw141292 	} else {
1650*cd37da74Snw141292 		if ((edomain = strdup(dname)) == NULL) {
1651*cd37da74Snw141292 			free(ecanonname);
1652*cd37da74Snw141292 			return (IDMAP_ERR_MEMORY);
1653*cd37da74Snw141292 		}
1654e3c2d6aaSnw141292 	}
1655c5c4113dSnw141292 
1656c5c4113dSnw141292 	/* Assemble filter */
1657e3c2d6aaSnw141292 	len = snprintf(NULL, 0, SANFILTER, samAcctNameLen, name) + 1;
1658e3c2d6aaSnw141292 	if ((filter = (char *)malloc(len)) == NULL) {
1659*cd37da74Snw141292 		free(ecanonname);
1660c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
1661c5c4113dSnw141292 	}
1662e3c2d6aaSnw141292 	(void) snprintf(filter, len, SANFILTER, samAcctNameLen, name);
1663c5c4113dSnw141292 
1664*cd37da74Snw141292 	retcode = idmap_batch_add1(state, filter, ecanonname, edomain,
1665*cd37da74Snw141292 	    canonname, sid, NULL, rid, sid_type, rc);
1666c5c4113dSnw141292 
1667c5c4113dSnw141292 	free(filter);
1668c5c4113dSnw141292 
1669c5c4113dSnw141292 	return (retcode);
1670c5c4113dSnw141292 }
1671c5c4113dSnw141292 
1672c5c4113dSnw141292 idmap_retcode
1673c5c4113dSnw141292 idmap_sid2name_batch_add1(idmap_query_state_t *state,
1674c5c4113dSnw141292 	const char *sid, const rid_t *rid,
1675c5c4113dSnw141292 	char **name, char **dname, int *sid_type, idmap_retcode *rc)
1676c5c4113dSnw141292 {
1677c5c4113dSnw141292 	idmap_retcode	retcode;
1678c5c4113dSnw141292 	int		flen, ret;
1679c5c4113dSnw141292 	char		*filter = NULL;
1680c5c4113dSnw141292 	char		cbinsid[MAXHEXBINSID + 1];
1681c5c4113dSnw141292 
1682c5c4113dSnw141292 	/*
1683c5c4113dSnw141292 	 * Strategy: search [the global catalog] for user/group by
1684c5c4113dSnw141292 	 * objectSid = SID with empty base DN.  The DN, sAMAccountName
1685c5c4113dSnw141292 	 * and objectClass of the result are all we need to figure out
1686c5c4113dSnw141292 	 * the name of the SID and whether it is a user, a group or a
1687c5c4113dSnw141292 	 * computer.
1688c5c4113dSnw141292 	 */
1689c5c4113dSnw141292 
1690c5c4113dSnw141292 	ret = idmap_txtsid2hexbinsid(sid, rid, &cbinsid[0], sizeof (cbinsid));
1691c5c4113dSnw141292 	if (ret != 0)
1692c5c4113dSnw141292 		return (IDMAP_ERR_SID);
1693c5c4113dSnw141292 
1694c5c4113dSnw141292 	/* Assemble filter */
1695e3c2d6aaSnw141292 	flen = snprintf(NULL, 0, OBJSIDFILTER, cbinsid) + 1;
1696c5c4113dSnw141292 	if ((filter = (char *)malloc(flen)) == NULL)
1697c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
1698e3c2d6aaSnw141292 	(void) snprintf(filter, flen, OBJSIDFILTER, cbinsid);
1699c5c4113dSnw141292 
1700*cd37da74Snw141292 	retcode = idmap_batch_add1(state, filter, NULL, NULL, NULL, name, dname,
1701c5c4113dSnw141292 	    NULL, sid_type, rc);
1702c5c4113dSnw141292 
1703c5c4113dSnw141292 	free(filter);
1704c5c4113dSnw141292 
1705c5c4113dSnw141292 	return (retcode);
1706c5c4113dSnw141292 }
1707