xref: /titanic_50/usr/src/cmd/idmap/idmapd/adutils.c (revision 84decf41e1c0970e397cc8710dfcf81db5b8c6da)
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>
50c5c4113dSnw141292 #include "idmapd.h"
51c5c4113dSnw141292 
52c5c4113dSnw141292 /*
53c5c4113dSnw141292  * Internal data structures for this code
54c5c4113dSnw141292  */
55c5c4113dSnw141292 
56c5c4113dSnw141292 /* Attribute names and filter format strings */
57c5c4113dSnw141292 #define	OBJECTSID	"objectSid"
58c5c4113dSnw141292 #define	OBJECTSIDFILTER	"(objectSid=%s)"
59c5c4113dSnw141292 #define	SAMACCOUNTNAME	"sAMAccountName"
60c5c4113dSnw141292 #define	SANFILTER	"(sAMAccountName=%.*s)"
61c5c4113dSnw141292 #define	OBJECTCLASS	"objectClass"
62c5c4113dSnw141292 
63c5c4113dSnw141292 /*
64c5c4113dSnw141292  * This should really be in some <sys/sid.h> file or so; we have a
65c5c4113dSnw141292  * private version of sid_t, and so must other components of ON until we
66c5c4113dSnw141292  * rationalize this.
67c5c4113dSnw141292  */
68c5c4113dSnw141292 typedef struct sid {
69c5c4113dSnw141292 	uchar_t		version;
70c5c4113dSnw141292 	uchar_t		sub_authority_count;
71c5c4113dSnw141292 	uint64_t	authority;  /* really, 48-bits */
72c5c4113dSnw141292 	rid_t		sub_authorities[SID_MAX_SUB_AUTHORITIES];
73c5c4113dSnw141292 } sid_t;
74c5c4113dSnw141292 
75c5c4113dSnw141292 /* A single DS */
76c5c4113dSnw141292 typedef struct ad_host {
77c5c4113dSnw141292 	struct ad_host		*next;
78c5c4113dSnw141292 	ad_t			*owner;		/* ad_t to which this belongs */
79c5c4113dSnw141292 	pthread_mutex_t		lock;
80c5c4113dSnw141292 	LDAP			*ld;		/* LDAP connection */
81c5c4113dSnw141292 	uint32_t		ref;		/* ref count */
82c5c4113dSnw141292 	time_t			idletime;	/* time since last activity */
83c5c4113dSnw141292 	int			dead;		/* error on LDAP connection */
84c5c4113dSnw141292 	/*
85c5c4113dSnw141292 	 * Used to distinguish between different instances of LDAP
86c5c4113dSnw141292 	 * connections to this same DS.  We need this so we never mix up
87c5c4113dSnw141292 	 * results for a given msgID from one connection with those of
88c5c4113dSnw141292 	 * another earlier connection where two batch state structures
89c5c4113dSnw141292 	 * share this ad_host object but used different LDAP connections
90c5c4113dSnw141292 	 * to send their LDAP searches.
91c5c4113dSnw141292 	 */
92c5c4113dSnw141292 	uint64_t		generation;
93c5c4113dSnw141292 
94c5c4113dSnw141292 	/* LDAP DS info */
95c5c4113dSnw141292 	char			*host;
96c5c4113dSnw141292 	int			port;
97c5c4113dSnw141292 
98c5c4113dSnw141292 	/* hardwired to SASL GSSAPI only for now */
99c5c4113dSnw141292 	char			*saslmech;
100c5c4113dSnw141292 	unsigned		saslflags;
101c5c4113dSnw141292 } ad_host_t;
102c5c4113dSnw141292 
103c5c4113dSnw141292 /* A set of DSs for a given AD partition; ad_t typedef comes from  adutil.h */
104c5c4113dSnw141292 struct ad {
105c5c4113dSnw141292 	char			*dflt_w2k_dom;	/* used to qualify bare names */
106c5c4113dSnw141292 	char			*basedn;	/* derived from dflt domain */
107c5c4113dSnw141292 	pthread_mutex_t		lock;
108c5c4113dSnw141292 	uint32_t		ref;
109c5c4113dSnw141292 	idmap_ad_partition_t	partition;	/* Data or global catalog? */
110c5c4113dSnw141292 };
111c5c4113dSnw141292 
112c5c4113dSnw141292 /*
113c5c4113dSnw141292  * A place to put the results of a batched (async) query
114c5c4113dSnw141292  *
115c5c4113dSnw141292  * There is one of these for every query added to a batch object
116c5c4113dSnw141292  * (idmap_query_state, see below).
117c5c4113dSnw141292  */
118c5c4113dSnw141292 typedef struct idmap_q {
119c5c4113dSnw141292 	char			**result;	/* name or stringified SID */
120c5c4113dSnw141292 	char			**domain;	/* name of domain of object */
121c5c4113dSnw141292 	rid_t			*rid;		/* for n2s, if not NULL */
122c5c4113dSnw141292 	int			*sid_type;	/* if not NULL */
123c5c4113dSnw141292 	idmap_retcode		*rc;
124c5c4113dSnw141292 	int			msgid;		/* LDAP message ID */
125c5c4113dSnw141292 	/*
126c5c4113dSnw141292 	 * Bitfield containing state needed to know when we're done
127c5c4113dSnw141292 	 * processing search results related to this query's LDAP
128c5c4113dSnw141292 	 * searches.  Mostly self-explanatory.
129c5c4113dSnw141292 	 */
130c5c4113dSnw141292 	uint16_t		n2s : 1;	/* name->SID or SID->name? */
131c5c4113dSnw141292 	uint16_t		got_reply : 1;
132c5c4113dSnw141292 	uint16_t		got_results : 1;
133c5c4113dSnw141292 	uint16_t		got_objectSid : 1;
134c5c4113dSnw141292 	uint16_t		got_objectClass : 1;
135c5c4113dSnw141292 	uint16_t		got_samAcctName : 1;
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 */
142*84decf41Sjp151216 	int			ref_cnt;	/* reference count */
143*84decf41Sjp151216 	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 
167*84decf41Sjp151216 
168*84decf41Sjp151216 static void
169*84decf41Sjp151216 idmap_lookup_unlock_batch(idmap_query_state_t **state);
170*84decf41Sjp151216 
171*84decf41Sjp151216 
172*84decf41Sjp151216 
173c5c4113dSnw141292 /*ARGSUSED*/
174c5c4113dSnw141292 static int
175c5c4113dSnw141292 idmap_saslcallback(LDAP *ld, unsigned flags, void *defaults, void *prompts) {
176c5c4113dSnw141292 	sasl_interact_t	*interact;
177c5c4113dSnw141292 
178c5c4113dSnw141292 	if (prompts == NULL || flags != LDAP_SASL_INTERACTIVE)
179c5c4113dSnw141292 		return (LDAP_PARAM_ERROR);
180c5c4113dSnw141292 
181c5c4113dSnw141292 	/* There should be no extra arguemnts for SASL/GSSAPI authentication */
182c5c4113dSnw141292 	for (interact = prompts; interact->id != SASL_CB_LIST_END;
183c5c4113dSnw141292 			interact++) {
184c5c4113dSnw141292 		interact->result = NULL;
185c5c4113dSnw141292 		interact->len = 0;
186c5c4113dSnw141292 	}
187c5c4113dSnw141292 	return (LDAP_SUCCESS);
188c5c4113dSnw141292 }
189c5c4113dSnw141292 
190c5c4113dSnw141292 /* Turn "foo.bar.com" into "dc=foo,dc=bar,dc=com" */
191c5c4113dSnw141292 static
192c5c4113dSnw141292 char *
193c5c4113dSnw141292 dns2dn(const char *dns)
194c5c4113dSnw141292 {
195c5c4113dSnw141292 	int nameparts;
196c5c4113dSnw141292 
197c5c4113dSnw141292 	/* Sigh, ldap_dns_to_dn()'s first arg should be a const char * */
198c5c4113dSnw141292 	return (ldap_dns_to_dn((char *)dns, &nameparts));
199c5c4113dSnw141292 }
200c5c4113dSnw141292 
201c5c4113dSnw141292 /*
202c5c4113dSnw141292  * Turn "dc=foo,dc=bar,dc=com" into "foo.bar.com"; ignores any other
203c5c4113dSnw141292  * attributes (CN, etc...)
204c5c4113dSnw141292  */
205c5c4113dSnw141292 static
206c5c4113dSnw141292 char *
207c5c4113dSnw141292 dn2dns(const char *dn)
208c5c4113dSnw141292 {
209c5c4113dSnw141292 	char **rdns = NULL;
210c5c4113dSnw141292 	char **attrs = NULL;
211c5c4113dSnw141292 	char **labels = NULL;
212c5c4113dSnw141292 	char *dns = NULL;
213c5c4113dSnw141292 	char **rdn, **attr, **label;
214c5c4113dSnw141292 	int maxlabels = 5;
215c5c4113dSnw141292 	int nlabels = 0;
216c5c4113dSnw141292 	int dnslen;
217c5c4113dSnw141292 
218c5c4113dSnw141292 	/*
219c5c4113dSnw141292 	 * There is no reverse of ldap_dns_to_dn() in our libldap, so we
220c5c4113dSnw141292 	 * have to do the hard work here for now.
221c5c4113dSnw141292 	 */
222c5c4113dSnw141292 
223c5c4113dSnw141292 	/*
224c5c4113dSnw141292 	 * This code is much too liberal: it looks for "dc" attributes
225c5c4113dSnw141292 	 * in all RDNs of the DN.  In theory this could cause problems
226c5c4113dSnw141292 	 * if people were to use "dc" in nodes other than the root of
227c5c4113dSnw141292 	 * the tree, but in practice noone, least of all Active
228c5c4113dSnw141292 	 * Directory, does that.
229c5c4113dSnw141292 	 *
230c5c4113dSnw141292 	 * On the other hand, this code is much too conservative: it
231c5c4113dSnw141292 	 * does not make assumptions about ldap_explode_dn(), and _that_
232c5c4113dSnw141292 	 * is the true for looking at every attr of every RDN.
233c5c4113dSnw141292 	 *
234c5c4113dSnw141292 	 * Since we only ever look at dc and those must be DNS labels,
235c5c4113dSnw141292 	 * at least until we get around to supporting IDN here we
236c5c4113dSnw141292 	 * shouldn't see escaped labels from AD nor from libldap, though
237c5c4113dSnw141292 	 * the spec (RFC2253) does allow libldap to escape things that
238c5c4113dSnw141292 	 * don't need escaping -- if that should ever happen then
239c5c4113dSnw141292 	 * libldap will need a spanking, and we can take care of that.
240c5c4113dSnw141292 	 */
241c5c4113dSnw141292 
242c5c4113dSnw141292 	/* Explode a DN into RDNs */
243c5c4113dSnw141292 	if ((rdns = ldap_explode_dn(dn, 0)) == NULL)
244c5c4113dSnw141292 		return (NULL);
245c5c4113dSnw141292 
246c5c4113dSnw141292 	labels = calloc(maxlabels + 1, sizeof (char *));
247c5c4113dSnw141292 	label = labels;
248c5c4113dSnw141292 
249c5c4113dSnw141292 	for (rdn = rdns; *rdn != NULL; rdn++) {
250c5c4113dSnw141292 		if (attrs != NULL)
251c5c4113dSnw141292 			ldap_value_free(attrs);
252c5c4113dSnw141292 
253c5c4113dSnw141292 		/* Explode each RDN, look for DC attr, save val as DNS label */
254c5c4113dSnw141292 		if ((attrs = ldap_explode_rdn(rdn[0], 0)) == NULL)
255c5c4113dSnw141292 			goto done;
256c5c4113dSnw141292 
257c5c4113dSnw141292 		for (attr = attrs; *attr != NULL; attr++) {
258c5c4113dSnw141292 			if (strncasecmp(*attr, "dc=", 3) != 0)
259c5c4113dSnw141292 				continue;
260c5c4113dSnw141292 
261c5c4113dSnw141292 			/* Found a DNS label */
262c5c4113dSnw141292 			labels[nlabels++] = strdup((*attr) + 3);
263c5c4113dSnw141292 
264c5c4113dSnw141292 			if (nlabels == maxlabels) {
265c5c4113dSnw141292 				char **tmp;
266c5c4113dSnw141292 				tmp = realloc(labels,
267c5c4113dSnw141292 				    sizeof (char *) * (maxlabels + 1));
268c5c4113dSnw141292 
269c5c4113dSnw141292 				if (tmp == NULL)
270c5c4113dSnw141292 					goto done;
271c5c4113dSnw141292 
272c5c4113dSnw141292 				labels = tmp;
273c5c4113dSnw141292 				labels[nlabels] = NULL;
274c5c4113dSnw141292 			}
275c5c4113dSnw141292 
276c5c4113dSnw141292 			/* There should be just one DC= attr per-RDN */
277c5c4113dSnw141292 			break;
278c5c4113dSnw141292 		}
279c5c4113dSnw141292 	}
280c5c4113dSnw141292 
281c5c4113dSnw141292 	/*
282c5c4113dSnw141292 	 * Got all the labels, now join with '.'
283c5c4113dSnw141292 	 *
284c5c4113dSnw141292 	 * We need room for nlabels - 1 periods ('.'), one nul
285c5c4113dSnw141292 	 * terminator, and the strlen() of each label.
286c5c4113dSnw141292 	 */
287c5c4113dSnw141292 	dnslen = nlabels;
288c5c4113dSnw141292 	for (label = labels; *label != NULL; label++)
289c5c4113dSnw141292 		dnslen += strlen(*label);
290c5c4113dSnw141292 
291c5c4113dSnw141292 	if ((dns = malloc(dnslen)) == NULL)
292c5c4113dSnw141292 		goto done;
293c5c4113dSnw141292 
294c5c4113dSnw141292 	*dns = '\0';
295c5c4113dSnw141292 
296c5c4113dSnw141292 	for (label = labels; *label != NULL; label++) {
297c5c4113dSnw141292 		(void) strlcat(dns, *label, dnslen);
298c5c4113dSnw141292 		/*
299c5c4113dSnw141292 		 * NOTE: the last '.' won't be appended -- there's no room
300c5c4113dSnw141292 		 * for it!
301c5c4113dSnw141292 		 */
302c5c4113dSnw141292 		(void) strlcat(dns, ".", dnslen);
303c5c4113dSnw141292 	}
304c5c4113dSnw141292 
305c5c4113dSnw141292 done:
306c5c4113dSnw141292 	if (labels != NULL) {
307c5c4113dSnw141292 		for (label = labels; *label != NULL; label++)
308c5c4113dSnw141292 			free(*label);
309c5c4113dSnw141292 		free(labels);
310c5c4113dSnw141292 	}
311c5c4113dSnw141292 	if (attrs != NULL)
312c5c4113dSnw141292 		ldap_value_free(attrs);
313c5c4113dSnw141292 	if (rdns != NULL)
314c5c4113dSnw141292 		ldap_value_free(rdns);
315c5c4113dSnw141292 
316c5c4113dSnw141292 	return (dns);
317c5c4113dSnw141292 }
318c5c4113dSnw141292 
319c5c4113dSnw141292 /*
320c5c4113dSnw141292  * Keep connection management simple for now, extend or replace later
321c5c4113dSnw141292  * with updated libsldap code.
322c5c4113dSnw141292  */
323c5c4113dSnw141292 #define	ADREAPERSLEEP	60
324c5c4113dSnw141292 #define	ADCONN_TIME	300
325c5c4113dSnw141292 
326c5c4113dSnw141292 /*
327c5c4113dSnw141292  * Idle connection reaping side of connection management
328c5c4113dSnw141292  *
329c5c4113dSnw141292  * Every minute wake up and look for connections that have been idle for
330c5c4113dSnw141292  * five minutes or more and close them.
331c5c4113dSnw141292  */
332c5c4113dSnw141292 /*ARGSUSED*/
333c5c4113dSnw141292 static
334c5c4113dSnw141292 void
335c5c4113dSnw141292 adreaper(void *arg)
336c5c4113dSnw141292 {
337c5c4113dSnw141292 	ad_host_t	*adh;
338c5c4113dSnw141292 	time_t		now;
339c5c4113dSnw141292 	timespec_t	ts;
340c5c4113dSnw141292 
341c5c4113dSnw141292 	ts.tv_sec = ADREAPERSLEEP;
342c5c4113dSnw141292 	ts.tv_nsec = 0;
343c5c4113dSnw141292 
344c5c4113dSnw141292 	for (;;) {
345c5c4113dSnw141292 		/*
346c5c4113dSnw141292 		 * nanosleep(3RT) is thead-safe (no SIGALRM) and more
347c5c4113dSnw141292 		 * portable than usleep(3C)
348c5c4113dSnw141292 		 */
349c5c4113dSnw141292 		(void) nanosleep(&ts, NULL);
350c5c4113dSnw141292 		(void) pthread_mutex_lock(&adhostlock);
351c5c4113dSnw141292 		now = time(NULL);
352c5c4113dSnw141292 		for (adh = host_head; adh != NULL; adh = adh->next) {
353c5c4113dSnw141292 			(void) pthread_mutex_lock(&adh->lock);
354c5c4113dSnw141292 			if (adh->ref == 0 && adh->idletime != 0 &&
355c5c4113dSnw141292 			    adh->idletime + ADCONN_TIME < now) {
356c5c4113dSnw141292 				if (adh->ld) {
357c5c4113dSnw141292 					(void) ldap_unbind(adh->ld);
358c5c4113dSnw141292 					adh->ld = NULL;
359c5c4113dSnw141292 					adh->idletime = 0;
360c5c4113dSnw141292 					adh->ref = 0;
361c5c4113dSnw141292 				}
362c5c4113dSnw141292 			}
363c5c4113dSnw141292 			(void) pthread_mutex_unlock(&adh->lock);
364c5c4113dSnw141292 		}
365c5c4113dSnw141292 		(void) pthread_mutex_unlock(&adhostlock);
366c5c4113dSnw141292 	}
367c5c4113dSnw141292 }
368c5c4113dSnw141292 
369c5c4113dSnw141292 int
370c5c4113dSnw141292 idmap_ad_alloc(ad_t **new_ad, const char *default_domain,
371c5c4113dSnw141292 		idmap_ad_partition_t part)
372c5c4113dSnw141292 {
373c5c4113dSnw141292 	ad_t *ad;
374c5c4113dSnw141292 
375c5c4113dSnw141292 	*new_ad = NULL;
376c5c4113dSnw141292 
377c5c4113dSnw141292 	if ((default_domain == NULL || *default_domain == '\0') &&
378c5c4113dSnw141292 	    part != IDMAP_AD_GLOBAL_CATALOG)
379c5c4113dSnw141292 		return (-1);
380c5c4113dSnw141292 
381c5c4113dSnw141292 	if ((ad = calloc(1, sizeof (ad_t))) == NULL)
382c5c4113dSnw141292 		return (-1);
383c5c4113dSnw141292 
384c5c4113dSnw141292 	ad->ref = 1;
385c5c4113dSnw141292 	ad->partition = part;
386c5c4113dSnw141292 
387c5c4113dSnw141292 	/*
388c5c4113dSnw141292 	 * If default_domain is NULL, deal, deferring errors until
389c5c4113dSnw141292 	 * idmap_lookup_batch_start() -- this makes it easier on the
390c5c4113dSnw141292 	 * caller, who can simply observe lookups failing as opposed to
391c5c4113dSnw141292 	 * having to conditionalize calls to lookups according to
392c5c4113dSnw141292 	 * whether it has a non-NULL ad_t *.
393c5c4113dSnw141292 	 */
394c5c4113dSnw141292 	if (default_domain == NULL)
395c5c4113dSnw141292 		default_domain = "";
396c5c4113dSnw141292 
397c5c4113dSnw141292 	if ((ad->dflt_w2k_dom = strdup(default_domain)) == NULL)
398c5c4113dSnw141292 		goto err;
399c5c4113dSnw141292 
400c5c4113dSnw141292 	/* If default_domain is empty, deal; see above */
401c5c4113dSnw141292 	if (*default_domain == '\0') {
402c5c4113dSnw141292 		if ((ad->basedn = strdup("")) == NULL)
403c5c4113dSnw141292 			goto err;
404c5c4113dSnw141292 	} else if ((ad->basedn = dns2dn(default_domain)) == NULL) {
405c5c4113dSnw141292 		goto err;
406c5c4113dSnw141292 	}
407c5c4113dSnw141292 
408c5c4113dSnw141292 	if (pthread_mutex_init(&ad->lock, NULL) != 0)
409c5c4113dSnw141292 		goto err;
410c5c4113dSnw141292 
411c5c4113dSnw141292 	*new_ad = ad;
412c5c4113dSnw141292 
413c5c4113dSnw141292 	return (0);
414c5c4113dSnw141292 err:
415c5c4113dSnw141292 	if (ad->dflt_w2k_dom != NULL)
416c5c4113dSnw141292 		free(ad->dflt_w2k_dom);
417c5c4113dSnw141292 	if (ad->basedn != NULL)
418c5c4113dSnw141292 		free(ad->basedn);
419c5c4113dSnw141292 	free(ad);
420c5c4113dSnw141292 	return (-1);
421c5c4113dSnw141292 }
422c5c4113dSnw141292 
423c5c4113dSnw141292 
424c5c4113dSnw141292 void
425c5c4113dSnw141292 idmap_ad_free(ad_t **ad)
426c5c4113dSnw141292 {
427c5c4113dSnw141292 	ad_host_t *p;
428c5c4113dSnw141292 
429c5c4113dSnw141292 	if (ad == NULL || *ad == NULL)
430c5c4113dSnw141292 		return;
431c5c4113dSnw141292 
432c5c4113dSnw141292 	(void) pthread_mutex_lock(&(*ad)->lock);
433c5c4113dSnw141292 
434c5c4113dSnw141292 	if (atomic_dec_32_nv(&(*ad)->ref) > 0) {
435c5c4113dSnw141292 		(void) pthread_mutex_unlock(&(*ad)->lock);
436c5c4113dSnw141292 		*ad = NULL;
437c5c4113dSnw141292 		return;
438c5c4113dSnw141292 	}
439c5c4113dSnw141292 
440c5c4113dSnw141292 	for (p = host_head; p != NULL; p = p->next) {
441c5c4113dSnw141292 		if (p->owner != (*ad))
442c5c4113dSnw141292 			continue;
443c5c4113dSnw141292 		idmap_delete_ds((*ad), p->host, p->port);
444c5c4113dSnw141292 	}
445c5c4113dSnw141292 
446c5c4113dSnw141292 	free((*ad)->basedn);
447c5c4113dSnw141292 
448c5c4113dSnw141292 	(void) pthread_mutex_unlock(&(*ad)->lock);
449c5c4113dSnw141292 	(void) pthread_mutex_destroy(&(*ad)->lock);
450c5c4113dSnw141292 
451c5c4113dSnw141292 	free(*ad);
452c5c4113dSnw141292 
453c5c4113dSnw141292 	*ad = NULL;
454c5c4113dSnw141292 }
455c5c4113dSnw141292 
456c5c4113dSnw141292 static
457c5c4113dSnw141292 int
458c5c4113dSnw141292 idmap_open_conn(ad_host_t *adh)
459c5c4113dSnw141292 {
460c5c4113dSnw141292 	int	rc, ldversion;
461c5c4113dSnw141292 
462c5c4113dSnw141292 	if (adh->dead && adh->ld != NULL) {
463c5c4113dSnw141292 		(void) ldap_unbind(adh->ld);
464c5c4113dSnw141292 		adh->ld = NULL;
465c5c4113dSnw141292 		adh->dead = 0;
466c5c4113dSnw141292 	}
467c5c4113dSnw141292 
468c5c4113dSnw141292 	if (adh->ld == NULL) {
469c5c4113dSnw141292 		int zero = 0;
470c5c4113dSnw141292 		int timeoutms = 30 * 1000;
471c5c4113dSnw141292 
472c5c4113dSnw141292 		atomic_inc_64(&adh->generation);
473c5c4113dSnw141292 		adh->ld = ldap_init(adh->host, adh->port);
474c5c4113dSnw141292 		if (adh->ld == NULL)
475c5c4113dSnw141292 			return (-1);
476c5c4113dSnw141292 
477c5c4113dSnw141292 		ldversion = LDAP_VERSION3;
478c5c4113dSnw141292 		(void) ldap_set_option(adh->ld, LDAP_OPT_PROTOCOL_VERSION,
479c5c4113dSnw141292 		    &ldversion);
480c5c4113dSnw141292 
481c5c4113dSnw141292 		(void) ldap_set_option(adh->ld, LDAP_OPT_REFERRALS,
482c5c4113dSnw141292 		    LDAP_OPT_OFF);
483c5c4113dSnw141292 		(void) ldap_set_option(adh->ld, LDAP_OPT_TIMELIMIT, &zero);
484c5c4113dSnw141292 		(void) ldap_set_option(adh->ld, LDAP_OPT_SIZELIMIT, &zero);
485c5c4113dSnw141292 		/* setup TCP/IP connect timeout */
486c5c4113dSnw141292 		(void) ldap_set_option(adh->ld, LDAP_X_OPT_CONNECT_TIMEOUT,
487c5c4113dSnw141292 		    &timeoutms);
488c5c4113dSnw141292 		(void) ldap_set_option(adh->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
489c5c4113dSnw141292 		rc = ldap_sasl_interactive_bind_s(adh->ld,
490c5c4113dSnw141292 		    "" /* binddn */, adh->saslmech, NULL, NULL, adh->saslflags,
491c5c4113dSnw141292 		    &idmap_saslcallback, NULL /* defaults */);
492c5c4113dSnw141292 
493c5c4113dSnw141292 		if (rc != LDAP_SUCCESS) {
4948edda628Sbaban 			idmapdlog(LOG_ERR, "ldap_sasl_interactive_bind_s() "
4958edda628Sbaban 			    "to server %s:%d failed. (%s)",
4968edda628Sbaban 			    adh->host, adh->port, ldap_err2string(rc));
497c5c4113dSnw141292 			return (rc);
498c5c4113dSnw141292 		}
499c5c4113dSnw141292 	}
500c5c4113dSnw141292 
501c5c4113dSnw141292 	adh->idletime = time(NULL);
502c5c4113dSnw141292 
503c5c4113dSnw141292 	return (LDAP_SUCCESS);
504c5c4113dSnw141292 }
505c5c4113dSnw141292 
506c5c4113dSnw141292 
507c5c4113dSnw141292 /*
508c5c4113dSnw141292  * Connection management: find an open connection or open one
509c5c4113dSnw141292  */
510c5c4113dSnw141292 static
511c5c4113dSnw141292 ad_host_t *
512c5c4113dSnw141292 idmap_get_conn(const ad_t *ad)
513c5c4113dSnw141292 {
514c5c4113dSnw141292 	ad_host_t	*adh = NULL;
515c5c4113dSnw141292 	int		rc;
516c5c4113dSnw141292 
517c5c4113dSnw141292 	(void) pthread_mutex_lock(&adhostlock);
518c5c4113dSnw141292 
519c5c4113dSnw141292 	/*
520c5c4113dSnw141292 	 * Search for any ad_host_t, preferably one with an open
521c5c4113dSnw141292 	 * connection
522c5c4113dSnw141292 	 */
523c5c4113dSnw141292 	for (adh = host_head; adh != NULL; adh = adh->next) {
524c5c4113dSnw141292 		if (adh->owner == ad) {
525c5c4113dSnw141292 			break;
526c5c4113dSnw141292 		}
527c5c4113dSnw141292 	}
528c5c4113dSnw141292 
529c5c4113dSnw141292 	if (adh != NULL)
530c5c4113dSnw141292 		atomic_inc_32(&adh->ref);
531c5c4113dSnw141292 
532c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adhostlock);
533c5c4113dSnw141292 
534c5c4113dSnw141292 	if (adh == NULL)
535c5c4113dSnw141292 		return (NULL);
536c5c4113dSnw141292 
537c5c4113dSnw141292 	/* found connection, open it if not opened */
538c5c4113dSnw141292 	(void) pthread_mutex_lock(&adh->lock);
539c5c4113dSnw141292 	rc = idmap_open_conn(adh);
540c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adh->lock);
541c5c4113dSnw141292 	if (rc != LDAP_SUCCESS)
542c5c4113dSnw141292 		return (NULL);
543c5c4113dSnw141292 
544c5c4113dSnw141292 	return (adh);
545c5c4113dSnw141292 }
546c5c4113dSnw141292 
547c5c4113dSnw141292 static
548c5c4113dSnw141292 void
549c5c4113dSnw141292 idmap_release_conn(ad_host_t *adh)
550c5c4113dSnw141292 {
551c5c4113dSnw141292 	(void) pthread_mutex_lock(&adh->lock);
552c5c4113dSnw141292 	if (atomic_dec_32_nv(&adh->ref) == 0)
553c5c4113dSnw141292 		adh->idletime = time(NULL);
554c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adh->lock);
555c5c4113dSnw141292 }
556c5c4113dSnw141292 
557c5c4113dSnw141292 /*
558c5c4113dSnw141292  * Take ad_host_config_t information, create a ad_host_t,
559c5c4113dSnw141292  * populate it and add it to the list of hosts.
560c5c4113dSnw141292  */
561c5c4113dSnw141292 
562c5c4113dSnw141292 int
563c5c4113dSnw141292 idmap_add_ds(ad_t *ad, const char *host, int port)
564c5c4113dSnw141292 {
565c5c4113dSnw141292 	ad_host_t	*p;
566c5c4113dSnw141292 	ad_host_t	*new = NULL;
567c5c4113dSnw141292 	int		ret = -1;
568c5c4113dSnw141292 
569c5c4113dSnw141292 	if (port == 0)
570c5c4113dSnw141292 		port = (int)ad->partition;
571c5c4113dSnw141292 
572c5c4113dSnw141292 	(void) pthread_mutex_lock(&adhostlock);
573c5c4113dSnw141292 	for (p = host_head; p != NULL; p = p->next) {
574c5c4113dSnw141292 		if (p->owner != ad)
575c5c4113dSnw141292 			continue;
576c5c4113dSnw141292 
577c5c4113dSnw141292 		if (strcmp(host, p->host) == 0 && p->port == port) {
578c5c4113dSnw141292 			/* already added */
579c5c4113dSnw141292 			ret = -2;
580c5c4113dSnw141292 			goto err;
581c5c4113dSnw141292 		}
582c5c4113dSnw141292 	}
583c5c4113dSnw141292 
584c5c4113dSnw141292 	/* add new entry */
585c5c4113dSnw141292 	new = (ad_host_t *)calloc(1, sizeof (ad_host_t));
586c5c4113dSnw141292 	if (new == NULL)
587c5c4113dSnw141292 		goto err;
588c5c4113dSnw141292 	new->owner = ad;
589c5c4113dSnw141292 	new->port = port;
590c5c4113dSnw141292 	new->dead = 0;
591c5c4113dSnw141292 	if ((new->host = strdup(host)) == NULL)
592c5c4113dSnw141292 		goto err;
593c5c4113dSnw141292 
594c5c4113dSnw141292 	/* default to SASL GSSAPI only for now */
595c5c4113dSnw141292 	new->saslflags = LDAP_SASL_INTERACTIVE;
596c5c4113dSnw141292 	new->saslmech = "GSSAPI";
597c5c4113dSnw141292 
598c5c4113dSnw141292 	if ((ret = pthread_mutex_init(&new->lock, NULL)) != 0) {
599c5c4113dSnw141292 		free(new->host);
600c5c4113dSnw141292 		new->host = NULL;
601c5c4113dSnw141292 		errno = ret;
602c5c4113dSnw141292 		ret = -1;
603c5c4113dSnw141292 		goto err;
604c5c4113dSnw141292 	}
605c5c4113dSnw141292 
606c5c4113dSnw141292 	/* link in */
607c5c4113dSnw141292 	new->next = host_head;
608c5c4113dSnw141292 	host_head = new;
609c5c4113dSnw141292 
610c5c4113dSnw141292 	/* Start reaper if it doesn't exist */
611c5c4113dSnw141292 	if (reaperid == 0)
612c5c4113dSnw141292 		(void) pthread_create(&reaperid, NULL,
613c5c4113dSnw141292 		    (void *(*)(void *))adreaper, (void *)NULL);
614c5c4113dSnw141292 
615c5c4113dSnw141292 err:
616c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adhostlock);
617c5c4113dSnw141292 
618c5c4113dSnw141292 	if (ret != 0 && new != NULL) {
619c5c4113dSnw141292 		if (new->host != NULL) {
620c5c4113dSnw141292 			(void) pthread_mutex_destroy(&new->lock);
621c5c4113dSnw141292 			free(new->host);
622c5c4113dSnw141292 		}
623c5c4113dSnw141292 		free(new);
624c5c4113dSnw141292 	}
625c5c4113dSnw141292 
626c5c4113dSnw141292 	return (ret);
627c5c4113dSnw141292 }
628c5c4113dSnw141292 
629c5c4113dSnw141292 /*
630c5c4113dSnw141292  * free a DS configuration
631c5c4113dSnw141292  */
632c5c4113dSnw141292 void
633c5c4113dSnw141292 idmap_delete_ds(ad_t *ad, const char *host, int port)
634c5c4113dSnw141292 {
635c5c4113dSnw141292 	ad_host_t	**p, *q;
636c5c4113dSnw141292 
637c5c4113dSnw141292 	(void) pthread_mutex_lock(&adhostlock);
638c5c4113dSnw141292 	for (p = &host_head; *p != NULL; p = &((*p)->next)) {
639c5c4113dSnw141292 		if ((*p)->owner != ad || strcmp(host, (*p)->host) != 0 ||
640c5c4113dSnw141292 		    (*p)->port != port)
641c5c4113dSnw141292 			continue;
642c5c4113dSnw141292 		/* found */
643c5c4113dSnw141292 		if (atomic_dec_32_nv(&((*p)->ref)) > 0)
644c5c4113dSnw141292 			break;	/* still in use */
645c5c4113dSnw141292 
646c5c4113dSnw141292 		q = *p;
647c5c4113dSnw141292 		*p = (*p)->next;
648c5c4113dSnw141292 
649c5c4113dSnw141292 		(void) pthread_mutex_destroy(&q->lock);
650c5c4113dSnw141292 
651c5c4113dSnw141292 		if (q->ld)
652c5c4113dSnw141292 			(void) ldap_unbind(q->ld);
653c5c4113dSnw141292 		if (q->host)
654c5c4113dSnw141292 			free(q->host);
655c5c4113dSnw141292 		free(q);
656c5c4113dSnw141292 		break;
657c5c4113dSnw141292 	}
658c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adhostlock);
659c5c4113dSnw141292 }
660c5c4113dSnw141292 
661c5c4113dSnw141292 /*
662c5c4113dSnw141292  * Convert a binary SID in a BerValue to a sid_t
663c5c4113dSnw141292  */
664c5c4113dSnw141292 static
665c5c4113dSnw141292 int
666c5c4113dSnw141292 idmap_getsid(BerValue *bval, sid_t *sidp)
667c5c4113dSnw141292 {
668c5c4113dSnw141292 	int		i, j;
669c5c4113dSnw141292 	uchar_t		*v;
670c5c4113dSnw141292 	uint32_t	a;
671c5c4113dSnw141292 
672c5c4113dSnw141292 	/*
673c5c4113dSnw141292 	 * The binary format of a SID is as follows:
674c5c4113dSnw141292 	 *
675c5c4113dSnw141292 	 * byte #0: version, always 0x01
676c5c4113dSnw141292 	 * byte #1: RID count, always <= 0x0f
677c5c4113dSnw141292 	 * bytes #2-#7: SID authority, big-endian 48-bit unsigned int
678c5c4113dSnw141292 	 *
679c5c4113dSnw141292 	 * followed by RID count RIDs, each a little-endian, unsigned
680c5c4113dSnw141292 	 * 32-bit int.
681c5c4113dSnw141292 	 */
682c5c4113dSnw141292 	/*
683c5c4113dSnw141292 	 * Sanity checks: must have at least one RID, version must be
684c5c4113dSnw141292 	 * 0x01, and the length must be 8 + rid count * 4
685c5c4113dSnw141292 	 */
686c5c4113dSnw141292 	if (bval->bv_len > 8 && bval->bv_val[0] == 0x01 &&
687c5c4113dSnw141292 	    bval->bv_len == 1 + 1 + 6 + bval->bv_val[1] * 4) {
688c5c4113dSnw141292 		v = (uchar_t *)bval->bv_val;
689c5c4113dSnw141292 		sidp->version = v[0];
690c5c4113dSnw141292 		sidp->sub_authority_count = v[1];
691c5c4113dSnw141292 		sidp->authority =
692c5c4113dSnw141292 		    /* big endian -- so start from the left */
693c5c4113dSnw141292 		    ((u_longlong_t)v[2] << 40) |
694c5c4113dSnw141292 		    ((u_longlong_t)v[3] << 32) |
695c5c4113dSnw141292 		    ((u_longlong_t)v[4] << 24) |
696c5c4113dSnw141292 		    ((u_longlong_t)v[5] << 16) |
697c5c4113dSnw141292 		    ((u_longlong_t)v[6] << 8) |
698c5c4113dSnw141292 		    (u_longlong_t)v[7];
699c5c4113dSnw141292 		for (i = 0; i < sidp->sub_authority_count; i++) {
700c5c4113dSnw141292 			j = 8 + (i * 4);
701c5c4113dSnw141292 			/* little endian -- so start from the right */
702c5c4113dSnw141292 			a = (v[j + 3] << 24) | (v[j + 2] << 16) |
703c5c4113dSnw141292 			    (v[j + 1] << 8) | (v[j]);
704c5c4113dSnw141292 			sidp->sub_authorities[i] = a;
705c5c4113dSnw141292 		}
706c5c4113dSnw141292 		return (0);
707c5c4113dSnw141292 	}
708c5c4113dSnw141292 	return (-1);
709c5c4113dSnw141292 }
710c5c4113dSnw141292 
711c5c4113dSnw141292 /*
712c5c4113dSnw141292  * Convert a sid_t to S-1-...
713c5c4113dSnw141292  */
714c5c4113dSnw141292 static
715c5c4113dSnw141292 char *
716c5c4113dSnw141292 idmap_sid2txt(sid_t *sidp)
717c5c4113dSnw141292 {
718c5c4113dSnw141292 	int	rlen, i, len;
719c5c4113dSnw141292 	char	*str, *cp;
720c5c4113dSnw141292 
721c5c4113dSnw141292 	if (sidp->version != 1)
722c5c4113dSnw141292 		return (NULL);
723c5c4113dSnw141292 
724c5c4113dSnw141292 	len = sizeof ("S-1-") - 1;
725c5c4113dSnw141292 
726c5c4113dSnw141292 	/*
727c5c4113dSnw141292 	 * We could optimize like so, but, why?
728c5c4113dSnw141292 	 *	if (sidp->authority < 10)
729c5c4113dSnw141292 	 *		len += 2;
730c5c4113dSnw141292 	 *	else if (sidp->authority < 100)
731c5c4113dSnw141292 	 *		len += 3;
732c5c4113dSnw141292 	 *	else
733c5c4113dSnw141292 	 *		len += snprintf(NULL, 0"%llu", sidp->authority);
734c5c4113dSnw141292 	 */
735c5c4113dSnw141292 	len += snprintf(NULL, 0, "%llu", sidp->authority);
736c5c4113dSnw141292 
737c5c4113dSnw141292 	/* Max length of a uint32_t printed out in ASCII is 10 bytes */
738c5c4113dSnw141292 	len += 1 + (sidp->sub_authority_count + 1) * 10;
739c5c4113dSnw141292 
740c5c4113dSnw141292 	if ((cp = str = malloc(len)) == NULL)
741c5c4113dSnw141292 		return (NULL);
742c5c4113dSnw141292 
743c5c4113dSnw141292 	rlen = snprintf(str, len, "S-1-%llu", sidp->authority);
744c5c4113dSnw141292 
745c5c4113dSnw141292 	cp += rlen;
746c5c4113dSnw141292 	len -= rlen;
747c5c4113dSnw141292 
748c5c4113dSnw141292 	for (i = 0; i < sidp->sub_authority_count; i++) {
749c5c4113dSnw141292 		assert(len > 0);
750c5c4113dSnw141292 		rlen = snprintf(cp, len, "-%u", sidp->sub_authorities[i]);
751c5c4113dSnw141292 		cp += rlen;
752c5c4113dSnw141292 		len -= rlen;
753c5c4113dSnw141292 		assert(len >= 0);
754c5c4113dSnw141292 	}
755c5c4113dSnw141292 
756c5c4113dSnw141292 	return (str);
757c5c4113dSnw141292 }
758c5c4113dSnw141292 
759c5c4113dSnw141292 /*
760c5c4113dSnw141292  * Convert a sid_t to on-the-wire encoding
761c5c4113dSnw141292  */
762c5c4113dSnw141292 static
763c5c4113dSnw141292 int
764c5c4113dSnw141292 idmap_sid2binsid(sid_t *sid, uchar_t *binsid, int binsidlen)
765c5c4113dSnw141292 {
766c5c4113dSnw141292 	uchar_t		*p;
767c5c4113dSnw141292 	int		i;
768c5c4113dSnw141292 	uint64_t	a;
769c5c4113dSnw141292 	uint32_t	r;
770c5c4113dSnw141292 
771c5c4113dSnw141292 	if (sid->version != 1 ||
772c5c4113dSnw141292 	    binsidlen != (1 + 1 + 6 + sid->sub_authority_count * 4))
773c5c4113dSnw141292 		return (-1);
774c5c4113dSnw141292 
775c5c4113dSnw141292 	p = binsid;
776c5c4113dSnw141292 	*p++ = 0x01;		/* version */
777c5c4113dSnw141292 	/* sub authority count */
778c5c4113dSnw141292 	*p++ = sid->sub_authority_count;
779c5c4113dSnw141292 	/* Authority */
780c5c4113dSnw141292 	a = sid->authority;
781c5c4113dSnw141292 	/* big-endian -- start from left */
782c5c4113dSnw141292 	*p++ = (a >> 40) & 0xFF;
783c5c4113dSnw141292 	*p++ = (a >> 32) & 0xFF;
784c5c4113dSnw141292 	*p++ = (a >> 24) & 0xFF;
785c5c4113dSnw141292 	*p++ = (a >> 16) & 0xFF;
786c5c4113dSnw141292 	*p++ = (a >> 8) & 0xFF;
787c5c4113dSnw141292 	*p++ = a & 0xFF;
788c5c4113dSnw141292 
789c5c4113dSnw141292 	/* sub-authorities */
790c5c4113dSnw141292 	for (i = 0; i < sid->sub_authority_count; i++) {
791c5c4113dSnw141292 		r = sid->sub_authorities[i];
792c5c4113dSnw141292 		/* little-endian -- start from right */
793c5c4113dSnw141292 		*p++ = (r & 0x000000FF);
794c5c4113dSnw141292 		*p++ = (r & 0x0000FF00) >> 8;
795c5c4113dSnw141292 		*p++ = (r & 0x00FF0000) >> 16;
796c5c4113dSnw141292 		*p++ = (r & 0xFF000000) >> 24;
797c5c4113dSnw141292 	}
798c5c4113dSnw141292 
799c5c4113dSnw141292 	return (0);
800c5c4113dSnw141292 }
801c5c4113dSnw141292 
802c5c4113dSnw141292 /*
803c5c4113dSnw141292  * Convert a stringified SID (S-1-...) into a hex-encoded version of the
804c5c4113dSnw141292  * on-the-wire encoding, but with each pair of hex digits pre-pended
805c5c4113dSnw141292  * with a '\', so we can pass this to libldap.
806c5c4113dSnw141292  */
807c5c4113dSnw141292 static
808c5c4113dSnw141292 int
809c5c4113dSnw141292 idmap_txtsid2hexbinsid(const char *txt, const rid_t *rid,
810c5c4113dSnw141292 	char *hexbinsid, int hexbinsidlen)
811c5c4113dSnw141292 {
812c5c4113dSnw141292 	sid_t		sid = { 0 };
813c5c4113dSnw141292 	int		i, j;
814c5c4113dSnw141292 	const char	*cp;
815c5c4113dSnw141292 	char		*ecp;
816c5c4113dSnw141292 	u_longlong_t	a;
817c5c4113dSnw141292 	unsigned long	r;
818c5c4113dSnw141292 	uchar_t		*binsid, b, hb;
819c5c4113dSnw141292 
820c5c4113dSnw141292 	/* Only version 1 SIDs please */
821c5c4113dSnw141292 	if (strncmp(txt, "S-1-", strlen("S-1-")) != 0)
822c5c4113dSnw141292 		return (-1);
823c5c4113dSnw141292 
824c5c4113dSnw141292 	if (strlen(txt) < (strlen("S-1-") + 1))
825c5c4113dSnw141292 		return (-1);
826c5c4113dSnw141292 
827c5c4113dSnw141292 	/* count '-'s */
828c5c4113dSnw141292 	for (j = 0, cp = strchr(txt, '-');
829c5c4113dSnw141292 	    cp != NULL && *cp != '\0';
830c5c4113dSnw141292 	    j++, cp = strchr(cp + 1, '-')) {
831c5c4113dSnw141292 		/* can't end on a '-' */
832c5c4113dSnw141292 		if (*(cp + 1) == '\0')
833c5c4113dSnw141292 			return (-1);
834c5c4113dSnw141292 	}
835c5c4113dSnw141292 
83662c60062Sbaban 	/* Adjust count for version and authority */
83762c60062Sbaban 	j -= 2;
83862c60062Sbaban 
83962c60062Sbaban 	/* we know the version number and RID count */
84062c60062Sbaban 	sid.version = 1;
84162c60062Sbaban 	sid.sub_authority_count = (rid != NULL) ? j + 1 : j;
84262c60062Sbaban 
843c5c4113dSnw141292 	/* must have at least one RID, but not too many */
84462c60062Sbaban 	if (sid.sub_authority_count < 1 ||
84562c60062Sbaban 	    sid.sub_authority_count > SID_MAX_SUB_AUTHORITIES)
846c5c4113dSnw141292 		return (-1);
847c5c4113dSnw141292 
848c5c4113dSnw141292 	/* check that we only have digits and '-' */
849c5c4113dSnw141292 	if (strspn(txt + 1, "0123456789-") < (strlen(txt) - 1))
850c5c4113dSnw141292 		return (-1);
851c5c4113dSnw141292 
852c5c4113dSnw141292 	cp = txt + strlen("S-1-");
853c5c4113dSnw141292 
854c5c4113dSnw141292 	/* 64-bit safe parsing of unsigned 48-bit authority value */
855c5c4113dSnw141292 	errno = 0;
856c5c4113dSnw141292 	a = strtoull(cp, &ecp, 10);
857c5c4113dSnw141292 
858c5c4113dSnw141292 	/* errors parsing the authority or too many bits */
859c5c4113dSnw141292 	if (cp == ecp || (a == 0 && errno == EINVAL) ||
860c5c4113dSnw141292 	    (a == ULLONG_MAX && errno == ERANGE) ||
861c5c4113dSnw141292 	    (a & 0x0000ffffffffffffULL) != a)
862c5c4113dSnw141292 		return (-1);
863c5c4113dSnw141292 
864c5c4113dSnw141292 	cp = ecp;
865c5c4113dSnw141292 
866c5c4113dSnw141292 	sid.authority = (uint64_t)a;
867c5c4113dSnw141292 
86862c60062Sbaban 	for (i = 0; i < j; i++) {
869c5c4113dSnw141292 		if (*cp++ != '-')
870c5c4113dSnw141292 			return (-1);
871c5c4113dSnw141292 		/* 64-bit safe parsing of unsigned 32-bit RID */
872c5c4113dSnw141292 		errno = 0;
873c5c4113dSnw141292 		r = strtoul(cp, &ecp, 10);
874c5c4113dSnw141292 		/* errors parsing the RID or too many bits */
875c5c4113dSnw141292 		if (cp == ecp || (r == 0 && errno == EINVAL) ||
876c5c4113dSnw141292 		    (r == ULONG_MAX && errno == ERANGE) ||
877c5c4113dSnw141292 		    (r & 0xffffffffUL) != r)
878c5c4113dSnw141292 			return (-1);
879c5c4113dSnw141292 		sid.sub_authorities[i] = (uint32_t)r;
880c5c4113dSnw141292 		cp = ecp;
881c5c4113dSnw141292 	}
882c5c4113dSnw141292 
883c5c4113dSnw141292 	/* check that all of the string SID has been consumed */
884c5c4113dSnw141292 	if (*cp != '\0')
885c5c4113dSnw141292 		return (-1);
886c5c4113dSnw141292 
88762c60062Sbaban 	if (rid != NULL)
88862c60062Sbaban 		sid.sub_authorities[j] = *rid;
889c5c4113dSnw141292 
890c5c4113dSnw141292 	j = 1 + 1 + 6 + sid.sub_authority_count * 4;
891c5c4113dSnw141292 
892c5c4113dSnw141292 	if (hexbinsidlen < (j * 3))
893c5c4113dSnw141292 		return (-2);
894c5c4113dSnw141292 
895c5c4113dSnw141292 	/* binary encode the SID */
896c5c4113dSnw141292 	binsid = (uchar_t *)alloca(j);
897c5c4113dSnw141292 	(void) idmap_sid2binsid(&sid, binsid, j);
898c5c4113dSnw141292 
899c5c4113dSnw141292 	/* hex encode, with a backslash before each byte */
900c5c4113dSnw141292 	for (ecp = hexbinsid, i = 0; i < j; i++) {
901c5c4113dSnw141292 		b = binsid[i];
902c5c4113dSnw141292 		*ecp++ = '\\';
903c5c4113dSnw141292 		hb = (b >> 4) & 0xF;
904c5c4113dSnw141292 		*ecp++ = (hb <= 0x9 ? hb + '0' : hb - 10 + 'A');
905c5c4113dSnw141292 		hb = b & 0xF;
906c5c4113dSnw141292 		*ecp++ = (hb <= 0x9 ? hb + '0' : hb - 10 + 'A');
907c5c4113dSnw141292 	}
908c5c4113dSnw141292 	*ecp = '\0';
909c5c4113dSnw141292 
910c5c4113dSnw141292 	return (0);
911c5c4113dSnw141292 }
912c5c4113dSnw141292 
913c5c4113dSnw141292 static
914c5c4113dSnw141292 char *
915c5c4113dSnw141292 convert_bval2sid(BerValue *bval, rid_t *rid)
916c5c4113dSnw141292 {
917c5c4113dSnw141292 	sid_t	sid;
918c5c4113dSnw141292 
919c5c4113dSnw141292 	if (idmap_getsid(bval, &sid) < 0)
920c5c4113dSnw141292 		return (NULL);
921c5c4113dSnw141292 
922c5c4113dSnw141292 	/*
923c5c4113dSnw141292 	 * If desired and if the SID is what should be a domain/computer
924c5c4113dSnw141292 	 * user or group SID (i.e., S-1-5-w-x-y-z-<user/group RID>) then
925c5c4113dSnw141292 	 * save the last RID and truncate the SID
926c5c4113dSnw141292 	 */
927c5c4113dSnw141292 	if (rid != NULL && sid.authority == 5 && sid.sub_authority_count == 5)
928c5c4113dSnw141292 		*rid = sid.sub_authorities[--sid.sub_authority_count];
929c5c4113dSnw141292 	return (idmap_sid2txt(&sid));
930c5c4113dSnw141292 }
931c5c4113dSnw141292 
932c5c4113dSnw141292 
933c5c4113dSnw141292 idmap_retcode
934c5c4113dSnw141292 idmap_lookup_batch_start(ad_t *ad, int nqueries, idmap_query_state_t **state)
935c5c4113dSnw141292 {
936c5c4113dSnw141292 	idmap_query_state_t *new_state;
937c5c4113dSnw141292 	ad_host_t	*adh = NULL;
938c5c4113dSnw141292 
939c5c4113dSnw141292 	*state = NULL;
940c5c4113dSnw141292 
941c5c4113dSnw141292 	if (*ad->dflt_w2k_dom == '\0')
942c5c4113dSnw141292 		return (-1);
943c5c4113dSnw141292 
944c5c4113dSnw141292 	adh = idmap_get_conn(ad);
945c5c4113dSnw141292 	if (adh == NULL)
946c5c4113dSnw141292 		return (IDMAP_ERR_OTHER);
947c5c4113dSnw141292 
948c5c4113dSnw141292 	new_state = calloc(1, sizeof (idmap_query_state_t) +
949c5c4113dSnw141292 	    (nqueries - 1) * sizeof (idmap_q_t));
950c5c4113dSnw141292 
951c5c4113dSnw141292 	if (new_state == NULL)
952c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
953c5c4113dSnw141292 
954*84decf41Sjp151216 	new_state->ref_cnt = 1;
955c5c4113dSnw141292 	new_state->qadh = adh;
956c5c4113dSnw141292 	new_state->qcount = nqueries;
957c5c4113dSnw141292 	new_state->qadh_gen = adh->generation;
958c5c4113dSnw141292 	/* should be -1, but the atomic routines want unsigned */
959c5c4113dSnw141292 	new_state->qlastsent = 0;
960*84decf41Sjp151216 	(void) pthread_cond_init(&new_state->cv, NULL);
961c5c4113dSnw141292 
962c5c4113dSnw141292 	(void) pthread_mutex_lock(&qstatelock);
963c5c4113dSnw141292 	new_state->next = qstatehead;
964c5c4113dSnw141292 	qstatehead = new_state;
965c5c4113dSnw141292 	(void) pthread_mutex_unlock(&qstatelock);
966c5c4113dSnw141292 
967c5c4113dSnw141292 	*state = new_state;
968c5c4113dSnw141292 
969c5c4113dSnw141292 	return (IDMAP_SUCCESS);
970c5c4113dSnw141292 }
971c5c4113dSnw141292 
972c5c4113dSnw141292 /*
973c5c4113dSnw141292  * Find the idmap_query_state_t to which a given LDAP result msgid on a
974*84decf41Sjp151216  * given connection belongs. This routine increaments the reference count
975*84decf41Sjp151216  * so that the object can not be freed. idmap_lookup_unlock_batch()
976*84decf41Sjp151216  * must be called to decreament the reference count.
977c5c4113dSnw141292  */
978c5c4113dSnw141292 static
979c5c4113dSnw141292 int
980c5c4113dSnw141292 idmap_msgid2query(ad_host_t *adh, int msgid,
981c5c4113dSnw141292 	idmap_query_state_t **state, int *qid)
982c5c4113dSnw141292 {
983c5c4113dSnw141292 	idmap_query_state_t *p;
984c5c4113dSnw141292 	int		    i;
985c5c4113dSnw141292 
986c5c4113dSnw141292 	(void) pthread_mutex_lock(&qstatelock);
987c5c4113dSnw141292 	for (p = qstatehead; p != NULL; p = p->next) {
988c5c4113dSnw141292 		if (p->qadh != adh || adh->generation != p->qadh_gen)
989c5c4113dSnw141292 			continue;
990c5c4113dSnw141292 		for (i = 0; i < p->qcount; i++) {
991c5c4113dSnw141292 			if ((p->queries[i]).msgid == msgid) {
992*84decf41Sjp151216 				p->ref_cnt++;
993c5c4113dSnw141292 				*state = p;
994c5c4113dSnw141292 				*qid = i;
995c5c4113dSnw141292 				(void) pthread_mutex_unlock(&qstatelock);
996c5c4113dSnw141292 				return (1);
997c5c4113dSnw141292 			}
998c5c4113dSnw141292 		}
999c5c4113dSnw141292 	}
1000c5c4113dSnw141292 	(void) pthread_mutex_unlock(&qstatelock);
1001c5c4113dSnw141292 	return (0);
1002c5c4113dSnw141292 }
1003c5c4113dSnw141292 
1004c5c4113dSnw141292 /*
1005c5c4113dSnw141292  * Handle an objectSid attr from a result
1006c5c4113dSnw141292  */
1007c5c4113dSnw141292 static
1008c5c4113dSnw141292 void
1009c5c4113dSnw141292 idmap_bv_objsid2sidstr(BerValue **bvalues, idmap_q_t *q)
1010c5c4113dSnw141292 {
1011c5c4113dSnw141292 	if (bvalues == NULL)
1012c5c4113dSnw141292 		return;
1013c5c4113dSnw141292 	/* objectSid is single valued */
1014c5c4113dSnw141292 	*(q->result) = convert_bval2sid(bvalues[0], q->rid);
1015c5c4113dSnw141292 	q->got_objectSid = 1;
1016c5c4113dSnw141292 }
1017c5c4113dSnw141292 
1018c5c4113dSnw141292 /*
1019c5c4113dSnw141292  * Handle a sAMAccountName attr from a result
1020c5c4113dSnw141292  */
1021c5c4113dSnw141292 static
1022c5c4113dSnw141292 void
1023c5c4113dSnw141292 idmap_bv_samaccountname2name(BerValue **bvalues, idmap_q_t *q, const char *dn)
1024c5c4113dSnw141292 {
1025c5c4113dSnw141292 	char *result, *domain;
1026c5c4113dSnw141292 	int len;
1027c5c4113dSnw141292 
1028c5c4113dSnw141292 	if (bvalues == NULL)
1029c5c4113dSnw141292 		return;
1030c5c4113dSnw141292 
1031c5c4113dSnw141292 	if ((domain = dn2dns(dn)) == NULL)
1032c5c4113dSnw141292 		return;
1033c5c4113dSnw141292 
1034c5c4113dSnw141292 	if (bvalues == NULL || bvalues[0] == NULL ||
1035c5c4113dSnw141292 	    bvalues[0]->bv_val == NULL)
1036c5c4113dSnw141292 		return;
1037c5c4113dSnw141292 
1038c5c4113dSnw141292 	len = bvalues[0]->bv_len + 1;
1039c5c4113dSnw141292 
1040c5c4113dSnw141292 	if (q->domain != NULL)
1041c5c4113dSnw141292 		*(q->domain) = domain;
1042c5c4113dSnw141292 	else
1043c5c4113dSnw141292 		len += strlen(domain) + 1;
1044c5c4113dSnw141292 
1045c5c4113dSnw141292 	if ((result = malloc(len)) == NULL) {
1046c5c4113dSnw141292 		if (q->domain != NULL)
1047c5c4113dSnw141292 			*(q->domain) = NULL;
1048c5c4113dSnw141292 		free(domain);
1049c5c4113dSnw141292 		return;
1050c5c4113dSnw141292 	}
1051c5c4113dSnw141292 
1052c5c4113dSnw141292 	(void) memcpy(result, bvalues[0]->bv_val, (size_t)bvalues[0]->bv_len);
1053c5c4113dSnw141292 	result[bvalues[0]->bv_len] = '\0';
1054c5c4113dSnw141292 
1055c5c4113dSnw141292 	if (q->domain == NULL) {
1056c5c4113dSnw141292 		(void) strlcat(result, "@", len);
1057c5c4113dSnw141292 		(void) strlcat(result, domain, len);
1058c5c4113dSnw141292 		free(domain);
1059c5c4113dSnw141292 	}
1060c5c4113dSnw141292 
1061c5c4113dSnw141292 	*(q->result) = result;
1062c5c4113dSnw141292 	q->got_samAcctName = 1;
1063c5c4113dSnw141292 }
1064c5c4113dSnw141292 
1065c5c4113dSnw141292 
1066c5c4113dSnw141292 #define	BVAL_CASEEQ(bv, str) \
1067c5c4113dSnw141292 		(((*(bv))->bv_len == (sizeof (str) - 1)) && \
1068c5c4113dSnw141292 		    strncasecmp((*(bv))->bv_val, str, (*(bv))->bv_len) == 0)
1069c5c4113dSnw141292 
1070c5c4113dSnw141292 /*
1071c5c4113dSnw141292  * Handle an objectClass attr from a result
1072c5c4113dSnw141292  */
1073c5c4113dSnw141292 static
1074c5c4113dSnw141292 void
1075c5c4113dSnw141292 idmap_bv_objclass2sidtype(BerValue **bvalues, idmap_q_t *q)
1076c5c4113dSnw141292 {
1077c5c4113dSnw141292 	BerValue	**cbval;
1078c5c4113dSnw141292 
1079c5c4113dSnw141292 	if (bvalues == NULL)
1080c5c4113dSnw141292 		return;
1081c5c4113dSnw141292 
1082c5c4113dSnw141292 	for (cbval = bvalues; *cbval != NULL; cbval++) {
1083c5c4113dSnw141292 		/* don't clobber sid_type */
1084c5c4113dSnw141292 		if (*(q->sid_type) == _IDMAP_T_COMPUTER ||
1085c5c4113dSnw141292 		    *(q->sid_type) == _IDMAP_T_GROUP ||
1086c5c4113dSnw141292 		    *(q->sid_type) == _IDMAP_T_USER)
1087c5c4113dSnw141292 			continue;
1088c5c4113dSnw141292 
1089c5c4113dSnw141292 		if (BVAL_CASEEQ(cbval, "Computer")) {
1090c5c4113dSnw141292 			*(q->sid_type) = _IDMAP_T_COMPUTER;
1091c5c4113dSnw141292 			return;
1092c5c4113dSnw141292 		} else if (BVAL_CASEEQ(cbval, "Group")) {
1093c5c4113dSnw141292 			*(q->sid_type) = _IDMAP_T_GROUP;
1094c5c4113dSnw141292 		} else if (BVAL_CASEEQ(cbval, "USER")) {
1095c5c4113dSnw141292 			*(q->sid_type) = _IDMAP_T_USER;
1096c5c4113dSnw141292 		} else
1097c5c4113dSnw141292 			*(q->sid_type) = _IDMAP_T_OTHER;
1098c5c4113dSnw141292 		q->got_objectClass = 1;
1099c5c4113dSnw141292 	}
1100c5c4113dSnw141292 }
1101c5c4113dSnw141292 
1102c5c4113dSnw141292 /*
1103c5c4113dSnw141292  * Handle a given search result entry
1104c5c4113dSnw141292  */
1105c5c4113dSnw141292 static
1106c5c4113dSnw141292 void
1107c5c4113dSnw141292 idmap_extract_object(idmap_query_state_t *state, int qid, LDAPMessage *res)
1108c5c4113dSnw141292 {
1109c5c4113dSnw141292 	char			*dn, *attr;
1110c5c4113dSnw141292 	BerElement		*ber = NULL;
1111c5c4113dSnw141292 	BerValue		**bvalues;
1112c5c4113dSnw141292 	ad_host_t		*adh;
1113c5c4113dSnw141292 	idmap_q_t		*q;
1114c5c4113dSnw141292 	idmap_retcode		orc;
1115c5c4113dSnw141292 
1116c5c4113dSnw141292 	adh = state->qadh;
1117c5c4113dSnw141292 
1118c5c4113dSnw141292 	(void) pthread_mutex_lock(&adh->lock);
1119c5c4113dSnw141292 
1120c5c4113dSnw141292 	if (adh->dead || (dn = ldap_get_dn(adh->ld, res)) == NULL) {
1121c5c4113dSnw141292 		(void) pthread_mutex_unlock(&adh->lock);
1122c5c4113dSnw141292 		return;
1123c5c4113dSnw141292 	}
1124c5c4113dSnw141292 
1125c5c4113dSnw141292 	q = &(state->queries[qid]);
1126c5c4113dSnw141292 
1127c5c4113dSnw141292 	for (attr = ldap_first_attribute(adh->ld, res, &ber); attr != NULL;
1128c5c4113dSnw141292 	    attr = ldap_next_attribute(adh->ld, res, ber)) {
1129c5c4113dSnw141292 		orc = *q->rc;
1130c5c4113dSnw141292 		bvalues = NULL;	/* for memory management below */
1131c5c4113dSnw141292 
1132c5c4113dSnw141292 		/*
1133c5c4113dSnw141292 		 * If this is an attribute we are looking for and
1134c5c4113dSnw141292 		 * haven't seen it yet, parse it
1135c5c4113dSnw141292 		 */
1136c5c4113dSnw141292 		if (orc != IDMAP_SUCCESS && q->n2s && !q->got_objectSid &&
1137c5c4113dSnw141292 		    strcasecmp(attr, OBJECTSID) == 0) {
1138c5c4113dSnw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
1139c5c4113dSnw141292 			idmap_bv_objsid2sidstr(bvalues, q);
1140c5c4113dSnw141292 		} else if (orc != IDMAP_SUCCESS && !q->n2s &&
1141c5c4113dSnw141292 		    !q->got_samAcctName &&
1142c5c4113dSnw141292 		    strcasecmp(attr, SAMACCOUNTNAME) == 0) {
1143c5c4113dSnw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
1144c5c4113dSnw141292 			idmap_bv_samaccountname2name(bvalues, q, dn);
1145c5c4113dSnw141292 		} else if (orc != IDMAP_SUCCESS && !q->got_objectClass &&
1146c5c4113dSnw141292 		    strcasecmp(attr, OBJECTCLASS) == 0) {
1147c5c4113dSnw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
1148c5c4113dSnw141292 			idmap_bv_objclass2sidtype(bvalues, q);
1149c5c4113dSnw141292 		}
1150c5c4113dSnw141292 
1151c5c4113dSnw141292 		if (bvalues != NULL)
1152c5c4113dSnw141292 			ldap_value_free_len(bvalues);
1153c5c4113dSnw141292 		ldap_memfree(attr);
1154c5c4113dSnw141292 
1155c5c4113dSnw141292 		if (q->n2s)
1156c5c4113dSnw141292 			*q->rc = (q->got_objectSid &&
1157c5c4113dSnw141292 			    q->got_objectClass) ?
1158c5c4113dSnw141292 			    IDMAP_SUCCESS : IDMAP_ERR_NORESULT;
1159c5c4113dSnw141292 		else
1160c5c4113dSnw141292 			*q->rc = (q->got_samAcctName &&
1161c5c4113dSnw141292 			    q->got_objectClass) ?
1162c5c4113dSnw141292 			    IDMAP_SUCCESS : IDMAP_ERR_NORESULT;
1163c5c4113dSnw141292 
1164c5c4113dSnw141292 		if (*q->rc == IDMAP_SUCCESS && *q->result == NULL)
1165c5c4113dSnw141292 			*q->rc = IDMAP_ERR_NORESULT;
1166c5c4113dSnw141292 	}
1167c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adh->lock);
1168c5c4113dSnw141292 
1169c5c4113dSnw141292 	/*
1170c5c4113dSnw141292 	 * If there should be multiple partial results for different
1171c5c4113dSnw141292 	 * entities (there should not be, but, if it should happen) then
1172c5c4113dSnw141292 	 * it's possible that they could get mixed up here and we could
1173c5c4113dSnw141292 	 * get bogus results.  We just mark the query's results as
1174c5c4113dSnw141292 	 * toxic (IDMAP_ERR_INTERNAL).
1175c5c4113dSnw141292 	 *
1176c5c4113dSnw141292 	 * Between this and ignoring results when we've already filled
1177c5c4113dSnw141292 	 * out a query's results we should be OK.  The first full reply
1178c5c4113dSnw141292 	 * wins.  In practice we should never get multiple results.
1179c5c4113dSnw141292 	 */
1180c5c4113dSnw141292 	if (orc == IDMAP_ERR_INTERNAL)
1181c5c4113dSnw141292 		*q->rc = IDMAP_ERR_INTERNAL;
1182c5c4113dSnw141292 	else if (*q->rc != IDMAP_SUCCESS)
1183c5c4113dSnw141292 		*q->rc = IDMAP_ERR_INTERNAL;
1184c5c4113dSnw141292 
1185c5c4113dSnw141292 	if (ber != NULL)
1186c5c4113dSnw141292 		ber_free(ber, 0);
1187c5c4113dSnw141292 
1188c5c4113dSnw141292 	ldap_memfree(dn);
1189c5c4113dSnw141292 }
1190c5c4113dSnw141292 
1191c5c4113dSnw141292 /*
1192c5c4113dSnw141292  * Try to get a result; if there is one, find the corresponding
1193c5c4113dSnw141292  * idmap_q_t and process the result.
1194c5c4113dSnw141292  */
1195c5c4113dSnw141292 static
1196c5c4113dSnw141292 int
1197c5c4113dSnw141292 idmap_get_adobject_batch(ad_host_t *adh, struct timeval *timeout)
1198c5c4113dSnw141292 {
1199c5c4113dSnw141292 	idmap_query_state_t	*query_state;
1200c5c4113dSnw141292 	LDAPMessage		*res = NULL;
1201c5c4113dSnw141292 	int			rc, ret, msgid, qid;
1202c5c4113dSnw141292 
1203c5c4113dSnw141292 	(void) pthread_mutex_lock(&adh->lock);
1204c5c4113dSnw141292 	if (adh->dead) {
1205c5c4113dSnw141292 		(void) pthread_mutex_unlock(&adh->lock);
1206c5c4113dSnw141292 		return (-1);
1207c5c4113dSnw141292 	}
1208c5c4113dSnw141292 
1209c5c4113dSnw141292 	/* Get one result */
1210c5c4113dSnw141292 	rc = ldap_result(adh->ld, LDAP_RES_ANY, 0,
1211c5c4113dSnw141292 	    timeout, &res);
1212c5c4113dSnw141292 	if (rc == LDAP_UNAVAILABLE || rc == LDAP_UNWILLING_TO_PERFORM ||
1213c5c4113dSnw141292 	    rc == LDAP_CONNECT_ERROR || rc == LDAP_SERVER_DOWN ||
1214c5c4113dSnw141292 	    rc == LDAP_BUSY)
1215c5c4113dSnw141292 		adh->dead = 1;
1216c5c4113dSnw141292 	(void) pthread_mutex_unlock(&adh->lock);
1217c5c4113dSnw141292 
1218c5c4113dSnw141292 	if (adh->dead)
1219c5c4113dSnw141292 		return (-1);
1220c5c4113dSnw141292 
1221c5c4113dSnw141292 	switch (rc) {
1222c5c4113dSnw141292 	case LDAP_RES_SEARCH_RESULT:
1223c5c4113dSnw141292 		/* We have all the LDAP replies for some search... */
1224c5c4113dSnw141292 		msgid = ldap_msgid(res);
1225c5c4113dSnw141292 		if (idmap_msgid2query(adh, msgid,
1226c5c4113dSnw141292 		    &query_state, &qid)) {
1227c5c4113dSnw141292 			/* ...so we can decrement qinflight */
1228c5c4113dSnw141292 			atomic_dec_32(&query_state->qinflight);
1229c5c4113dSnw141292 			/* we saw at least one reply */
1230c5c4113dSnw141292 			query_state->queries[qid].got_reply = 1;
1231*84decf41Sjp151216 			idmap_lookup_unlock_batch(&query_state);
1232c5c4113dSnw141292 		}
1233c5c4113dSnw141292 		(void) ldap_msgfree(res);
1234c5c4113dSnw141292 		ret = 0;
1235c5c4113dSnw141292 		break;
1236c5c4113dSnw141292 	case LDAP_RES_SEARCH_REFERENCE:
1237c5c4113dSnw141292 		/*
1238c5c4113dSnw141292 		 * We have no need for these at the moment.  Eventually,
1239c5c4113dSnw141292 		 * when we query things that we can't expect to find in
1240c5c4113dSnw141292 		 * the Global Catalog then we'll need to learn to follow
1241c5c4113dSnw141292 		 * references.
1242c5c4113dSnw141292 		 */
1243c5c4113dSnw141292 		(void) ldap_msgfree(res);
1244c5c4113dSnw141292 		ret = 0;
1245c5c4113dSnw141292 		break;
1246c5c4113dSnw141292 	case LDAP_RES_SEARCH_ENTRY:
1247c5c4113dSnw141292 		/* Got a result */
1248c5c4113dSnw141292 		msgid = ldap_msgid(res);
1249c5c4113dSnw141292 		if (idmap_msgid2query(adh, msgid,
1250c5c4113dSnw141292 		    &query_state, &qid)) {
1251c5c4113dSnw141292 			idmap_extract_object(query_state, qid, res);
1252c5c4113dSnw141292 			/* we saw at least one result */
1253c5c4113dSnw141292 			query_state->queries[qid].got_reply = 1;
1254c5c4113dSnw141292 			query_state->queries[qid].got_results = 1;
1255*84decf41Sjp151216 			idmap_lookup_unlock_batch(&query_state);
1256c5c4113dSnw141292 		}
1257c5c4113dSnw141292 		(void) ldap_msgfree(res);
1258c5c4113dSnw141292 		ret = 0;
1259c5c4113dSnw141292 		break;
1260c5c4113dSnw141292 	default:
1261c5c4113dSnw141292 		/* timeout or error; treat the same */
1262c5c4113dSnw141292 		ret = -1;
1263c5c4113dSnw141292 		break;
1264c5c4113dSnw141292 	}
1265c5c4113dSnw141292 
1266c5c4113dSnw141292 	return (ret);
1267c5c4113dSnw141292 }
1268c5c4113dSnw141292 
1269*84decf41Sjp151216 /*
1270*84decf41Sjp151216  * This routine decreament the reference count of the
1271*84decf41Sjp151216  * idmap_query_state_t
1272*84decf41Sjp151216  */
1273*84decf41Sjp151216 static void
1274*84decf41Sjp151216 idmap_lookup_unlock_batch(idmap_query_state_t **state)
1275*84decf41Sjp151216 {
1276*84decf41Sjp151216 	/*
1277*84decf41Sjp151216 	 * Decrement reference count with qstatelock locked
1278*84decf41Sjp151216 	 */
1279*84decf41Sjp151216 	(void) pthread_mutex_lock(&qstatelock);
1280*84decf41Sjp151216 	(*state)->ref_cnt--;
1281*84decf41Sjp151216 	/*
1282*84decf41Sjp151216 	 * If there are no references wakup the allocating thread
1283*84decf41Sjp151216 	 */
1284*84decf41Sjp151216 	if ((*state)->ref_cnt == 0)
1285*84decf41Sjp151216 		(void) pthread_cond_signal(&(*state)->cv);
1286*84decf41Sjp151216 	(void) pthread_mutex_unlock(&qstatelock);
1287*84decf41Sjp151216 	*state = NULL;
1288*84decf41Sjp151216 }
1289*84decf41Sjp151216 
1290*84decf41Sjp151216 /*
1291*84decf41Sjp151216  * This routine frees the idmap_query_state_t structure
1292*84decf41Sjp151216  * If the reference count is greater than 1 it waits
1293*84decf41Sjp151216  * for the other threads to finish using it.
1294*84decf41Sjp151216  */
1295c5c4113dSnw141292 void
1296*84decf41Sjp151216 idmap_lookup_release_batch(idmap_query_state_t **state)
1297c5c4113dSnw141292 {
1298c5c4113dSnw141292 	idmap_query_state_t **p;
1299c5c4113dSnw141292 
1300*84decf41Sjp151216 	/*
1301*84decf41Sjp151216 	 * Decrement reference count with qstatelock locked
1302*84decf41Sjp151216 	 * and wait for reference count to get to zero
1303*84decf41Sjp151216 	 */
1304*84decf41Sjp151216 	(void) pthread_mutex_lock(&qstatelock);
1305*84decf41Sjp151216 	(*state)->ref_cnt--;
1306*84decf41Sjp151216 	while ((*state)->ref_cnt > 0) {
1307*84decf41Sjp151216 		(void) pthread_cond_wait(&(*state)->cv, &qstatelock);
1308*84decf41Sjp151216 	}
1309c5c4113dSnw141292 
1310c5c4113dSnw141292 	/* Remove this state struct from the list of state structs */
1311c5c4113dSnw141292 	for (p = &qstatehead; *p != NULL; p = &(*p)->next) {
1312c5c4113dSnw141292 		if (*p == (*state)) {
1313c5c4113dSnw141292 			*p = (*state)->next;
1314c5c4113dSnw141292 			break;
1315c5c4113dSnw141292 		}
1316c5c4113dSnw141292 	}
1317c5c4113dSnw141292 	(void) pthread_mutex_unlock(&qstatelock);
1318c5c4113dSnw141292 
1319*84decf41Sjp151216 	(void) pthread_cond_destroy(&(*state)->cv);
1320*84decf41Sjp151216 
1321*84decf41Sjp151216 	idmap_release_conn((*state)->qadh);
1322*84decf41Sjp151216 
1323c5c4113dSnw141292 	free(*state);
1324c5c4113dSnw141292 	*state = NULL;
1325c5c4113dSnw141292 }
1326c5c4113dSnw141292 
1327c5c4113dSnw141292 idmap_retcode
1328c5c4113dSnw141292 idmap_lookup_batch_end(idmap_query_state_t **state,
1329c5c4113dSnw141292 	struct timeval *timeout)
1330c5c4113dSnw141292 {
1331c5c4113dSnw141292 	idmap_q_t	    *q;
1332c5c4113dSnw141292 	int		    i;
1333c5c4113dSnw141292 	int		    rc = LDAP_SUCCESS;
1334c5c4113dSnw141292 	idmap_retcode	    retcode = IDMAP_SUCCESS;
1335c5c4113dSnw141292 
1336c5c4113dSnw141292 	(*state)->qdead = 1;
1337c5c4113dSnw141292 
1338c5c4113dSnw141292 	/* Process results until done or until timeout, if given */
1339c5c4113dSnw141292 	while ((*state)->qinflight > 0) {
1340c5c4113dSnw141292 		if ((rc = idmap_get_adobject_batch((*state)->qadh,
1341c5c4113dSnw141292 		    timeout)) != 0)
1342c5c4113dSnw141292 			break;
1343c5c4113dSnw141292 	}
1344c5c4113dSnw141292 
1345c5c4113dSnw141292 	if (rc == LDAP_UNAVAILABLE || rc == LDAP_UNWILLING_TO_PERFORM ||
1346c5c4113dSnw141292 	    rc == LDAP_CONNECT_ERROR || rc == LDAP_SERVER_DOWN ||
1347c5c4113dSnw141292 	    rc == LDAP_BUSY) {
1348c5c4113dSnw141292 		retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
1349c5c4113dSnw141292 		(*state)->qadh->dead = 1;
1350c5c4113dSnw141292 	}
1351c5c4113dSnw141292 
1352c5c4113dSnw141292 	for (i = 0; i < (*state)->qcount; i++) {
1353c5c4113dSnw141292 		q = &((*state)->queries[i]);
1354c5c4113dSnw141292 		if (q->got_reply && !q->got_results) {
1355c5c4113dSnw141292 			if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR)
1356c5c4113dSnw141292 				*q->rc = IDMAP_ERR_RETRIABLE_NET_ERR;
1357c5c4113dSnw141292 			else
1358c5c4113dSnw141292 				*q->rc = IDMAP_ERR_NOTFOUND;
1359c5c4113dSnw141292 		}
1360c5c4113dSnw141292 	}
1361c5c4113dSnw141292 
1362*84decf41Sjp151216 	idmap_lookup_release_batch(state);
1363c5c4113dSnw141292 
1364c5c4113dSnw141292 	return (retcode);
1365c5c4113dSnw141292 }
1366c5c4113dSnw141292 
1367c5c4113dSnw141292 /*
1368c5c4113dSnw141292  * Send one prepared search, queue up msgid, process what results are
1369c5c4113dSnw141292  * available
1370c5c4113dSnw141292  */
1371c5c4113dSnw141292 static
1372c5c4113dSnw141292 idmap_retcode
1373c5c4113dSnw141292 idmap_batch_add1(idmap_query_state_t *state, int n2s,
1374c5c4113dSnw141292 	const char *filter, const char *basedn,
1375c5c4113dSnw141292 	char **result, char **dname, rid_t *rid, int *sid_type,
1376c5c4113dSnw141292 	idmap_retcode *rc)
1377c5c4113dSnw141292 {
1378c5c4113dSnw141292 	idmap_retcode	retcode = IDMAP_SUCCESS;
1379c5c4113dSnw141292 	int		lrc, qid;
1380c5c4113dSnw141292 	struct timeval	tv;
1381c5c4113dSnw141292 	idmap_q_t	*q;
1382c5c4113dSnw141292 
1383c5c4113dSnw141292 	if (state->qdead) {
1384c5c4113dSnw141292 		*rc = IDMAP_ERR_NORESULT;
1385c5c4113dSnw141292 		return (IDMAP_ERR_RETRIABLE_NET_ERR);
1386c5c4113dSnw141292 	}
1387c5c4113dSnw141292 
1388c5c4113dSnw141292 	qid = atomic_inc_32_nv(&state->qlastsent) - 1;
1389c5c4113dSnw141292 
1390c5c4113dSnw141292 	q = &(state->queries[qid]);
1391c5c4113dSnw141292 
1392c5c4113dSnw141292 	/* Remember where to put the results */
1393c5c4113dSnw141292 	q->result = result;
1394c5c4113dSnw141292 	q->domain = dname;
1395c5c4113dSnw141292 	q->rid = rid;
1396c5c4113dSnw141292 	q->sid_type = sid_type;
1397c5c4113dSnw141292 	q->rc = rc;
1398c5c4113dSnw141292 	q->n2s = n2s ? 1 : 0;
1399c5c4113dSnw141292 	q->got_objectSid = 0;
1400c5c4113dSnw141292 	q->got_objectClass = 0;
1401c5c4113dSnw141292 	q->got_samAcctName = 0;
1402c5c4113dSnw141292 
1403c5c4113dSnw141292 	/*
1404c5c4113dSnw141292 	 * Provide sane defaults for the results in case we never hear
1405c5c4113dSnw141292 	 * back from the DS before closing the connection.
1406c5c4113dSnw141292 	 */
1407c5c4113dSnw141292 	*rc = IDMAP_ERR_RETRIABLE_NET_ERR;
1408c5c4113dSnw141292 	*sid_type = _IDMAP_T_OTHER;
1409c5c4113dSnw141292 	*result = NULL;
1410c5c4113dSnw141292 	if (dname != NULL)
1411c5c4113dSnw141292 		*dname = NULL;
1412c5c4113dSnw141292 	if (rid != NULL)
1413c5c4113dSnw141292 		*rid = 0;
1414c5c4113dSnw141292 
1415c5c4113dSnw141292 	/* Send this lookup, don't wait for a result here */
1416c5c4113dSnw141292 	(void) pthread_mutex_lock(&state->qadh->lock);
1417c5c4113dSnw141292 
1418c5c4113dSnw141292 	if (!state->qadh->dead) {
1419c5c4113dSnw141292 		state->qadh->idletime = time(NULL);
1420c5c4113dSnw141292 		lrc = ldap_search_ext(state->qadh->ld, basedn,
1421c5c4113dSnw141292 		    LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL,
1422c5c4113dSnw141292 		    NULL, -1, &q->msgid);
1423c5c4113dSnw141292 		if (lrc == LDAP_BUSY || lrc == LDAP_UNAVAILABLE ||
1424c5c4113dSnw141292 		    lrc == LDAP_CONNECT_ERROR || lrc == LDAP_SERVER_DOWN ||
1425c5c4113dSnw141292 		    lrc == LDAP_UNWILLING_TO_PERFORM) {
1426c5c4113dSnw141292 			retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
1427c5c4113dSnw141292 			state->qadh->dead = 1;
1428c5c4113dSnw141292 		} else if (lrc != LDAP_SUCCESS) {
1429c5c4113dSnw141292 			retcode = IDMAP_ERR_OTHER;
1430c5c4113dSnw141292 			state->qadh->dead = 1;
1431c5c4113dSnw141292 		}
1432c5c4113dSnw141292 	}
1433c5c4113dSnw141292 	(void) pthread_mutex_unlock(&state->qadh->lock);
1434c5c4113dSnw141292 
1435c5c4113dSnw141292 	if (state->qadh->dead)
1436c5c4113dSnw141292 		return (retcode);
1437c5c4113dSnw141292 
1438c5c4113dSnw141292 	atomic_inc_32(&state->qinflight);
1439c5c4113dSnw141292 
1440c5c4113dSnw141292 	/*
1441c5c4113dSnw141292 	 * Reap as many requests as we can _without_ waiting
1442c5c4113dSnw141292 	 *
1443c5c4113dSnw141292 	 * We do this to prevent any possible TCP socket buffer
1444c5c4113dSnw141292 	 * starvation deadlocks.
1445c5c4113dSnw141292 	 */
1446c5c4113dSnw141292 	(void) memset(&tv, 0, sizeof (tv));
1447c5c4113dSnw141292 	while (idmap_get_adobject_batch(state->qadh, &tv) == 0)
1448c5c4113dSnw141292 		;
1449c5c4113dSnw141292 
1450c5c4113dSnw141292 	return (IDMAP_SUCCESS);
1451c5c4113dSnw141292 }
1452c5c4113dSnw141292 
1453c5c4113dSnw141292 idmap_retcode
1454c5c4113dSnw141292 idmap_name2sid_batch_add1(idmap_query_state_t *state,
1455c5c4113dSnw141292 	const char *name, const char *dname,
1456c5c4113dSnw141292 	char **sid, rid_t *rid, int *sid_type, idmap_retcode *rc)
1457c5c4113dSnw141292 {
1458c5c4113dSnw141292 	idmap_retcode	retcode;
1459c5c4113dSnw141292 	int		flen, samAcctNameLen;
1460c5c4113dSnw141292 	char		*filter = NULL;
1461c5c4113dSnw141292 	char		*basedn = NULL;
1462c5c4113dSnw141292 	char		*cp;
1463c5c4113dSnw141292 
1464c5c4113dSnw141292 	/*
1465c5c4113dSnw141292 	 * Strategy: search [the global catalog] for user/group by
1466c5c4113dSnw141292 	 * sAMAccountName = user/groupname with base DN derived from the
1467c5c4113dSnw141292 	 * domain name.  The objectSid and objectClass of the result are
1468c5c4113dSnw141292 	 * all we need to figure out the SID of the user/group and
1469c5c4113dSnw141292 	 * whether it is a user or a group.
1470c5c4113dSnw141292 	 */
1471c5c4113dSnw141292 
1472c5c4113dSnw141292 	/*
1473c5c4113dSnw141292 	 * Handle optional domain parameter and default domain
1474c5c4113dSnw141292 	 * semantics.  The get a basedn from the domainname.
1475c5c4113dSnw141292 	 */
1476c5c4113dSnw141292 	if (dname == NULL || *dname != '\0') {
1477c5c4113dSnw141292 		/* domain name not given separately */
1478c5c4113dSnw141292 		if ((cp = strchr(name, '@')) == NULL) {
1479c5c4113dSnw141292 			/* nor is the name qualified */
1480c5c4113dSnw141292 			dname = state->qadh->owner->dflt_w2k_dom;
1481c5c4113dSnw141292 			basedn = state->qadh->owner->basedn;
1482c5c4113dSnw141292 			samAcctNameLen = strlen(name);
1483c5c4113dSnw141292 		} else {
1484c5c4113dSnw141292 			/* the name is qualified */
1485c5c4113dSnw141292 			/* LINTED */
1486c5c4113dSnw141292 			samAcctNameLen = cp - name;
1487c5c4113dSnw141292 			dname = cp + 1;
1488c5c4113dSnw141292 		}
1489c5c4113dSnw141292 	}
1490c5c4113dSnw141292 
1491c5c4113dSnw141292 	if (basedn == NULL)
1492c5c4113dSnw141292 		basedn = dns2dn(dname);
1493c5c4113dSnw141292 
1494c5c4113dSnw141292 	/* Assemble filter */
1495c5c4113dSnw141292 	flen = snprintf(NULL, 0, SANFILTER, samAcctNameLen, name) + 1;
1496c5c4113dSnw141292 	if ((filter = (char *)malloc(flen)) == NULL) {
1497c5c4113dSnw141292 		if (basedn != state->qadh->owner->basedn)
1498c5c4113dSnw141292 			free(basedn);
1499c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
1500c5c4113dSnw141292 	}
1501c5c4113dSnw141292 	(void) snprintf(filter, flen, SANFILTER, samAcctNameLen, name);
1502c5c4113dSnw141292 
1503c5c4113dSnw141292 	retcode = idmap_batch_add1(state, 1, filter, basedn,
1504c5c4113dSnw141292 	    sid, NULL, rid, sid_type, rc);
1505c5c4113dSnw141292 
1506c5c4113dSnw141292 	if (basedn != state->qadh->owner->basedn)
1507c5c4113dSnw141292 		free(basedn);
1508c5c4113dSnw141292 	free(filter);
1509c5c4113dSnw141292 
1510c5c4113dSnw141292 	return (retcode);
1511c5c4113dSnw141292 }
1512c5c4113dSnw141292 
1513c5c4113dSnw141292 idmap_retcode
1514c5c4113dSnw141292 idmap_sid2name_batch_add1(idmap_query_state_t *state,
1515c5c4113dSnw141292 	const char *sid, const rid_t *rid,
1516c5c4113dSnw141292 	char **name, char **dname, int *sid_type, idmap_retcode *rc)
1517c5c4113dSnw141292 {
1518c5c4113dSnw141292 	idmap_retcode	retcode;
1519c5c4113dSnw141292 	int		flen, ret;
1520c5c4113dSnw141292 	char		*filter = NULL;
1521c5c4113dSnw141292 	char		cbinsid[MAXHEXBINSID + 1];
1522c5c4113dSnw141292 
1523c5c4113dSnw141292 	/*
1524c5c4113dSnw141292 	 * Strategy: search [the global catalog] for user/group by
1525c5c4113dSnw141292 	 * objectSid = SID with empty base DN.  The DN, sAMAccountName
1526c5c4113dSnw141292 	 * and objectClass of the result are all we need to figure out
1527c5c4113dSnw141292 	 * the name of the SID and whether it is a user, a group or a
1528c5c4113dSnw141292 	 * computer.
1529c5c4113dSnw141292 	 */
1530c5c4113dSnw141292 
1531c5c4113dSnw141292 	ret = idmap_txtsid2hexbinsid(sid, rid, &cbinsid[0], sizeof (cbinsid));
1532c5c4113dSnw141292 	if (ret != 0)
1533c5c4113dSnw141292 		return (IDMAP_ERR_SID);
1534c5c4113dSnw141292 
1535c5c4113dSnw141292 	/* Assemble filter */
1536c5c4113dSnw141292 	flen = snprintf(NULL, 0, OBJECTSIDFILTER, cbinsid) + 1;
1537c5c4113dSnw141292 	if ((filter = (char *)malloc(flen)) == NULL)
1538c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
1539c5c4113dSnw141292 	(void) snprintf(filter, flen, OBJECTSIDFILTER, cbinsid);
1540c5c4113dSnw141292 
1541c5c4113dSnw141292 	retcode = idmap_batch_add1(state, 0, filter, NULL, name, dname,
1542c5c4113dSnw141292 	    NULL, sid_type, rc);
1543c5c4113dSnw141292 
1544c5c4113dSnw141292 	free(filter);
1545c5c4113dSnw141292 
1546c5c4113dSnw141292 	return (retcode);
1547c5c4113dSnw141292 }
1548