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