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