14520Snw141292 /*
24520Snw141292  * CDDL HEADER START
34520Snw141292  *
44520Snw141292  * The contents of this file are subject to the terms of the
54520Snw141292  * Common Development and Distribution License (the "License").
64520Snw141292  * You may not use this file except in compliance with the License.
74520Snw141292  *
84520Snw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94520Snw141292  * or http://www.opensolaris.org/os/licensing.
104520Snw141292  * See the License for the specific language governing permissions
114520Snw141292  * and limitations under the License.
124520Snw141292  *
134520Snw141292  * When distributing Covered Code, include this CDDL HEADER in each
144520Snw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154520Snw141292  * If applicable, add the following below this CDDL HEADER, with the
164520Snw141292  * fields enclosed by brackets "[]" replaced with your own identifying
174520Snw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
184520Snw141292  *
194520Snw141292  * CDDL HEADER END
204520Snw141292  */
214520Snw141292 
224520Snw141292 /*
235908Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
244520Snw141292  * Use is subject to license terms.
254520Snw141292  */
264520Snw141292 
274520Snw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
284520Snw141292 
294520Snw141292 /*
304520Snw141292  * Processes name2sid & sid2name batched lookups for a given user or
314520Snw141292  * computer from an AD Directory server using GSSAPI authentication
324520Snw141292  */
334520Snw141292 
344520Snw141292 #include <stdio.h>
354520Snw141292 #include <stdlib.h>
364520Snw141292 #include <alloca.h>
374520Snw141292 #include <string.h>
384520Snw141292 #include <strings.h>
394520Snw141292 #include <lber.h>
404520Snw141292 #include <ldap.h>
414520Snw141292 #include <sasl/sasl.h>
424520Snw141292 #include <string.h>
434520Snw141292 #include <ctype.h>
444520Snw141292 #include <pthread.h>
454520Snw141292 #include <synch.h>
464520Snw141292 #include <atomic.h>
474520Snw141292 #include <errno.h>
484520Snw141292 #include <assert.h>
494520Snw141292 #include <limits.h>
505696Snw141292 #include <sys/u8_textprep.h>
514520Snw141292 #include "idmapd.h"
524520Snw141292 
534520Snw141292 /*
544520Snw141292  * Internal data structures for this code
554520Snw141292  */
564520Snw141292 
574520Snw141292 /* Attribute names and filter format strings */
585447Snw141292 #define	SAN		"sAMAccountName"
595447Snw141292 #define	OBJSID		"objectSid"
605447Snw141292 #define	OBJCLASS	"objectClass"
614520Snw141292 #define	SANFILTER	"(sAMAccountName=%.*s)"
625447Snw141292 #define	OBJSIDFILTER	"(objectSid=%s)"
634520Snw141292 
644520Snw141292 /*
654520Snw141292  * This should really be in some <sys/sid.h> file or so; we have a
664520Snw141292  * private version of sid_t, and so must other components of ON until we
674520Snw141292  * rationalize this.
684520Snw141292  */
694520Snw141292 typedef struct sid {
704520Snw141292 	uchar_t		version;
714520Snw141292 	uchar_t		sub_authority_count;
724520Snw141292 	uint64_t	authority;  /* really, 48-bits */
734520Snw141292 	rid_t		sub_authorities[SID_MAX_SUB_AUTHORITIES];
744520Snw141292 } sid_t;
754520Snw141292 
764520Snw141292 /* A single DS */
774520Snw141292 typedef struct ad_host {
784520Snw141292 	struct ad_host		*next;
794520Snw141292 	ad_t			*owner;		/* ad_t to which this belongs */
804520Snw141292 	pthread_mutex_t		lock;
814520Snw141292 	LDAP			*ld;		/* LDAP connection */
824520Snw141292 	uint32_t		ref;		/* ref count */
834520Snw141292 	time_t			idletime;	/* time since last activity */
844520Snw141292 	int			dead;		/* error on LDAP connection */
854520Snw141292 	/*
864520Snw141292 	 * Used to distinguish between different instances of LDAP
874520Snw141292 	 * connections to this same DS.  We need this so we never mix up
884520Snw141292 	 * results for a given msgID from one connection with those of
894520Snw141292 	 * another earlier connection where two batch state structures
904520Snw141292 	 * share this ad_host object but used different LDAP connections
914520Snw141292 	 * to send their LDAP searches.
924520Snw141292 	 */
934520Snw141292 	uint64_t		generation;
944520Snw141292 
954520Snw141292 	/* LDAP DS info */
964520Snw141292 	char			*host;
974520Snw141292 	int			port;
984520Snw141292 
994520Snw141292 	/* hardwired to SASL GSSAPI only for now */
1004520Snw141292 	char			*saslmech;
1014520Snw141292 	unsigned		saslflags;
1024520Snw141292 } ad_host_t;
1034520Snw141292 
1044520Snw141292 /* A set of DSs for a given AD partition; ad_t typedef comes from  adutil.h */
1054520Snw141292 struct ad {
1064520Snw141292 	char			*dflt_w2k_dom;	/* used to qualify bare names */
1074520Snw141292 	pthread_mutex_t		lock;
1084520Snw141292 	uint32_t		ref;
1095317Sjp151216 	ad_host_t		*last_adh;
1104520Snw141292 	idmap_ad_partition_t	partition;	/* Data or global catalog? */
1114520Snw141292 };
1124520Snw141292 
1134520Snw141292 /*
1144520Snw141292  * A place to put the results of a batched (async) query
1154520Snw141292  *
1164520Snw141292  * There is one of these for every query added to a batch object
1174520Snw141292  * (idmap_query_state, see below).
1184520Snw141292  */
1194520Snw141292 typedef struct idmap_q {
1205447Snw141292 	/*
1215447Snw141292 	 * data used for validating search result entries for name->SID
1225731Sbaban 	 * lookups
1235447Snw141292 	 */
1245696Snw141292 	char			*ecanonname;	/* expected canon name */
1255696Snw141292 	char			*edomain;	/* expected domain name */
1265731Sbaban 	int			eunixtype;	/* expected unix type */
1275447Snw141292 	/* results */
1285696Snw141292 	char			**canonname;	/* actual canon name */
1294520Snw141292 	char			**domain;	/* name of domain of object */
1305731Sbaban 	char			**sid;		/* stringified SID */
1315731Sbaban 	rid_t			*rid;		/* RID */
1325731Sbaban 	int			*sid_type;	/* user or group SID? */
1335731Sbaban 	char			**unixname;	/* unixname for name mapping */
134*6386Sjp151216 	char			**dn;		/* DN of entry */
135*6386Sjp151216 	char			**attr;		/* Attr for name mapping */
136*6386Sjp151216 	char			**value;	/* value for name mapping */
1374520Snw141292 	idmap_retcode		*rc;
1385447Snw141292 
1395447Snw141292 	/* lookup state */
1404520Snw141292 	int			msgid;		/* LDAP message ID */
1414520Snw141292 } idmap_q_t;
1424520Snw141292 
1434520Snw141292 /* Batch context structure; typedef is in header file */
1444520Snw141292 struct idmap_query_state {
1454520Snw141292 	idmap_query_state_t	*next;
1464520Snw141292 	int			qcount;		/* how many queries */
1474884Sjp151216 	int			ref_cnt;	/* reference count */
1484884Sjp151216 	pthread_cond_t		cv;		/* Condition wait variable */
1494520Snw141292 	uint32_t		qlastsent;
1504520Snw141292 	uint32_t		qinflight;	/* how many queries in flight */
1514520Snw141292 	uint16_t		qdead;		/* oops, lost LDAP connection */
1524520Snw141292 	ad_host_t		*qadh;		/* LDAP connection */
1534520Snw141292 	uint64_t		qadh_gen;	/* same as qadh->generation */
1545731Sbaban 	const char		*ad_unixuser_attr;
1555731Sbaban 	const char		*ad_unixgroup_attr;
1564520Snw141292 	idmap_q_t		queries[1];	/* array of query results */
1574520Snw141292 };
1584520Snw141292 
1594520Snw141292 /*
1604520Snw141292  * List of query state structs -- needed so we can "route" LDAP results
1614520Snw141292  * to the right context if multiple threads should be using the same
1624520Snw141292  * connection concurrently
1634520Snw141292  */
1644520Snw141292 static idmap_query_state_t	*qstatehead = NULL;
1654520Snw141292 static pthread_mutex_t		qstatelock = PTHREAD_MUTEX_INITIALIZER;
1664520Snw141292 
1674520Snw141292 /*
1684520Snw141292  * List of DSs, needed by the idle connection reaper thread
1694520Snw141292  */
1704520Snw141292 static ad_host_t	*host_head = NULL;
1714644Sbaban static pthread_t	reaperid = 0;
1724520Snw141292 static pthread_mutex_t	adhostlock = PTHREAD_MUTEX_INITIALIZER;
1734520Snw141292 
1744884Sjp151216 
1754884Sjp151216 static void
1764884Sjp151216 idmap_lookup_unlock_batch(idmap_query_state_t **state);
1774884Sjp151216 
1785317Sjp151216 static void
1795317Sjp151216 delete_ds(ad_t *ad, const char *host, int port);
1804884Sjp151216 
1814520Snw141292 /*ARGSUSED*/
1824520Snw141292 static int
1835908Sjp151216 idmap_saslcallback(LDAP *ld, unsigned flags, void *defaults, void *prompts)
1845908Sjp151216 {
1854520Snw141292 	sasl_interact_t	*interact;
1864520Snw141292 
1874520Snw141292 	if (prompts == NULL || flags != LDAP_SASL_INTERACTIVE)
1884520Snw141292 		return (LDAP_PARAM_ERROR);
1894520Snw141292 
1904520Snw141292 	/* There should be no extra arguemnts for SASL/GSSAPI authentication */
1914520Snw141292 	for (interact = prompts; interact->id != SASL_CB_LIST_END;
1925908Sjp151216 	    interact++) {
1934520Snw141292 		interact->result = NULL;
1944520Snw141292 		interact->len = 0;
1954520Snw141292 	}
1964520Snw141292 	return (LDAP_SUCCESS);
1974520Snw141292 }
1984520Snw141292 
1994520Snw141292 
2004520Snw141292 /*
2014520Snw141292  * Turn "dc=foo,dc=bar,dc=com" into "foo.bar.com"; ignores any other
2025447Snw141292  * attributes (CN, etc...).  We don't need the reverse, for now.
2034520Snw141292  */
2044520Snw141292 static
2054520Snw141292 char *
2064520Snw141292 dn2dns(const char *dn)
2074520Snw141292 {
2084520Snw141292 	char **rdns = NULL;
2094520Snw141292 	char **attrs = NULL;
2104520Snw141292 	char **labels = NULL;
2114520Snw141292 	char *dns = NULL;
2124520Snw141292 	char **rdn, **attr, **label;
2134520Snw141292 	int maxlabels = 5;
2144520Snw141292 	int nlabels = 0;
2154520Snw141292 	int dnslen;
2164520Snw141292 
2174520Snw141292 	/*
2184520Snw141292 	 * There is no reverse of ldap_dns_to_dn() in our libldap, so we
2194520Snw141292 	 * have to do the hard work here for now.
2204520Snw141292 	 */
2214520Snw141292 
2224520Snw141292 	/*
2234520Snw141292 	 * This code is much too liberal: it looks for "dc" attributes
2244520Snw141292 	 * in all RDNs of the DN.  In theory this could cause problems
2254520Snw141292 	 * if people were to use "dc" in nodes other than the root of
2264520Snw141292 	 * the tree, but in practice noone, least of all Active
2274520Snw141292 	 * Directory, does that.
2284520Snw141292 	 *
2294520Snw141292 	 * On the other hand, this code is much too conservative: it
2304520Snw141292 	 * does not make assumptions about ldap_explode_dn(), and _that_
2314520Snw141292 	 * is the true for looking at every attr of every RDN.
2324520Snw141292 	 *
2334520Snw141292 	 * Since we only ever look at dc and those must be DNS labels,
2344520Snw141292 	 * at least until we get around to supporting IDN here we
2354520Snw141292 	 * shouldn't see escaped labels from AD nor from libldap, though
2364520Snw141292 	 * the spec (RFC2253) does allow libldap to escape things that
2374520Snw141292 	 * don't need escaping -- if that should ever happen then
2384520Snw141292 	 * libldap will need a spanking, and we can take care of that.
2394520Snw141292 	 */
2404520Snw141292 
2414520Snw141292 	/* Explode a DN into RDNs */
2424520Snw141292 	if ((rdns = ldap_explode_dn(dn, 0)) == NULL)
2434520Snw141292 		return (NULL);
2444520Snw141292 
2454520Snw141292 	labels = calloc(maxlabels + 1, sizeof (char *));
2464520Snw141292 	label = labels;
2474520Snw141292 
2484520Snw141292 	for (rdn = rdns; *rdn != NULL; rdn++) {
2494520Snw141292 		if (attrs != NULL)
2504520Snw141292 			ldap_value_free(attrs);
2514520Snw141292 
2524520Snw141292 		/* Explode each RDN, look for DC attr, save val as DNS label */
2534520Snw141292 		if ((attrs = ldap_explode_rdn(rdn[0], 0)) == NULL)
2544520Snw141292 			goto done;
2554520Snw141292 
2564520Snw141292 		for (attr = attrs; *attr != NULL; attr++) {
2574520Snw141292 			if (strncasecmp(*attr, "dc=", 3) != 0)
2584520Snw141292 				continue;
2594520Snw141292 
2604520Snw141292 			/* Found a DNS label */
2614520Snw141292 			labels[nlabels++] = strdup((*attr) + 3);
2624520Snw141292 
2634520Snw141292 			if (nlabels == maxlabels) {
2644520Snw141292 				char **tmp;
2654520Snw141292 				tmp = realloc(labels,
2664520Snw141292 				    sizeof (char *) * (maxlabels + 1));
2674520Snw141292 
2684520Snw141292 				if (tmp == NULL)
2694520Snw141292 					goto done;
2704520Snw141292 
2714520Snw141292 				labels = tmp;
2724520Snw141292 				labels[nlabels] = NULL;
2734520Snw141292 			}
2744520Snw141292 
2754520Snw141292 			/* There should be just one DC= attr per-RDN */
2764520Snw141292 			break;
2774520Snw141292 		}
2784520Snw141292 	}
2794520Snw141292 
2804520Snw141292 	/*
2814520Snw141292 	 * Got all the labels, now join with '.'
2824520Snw141292 	 *
2834520Snw141292 	 * We need room for nlabels - 1 periods ('.'), one nul
2844520Snw141292 	 * terminator, and the strlen() of each label.
2854520Snw141292 	 */
2864520Snw141292 	dnslen = nlabels;
2874520Snw141292 	for (label = labels; *label != NULL; label++)
2884520Snw141292 		dnslen += strlen(*label);
2894520Snw141292 
2904520Snw141292 	if ((dns = malloc(dnslen)) == NULL)
2914520Snw141292 		goto done;
2924520Snw141292 
2934520Snw141292 	*dns = '\0';
2944520Snw141292 
2954520Snw141292 	for (label = labels; *label != NULL; label++) {
2964520Snw141292 		(void) strlcat(dns, *label, dnslen);
2974520Snw141292 		/*
2984520Snw141292 		 * NOTE: the last '.' won't be appended -- there's no room
2994520Snw141292 		 * for it!
3004520Snw141292 		 */
3014520Snw141292 		(void) strlcat(dns, ".", dnslen);
3024520Snw141292 	}
3034520Snw141292 
3044520Snw141292 done:
3054520Snw141292 	if (labels != NULL) {
3064520Snw141292 		for (label = labels; *label != NULL; label++)
3074520Snw141292 			free(*label);
3084520Snw141292 		free(labels);
3094520Snw141292 	}
3104520Snw141292 	if (attrs != NULL)
3114520Snw141292 		ldap_value_free(attrs);
3124520Snw141292 	if (rdns != NULL)
3134520Snw141292 		ldap_value_free(rdns);
3144520Snw141292 
3154520Snw141292 	return (dns);
3164520Snw141292 }
3174520Snw141292 
3184520Snw141292 /*
3194520Snw141292  * Keep connection management simple for now, extend or replace later
3204520Snw141292  * with updated libsldap code.
3214520Snw141292  */
3224520Snw141292 #define	ADREAPERSLEEP	60
3234520Snw141292 #define	ADCONN_TIME	300
3244520Snw141292 
3254520Snw141292 /*
3264520Snw141292  * Idle connection reaping side of connection management
3274520Snw141292  *
3284520Snw141292  * Every minute wake up and look for connections that have been idle for
3294520Snw141292  * five minutes or more and close them.
3304520Snw141292  */
3314520Snw141292 /*ARGSUSED*/
3324520Snw141292 static
3334520Snw141292 void
3344520Snw141292 adreaper(void *arg)
3354520Snw141292 {
3364520Snw141292 	ad_host_t	*adh;
3374520Snw141292 	time_t		now;
3384520Snw141292 	timespec_t	ts;
3394520Snw141292 
3404520Snw141292 	ts.tv_sec = ADREAPERSLEEP;
3414520Snw141292 	ts.tv_nsec = 0;
3424520Snw141292 
3434520Snw141292 	for (;;) {
3444520Snw141292 		/*
3454520Snw141292 		 * nanosleep(3RT) is thead-safe (no SIGALRM) and more
3464520Snw141292 		 * portable than usleep(3C)
3474520Snw141292 		 */
3484520Snw141292 		(void) nanosleep(&ts, NULL);
3494520Snw141292 		(void) pthread_mutex_lock(&adhostlock);
3504520Snw141292 		now = time(NULL);
3514520Snw141292 		for (adh = host_head; adh != NULL; adh = adh->next) {
3524520Snw141292 			(void) pthread_mutex_lock(&adh->lock);
3534520Snw141292 			if (adh->ref == 0 && adh->idletime != 0 &&
3544520Snw141292 			    adh->idletime + ADCONN_TIME < now) {
3554520Snw141292 				if (adh->ld) {
3564520Snw141292 					(void) ldap_unbind(adh->ld);
3574520Snw141292 					adh->ld = NULL;
3584520Snw141292 					adh->idletime = 0;
3594520Snw141292 					adh->ref = 0;
3604520Snw141292 				}
3614520Snw141292 			}
3624520Snw141292 			(void) pthread_mutex_unlock(&adh->lock);
3634520Snw141292 		}
3644520Snw141292 		(void) pthread_mutex_unlock(&adhostlock);
3654520Snw141292 	}
3664520Snw141292 }
3674520Snw141292 
3684520Snw141292 int
3694520Snw141292 idmap_ad_alloc(ad_t **new_ad, const char *default_domain,
3704520Snw141292 		idmap_ad_partition_t part)
3714520Snw141292 {
3724520Snw141292 	ad_t *ad;
3734520Snw141292 
3744520Snw141292 	*new_ad = NULL;
3754520Snw141292 
3764520Snw141292 	if ((default_domain == NULL || *default_domain == '\0') &&
3774520Snw141292 	    part != IDMAP_AD_GLOBAL_CATALOG)
3784520Snw141292 		return (-1);
3794520Snw141292 
3804520Snw141292 	if ((ad = calloc(1, sizeof (ad_t))) == NULL)
3814520Snw141292 		return (-1);
3824520Snw141292 
3834520Snw141292 	ad->ref = 1;
3844520Snw141292 	ad->partition = part;
3854520Snw141292 
3864520Snw141292 	if (default_domain == NULL)
3874520Snw141292 		default_domain = "";
3884520Snw141292 
3894520Snw141292 	if ((ad->dflt_w2k_dom = strdup(default_domain)) == NULL)
3904520Snw141292 		goto err;
3914520Snw141292 
3924520Snw141292 	if (pthread_mutex_init(&ad->lock, NULL) != 0)
3934520Snw141292 		goto err;
3944520Snw141292 
3954520Snw141292 	*new_ad = ad;
3964520Snw141292 
3974520Snw141292 	return (0);
3984520Snw141292 err:
3994520Snw141292 	if (ad->dflt_w2k_dom != NULL)
4004520Snw141292 		free(ad->dflt_w2k_dom);
4014520Snw141292 	free(ad);
4024520Snw141292 	return (-1);
4034520Snw141292 }
4044520Snw141292 
4054520Snw141292 
4064520Snw141292 void
4074520Snw141292 idmap_ad_free(ad_t **ad)
4084520Snw141292 {
4094520Snw141292 	ad_host_t *p;
4105317Sjp151216 	ad_host_t *prev;
4114520Snw141292 
4124520Snw141292 	if (ad == NULL || *ad == NULL)
4134520Snw141292 		return;
4144520Snw141292 
4154520Snw141292 	(void) pthread_mutex_lock(&(*ad)->lock);
4164520Snw141292 
4174520Snw141292 	if (atomic_dec_32_nv(&(*ad)->ref) > 0) {
4184520Snw141292 		(void) pthread_mutex_unlock(&(*ad)->lock);
4194520Snw141292 		*ad = NULL;
4204520Snw141292 		return;
4214520Snw141292 	}
4224520Snw141292 
4235317Sjp151216 	(void) pthread_mutex_lock(&adhostlock);
4245317Sjp151216 	prev = NULL;
4255317Sjp151216 	p = host_head;
4265317Sjp151216 	while (p != NULL) {
4275317Sjp151216 		if (p->owner != (*ad)) {
4285317Sjp151216 			prev = p;
4295317Sjp151216 			p = p->next;
4304520Snw141292 			continue;
4315317Sjp151216 		} else {
4325317Sjp151216 			delete_ds((*ad), p->host, p->port);
4335317Sjp151216 			if (prev == NULL)
4345317Sjp151216 				p = host_head;
4355317Sjp151216 			else
4365317Sjp151216 				p = prev->next;
4375317Sjp151216 		}
4384520Snw141292 	}
4395317Sjp151216 	(void) pthread_mutex_unlock(&adhostlock);
4404520Snw141292 
4414520Snw141292 	(void) pthread_mutex_unlock(&(*ad)->lock);
4424520Snw141292 	(void) pthread_mutex_destroy(&(*ad)->lock);
4434520Snw141292 
4445447Snw141292 	free((*ad)->dflt_w2k_dom);
4454520Snw141292 	free(*ad);
4464520Snw141292 
4474520Snw141292 	*ad = NULL;
4484520Snw141292 }
4494520Snw141292 
4505317Sjp151216 
4514520Snw141292 static
4524520Snw141292 int
4535968Snw141292 idmap_open_conn(ad_host_t *adh, int timeoutsecs)
4544520Snw141292 {
4555317Sjp151216 	int zero = 0;
4565317Sjp151216 	int ldversion, rc;
4575968Snw141292 	int timeoutms = timeoutsecs * 1000;
4585317Sjp151216 
4595317Sjp151216 	if (adh == NULL)
4605317Sjp151216 		return (0);
4614520Snw141292 
4625317Sjp151216 	(void) pthread_mutex_lock(&adh->lock);
4635317Sjp151216 
4645317Sjp151216 	if (!adh->dead && adh->ld != NULL)
4655317Sjp151216 		/* done! */
4665317Sjp151216 		goto out;
4675317Sjp151216 
4685317Sjp151216 	if (adh->ld != NULL) {
4694520Snw141292 		(void) ldap_unbind(adh->ld);
4704520Snw141292 		adh->ld = NULL;
4714520Snw141292 	}
4724520Snw141292 
4735317Sjp151216 	atomic_inc_64(&adh->generation);
4744520Snw141292 
4755317Sjp151216 	/* Open and bind an LDAP connection */
4765317Sjp151216 	adh->ld = ldap_init(adh->host, adh->port);
4775317Sjp151216 	if (adh->ld == NULL) {
4785317Sjp151216 		idmapdlog(LOG_INFO, "ldap_init() to server "
4795317Sjp151216 		    "%s port %d failed. (%s)", adh->host,
4805317Sjp151216 		    adh->port, strerror(errno));
4815317Sjp151216 		goto out;
4825317Sjp151216 	}
4835317Sjp151216 	ldversion = LDAP_VERSION3;
4845317Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_OPT_PROTOCOL_VERSION, &ldversion);
4855317Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
4865317Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_OPT_TIMELIMIT, &zero);
4875317Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_OPT_SIZELIMIT, &zero);
4885317Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_X_OPT_CONNECT_TIMEOUT, &timeoutms);
4895317Sjp151216 	(void) ldap_set_option(adh->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
4905317Sjp151216 	rc = ldap_sasl_interactive_bind_s(adh->ld, "" /* binddn */,
4915317Sjp151216 	    adh->saslmech, NULL, NULL, adh->saslflags, &idmap_saslcallback,
4925317Sjp151216 	    NULL);
4934520Snw141292 
4945317Sjp151216 	if (rc != LDAP_SUCCESS) {
4955317Sjp151216 		(void) ldap_unbind(adh->ld);
4965317Sjp151216 		adh->ld = NULL;
4975317Sjp151216 		idmapdlog(LOG_INFO, "ldap_sasl_interactive_bind_s() to server "
4985317Sjp151216 		    "%s port %d failed. (%s)", adh->host, adh->port,
4995317Sjp151216 		    ldap_err2string(rc));
5004520Snw141292 	}
5014520Snw141292 
5025317Sjp151216 	idmapdlog(LOG_DEBUG, "Using global catalog server %s:%d",
5035317Sjp151216 	    adh->host, adh->port);
5044520Snw141292 
5055317Sjp151216 out:
5065317Sjp151216 	if (adh->ld != NULL) {
5075317Sjp151216 		atomic_inc_32(&adh->ref);
5085317Sjp151216 		adh->idletime = time(NULL);
5095317Sjp151216 		adh->dead = 0;
5105317Sjp151216 		(void) pthread_mutex_unlock(&adh->lock);
5115317Sjp151216 		return (1);
5125317Sjp151216 	}
5135317Sjp151216 
5145317Sjp151216 	(void) pthread_mutex_unlock(&adh->lock);
5155317Sjp151216 	return (0);
5164520Snw141292 }
5174520Snw141292 
5184520Snw141292 
5194520Snw141292 /*
5204520Snw141292  * Connection management: find an open connection or open one
5214520Snw141292  */
5224520Snw141292 static
5234520Snw141292 ad_host_t *
5245317Sjp151216 idmap_get_conn(ad_t *ad)
5254520Snw141292 {
5264520Snw141292 	ad_host_t	*adh = NULL;
5275968Snw141292 	int		tries;
5285968Snw141292 	int		dscount = 0;
5295968Snw141292 	int		timeoutsecs = IDMAPD_LDAP_OPEN_TIMEOUT;
5304520Snw141292 
5315317Sjp151216 retry:
5324520Snw141292 	(void) pthread_mutex_lock(&adhostlock);
5334520Snw141292 
5345968Snw141292 	if (host_head == NULL) {
5355968Snw141292 		(void) pthread_mutex_unlock(&adhostlock);
5365317Sjp151216 		goto out;
5375968Snw141292 	}
5385317Sjp151216 
5395968Snw141292 	if (dscount == 0) {
5405968Snw141292 		/*
5415968Snw141292 		 * First try: count the number of DSes.
5425968Snw141292 		 *
5435968Snw141292 		 * Integer overflow is not an issue -- we can't have so many
5445968Snw141292 		 * DSes because they won't fit even DNS over TCP, and SMF
5455968Snw141292 		 * shouldn't let you set so many.
5465968Snw141292 		 */
5475968Snw141292 		for (adh = host_head, tries = 0; adh != NULL; adh = adh->next) {
5485968Snw141292 			if (adh->owner == ad)
5495968Snw141292 				dscount++;
5505968Snw141292 		}
5515968Snw141292 
5525968Snw141292 		if (dscount == 0) {
5535968Snw141292 			(void) pthread_mutex_unlock(&adhostlock);
5545968Snw141292 			goto out;
5555968Snw141292 		}
5565968Snw141292 
5575968Snw141292 		tries = dscount * 3;	/* three tries per-ds */
5585968Snw141292 
5595968Snw141292 		/*
5605968Snw141292 		 * Begin round-robin at the next DS in the list after the last
5615968Snw141292 		 * one that we had a connection to, else start with the first
5625968Snw141292 		 * DS in the list.
5635968Snw141292 		 */
5645968Snw141292 		adh = ad->last_adh;
5654520Snw141292 	}
5664520Snw141292 
5675317Sjp151216 	/*
5685968Snw141292 	 * Round-robin -- pick the next one on the list; if the list
5695968Snw141292 	 * changes on us, no big deal, we'll just potentially go
5705968Snw141292 	 * around the wrong number of times.
5715317Sjp151216 	 */
5725968Snw141292 	for (;;) {
5736017Snw141292 		if (adh != NULL && adh->ld != NULL && !adh->dead)
5746017Snw141292 			break;
5755968Snw141292 		if (adh == NULL || (adh = adh->next) == NULL)
5765968Snw141292 			adh = host_head;
5775968Snw141292 		if (adh->owner == ad)
5785317Sjp151216 			break;
5795317Sjp151216 	}
5805317Sjp151216 
5815968Snw141292 	ad->last_adh = adh;
5825968Snw141292 	(void) pthread_mutex_unlock(&adhostlock);
5835968Snw141292 
5845968Snw141292 
5855968Snw141292 	/* Found suitable DS, open it if not already opened */
5865968Snw141292 	if (idmap_open_conn(adh, timeoutsecs))
5875968Snw141292 		return (adh);
5885317Sjp151216 
5895968Snw141292 	tries--;
5905968Snw141292 
5915968Snw141292 	if ((tries % dscount) == 0)
5925968Snw141292 		timeoutsecs *= 2;
5935968Snw141292 
5945968Snw141292 	if (tries > 0)
5955968Snw141292 		goto retry;
5964520Snw141292 
5975317Sjp151216 out:
5985968Snw141292 	idmapdlog(LOG_NOTICE, "Couldn't open an LDAP connection to any global "
5995317Sjp151216 	    "catalog server!");
6005317Sjp151216 
6015317Sjp151216 	return (NULL);
6024520Snw141292 }
6034520Snw141292 
6044520Snw141292 static
6054520Snw141292 void
6064520Snw141292 idmap_release_conn(ad_host_t *adh)
6074520Snw141292 {
6084520Snw141292 	(void) pthread_mutex_lock(&adh->lock);
6094520Snw141292 	if (atomic_dec_32_nv(&adh->ref) == 0)
6104520Snw141292 		adh->idletime = time(NULL);
6114520Snw141292 	(void) pthread_mutex_unlock(&adh->lock);
6124520Snw141292 }
6134520Snw141292 
6144520Snw141292 /*
6154520Snw141292  * Take ad_host_config_t information, create a ad_host_t,
6164520Snw141292  * populate it and add it to the list of hosts.
6174520Snw141292  */
6184520Snw141292 
6194520Snw141292 int
6204520Snw141292 idmap_add_ds(ad_t *ad, const char *host, int port)
6214520Snw141292 {
6224520Snw141292 	ad_host_t	*p;
6234520Snw141292 	ad_host_t	*new = NULL;
6244520Snw141292 	int		ret = -1;
6254520Snw141292 
6264520Snw141292 	if (port == 0)
6274520Snw141292 		port = (int)ad->partition;
6284520Snw141292 
6294520Snw141292 	(void) pthread_mutex_lock(&adhostlock);
6304520Snw141292 	for (p = host_head; p != NULL; p = p->next) {
6314520Snw141292 		if (p->owner != ad)
6324520Snw141292 			continue;
6334520Snw141292 
6344520Snw141292 		if (strcmp(host, p->host) == 0 && p->port == port) {
6354520Snw141292 			/* already added */
6365317Sjp151216 			ret = 0;
6374520Snw141292 			goto err;
6384520Snw141292 		}
6394520Snw141292 	}
6404520Snw141292 
6414520Snw141292 	/* add new entry */
6424520Snw141292 	new = (ad_host_t *)calloc(1, sizeof (ad_host_t));
6434520Snw141292 	if (new == NULL)
6444520Snw141292 		goto err;
6454520Snw141292 	new->owner = ad;
6464520Snw141292 	new->port = port;
6474520Snw141292 	new->dead = 0;
6484520Snw141292 	if ((new->host = strdup(host)) == NULL)
6494520Snw141292 		goto err;
6504520Snw141292 
6514520Snw141292 	/* default to SASL GSSAPI only for now */
6524520Snw141292 	new->saslflags = LDAP_SASL_INTERACTIVE;
6534520Snw141292 	new->saslmech = "GSSAPI";
6544520Snw141292 
6554520Snw141292 	if ((ret = pthread_mutex_init(&new->lock, NULL)) != 0) {
6564520Snw141292 		free(new->host);
6574520Snw141292 		new->host = NULL;
6584520Snw141292 		errno = ret;
6594520Snw141292 		ret = -1;
6604520Snw141292 		goto err;
6614520Snw141292 	}
6624520Snw141292 
6634520Snw141292 	/* link in */
6644520Snw141292 	new->next = host_head;
6654520Snw141292 	host_head = new;
6664520Snw141292 
6674520Snw141292 	/* Start reaper if it doesn't exist */
6684520Snw141292 	if (reaperid == 0)
6694520Snw141292 		(void) pthread_create(&reaperid, NULL,
6704520Snw141292 		    (void *(*)(void *))adreaper, (void *)NULL);
6714520Snw141292 
6724520Snw141292 err:
6734520Snw141292 	(void) pthread_mutex_unlock(&adhostlock);
6744520Snw141292 
6754520Snw141292 	if (ret != 0 && new != NULL) {
6764520Snw141292 		if (new->host != NULL) {
6774520Snw141292 			(void) pthread_mutex_destroy(&new->lock);
6784520Snw141292 			free(new->host);
6794520Snw141292 		}
6804520Snw141292 		free(new);
6814520Snw141292 	}
6824520Snw141292 
6834520Snw141292 	return (ret);
6844520Snw141292 }
6854520Snw141292 
6864520Snw141292 /*
6875317Sjp151216  * Free a DS configuration.
6885317Sjp151216  * Caller must lock the adhostlock mutex
6894520Snw141292  */
6905317Sjp151216 static void
6915317Sjp151216 delete_ds(ad_t *ad, const char *host, int port)
6924520Snw141292 {
6934520Snw141292 	ad_host_t	**p, *q;
6944520Snw141292 
6954520Snw141292 	for (p = &host_head; *p != NULL; p = &((*p)->next)) {
6964520Snw141292 		if ((*p)->owner != ad || strcmp(host, (*p)->host) != 0 ||
6974520Snw141292 		    (*p)->port != port)
6984520Snw141292 			continue;
6994520Snw141292 		/* found */
7005317Sjp151216 		if ((*p)->ref > 0)
7014520Snw141292 			break;	/* still in use */
7024520Snw141292 
7034520Snw141292 		q = *p;
7044520Snw141292 		*p = (*p)->next;
7054520Snw141292 
7064520Snw141292 		(void) pthread_mutex_destroy(&q->lock);
7074520Snw141292 
7084520Snw141292 		if (q->ld)
7094520Snw141292 			(void) ldap_unbind(q->ld);
7104520Snw141292 		if (q->host)
7114520Snw141292 			free(q->host);
7124520Snw141292 		free(q);
7134520Snw141292 		break;
7144520Snw141292 	}
7155317Sjp151216 
7164520Snw141292 }
7174520Snw141292 
7185317Sjp151216 
7194520Snw141292 /*
7204520Snw141292  * Convert a binary SID in a BerValue to a sid_t
7214520Snw141292  */
7224520Snw141292 static
7234520Snw141292 int
7244520Snw141292 idmap_getsid(BerValue *bval, sid_t *sidp)
7254520Snw141292 {
7264520Snw141292 	int		i, j;
7274520Snw141292 	uchar_t		*v;
7284520Snw141292 	uint32_t	a;
7294520Snw141292 
7304520Snw141292 	/*
7314520Snw141292 	 * The binary format of a SID is as follows:
7324520Snw141292 	 *
7334520Snw141292 	 * byte #0: version, always 0x01
7344520Snw141292 	 * byte #1: RID count, always <= 0x0f
7354520Snw141292 	 * bytes #2-#7: SID authority, big-endian 48-bit unsigned int
7364520Snw141292 	 *
7374520Snw141292 	 * followed by RID count RIDs, each a little-endian, unsigned
7384520Snw141292 	 * 32-bit int.
7394520Snw141292 	 */
7404520Snw141292 	/*
7414520Snw141292 	 * Sanity checks: must have at least one RID, version must be
7424520Snw141292 	 * 0x01, and the length must be 8 + rid count * 4
7434520Snw141292 	 */
7444520Snw141292 	if (bval->bv_len > 8 && bval->bv_val[0] == 0x01 &&
7454520Snw141292 	    bval->bv_len == 1 + 1 + 6 + bval->bv_val[1] * 4) {
7464520Snw141292 		v = (uchar_t *)bval->bv_val;
7474520Snw141292 		sidp->version = v[0];
7484520Snw141292 		sidp->sub_authority_count = v[1];
7494520Snw141292 		sidp->authority =
7504520Snw141292 		    /* big endian -- so start from the left */
7514520Snw141292 		    ((u_longlong_t)v[2] << 40) |
7524520Snw141292 		    ((u_longlong_t)v[3] << 32) |
7534520Snw141292 		    ((u_longlong_t)v[4] << 24) |
7544520Snw141292 		    ((u_longlong_t)v[5] << 16) |
7554520Snw141292 		    ((u_longlong_t)v[6] << 8) |
7564520Snw141292 		    (u_longlong_t)v[7];
7574520Snw141292 		for (i = 0; i < sidp->sub_authority_count; i++) {
7584520Snw141292 			j = 8 + (i * 4);
7594520Snw141292 			/* little endian -- so start from the right */
7604520Snw141292 			a = (v[j + 3] << 24) | (v[j + 2] << 16) |
7614520Snw141292 			    (v[j + 1] << 8) | (v[j]);
7624520Snw141292 			sidp->sub_authorities[i] = a;
7634520Snw141292 		}
7644520Snw141292 		return (0);
7654520Snw141292 	}
7664520Snw141292 	return (-1);
7674520Snw141292 }
7684520Snw141292 
7694520Snw141292 /*
7704520Snw141292  * Convert a sid_t to S-1-...
7714520Snw141292  */
7724520Snw141292 static
7734520Snw141292 char *
7744520Snw141292 idmap_sid2txt(sid_t *sidp)
7754520Snw141292 {
7764520Snw141292 	int	rlen, i, len;
7774520Snw141292 	char	*str, *cp;
7784520Snw141292 
7794520Snw141292 	if (sidp->version != 1)
7804520Snw141292 		return (NULL);
7814520Snw141292 
7824520Snw141292 	len = sizeof ("S-1-") - 1;
7834520Snw141292 
7844520Snw141292 	/*
7854520Snw141292 	 * We could optimize like so, but, why?
7864520Snw141292 	 *	if (sidp->authority < 10)
7874520Snw141292 	 *		len += 2;
7884520Snw141292 	 *	else if (sidp->authority < 100)
7894520Snw141292 	 *		len += 3;
7904520Snw141292 	 *	else
7914520Snw141292 	 *		len += snprintf(NULL, 0"%llu", sidp->authority);
7924520Snw141292 	 */
7934520Snw141292 	len += snprintf(NULL, 0, "%llu", sidp->authority);
7944520Snw141292 
7954520Snw141292 	/* Max length of a uint32_t printed out in ASCII is 10 bytes */
7964520Snw141292 	len += 1 + (sidp->sub_authority_count + 1) * 10;
7974520Snw141292 
7984520Snw141292 	if ((cp = str = malloc(len)) == NULL)
7994520Snw141292 		return (NULL);
8004520Snw141292 
8014520Snw141292 	rlen = snprintf(str, len, "S-1-%llu", sidp->authority);
8024520Snw141292 
8034520Snw141292 	cp += rlen;
8044520Snw141292 	len -= rlen;
8054520Snw141292 
8064520Snw141292 	for (i = 0; i < sidp->sub_authority_count; i++) {
8074520Snw141292 		assert(len > 0);
8084520Snw141292 		rlen = snprintf(cp, len, "-%u", sidp->sub_authorities[i]);
8094520Snw141292 		cp += rlen;
8104520Snw141292 		len -= rlen;
8114520Snw141292 		assert(len >= 0);
8124520Snw141292 	}
8134520Snw141292 
8144520Snw141292 	return (str);
8154520Snw141292 }
8164520Snw141292 
8174520Snw141292 /*
8184520Snw141292  * Convert a sid_t to on-the-wire encoding
8194520Snw141292  */
8204520Snw141292 static
8214520Snw141292 int
8224520Snw141292 idmap_sid2binsid(sid_t *sid, uchar_t *binsid, int binsidlen)
8234520Snw141292 {
8244520Snw141292 	uchar_t		*p;
8254520Snw141292 	int		i;
8264520Snw141292 	uint64_t	a;
8274520Snw141292 	uint32_t	r;
8284520Snw141292 
8294520Snw141292 	if (sid->version != 1 ||
8304520Snw141292 	    binsidlen != (1 + 1 + 6 + sid->sub_authority_count * 4))
8314520Snw141292 		return (-1);
8324520Snw141292 
8334520Snw141292 	p = binsid;
8344520Snw141292 	*p++ = 0x01;		/* version */
8354520Snw141292 	/* sub authority count */
8364520Snw141292 	*p++ = sid->sub_authority_count;
8374520Snw141292 	/* Authority */
8384520Snw141292 	a = sid->authority;
8394520Snw141292 	/* big-endian -- start from left */
8404520Snw141292 	*p++ = (a >> 40) & 0xFF;
8414520Snw141292 	*p++ = (a >> 32) & 0xFF;
8424520Snw141292 	*p++ = (a >> 24) & 0xFF;
8434520Snw141292 	*p++ = (a >> 16) & 0xFF;
8444520Snw141292 	*p++ = (a >> 8) & 0xFF;
8454520Snw141292 	*p++ = a & 0xFF;
8464520Snw141292 
8474520Snw141292 	/* sub-authorities */
8484520Snw141292 	for (i = 0; i < sid->sub_authority_count; i++) {
8494520Snw141292 		r = sid->sub_authorities[i];
8504520Snw141292 		/* little-endian -- start from right */
8514520Snw141292 		*p++ = (r & 0x000000FF);
8524520Snw141292 		*p++ = (r & 0x0000FF00) >> 8;
8534520Snw141292 		*p++ = (r & 0x00FF0000) >> 16;
8544520Snw141292 		*p++ = (r & 0xFF000000) >> 24;
8554520Snw141292 	}
8564520Snw141292 
8574520Snw141292 	return (0);
8584520Snw141292 }
8594520Snw141292 
8604520Snw141292 /*
8614520Snw141292  * Convert a stringified SID (S-1-...) into a hex-encoded version of the
8624520Snw141292  * on-the-wire encoding, but with each pair of hex digits pre-pended
8634520Snw141292  * with a '\', so we can pass this to libldap.
8644520Snw141292  */
8654520Snw141292 static
8664520Snw141292 int
8674520Snw141292 idmap_txtsid2hexbinsid(const char *txt, const rid_t *rid,
8684520Snw141292 	char *hexbinsid, int hexbinsidlen)
8694520Snw141292 {
8704520Snw141292 	sid_t		sid = { 0 };
8714520Snw141292 	int		i, j;
8724520Snw141292 	const char	*cp;
8734520Snw141292 	char		*ecp;
8744520Snw141292 	u_longlong_t	a;
8754520Snw141292 	unsigned long	r;
8764520Snw141292 	uchar_t		*binsid, b, hb;
8774520Snw141292 
8784520Snw141292 	/* Only version 1 SIDs please */
8794520Snw141292 	if (strncmp(txt, "S-1-", strlen("S-1-")) != 0)
8804520Snw141292 		return (-1);
8814520Snw141292 
8824520Snw141292 	if (strlen(txt) < (strlen("S-1-") + 1))
8834520Snw141292 		return (-1);
8844520Snw141292 
8854520Snw141292 	/* count '-'s */
8864520Snw141292 	for (j = 0, cp = strchr(txt, '-');
8874520Snw141292 	    cp != NULL && *cp != '\0';
8884520Snw141292 	    j++, cp = strchr(cp + 1, '-')) {
8894520Snw141292 		/* can't end on a '-' */
8904520Snw141292 		if (*(cp + 1) == '\0')
8914520Snw141292 			return (-1);
8924520Snw141292 	}
8934520Snw141292 
8944864Sbaban 	/* Adjust count for version and authority */
8954864Sbaban 	j -= 2;
8964864Sbaban 
8974864Sbaban 	/* we know the version number and RID count */
8984864Sbaban 	sid.version = 1;
8994864Sbaban 	sid.sub_authority_count = (rid != NULL) ? j + 1 : j;
9004864Sbaban 
9014520Snw141292 	/* must have at least one RID, but not too many */
9024864Sbaban 	if (sid.sub_authority_count < 1 ||
9034864Sbaban 	    sid.sub_authority_count > SID_MAX_SUB_AUTHORITIES)
9044520Snw141292 		return (-1);
9054520Snw141292 
9064520Snw141292 	/* check that we only have digits and '-' */
9074520Snw141292 	if (strspn(txt + 1, "0123456789-") < (strlen(txt) - 1))
9084520Snw141292 		return (-1);
9094520Snw141292 
9104520Snw141292 	cp = txt + strlen("S-1-");
9114520Snw141292 
9124520Snw141292 	/* 64-bit safe parsing of unsigned 48-bit authority value */
9134520Snw141292 	errno = 0;
9144520Snw141292 	a = strtoull(cp, &ecp, 10);
9154520Snw141292 
9164520Snw141292 	/* errors parsing the authority or too many bits */
9174520Snw141292 	if (cp == ecp || (a == 0 && errno == EINVAL) ||
9184520Snw141292 	    (a == ULLONG_MAX && errno == ERANGE) ||
9194520Snw141292 	    (a & 0x0000ffffffffffffULL) != a)
9204520Snw141292 		return (-1);
9214520Snw141292 
9224520Snw141292 	cp = ecp;
9234520Snw141292 
9244520Snw141292 	sid.authority = (uint64_t)a;
9254520Snw141292 
9264864Sbaban 	for (i = 0; i < j; i++) {
9274520Snw141292 		if (*cp++ != '-')
9284520Snw141292 			return (-1);
9294520Snw141292 		/* 64-bit safe parsing of unsigned 32-bit RID */
9304520Snw141292 		errno = 0;
9314520Snw141292 		r = strtoul(cp, &ecp, 10);
9324520Snw141292 		/* errors parsing the RID or too many bits */
9334520Snw141292 		if (cp == ecp || (r == 0 && errno == EINVAL) ||
9344520Snw141292 		    (r == ULONG_MAX && errno == ERANGE) ||
9354520Snw141292 		    (r & 0xffffffffUL) != r)
9364520Snw141292 			return (-1);
9374520Snw141292 		sid.sub_authorities[i] = (uint32_t)r;
9384520Snw141292 		cp = ecp;
9394520Snw141292 	}
9404520Snw141292 
9414520Snw141292 	/* check that all of the string SID has been consumed */
9424520Snw141292 	if (*cp != '\0')
9434520Snw141292 		return (-1);
9444520Snw141292 
9454864Sbaban 	if (rid != NULL)
9464864Sbaban 		sid.sub_authorities[j] = *rid;
9474520Snw141292 
9484520Snw141292 	j = 1 + 1 + 6 + sid.sub_authority_count * 4;
9494520Snw141292 
9504520Snw141292 	if (hexbinsidlen < (j * 3))
9514520Snw141292 		return (-2);
9524520Snw141292 
9534520Snw141292 	/* binary encode the SID */
9544520Snw141292 	binsid = (uchar_t *)alloca(j);
9554520Snw141292 	(void) idmap_sid2binsid(&sid, binsid, j);
9564520Snw141292 
9574520Snw141292 	/* hex encode, with a backslash before each byte */
9584520Snw141292 	for (ecp = hexbinsid, i = 0; i < j; i++) {
9594520Snw141292 		b = binsid[i];
9604520Snw141292 		*ecp++ = '\\';
9614520Snw141292 		hb = (b >> 4) & 0xF;
9624520Snw141292 		*ecp++ = (hb <= 0x9 ? hb + '0' : hb - 10 + 'A');
9634520Snw141292 		hb = b & 0xF;
9644520Snw141292 		*ecp++ = (hb <= 0x9 ? hb + '0' : hb - 10 + 'A');
9654520Snw141292 	}
9664520Snw141292 	*ecp = '\0';
9674520Snw141292 
9684520Snw141292 	return (0);
9694520Snw141292 }
9704520Snw141292 
9714520Snw141292 static
9724520Snw141292 char *
9734520Snw141292 convert_bval2sid(BerValue *bval, rid_t *rid)
9744520Snw141292 {
9754520Snw141292 	sid_t	sid;
9764520Snw141292 
9774520Snw141292 	if (idmap_getsid(bval, &sid) < 0)
9784520Snw141292 		return (NULL);
9794520Snw141292 
9804520Snw141292 	/*
9814520Snw141292 	 * If desired and if the SID is what should be a domain/computer
9824520Snw141292 	 * user or group SID (i.e., S-1-5-w-x-y-z-<user/group RID>) then
9834520Snw141292 	 * save the last RID and truncate the SID
9844520Snw141292 	 */
9854520Snw141292 	if (rid != NULL && sid.authority == 5 && sid.sub_authority_count == 5)
9864520Snw141292 		*rid = sid.sub_authorities[--sid.sub_authority_count];
9874520Snw141292 	return (idmap_sid2txt(&sid));
9884520Snw141292 }
9894520Snw141292 
9904520Snw141292 
9914520Snw141292 idmap_retcode
9924520Snw141292 idmap_lookup_batch_start(ad_t *ad, int nqueries, idmap_query_state_t **state)
9934520Snw141292 {
9944520Snw141292 	idmap_query_state_t *new_state;
9954520Snw141292 	ad_host_t	*adh = NULL;
9964520Snw141292 
9974520Snw141292 	*state = NULL;
9984520Snw141292 
9996097Snw141292 	if (ad == NULL)
10005731Sbaban 		return (IDMAP_ERR_INTERNAL);
10014520Snw141292 
10024520Snw141292 	adh = idmap_get_conn(ad);
10034520Snw141292 	if (adh == NULL)
10045968Snw141292 		return (IDMAP_ERR_RETRIABLE_NET_ERR);
10054520Snw141292 
10064520Snw141292 	new_state = calloc(1, sizeof (idmap_query_state_t) +
10074520Snw141292 	    (nqueries - 1) * sizeof (idmap_q_t));
10084520Snw141292 
10094520Snw141292 	if (new_state == NULL)
10104520Snw141292 		return (IDMAP_ERR_MEMORY);
10114520Snw141292 
10124884Sjp151216 	new_state->ref_cnt = 1;
10134520Snw141292 	new_state->qadh = adh;
10144520Snw141292 	new_state->qcount = nqueries;
10154520Snw141292 	new_state->qadh_gen = adh->generation;
10164520Snw141292 	/* should be -1, but the atomic routines want unsigned */
10174520Snw141292 	new_state->qlastsent = 0;
10184884Sjp151216 	(void) pthread_cond_init(&new_state->cv, NULL);
10194520Snw141292 
10204520Snw141292 	(void) pthread_mutex_lock(&qstatelock);
10214520Snw141292 	new_state->next = qstatehead;
10224520Snw141292 	qstatehead = new_state;
10234520Snw141292 	(void) pthread_mutex_unlock(&qstatelock);
10244520Snw141292 
10254520Snw141292 	*state = new_state;
10264520Snw141292 
10274520Snw141292 	return (IDMAP_SUCCESS);
10284520Snw141292 }
10294520Snw141292 
10304520Snw141292 /*
10315731Sbaban  * Set unixuser_attr and unixgroup_attr for AD-based name mapping
10325731Sbaban  */
10335731Sbaban void
10345731Sbaban idmap_lookup_batch_set_unixattr(idmap_query_state_t *state,
10355908Sjp151216 		const char *unixuser_attr, const char *unixgroup_attr)
10365908Sjp151216 {
10375731Sbaban 	state->ad_unixuser_attr = unixuser_attr;
10385731Sbaban 	state->ad_unixgroup_attr = unixgroup_attr;
10395731Sbaban }
10405731Sbaban 
10415731Sbaban /*
10424520Snw141292  * Find the idmap_query_state_t to which a given LDAP result msgid on a
10434884Sjp151216  * given connection belongs. This routine increaments the reference count
10444884Sjp151216  * so that the object can not be freed. idmap_lookup_unlock_batch()
10454884Sjp151216  * must be called to decreament the reference count.
10464520Snw141292  */
10474520Snw141292 static
10484520Snw141292 int
10494520Snw141292 idmap_msgid2query(ad_host_t *adh, int msgid,
10504520Snw141292 	idmap_query_state_t **state, int *qid)
10514520Snw141292 {
10524520Snw141292 	idmap_query_state_t *p;
10534520Snw141292 	int		    i;
10544520Snw141292 
10554520Snw141292 	(void) pthread_mutex_lock(&qstatelock);
10564520Snw141292 	for (p = qstatehead; p != NULL; p = p->next) {
10574520Snw141292 		if (p->qadh != adh || adh->generation != p->qadh_gen)
10584520Snw141292 			continue;
10594520Snw141292 		for (i = 0; i < p->qcount; i++) {
10604520Snw141292 			if ((p->queries[i]).msgid == msgid) {
10614884Sjp151216 				p->ref_cnt++;
10624520Snw141292 				*state = p;
10634520Snw141292 				*qid = i;
10644520Snw141292 				(void) pthread_mutex_unlock(&qstatelock);
10654520Snw141292 				return (1);
10664520Snw141292 			}
10674520Snw141292 		}
10684520Snw141292 	}
10694520Snw141292 	(void) pthread_mutex_unlock(&qstatelock);
10704520Snw141292 	return (0);
10714520Snw141292 }
10724520Snw141292 
10734520Snw141292 /*
10745696Snw141292  * Take parsed attribute values from a search result entry and check if
10755696Snw141292  * it is the result that was desired and, if so, set the result fields
10765696Snw141292  * of the given idmap_q_t.
10775696Snw141292  *
10785731Sbaban  * Frees the unused char * values.
10794520Snw141292  */
10804520Snw141292 static
10815696Snw141292 void
1082*6386Sjp151216 idmap_setqresults(idmap_q_t *q, char *san, char *dn, const char *attr,
1083*6386Sjp151216 	char *sid, rid_t rid, int sid_type, char *unixname)
10844520Snw141292 {
10855696Snw141292 	char *domain;
10865731Sbaban 	int err1, err2;
10875696Snw141292 
10885696Snw141292 	assert(dn != NULL);
10895696Snw141292 
10905696Snw141292 	if ((domain = dn2dns(dn)) == NULL)
10915696Snw141292 		goto out;
10925696Snw141292 
10935731Sbaban 	if (q->ecanonname != NULL && san != NULL) {
10945731Sbaban 		/* Check that this is the canonname that we were looking for */
10955696Snw141292 		if (u8_strcmp(q->ecanonname, san, 0,
10965696Snw141292 		    U8_STRCMP_CI_LOWER, /* no normalization, for now */
10975731Sbaban 		    U8_UNICODE_LATEST, &err1) != 0 || err1 != 0)
10985731Sbaban 			goto out;
10995731Sbaban 	}
11005731Sbaban 
11015731Sbaban 	if (q->edomain != NULL) {
11025731Sbaban 		/* Check that this is the domain that we were looking for */
11035731Sbaban 		if (u8_strcmp(q->edomain, domain, 0, U8_STRCMP_CI_LOWER,
11045696Snw141292 		    U8_UNICODE_LATEST, &err2) != 0 || err2 != 0)
11055696Snw141292 			goto out;
11065696Snw141292 	}
11075696Snw141292 
1108*6386Sjp151216 	/* Copy the DN and attr and value */
1109*6386Sjp151216 	if (q->dn != NULL)
1110*6386Sjp151216 		*q->dn = strdup(dn);
1111*6386Sjp151216 
1112*6386Sjp151216 	if (q->attr != NULL && attr != NULL)
1113*6386Sjp151216 		*q->attr = strdup(attr);
1114*6386Sjp151216 
1115*6386Sjp151216 	if (q->value != NULL && unixname != NULL)
1116*6386Sjp151216 		*q->value = strdup(unixname);
1117*6386Sjp151216 
11185731Sbaban 	/* Set results */
11195731Sbaban 	if (q->sid) {
11205731Sbaban 		*q->sid = sid;
11215731Sbaban 		sid = NULL;
11225731Sbaban 	}
11235731Sbaban 	if (q->rid)
11245731Sbaban 		*q->rid = rid;
11255731Sbaban 	if (q->sid_type)
11265731Sbaban 		*q->sid_type = sid_type;
11275731Sbaban 	if (q->unixname) {
11285731Sbaban 		*q->unixname = unixname;
11295731Sbaban 		unixname = NULL;
11305731Sbaban 	}
11315731Sbaban 	if (q->domain != NULL) {
11325731Sbaban 		*q->domain = domain;
11335731Sbaban 		domain = NULL;
11345731Sbaban 	}
11355731Sbaban 	if (q->canonname != NULL) {
11365731Sbaban 		*q->canonname = san;
11375731Sbaban 		san = NULL;
11385731Sbaban 	}
11395731Sbaban 
11405731Sbaban 	/* Always have q->rc; idmap_extract_object() asserts this */
11415696Snw141292 	*q->rc = IDMAP_SUCCESS;
11425696Snw141292 
11435696Snw141292 out:
11445696Snw141292 	/* Free unused attribute values */
11455696Snw141292 	free(san);
11465696Snw141292 	free(sid);
11475696Snw141292 	free(domain);
11485731Sbaban 	free(unixname);
11494520Snw141292 }
11504520Snw141292 
11514520Snw141292 /*
11525696Snw141292  * The following three functions extract objectSid, sAMAccountName and
11535696Snw141292  * objectClass attribute values and, in the case of objectSid and
11545696Snw141292  * objectClass, parse them.
11555696Snw141292  *
11565696Snw141292  * idmap_setqresults() takes care of dealing with the result entry's DN.
11575696Snw141292  */
11585696Snw141292 
11595696Snw141292 /*
11605696Snw141292  * Return a NUL-terminated stringified SID from the value of an
11615696Snw141292  * objectSid attribute and put the last RID in *rid.
11624520Snw141292  */
11634520Snw141292 static
11645696Snw141292 char *
11655696Snw141292 idmap_bv_objsid2sidstr(BerValue **bvalues, rid_t *rid)
11664520Snw141292 {
11675696Snw141292 	char *sid;
11685696Snw141292 
11695696Snw141292 	if (bvalues == NULL)
11705696Snw141292 		return (NULL);
11715696Snw141292 	/* objectSid is single valued */
11725696Snw141292 	if ((sid = convert_bval2sid(bvalues[0], rid)) == NULL)
11735696Snw141292 		return (NULL);
11745696Snw141292 	return (sid);
11755696Snw141292 }
11765696Snw141292 
11775696Snw141292 /*
11785696Snw141292  * Return a NUL-terminated string from the value of a sAMAccountName
11795731Sbaban  * or unixname attribute.
11805696Snw141292  */
11815696Snw141292 static
11825696Snw141292 char *
11835731Sbaban idmap_bv_name2str(BerValue **bvalues)
11845696Snw141292 {
11855696Snw141292 	char *s;
11864520Snw141292 
11874520Snw141292 	if (bvalues == NULL || bvalues[0] == NULL ||
11884520Snw141292 	    bvalues[0]->bv_val == NULL)
11895696Snw141292 		return (NULL);
11905447Snw141292 
11915696Snw141292 	if ((s = malloc(bvalues[0]->bv_len + 1)) == NULL)
11925696Snw141292 		return (NULL);
11935447Snw141292 
11945696Snw141292 	(void) snprintf(s, bvalues[0]->bv_len + 1, "%.*s", bvalues[0]->bv_len,
11955696Snw141292 	    bvalues[0]->bv_val);
11964520Snw141292 
11975696Snw141292 	return (s);
11984520Snw141292 }
11994520Snw141292 
12004520Snw141292 
12014520Snw141292 #define	BVAL_CASEEQ(bv, str) \
12024520Snw141292 		(((*(bv))->bv_len == (sizeof (str) - 1)) && \
12034520Snw141292 		    strncasecmp((*(bv))->bv_val, str, (*(bv))->bv_len) == 0)
12044520Snw141292 
12054520Snw141292 /*
12065696Snw141292  * Extract the class of the result entry.  Returns 1 on success, 0 on
12075696Snw141292  * failure.
12084520Snw141292  */
12094520Snw141292 static
12105447Snw141292 int
12115696Snw141292 idmap_bv_objclass2sidtype(BerValue **bvalues, int *sid_type)
12124520Snw141292 {
12134520Snw141292 	BerValue	**cbval;
12144520Snw141292 
12155696Snw141292 	*sid_type = _IDMAP_T_OTHER;
12164520Snw141292 	if (bvalues == NULL)
12175447Snw141292 		return (0);
12184520Snw141292 
12195696Snw141292 	/*
12205696Snw141292 	 * We iterate over all the values because computer is a
12215696Snw141292 	 * sub-class of user.
12225696Snw141292 	 */
12234520Snw141292 	for (cbval = bvalues; *cbval != NULL; cbval++) {
12244520Snw141292 		if (BVAL_CASEEQ(cbval, "Computer")) {
12255696Snw141292 			*sid_type = _IDMAP_T_COMPUTER;
12265696Snw141292 			break;
12274520Snw141292 		} else if (BVAL_CASEEQ(cbval, "Group")) {
12285696Snw141292 			*sid_type = _IDMAP_T_GROUP;
12295696Snw141292 			break;
12304520Snw141292 		} else if (BVAL_CASEEQ(cbval, "USER")) {
12315696Snw141292 			*sid_type = _IDMAP_T_USER;
12325696Snw141292 			/* Continue looping -- this may be a computer yet */
12335696Snw141292 		}
12345696Snw141292 		/*
12355696Snw141292 		 * "else if (*sid_type = _IDMAP_T_USER)" then this is a
12365696Snw141292 		 * new sub-class of user -- what to do with it??
12375696Snw141292 		 */
12384520Snw141292 	}
12395447Snw141292 
12405447Snw141292 	return (1);
12414520Snw141292 }
12424520Snw141292 
12434520Snw141292 /*
12444520Snw141292  * Handle a given search result entry
12454520Snw141292  */
12464520Snw141292 static
12474520Snw141292 void
12484520Snw141292 idmap_extract_object(idmap_query_state_t *state, int qid, LDAPMessage *res)
12494520Snw141292 {
12504520Snw141292 	BerElement		*ber = NULL;
12514520Snw141292 	BerValue		**bvalues;
12524520Snw141292 	ad_host_t		*adh;
12534520Snw141292 	idmap_q_t		*q;
12545696Snw141292 	char			*attr;
12555731Sbaban 	const char		*unixuser_attr = NULL;
12565731Sbaban 	const char		*unixgroup_attr = NULL;
12575731Sbaban 	char			*unixuser = NULL;
12585731Sbaban 	char			*unixgroup = NULL;
12595696Snw141292 	char			*dn = NULL;
12605696Snw141292 	char			*san = NULL;
12615696Snw141292 	char			*sid = NULL;
12625696Snw141292 	rid_t			rid = 0;
12635731Sbaban 	int			sid_type = _IDMAP_T_UNDEF;
12645447Snw141292 	int			has_class, has_san, has_sid;
12655731Sbaban 	int			has_unixuser, has_unixgroup;
12664520Snw141292 
12674520Snw141292 	adh = state->qadh;
12684520Snw141292 
12694520Snw141292 	(void) pthread_mutex_lock(&adh->lock);
12704520Snw141292 
12715447Snw141292 	q = &(state->queries[qid]);
12725447Snw141292 
12735731Sbaban 	assert(q->rc != NULL);
12745731Sbaban 
12755447Snw141292 	if (*q->rc == IDMAP_SUCCESS || adh->dead ||
12765447Snw141292 	    (dn = ldap_get_dn(adh->ld, res)) == NULL) {
12774520Snw141292 		(void) pthread_mutex_unlock(&adh->lock);
12784520Snw141292 		return;
12794520Snw141292 	}
12804520Snw141292 
12815447Snw141292 	assert(q->domain == NULL || *q->domain == NULL);
12824520Snw141292 
12835731Sbaban 	/*
12845731Sbaban 	 * If the caller has requested unixname then determine the
12855731Sbaban 	 * AD attribute name that will have the unixname.
12865731Sbaban 	 */
12875731Sbaban 	if (q->unixname != NULL) {
12885731Sbaban 		if (q->eunixtype == _IDMAP_T_USER)
12895731Sbaban 			unixuser_attr = state->ad_unixuser_attr;
12905731Sbaban 		else if (q->eunixtype == _IDMAP_T_GROUP)
12915731Sbaban 			unixgroup_attr = state->ad_unixgroup_attr;
12925731Sbaban 		else if (q->eunixtype == _IDMAP_T_UNDEF) {
12935731Sbaban 			/*
12945731Sbaban 			 * This is the case where we don't know
12955731Sbaban 			 * before hand whether we need unixuser
12965731Sbaban 			 * or unixgroup. This will be determined
12975731Sbaban 			 * by the "sid_type" (i.e whether the given
12985731Sbaban 			 * winname is user or group). If sid_type
12995731Sbaban 			 * turns out to be user we will return
13005731Sbaban 			 * unixuser (if found) and if it is a group
13015731Sbaban 			 * we will return unixgroup (if found). We
13025731Sbaban 			 * lookup for both ad_unixuser_attr and
13035731Sbaban 			 * ad_unixgroup_attr and discard one of them
13045731Sbaban 			 * after we know the "sidtype". This
13055731Sbaban 			 * supports the following type of lookups.
13065731Sbaban 			 *
13075731Sbaban 			 * Example:
13085731Sbaban 			 *   $idmap show -c winname:foo
13095731Sbaban 			 * In the above example, idmap will
13105731Sbaban 			 * return uid if winname is winuser
13115731Sbaban 			 * and gid if winname is wingroup.
13125731Sbaban 			 */
13135731Sbaban 			unixuser_attr = state->ad_unixuser_attr;
13145731Sbaban 			unixgroup_attr = state->ad_unixgroup_attr;
13155731Sbaban 		}
13165731Sbaban 	}
13175731Sbaban 
13185731Sbaban 	has_class = has_san = has_sid = has_unixuser = has_unixgroup = 0;
13194520Snw141292 	for (attr = ldap_first_attribute(adh->ld, res, &ber); attr != NULL;
13204520Snw141292 	    attr = ldap_next_attribute(adh->ld, res, ber)) {
13214520Snw141292 		bvalues = NULL;	/* for memory management below */
13224520Snw141292 
13234520Snw141292 		/*
13244520Snw141292 		 * If this is an attribute we are looking for and
13254520Snw141292 		 * haven't seen it yet, parse it
13264520Snw141292 		 */
13275731Sbaban 		if (q->sid != NULL && !has_sid &&
13285447Snw141292 		    strcasecmp(attr, OBJSID) == 0) {
13294520Snw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
13305696Snw141292 			sid = idmap_bv_objsid2sidstr(bvalues, &rid);
13315696Snw141292 			has_sid = (sid != NULL);
13325447Snw141292 		} else if (!has_san && strcasecmp(attr, SAN) == 0) {
13334520Snw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
13345731Sbaban 			san = idmap_bv_name2str(bvalues);
13355696Snw141292 			has_san = (san != NULL);
13365447Snw141292 		} else if (!has_class && strcasecmp(attr, OBJCLASS) == 0) {
13374520Snw141292 			bvalues = ldap_get_values_len(adh->ld, res, attr);
13385696Snw141292 			has_class = idmap_bv_objclass2sidtype(bvalues,
13395696Snw141292 			    &sid_type);
13405731Sbaban 			if (has_class && q->unixname != NULL &&
13415731Sbaban 			    q->eunixtype == _IDMAP_T_UNDEF) {
13425731Sbaban 				/*
13435731Sbaban 				 * This is the case where we didn't
13445731Sbaban 				 * know whether we wanted unixuser or
13455731Sbaban 				 * unixgroup as described above.
13465731Sbaban 				 * Now since we know the "sid_type"
13475731Sbaban 				 * we discard the unwanted value
13485731Sbaban 				 * if it was retrieved before we
13495731Sbaban 				 * got here.
13505731Sbaban 				 */
13515731Sbaban 				if (sid_type == _IDMAP_T_USER) {
13525731Sbaban 					free(unixgroup);
13535731Sbaban 					unixgroup_attr = unixgroup = NULL;
13545731Sbaban 				} else if (sid_type == _IDMAP_T_GROUP) {
13555731Sbaban 					free(unixuser);
13565731Sbaban 					unixuser_attr = unixuser = NULL;
13575731Sbaban 				} else {
13585731Sbaban 					free(unixuser);
13595731Sbaban 					free(unixgroup);
13605731Sbaban 					unixuser_attr = unixuser = NULL;
13615731Sbaban 					unixgroup_attr = unixgroup = NULL;
13625731Sbaban 				}
13635731Sbaban 			}
13645731Sbaban 		} else if (!has_unixuser && unixuser_attr != NULL &&
13655731Sbaban 		    strcasecmp(attr, unixuser_attr) == 0) {
13665731Sbaban 			bvalues = ldap_get_values_len(adh->ld, res, attr);
13675731Sbaban 			unixuser = idmap_bv_name2str(bvalues);
13685731Sbaban 			has_unixuser = (unixuser != NULL);
1369*6386Sjp151216 
13705731Sbaban 		} else if (!has_unixgroup && unixgroup_attr != NULL &&
13715731Sbaban 		    strcasecmp(attr, unixgroup_attr) == 0) {
13725731Sbaban 			bvalues = ldap_get_values_len(adh->ld, res, attr);
13735731Sbaban 			unixgroup = idmap_bv_name2str(bvalues);
13745731Sbaban 			has_unixgroup = (unixgroup != NULL);
13754520Snw141292 		}
13764520Snw141292 
13774520Snw141292 		if (bvalues != NULL)
13784520Snw141292 			ldap_value_free_len(bvalues);
13794520Snw141292 		ldap_memfree(attr);
13804520Snw141292 
13815696Snw141292 		if (has_class && has_san &&
13825731Sbaban 		    (q->sid == NULL || has_sid) &&
13835731Sbaban 		    (unixuser_attr == NULL || has_unixuser) &&
13845731Sbaban 		    (unixgroup_attr == NULL || has_unixgroup)) {
13855731Sbaban 			/* Got what we need */
13865447Snw141292 			break;
13875696Snw141292 		}
13884520Snw141292 	}
13894520Snw141292 
13905731Sbaban 	if (!has_class) {
13915731Sbaban 		/*
13925731Sbaban 		 * Didn't find objectclass. Something's wrong with our
13935731Sbaban 		 * AD data.
13945731Sbaban 		 */
13955731Sbaban 		free(san);
13965731Sbaban 		free(sid);
13975731Sbaban 		free(unixuser);
13985731Sbaban 		free(unixgroup);
13995731Sbaban 	} else {
14005731Sbaban 		/*
14015731Sbaban 		 * Either we got what we needed and came out of the loop
14025731Sbaban 		 * early OR we completed the loop in which case we didn't
14035731Sbaban 		 * find some attributes that we were looking for. In either
14045731Sbaban 		 * case set the result with what we got.
14055731Sbaban 		 */
1406*6386Sjp151216 		idmap_setqresults(q, san, dn,
1407*6386Sjp151216 		    (unixuser != NULL) ? unixuser_attr : unixgroup_attr,
1408*6386Sjp151216 		    sid, rid, sid_type,
14095731Sbaban 		    (unixuser != NULL) ? unixuser : unixgroup);
14105731Sbaban 	}
14115731Sbaban 
14125447Snw141292 	(void) pthread_mutex_unlock(&adh->lock);
14134520Snw141292 
14144520Snw141292 	if (ber != NULL)
14154520Snw141292 		ber_free(ber, 0);
14164520Snw141292 
14174520Snw141292 	ldap_memfree(dn);
14184520Snw141292 }
14194520Snw141292 
14204520Snw141292 /*
14214520Snw141292  * Try to get a result; if there is one, find the corresponding
14224520Snw141292  * idmap_q_t and process the result.
14234520Snw141292  */
14244520Snw141292 static
14254520Snw141292 int
14264520Snw141292 idmap_get_adobject_batch(ad_host_t *adh, struct timeval *timeout)
14274520Snw141292 {
14284520Snw141292 	idmap_query_state_t	*query_state;
14294520Snw141292 	LDAPMessage		*res = NULL;
14304520Snw141292 	int			rc, ret, msgid, qid;
14314520Snw141292 
14324520Snw141292 	(void) pthread_mutex_lock(&adh->lock);
14334520Snw141292 	if (adh->dead) {
14344520Snw141292 		(void) pthread_mutex_unlock(&adh->lock);
14354520Snw141292 		return (-1);
14364520Snw141292 	}
14374520Snw141292 
14384520Snw141292 	/* Get one result */
14394520Snw141292 	rc = ldap_result(adh->ld, LDAP_RES_ANY, 0,
14404520Snw141292 	    timeout, &res);
14415968Snw141292 	if ((timeout != NULL && timeout->tv_sec > 0 && rc == LDAP_SUCCESS) ||
14425968Snw141292 	    rc < 0)
14434520Snw141292 		adh->dead = 1;
14444520Snw141292 	(void) pthread_mutex_unlock(&adh->lock);
14454520Snw141292 
14464520Snw141292 	if (adh->dead)
14474520Snw141292 		return (-1);
14484520Snw141292 
14494520Snw141292 	switch (rc) {
14504520Snw141292 	case LDAP_RES_SEARCH_RESULT:
14514520Snw141292 		/* We have all the LDAP replies for some search... */
14524520Snw141292 		msgid = ldap_msgid(res);
14534520Snw141292 		if (idmap_msgid2query(adh, msgid,
14544520Snw141292 		    &query_state, &qid)) {
14554520Snw141292 			/* ...so we can decrement qinflight */
14564520Snw141292 			atomic_dec_32(&query_state->qinflight);
14575447Snw141292 			/* We've seen all the result entries we'll see */
14585447Snw141292 			if (*query_state->queries[qid].rc != IDMAP_SUCCESS)
14595447Snw141292 				*query_state->queries[qid].rc =
14605447Snw141292 				    IDMAP_ERR_NOTFOUND;
14614884Sjp151216 			idmap_lookup_unlock_batch(&query_state);
14624520Snw141292 		}
14634520Snw141292 		(void) ldap_msgfree(res);
14644520Snw141292 		ret = 0;
14654520Snw141292 		break;
14664520Snw141292 	case LDAP_RES_SEARCH_REFERENCE:
14674520Snw141292 		/*
14684520Snw141292 		 * We have no need for these at the moment.  Eventually,
14694520Snw141292 		 * when we query things that we can't expect to find in
14704520Snw141292 		 * the Global Catalog then we'll need to learn to follow
14714520Snw141292 		 * references.
14724520Snw141292 		 */
14734520Snw141292 		(void) ldap_msgfree(res);
14744520Snw141292 		ret = 0;
14754520Snw141292 		break;
14764520Snw141292 	case LDAP_RES_SEARCH_ENTRY:
14774520Snw141292 		/* Got a result */
14784520Snw141292 		msgid = ldap_msgid(res);
14794520Snw141292 		if (idmap_msgid2query(adh, msgid,
14804520Snw141292 		    &query_state, &qid)) {
14814520Snw141292 			idmap_extract_object(query_state, qid, res);
14824520Snw141292 			/* we saw at least one result */
14834884Sjp151216 			idmap_lookup_unlock_batch(&query_state);
14844520Snw141292 		}
14854520Snw141292 		(void) ldap_msgfree(res);
14864520Snw141292 		ret = 0;
14874520Snw141292 		break;
14884520Snw141292 	default:
14894520Snw141292 		/* timeout or error; treat the same */
14904520Snw141292 		ret = -1;
14914520Snw141292 		break;
14924520Snw141292 	}
14934520Snw141292 
14944520Snw141292 	return (ret);
14954520Snw141292 }
14964520Snw141292 
14974884Sjp151216 /*
14984884Sjp151216  * This routine decreament the reference count of the
14994884Sjp151216  * idmap_query_state_t
15004884Sjp151216  */
15014884Sjp151216 static void
15024884Sjp151216 idmap_lookup_unlock_batch(idmap_query_state_t **state)
15034884Sjp151216 {
15044884Sjp151216 	/*
15054884Sjp151216 	 * Decrement reference count with qstatelock locked
15064884Sjp151216 	 */
15074884Sjp151216 	(void) pthread_mutex_lock(&qstatelock);
15084884Sjp151216 	(*state)->ref_cnt--;
15094884Sjp151216 	/*
15104884Sjp151216 	 * If there are no references wakup the allocating thread
15114884Sjp151216 	 */
15124884Sjp151216 	if ((*state)->ref_cnt == 0)
15134884Sjp151216 		(void) pthread_cond_signal(&(*state)->cv);
15144884Sjp151216 	(void) pthread_mutex_unlock(&qstatelock);
15154884Sjp151216 	*state = NULL;
15164884Sjp151216 }
15174884Sjp151216 
15185447Snw141292 static
15195447Snw141292 void
15205447Snw141292 idmap_cleanup_batch(idmap_query_state_t *batch)
15215447Snw141292 {
15225447Snw141292 	int i;
15235447Snw141292 
15245447Snw141292 	for (i = 0; i < batch->qcount; i++) {
15255696Snw141292 		if (batch->queries[i].ecanonname != NULL)
15265696Snw141292 			free(batch->queries[i].ecanonname);
15275696Snw141292 		batch->queries[i].ecanonname = NULL;
15285696Snw141292 		if (batch->queries[i].edomain != NULL)
15295696Snw141292 			free(batch->queries[i].edomain);
15305696Snw141292 		batch->queries[i].edomain = NULL;
15315447Snw141292 	}
15325447Snw141292 }
15335447Snw141292 
15344884Sjp151216 /*
15354884Sjp151216  * This routine frees the idmap_query_state_t structure
15364884Sjp151216  * If the reference count is greater than 1 it waits
15374884Sjp151216  * for the other threads to finish using it.
15384884Sjp151216  */
15394520Snw141292 void
15404884Sjp151216 idmap_lookup_release_batch(idmap_query_state_t **state)
15414520Snw141292 {
15424520Snw141292 	idmap_query_state_t **p;
15434520Snw141292 
15444884Sjp151216 	/*
15454884Sjp151216 	 * Decrement reference count with qstatelock locked
15464884Sjp151216 	 * and wait for reference count to get to zero
15474884Sjp151216 	 */
15484884Sjp151216 	(void) pthread_mutex_lock(&qstatelock);
15494884Sjp151216 	(*state)->ref_cnt--;
15504884Sjp151216 	while ((*state)->ref_cnt > 0) {
15514884Sjp151216 		(void) pthread_cond_wait(&(*state)->cv, &qstatelock);
15524884Sjp151216 	}
15534520Snw141292 
15544520Snw141292 	/* Remove this state struct from the list of state structs */
15554520Snw141292 	for (p = &qstatehead; *p != NULL; p = &(*p)->next) {
15564520Snw141292 		if (*p == (*state)) {
15574520Snw141292 			*p = (*state)->next;
15584520Snw141292 			break;
15594520Snw141292 		}
15604520Snw141292 	}
15615447Snw141292 
15625447Snw141292 	idmap_cleanup_batch(*state);
15635447Snw141292 
15644520Snw141292 	(void) pthread_mutex_unlock(&qstatelock);
15654520Snw141292 
15664884Sjp151216 	(void) pthread_cond_destroy(&(*state)->cv);
15674884Sjp151216 
15684884Sjp151216 	idmap_release_conn((*state)->qadh);
15694884Sjp151216 
15704520Snw141292 	free(*state);
15714520Snw141292 	*state = NULL;
15724520Snw141292 }
15734520Snw141292 
15744520Snw141292 idmap_retcode
15755968Snw141292 idmap_lookup_batch_end(idmap_query_state_t **state)
15764520Snw141292 {
15774520Snw141292 	int		    rc = LDAP_SUCCESS;
15784520Snw141292 	idmap_retcode	    retcode = IDMAP_SUCCESS;
15795968Snw141292 	struct timeval	    timeout;
15804520Snw141292 
15814520Snw141292 	(*state)->qdead = 1;
15825968Snw141292 	timeout.tv_sec = IDMAPD_SEARCH_TIMEOUT;
15835968Snw141292 	timeout.tv_usec = 0;
15844520Snw141292 
15854520Snw141292 	/* Process results until done or until timeout, if given */
15864520Snw141292 	while ((*state)->qinflight > 0) {
15874520Snw141292 		if ((rc = idmap_get_adobject_batch((*state)->qadh,
15885968Snw141292 		    &timeout)) != 0)
15894520Snw141292 			break;
15904520Snw141292 	}
15914520Snw141292 
15925968Snw141292 	if (rc != 0)
15934520Snw141292 		retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
15944520Snw141292 
15954884Sjp151216 	idmap_lookup_release_batch(state);
15964520Snw141292 
15974520Snw141292 	return (retcode);
15984520Snw141292 }
15994520Snw141292 
16004520Snw141292 /*
16014520Snw141292  * Send one prepared search, queue up msgid, process what results are
16024520Snw141292  * available
16034520Snw141292  */
16044520Snw141292 static
16054520Snw141292 idmap_retcode
1606*6386Sjp151216 idmap_batch_add1(idmap_query_state_t *state, const char *filter,
1607*6386Sjp151216 	char *ecanonname, char *edomain, int eunixtype,
1608*6386Sjp151216 	char **dn, char **attr, char **value,
1609*6386Sjp151216 	char **canonname, char **dname,
1610*6386Sjp151216 	char **sid, rid_t *rid, int *sid_type, char **unixname,
1611*6386Sjp151216 	idmap_retcode *rc)
16124520Snw141292 {
16134520Snw141292 	idmap_retcode	retcode = IDMAP_SUCCESS;
16145731Sbaban 	int		lrc, qid, i;
16154520Snw141292 	struct timeval	tv;
16164520Snw141292 	idmap_q_t	*q;
16175696Snw141292 	static char	*attrs[] = {
16185696Snw141292 		SAN,
16195696Snw141292 		OBJSID,
16205696Snw141292 		OBJCLASS,
16215731Sbaban 		NULL,	/* placeholder for unixname attr */
16225731Sbaban 		NULL,	/* placeholder for unixname attr */
16235696Snw141292 		NULL
16245696Snw141292 	};
16254520Snw141292 
16264520Snw141292 	qid = atomic_inc_32_nv(&state->qlastsent) - 1;
16274520Snw141292 
16284520Snw141292 	q = &(state->queries[qid]);
16294520Snw141292 
16305696Snw141292 	/*
16315696Snw141292 	 * Remember the expected canonname so we can check the results
16325696Snw141292 	 * agains it
16335696Snw141292 	 */
16345696Snw141292 	q->ecanonname = ecanonname;
16355696Snw141292 	q->edomain = edomain;
16365731Sbaban 	q->eunixtype = eunixtype;
16375447Snw141292 
16384520Snw141292 	/* Remember where to put the results */
16395696Snw141292 	q->canonname = canonname;
16405731Sbaban 	q->sid = sid;
16414520Snw141292 	q->domain = dname;
16424520Snw141292 	q->rid = rid;
16434520Snw141292 	q->sid_type = sid_type;
16444520Snw141292 	q->rc = rc;
16455731Sbaban 	q->unixname = unixname;
1646*6386Sjp151216 	q->dn = dn;
1647*6386Sjp151216 	q->attr = attr;
1648*6386Sjp151216 	q->value = value;
16495731Sbaban 
16505731Sbaban 	/* Add unixuser/unixgroup attribute names to the attrs list */
16515731Sbaban 	if (unixname != NULL) {
16525731Sbaban 		i = 3;
16535731Sbaban 		if (eunixtype != _IDMAP_T_GROUP &&
16545731Sbaban 		    state->ad_unixuser_attr != NULL)
16555731Sbaban 			attrs[i++] = (char *)state->ad_unixuser_attr;
16565731Sbaban 		if (eunixtype != _IDMAP_T_USER &&
16575731Sbaban 		    state->ad_unixgroup_attr != NULL)
16585731Sbaban 			attrs[i] = (char *)state->ad_unixgroup_attr;
16595731Sbaban 	}
16604520Snw141292 
16614520Snw141292 	/*
16624520Snw141292 	 * Provide sane defaults for the results in case we never hear
16634520Snw141292 	 * back from the DS before closing the connection.
16645447Snw141292 	 *
16655447Snw141292 	 * In particular we default the result to indicate a retriable
16665447Snw141292 	 * error.  The first complete matching result entry will cause
16675447Snw141292 	 * this to be set to IDMAP_SUCCESS, and the end of the results
16685447Snw141292 	 * for this search will cause this to indicate "not found" if no
16695447Snw141292 	 * result entries arrived or no complete ones matched the lookup
16705447Snw141292 	 * we were doing.
16714520Snw141292 	 */
16724520Snw141292 	*rc = IDMAP_ERR_RETRIABLE_NET_ERR;
16735731Sbaban 	if (sid_type != NULL)
16745731Sbaban 		*sid_type = _IDMAP_T_OTHER;
16755731Sbaban 	if (sid != NULL)
16765731Sbaban 		*sid = NULL;
16775696Snw141292 	if (canonname != NULL)
16785696Snw141292 		*canonname = NULL;
16794520Snw141292 	if (dname != NULL)
16804520Snw141292 		*dname = NULL;
16814520Snw141292 	if (rid != NULL)
16824520Snw141292 		*rid = 0;
1683*6386Sjp151216 	if (dn != NULL)
1684*6386Sjp151216 		*dn = NULL;
1685*6386Sjp151216 	if (attr != NULL)
1686*6386Sjp151216 		*attr = NULL;
1687*6386Sjp151216 	if (value != NULL)
1688*6386Sjp151216 		*value = NULL;
16894520Snw141292 
16904520Snw141292 	/* Send this lookup, don't wait for a result here */
16914520Snw141292 	(void) pthread_mutex_lock(&state->qadh->lock);
16924520Snw141292 
16934520Snw141292 	if (!state->qadh->dead) {
16944520Snw141292 		state->qadh->idletime = time(NULL);
16955447Snw141292 		lrc = ldap_search_ext(state->qadh->ld, "",
16965696Snw141292 		    LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL,
16974520Snw141292 		    NULL, -1, &q->msgid);
16984520Snw141292 		if (lrc == LDAP_BUSY || lrc == LDAP_UNAVAILABLE ||
16994520Snw141292 		    lrc == LDAP_CONNECT_ERROR || lrc == LDAP_SERVER_DOWN ||
17004520Snw141292 		    lrc == LDAP_UNWILLING_TO_PERFORM) {
17014520Snw141292 			retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
17024520Snw141292 			state->qadh->dead = 1;
17034520Snw141292 		} else if (lrc != LDAP_SUCCESS) {
17044520Snw141292 			retcode = IDMAP_ERR_OTHER;
17054520Snw141292 			state->qadh->dead = 1;
17064520Snw141292 		}
17074520Snw141292 	}
17084520Snw141292 	(void) pthread_mutex_unlock(&state->qadh->lock);
17094520Snw141292 
17104520Snw141292 	if (state->qadh->dead)
17114520Snw141292 		return (retcode);
17124520Snw141292 
17134520Snw141292 	atomic_inc_32(&state->qinflight);
17144520Snw141292 
17154520Snw141292 	/*
17164520Snw141292 	 * Reap as many requests as we can _without_ waiting
17174520Snw141292 	 *
17184520Snw141292 	 * We do this to prevent any possible TCP socket buffer
17194520Snw141292 	 * starvation deadlocks.
17204520Snw141292 	 */
17214520Snw141292 	(void) memset(&tv, 0, sizeof (tv));
17224520Snw141292 	while (idmap_get_adobject_batch(state->qadh, &tv) == 0)
17234520Snw141292 		;
17244520Snw141292 
17254520Snw141292 	return (IDMAP_SUCCESS);
17264520Snw141292 }
17274520Snw141292 
17284520Snw141292 idmap_retcode
17294520Snw141292 idmap_name2sid_batch_add1(idmap_query_state_t *state,
17305731Sbaban 	const char *name, const char *dname, int eunixtype,
1731*6386Sjp151216 	char **dn, char **attr, char **value,
1732*6386Sjp151216 	char **canonname, char **sid, rid_t *rid,
1733*6386Sjp151216 	int *sid_type, char **unixname, idmap_retcode *rc)
17344520Snw141292 {
17354520Snw141292 	idmap_retcode	retcode;
17365447Snw141292 	int		len, samAcctNameLen;
17374520Snw141292 	char		*filter = NULL;
17385696Snw141292 	char		*ecanonname, *edomain; /* expected canonname */
17394520Snw141292 
17404520Snw141292 	/*
17415447Snw141292 	 * Strategy: search the global catalog for user/group by
17425447Snw141292 	 * sAMAccountName = user/groupname with "" as the base DN and by
17435447Snw141292 	 * userPrincipalName = user/groupname@domain.  The result
17445447Snw141292 	 * entries will be checked to conform to the name and domain
17455447Snw141292 	 * name given here.  The DN, sAMAccountName, userPrincipalName,
17465447Snw141292 	 * objectSid and objectClass of the result entries are all we
17475447Snw141292 	 * need to figure out which entries match the lookup, the SID of
17485447Snw141292 	 * the user/group and whether it is a user or a group.
17494520Snw141292 	 */
17504520Snw141292 
17514520Snw141292 	/*
17525447Snw141292 	 * We need the name and the domain name separately and as
17535447Snw141292 	 * name@domain.  We also allow the domain to be provided
17545447Snw141292 	 * separately.
17554520Snw141292 	 */
17565232Snw141292 	samAcctNameLen = strlen(name);
17575447Snw141292 
17585696Snw141292 	if ((ecanonname = strdup(name)) == NULL)
17595696Snw141292 		return (IDMAP_ERR_MEMORY);
17604520Snw141292 
17615447Snw141292 	if (dname == NULL || *dname == '\0') {
17625696Snw141292 		if ((dname = strchr(name, '@')) != NULL) {
17635696Snw141292 			/* 'name' is qualified with a domain name */
17645696Snw141292 			if ((edomain = strdup(dname + 1)) == NULL) {
17655696Snw141292 				free(ecanonname);
17665696Snw141292 				return (IDMAP_ERR_MEMORY);
17675696Snw141292 			}
17685696Snw141292 			*strchr(ecanonname, '@') = '\0';
17695696Snw141292 		} else {
17706097Snw141292 			/*
17716097Snw141292 			 * 'name' not qualified and dname not given
17726097Snw141292 			 *
17736097Snw141292 			 * Note: ad->dflt_w2k_dom cannot be NULL - see
17746097Snw141292 			 * idmap_ad_alloc()
17756097Snw141292 			 */
17766097Snw141292 			if (*state->qadh->owner->dflt_w2k_dom == '\0') {
17775696Snw141292 				free(ecanonname);
17785696Snw141292 				return (IDMAP_ERR_DOMAIN);
17795696Snw141292 			}
17805696Snw141292 			edomain = strdup(state->qadh->owner->dflt_w2k_dom);
17815696Snw141292 			if (edomain == NULL) {
17825696Snw141292 				free(ecanonname);
17835696Snw141292 				return (IDMAP_ERR_MEMORY);
17845696Snw141292 			}
17855696Snw141292 		}
17865696Snw141292 	} else {
17875696Snw141292 		if ((edomain = strdup(dname)) == NULL) {
17885696Snw141292 			free(ecanonname);
17895447Snw141292 			return (IDMAP_ERR_MEMORY);
17905696Snw141292 		}
17915447Snw141292 	}
17924520Snw141292 
17934520Snw141292 	/* Assemble filter */
17945447Snw141292 	len = snprintf(NULL, 0, SANFILTER, samAcctNameLen, name) + 1;
17955447Snw141292 	if ((filter = (char *)malloc(len)) == NULL) {
17965696Snw141292 		free(ecanonname);
17974520Snw141292 		return (IDMAP_ERR_MEMORY);
17984520Snw141292 	}
17995447Snw141292 	(void) snprintf(filter, len, SANFILTER, samAcctNameLen, name);
18004520Snw141292 
18015696Snw141292 	retcode = idmap_batch_add1(state, filter, ecanonname, edomain,
1802*6386Sjp151216 	    eunixtype, dn, attr, value, canonname, NULL, sid, rid, sid_type,
1803*6386Sjp151216 	    unixname, rc);
18044520Snw141292 
18054520Snw141292 	free(filter);
18064520Snw141292 
18074520Snw141292 	return (retcode);
18084520Snw141292 }
18094520Snw141292 
18104520Snw141292 idmap_retcode
18114520Snw141292 idmap_sid2name_batch_add1(idmap_query_state_t *state,
18125731Sbaban 	const char *sid, const rid_t *rid, int eunixtype,
1813*6386Sjp151216 	char **dn, char **attr, char **value,
1814*6386Sjp151216 	char **name, char **dname, int *sid_type,
1815*6386Sjp151216 	char **unixname, idmap_retcode *rc)
18164520Snw141292 {
18174520Snw141292 	idmap_retcode	retcode;
18184520Snw141292 	int		flen, ret;
18194520Snw141292 	char		*filter = NULL;
18204520Snw141292 	char		cbinsid[MAXHEXBINSID + 1];
18214520Snw141292 
18224520Snw141292 	/*
18234520Snw141292 	 * Strategy: search [the global catalog] for user/group by
18244520Snw141292 	 * objectSid = SID with empty base DN.  The DN, sAMAccountName
18254520Snw141292 	 * and objectClass of the result are all we need to figure out
18264520Snw141292 	 * the name of the SID and whether it is a user, a group or a
18274520Snw141292 	 * computer.
18284520Snw141292 	 */
18294520Snw141292 
18304520Snw141292 	ret = idmap_txtsid2hexbinsid(sid, rid, &cbinsid[0], sizeof (cbinsid));
18314520Snw141292 	if (ret != 0)
18324520Snw141292 		return (IDMAP_ERR_SID);
18334520Snw141292 
18344520Snw141292 	/* Assemble filter */
18355447Snw141292 	flen = snprintf(NULL, 0, OBJSIDFILTER, cbinsid) + 1;
18364520Snw141292 	if ((filter = (char *)malloc(flen)) == NULL)
18374520Snw141292 		return (IDMAP_ERR_MEMORY);
18385447Snw141292 	(void) snprintf(filter, flen, OBJSIDFILTER, cbinsid);
18394520Snw141292 
18405731Sbaban 	retcode = idmap_batch_add1(state, filter, NULL, NULL, eunixtype,
1841*6386Sjp151216 	    dn, attr, value, name, dname, NULL, NULL, sid_type, unixname, rc);
18424520Snw141292 
18434520Snw141292 	free(filter);
18444520Snw141292 
18454520Snw141292 	return (retcode);
18464520Snw141292 }
18475731Sbaban 
18485731Sbaban idmap_retcode
18495731Sbaban idmap_unixname2sid_batch_add1(idmap_query_state_t *state,
18505731Sbaban 	const char *unixname, int is_user, int is_wuser,
1851*6386Sjp151216 	char **dn, char **attr, char **value,
1852*6386Sjp151216 	char **sid, rid_t *rid, char **name,
1853*6386Sjp151216 	char **dname, int *sid_type, idmap_retcode *rc)
18545731Sbaban {
18555731Sbaban 	idmap_retcode	retcode;
18565731Sbaban 	int		len, ulen;
18575731Sbaban 	char		*filter = NULL;
18585731Sbaban 	const char	*attrname = NULL;
18595731Sbaban 
18605731Sbaban 	/* Get unixuser or unixgroup AD attribute name */
18615731Sbaban 	attrname = (is_user) ?
18625731Sbaban 	    state->ad_unixuser_attr : state->ad_unixgroup_attr;
18635731Sbaban 	if (attrname == NULL)
18645731Sbaban 		return (IDMAP_ERR_NOTFOUND);
18655731Sbaban 
18665731Sbaban 	/*  Assemble filter */
18675731Sbaban 	ulen = strlen(unixname);
18685731Sbaban 	len = snprintf(NULL, 0, "(&(objectclass=%s)(%s=%.*s))",
18695731Sbaban 	    is_wuser ? "user" : "group", attrname, ulen, unixname) + 1;
18705731Sbaban 	if ((filter = (char *)malloc(len)) == NULL)
18715731Sbaban 		return (IDMAP_ERR_MEMORY);
18725731Sbaban 	(void) snprintf(filter, len, "(&(objectclass=%s)(%s=%.*s))",
18735731Sbaban 	    is_wuser ? "user" : "group", attrname, ulen, unixname);
18745731Sbaban 
18755731Sbaban 	retcode = idmap_batch_add1(state, filter, NULL, NULL,
1876*6386Sjp151216 	    _IDMAP_T_UNDEF, dn, NULL, NULL, name, dname, sid, rid, sid_type,
1877*6386Sjp151216 	    NULL, rc);
1878*6386Sjp151216 
1879*6386Sjp151216 	if (retcode == IDMAP_SUCCESS && attr != NULL) {
1880*6386Sjp151216 		if ((*attr = strdup(attrname)) == NULL)
1881*6386Sjp151216 			retcode = IDMAP_ERR_MEMORY;
1882*6386Sjp151216 	}
1883*6386Sjp151216 
1884*6386Sjp151216 	if (retcode == IDMAP_SUCCESS && value != NULL) {
1885*6386Sjp151216 		if (ulen > 0) {
1886*6386Sjp151216 			if ((*value = strdup(unixname)) == NULL)
1887*6386Sjp151216 				retcode = IDMAP_ERR_MEMORY;
1888*6386Sjp151216 		}
1889*6386Sjp151216 		else
1890*6386Sjp151216 			*value = NULL;
1891*6386Sjp151216 	}
18925731Sbaban 
18935731Sbaban 	free(filter);
18945731Sbaban 
18955731Sbaban 	return (retcode);
18965731Sbaban }
1897