1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <stdio.h> 27 #include <sys/types.h> 28 #include <stdlib.h> 29 #include <libintl.h> 30 #include <ctype.h> 31 #include <syslog.h> 32 #include <sys/stat.h> 33 #include <fcntl.h> 34 #include <unistd.h> 35 #include <string.h> 36 #include <strings.h> 37 #include <priv.h> 38 39 #include "ns_sldap.h" 40 #include "ns_internal.h" 41 #include "ns_cache_door.h" 42 #include "ns_connmgmt.h" 43 44 #define _NIS_FILTER "nisdomain=*" 45 #define _NIS_DOMAIN "nisdomain" 46 static const char *nis_domain_attrs[] = { 47 _NIS_DOMAIN, 48 (char *)NULL 49 }; 50 51 static int validate_filter(ns_ldap_cookie_t *cookie); 52 53 void 54 __ns_ldap_freeEntry(ns_ldap_entry_t *ep) 55 { 56 int j, k = 0; 57 58 if (ep == NULL) 59 return; 60 61 if (ep->attr_pair == NULL) { 62 free(ep); 63 return; 64 } 65 for (j = 0; j < ep->attr_count; j++) { 66 if (ep->attr_pair[j] == NULL) 67 continue; 68 if (ep->attr_pair[j]->attrname) 69 free(ep->attr_pair[j]->attrname); 70 if (ep->attr_pair[j]->attrvalue) { 71 for (k = 0; (k < ep->attr_pair[j]->value_count) && 72 (ep->attr_pair[j]->attrvalue[k]); k++) { 73 free(ep->attr_pair[j]->attrvalue[k]); 74 } 75 free(ep->attr_pair[j]->attrvalue); 76 } 77 free(ep->attr_pair[j]); 78 } 79 free(ep->attr_pair); 80 free(ep); 81 } 82 83 static void 84 _freeControlList(LDAPControl ***ctrls) 85 { 86 LDAPControl **ctrl; 87 88 if (ctrls == NULL || *ctrls == NULL) 89 return; 90 91 for (ctrl = *ctrls; *ctrl != NULL; ctrl++) 92 ldap_control_free(*ctrl); 93 free(*ctrls); 94 *ctrls = NULL; 95 } 96 /* 97 * Convert attribute type in a RDN that has an attribute mapping to the 98 * original mappped type. 99 * e.g. 100 * cn<->cn-st and iphostnumber<->iphostnumber-st 101 * cn-st=aaa+iphostnumber-st=10.10.01.01 102 * is mapped to 103 * cn=aaa+iphostnumber=10.10.01.01 104 * 105 * Input - service: e.g. hosts, passwd etc. 106 * rdn: RDN 107 * Return: NULL - No attribute mapping in the RDN 108 * Non-NULL - The attribute type(s) in the RDN are mapped and 109 * the memory is allocated for the new rdn. 110 * 111 */ 112 static char * 113 _cvtRDN(const char *service, const char *rdn) { 114 char **attrs, **mapped_attrs, **mapp, *type, *value, *attr; 115 char *new_rdn = NULL; 116 int nAttr = 0, i, attr_mapped, len = 0; 117 118 /* Break down "type=value\0" pairs. Assume RDN is normalized */ 119 if ((attrs = ldap_explode_rdn(rdn, 0)) == NULL) 120 return (NULL); 121 122 for (nAttr = 0; attrs[nAttr] != NULL; nAttr++); 123 124 if ((mapped_attrs = (char **)calloc(nAttr, sizeof (char *))) == NULL) { 125 ldap_value_free(attrs); 126 return (NULL); 127 } 128 129 attr_mapped = 0; 130 for (i = 0; i < nAttr; i++) { 131 /* Parse type=value pair */ 132 if ((type = strtok_r(attrs[i], "=", &value)) == NULL || 133 value == NULL) 134 goto cleanup; 135 /* Reverse map: e.g. cn-sm -> cn */ 136 mapp = __ns_ldap_getOrigAttribute(service, type); 137 if (mapp != NULL && mapp[0] != NULL) { 138 /* The attribute mapping is found */ 139 type = mapp[0]; 140 attr_mapped = 1; 141 142 /* "type=value\0" */ 143 len = strlen(type) + strlen(value) + 2; 144 145 /* Reconstruct type=value pair. A string is allocated */ 146 if ((attr = (char *)calloc(1, len)) == NULL) { 147 __s_api_free2dArray(mapp); 148 goto cleanup; 149 } 150 (void) snprintf(attr, len, "%s=%s", 151 type, value); 152 mapped_attrs[i] = attr; 153 } else { 154 /* 155 * No attribute mapping. attrs[i] is going to be copied 156 * later. Restore "type\0value\0" back to 157 * "type=value\0". 158 */ 159 type[strlen(type)] = '='; 160 } 161 __s_api_free2dArray(mapp); 162 } 163 if (attr_mapped == 0) 164 /* No attribute mapping. Don't bother to reconstruct RDN */ 165 goto cleanup; 166 167 len = 0; 168 /* Reconstruct RDN from type=value pairs */ 169 for (i = 0; i < nAttr; i++) { 170 if (mapped_attrs[i]) 171 len += strlen(mapped_attrs[i]); 172 else 173 len += strlen(attrs[i]); 174 /* Add 1 for "+" */ 175 len++; 176 } 177 if ((new_rdn = (char *)calloc(1, ++len)) == NULL) 178 goto cleanup; 179 for (i = 0; i < nAttr; i++) { 180 if (i > 0) 181 /* Add seperator */ 182 (void) strlcat(new_rdn, "+", len); 183 184 if (mapped_attrs[i]) 185 (void) strlcat(new_rdn, mapped_attrs[i], len); 186 else 187 (void) strlcat(new_rdn, attrs[i], len); 188 189 } 190 cleanup: 191 ldap_value_free(attrs); 192 if (mapped_attrs) { 193 if (attr_mapped) { 194 for (i = 0; i < nAttr; i++) { 195 if (mapped_attrs[i]) 196 free(mapped_attrs[i]); 197 } 198 } 199 free(mapped_attrs); 200 } 201 202 return (new_rdn); 203 } 204 /* 205 * Convert attribute type in a DN that has an attribute mapping to the 206 * original mappped type. 207 * e.g 208 * The mappings are cn<->cn-sm, iphostnumber<->iphostnumber-sm 209 * 210 * dn: cn-sm=aaa+iphostnumber-sm=9.9.9.9,dc=central,dc=sun,dc=com 211 * is converted to 212 * dn: cn=aaa+iphostnumber=9.9.9.9,dc=central,dc=sun,dc=com 213 * 214 * Input - service: e.g. hosts, passwd etc. 215 * dn: the value of a distinguished name 216 * Return - NULL: error 217 * non-NULL: A converted DN and the memory is allocated 218 */ 219 static char * 220 _cvtDN(const char *service, const char *dn) { 221 char **mapped_rdns; 222 char **rdns, *new_rdn, *new_dn = NULL; 223 int nRdn = 0, i, len = 0, rdn_mapped; 224 225 if (service == NULL || dn == NULL) 226 return (NULL); 227 228 if ((rdns = ldap_explode_dn(dn, 0)) == NULL) 229 return (NULL); 230 231 for (nRdn = 0; rdns[nRdn] != NULL; nRdn++); 232 233 if ((mapped_rdns = (char **)calloc(nRdn, sizeof (char *))) == NULL) { 234 ldap_value_free(rdns); 235 return (NULL); 236 } 237 238 rdn_mapped = 0; 239 /* Break down RDNs in a DN */ 240 for (i = 0; i < nRdn; i++) { 241 if ((new_rdn = _cvtRDN(service, rdns[i])) != NULL) { 242 mapped_rdns[i] = new_rdn; 243 rdn_mapped = 1; 244 } 245 } 246 if (rdn_mapped == 0) { 247 /* 248 * No RDN contains any attribute mapping. 249 * Don't bother to reconstruct DN from RDN. Copy DN directly. 250 */ 251 new_dn = strdup(dn); 252 goto cleanup; 253 } 254 /* 255 * Reconstruct dn from RDNs. 256 * Calculate the length first. 257 */ 258 for (i = 0; i < nRdn; i++) { 259 if (mapped_rdns[i]) 260 len += strlen(mapped_rdns[i]); 261 else 262 len += strlen(rdns[i]); 263 264 /* add 1 for ',' */ 265 len ++; 266 } 267 if ((new_dn = (char *)calloc(1, ++len)) == NULL) 268 goto cleanup; 269 for (i = 0; i < nRdn; i++) { 270 if (i > 0) 271 /* Add seperator */ 272 (void) strlcat(new_dn, ",", len); 273 274 if (mapped_rdns[i]) 275 (void) strlcat(new_dn, mapped_rdns[i], len); 276 else 277 (void) strlcat(new_dn, rdns[i], len); 278 279 } 280 281 cleanup: 282 ldap_value_free(rdns); 283 if (mapped_rdns) { 284 if (rdn_mapped) { 285 for (i = 0; i < nRdn; i++) { 286 if (mapped_rdns[i]) 287 free(mapped_rdns[i]); 288 } 289 } 290 free(mapped_rdns); 291 } 292 293 return (new_dn); 294 } 295 /* 296 * Convert a single ldap entry from a LDAPMessage 297 * into an ns_ldap_entry structure. 298 * Schema map the entry if specified in flags 299 */ 300 301 static int 302 __s_api_cvtEntry(LDAP *ld, 303 const char *service, 304 LDAPMessage *e, 305 int flags, 306 ns_ldap_entry_t **ret, 307 ns_ldap_error_t **error) 308 { 309 310 ns_ldap_entry_t *ep = NULL; 311 ns_ldap_attr_t **ap = NULL; 312 BerElement *ber; 313 char *attr = NULL; 314 char **vals = NULL; 315 char **mapping; 316 char *dn; 317 int nAttrs = 0; 318 int i, j, k = 0; 319 char **gecos_mapping = NULL; 320 int gecos_val_index[3] = { -1, -1, -1}; 321 char errstr[MAXERROR]; 322 int schema_mapping_existed = FALSE; 323 int gecos_mapping_existed = FALSE; 324 int gecos_attr_matched; 325 int auto_service = FALSE; 326 int rc = NS_LDAP_SUCCESS; 327 328 if (e == NULL || ret == NULL || error == NULL) 329 return (NS_LDAP_INVALID_PARAM); 330 331 *error = NULL; 332 333 ep = (ns_ldap_entry_t *)calloc(1, sizeof (ns_ldap_entry_t)); 334 if (ep == NULL) 335 return (NS_LDAP_MEMORY); 336 337 if (service != NULL && 338 (strncasecmp(service, "auto_", 5) == 0 || 339 strcasecmp(service, "automount") == 0)) 340 auto_service = TRUE; 341 /* 342 * see if schema mapping existed for the given service 343 */ 344 mapping = __ns_ldap_getOrigAttribute(service, 345 NS_HASH_SCHEMA_MAPPING_EXISTED); 346 if (mapping) { 347 schema_mapping_existed = TRUE; 348 __s_api_free2dArray(mapping); 349 mapping = NULL; 350 } else if (auto_service) { 351 /* 352 * If service == auto_* and no 353 * schema mapping found 354 * then try automount 355 * There is certain case that schema mapping exist 356 * but __ns_ldap_getOrigAttribute(service, 357 * NS_HASH_SCHEMA_MAPPING_EXISTED); 358 * returns NULL. 359 * e.g. 360 * NS_LDAP_ATTRIBUTEMAP = automount:automountMapName=AAA 361 * NS_LDAP_OBJECTCLASSMAP = automount:automountMap=MynisMap 362 * NS_LDAP_OBJECTCLASSMAP = automount:automount=MynisObject 363 * 364 * Make a check for schema_mapping_existed here 365 * so later on __s_api_convert_automountmapname won't be called 366 * unnecessarily. It is also used for attribute mapping 367 * and objectclass mapping. 368 */ 369 mapping = __ns_ldap_getOrigAttribute("automount", 370 NS_HASH_SCHEMA_MAPPING_EXISTED); 371 if (mapping) { 372 schema_mapping_existed = TRUE; 373 __s_api_free2dArray(mapping); 374 mapping = NULL; 375 } 376 } 377 378 nAttrs = 1; /* start with 1 for the DN attr */ 379 for (attr = ldap_first_attribute(ld, e, &ber); attr != NULL; 380 attr = ldap_next_attribute(ld, e, ber)) { 381 nAttrs++; 382 ldap_memfree(attr); 383 attr = NULL; 384 } 385 ber_free(ber, 0); 386 ber = NULL; 387 388 ep->attr_count = nAttrs; 389 390 /* 391 * add 1 for "gecos" 1 to N attribute mapping, 392 * just in case it is needed. 393 * ep->attr_count will be updated later if that is true. 394 */ 395 ap = (ns_ldap_attr_t **)calloc(ep->attr_count + 1, 396 sizeof (ns_ldap_attr_t *)); 397 if (ap == NULL) { 398 __ns_ldap_freeEntry(ep); 399 ep = NULL; 400 return (NS_LDAP_MEMORY); 401 } 402 ep->attr_pair = ap; 403 404 /* DN attribute */ 405 dn = ldap_get_dn(ld, e); 406 ap[0] = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t)); 407 if (ap[0] == NULL) { 408 ldap_memfree(dn); 409 dn = NULL; 410 __ns_ldap_freeEntry(ep); 411 ep = NULL; 412 return (NS_LDAP_MEMORY); 413 } 414 415 if ((ap[0]->attrname = strdup("dn")) == NULL) { 416 ldap_memfree(dn); 417 dn = NULL; 418 __ns_ldap_freeEntry(ep); 419 ep = NULL; 420 return (NS_LDAP_INVALID_PARAM); 421 } 422 ap[0]->value_count = 1; 423 if ((ap[0]->attrvalue = (char **) 424 calloc(2, sizeof (char *))) == NULL) { 425 ldap_memfree(dn); 426 dn = NULL; 427 __ns_ldap_freeEntry(ep); 428 ep = NULL; 429 return (NS_LDAP_MEMORY); 430 } 431 432 if (schema_mapping_existed && ((flags & NS_LDAP_NOT_CVT_DN) == 0)) 433 ap[0]->attrvalue[0] = _cvtDN(service, dn); 434 else 435 ap[0]->attrvalue[0] = strdup(dn); 436 437 if (ap[0]->attrvalue[0] == NULL) { 438 ldap_memfree(dn); 439 dn = NULL; 440 __ns_ldap_freeEntry(ep); 441 ep = NULL; 442 return (NS_LDAP_MEMORY); 443 } 444 ldap_memfree(dn); 445 dn = NULL; 446 447 if ((flags & NS_LDAP_NOMAP) == 0 && auto_service && 448 schema_mapping_existed) { 449 rc = __s_api_convert_automountmapname(service, 450 &ap[0]->attrvalue[0], 451 error); 452 if (rc != NS_LDAP_SUCCESS) { 453 __ns_ldap_freeEntry(ep); 454 ep = NULL; 455 return (rc); 456 } 457 } 458 459 /* other attributes */ 460 for (attr = ldap_first_attribute(ld, e, &ber), j = 1; 461 attr != NULL && j != nAttrs; 462 attr = ldap_next_attribute(ld, e, ber), j++) { 463 /* allocate new attr name */ 464 465 if ((ap[j] = (ns_ldap_attr_t *) 466 calloc(1, sizeof (ns_ldap_attr_t))) == NULL) { 467 ber_free(ber, 0); 468 ber = NULL; 469 __ns_ldap_freeEntry(ep); 470 ep = NULL; 471 if (gecos_mapping) 472 __s_api_free2dArray(gecos_mapping); 473 gecos_mapping = NULL; 474 return (NS_LDAP_MEMORY); 475 } 476 477 if ((flags & NS_LDAP_NOMAP) || schema_mapping_existed == FALSE) 478 mapping = NULL; 479 else 480 mapping = __ns_ldap_getOrigAttribute(service, attr); 481 482 if (mapping == NULL && auto_service && 483 schema_mapping_existed && (flags & NS_LDAP_NOMAP) == 0) 484 /* 485 * if service == auto_* and no schema mapping found 486 * and schema_mapping_existed is TRUE and NS_LDAP_NOMAP 487 * is not set then try automount e.g. 488 * NS_LDAP_ATTRIBUTEMAP = automount:automountMapName=AAA 489 */ 490 mapping = __ns_ldap_getOrigAttribute("automount", 491 attr); 492 493 if (mapping == NULL) { 494 if ((ap[j]->attrname = strdup(attr)) == NULL) { 495 ber_free(ber, 0); 496 ber = NULL; 497 __ns_ldap_freeEntry(ep); 498 ep = NULL; 499 if (gecos_mapping) 500 __s_api_free2dArray(gecos_mapping); 501 gecos_mapping = NULL; 502 return (NS_LDAP_MEMORY); 503 } 504 } else { 505 /* 506 * for "gecos" 1 to N mapping, 507 * do not remove the mapped attribute, 508 * just create a new gecos attribute 509 * and append it to the end of the attribute list 510 */ 511 if (strcasecmp(mapping[0], "gecos") == 0) { 512 ap[j]->attrname = strdup(attr); 513 gecos_mapping_existed = TRUE; 514 } else 515 ap[j]->attrname = strdup(mapping[0]); 516 517 if (ap[j]->attrname == NULL) { 518 ber_free(ber, 0); 519 ber = NULL; 520 __ns_ldap_freeEntry(ep); 521 ep = NULL; 522 if (gecos_mapping) 523 __s_api_free2dArray(gecos_mapping); 524 gecos_mapping = NULL; 525 return (NS_LDAP_MEMORY); 526 } 527 /* 528 * 1 to N attribute mapping processing 529 * is only done for "gecos" 530 */ 531 532 if (strcasecmp(mapping[0], "gecos") == 0) { 533 /* 534 * get attribute mapping for "gecos", 535 * need to know the number and order of the 536 * mapped attributes 537 */ 538 if (gecos_mapping == NULL) { 539 gecos_mapping = 540 __ns_ldap_getMappedAttributes( 541 service, mapping[0]); 542 if (gecos_mapping == NULL || 543 gecos_mapping[0] == NULL) { 544 /* 545 * this should never happens, 546 * syslog the error 547 */ 548 (void) sprintf(errstr, 549 gettext( 550 "Attribute mapping " 551 "inconsistency " 552 "found for attributes " 553 "'%s' and '%s'."), 554 mapping[0], attr); 555 syslog(LOG_ERR, "libsldap: %s", 556 errstr); 557 558 ber_free(ber, 0); 559 ber = NULL; 560 __ns_ldap_freeEntry(ep); 561 ep = NULL; 562 __s_api_free2dArray(mapping); 563 mapping = NULL; 564 if (gecos_mapping) 565 __s_api_free2dArray( 566 gecos_mapping); 567 gecos_mapping = NULL; 568 return (NS_LDAP_INTERNAL); 569 } 570 } 571 572 /* 573 * is this attribute the 1st, 2nd, or 574 * 3rd attr in the mapping list? 575 */ 576 gecos_attr_matched = FALSE; 577 for (i = 0; i < 3 && gecos_mapping[i]; i++) { 578 if (gecos_mapping[i] && 579 strcasecmp(gecos_mapping[i], 580 attr) == 0) { 581 gecos_val_index[i] = j; 582 gecos_attr_matched = TRUE; 583 break; 584 } 585 } 586 if (gecos_attr_matched == FALSE) { 587 /* 588 * Not match found. 589 * This should never happens, 590 * syslog the error 591 */ 592 (void) sprintf(errstr, 593 gettext( 594 "Attribute mapping " 595 "inconsistency " 596 "found for attributes " 597 "'%s' and '%s'."), 598 mapping[0], attr); 599 syslog(LOG_ERR, "libsldap: %s", errstr); 600 601 ber_free(ber, 0); 602 ber = NULL; 603 __ns_ldap_freeEntry(ep); 604 ep = NULL; 605 __s_api_free2dArray(mapping); 606 mapping = NULL; 607 __s_api_free2dArray(gecos_mapping); 608 gecos_mapping = NULL; 609 return (NS_LDAP_INTERNAL); 610 } 611 } 612 __s_api_free2dArray(mapping); 613 mapping = NULL; 614 } 615 616 if ((vals = ldap_get_values(ld, e, attr)) != NULL) { 617 618 if ((ap[j]->value_count = 619 ldap_count_values(vals)) == 0) { 620 ldap_value_free(vals); 621 vals = NULL; 622 continue; 623 } else { 624 ap[j]->attrvalue = (char **) 625 calloc(ap[j]->value_count+1, 626 sizeof (char *)); 627 if (ap[j]->attrvalue == NULL) { 628 ber_free(ber, 0); 629 ber = NULL; 630 __ns_ldap_freeEntry(ep); 631 ep = NULL; 632 if (gecos_mapping) 633 __s_api_free2dArray( 634 gecos_mapping); 635 gecos_mapping = NULL; 636 return (NS_LDAP_MEMORY); 637 } 638 } 639 640 /* map object classes if necessary */ 641 if ((flags & NS_LDAP_NOMAP) == 0 && 642 schema_mapping_existed && ap[j]->attrname && 643 strcasecmp(ap[j]->attrname, "objectclass") == 0) { 644 for (k = 0; k < ap[j]->value_count; k++) { 645 mapping = 646 __ns_ldap_getOrigObjectClass( 647 service, vals[k]); 648 649 if (mapping == NULL && auto_service) 650 /* 651 * if service == auto_* and no 652 * schema mapping found 653 * then try automount 654 */ 655 mapping = 656 __ns_ldap_getOrigObjectClass( 657 "automount", vals[k]); 658 659 if (mapping == NULL) { 660 ap[j]->attrvalue[k] = 661 strdup(vals[k]); 662 } else { 663 ap[j]->attrvalue[k] = 664 strdup(mapping[0]); 665 __s_api_free2dArray(mapping); 666 mapping = NULL; 667 } 668 if (ap[j]->attrvalue[k] == NULL) { 669 ber_free(ber, 0); 670 ber = NULL; 671 __ns_ldap_freeEntry(ep); 672 ep = NULL; 673 if (gecos_mapping) 674 __s_api_free2dArray( 675 gecos_mapping); 676 gecos_mapping = NULL; 677 return (NS_LDAP_MEMORY); 678 } 679 } 680 } else { 681 for (k = 0; k < ap[j]->value_count; k++) { 682 if ((ap[j]->attrvalue[k] = 683 strdup(vals[k])) == NULL) { 684 ber_free(ber, 0); 685 ber = NULL; 686 __ns_ldap_freeEntry(ep); 687 ep = NULL; 688 if (gecos_mapping) 689 __s_api_free2dArray( 690 gecos_mapping); 691 gecos_mapping = NULL; 692 return (NS_LDAP_MEMORY); 693 } 694 } 695 } 696 697 ap[j]->attrvalue[k] = NULL; 698 ldap_value_free(vals); 699 vals = NULL; 700 } 701 702 ldap_memfree(attr); 703 attr = NULL; 704 } 705 706 ber_free(ber, 0); 707 ber = NULL; 708 709 if (gecos_mapping) { 710 __s_api_free2dArray(gecos_mapping); 711 gecos_mapping = NULL; 712 } 713 714 /* special processing for gecos 1 to up to 3 attribute mapping */ 715 if (schema_mapping_existed && gecos_mapping_existed) { 716 717 int f = -1; 718 719 for (i = 0; i < 3; i++) { 720 k = gecos_val_index[i]; 721 722 /* 723 * f is the index of the first returned 724 * attribute which "gecos" attribute mapped to 725 */ 726 if (k != -1 && f == -1) 727 f = k; 728 729 if (k != -1 && ap[k]->value_count > 0 && 730 ap[k]->attrvalue[0] && 731 strlen(ap[k]->attrvalue[0]) > 0) { 732 733 if (k == f) { 734 /* 735 * Create and fill in the last reserved 736 * ap with the data from the "gecos" 737 * mapping attributes 738 */ 739 ap[nAttrs] = (ns_ldap_attr_t *) 740 calloc(1, 741 sizeof (ns_ldap_attr_t)); 742 if (ap[nAttrs] == NULL) { 743 __ns_ldap_freeEntry(ep); 744 ep = NULL; 745 return (NS_LDAP_MEMORY); 746 } 747 ap[nAttrs]->attrvalue = (char **)calloc( 748 2, sizeof (char *)); 749 if (ap[nAttrs]->attrvalue == NULL) { 750 __ns_ldap_freeEntry(ep); 751 ep = NULL; 752 return (NS_LDAP_MEMORY); 753 } 754 /* add 1 more for a possible "," */ 755 ap[nAttrs]->attrvalue[0] = 756 (char *)calloc( 757 strlen(ap[f]->attrvalue[0]) + 758 2, 1); 759 if (ap[nAttrs]->attrvalue[0] == NULL) { 760 __ns_ldap_freeEntry(ep); 761 ep = NULL; 762 return (NS_LDAP_MEMORY); 763 } 764 (void) strcpy(ap[nAttrs]->attrvalue[0], 765 ap[f]->attrvalue[0]); 766 767 ap[nAttrs]->attrname = strdup("gecos"); 768 if (ap[nAttrs]->attrname == NULL) { 769 __ns_ldap_freeEntry(ep); 770 ep = NULL; 771 return (NS_LDAP_MEMORY); 772 } 773 774 ap[nAttrs]->value_count = 1; 775 ep->attr_count = nAttrs + 1; 776 777 } else { 778 char *tmp = NULL; 779 780 /* 781 * realloc to add "," and 782 * ap[k]->attrvalue[0] 783 */ 784 tmp = (char *)realloc( 785 ap[nAttrs]->attrvalue[0], 786 strlen(ap[nAttrs]-> 787 attrvalue[0]) + 788 strlen(ap[k]-> 789 attrvalue[0]) + 2); 790 if (tmp == NULL) { 791 __ns_ldap_freeEntry(ep); 792 ep = NULL; 793 return (NS_LDAP_MEMORY); 794 } 795 ap[nAttrs]->attrvalue[0] = tmp; 796 (void) strcat(ap[nAttrs]->attrvalue[0], 797 ","); 798 (void) strcat(ap[nAttrs]->attrvalue[0], 799 ap[k]->attrvalue[0]); 800 } 801 } 802 } 803 } 804 805 *ret = ep; 806 return (NS_LDAP_SUCCESS); 807 } 808 809 static int 810 __s_api_getEntry(ns_ldap_cookie_t *cookie) 811 { 812 ns_ldap_entry_t *curEntry = NULL; 813 int ret; 814 815 #ifdef DEBUG 816 (void) fprintf(stderr, "__s_api_getEntry START\n"); 817 #endif 818 819 if (cookie->resultMsg == NULL) { 820 return (NS_LDAP_INVALID_PARAM); 821 } 822 ret = __s_api_cvtEntry(cookie->conn->ld, cookie->service, 823 cookie->resultMsg, cookie->i_flags, 824 &curEntry, &cookie->errorp); 825 if (ret != NS_LDAP_SUCCESS) { 826 return (ret); 827 } 828 829 if (cookie->result == NULL) { 830 cookie->result = (ns_ldap_result_t *) 831 calloc(1, sizeof (ns_ldap_result_t)); 832 if (cookie->result == NULL) { 833 __ns_ldap_freeEntry(curEntry); 834 curEntry = NULL; 835 return (NS_LDAP_MEMORY); 836 } 837 cookie->result->entry = curEntry; 838 cookie->nextEntry = curEntry; 839 } else { 840 cookie->nextEntry->next = curEntry; 841 cookie->nextEntry = curEntry; 842 } 843 cookie->result->entries_count++; 844 845 return (NS_LDAP_SUCCESS); 846 } 847 848 static int 849 __s_api_get_cachemgr_data(const char *type, 850 const char *from, char **to) 851 { 852 union { 853 ldap_data_t s_d; 854 char s_b[DOORBUFFERSIZE]; 855 } space; 856 ldap_data_t *sptr; 857 int ndata; 858 int adata; 859 int rc; 860 861 #ifdef DEBUG 862 (void) fprintf(stderr, "__s_api_get_cachemgr_data START\n"); 863 #endif 864 /* 865 * We are not going to perform DN to domain mapping 866 * in the Standalone mode 867 */ 868 if (__s_api_isStandalone()) { 869 return (-1); 870 } 871 872 if (from == NULL || from[0] == '\0' || to == NULL) 873 return (-1); 874 875 *to = NULL; 876 (void) memset(space.s_b, 0, DOORBUFFERSIZE); 877 878 space.s_d.ldap_call.ldap_callnumber = GETCACHE; 879 (void) snprintf(space.s_d.ldap_call.ldap_u.domainname, 880 DOORBUFFERSIZE - sizeof (space.s_d.ldap_call.ldap_callnumber), 881 "%s%s%s", 882 type, 883 DOORLINESEP, 884 from); 885 ndata = sizeof (space); 886 adata = sizeof (ldap_call_t) + 887 strlen(space.s_d.ldap_call.ldap_u.domainname) + 1; 888 sptr = &space.s_d; 889 890 rc = __ns_ldap_trydoorcall(&sptr, &ndata, &adata); 891 if (rc != NS_CACHE_SUCCESS) 892 return (-1); 893 else 894 *to = strdup(sptr->ldap_ret.ldap_u.buff); 895 return (NS_LDAP_SUCCESS); 896 } 897 898 static int 899 __s_api_set_cachemgr_data(const char *type, 900 const char *from, const char *to) 901 { 902 union { 903 ldap_data_t s_d; 904 char s_b[DOORBUFFERSIZE]; 905 } space; 906 ldap_data_t *sptr; 907 int ndata; 908 int adata; 909 int rc; 910 911 #ifdef DEBUG 912 (void) fprintf(stderr, "__s_api_set_cachemgr_data START\n"); 913 #endif 914 /* 915 * We are not going to perform DN to domain mapping 916 * in the Standalone mode 917 */ 918 if (__s_api_isStandalone()) { 919 return (-1); 920 } 921 922 if ((from == NULL) || (from[0] == '\0') || 923 (to == NULL) || (to[0] == '\0')) 924 return (-1); 925 926 (void) memset(space.s_b, 0, DOORBUFFERSIZE); 927 928 space.s_d.ldap_call.ldap_callnumber = SETCACHE; 929 (void) snprintf(space.s_d.ldap_call.ldap_u.domainname, 930 DOORBUFFERSIZE - sizeof (space.s_d.ldap_call.ldap_callnumber), 931 "%s%s%s%s%s", 932 type, 933 DOORLINESEP, 934 from, 935 DOORLINESEP, 936 to); 937 938 ndata = sizeof (space); 939 adata = sizeof (ldap_call_t) + 940 strlen(space.s_d.ldap_call.ldap_u.domainname) + 1; 941 sptr = &space.s_d; 942 943 rc = __ns_ldap_trydoorcall(&sptr, &ndata, &adata); 944 if (rc != NS_CACHE_SUCCESS) 945 return (-1); 946 947 return (NS_LDAP_SUCCESS); 948 } 949 950 951 static char * 952 __s_api_remove_rdn_space(char *rdn) 953 { 954 char *tf, *tl, *vf, *vl, *eqsign; 955 956 /* if no space(s) to remove, return */ 957 if (strchr(rdn, SPACETOK) == NULL) 958 return (rdn); 959 960 /* if no '=' separator, return */ 961 eqsign = strchr(rdn, '='); 962 if (eqsign == NULL) 963 return (rdn); 964 965 tf = rdn; 966 tl = eqsign - 1; 967 vf = eqsign + 1; 968 vl = rdn + strlen(rdn) - 1; 969 970 /* now two strings, type and value */ 971 *eqsign = '\0'; 972 973 /* remove type's leading spaces */ 974 while (tf < tl && *tf == SPACETOK) 975 tf++; 976 /* remove type's trailing spaces */ 977 while (tf < tl && *tl == SPACETOK) 978 tl--; 979 /* add '=' separator back */ 980 *(++tl) = '='; 981 /* remove value's leading spaces */ 982 while (vf < vl && *vf == SPACETOK) 983 vf++; 984 /* remove value's trailing spaces */ 985 while (vf < vl && *vl == SPACETOK) 986 *vl-- = '\0'; 987 988 /* move value up if necessary */ 989 if (vf != tl + 1) 990 (void) strcpy(tl + 1, vf); 991 992 return (tf); 993 } 994 995 static 996 ns_ldap_cookie_t * 997 init_search_state_machine() 998 { 999 ns_ldap_cookie_t *cookie; 1000 ns_config_t *cfg; 1001 1002 cookie = (ns_ldap_cookie_t *)calloc(1, sizeof (ns_ldap_cookie_t)); 1003 if (cookie == NULL) 1004 return (NULL); 1005 cookie->state = INIT; 1006 /* assign other state variables */ 1007 cfg = __s_api_loadrefresh_config(); 1008 cookie->connectionId = -1; 1009 if (cfg == NULL || 1010 cfg->paramList[NS_LDAP_SEARCH_TIME_P].ns_ptype == NS_UNKNOWN) { 1011 cookie->search_timeout.tv_sec = NS_DEFAULT_SEARCH_TIMEOUT; 1012 } else { 1013 cookie->search_timeout.tv_sec = 1014 cfg->paramList[NS_LDAP_SEARCH_TIME_P].ns_i; 1015 } 1016 if (cfg != NULL) 1017 __s_api_release_config(cfg); 1018 cookie->search_timeout.tv_usec = 0; 1019 1020 return (cookie); 1021 } 1022 1023 static void 1024 delete_search_cookie(ns_ldap_cookie_t *cookie) 1025 { 1026 if (cookie == NULL) 1027 return; 1028 if (cookie->connectionId > -1) 1029 DropConnection(cookie->connectionId, cookie->i_flags); 1030 if (cookie->filter) 1031 free(cookie->filter); 1032 if (cookie->i_filter) 1033 free(cookie->i_filter); 1034 if (cookie->service) 1035 free(cookie->service); 1036 if (cookie->sdlist) 1037 (void) __ns_ldap_freeSearchDescriptors(&(cookie->sdlist)); 1038 if (cookie->result) 1039 (void) __ns_ldap_freeResult(&cookie->result); 1040 if (cookie->attribute) 1041 __s_api_free2dArray(cookie->attribute); 1042 if (cookie->errorp) 1043 (void) __ns_ldap_freeError(&cookie->errorp); 1044 if (cookie->reflist) 1045 __s_api_deleteRefInfo(cookie->reflist); 1046 if (cookie->basedn) 1047 free(cookie->basedn); 1048 if (cookie->ctrlCookie) 1049 ber_bvfree(cookie->ctrlCookie); 1050 _freeControlList(&cookie->p_serverctrls); 1051 if (cookie->resultctrl) 1052 ldap_controls_free(cookie->resultctrl); 1053 free(cookie); 1054 } 1055 1056 static int 1057 get_mapped_filter(ns_ldap_cookie_t *cookie, char **new_filter) 1058 { 1059 1060 typedef struct filter_mapping_info { 1061 char oc_or_attr; 1062 char *name_start; 1063 char *name_end; 1064 char *veq_pos; 1065 char *from_name; 1066 char *to_name; 1067 char **mapping; 1068 } filter_mapping_info_t; 1069 1070 char *c, *last_copied; 1071 char *filter_c, *filter_c_next; 1072 char *key, *tail, *head; 1073 char errstr[MAXERROR]; 1074 int num_eq = 0, num_veq = 0; 1075 int in_quote = FALSE; 1076 int is_value = FALSE; 1077 int i, j, oc_len, len; 1078 int at_least_one = FALSE; 1079 filter_mapping_info_t **info, *info1; 1080 char **mapping; 1081 char *service, *filter, *err; 1082 int auto_service = FALSE; 1083 1084 if (cookie == NULL || new_filter == NULL) 1085 return (NS_LDAP_INVALID_PARAM); 1086 1087 *new_filter = NULL; 1088 service = cookie->service; 1089 filter = cookie->filter; 1090 1091 /* 1092 * count the number of '=' char 1093 */ 1094 for (c = filter; *c; c++) { 1095 if (*c == TOKENSEPARATOR) 1096 num_eq++; 1097 } 1098 1099 if (service != NULL && strncasecmp(service, "auto_", 5) == 0) 1100 auto_service = TRUE; 1101 1102 /* 1103 * See if schema mapping existed for the given service. 1104 * If not, just return success. 1105 */ 1106 mapping = __ns_ldap_getOrigAttribute(service, 1107 NS_HASH_SCHEMA_MAPPING_EXISTED); 1108 1109 if (mapping == NULL && auto_service) 1110 /* 1111 * if service == auto_* and no 1112 * schema mapping found 1113 * then try automount 1114 */ 1115 mapping = __ns_ldap_getOrigAttribute( 1116 "automount", NS_HASH_SCHEMA_MAPPING_EXISTED); 1117 1118 if (mapping) 1119 __s_api_free2dArray(mapping); 1120 else 1121 return (NS_LDAP_SUCCESS); 1122 1123 /* 1124 * no '=' sign, just say OK and return nothing 1125 */ 1126 if (num_eq == 0) 1127 return (NS_LDAP_SUCCESS); 1128 1129 /* 1130 * Make a copy of the filter string 1131 * for saving the name of the objectclasses or 1132 * attributes that need to be passed to the 1133 * objectclass or attribute mapping functions. 1134 * pointer "info->from_name" points to the locations 1135 * within this string. 1136 * 1137 * The input filter string, filter, will be used 1138 * to indicate where these names start and end. 1139 * pointers "info->name_start" and "info->name_end" 1140 * point to locations within the input filter string, 1141 * and are used at the end of this function to 1142 * merge the original filter data with the 1143 * mapped objectclass or attribute names. 1144 */ 1145 filter_c = strdup(filter); 1146 if (filter_c == NULL) 1147 return (NS_LDAP_MEMORY); 1148 filter_c_next = filter_c; 1149 1150 /* 1151 * get memory for info arrays 1152 */ 1153 info = (filter_mapping_info_t **)calloc(num_eq + 1, 1154 sizeof (filter_mapping_info_t *)); 1155 1156 if (info == NULL) { 1157 free(filter_c); 1158 return (NS_LDAP_MEMORY); 1159 } 1160 1161 /* 1162 * find valid '=' for further processing, 1163 * ignore the "escaped =" (.i.e. "\="), or 1164 * "=" in quoted string 1165 */ 1166 for (c = filter_c; *c; c++) { 1167 1168 switch (*c) { 1169 case TOKENSEPARATOR: 1170 if (!in_quote && !is_value) { 1171 info1 = (filter_mapping_info_t *)calloc(1, 1172 sizeof (filter_mapping_info_t)); 1173 if (!info1) { 1174 free(filter_c); 1175 for (i = 0; i < num_veq; i++) 1176 free(info[i]); 1177 free(info); 1178 return (NS_LDAP_MEMORY); 1179 } 1180 info[num_veq] = info1; 1181 1182 /* 1183 * remember the location of this "=" 1184 */ 1185 info[num_veq++]->veq_pos = c; 1186 1187 /* 1188 * skip until the end of the attribute value 1189 */ 1190 is_value = TRUE; 1191 } 1192 break; 1193 case CPARATOK: 1194 /* 1195 * mark the end of the attribute value 1196 */ 1197 if (!in_quote) 1198 is_value = FALSE; 1199 break; 1200 case QUOTETOK: 1201 /* 1202 * switch on/off the in_quote mode 1203 */ 1204 in_quote = (in_quote == FALSE); 1205 break; 1206 case '\\': 1207 /* 1208 * ignore escape characters 1209 * don't skip if next char is '\0' 1210 */ 1211 if (!in_quote) 1212 if (*(++c) == '\0') 1213 c--; 1214 break; 1215 } 1216 1217 } 1218 1219 /* 1220 * for each valid "=" found, get the name to 1221 * be mapped 1222 */ 1223 oc_len = strlen("objectclass"); 1224 for (i = 0; i < num_veq; i++) { 1225 1226 /* 1227 * look at the left side of "=" to see 1228 * if assertion is "objectclass=<ocname>" 1229 * or "<attribute name>=<attribute value>" 1230 * 1231 * first skip spaces before "=". 1232 * Note that filter_c_next may not point to the 1233 * start of the filter string. For i > 0, 1234 * it points to the end of the last name processed + 2 1235 */ 1236 for (tail = info[i]->veq_pos; (tail > filter_c_next) && 1237 (*(tail - 1) == SPACETOK); tail--) 1238 ; 1239 1240 /* 1241 * mark the end of the left side string (the key) 1242 */ 1243 *tail = '\0'; 1244 info[i]->name_end = tail - filter_c - 1 + filter; 1245 1246 /* 1247 * find the start of the key 1248 */ 1249 key = filter_c_next; 1250 for (c = tail; filter_c_next <= c; c--) { 1251 /* OPARATOK is '(' */ 1252 if (*c == OPARATOK || 1253 *c == SPACETOK) { 1254 key = c + 1; 1255 break; 1256 } 1257 } 1258 info[i]->name_start = key - filter_c + filter; 1259 1260 if ((key + oc_len) <= tail) { 1261 if (strncasecmp(key, "objectclass", 1262 oc_len) == 0) { 1263 /* 1264 * assertion is "objectclass=ocname", 1265 * ocname is the one needs to be mapped 1266 * 1267 * skip spaces after "=" to find start 1268 * of the ocname 1269 */ 1270 head = info[i]->veq_pos; 1271 for (head = info[i]->veq_pos + 1; 1272 *head && *head == SPACETOK; head++) 1273 ; 1274 1275 /* ignore empty ocname */ 1276 if (!(*head)) 1277 continue; 1278 1279 info[i]->name_start = head - filter_c + 1280 filter; 1281 1282 /* 1283 * now find the end of the ocname 1284 */ 1285 for (c = head; ; c++) { 1286 /* CPARATOK is ')' */ 1287 if (*c == CPARATOK || 1288 *c == '\0' || 1289 *c == SPACETOK) { 1290 *c = '\0'; 1291 info[i]->name_end = 1292 c - filter_c - 1 + 1293 filter; 1294 filter_c_next = c + 1; 1295 info[i]->oc_or_attr = 'o'; 1296 info[i]->from_name = head; 1297 break; 1298 } 1299 } 1300 } 1301 } 1302 1303 /* 1304 * assertion is not "objectclass=ocname", 1305 * assume assertion is "<key> = <value>", 1306 * <key> is the one needs to be mapped 1307 */ 1308 if (info[i]->from_name == NULL && strlen(key) > 0) { 1309 info[i]->oc_or_attr = 'a'; 1310 info[i]->from_name = key; 1311 } 1312 } 1313 1314 /* perform schema mapping */ 1315 for (i = 0; i < num_veq; i++) { 1316 if (info[i]->from_name == NULL) 1317 continue; 1318 1319 if (info[i]->oc_or_attr == 'a') 1320 info[i]->mapping = 1321 __ns_ldap_getMappedAttributes(service, 1322 info[i]->from_name); 1323 else 1324 info[i]->mapping = 1325 __ns_ldap_getMappedObjectClass(service, 1326 info[i]->from_name); 1327 1328 if (info[i]->mapping == NULL && auto_service) { 1329 /* 1330 * If no mapped attribute/objectclass is found 1331 * and service == auto* 1332 * try to find automount's 1333 * mapped attribute/objectclass 1334 */ 1335 if (info[i]->oc_or_attr == 'a') 1336 info[i]->mapping = 1337 __ns_ldap_getMappedAttributes("automount", 1338 info[i]->from_name); 1339 else 1340 info[i]->mapping = 1341 __ns_ldap_getMappedObjectClass("automount", 1342 info[i]->from_name); 1343 } 1344 1345 if (info[i]->mapping == NULL || 1346 info[i]->mapping[0] == NULL) { 1347 info[i]->to_name = NULL; 1348 } else if (info[i]->mapping[1] == NULL) { 1349 info[i]->to_name = info[i]->mapping[0]; 1350 at_least_one = TRUE; 1351 } else { 1352 __s_api_free2dArray(info[i]->mapping); 1353 /* 1354 * multiple mapping 1355 * not allowed 1356 */ 1357 (void) sprintf(errstr, 1358 gettext( 1359 "Multiple attribute or objectclass " 1360 "mapping for '%s' in filter " 1361 "'%s' not allowed."), 1362 info[i]->from_name, filter); 1363 err = strdup(errstr); 1364 if (err) 1365 MKERROR(LOG_WARNING, cookie->errorp, 1366 NS_CONFIG_SYNTAX, 1367 err, NULL); 1368 1369 free(filter_c); 1370 for (j = 0; j < num_veq; j++) { 1371 if (info[j]->mapping) 1372 __s_api_free2dArray( 1373 info[j]->mapping); 1374 free(info[j]); 1375 } 1376 free(info); 1377 return (NS_LDAP_CONFIG); 1378 } 1379 } 1380 1381 1382 if (at_least_one) { 1383 1384 len = strlen(filter); 1385 last_copied = filter - 1; 1386 1387 for (i = 0; i < num_veq; i++) { 1388 if (info[i]->to_name) 1389 len += strlen(info[i]->to_name); 1390 } 1391 1392 *new_filter = (char *)calloc(1, len); 1393 if (*new_filter == NULL) { 1394 free(filter_c); 1395 for (j = 0; j < num_veq; j++) { 1396 if (info[j]->mapping) 1397 __s_api_free2dArray( 1398 info[j]->mapping); 1399 free(info[j]); 1400 } 1401 free(info); 1402 return (NS_LDAP_MEMORY); 1403 } 1404 1405 for (i = 0; i < num_veq; i++) { 1406 if (info[i]->to_name != NULL && 1407 info[i]->to_name != NULL) { 1408 1409 /* 1410 * copy the original filter data 1411 * between the last name and current 1412 * name 1413 */ 1414 if ((last_copied + 1) != info[i]->name_start) 1415 (void) strncat(*new_filter, 1416 last_copied + 1, 1417 info[i]->name_start - 1418 last_copied - 1); 1419 1420 /* the data is copied */ 1421 last_copied = info[i]->name_end; 1422 1423 /* 1424 * replace the name with 1425 * the mapped name 1426 */ 1427 (void) strcat(*new_filter, info[i]->to_name); 1428 } 1429 1430 /* copy the filter data after the last name */ 1431 if (i == (num_veq -1) && 1432 info[i]->name_end < 1433 (filter + strlen(filter))) 1434 (void) strncat(*new_filter, last_copied + 1, 1435 filter + strlen(filter) - 1436 last_copied - 1); 1437 } 1438 1439 } 1440 1441 /* free memory */ 1442 free(filter_c); 1443 for (j = 0; j < num_veq; j++) { 1444 if (info[j]->mapping) 1445 __s_api_free2dArray(info[j]->mapping); 1446 free(info[j]); 1447 } 1448 free(info); 1449 1450 return (NS_LDAP_SUCCESS); 1451 } 1452 1453 static int 1454 setup_next_search(ns_ldap_cookie_t *cookie) 1455 { 1456 ns_ldap_search_desc_t *dptr; 1457 int scope; 1458 char *filter, *str; 1459 int baselen; 1460 int rc; 1461 void **param; 1462 1463 dptr = *cookie->sdpos; 1464 scope = cookie->i_flags & (NS_LDAP_SCOPE_BASE | 1465 NS_LDAP_SCOPE_ONELEVEL | 1466 NS_LDAP_SCOPE_SUBTREE); 1467 if (scope) 1468 cookie->scope = scope; 1469 else 1470 cookie->scope = dptr->scope; 1471 switch (cookie->scope) { 1472 case NS_LDAP_SCOPE_BASE: 1473 cookie->scope = LDAP_SCOPE_BASE; 1474 break; 1475 case NS_LDAP_SCOPE_ONELEVEL: 1476 cookie->scope = LDAP_SCOPE_ONELEVEL; 1477 break; 1478 case NS_LDAP_SCOPE_SUBTREE: 1479 cookie->scope = LDAP_SCOPE_SUBTREE; 1480 break; 1481 } 1482 1483 filter = NULL; 1484 if (cookie->use_filtercb && cookie->init_filter_cb && 1485 dptr->filter && strlen(dptr->filter) > 0) { 1486 (*cookie->init_filter_cb)(dptr, &filter, 1487 cookie->userdata); 1488 } 1489 if (filter == NULL) { 1490 if (cookie->i_filter == NULL) { 1491 cookie->err_rc = NS_LDAP_INVALID_PARAM; 1492 return (-1); 1493 } else { 1494 if (cookie->filter) 1495 free(cookie->filter); 1496 cookie->filter = strdup(cookie->i_filter); 1497 if (cookie->filter == NULL) { 1498 cookie->err_rc = NS_LDAP_MEMORY; 1499 return (-1); 1500 } 1501 } 1502 } else { 1503 if (cookie->filter) 1504 free(cookie->filter); 1505 cookie->filter = strdup(filter); 1506 free(filter); 1507 if (cookie->filter == NULL) { 1508 cookie->err_rc = NS_LDAP_MEMORY; 1509 return (-1); 1510 } 1511 } 1512 1513 /* 1514 * perform attribute/objectclass mapping on filter 1515 */ 1516 filter = NULL; 1517 1518 if (cookie->service) { 1519 rc = get_mapped_filter(cookie, &filter); 1520 if (rc != NS_LDAP_SUCCESS) { 1521 cookie->err_rc = rc; 1522 return (-1); 1523 } else { 1524 /* 1525 * get_mapped_filter returns 1526 * NULL filter pointer, if 1527 * no mapping was done 1528 */ 1529 if (filter) { 1530 free(cookie->filter); 1531 cookie->filter = filter; 1532 } 1533 } 1534 } 1535 1536 /* 1537 * validate filter to make sure it's legal 1538 * [remove redundant ()'s] 1539 */ 1540 rc = validate_filter(cookie); 1541 if (rc != NS_LDAP_SUCCESS) { 1542 cookie->err_rc = rc; 1543 return (-1); 1544 } 1545 1546 baselen = strlen(dptr->basedn); 1547 if (baselen > 0 && dptr->basedn[baselen-1] == COMMATOK) { 1548 rc = __ns_ldap_getParam(NS_LDAP_SEARCH_BASEDN_P, 1549 (void ***)¶m, &cookie->errorp); 1550 if (rc != NS_LDAP_SUCCESS) { 1551 cookie->err_rc = rc; 1552 return (-1); 1553 } 1554 str = ((char **)param)[0]; 1555 baselen += strlen(str)+1; 1556 if (cookie->basedn) 1557 free(cookie->basedn); 1558 cookie->basedn = (char *)malloc(baselen); 1559 if (cookie->basedn == NULL) { 1560 cookie->err_rc = NS_LDAP_MEMORY; 1561 return (-1); 1562 } 1563 (void) strcpy(cookie->basedn, dptr->basedn); 1564 (void) strcat(cookie->basedn, str); 1565 (void) __ns_ldap_freeParam(¶m); 1566 } else { 1567 if (cookie->basedn) 1568 free(cookie->basedn); 1569 cookie->basedn = strdup(dptr->basedn); 1570 } 1571 return (0); 1572 } 1573 1574 static int 1575 setup_referral_search(ns_ldap_cookie_t *cookie) 1576 { 1577 ns_referral_info_t *ref; 1578 1579 ref = cookie->refpos; 1580 cookie->scope = ref->refScope; 1581 if (cookie->filter) { 1582 free(cookie->filter); 1583 } 1584 cookie->filter = strdup(ref->refFilter); 1585 if (cookie->basedn) { 1586 free(cookie->basedn); 1587 } 1588 cookie->basedn = strdup(ref->refDN); 1589 if (cookie->filter == NULL || cookie->basedn == NULL) { 1590 cookie->err_rc = NS_LDAP_MEMORY; 1591 return (-1); 1592 } 1593 return (0); 1594 } 1595 1596 static int 1597 get_current_session(ns_ldap_cookie_t *cookie) 1598 { 1599 ConnectionID connectionId = -1; 1600 Connection *conp = NULL; 1601 int rc; 1602 int fail_if_new_pwd_reqd = 1; 1603 1604 rc = __s_api_getConnection(NULL, cookie->i_flags, 1605 cookie->i_auth, &connectionId, &conp, 1606 &cookie->errorp, fail_if_new_pwd_reqd, 1607 cookie->nopasswd_acct_mgmt, cookie->conn_user); 1608 1609 /* 1610 * If password control attached in *cookie->errorp, 1611 * e.g. rc == NS_LDAP_SUCCESS_WITH_INFO, 1612 * free the error structure (we do not need 1613 * the sec_to_expired info). 1614 * Reset rc to NS_LDAP_SUCCESS. 1615 */ 1616 if (rc == NS_LDAP_SUCCESS_WITH_INFO) { 1617 (void) __ns_ldap_freeError( 1618 &cookie->errorp); 1619 cookie->errorp = NULL; 1620 rc = NS_LDAP_SUCCESS; 1621 } 1622 1623 if (rc != NS_LDAP_SUCCESS) { 1624 cookie->err_rc = rc; 1625 return (-1); 1626 } 1627 cookie->conn = conp; 1628 cookie->connectionId = connectionId; 1629 1630 return (0); 1631 } 1632 1633 static int 1634 get_next_session(ns_ldap_cookie_t *cookie) 1635 { 1636 ConnectionID connectionId = -1; 1637 Connection *conp = NULL; 1638 int rc; 1639 int fail_if_new_pwd_reqd = 1; 1640 1641 if (cookie->connectionId > -1) { 1642 DropConnection(cookie->connectionId, cookie->i_flags); 1643 cookie->connectionId = -1; 1644 } 1645 1646 /* If using a MT connection, return it. */ 1647 if (cookie->conn_user != NULL && 1648 cookie->conn_user->conn_mt != NULL) 1649 __s_api_conn_mt_return(cookie->conn_user); 1650 1651 rc = __s_api_getConnection(NULL, cookie->i_flags, 1652 cookie->i_auth, &connectionId, &conp, 1653 &cookie->errorp, fail_if_new_pwd_reqd, 1654 cookie->nopasswd_acct_mgmt, cookie->conn_user); 1655 1656 /* 1657 * If password control attached in *cookie->errorp, 1658 * e.g. rc == NS_LDAP_SUCCESS_WITH_INFO, 1659 * free the error structure (we do not need 1660 * the sec_to_expired info). 1661 * Reset rc to NS_LDAP_SUCCESS. 1662 */ 1663 if (rc == NS_LDAP_SUCCESS_WITH_INFO) { 1664 (void) __ns_ldap_freeError( 1665 &cookie->errorp); 1666 cookie->errorp = NULL; 1667 rc = NS_LDAP_SUCCESS; 1668 } 1669 1670 if (rc != NS_LDAP_SUCCESS) { 1671 cookie->err_rc = rc; 1672 return (-1); 1673 } 1674 cookie->conn = conp; 1675 cookie->connectionId = connectionId; 1676 return (0); 1677 } 1678 1679 static int 1680 get_referral_session(ns_ldap_cookie_t *cookie) 1681 { 1682 ConnectionID connectionId = -1; 1683 Connection *conp = NULL; 1684 int rc; 1685 int fail_if_new_pwd_reqd = 1; 1686 1687 if (cookie->connectionId > -1) { 1688 DropConnection(cookie->connectionId, cookie->i_flags); 1689 cookie->connectionId = -1; 1690 } 1691 1692 /* set it up to use a connection opened for referral */ 1693 if (cookie->conn_user != NULL) { 1694 /* If using a MT connection, return it. */ 1695 if (cookie->conn_user->conn_mt != NULL) 1696 __s_api_conn_mt_return(cookie->conn_user); 1697 cookie->conn_user->referral = B_TRUE; 1698 } 1699 1700 rc = __s_api_getConnection(cookie->refpos->refHost, 0, 1701 cookie->i_auth, &connectionId, &conp, 1702 &cookie->errorp, fail_if_new_pwd_reqd, 1703 cookie->nopasswd_acct_mgmt, cookie->conn_user); 1704 1705 /* 1706 * If password control attached in *cookie->errorp, 1707 * e.g. rc == NS_LDAP_SUCCESS_WITH_INFO, 1708 * free the error structure (we do not need 1709 * the sec_to_expired info). 1710 * Reset rc to NS_LDAP_SUCCESS. 1711 */ 1712 if (rc == NS_LDAP_SUCCESS_WITH_INFO) { 1713 (void) __ns_ldap_freeError( 1714 &cookie->errorp); 1715 cookie->errorp = NULL; 1716 rc = NS_LDAP_SUCCESS; 1717 } 1718 1719 if (rc != NS_LDAP_SUCCESS) { 1720 cookie->err_rc = rc; 1721 return (-1); 1722 } 1723 cookie->conn = conp; 1724 cookie->connectionId = connectionId; 1725 return (0); 1726 } 1727 1728 static int 1729 paging_supported(ns_ldap_cookie_t *cookie) 1730 { 1731 int rc; 1732 1733 cookie->listType = 0; 1734 rc = __s_api_isCtrlSupported(cookie->conn, 1735 LDAP_CONTROL_VLVREQUEST); 1736 if (rc == NS_LDAP_SUCCESS) { 1737 cookie->listType = VLVCTRLFLAG; 1738 return (1); 1739 } 1740 rc = __s_api_isCtrlSupported(cookie->conn, 1741 LDAP_CONTROL_SIMPLE_PAGE); 1742 if (rc == NS_LDAP_SUCCESS) { 1743 cookie->listType = SIMPLEPAGECTRLFLAG; 1744 return (1); 1745 } 1746 return (0); 1747 } 1748 1749 static int 1750 setup_vlv_params(ns_ldap_cookie_t *cookie) 1751 { 1752 LDAPControl **ctrls; 1753 LDAPsortkey **sortkeylist; 1754 LDAPControl *sortctrl = NULL; 1755 LDAPControl *vlvctrl = NULL; 1756 LDAPVirtualList vlist; 1757 int rc; 1758 1759 _freeControlList(&cookie->p_serverctrls); 1760 1761 rc = ldap_create_sort_keylist(&sortkeylist, SORTKEYLIST); 1762 if (rc != LDAP_SUCCESS) { 1763 (void) ldap_get_option(cookie->conn->ld, 1764 LDAP_OPT_ERROR_NUMBER, &rc); 1765 return (rc); 1766 } 1767 rc = ldap_create_sort_control(cookie->conn->ld, 1768 sortkeylist, 1, &sortctrl); 1769 ldap_free_sort_keylist(sortkeylist); 1770 if (rc != LDAP_SUCCESS) { 1771 (void) ldap_get_option(cookie->conn->ld, 1772 LDAP_OPT_ERROR_NUMBER, &rc); 1773 return (rc); 1774 } 1775 1776 vlist.ldvlist_index = cookie->index; 1777 vlist.ldvlist_size = 0; 1778 1779 vlist.ldvlist_before_count = 0; 1780 vlist.ldvlist_after_count = LISTPAGESIZE-1; 1781 vlist.ldvlist_attrvalue = NULL; 1782 vlist.ldvlist_extradata = NULL; 1783 1784 rc = ldap_create_virtuallist_control(cookie->conn->ld, 1785 &vlist, &vlvctrl); 1786 if (rc != LDAP_SUCCESS) { 1787 ldap_control_free(sortctrl); 1788 (void) ldap_get_option(cookie->conn->ld, LDAP_OPT_ERROR_NUMBER, 1789 &rc); 1790 return (rc); 1791 } 1792 1793 ctrls = (LDAPControl **)calloc(3, sizeof (LDAPControl *)); 1794 if (ctrls == NULL) { 1795 ldap_control_free(sortctrl); 1796 ldap_control_free(vlvctrl); 1797 return (LDAP_NO_MEMORY); 1798 } 1799 1800 ctrls[0] = sortctrl; 1801 ctrls[1] = vlvctrl; 1802 1803 cookie->p_serverctrls = ctrls; 1804 return (LDAP_SUCCESS); 1805 } 1806 1807 static int 1808 setup_simplepg_params(ns_ldap_cookie_t *cookie) 1809 { 1810 LDAPControl **ctrls; 1811 LDAPControl *pgctrl = NULL; 1812 int rc; 1813 1814 _freeControlList(&cookie->p_serverctrls); 1815 1816 rc = ldap_create_page_control(cookie->conn->ld, LISTPAGESIZE, 1817 cookie->ctrlCookie, (char)0, &pgctrl); 1818 if (rc != LDAP_SUCCESS) { 1819 (void) ldap_get_option(cookie->conn->ld, LDAP_OPT_ERROR_NUMBER, 1820 &rc); 1821 return (rc); 1822 } 1823 1824 ctrls = (LDAPControl **)calloc(2, sizeof (LDAPControl *)); 1825 if (ctrls == NULL) { 1826 ldap_control_free(pgctrl); 1827 return (LDAP_NO_MEMORY); 1828 } 1829 ctrls[0] = pgctrl; 1830 cookie->p_serverctrls = ctrls; 1831 return (LDAP_SUCCESS); 1832 } 1833 1834 static void 1835 proc_result_referrals(ns_ldap_cookie_t *cookie) 1836 { 1837 int errCode, i, rc; 1838 char **referrals = NULL; 1839 1840 /* 1841 * Only follow one level of referrals, i.e. 1842 * if already in referral mode, do nothing 1843 */ 1844 if (cookie->refpos == NULL) { 1845 cookie->new_state = END_RESULT; 1846 rc = ldap_parse_result(cookie->conn->ld, 1847 cookie->resultMsg, 1848 &errCode, NULL, 1849 NULL, &referrals, 1850 NULL, 0); 1851 if (rc != NS_LDAP_SUCCESS) { 1852 (void) ldap_get_option(cookie->conn->ld, 1853 LDAP_OPT_ERROR_NUMBER, 1854 &cookie->err_rc); 1855 cookie->new_state = LDAP_ERROR; 1856 return; 1857 } 1858 if (errCode == LDAP_REFERRAL) { 1859 for (i = 0; referrals[i] != NULL; 1860 i++) { 1861 /* add to referral list */ 1862 rc = __s_api_addRefInfo( 1863 &cookie->reflist, 1864 referrals[i], 1865 cookie->basedn, 1866 &cookie->scope, 1867 cookie->filter, 1868 cookie->conn->ld); 1869 if (rc != NS_LDAP_SUCCESS) { 1870 cookie->new_state = 1871 ERROR; 1872 break; 1873 } 1874 } 1875 ldap_value_free(referrals); 1876 } 1877 } 1878 } 1879 1880 static void 1881 proc_search_references(ns_ldap_cookie_t *cookie) 1882 { 1883 char **refurls = NULL; 1884 int i, rc; 1885 1886 /* 1887 * Only follow one level of referrals, i.e. 1888 * if already in referral mode, do nothing 1889 */ 1890 if (cookie->refpos == NULL) { 1891 refurls = ldap_get_reference_urls( 1892 cookie->conn->ld, 1893 cookie->resultMsg); 1894 if (refurls == NULL) { 1895 (void) ldap_get_option(cookie->conn->ld, 1896 LDAP_OPT_ERROR_NUMBER, 1897 &cookie->err_rc); 1898 cookie->new_state = LDAP_ERROR; 1899 return; 1900 } 1901 for (i = 0; refurls[i] != NULL; i++) { 1902 /* add to referral list */ 1903 rc = __s_api_addRefInfo( 1904 &cookie->reflist, 1905 refurls[i], 1906 cookie->basedn, 1907 &cookie->scope, 1908 cookie->filter, 1909 cookie->conn->ld); 1910 if (rc != NS_LDAP_SUCCESS) { 1911 cookie->new_state = 1912 ERROR; 1913 break; 1914 } 1915 } 1916 /* free allocated storage */ 1917 for (i = 0; refurls[i] != NULL; i++) 1918 free(refurls[i]); 1919 } 1920 } 1921 1922 static ns_state_t 1923 multi_result(ns_ldap_cookie_t *cookie) 1924 { 1925 char errstr[MAXERROR]; 1926 char *err; 1927 ns_ldap_error_t **errorp = NULL; 1928 LDAPControl **retCtrls = NULL; 1929 int i, rc; 1930 int errCode; 1931 int finished = 0; 1932 unsigned long target_posp = 0; 1933 unsigned long list_size = 0; 1934 unsigned int count = 0; 1935 char **referrals = NULL; 1936 1937 if (cookie->listType == VLVCTRLFLAG) { 1938 rc = ldap_parse_result(cookie->conn->ld, cookie->resultMsg, 1939 &errCode, NULL, NULL, &referrals, &retCtrls, 0); 1940 if (rc != LDAP_SUCCESS) { 1941 (void) ldap_get_option(cookie->conn->ld, 1942 LDAP_OPT_ERROR_NUMBER, 1943 &cookie->err_rc); 1944 (void) sprintf(errstr, 1945 gettext("LDAP ERROR (%d): %s.\n"), 1946 cookie->err_rc, 1947 gettext(ldap_err2string(cookie->err_rc))); 1948 err = strdup(errstr); 1949 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 1950 NULL); 1951 cookie->err_rc = NS_LDAP_INTERNAL; 1952 cookie->errorp = *errorp; 1953 return (LDAP_ERROR); 1954 } 1955 if (errCode == LDAP_REFERRAL) { 1956 for (i = 0; referrals[i] != NULL; 1957 i++) { 1958 /* add to referral list */ 1959 rc = __s_api_addRefInfo( 1960 &cookie->reflist, 1961 referrals[i], 1962 cookie->basedn, 1963 &cookie->scope, 1964 cookie->filter, 1965 cookie->conn->ld); 1966 if (rc != NS_LDAP_SUCCESS) { 1967 ldap_value_free( 1968 referrals); 1969 if (retCtrls) 1970 ldap_controls_free( 1971 retCtrls); 1972 return (ERROR); 1973 } 1974 } 1975 ldap_value_free(referrals); 1976 if (retCtrls) 1977 ldap_controls_free(retCtrls); 1978 return (END_RESULT); 1979 } 1980 if (retCtrls) { 1981 rc = ldap_parse_virtuallist_control( 1982 cookie->conn->ld, retCtrls, 1983 &target_posp, &list_size, &errCode); 1984 if (rc == LDAP_SUCCESS) { 1985 cookie->index = target_posp + LISTPAGESIZE; 1986 if (cookie->index > list_size) { 1987 finished = 1; 1988 } 1989 } 1990 ldap_controls_free(retCtrls); 1991 retCtrls = NULL; 1992 } 1993 else 1994 finished = 1; 1995 } else if (cookie->listType == SIMPLEPAGECTRLFLAG) { 1996 rc = ldap_parse_result(cookie->conn->ld, cookie->resultMsg, 1997 &errCode, NULL, NULL, &referrals, &retCtrls, 0); 1998 if (rc != LDAP_SUCCESS) { 1999 (void) ldap_get_option(cookie->conn->ld, 2000 LDAP_OPT_ERROR_NUMBER, 2001 &cookie->err_rc); 2002 (void) sprintf(errstr, 2003 gettext("LDAP ERROR (%d): %s.\n"), 2004 cookie->err_rc, 2005 gettext(ldap_err2string(cookie->err_rc))); 2006 err = strdup(errstr); 2007 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 2008 NULL); 2009 cookie->err_rc = NS_LDAP_INTERNAL; 2010 cookie->errorp = *errorp; 2011 return (LDAP_ERROR); 2012 } 2013 if (errCode == LDAP_REFERRAL) { 2014 for (i = 0; referrals[i] != NULL; 2015 i++) { 2016 /* add to referral list */ 2017 rc = __s_api_addRefInfo( 2018 &cookie->reflist, 2019 referrals[i], 2020 cookie->basedn, 2021 &cookie->scope, 2022 cookie->filter, 2023 cookie->conn->ld); 2024 if (rc != NS_LDAP_SUCCESS) { 2025 ldap_value_free( 2026 referrals); 2027 if (retCtrls) 2028 ldap_controls_free( 2029 retCtrls); 2030 return (ERROR); 2031 } 2032 } 2033 ldap_value_free(referrals); 2034 if (retCtrls) 2035 ldap_controls_free(retCtrls); 2036 return (END_RESULT); 2037 } 2038 if (retCtrls) { 2039 if (cookie->ctrlCookie) 2040 ber_bvfree(cookie->ctrlCookie); 2041 cookie->ctrlCookie = NULL; 2042 rc = ldap_parse_page_control( 2043 cookie->conn->ld, retCtrls, 2044 &count, &cookie->ctrlCookie); 2045 if (rc == LDAP_SUCCESS) { 2046 if ((cookie->ctrlCookie == NULL) || 2047 (cookie->ctrlCookie->bv_val == NULL) || 2048 (cookie->ctrlCookie->bv_len == 0)) 2049 finished = 1; 2050 } 2051 ldap_controls_free(retCtrls); 2052 retCtrls = NULL; 2053 } 2054 else 2055 finished = 1; 2056 } 2057 if (!finished && cookie->listType == VLVCTRLFLAG) 2058 return (NEXT_VLV); 2059 if (!finished && cookie->listType == SIMPLEPAGECTRLFLAG) 2060 return (NEXT_PAGE); 2061 if (finished) 2062 return (END_RESULT); 2063 return (ERROR); 2064 } 2065 2066 /* 2067 * clear_results(ns_ldap_cookie_t): 2068 * 2069 * Attempt to obtain remnants of ldap responses and free them. If remnants are 2070 * not obtained within a certain time period tell the server we wish to abandon 2071 * the request. 2072 * 2073 * Note that we do not initially tell the server to abandon the request as that 2074 * can be an expensive operation for the server, while it is cheap for us to 2075 * just flush the input. 2076 * 2077 * If something was to remain in libldap queue as a result of some error then 2078 * it would be freed later during drop connection call or when no other 2079 * requests share the connection. 2080 */ 2081 static void 2082 clear_results(ns_ldap_cookie_t *cookie) 2083 { 2084 int rc; 2085 if (cookie->conn != NULL && cookie->conn->ld != NULL && 2086 (cookie->connectionId != -1 || 2087 (cookie->conn_user != NULL && 2088 cookie->conn_user->conn_mt != NULL)) && 2089 cookie->msgId != 0) { 2090 /* 2091 * We need to cleanup the rest of response (if there is such) 2092 * and LDAP abandon is too heavy for LDAP servers, so we will 2093 * wait for the rest of response till timeout and "process" it. 2094 */ 2095 rc = ldap_result(cookie->conn->ld, cookie->msgId, LDAP_MSG_ALL, 2096 (struct timeval *)&cookie->search_timeout, 2097 &cookie->resultMsg); 2098 if (rc != -1 && rc != 0 && cookie->resultMsg != NULL) 2099 (void) ldap_msgfree(cookie->resultMsg); 2100 /* 2101 * If there was timeout then we will send ABANDON request to 2102 * LDAP server to decrease load. 2103 */ 2104 if (rc == 0) 2105 (void) ldap_abandon_ext(cookie->conn->ld, cookie->msgId, 2106 NULL, NULL); 2107 /* Disassociate cookie with msgId */ 2108 cookie->msgId = 0; 2109 } 2110 } 2111 2112 /* 2113 * This state machine performs one or more LDAP searches to a given 2114 * directory server using service search descriptors and schema 2115 * mapping as appropriate. The approximate pseudocode for 2116 * this routine is the following: 2117 * Given the current configuration [set/reset connection etc.] 2118 * and the current service search descriptor list 2119 * or default search filter parameters 2120 * foreach (service search filter) { 2121 * initialize the filter [via filter_init if appropriate] 2122 * get a valid session/connection (preferably the current one) 2123 * Recover if the connection is lost 2124 * perform the search 2125 * foreach (result entry) { 2126 * process result [via callback if appropriate] 2127 * save result for caller if accepted. 2128 * exit and return all collected if allResults found; 2129 * } 2130 * } 2131 * return collected results and exit 2132 */ 2133 2134 static 2135 ns_state_t 2136 search_state_machine(ns_ldap_cookie_t *cookie, ns_state_t state, int cycle) 2137 { 2138 char errstr[MAXERROR]; 2139 char *err; 2140 int rc, ret; 2141 int rc_save; 2142 ns_ldap_entry_t *nextEntry; 2143 ns_ldap_error_t *error = NULL; 2144 ns_ldap_error_t **errorp; 2145 struct timeval tv; 2146 2147 errorp = &error; 2148 cookie->state = state; 2149 errstr[0] = '\0'; 2150 2151 for (;;) { 2152 switch (cookie->state) { 2153 case CLEAR_RESULTS: 2154 clear_results(cookie); 2155 cookie->new_state = EXIT; 2156 break; 2157 case GET_ACCT_MGMT_INFO: 2158 /* 2159 * Set the flag to get ldap account management controls. 2160 */ 2161 cookie->nopasswd_acct_mgmt = 1; 2162 cookie->new_state = INIT; 2163 break; 2164 case EXIT: 2165 /* state engine/connection cleaned up in delete */ 2166 if (cookie->attribute) { 2167 __s_api_free2dArray(cookie->attribute); 2168 cookie->attribute = NULL; 2169 } 2170 if (cookie->reflist) { 2171 __s_api_deleteRefInfo(cookie->reflist); 2172 cookie->reflist = NULL; 2173 } 2174 return (EXIT); 2175 case INIT: 2176 cookie->sdpos = NULL; 2177 cookie->new_state = NEXT_SEARCH_DESCRIPTOR; 2178 if (cookie->attribute) { 2179 __s_api_free2dArray(cookie->attribute); 2180 cookie->attribute = NULL; 2181 } 2182 if ((cookie->i_flags & NS_LDAP_NOMAP) == 0 && 2183 cookie->i_attr) { 2184 cookie->attribute = 2185 __ns_ldap_mapAttributeList( 2186 cookie->service, 2187 cookie->i_attr); 2188 } 2189 break; 2190 case REINIT: 2191 /* Check if we've reached MAX retries. */ 2192 cookie->retries++; 2193 if (cookie->retries > NS_LIST_TRY_MAX - 1) { 2194 cookie->new_state = LDAP_ERROR; 2195 break; 2196 } 2197 2198 /* 2199 * Even if we still have retries left, check 2200 * if retry is possible. 2201 */ 2202 if (cookie->conn_user != NULL) { 2203 int retry; 2204 ns_conn_mgmt_t *cmg; 2205 cmg = cookie->conn_user->conn_mgmt; 2206 retry = cookie->conn_user->retry; 2207 if (cmg != NULL && cmg->cfg_reloaded == 1) 2208 retry = 1; 2209 if (retry == 0) { 2210 cookie->new_state = LDAP_ERROR; 2211 break; 2212 } 2213 } 2214 /* 2215 * Free results if any, reset to the first 2216 * search descriptor and start a new session. 2217 */ 2218 if (cookie->resultMsg != NULL) { 2219 (void) ldap_msgfree(cookie->resultMsg); 2220 cookie->resultMsg = NULL; 2221 } 2222 (void) __ns_ldap_freeError(&cookie->errorp); 2223 (void) __ns_ldap_freeResult(&cookie->result); 2224 cookie->sdpos = cookie->sdlist; 2225 cookie->err_from_result = 0; 2226 cookie->err_rc = 0; 2227 cookie->new_state = NEXT_SESSION; 2228 break; 2229 case NEXT_SEARCH_DESCRIPTOR: 2230 /* get next search descriptor */ 2231 if (cookie->sdpos == NULL) { 2232 cookie->sdpos = cookie->sdlist; 2233 cookie->new_state = GET_SESSION; 2234 } else { 2235 cookie->sdpos++; 2236 cookie->new_state = NEXT_SEARCH; 2237 } 2238 if (*cookie->sdpos == NULL) 2239 cookie->new_state = EXIT; 2240 break; 2241 case GET_SESSION: 2242 if (get_current_session(cookie) < 0) 2243 cookie->new_state = NEXT_SESSION; 2244 else 2245 cookie->new_state = NEXT_SEARCH; 2246 break; 2247 case NEXT_SESSION: 2248 if (get_next_session(cookie) < 0) 2249 cookie->new_state = RESTART_SESSION; 2250 else 2251 cookie->new_state = NEXT_SEARCH; 2252 break; 2253 case RESTART_SESSION: 2254 if (cookie->i_flags & NS_LDAP_HARD) { 2255 cookie->new_state = NEXT_SESSION; 2256 break; 2257 } 2258 (void) sprintf(errstr, 2259 gettext("Session error no available conn.\n"), 2260 state); 2261 err = strdup(errstr); 2262 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 2263 NULL); 2264 cookie->err_rc = NS_LDAP_INTERNAL; 2265 cookie->errorp = *errorp; 2266 cookie->new_state = EXIT; 2267 break; 2268 case NEXT_SEARCH: 2269 /* setup referrals search if necessary */ 2270 if (cookie->refpos) { 2271 if (setup_referral_search(cookie) < 0) { 2272 cookie->new_state = EXIT; 2273 break; 2274 } 2275 } else if (setup_next_search(cookie) < 0) { 2276 cookie->new_state = EXIT; 2277 break; 2278 } 2279 /* only do VLV/PAGE on scopes onelevel/subtree */ 2280 if (paging_supported(cookie)) { 2281 if (cookie->use_paging && 2282 (cookie->scope != LDAP_SCOPE_BASE)) { 2283 cookie->index = 1; 2284 if (cookie->listType == VLVCTRLFLAG) 2285 cookie->new_state = NEXT_VLV; 2286 else 2287 cookie->new_state = NEXT_PAGE; 2288 break; 2289 } 2290 } 2291 cookie->new_state = ONE_SEARCH; 2292 break; 2293 case NEXT_VLV: 2294 rc = setup_vlv_params(cookie); 2295 if (rc != LDAP_SUCCESS) { 2296 cookie->err_rc = rc; 2297 cookie->new_state = LDAP_ERROR; 2298 break; 2299 } 2300 cookie->next_state = MULTI_RESULT; 2301 cookie->new_state = DO_SEARCH; 2302 break; 2303 case NEXT_PAGE: 2304 rc = setup_simplepg_params(cookie); 2305 if (rc != LDAP_SUCCESS) { 2306 cookie->err_rc = rc; 2307 cookie->new_state = LDAP_ERROR; 2308 break; 2309 } 2310 cookie->next_state = MULTI_RESULT; 2311 cookie->new_state = DO_SEARCH; 2312 break; 2313 case ONE_SEARCH: 2314 cookie->next_state = NEXT_RESULT; 2315 cookie->new_state = DO_SEARCH; 2316 break; 2317 case DO_SEARCH: 2318 rc = ldap_search_ext(cookie->conn->ld, 2319 cookie->basedn, 2320 cookie->scope, 2321 cookie->filter, 2322 cookie->attribute, 2323 0, 2324 cookie->p_serverctrls, 2325 NULL, 2326 &cookie->search_timeout, 0, 2327 &cookie->msgId); 2328 if (rc != LDAP_SUCCESS) { 2329 if (rc == LDAP_BUSY || 2330 rc == LDAP_UNAVAILABLE || 2331 rc == LDAP_UNWILLING_TO_PERFORM || 2332 rc == LDAP_CONNECT_ERROR || 2333 rc == LDAP_SERVER_DOWN) { 2334 2335 if (cookie->reinit_on_retriable_err) { 2336 cookie->err_rc = rc; 2337 cookie->new_state = REINIT; 2338 } else 2339 cookie->new_state = 2340 NEXT_SESSION; 2341 2342 /* 2343 * If not able to reach the 2344 * server, inform the ldap 2345 * cache manager that the 2346 * server should be removed 2347 * from it's server list. 2348 * Thus, the manager will not 2349 * return this server on the next 2350 * get-server request and will 2351 * also reduce the server list 2352 * refresh TTL, so that it will 2353 * find out sooner when the server 2354 * is up again. 2355 */ 2356 if ((rc == LDAP_CONNECT_ERROR || 2357 rc == LDAP_SERVER_DOWN) && 2358 (cookie->conn_user == NULL || 2359 cookie->conn_user->conn_mt == 2360 NULL)) { 2361 ret = __s_api_removeServer( 2362 cookie->conn->serverAddr); 2363 if (ret == NS_CACHE_NOSERVER && 2364 cookie->conn_auth_type 2365 == NS_LDAP_AUTH_NONE) { 2366 /* 2367 * Couldn't remove 2368 * server from server 2369 * list. 2370 * Exit to avoid 2371 * potential infinite 2372 * loop. 2373 */ 2374 cookie->err_rc = rc; 2375 cookie->new_state = 2376 LDAP_ERROR; 2377 } 2378 if (cookie->connectionId > -1) { 2379 /* 2380 * NS_LDAP_NEW_CONN 2381 * indicates that the 2382 * connection should 2383 * be deleted, not 2384 * kept alive 2385 */ 2386 DropConnection( 2387 cookie-> 2388 connectionId, 2389 NS_LDAP_NEW_CONN); 2390 cookie->connectionId = 2391 -1; 2392 } 2393 } else if ((rc == LDAP_CONNECT_ERROR || 2394 rc == LDAP_SERVER_DOWN) && 2395 cookie->conn_user != NULL && 2396 cookie->reinit_on_retriable_err) { 2397 /* 2398 * MT connection not usable, 2399 * close it before REINIT. 2400 * rc has already been saved 2401 * in cookie->err_rc above. 2402 */ 2403 __s_api_conn_mt_close( 2404 cookie->conn_user, 2405 rc, &cookie->errorp); 2406 } 2407 break; 2408 } 2409 cookie->err_rc = rc; 2410 cookie->new_state = LDAP_ERROR; 2411 break; 2412 } 2413 cookie->new_state = cookie->next_state; 2414 break; 2415 case NEXT_RESULT: 2416 /* 2417 * Caller (e.g. __ns_ldap_list_batch_add) 2418 * does not want to block on ldap_result(). 2419 * Therefore we execute ldap_result() with 2420 * a zeroed timeval. 2421 */ 2422 if (cookie->no_wait == B_TRUE) 2423 (void) memset(&tv, 0, sizeof (tv)); 2424 else 2425 tv = cookie->search_timeout; 2426 rc = ldap_result(cookie->conn->ld, cookie->msgId, 2427 LDAP_MSG_ONE, 2428 &tv, 2429 &cookie->resultMsg); 2430 if (rc == LDAP_RES_SEARCH_RESULT) { 2431 cookie->new_state = END_RESULT; 2432 /* check and process referrals info */ 2433 if (cookie->followRef) 2434 proc_result_referrals( 2435 cookie); 2436 (void) ldap_msgfree(cookie->resultMsg); 2437 cookie->resultMsg = NULL; 2438 break; 2439 } 2440 /* handle referrals if necessary */ 2441 if (rc == LDAP_RES_SEARCH_REFERENCE) { 2442 if (cookie->followRef) 2443 proc_search_references(cookie); 2444 (void) ldap_msgfree(cookie->resultMsg); 2445 cookie->resultMsg = NULL; 2446 break; 2447 } 2448 if (rc != LDAP_RES_SEARCH_ENTRY) { 2449 switch (rc) { 2450 case 0: 2451 if (cookie->no_wait == B_TRUE) { 2452 (void) ldap_msgfree( 2453 cookie->resultMsg); 2454 cookie->resultMsg = NULL; 2455 return (cookie->new_state); 2456 } 2457 rc = LDAP_TIMEOUT; 2458 break; 2459 case -1: 2460 rc = ldap_get_lderrno(cookie->conn->ld, 2461 NULL, NULL); 2462 break; 2463 default: 2464 rc = ldap_result2error(cookie->conn->ld, 2465 cookie->resultMsg, 1); 2466 break; 2467 } 2468 if ((rc == LDAP_TIMEOUT || 2469 rc == LDAP_SERVER_DOWN) && 2470 (cookie->conn_user == NULL || 2471 cookie->conn_user->conn_mt == NULL)) { 2472 if (rc == LDAP_TIMEOUT) 2473 (void) __s_api_removeServer( 2474 cookie->conn->serverAddr); 2475 if (cookie->connectionId > -1) { 2476 DropConnection( 2477 cookie->connectionId, 2478 NS_LDAP_NEW_CONN); 2479 cookie->connectionId = -1; 2480 } 2481 cookie->err_from_result = 1; 2482 } 2483 (void) ldap_msgfree(cookie->resultMsg); 2484 cookie->resultMsg = NULL; 2485 if (rc == LDAP_BUSY || 2486 rc == LDAP_UNAVAILABLE || 2487 rc == LDAP_UNWILLING_TO_PERFORM) { 2488 if (cookie->reinit_on_retriable_err) { 2489 cookie->err_rc = rc; 2490 cookie->err_from_result = 1; 2491 cookie->new_state = REINIT; 2492 } else 2493 cookie->new_state = 2494 NEXT_SESSION; 2495 break; 2496 } 2497 if ((rc == LDAP_CONNECT_ERROR || 2498 rc == LDAP_SERVER_DOWN) && 2499 cookie->reinit_on_retriable_err) { 2500 ns_ldap_error_t *errorp = NULL; 2501 cookie->err_rc = rc; 2502 cookie->err_from_result = 1; 2503 cookie->new_state = REINIT; 2504 if (cookie->conn_user != NULL) 2505 __s_api_conn_mt_close( 2506 cookie->conn_user, 2507 rc, &errorp); 2508 if (errorp != NULL) { 2509 (void) __ns_ldap_freeError( 2510 &cookie->errorp); 2511 cookie->errorp = errorp; 2512 } 2513 break; 2514 } 2515 cookie->err_rc = rc; 2516 cookie->new_state = LDAP_ERROR; 2517 break; 2518 } 2519 /* else LDAP_RES_SEARCH_ENTRY */ 2520 /* get account management response control */ 2521 if (cookie->nopasswd_acct_mgmt == 1) { 2522 rc = ldap_get_entry_controls(cookie->conn->ld, 2523 cookie->resultMsg, 2524 &(cookie->resultctrl)); 2525 if (rc != LDAP_SUCCESS) { 2526 cookie->new_state = LDAP_ERROR; 2527 cookie->err_rc = rc; 2528 break; 2529 } 2530 } 2531 rc = __s_api_getEntry(cookie); 2532 (void) ldap_msgfree(cookie->resultMsg); 2533 cookie->resultMsg = NULL; 2534 if (rc != NS_LDAP_SUCCESS) { 2535 cookie->new_state = LDAP_ERROR; 2536 break; 2537 } 2538 cookie->new_state = PROCESS_RESULT; 2539 cookie->next_state = NEXT_RESULT; 2540 break; 2541 case MULTI_RESULT: 2542 if (cookie->no_wait == B_TRUE) 2543 (void) memset(&tv, 0, sizeof (tv)); 2544 else 2545 tv = cookie->search_timeout; 2546 rc = ldap_result(cookie->conn->ld, cookie->msgId, 2547 LDAP_MSG_ONE, 2548 &tv, 2549 &cookie->resultMsg); 2550 if (rc == LDAP_RES_SEARCH_RESULT) { 2551 rc = ldap_result2error(cookie->conn->ld, 2552 cookie->resultMsg, 0); 2553 if (rc != LDAP_SUCCESS) { 2554 cookie->err_rc = rc; 2555 cookie->new_state = LDAP_ERROR; 2556 (void) ldap_msgfree(cookie->resultMsg); 2557 break; 2558 } 2559 cookie->new_state = multi_result(cookie); 2560 (void) ldap_msgfree(cookie->resultMsg); 2561 cookie->resultMsg = NULL; 2562 break; 2563 } 2564 /* handle referrals if necessary */ 2565 if (rc == LDAP_RES_SEARCH_REFERENCE && 2566 cookie->followRef) { 2567 proc_search_references(cookie); 2568 (void) ldap_msgfree(cookie->resultMsg); 2569 cookie->resultMsg = NULL; 2570 break; 2571 } 2572 if (rc != LDAP_RES_SEARCH_ENTRY) { 2573 switch (rc) { 2574 case 0: 2575 if (cookie->no_wait == B_TRUE) { 2576 (void) ldap_msgfree( 2577 cookie->resultMsg); 2578 cookie->resultMsg = NULL; 2579 return (cookie->new_state); 2580 } 2581 rc = LDAP_TIMEOUT; 2582 break; 2583 case -1: 2584 rc = ldap_get_lderrno(cookie->conn->ld, 2585 NULL, NULL); 2586 break; 2587 default: 2588 rc = ldap_result2error(cookie->conn->ld, 2589 cookie->resultMsg, 1); 2590 break; 2591 } 2592 if ((rc == LDAP_TIMEOUT || 2593 rc == LDAP_SERVER_DOWN) && 2594 (cookie->conn_user == NULL || 2595 cookie->conn_user->conn_mt == NULL)) { 2596 if (rc == LDAP_TIMEOUT) 2597 (void) __s_api_removeServer( 2598 cookie->conn->serverAddr); 2599 if (cookie->connectionId > -1) { 2600 DropConnection( 2601 cookie->connectionId, 2602 NS_LDAP_NEW_CONN); 2603 cookie->connectionId = -1; 2604 } 2605 cookie->err_from_result = 1; 2606 } 2607 (void) ldap_msgfree(cookie->resultMsg); 2608 cookie->resultMsg = NULL; 2609 if (rc == LDAP_BUSY || 2610 rc == LDAP_UNAVAILABLE || 2611 rc == LDAP_UNWILLING_TO_PERFORM) { 2612 if (cookie->reinit_on_retriable_err) { 2613 cookie->err_rc = rc; 2614 cookie->err_from_result = 1; 2615 cookie->new_state = REINIT; 2616 } else 2617 cookie->new_state = 2618 NEXT_SESSION; 2619 break; 2620 } 2621 if ((rc == LDAP_CONNECT_ERROR || 2622 rc == LDAP_SERVER_DOWN) && 2623 cookie->reinit_on_retriable_err) { 2624 ns_ldap_error_t *errorp = NULL; 2625 cookie->err_rc = rc; 2626 cookie->err_from_result = 1; 2627 cookie->new_state = REINIT; 2628 if (cookie->conn_user != NULL) 2629 __s_api_conn_mt_close( 2630 cookie->conn_user, 2631 rc, &errorp); 2632 if (errorp != NULL) { 2633 (void) __ns_ldap_freeError( 2634 &cookie->errorp); 2635 cookie->errorp = errorp; 2636 } 2637 break; 2638 } 2639 cookie->err_rc = rc; 2640 cookie->new_state = LDAP_ERROR; 2641 break; 2642 } 2643 /* else LDAP_RES_SEARCH_ENTRY */ 2644 rc = __s_api_getEntry(cookie); 2645 (void) ldap_msgfree(cookie->resultMsg); 2646 cookie->resultMsg = NULL; 2647 if (rc != NS_LDAP_SUCCESS) { 2648 cookie->new_state = LDAP_ERROR; 2649 break; 2650 } 2651 cookie->new_state = PROCESS_RESULT; 2652 cookie->next_state = MULTI_RESULT; 2653 break; 2654 case PROCESS_RESULT: 2655 /* NOTE THIS STATE MAY BE PROCESSED BY CALLER */ 2656 if (cookie->use_usercb && cookie->callback) { 2657 rc = 0; 2658 for (nextEntry = cookie->result->entry; 2659 nextEntry != NULL; 2660 nextEntry = nextEntry->next) { 2661 rc = (*cookie->callback)(nextEntry, 2662 cookie->userdata); 2663 2664 if (rc == NS_LDAP_CB_DONE) { 2665 /* cb doesn't want any more data */ 2666 rc = NS_LDAP_PARTIAL; 2667 cookie->err_rc = rc; 2668 break; 2669 } else if (rc != NS_LDAP_CB_NEXT) { 2670 /* invalid return code */ 2671 rc = NS_LDAP_OP_FAILED; 2672 cookie->err_rc = rc; 2673 break; 2674 } 2675 } 2676 (void) __ns_ldap_freeResult(&cookie->result); 2677 cookie->result = NULL; 2678 } 2679 if (rc != 0) { 2680 cookie->new_state = EXIT; 2681 break; 2682 } 2683 /* NOTE PREVIOUS STATE SPECIFIES NEXT STATE */ 2684 cookie->new_state = cookie->next_state; 2685 break; 2686 case END_PROCESS_RESULT: 2687 cookie->new_state = cookie->next_state; 2688 break; 2689 case END_RESULT: 2690 /* 2691 * XXX DO WE NEED THIS CASE? 2692 * if (search is complete) { 2693 * cookie->new_state = EXIT; 2694 * } else 2695 */ 2696 /* 2697 * entering referral mode if necessary 2698 */ 2699 if (cookie->followRef && cookie->reflist) 2700 cookie->new_state = 2701 NEXT_REFERRAL; 2702 else 2703 cookie->new_state = 2704 NEXT_SEARCH_DESCRIPTOR; 2705 break; 2706 case NEXT_REFERRAL: 2707 /* get next referral info */ 2708 if (cookie->refpos == NULL) 2709 cookie->refpos = 2710 cookie->reflist; 2711 else 2712 cookie->refpos = 2713 cookie->refpos->next; 2714 /* check see if done with all referrals */ 2715 if (cookie->refpos != NULL) 2716 cookie->new_state = 2717 GET_REFERRAL_SESSION; 2718 else { 2719 __s_api_deleteRefInfo(cookie->reflist); 2720 cookie->reflist = NULL; 2721 cookie->new_state = 2722 NEXT_SEARCH_DESCRIPTOR; 2723 if (cookie->conn_user != NULL) 2724 cookie->conn_user->referral = B_FALSE; 2725 } 2726 break; 2727 case GET_REFERRAL_SESSION: 2728 if (get_referral_session(cookie) < 0) 2729 cookie->new_state = EXIT; 2730 else { 2731 cookie->new_state = NEXT_SEARCH; 2732 } 2733 break; 2734 case LDAP_ERROR: 2735 rc_save = cookie->err_rc; 2736 if (cookie->err_from_result) { 2737 if (cookie->err_rc == LDAP_SERVER_DOWN) { 2738 (void) sprintf(errstr, 2739 gettext("LDAP ERROR (%d): " 2740 "Error occurred during" 2741 " receiving results. " 2742 "Connection to server lost."), 2743 cookie->err_rc); 2744 } else if (cookie->err_rc == LDAP_TIMEOUT) { 2745 (void) sprintf(errstr, 2746 gettext("LDAP ERROR (%d): " 2747 "Error occurred during" 2748 " receiving results. %s" 2749 "."), cookie->err_rc, 2750 ldap_err2string( 2751 cookie->err_rc)); 2752 } 2753 } else 2754 (void) sprintf(errstr, 2755 gettext("LDAP ERROR (%d): %s."), 2756 cookie->err_rc, 2757 ldap_err2string(cookie->err_rc)); 2758 err = strdup(errstr); 2759 if (cookie->err_from_result) { 2760 if (cookie->err_rc == LDAP_SERVER_DOWN) { 2761 MKERROR(LOG_INFO, *errorp, 2762 cookie->err_rc, err, NULL); 2763 } else { 2764 MKERROR(LOG_WARNING, *errorp, 2765 cookie->err_rc, err, NULL); 2766 } 2767 } else { 2768 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, 2769 err, NULL); 2770 } 2771 cookie->err_rc = NS_LDAP_INTERNAL; 2772 cookie->errorp = *errorp; 2773 if (cookie->conn_user != NULL) { 2774 if (rc_save == LDAP_SERVER_DOWN || 2775 rc_save == LDAP_CONNECT_ERROR) { 2776 /* 2777 * MT connection is not usable, 2778 * close it. 2779 */ 2780 __s_api_conn_mt_close(cookie->conn_user, 2781 rc_save, &cookie->errorp); 2782 return (ERROR); 2783 } 2784 } 2785 return (ERROR); 2786 default: 2787 case ERROR: 2788 (void) sprintf(errstr, 2789 gettext("Internal State machine exit (%d).\n"), 2790 cookie->state); 2791 err = strdup(errstr); 2792 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 2793 NULL); 2794 cookie->err_rc = NS_LDAP_INTERNAL; 2795 cookie->errorp = *errorp; 2796 return (ERROR); 2797 } 2798 2799 if (cookie->conn_user != NULL && 2800 cookie->conn_user->bad_mt_conn == B_TRUE) { 2801 __s_api_conn_mt_close(cookie->conn_user, 0, NULL); 2802 cookie->err_rc = cookie->conn_user->ns_rc; 2803 cookie->errorp = cookie->conn_user->ns_error; 2804 cookie->conn_user->ns_error = NULL; 2805 return (ERROR); 2806 } 2807 2808 if (cycle == ONE_STEP) { 2809 return (cookie->new_state); 2810 } 2811 cookie->state = cookie->new_state; 2812 } 2813 /*NOTREACHED*/ 2814 #if 0 2815 (void) sprintf(errstr, 2816 gettext("Unexpected State machine error.\n")); 2817 err = strdup(errstr); 2818 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, NULL); 2819 cookie->err_rc = NS_LDAP_INTERNAL; 2820 cookie->errorp = *errorp; 2821 return (ERROR); 2822 #endif 2823 } 2824 2825 /* 2826 * For a lookup of shadow data, if shadow update is enabled, 2827 * check the calling process' privilege to ensure it's 2828 * allowed to perform such operation. 2829 */ 2830 static int 2831 check_shadow(ns_ldap_cookie_t *cookie, const char *service) 2832 { 2833 char errstr[MAXERROR]; 2834 char *err; 2835 boolean_t priv; 2836 /* caller */ 2837 priv_set_t *ps; 2838 /* zone */ 2839 priv_set_t *zs; 2840 2841 /* 2842 * If service is "shadow", we may need 2843 * to use privilege credentials. 2844 */ 2845 if ((strcmp(service, "shadow") == 0) && 2846 __ns_ldap_is_shadow_update_enabled()) { 2847 /* 2848 * Since we release admin credentials after 2849 * connection is closed and we do not cache 2850 * them, we allow any root or all zone 2851 * privilege process to read shadow data. 2852 */ 2853 priv = (geteuid() == 0); 2854 if (!priv) { 2855 /* caller */ 2856 ps = priv_allocset(); 2857 2858 (void) getppriv(PRIV_EFFECTIVE, ps); 2859 zs = priv_str_to_set("zone", ",", NULL); 2860 priv = priv_isequalset(ps, zs); 2861 priv_freeset(ps); 2862 priv_freeset(zs); 2863 } 2864 if (!priv) { 2865 (void) sprintf(errstr, 2866 gettext("Permission denied")); 2867 err = strdup(errstr); 2868 if (err == NULL) 2869 return (NS_LDAP_MEMORY); 2870 MKERROR(LOG_INFO, cookie->errorp, NS_LDAP_INTERNAL, err, 2871 NULL); 2872 return (NS_LDAP_INTERNAL); 2873 } 2874 cookie->i_flags |= NS_LDAP_READ_SHADOW; 2875 /* 2876 * We do not want to reuse connection (hence 2877 * keep it open) with admin credentials. 2878 * If NS_LDAP_KEEP_CONN is set, reject the 2879 * request. 2880 */ 2881 if (cookie->i_flags & NS_LDAP_KEEP_CONN) 2882 return (NS_LDAP_INVALID_PARAM); 2883 cookie->i_flags |= NS_LDAP_NEW_CONN; 2884 } 2885 2886 return (NS_LDAP_SUCCESS); 2887 } 2888 2889 /* 2890 * internal function for __ns_ldap_list 2891 */ 2892 static int 2893 ldap_list( 2894 ns_ldap_list_batch_t *batch, 2895 const char *service, 2896 const char *filter, 2897 int (*init_filter_cb)(const ns_ldap_search_desc_t *desc, 2898 char **realfilter, const void *userdata), 2899 const char * const *attribute, 2900 const ns_cred_t *auth, 2901 const int flags, 2902 ns_ldap_result_t **rResult, /* return result entries */ 2903 ns_ldap_error_t **errorp, 2904 int *rcp, 2905 int (*callback)(const ns_ldap_entry_t *entry, const void *userdata), 2906 const void *userdata, ns_conn_user_t *conn_user) 2907 { 2908 ns_ldap_cookie_t *cookie; 2909 ns_ldap_search_desc_t **sdlist = NULL; 2910 ns_ldap_search_desc_t *dptr; 2911 ns_ldap_error_t *error = NULL; 2912 char **dns = NULL; 2913 int scope; 2914 int rc; 2915 int from_result; 2916 2917 *errorp = NULL; 2918 *rResult = NULL; 2919 *rcp = NS_LDAP_SUCCESS; 2920 2921 /* 2922 * Sanity check - NS_LDAP_READ_SHADOW is for our 2923 * own internal use. 2924 */ 2925 if (flags & NS_LDAP_READ_SHADOW) 2926 return (NS_LDAP_INVALID_PARAM); 2927 2928 /* Initialize State machine cookie */ 2929 cookie = init_search_state_machine(); 2930 if (cookie == NULL) { 2931 *rcp = NS_LDAP_MEMORY; 2932 return (NS_LDAP_MEMORY); 2933 } 2934 cookie->conn_user = conn_user; 2935 2936 /* see if need to follow referrals */ 2937 rc = __s_api_toFollowReferrals(flags, 2938 &cookie->followRef, errorp); 2939 if (rc != NS_LDAP_SUCCESS) { 2940 delete_search_cookie(cookie); 2941 *rcp = rc; 2942 return (rc); 2943 } 2944 2945 /* get the service descriptor - or create a default one */ 2946 rc = __s_api_get_SSD_from_SSDtoUse_service(service, 2947 &sdlist, errorp); 2948 if (rc != NS_LDAP_SUCCESS) { 2949 delete_search_cookie(cookie); 2950 *errorp = error; 2951 *rcp = rc; 2952 return (rc); 2953 } 2954 2955 if (sdlist == NULL) { 2956 /* Create default service Desc */ 2957 sdlist = (ns_ldap_search_desc_t **)calloc(2, 2958 sizeof (ns_ldap_search_desc_t *)); 2959 if (sdlist == NULL) { 2960 delete_search_cookie(cookie); 2961 cookie = NULL; 2962 *rcp = NS_LDAP_MEMORY; 2963 return (NS_LDAP_MEMORY); 2964 } 2965 dptr = (ns_ldap_search_desc_t *) 2966 calloc(1, sizeof (ns_ldap_search_desc_t)); 2967 if (dptr == NULL) { 2968 free(sdlist); 2969 delete_search_cookie(cookie); 2970 cookie = NULL; 2971 *rcp = NS_LDAP_MEMORY; 2972 return (NS_LDAP_MEMORY); 2973 } 2974 sdlist[0] = dptr; 2975 2976 /* default base */ 2977 rc = __s_api_getDNs(&dns, service, &cookie->errorp); 2978 if (rc != NS_LDAP_SUCCESS) { 2979 if (dns) { 2980 __s_api_free2dArray(dns); 2981 dns = NULL; 2982 } 2983 *errorp = cookie->errorp; 2984 cookie->errorp = NULL; 2985 delete_search_cookie(cookie); 2986 cookie = NULL; 2987 *rcp = rc; 2988 return (rc); 2989 } 2990 dptr->basedn = strdup(dns[0]); 2991 __s_api_free2dArray(dns); 2992 dns = NULL; 2993 2994 /* default scope */ 2995 scope = 0; 2996 rc = __s_api_getSearchScope(&scope, &cookie->errorp); 2997 dptr->scope = scope; 2998 } 2999 3000 cookie->sdlist = sdlist; 3001 3002 /* 3003 * use VLV/PAGE control only if NS_LDAP_PAGE_CTRL is set 3004 */ 3005 if (flags & NS_LDAP_PAGE_CTRL) 3006 cookie->use_paging = TRUE; 3007 else 3008 cookie->use_paging = FALSE; 3009 3010 /* Set up other arguments */ 3011 cookie->userdata = userdata; 3012 if (init_filter_cb != NULL) { 3013 cookie->init_filter_cb = init_filter_cb; 3014 cookie->use_filtercb = 1; 3015 } 3016 if (callback != NULL) { 3017 cookie->callback = callback; 3018 cookie->use_usercb = 1; 3019 } 3020 3021 /* check_shadow() may add extra value to cookie->i_flags */ 3022 cookie->i_flags = flags; 3023 if (service) { 3024 cookie->service = strdup(service); 3025 if (cookie->service == NULL) { 3026 delete_search_cookie(cookie); 3027 cookie = NULL; 3028 *rcp = NS_LDAP_MEMORY; 3029 return (NS_LDAP_MEMORY); 3030 } 3031 3032 /* 3033 * If given, use the credential given by the caller, and 3034 * skip the credential check required for shadow update. 3035 */ 3036 if (auth == NULL) { 3037 rc = check_shadow(cookie, service); 3038 if (rc != NS_LDAP_SUCCESS) { 3039 *errorp = cookie->errorp; 3040 cookie->errorp = NULL; 3041 delete_search_cookie(cookie); 3042 cookie = NULL; 3043 *rcp = rc; 3044 return (rc); 3045 } 3046 } 3047 } 3048 3049 cookie->i_filter = strdup(filter); 3050 cookie->i_attr = attribute; 3051 cookie->i_auth = auth; 3052 3053 if (batch != NULL) { 3054 cookie->batch = batch; 3055 cookie->reinit_on_retriable_err = B_TRUE; 3056 cookie->no_wait = B_TRUE; 3057 (void) search_state_machine(cookie, INIT, 0); 3058 cookie->no_wait = B_FALSE; 3059 rc = cookie->err_rc; 3060 3061 if (rc == NS_LDAP_SUCCESS) { 3062 /* 3063 * Here rc == NS_LDAP_SUCCESS means that the state 3064 * machine init'ed successfully. The actual status 3065 * of the search will be determined by 3066 * __ns_ldap_list_batch_end(). Add the cookie to our 3067 * batch. 3068 */ 3069 cookie->caller_result = rResult; 3070 cookie->caller_errorp = errorp; 3071 cookie->caller_rc = rcp; 3072 cookie->next_cookie_in_batch = batch->cookie_list; 3073 batch->cookie_list = cookie; 3074 batch->nactive++; 3075 return (rc); 3076 } 3077 /* 3078 * If state machine init failed then copy error to the caller 3079 * and delete the cookie. 3080 */ 3081 } else { 3082 (void) search_state_machine(cookie, INIT, 0); 3083 } 3084 3085 /* Copy results back to user */ 3086 rc = cookie->err_rc; 3087 if (rc != NS_LDAP_SUCCESS) { 3088 if (conn_user != NULL && conn_user->ns_error != NULL) { 3089 *errorp = conn_user->ns_error; 3090 conn_user->ns_error = NULL; 3091 } else 3092 *errorp = cookie->errorp; 3093 } 3094 *rResult = cookie->result; 3095 from_result = cookie->err_from_result; 3096 3097 cookie->errorp = NULL; 3098 cookie->result = NULL; 3099 delete_search_cookie(cookie); 3100 cookie = NULL; 3101 3102 if (from_result == 0 && *rResult == NULL) 3103 rc = NS_LDAP_NOTFOUND; 3104 *rcp = rc; 3105 return (rc); 3106 } 3107 3108 3109 /* 3110 * __ns_ldap_list performs one or more LDAP searches to a given 3111 * directory server using service search descriptors and schema 3112 * mapping as appropriate. The operation may be retried a 3113 * couple of times in error situations. 3114 */ 3115 3116 int 3117 __ns_ldap_list( 3118 const char *service, 3119 const char *filter, 3120 int (*init_filter_cb)(const ns_ldap_search_desc_t *desc, 3121 char **realfilter, const void *userdata), 3122 const char * const *attribute, 3123 const ns_cred_t *auth, 3124 const int flags, 3125 ns_ldap_result_t **rResult, /* return result entries */ 3126 ns_ldap_error_t **errorp, 3127 int (*callback)(const ns_ldap_entry_t *entry, const void *userdata), 3128 const void *userdata) 3129 { 3130 ns_conn_user_t *cu = NULL; 3131 int try_cnt = 0; 3132 int rc = NS_LDAP_SUCCESS, trc; 3133 3134 for (;;) { 3135 if (__s_api_setup_retry_search(&cu, NS_CONN_USER_SEARCH, 3136 &try_cnt, &rc, errorp) == 0) 3137 break; 3138 rc = ldap_list(NULL, service, filter, init_filter_cb, attribute, 3139 auth, flags, rResult, errorp, &trc, callback, userdata, cu); 3140 } 3141 3142 return (rc); 3143 } 3144 3145 /* 3146 * Create and initialize batch for native LDAP lookups 3147 */ 3148 int 3149 __ns_ldap_list_batch_start(ns_ldap_list_batch_t **batch) 3150 { 3151 *batch = calloc(1, sizeof (ns_ldap_list_batch_t)); 3152 if (*batch == NULL) 3153 return (NS_LDAP_MEMORY); 3154 return (NS_LDAP_SUCCESS); 3155 } 3156 3157 3158 /* 3159 * Add a LDAP search request to the batch. 3160 */ 3161 int 3162 __ns_ldap_list_batch_add( 3163 ns_ldap_list_batch_t *batch, 3164 const char *service, 3165 const char *filter, 3166 int (*init_filter_cb)(const ns_ldap_search_desc_t *desc, 3167 char **realfilter, const void *userdata), 3168 const char * const *attribute, 3169 const ns_cred_t *auth, 3170 const int flags, 3171 ns_ldap_result_t **rResult, /* return result entries */ 3172 ns_ldap_error_t **errorp, 3173 int *rcp, 3174 int (*callback)(const ns_ldap_entry_t *entry, const void *userdata), 3175 const void *userdata) 3176 { 3177 ns_conn_user_t *cu; 3178 int rc; 3179 3180 cu = __s_api_conn_user_init(NS_CONN_USER_SEARCH, NULL, 0); 3181 if (cu == NULL) { 3182 if (rcp != NULL) 3183 *rcp = NS_LDAP_MEMORY; 3184 return (NS_LDAP_MEMORY); 3185 } 3186 3187 rc = ldap_list(batch, service, filter, init_filter_cb, attribute, auth, 3188 flags, rResult, errorp, rcp, callback, userdata, cu); 3189 3190 /* 3191 * Free the conn_user if the cookie was not batched. If the cookie 3192 * was batched then __ns_ldap_list_batch_end or release will free the 3193 * conn_user. The batch API instructs the search_state_machine 3194 * to reinit and retry (max 3 times) on retriable LDAP errors. 3195 */ 3196 if (rc != NS_LDAP_SUCCESS && cu != NULL) { 3197 if (cu->conn_mt != NULL) 3198 __s_api_conn_mt_return(cu); 3199 __s_api_conn_user_free(cu); 3200 } 3201 return (rc); 3202 } 3203 3204 3205 /* 3206 * Free batch. 3207 */ 3208 void 3209 __ns_ldap_list_batch_release(ns_ldap_list_batch_t *batch) 3210 { 3211 ns_ldap_cookie_t *c, *next; 3212 3213 for (c = batch->cookie_list; c != NULL; c = next) { 3214 next = c->next_cookie_in_batch; 3215 if (c->conn_user != NULL) { 3216 if (c->conn_user->conn_mt != NULL) 3217 __s_api_conn_mt_return(c->conn_user); 3218 __s_api_conn_user_free(c->conn_user); 3219 c->conn_user = NULL; 3220 } 3221 delete_search_cookie(c); 3222 } 3223 free(batch); 3224 } 3225 3226 #define LD_USING_STATE(st) \ 3227 ((st == DO_SEARCH) || (st == MULTI_RESULT) || (st == NEXT_RESULT)) 3228 3229 /* 3230 * Process batch. Everytime this function is called it selects an 3231 * active cookie from the batch and single steps through the 3232 * search_state_machine for the selected cookie. If lookup associated 3233 * with the cookie is complete (success or error) then the cookie is 3234 * removed from the batch and its memory freed. 3235 * 3236 * Returns 1 (if batch still has active cookies) 3237 * 0 (if batch has no more active cookies) 3238 * -1 (on errors, *rcp will contain the error code) 3239 * 3240 * The caller should call this function in a loop as long as it returns 1 3241 * to process all the requests added to the batch. The results (and errors) 3242 * will be available in the locations provided by the caller at the time of 3243 * __ns_ldap_list_batch_add(). 3244 */ 3245 static 3246 int 3247 __ns_ldap_list_batch_process(ns_ldap_list_batch_t *batch, int *rcp) 3248 { 3249 ns_ldap_cookie_t *c, *ptr, **prev; 3250 ns_state_t state; 3251 ns_ldap_error_t *errorp = NULL; 3252 int rc; 3253 3254 /* Check if are already done */ 3255 if (batch->nactive == 0) 3256 return (0); 3257 3258 /* Get the next cookie from the batch */ 3259 c = (batch->next_cookie == NULL) ? 3260 batch->cookie_list : batch->next_cookie; 3261 3262 batch->next_cookie = c->next_cookie_in_batch; 3263 3264 /* 3265 * Checks the status of the cookie's connection if it needs 3266 * to use that connection for ldap_search_ext or ldap_result. 3267 * If the connection is no longer good but worth retrying 3268 * then reinit the search_state_machine for this cookie 3269 * starting from the first search descriptor. REINIT will 3270 * clear any leftover results if max retries have not been 3271 * reached and redo the search (which may also involve 3272 * following referrals again). 3273 * 3274 * Note that each cookie in the batch will make this 3275 * determination when it reaches one of the LD_USING_STATES. 3276 */ 3277 if (LD_USING_STATE(c->new_state) && c->conn_user != NULL) { 3278 rc = __s_api_setup_getnext(c->conn_user, &c->err_rc, &errorp); 3279 if (rc == LDAP_BUSY || rc == LDAP_UNAVAILABLE || 3280 rc == LDAP_UNWILLING_TO_PERFORM) { 3281 if (errorp != NULL) { 3282 (void) __ns_ldap_freeError(&c->errorp); 3283 c->errorp = errorp; 3284 } 3285 c->new_state = REINIT; 3286 } else if (rc == LDAP_CONNECT_ERROR || 3287 rc == LDAP_SERVER_DOWN) { 3288 if (errorp != NULL) { 3289 (void) __ns_ldap_freeError(&c->errorp); 3290 c->errorp = errorp; 3291 } 3292 c->new_state = REINIT; 3293 /* 3294 * MT connection is not usable, 3295 * close it before REINIT. 3296 */ 3297 __s_api_conn_mt_close( 3298 c->conn_user, rc, NULL); 3299 } else if (rc != NS_LDAP_SUCCESS) { 3300 if (rcp != NULL) 3301 *rcp = rc; 3302 *c->caller_result = NULL; 3303 *c->caller_errorp = errorp; 3304 *c->caller_rc = rc; 3305 return (-1); 3306 } 3307 } 3308 3309 for (;;) { 3310 /* Single step through the search_state_machine */ 3311 state = search_state_machine(c, c->new_state, ONE_STEP); 3312 switch (state) { 3313 case LDAP_ERROR: 3314 (void) search_state_machine(c, state, ONE_STEP); 3315 (void) search_state_machine(c, CLEAR_RESULTS, ONE_STEP); 3316 /* FALLTHROUGH */ 3317 case ERROR: 3318 case EXIT: 3319 *c->caller_result = c->result; 3320 *c->caller_errorp = c->errorp; 3321 *c->caller_rc = 3322 (c->result == NULL && c->err_from_result == 0) 3323 ? NS_LDAP_NOTFOUND : c->err_rc; 3324 c->result = NULL; 3325 c->errorp = NULL; 3326 /* Remove the cookie from the batch */ 3327 ptr = batch->cookie_list; 3328 prev = &batch->cookie_list; 3329 while (ptr != NULL) { 3330 if (ptr == c) { 3331 *prev = ptr->next_cookie_in_batch; 3332 break; 3333 } 3334 prev = &ptr->next_cookie_in_batch; 3335 ptr = ptr->next_cookie_in_batch; 3336 } 3337 /* Delete cookie and decrement active cookie count */ 3338 if (c->conn_user != NULL) { 3339 if (c->conn_user->conn_mt != NULL) 3340 __s_api_conn_mt_return(c->conn_user); 3341 __s_api_conn_user_free(c->conn_user); 3342 c->conn_user = NULL; 3343 } 3344 delete_search_cookie(c); 3345 batch->nactive--; 3346 break; 3347 case NEXT_RESULT: 3348 case MULTI_RESULT: 3349 /* 3350 * This means that search_state_machine needs to do 3351 * another ldap_result() for the cookie in question. 3352 * We only do at most one ldap_result() per call in 3353 * this function and therefore we return. This allows 3354 * the caller to process results from other cookies 3355 * in the batch without getting tied up on just one 3356 * cookie. 3357 */ 3358 break; 3359 default: 3360 /* 3361 * This includes states that follow NEXT_RESULT or 3362 * MULTI_RESULT such as PROCESS_RESULT and 3363 * END_PROCESS_RESULT. We continue processing 3364 * this cookie till we reach either the error, exit 3365 * or the result states. 3366 */ 3367 continue; 3368 } 3369 break; 3370 } 3371 3372 /* Return 0 if no more cookies left otherwise 1 */ 3373 return ((batch->nactive > 0) ? 1 : 0); 3374 } 3375 3376 3377 /* 3378 * Process all the active cookies in the batch and when none 3379 * remains finalize the batch. 3380 */ 3381 int 3382 __ns_ldap_list_batch_end(ns_ldap_list_batch_t *batch) 3383 { 3384 int rc = NS_LDAP_SUCCESS; 3385 while (__ns_ldap_list_batch_process(batch, &rc) > 0) 3386 ; 3387 __ns_ldap_list_batch_release(batch); 3388 return (rc); 3389 } 3390 3391 /* 3392 * find_domainname performs one or more LDAP searches to 3393 * find the value of the nisdomain attribute associated with 3394 * the input DN (with no retry). 3395 */ 3396 3397 static int 3398 find_domainname(const char *dn, char **domainname, const ns_cred_t *cred, 3399 ns_ldap_error_t **errorp, ns_conn_user_t *conn_user) 3400 { 3401 3402 ns_ldap_cookie_t *cookie; 3403 ns_ldap_search_desc_t **sdlist; 3404 ns_ldap_search_desc_t *dptr; 3405 int rc; 3406 char **value; 3407 int flags = 0; 3408 3409 *domainname = NULL; 3410 *errorp = NULL; 3411 3412 /* Initialize State machine cookie */ 3413 cookie = init_search_state_machine(); 3414 if (cookie == NULL) { 3415 return (NS_LDAP_MEMORY); 3416 } 3417 cookie->conn_user = conn_user; 3418 3419 /* see if need to follow referrals */ 3420 rc = __s_api_toFollowReferrals(flags, 3421 &cookie->followRef, errorp); 3422 if (rc != NS_LDAP_SUCCESS) { 3423 delete_search_cookie(cookie); 3424 return (rc); 3425 } 3426 3427 /* Create default service Desc */ 3428 sdlist = (ns_ldap_search_desc_t **)calloc(2, 3429 sizeof (ns_ldap_search_desc_t *)); 3430 if (sdlist == NULL) { 3431 delete_search_cookie(cookie); 3432 cookie = NULL; 3433 return (NS_LDAP_MEMORY); 3434 } 3435 dptr = (ns_ldap_search_desc_t *) 3436 calloc(1, sizeof (ns_ldap_search_desc_t)); 3437 if (dptr == NULL) { 3438 free(sdlist); 3439 delete_search_cookie(cookie); 3440 cookie = NULL; 3441 return (NS_LDAP_MEMORY); 3442 } 3443 sdlist[0] = dptr; 3444 3445 /* search base is dn */ 3446 dptr->basedn = strdup(dn); 3447 3448 /* search scope is base */ 3449 dptr->scope = NS_LDAP_SCOPE_BASE; 3450 3451 /* search filter is "nisdomain=*" */ 3452 dptr->filter = strdup(_NIS_FILTER); 3453 3454 cookie->sdlist = sdlist; 3455 cookie->i_filter = strdup(dptr->filter); 3456 cookie->i_attr = nis_domain_attrs; 3457 cookie->i_auth = cred; 3458 cookie->i_flags = 0; 3459 3460 /* Process search */ 3461 rc = search_state_machine(cookie, INIT, 0); 3462 3463 /* Copy domain name if found */ 3464 rc = cookie->err_rc; 3465 if (rc != NS_LDAP_SUCCESS) { 3466 if (conn_user != NULL && conn_user->ns_error != NULL) { 3467 *errorp = conn_user->ns_error; 3468 conn_user->ns_error = NULL; 3469 } else 3470 *errorp = cookie->errorp; 3471 } 3472 if (cookie->result == NULL) 3473 rc = NS_LDAP_NOTFOUND; 3474 if (rc == NS_LDAP_SUCCESS) { 3475 value = __ns_ldap_getAttr(cookie->result->entry, 3476 _NIS_DOMAIN); 3477 if (value[0]) 3478 *domainname = strdup(value[0]); 3479 else 3480 rc = NS_LDAP_NOTFOUND; 3481 } 3482 if (cookie->result != NULL) 3483 (void) __ns_ldap_freeResult(&cookie->result); 3484 cookie->errorp = NULL; 3485 delete_search_cookie(cookie); 3486 cookie = NULL; 3487 return (rc); 3488 } 3489 3490 /* 3491 * __s_api_find_domainname performs one or more LDAP searches to 3492 * find the value of the nisdomain attribute associated with 3493 * the input DN (with retry). 3494 */ 3495 3496 static int 3497 __s_api_find_domainname(const char *dn, char **domainname, 3498 const ns_cred_t *cred, ns_ldap_error_t **errorp) 3499 { 3500 ns_conn_user_t *cu = NULL; 3501 int try_cnt = 0; 3502 int rc = NS_LDAP_SUCCESS; 3503 3504 for (;;) { 3505 if (__s_api_setup_retry_search(&cu, NS_CONN_USER_SEARCH, 3506 &try_cnt, &rc, errorp) == 0) 3507 break; 3508 rc = find_domainname(dn, domainname, cred, errorp, cu); 3509 } 3510 3511 return (rc); 3512 } 3513 3514 static int 3515 firstEntry( 3516 const char *service, 3517 const char *filter, 3518 int (*init_filter_cb)(const ns_ldap_search_desc_t *desc, 3519 char **realfilter, const void *userdata), 3520 const char * const *attribute, 3521 const ns_cred_t *auth, 3522 const int flags, 3523 void **vcookie, 3524 ns_ldap_result_t **result, 3525 ns_ldap_error_t ** errorp, 3526 const void *userdata, 3527 ns_conn_user_t *conn_user) 3528 { 3529 ns_ldap_cookie_t *cookie = NULL; 3530 ns_ldap_error_t *error = NULL; 3531 ns_state_t state; 3532 ns_ldap_search_desc_t **sdlist; 3533 ns_ldap_search_desc_t *dptr; 3534 char **dns = NULL; 3535 int scope; 3536 int rc; 3537 3538 *errorp = NULL; 3539 *result = NULL; 3540 3541 /* 3542 * Sanity check - NS_LDAP_READ_SHADOW is for our 3543 * own internal use. 3544 */ 3545 if (flags & NS_LDAP_READ_SHADOW) 3546 return (NS_LDAP_INVALID_PARAM); 3547 3548 /* get the service descriptor - or create a default one */ 3549 rc = __s_api_get_SSD_from_SSDtoUse_service(service, 3550 &sdlist, errorp); 3551 if (rc != NS_LDAP_SUCCESS) { 3552 *errorp = error; 3553 return (rc); 3554 } 3555 if (sdlist == NULL) { 3556 /* Create default service Desc */ 3557 sdlist = (ns_ldap_search_desc_t **)calloc(2, 3558 sizeof (ns_ldap_search_desc_t *)); 3559 if (sdlist == NULL) { 3560 return (NS_LDAP_MEMORY); 3561 } 3562 dptr = (ns_ldap_search_desc_t *) 3563 calloc(1, sizeof (ns_ldap_search_desc_t)); 3564 if (dptr == NULL) { 3565 free(sdlist); 3566 return (NS_LDAP_MEMORY); 3567 } 3568 sdlist[0] = dptr; 3569 3570 /* default base */ 3571 rc = __s_api_getDNs(&dns, service, &error); 3572 if (rc != NS_LDAP_SUCCESS) { 3573 if (dns) { 3574 __s_api_free2dArray(dns); 3575 dns = NULL; 3576 } 3577 if (sdlist) { 3578 (void) __ns_ldap_freeSearchDescriptors( 3579 &sdlist); 3580 3581 sdlist = NULL; 3582 } 3583 *errorp = error; 3584 return (rc); 3585 } 3586 dptr->basedn = strdup(dns[0]); 3587 __s_api_free2dArray(dns); 3588 dns = NULL; 3589 3590 /* default scope */ 3591 scope = 0; 3592 cookie = init_search_state_machine(); 3593 if (cookie == NULL) { 3594 if (sdlist) { 3595 (void) __ns_ldap_freeSearchDescriptors(&sdlist); 3596 sdlist = NULL; 3597 } 3598 return (NS_LDAP_MEMORY); 3599 } 3600 rc = __s_api_getSearchScope(&scope, &cookie->errorp); 3601 dptr->scope = scope; 3602 } 3603 3604 /* Initialize State machine cookie */ 3605 if (cookie == NULL) 3606 cookie = init_search_state_machine(); 3607 if (cookie == NULL) { 3608 if (sdlist) { 3609 (void) __ns_ldap_freeSearchDescriptors(&sdlist); 3610 sdlist = NULL; 3611 } 3612 return (NS_LDAP_MEMORY); 3613 } 3614 3615 /* identify self as a getent user */ 3616 cookie->conn_user = conn_user; 3617 3618 cookie->sdlist = sdlist; 3619 3620 /* see if need to follow referrals */ 3621 rc = __s_api_toFollowReferrals(flags, 3622 &cookie->followRef, errorp); 3623 if (rc != NS_LDAP_SUCCESS) { 3624 delete_search_cookie(cookie); 3625 return (rc); 3626 } 3627 3628 /* 3629 * use VLV/PAGE control only if NS_LDAP_NO_PAGE_CTRL is not set 3630 */ 3631 if (flags & NS_LDAP_NO_PAGE_CTRL) 3632 cookie->use_paging = FALSE; 3633 else 3634 cookie->use_paging = TRUE; 3635 3636 /* Set up other arguments */ 3637 cookie->userdata = userdata; 3638 if (init_filter_cb != NULL) { 3639 cookie->init_filter_cb = init_filter_cb; 3640 cookie->use_filtercb = 1; 3641 } 3642 cookie->use_usercb = 0; 3643 /* check_shadow() may add extra value to cookie->i_flags */ 3644 cookie->i_flags = flags; 3645 if (service) { 3646 cookie->service = strdup(service); 3647 if (cookie->service == NULL) { 3648 delete_search_cookie(cookie); 3649 return (NS_LDAP_MEMORY); 3650 } 3651 3652 /* 3653 * If given, use the credential given by the caller, and 3654 * skip the credential check required for shadow update. 3655 */ 3656 if (auth == NULL) { 3657 rc = check_shadow(cookie, service); 3658 if (rc != NS_LDAP_SUCCESS) { 3659 *errorp = cookie->errorp; 3660 cookie->errorp = NULL; 3661 delete_search_cookie(cookie); 3662 cookie = NULL; 3663 return (rc); 3664 } 3665 } 3666 } 3667 3668 cookie->i_filter = strdup(filter); 3669 cookie->i_attr = attribute; 3670 cookie->i_auth = auth; 3671 3672 state = INIT; 3673 for (;;) { 3674 state = search_state_machine(cookie, state, ONE_STEP); 3675 switch (state) { 3676 case PROCESS_RESULT: 3677 *result = cookie->result; 3678 cookie->result = NULL; 3679 *vcookie = (void *)cookie; 3680 return (NS_LDAP_SUCCESS); 3681 case LDAP_ERROR: 3682 state = search_state_machine(cookie, state, ONE_STEP); 3683 state = search_state_machine(cookie, CLEAR_RESULTS, 3684 ONE_STEP); 3685 /* FALLTHROUGH */ 3686 case ERROR: 3687 rc = cookie->err_rc; 3688 if (conn_user != NULL && conn_user->ns_error != NULL) { 3689 *errorp = conn_user->ns_error; 3690 conn_user->ns_error = NULL; 3691 } else { 3692 *errorp = cookie->errorp; 3693 cookie->errorp = NULL; 3694 } 3695 delete_search_cookie(cookie); 3696 return (rc); 3697 case EXIT: 3698 rc = cookie->err_rc; 3699 if (rc != NS_LDAP_SUCCESS) { 3700 *errorp = cookie->errorp; 3701 cookie->errorp = NULL; 3702 } else { 3703 rc = NS_LDAP_NOTFOUND; 3704 } 3705 3706 delete_search_cookie(cookie); 3707 return (rc); 3708 3709 default: 3710 break; 3711 } 3712 } 3713 } 3714 3715 int 3716 __ns_ldap_firstEntry( 3717 const char *service, 3718 const char *filter, 3719 int (*init_filter_cb)(const ns_ldap_search_desc_t *desc, 3720 char **realfilter, const void *userdata), 3721 const char * const *attribute, 3722 const ns_cred_t *auth, 3723 const int flags, 3724 void **vcookie, 3725 ns_ldap_result_t **result, 3726 ns_ldap_error_t ** errorp, 3727 const void *userdata) 3728 { 3729 ns_conn_user_t *cu = NULL; 3730 int try_cnt = 0; 3731 int rc = NS_LDAP_SUCCESS; 3732 3733 for (;;) { 3734 if (__s_api_setup_retry_search(&cu, NS_CONN_USER_GETENT, 3735 &try_cnt, &rc, errorp) == 0) 3736 break; 3737 rc = firstEntry(service, filter, init_filter_cb, attribute, 3738 auth, flags, vcookie, result, errorp, userdata, cu); 3739 } 3740 return (rc); 3741 } 3742 3743 /*ARGSUSED2*/ 3744 int 3745 __ns_ldap_nextEntry(void *vcookie, ns_ldap_result_t **result, 3746 ns_ldap_error_t ** errorp) 3747 { 3748 ns_ldap_cookie_t *cookie; 3749 ns_state_t state; 3750 int rc; 3751 3752 cookie = (ns_ldap_cookie_t *)vcookie; 3753 cookie->result = NULL; 3754 *result = NULL; 3755 3756 if (cookie->conn_user != NULL) { 3757 rc = __s_api_setup_getnext(cookie->conn_user, 3758 &cookie->err_rc, errorp); 3759 if (rc != NS_LDAP_SUCCESS) 3760 return (rc); 3761 } 3762 3763 state = END_PROCESS_RESULT; 3764 for (;;) { 3765 state = search_state_machine(cookie, state, ONE_STEP); 3766 switch (state) { 3767 case PROCESS_RESULT: 3768 *result = cookie->result; 3769 cookie->result = NULL; 3770 return (NS_LDAP_SUCCESS); 3771 case LDAP_ERROR: 3772 state = search_state_machine(cookie, state, ONE_STEP); 3773 state = search_state_machine(cookie, CLEAR_RESULTS, 3774 ONE_STEP); 3775 /* FALLTHROUGH */ 3776 case ERROR: 3777 rc = cookie->err_rc; 3778 *errorp = cookie->errorp; 3779 cookie->errorp = NULL; 3780 return (rc); 3781 case EXIT: 3782 return (NS_LDAP_SUCCESS); 3783 } 3784 } 3785 } 3786 3787 int 3788 __ns_ldap_endEntry( 3789 void **vcookie, 3790 ns_ldap_error_t ** errorp) 3791 { 3792 ns_ldap_cookie_t *cookie; 3793 int rc; 3794 3795 if (*vcookie == NULL) 3796 return (NS_LDAP_INVALID_PARAM); 3797 3798 cookie = (ns_ldap_cookie_t *)(*vcookie); 3799 cookie->result = NULL; 3800 3801 /* Complete search */ 3802 rc = search_state_machine(cookie, CLEAR_RESULTS, 0); 3803 3804 /* Copy results back to user */ 3805 rc = cookie->err_rc; 3806 if (rc != NS_LDAP_SUCCESS) 3807 *errorp = cookie->errorp; 3808 3809 cookie->errorp = NULL; 3810 if (cookie->conn_user != NULL) { 3811 if (cookie->conn_user->conn_mt != NULL) 3812 __s_api_conn_mt_return(cookie->conn_user); 3813 __s_api_conn_user_free(cookie->conn_user); 3814 } 3815 delete_search_cookie(cookie); 3816 cookie = NULL; 3817 *vcookie = NULL; 3818 3819 return (rc); 3820 } 3821 3822 3823 int 3824 __ns_ldap_freeResult(ns_ldap_result_t **result) 3825 { 3826 3827 ns_ldap_entry_t *curEntry = NULL; 3828 ns_ldap_entry_t *delEntry = NULL; 3829 int i; 3830 ns_ldap_result_t *res = *result; 3831 3832 #ifdef DEBUG 3833 (void) fprintf(stderr, "__ns_ldap_freeResult START\n"); 3834 #endif 3835 if (res == NULL) 3836 return (NS_LDAP_INVALID_PARAM); 3837 3838 if (res->entry != NULL) 3839 curEntry = res->entry; 3840 3841 for (i = 0; i < res->entries_count; i++) { 3842 if (curEntry != NULL) { 3843 delEntry = curEntry; 3844 curEntry = curEntry->next; 3845 __ns_ldap_freeEntry(delEntry); 3846 } 3847 } 3848 3849 free(res); 3850 *result = NULL; 3851 return (NS_LDAP_SUCCESS); 3852 } 3853 3854 /*ARGSUSED*/ 3855 int 3856 __ns_ldap_auth(const ns_cred_t *auth, 3857 const int flags, 3858 ns_ldap_error_t **errorp, 3859 LDAPControl **serverctrls, 3860 LDAPControl **clientctrls) 3861 { 3862 3863 ConnectionID connectionId = -1; 3864 Connection *conp; 3865 int rc = 0; 3866 int do_not_fail_if_new_pwd_reqd = 0; 3867 int nopasswd_acct_mgmt = 0; 3868 ns_conn_user_t *conn_user; 3869 3870 3871 #ifdef DEBUG 3872 (void) fprintf(stderr, "__ns_ldap_auth START\n"); 3873 #endif 3874 3875 *errorp = NULL; 3876 if (!auth) 3877 return (NS_LDAP_INVALID_PARAM); 3878 3879 conn_user = __s_api_conn_user_init(NS_CONN_USER_AUTH, 3880 NULL, B_FALSE); 3881 3882 rc = __s_api_getConnection(NULL, flags | NS_LDAP_NEW_CONN, 3883 auth, &connectionId, &conp, errorp, 3884 do_not_fail_if_new_pwd_reqd, nopasswd_acct_mgmt, 3885 conn_user); 3886 3887 if (conn_user != NULL) 3888 __s_api_conn_user_free(conn_user); 3889 3890 if (rc == NS_LDAP_OP_FAILED && *errorp) 3891 (void) __ns_ldap_freeError(errorp); 3892 3893 if (connectionId > -1) 3894 DropConnection(connectionId, flags); 3895 return (rc); 3896 } 3897 3898 char ** 3899 __ns_ldap_getAttr(const ns_ldap_entry_t *entry, const char *attrname) 3900 { 3901 int i; 3902 3903 if (entry == NULL) 3904 return (NULL); 3905 for (i = 0; i < entry->attr_count; i++) { 3906 if (strcasecmp(entry->attr_pair[i]->attrname, attrname) == NULL) 3907 return (entry->attr_pair[i]->attrvalue); 3908 } 3909 return (NULL); 3910 } 3911 3912 ns_ldap_attr_t * 3913 __ns_ldap_getAttrStruct(const ns_ldap_entry_t *entry, const char *attrname) 3914 { 3915 int i; 3916 3917 if (entry == NULL) 3918 return (NULL); 3919 for (i = 0; i < entry->attr_count; i++) { 3920 if (strcasecmp(entry->attr_pair[i]->attrname, attrname) == NULL) 3921 return (entry->attr_pair[i]); 3922 } 3923 return (NULL); 3924 } 3925 3926 3927 /*ARGSUSED*/ 3928 int 3929 __ns_ldap_uid2dn(const char *uid, 3930 char **userDN, 3931 const ns_cred_t *cred, /* cred is ignored */ 3932 ns_ldap_error_t **errorp) 3933 { 3934 ns_ldap_result_t *result = NULL; 3935 char *filter, *userdata; 3936 char errstr[MAXERROR]; 3937 char **value; 3938 int rc = 0; 3939 int i = 0; 3940 size_t len; 3941 3942 *errorp = NULL; 3943 *userDN = NULL; 3944 if ((uid == NULL) || (uid[0] == '\0')) 3945 return (NS_LDAP_INVALID_PARAM); 3946 3947 while (uid[i] != '\0') { 3948 if (uid[i] == '=') { 3949 *userDN = strdup(uid); 3950 return (NS_LDAP_SUCCESS); 3951 } 3952 i++; 3953 } 3954 i = 0; 3955 while ((uid[i] != '\0') && (isdigit(uid[i]))) 3956 i++; 3957 if (uid[i] == '\0') { 3958 len = strlen(UIDNUMFILTER) + strlen(uid) + 1; 3959 filter = (char *)malloc(len); 3960 if (filter == NULL) { 3961 *userDN = NULL; 3962 return (NS_LDAP_MEMORY); 3963 } 3964 (void) snprintf(filter, len, UIDNUMFILTER, uid); 3965 3966 len = strlen(UIDNUMFILTER_SSD) + strlen(uid) + 1; 3967 userdata = (char *)malloc(len); 3968 if (userdata == NULL) { 3969 *userDN = NULL; 3970 return (NS_LDAP_MEMORY); 3971 } 3972 (void) snprintf(userdata, len, UIDNUMFILTER_SSD, uid); 3973 } else { 3974 len = strlen(UIDFILTER) + strlen(uid) + 1; 3975 filter = (char *)malloc(len); 3976 if (filter == NULL) { 3977 *userDN = NULL; 3978 return (NS_LDAP_MEMORY); 3979 } 3980 (void) snprintf(filter, len, UIDFILTER, uid); 3981 3982 len = strlen(UIDFILTER_SSD) + strlen(uid) + 1; 3983 userdata = (char *)malloc(len); 3984 if (userdata == NULL) { 3985 *userDN = NULL; 3986 return (NS_LDAP_MEMORY); 3987 } 3988 (void) snprintf(userdata, len, UIDFILTER_SSD, uid); 3989 } 3990 3991 /* 3992 * we want to retrieve the DN as it appears in LDAP 3993 * hence the use of NS_LDAP_NOT_CVT_DN in flags 3994 */ 3995 rc = __ns_ldap_list("passwd", filter, 3996 __s_api_merge_SSD_filter, 3997 NULL, cred, NS_LDAP_NOT_CVT_DN, 3998 &result, errorp, NULL, 3999 userdata); 4000 free(filter); 4001 filter = NULL; 4002 free(userdata); 4003 userdata = NULL; 4004 if (rc != NS_LDAP_SUCCESS) { 4005 if (result) { 4006 (void) __ns_ldap_freeResult(&result); 4007 result = NULL; 4008 } 4009 return (rc); 4010 } 4011 if (result->entries_count > 1) { 4012 (void) __ns_ldap_freeResult(&result); 4013 result = NULL; 4014 *userDN = NULL; 4015 (void) sprintf(errstr, 4016 gettext("Too many entries are returned for %s"), uid); 4017 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, strdup(errstr), 4018 NULL); 4019 return (NS_LDAP_INTERNAL); 4020 } 4021 4022 value = __ns_ldap_getAttr(result->entry, "dn"); 4023 *userDN = strdup(value[0]); 4024 (void) __ns_ldap_freeResult(&result); 4025 result = NULL; 4026 return (NS_LDAP_SUCCESS); 4027 } 4028 4029 4030 /*ARGSUSED*/ 4031 int 4032 __ns_ldap_host2dn(const char *host, 4033 const char *domain, 4034 char **hostDN, 4035 const ns_cred_t *cred, /* cred is ignored */ 4036 ns_ldap_error_t **errorp) 4037 { 4038 ns_ldap_result_t *result = NULL; 4039 char *filter, *userdata; 4040 char errstr[MAXERROR]; 4041 char **value; 4042 int rc; 4043 size_t len; 4044 4045 /* 4046 * XXX 4047 * the domain parameter needs to be used in case domain is not local, if 4048 * this routine is to support multi domain setups, it needs lots of work... 4049 */ 4050 *errorp = NULL; 4051 *hostDN = NULL; 4052 if ((host == NULL) || (host[0] == '\0')) 4053 return (NS_LDAP_INVALID_PARAM); 4054 4055 len = strlen(HOSTFILTER) + strlen(host) + 1; 4056 filter = (char *)malloc(len); 4057 if (filter == NULL) { 4058 return (NS_LDAP_MEMORY); 4059 } 4060 (void) snprintf(filter, len, HOSTFILTER, host); 4061 4062 len = strlen(HOSTFILTER_SSD) + strlen(host) + 1; 4063 userdata = (char *)malloc(len); 4064 if (userdata == NULL) { 4065 return (NS_LDAP_MEMORY); 4066 } 4067 (void) snprintf(userdata, len, HOSTFILTER_SSD, host); 4068 4069 /* 4070 * we want to retrieve the DN as it appears in LDAP 4071 * hence the use of NS_LDAP_NOT_CVT_DN in flags 4072 */ 4073 rc = __ns_ldap_list("hosts", filter, 4074 __s_api_merge_SSD_filter, 4075 NULL, cred, NS_LDAP_NOT_CVT_DN, &result, 4076 errorp, NULL, 4077 userdata); 4078 free(filter); 4079 filter = NULL; 4080 free(userdata); 4081 userdata = NULL; 4082 if (rc != NS_LDAP_SUCCESS) { 4083 if (result) { 4084 (void) __ns_ldap_freeResult(&result); 4085 result = NULL; 4086 } 4087 return (rc); 4088 } 4089 4090 if (result->entries_count > 1) { 4091 (void) __ns_ldap_freeResult(&result); 4092 result = NULL; 4093 *hostDN = NULL; 4094 (void) sprintf(errstr, 4095 gettext("Too many entries are returned for %s"), host); 4096 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, strdup(errstr), 4097 NULL); 4098 return (NS_LDAP_INTERNAL); 4099 } 4100 4101 value = __ns_ldap_getAttr(result->entry, "dn"); 4102 *hostDN = strdup(value[0]); 4103 (void) __ns_ldap_freeResult(&result); 4104 result = NULL; 4105 return (NS_LDAP_SUCCESS); 4106 } 4107 4108 /*ARGSUSED*/ 4109 int 4110 __ns_ldap_dn2domain(const char *dn, 4111 char **domain, 4112 const ns_cred_t *cred, 4113 ns_ldap_error_t **errorp) 4114 { 4115 int rc, pnum, i, j, len = 0; 4116 char *newdn, **rdns = NULL; 4117 char **dns, *dn1; 4118 4119 *errorp = NULL; 4120 4121 if (domain == NULL) 4122 return (NS_LDAP_INVALID_PARAM); 4123 else 4124 *domain = NULL; 4125 4126 if ((dn == NULL) || (dn[0] == '\0')) 4127 return (NS_LDAP_INVALID_PARAM); 4128 4129 /* 4130 * break dn into rdns 4131 */ 4132 dn1 = strdup(dn); 4133 if (dn1 == NULL) 4134 return (NS_LDAP_MEMORY); 4135 rdns = ldap_explode_dn(dn1, 0); 4136 free(dn1); 4137 if (rdns == NULL || *rdns == NULL) 4138 return (NS_LDAP_INVALID_PARAM); 4139 4140 for (i = 0; rdns[i]; i++) 4141 len += strlen(rdns[i]) + 1; 4142 pnum = i; 4143 4144 newdn = (char *)malloc(len + 1); 4145 dns = (char **)calloc(pnum, sizeof (char *)); 4146 if (newdn == NULL || dns == NULL) { 4147 if (newdn) 4148 free(newdn); 4149 ldap_value_free(rdns); 4150 return (NS_LDAP_MEMORY); 4151 } 4152 4153 /* construct a semi-normalized dn, newdn */ 4154 *newdn = '\0'; 4155 for (i = 0; rdns[i]; i++) { 4156 dns[i] = newdn + strlen(newdn); 4157 (void) strcat(newdn, 4158 __s_api_remove_rdn_space(rdns[i])); 4159 (void) strcat(newdn, ","); 4160 } 4161 /* remove the last ',' */ 4162 newdn[strlen(newdn) - 1] = '\0'; 4163 ldap_value_free(rdns); 4164 4165 /* 4166 * loop and find the domain name associated with newdn, 4167 * removing rdn one by one from left to right 4168 */ 4169 for (i = 0; i < pnum; i++) { 4170 4171 if (*errorp) 4172 (void) __ns_ldap_freeError(errorp); 4173 4174 /* 4175 * try cache manager first 4176 */ 4177 rc = __s_api_get_cachemgr_data(NS_CACHE_DN2DOMAIN, 4178 dns[i], domain); 4179 if (rc != NS_LDAP_SUCCESS) { 4180 /* 4181 * try ldap server second 4182 */ 4183 rc = __s_api_find_domainname(dns[i], domain, 4184 cred, errorp); 4185 } else { 4186 /* 4187 * skip the last one, 4188 * since it is already cached by ldap_cachemgr 4189 */ 4190 i--; 4191 } 4192 if (rc == NS_LDAP_SUCCESS) { 4193 if (__s_api_nscd_proc()) { 4194 /* 4195 * If it's nscd, ask cache manager to save the 4196 * dn to domain mapping(s) 4197 */ 4198 for (j = 0; j <= i; j++) { 4199 (void) __s_api_set_cachemgr_data( 4200 NS_CACHE_DN2DOMAIN, 4201 dns[j], 4202 *domain); 4203 } 4204 } 4205 break; 4206 } 4207 } 4208 4209 free(dns); 4210 free(newdn); 4211 if (rc != NS_LDAP_SUCCESS) 4212 rc = NS_LDAP_NOTFOUND; 4213 return (rc); 4214 } 4215 4216 /*ARGSUSED*/ 4217 int 4218 __ns_ldap_getServiceAuthMethods(const char *service, 4219 ns_auth_t ***auth, 4220 ns_ldap_error_t **errorp) 4221 { 4222 char errstr[MAXERROR]; 4223 int rc, i, done = 0; 4224 int slen; 4225 void **param; 4226 char **sam, *srv, *send; 4227 ns_auth_t **authpp = NULL, *ap; 4228 int cnt, max; 4229 ns_config_t *cfg; 4230 ns_ldap_error_t *error = NULL; 4231 4232 if (errorp == NULL) 4233 return (NS_LDAP_INVALID_PARAM); 4234 *errorp = NULL; 4235 4236 if ((service == NULL) || (service[0] == '\0') || 4237 (auth == NULL)) 4238 return (NS_LDAP_INVALID_PARAM); 4239 4240 *auth = NULL; 4241 rc = __ns_ldap_getParam(NS_LDAP_SERVICE_AUTH_METHOD_P, ¶m, &error); 4242 if (rc != NS_LDAP_SUCCESS || param == NULL) { 4243 *errorp = error; 4244 return (rc); 4245 } 4246 sam = (char **)param; 4247 4248 cfg = __s_api_get_default_config(); 4249 cnt = 0; 4250 4251 slen = strlen(service); 4252 4253 for (; *sam; sam++) { 4254 srv = *sam; 4255 if (strncasecmp(service, srv, slen) != 0) 4256 continue; 4257 srv += slen; 4258 if (*srv != COLONTOK) 4259 continue; 4260 send = srv; 4261 srv++; 4262 for (max = 1; (send = strchr(++send, SEMITOK)) != NULL; 4263 max++) {} 4264 authpp = (ns_auth_t **)calloc(++max, sizeof (ns_auth_t *)); 4265 if (authpp == NULL) { 4266 (void) __ns_ldap_freeParam(¶m); 4267 __s_api_release_config(cfg); 4268 return (NS_LDAP_MEMORY); 4269 } 4270 while (!done) { 4271 send = strchr(srv, SEMITOK); 4272 if (send != NULL) { 4273 *send = '\0'; 4274 send++; 4275 } 4276 i = __s_get_enum_value(cfg, srv, NS_LDAP_AUTH_P); 4277 if (i == -1) { 4278 (void) __ns_ldap_freeParam(¶m); 4279 (void) sprintf(errstr, 4280 gettext("Unsupported " 4281 "serviceAuthenticationMethod: %s.\n"), srv); 4282 MKERROR(LOG_WARNING, *errorp, NS_CONFIG_SYNTAX, 4283 strdup(errstr), NULL); 4284 __s_api_release_config(cfg); 4285 return (NS_LDAP_CONFIG); 4286 } 4287 ap = __s_api_AuthEnumtoStruct((EnumAuthType_t)i); 4288 if (ap == NULL) { 4289 (void) __ns_ldap_freeParam(¶m); 4290 __s_api_release_config(cfg); 4291 return (NS_LDAP_MEMORY); 4292 } 4293 authpp[cnt++] = ap; 4294 if (send == NULL) 4295 done = TRUE; 4296 else 4297 srv = send; 4298 } 4299 } 4300 4301 *auth = authpp; 4302 (void) __ns_ldap_freeParam(¶m); 4303 __s_api_release_config(cfg); 4304 return (NS_LDAP_SUCCESS); 4305 } 4306 4307 /* 4308 * This routine is called when certain scenario occurs 4309 * e.g. 4310 * service == auto_home 4311 * SSD = automount: ou = mytest, 4312 * NS_LDAP_MAPATTRIBUTE= auto_home: automountMapName=AAA 4313 * NS_LDAP_OBJECTCLASSMAP= auto_home:automountMap=MynisMap 4314 * NS_LDAP_OBJECTCLASSMAP= auto_home:automount=MynisObject 4315 * 4316 * The automountMapName is prepended implicitely but is mapped 4317 * to AAA. So dn could appers as 4318 * dn: AAA=auto_home,ou=bar,dc=foo,dc=com 4319 * dn: automountKey=user_01,AAA=auto_home,ou=bar,dc=foo,dc=com 4320 * dn: automountKey=user_02,AAA=auto_home,ou=bar,dc=foo,dc=com 4321 * in the directory. 4322 * This function is called to covert the mapped attr back to 4323 * orig attr when the entries are searched and returned 4324 */ 4325 4326 int 4327 __s_api_convert_automountmapname(const char *service, char **dn, 4328 ns_ldap_error_t **errp) { 4329 4330 char **mapping = NULL; 4331 char *mapped_attr = NULL; 4332 char *automountmapname = "automountMapName"; 4333 char *buffer = NULL; 4334 int rc = NS_LDAP_SUCCESS; 4335 char errstr[MAXERROR]; 4336 4337 /* 4338 * dn is an input/out parameter, check it first 4339 */ 4340 4341 if (service == NULL || dn == NULL || *dn == NULL) 4342 return (NS_LDAP_INVALID_PARAM); 4343 4344 /* 4345 * Check to see if there is a mapped attribute for auto_xxx 4346 */ 4347 4348 mapping = __ns_ldap_getMappedAttributes(service, automountmapname); 4349 4350 /* 4351 * if no mapped attribute for auto_xxx, try automount 4352 */ 4353 4354 if (mapping == NULL) 4355 mapping = __ns_ldap_getMappedAttributes( 4356 "automount", automountmapname); 4357 4358 /* 4359 * if no mapped attribute is found, return SUCCESS (no op) 4360 */ 4361 4362 if (mapping == NULL) 4363 return (NS_LDAP_SUCCESS); 4364 4365 /* 4366 * if the mapped attribute is found and attr is not empty, 4367 * copy it 4368 */ 4369 4370 if (mapping[0] != NULL) { 4371 mapped_attr = strdup(mapping[0]); 4372 __s_api_free2dArray(mapping); 4373 if (mapped_attr == NULL) { 4374 return (NS_LDAP_MEMORY); 4375 } 4376 } else { 4377 __s_api_free2dArray(mapping); 4378 4379 (void) snprintf(errstr, (2 * MAXERROR), 4380 gettext( 4381 "Attribute nisMapName is mapped to an " 4382 "empty string.\n")); 4383 4384 MKERROR(LOG_ERR, *errp, NS_CONFIG_SYNTAX, 4385 strdup(errstr), NULL); 4386 4387 return (NS_LDAP_CONFIG); 4388 } 4389 4390 /* 4391 * Locate the mapped attribute in the dn 4392 * and replace it if it exists 4393 */ 4394 4395 rc = __s_api_replace_mapped_attr_in_dn( 4396 (const char *) automountmapname, (const char *) mapped_attr, 4397 (const char *) *dn, &buffer); 4398 4399 /* clean up */ 4400 4401 free(mapped_attr); 4402 4403 /* 4404 * If mapped attr is found(buffer != NULL) 4405 * a new dn is returned 4406 * If no mapped attribute is in dn, 4407 * return NS_LDAP_SUCCESS (no op) 4408 * If no memory, 4409 * return NS_LDAP_MEMORY (no op) 4410 */ 4411 4412 if (buffer != NULL) { 4413 free(*dn); 4414 *dn = buffer; 4415 } 4416 4417 return (rc); 4418 } 4419 4420 /* 4421 * If the mapped attr is found in the dn, 4422 * return NS_LDAP_SUCCESS and a new_dn. 4423 * If no mapped attr is found, 4424 * return NS_LDAP_SUCCESS and *new_dn == NULL 4425 * If there is not enough memory, 4426 * return NS_LDAP_MEMORY and *new_dn == NULL 4427 */ 4428 4429 int 4430 __s_api_replace_mapped_attr_in_dn( 4431 const char *orig_attr, const char *mapped_attr, 4432 const char *dn, char **new_dn) { 4433 4434 char **dnArray = NULL; 4435 char *cur = NULL, *start = NULL; 4436 int i = 0, found = 0; 4437 int len = 0, orig_len = 0, mapped_len = 0; 4438 int dn_len = 0, tmp_len = 0; 4439 4440 *new_dn = NULL; 4441 4442 /* 4443 * seperate dn into individual componets 4444 * e.g. 4445 * "automountKey=user_01" , "automountMapName_test=auto_home", ... 4446 */ 4447 dnArray = ldap_explode_dn(dn, 0); 4448 4449 /* 4450 * This will find "mapped attr=value" in dn. 4451 * It won't find match if mapped attr appears 4452 * in the value. 4453 */ 4454 for (i = 0; dnArray[i] != NULL; i++) { 4455 /* 4456 * This function is called when reading from 4457 * the directory so assume each component has "=". 4458 * Any ill formatted dn should be rejected 4459 * before adding to the directory 4460 */ 4461 cur = strchr(dnArray[i], '='); 4462 *cur = '\0'; 4463 if (strcasecmp(mapped_attr, dnArray[i]) == 0) 4464 found = 1; 4465 *cur = '='; 4466 if (found) break; 4467 } 4468 4469 if (!found) { 4470 __s_api_free2dArray(dnArray); 4471 *new_dn = NULL; 4472 return (NS_LDAP_SUCCESS); 4473 } 4474 /* 4475 * The new length is *dn length + (difference between 4476 * orig attr and mapped attr) + 1 ; 4477 * e.g. 4478 * automountKey=aa,automountMapName_test=auto_home,dc=foo,dc=com 4479 * ==> 4480 * automountKey=aa,automountMapName=auto_home,dc=foo,dc=com 4481 */ 4482 mapped_len = strlen(mapped_attr); 4483 orig_len = strlen(orig_attr); 4484 dn_len = strlen(dn); 4485 len = dn_len + orig_len - mapped_len + 1; 4486 *new_dn = (char *)calloc(1, len); 4487 if (*new_dn == NULL) { 4488 __s_api_free2dArray(dnArray); 4489 return (NS_LDAP_MEMORY); 4490 } 4491 4492 /* 4493 * Locate the mapped attr in the dn. 4494 * Use dnArray[i] instead of mapped_attr 4495 * because mapped_attr could appear in 4496 * the value 4497 */ 4498 4499 cur = strstr(dn, dnArray[i]); 4500 __s_api_free2dArray(dnArray); 4501 /* copy the portion before mapped attr in dn */ 4502 start = *new_dn; 4503 tmp_len = cur - dn; 4504 (void) memcpy((void *) start, (const void*) dn, tmp_len); 4505 4506 /* 4507 * Copy the orig_attr. e.g. automountMapName 4508 * This replaces mapped attr with orig attr 4509 */ 4510 start = start + (cur - dn); /* move cursor in buffer */ 4511 (void) memcpy((void *) start, (const void*) orig_attr, orig_len); 4512 4513 /* 4514 * Copy the portion after mapped attr in dn 4515 */ 4516 cur = cur + mapped_len; /* move cursor in dn */ 4517 start = start + orig_len; /* move cursor in buffer */ 4518 (void) strcpy(start, cur); 4519 4520 return (NS_LDAP_SUCCESS); 4521 } 4522 4523 /* 4524 * Validate Filter functions 4525 */ 4526 4527 /* ***** Start of modified libldap.so.5 filter parser ***** */ 4528 4529 /* filter parsing routine forward references */ 4530 static int adj_filter_list(char *str); 4531 static int adj_simple_filter(char *str); 4532 static int unescape_filterval(char *val); 4533 static int hexchar2int(char c); 4534 static int adj_substring_filter(char *val); 4535 4536 4537 /* 4538 * assumes string manipulation is in-line 4539 * and all strings are sufficient in size 4540 * return value is the position after 'c' 4541 */ 4542 4543 static char * 4544 resync_str(char *str, char *next, char c) 4545 { 4546 char *ret; 4547 4548 ret = str + strlen(str); 4549 *next = c; 4550 if (ret == next) 4551 return (ret); 4552 (void) strcat(str, next); 4553 return (ret); 4554 } 4555 4556 static char * 4557 find_right_paren(char *s) 4558 { 4559 int balance, escape; 4560 4561 balance = 1; 4562 escape = 0; 4563 while (*s && balance) { 4564 if (escape == 0) { 4565 if (*s == '(') 4566 balance++; 4567 else if (*s == ')') 4568 balance--; 4569 } 4570 if (*s == '\\' && ! escape) 4571 escape = 1; 4572 else 4573 escape = 0; 4574 if (balance) 4575 s++; 4576 } 4577 4578 return (*s ? s : NULL); 4579 } 4580 4581 static char * 4582 adj_complex_filter(char *str) 4583 { 4584 char *next; 4585 4586 /* 4587 * We have (x(filter)...) with str sitting on 4588 * the x. We have to find the paren matching 4589 * the one before the x and put the intervening 4590 * filters by calling adj_filter_list(). 4591 */ 4592 4593 str++; 4594 if ((next = find_right_paren(str)) == NULL) 4595 return (NULL); 4596 4597 *next = '\0'; 4598 if (adj_filter_list(str) == -1) 4599 return (NULL); 4600 next = resync_str(str, next, ')'); 4601 next++; 4602 4603 return (next); 4604 } 4605 4606 static int 4607 adj_filter(char *str) 4608 { 4609 char *next; 4610 int parens, balance, escape; 4611 char *np, *cp, *dp; 4612 4613 parens = 0; 4614 while (*str) { 4615 switch (*str) { 4616 case '(': 4617 str++; 4618 parens++; 4619 switch (*str) { 4620 case '&': 4621 if ((str = adj_complex_filter(str)) == NULL) 4622 return (-1); 4623 4624 parens--; 4625 break; 4626 4627 case '|': 4628 if ((str = adj_complex_filter(str)) == NULL) 4629 return (-1); 4630 4631 parens--; 4632 break; 4633 4634 case '!': 4635 if ((str = adj_complex_filter(str)) == NULL) 4636 return (-1); 4637 4638 parens--; 4639 break; 4640 4641 case '(': 4642 /* illegal ((case - generated by conversion */ 4643 4644 /* find missing close) */ 4645 np = find_right_paren(str+1); 4646 4647 /* error if not found */ 4648 if (np == NULL) 4649 return (-1); 4650 4651 /* remove redundant (and) */ 4652 for (dp = str, cp = str+1; cp < np; ) { 4653 *dp++ = *cp++; 4654 } 4655 cp++; 4656 while (*cp) 4657 *dp++ = *cp++; 4658 *dp = '\0'; 4659 4660 /* re-start test at original ( */ 4661 parens--; 4662 str--; 4663 break; 4664 4665 default: 4666 balance = 1; 4667 escape = 0; 4668 next = str; 4669 while (*next && balance) { 4670 if (escape == 0) { 4671 if (*next == '(') 4672 balance++; 4673 else if (*next == ')') 4674 balance--; 4675 } 4676 if (*next == '\\' && ! escape) 4677 escape = 1; 4678 else 4679 escape = 0; 4680 if (balance) 4681 next++; 4682 } 4683 if (balance != 0) 4684 return (-1); 4685 4686 *next = '\0'; 4687 if (adj_simple_filter(str) == -1) { 4688 return (-1); 4689 } 4690 next = resync_str(str, next, ')'); 4691 next++; 4692 str = next; 4693 parens--; 4694 break; 4695 } 4696 break; 4697 4698 case ')': 4699 str++; 4700 parens--; 4701 break; 4702 4703 case ' ': 4704 str++; 4705 break; 4706 4707 default: /* assume it's a simple type=value filter */ 4708 next = strchr(str, '\0'); 4709 if (adj_simple_filter(str) == -1) { 4710 return (-1); 4711 } 4712 str = next; 4713 break; 4714 } 4715 } 4716 4717 return (parens ? -1 : 0); 4718 } 4719 4720 4721 /* 4722 * Put a list of filters like this "(filter1)(filter2)..." 4723 */ 4724 4725 static int 4726 adj_filter_list(char *str) 4727 { 4728 char *next; 4729 char save; 4730 4731 while (*str) { 4732 while (*str && isspace(*str)) 4733 str++; 4734 if (*str == '\0') 4735 break; 4736 4737 if ((next = find_right_paren(str + 1)) == NULL) 4738 return (-1); 4739 save = *++next; 4740 4741 /* now we have "(filter)" with str pointing to it */ 4742 *next = '\0'; 4743 if (adj_filter(str) == -1) 4744 return (-1); 4745 next = resync_str(str, next, save); 4746 4747 str = next; 4748 } 4749 4750 return (0); 4751 } 4752 4753 4754 /* 4755 * is_valid_attr - returns 1 if a is a syntactically valid left-hand side 4756 * of a filter expression, 0 otherwise. A valid string may contain only 4757 * letters, numbers, hyphens, semi-colons, colons and periods. examples: 4758 * cn 4759 * cn;lang-fr 4760 * 1.2.3.4;binary;dynamic 4761 * mail;dynamic 4762 * cn:dn:1.2.3.4 4763 * 4764 * For compatibility with older servers, we also allow underscores in 4765 * attribute types, even through they are not allowed by the LDAPv3 RFCs. 4766 */ 4767 static int 4768 is_valid_attr(char *a) 4769 { 4770 for (; *a; a++) { 4771 if (!isascii(*a)) { 4772 return (0); 4773 } else if (!isalnum(*a)) { 4774 switch (*a) { 4775 case '-': 4776 case '.': 4777 case ';': 4778 case ':': 4779 case '_': 4780 break; /* valid */ 4781 default: 4782 return (0); 4783 } 4784 } 4785 } 4786 return (1); 4787 } 4788 4789 static char * 4790 find_star(char *s) 4791 { 4792 for (; *s; ++s) { 4793 switch (*s) { 4794 case '*': 4795 return (s); 4796 case '\\': 4797 ++s; 4798 if (hexchar2int(s[0]) >= 0 && hexchar2int(s[1]) >= 0) 4799 ++s; 4800 default: 4801 break; 4802 } 4803 } 4804 return (NULL); 4805 } 4806 4807 static int 4808 adj_simple_filter(char *str) 4809 { 4810 char *s, *s2, *s3, filterop; 4811 char *value; 4812 int ftype = 0; 4813 int rc; 4814 4815 rc = -1; /* pessimistic */ 4816 4817 if ((str = strdup(str)) == NULL) { 4818 return (rc); 4819 } 4820 4821 if ((s = strchr(str, '=')) == NULL) { 4822 goto free_and_return; 4823 } 4824 value = s + 1; 4825 *s-- = '\0'; 4826 filterop = *s; 4827 if (filterop == '<' || filterop == '>' || filterop == '~' || 4828 filterop == ':') { 4829 *s = '\0'; 4830 } 4831 4832 if (! is_valid_attr(str)) { 4833 goto free_and_return; 4834 } 4835 4836 switch (filterop) { 4837 case '<': /* LDAP_FILTER_LE */ 4838 case '>': /* LDAP_FILTER_GE */ 4839 case '~': /* LDAP_FILTER_APPROX */ 4840 break; 4841 case ':': /* extended filter - v3 only */ 4842 /* 4843 * extended filter looks like this: 4844 * 4845 * [type][':dn'][':'oid]':='value 4846 * 4847 * where one of type or :oid is required. 4848 * 4849 */ 4850 s2 = s3 = NULL; 4851 if ((s2 = strrchr(str, ':')) == NULL) { 4852 goto free_and_return; 4853 } 4854 if (strcasecmp(s2, ":dn") == 0) { 4855 *s2 = '\0'; 4856 } else { 4857 *s2 = '\0'; 4858 if ((s3 = strrchr(str, ':')) != NULL) { 4859 if (strcasecmp(s3, ":dn") != 0) { 4860 goto free_and_return; 4861 } 4862 *s3 = '\0'; 4863 } 4864 } 4865 if (unescape_filterval(value) < 0) { 4866 goto free_and_return; 4867 } 4868 rc = 0; 4869 goto free_and_return; 4870 /* break; */ 4871 default: 4872 if (find_star(value) == NULL) { 4873 ftype = 0; /* LDAP_FILTER_EQUALITY */ 4874 } else if (strcmp(value, "*") == 0) { 4875 ftype = 1; /* LDAP_FILTER_PRESENT */ 4876 } else { 4877 rc = adj_substring_filter(value); 4878 goto free_and_return; 4879 } 4880 break; 4881 } 4882 4883 if (ftype != 0) { /* == LDAP_FILTER_PRESENT */ 4884 rc = 0; 4885 } else if (unescape_filterval(value) >= 0) { 4886 rc = 0; 4887 } 4888 if (rc != -1) { 4889 rc = 0; 4890 } 4891 4892 free_and_return: 4893 free(str); 4894 return (rc); 4895 } 4896 4897 4898 /* 4899 * Check in place both LDAPv2 (RFC-1960) and LDAPv3 (hexadecimal) escape 4900 * sequences within the null-terminated string 'val'. 4901 * 4902 * If 'val' contains invalid escape sequences we return -1. 4903 * Otherwise return 1 4904 */ 4905 static int 4906 unescape_filterval(char *val) 4907 { 4908 int escape, firstdigit; 4909 char *s; 4910 4911 firstdigit = 0; 4912 escape = 0; 4913 for (s = val; *s; s++) { 4914 if (escape) { 4915 /* 4916 * first try LDAPv3 escape (hexadecimal) sequence 4917 */ 4918 if (hexchar2int(*s) < 0) { 4919 if (firstdigit) { 4920 /* 4921 * LDAPv2 (RFC1960) escape sequence 4922 */ 4923 escape = 0; 4924 } else { 4925 return (-1); 4926 } 4927 } 4928 if (firstdigit) { 4929 firstdigit = 0; 4930 } else { 4931 escape = 0; 4932 } 4933 4934 } else if (*s != '\\') { 4935 escape = 0; 4936 4937 } else { 4938 escape = 1; 4939 firstdigit = 1; 4940 } 4941 } 4942 4943 return (1); 4944 } 4945 4946 4947 /* 4948 * convert character 'c' that represents a hexadecimal digit to an integer. 4949 * if 'c' is not a hexidecimal digit [0-9A-Fa-f], -1 is returned. 4950 * otherwise the converted value is returned. 4951 */ 4952 static int 4953 hexchar2int(char c) 4954 { 4955 if (c >= '0' && c <= '9') { 4956 return (c - '0'); 4957 } 4958 if (c >= 'A' && c <= 'F') { 4959 return (c - 'A' + 10); 4960 } 4961 if (c >= 'a' && c <= 'f') { 4962 return (c - 'a' + 10); 4963 } 4964 return (-1); 4965 } 4966 4967 static int 4968 adj_substring_filter(char *val) 4969 { 4970 char *nextstar; 4971 4972 for (; val != NULL; val = nextstar) { 4973 if ((nextstar = find_star(val)) != NULL) { 4974 *nextstar++ = '\0'; 4975 } 4976 4977 if (*val != '\0') { 4978 if (unescape_filterval(val) < 0) { 4979 return (-1); 4980 } 4981 } 4982 } 4983 4984 return (0); 4985 } 4986 4987 /* ***** End of modified libldap.so.5 filter parser ***** */ 4988 4989 4990 /* 4991 * Walk filter, remove redundant parentheses in-line 4992 * verify that the filter is reasonable 4993 */ 4994 static int 4995 validate_filter(ns_ldap_cookie_t *cookie) 4996 { 4997 char *filter = cookie->filter; 4998 int rc; 4999 5000 /* Parse filter looking for illegal values */ 5001 5002 rc = adj_filter(filter); 5003 if (rc != 0) { 5004 return (NS_LDAP_OP_FAILED); 5005 } 5006 5007 /* end of filter checking */ 5008 5009 return (NS_LDAP_SUCCESS); 5010 } 5011 5012 /* 5013 * Set the account management request control that needs to be sent to server. 5014 * This control is required to get the account management information of 5015 * a user to do local account checking. 5016 */ 5017 static int 5018 setup_acctmgmt_params(ns_ldap_cookie_t *cookie) 5019 { 5020 LDAPControl *req = NULL, **requestctrls; 5021 5022 req = (LDAPControl *)malloc(sizeof (LDAPControl)); 5023 5024 if (req == NULL) 5025 return (NS_LDAP_MEMORY); 5026 5027 /* fill in the fields of this new control */ 5028 req->ldctl_iscritical = 1; 5029 req->ldctl_oid = strdup(NS_LDAP_ACCOUNT_USABLE_CONTROL); 5030 if (req->ldctl_oid == NULL) { 5031 free(req); 5032 return (NS_LDAP_MEMORY); 5033 } 5034 req->ldctl_value.bv_len = 0; 5035 req->ldctl_value.bv_val = NULL; 5036 5037 requestctrls = (LDAPControl **)calloc(2, sizeof (LDAPControl *)); 5038 if (requestctrls == NULL) { 5039 ldap_control_free(req); 5040 return (NS_LDAP_MEMORY); 5041 } 5042 5043 requestctrls[0] = req; 5044 5045 cookie->p_serverctrls = requestctrls; 5046 5047 return (NS_LDAP_SUCCESS); 5048 } 5049 5050 /* 5051 * int get_new_acct_more_info(BerElement *ber, 5052 * AcctUsableResponse_t *acctResp) 5053 * 5054 * Decode the more_info data from an Account Management control response, 5055 * when the account is not usable and when code style is from recent LDAP 5056 * servers (see below comments for parse_acct_cont_resp_msg() to get more 5057 * details on coding styles and ASN1 description). 5058 * 5059 * Expected BER encoding: {tbtbtbtiti} 5060 * +t: tag is 0 5061 * +b: TRUE if inactive due to account inactivation 5062 * +t: tag is 1 5063 * +b: TRUE if password has been reset 5064 * +t: tag is 2 5065 * +b: TRUE if password is expired 5066 * +t: tag is 3 5067 * +i: contains num of remaining grace, 0 means no grace 5068 * +t: tag is 4 5069 * +i: contains num of seconds before auto-unlock. -1 means acct is locked 5070 * forever (i.e. until reset) 5071 * 5072 * Asumptions: 5073 * - ber is not null 5074 * - acctResp is not null and is initialized with default values for the 5075 * fields in its AcctUsableResp.more_info structure 5076 * - the ber stream is received in the correct order, per the ASN1 description. 5077 * We do not check this order and make the asumption that it is correct. 5078 * Note that the ber stream may not (and will not in most cases) contain 5079 * all fields. 5080 */ 5081 static int 5082 get_new_acct_more_info(BerElement *ber, AcctUsableResponse_t *acctResp) 5083 { 5084 int rc = NS_LDAP_SUCCESS; 5085 char errstr[MAXERROR]; 5086 ber_tag_t rTag = LBER_DEFAULT; 5087 ber_len_t rLen = 0; 5088 ber_int_t rValue; 5089 char *last; 5090 int berRC = 0; 5091 5092 /* 5093 * Look at what more_info BER element is/are left to be decoded. 5094 * look at each of them 1 by 1, without checking on their order 5095 * and possible multi values. 5096 */ 5097 for (rTag = ber_first_element(ber, &rLen, &last); 5098 rTag != LBER_END_OF_SEQORSET; 5099 rTag = ber_next_element(ber, &rLen, last)) { 5100 5101 berRC = 0; 5102 switch (rTag) { 5103 case 0 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 5104 /* inactive */ 5105 berRC = ber_scanf(ber, "b", &rValue); 5106 if (berRC != LBER_ERROR) { 5107 (acctResp->AcctUsableResp).more_info. 5108 inactive = (rValue != 0) ? 1 : 0; 5109 } 5110 break; 5111 5112 case 1 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 5113 /* reset */ 5114 berRC = ber_scanf(ber, "b", &rValue); 5115 if (berRC != LBER_ERROR) { 5116 (acctResp->AcctUsableResp).more_info.reset 5117 = (rValue != 0) ? 1 : 0; 5118 } 5119 break; 5120 5121 case 2 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 5122 /* expired */ 5123 berRC = ber_scanf(ber, "b", &rValue); 5124 if (berRC != LBER_ERROR) { 5125 (acctResp->AcctUsableResp).more_info.expired 5126 = (rValue != 0) ? 1 : 0; 5127 } 5128 break; 5129 5130 case 3 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 5131 /* remaining grace */ 5132 berRC = ber_scanf(ber, "i", &rValue); 5133 if (berRC != LBER_ERROR) { 5134 (acctResp->AcctUsableResp).more_info.rem_grace 5135 = rValue; 5136 } 5137 break; 5138 5139 case 4 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 5140 /* seconds before unlock */ 5141 berRC = ber_scanf(ber, "i", &rValue); 5142 if (berRC != LBER_ERROR) { 5143 (acctResp->AcctUsableResp).more_info. 5144 sec_b4_unlock = rValue; 5145 } 5146 break; 5147 5148 default : 5149 (void) sprintf(errstr, 5150 gettext("invalid reason tag 0x%x"), rTag); 5151 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5152 rc = NS_LDAP_INTERNAL; 5153 break; 5154 } 5155 if (berRC == LBER_ERROR) { 5156 (void) sprintf(errstr, 5157 gettext("error 0x%x decoding value for " 5158 "tag 0x%x"), berRC, rTag); 5159 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5160 rc = NS_LDAP_INTERNAL; 5161 } 5162 if (rc != NS_LDAP_SUCCESS) { 5163 /* exit the for loop */ 5164 break; 5165 } 5166 } 5167 5168 return (rc); 5169 } 5170 5171 /* 5172 * int get_old_acct_opt_more_info(BerElement *ber, 5173 * AcctUsableResponse_t *acctResp) 5174 * 5175 * Decode the optional more_info data from an Account Management control 5176 * response, when the account is not usable and when code style is from LDAP 5177 * server 5.2p4 (see below comments for parse_acct_cont_resp_msg() to get more 5178 * details on coding styles and ASN1 description). 5179 * 5180 * Expected BER encoding: titi} 5181 * +t: tag is 2 5182 * +i: contains num of remaining grace, 0 means no grace 5183 * +t: tag is 3 5184 * +i: contains num of seconds before auto-unlock. -1 means acct is locked 5185 * forever (i.e. until reset) 5186 * 5187 * Asumptions: 5188 * - ber is a valid BER element 5189 * - acctResp is initialized for the fields in its AcctUsableResp.more_info 5190 * structure 5191 */ 5192 static int 5193 get_old_acct_opt_more_info(ber_tag_t tag, BerElement *ber, 5194 AcctUsableResponse_t *acctResp) 5195 { 5196 int rc = NS_LDAP_SUCCESS; 5197 char errstr[MAXERROR]; 5198 ber_len_t len; 5199 int rem_grace, sec_b4_unlock; 5200 5201 switch (tag) { 5202 case 2: 5203 /* decode and maybe 3 is following */ 5204 if ((tag = ber_scanf(ber, "i", &rem_grace)) == LBER_ERROR) { 5205 (void) sprintf(errstr, gettext("Can not get " 5206 "rem_grace")); 5207 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5208 rc = NS_LDAP_INTERNAL; 5209 break; 5210 } 5211 (acctResp->AcctUsableResp).more_info.rem_grace = rem_grace; 5212 5213 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 5214 /* this is a success case, break to exit */ 5215 (void) sprintf(errstr, gettext("No more " 5216 "optional data")); 5217 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5218 break; 5219 } 5220 5221 if (tag == 3) { 5222 if (ber_scanf(ber, "i", &sec_b4_unlock) == LBER_ERROR) { 5223 (void) sprintf(errstr, 5224 gettext("Can not get sec_b4_unlock " 5225 "- 1st case")); 5226 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5227 rc = NS_LDAP_INTERNAL; 5228 break; 5229 } 5230 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 5231 sec_b4_unlock; 5232 } else { /* unknown tag */ 5233 (void) sprintf(errstr, gettext("Unknown tag " 5234 "- 1st case")); 5235 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5236 rc = NS_LDAP_INTERNAL; 5237 break; 5238 } 5239 break; 5240 5241 case 3: 5242 if (ber_scanf(ber, "i", &sec_b4_unlock) == LBER_ERROR) { 5243 (void) sprintf(errstr, gettext("Can not get " 5244 "sec_b4_unlock - 2nd case")); 5245 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5246 rc = NS_LDAP_INTERNAL; 5247 break; 5248 } 5249 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 5250 sec_b4_unlock; 5251 break; 5252 5253 default: /* unknown tag */ 5254 (void) sprintf(errstr, gettext("Unknown tag - 2nd case")); 5255 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5256 rc = NS_LDAP_INTERNAL; 5257 break; 5258 } 5259 5260 return (rc); 5261 } 5262 5263 /* 5264 * **** This function needs to be moved to libldap library **** 5265 * parse_acct_cont_resp_msg() parses the message received by server according to 5266 * following format (ASN1 notation): 5267 * 5268 * ACCOUNT_USABLE_RESPONSE::= CHOICE { 5269 * is_available [0] INTEGER, 5270 * ** seconds before expiration ** 5271 * is_not_available [1] more_info 5272 * } 5273 * more_info::= SEQUENCE { 5274 * inactive [0] BOOLEAN DEFAULT FALSE, 5275 * reset [1] BOOLEAN DEFAULT FALSE, 5276 * expired [2] BOOLEAN DEFAULT FALSE, 5277 * remaining_grace [3] INTEGER OPTIONAL, 5278 * seconds_before_unlock [4] INTEGER OPTIONAL 5279 * } 5280 */ 5281 /* 5282 * #define used to make the difference between coding style as done 5283 * by LDAP server 5.2p4 and newer LDAP servers. There are 4 values: 5284 * - DS52p4_USABLE: 5.2p4 coding style, account is usable 5285 * - DS52p4_NOT_USABLE: 5.2p4 coding style, account is not usable 5286 * - NEW_USABLE: newer LDAP servers coding style, account is usable 5287 * - NEW_NOT_USABLE: newer LDAP servers coding style, account is not usable 5288 * 5289 * An account would be considered not usable if for instance: 5290 * - it's been made inactive in the LDAP server 5291 * - or its password was reset in the LDAP server database 5292 * - or its password expired 5293 * - or the account has been locked, possibly forever 5294 */ 5295 #define DS52p4_USABLE 0x00 5296 #define DS52p4_NOT_USABLE 0x01 5297 #define NEW_USABLE 0x00 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE 5298 #define NEW_NOT_USABLE 0x01 | LBER_CLASS_CONTEXT | LBER_CONSTRUCTED 5299 static int 5300 parse_acct_cont_resp_msg(LDAPControl **ectrls, AcctUsableResponse_t *acctResp) 5301 { 5302 int rc = NS_LDAP_SUCCESS; 5303 BerElement *ber; 5304 ber_tag_t tag; 5305 ber_len_t len; 5306 int i; 5307 char errstr[MAXERROR]; 5308 /* used for any coding style when account is usable */ 5309 int seconds_before_expiry; 5310 /* used for 5.2p4 coding style when account is not usable */ 5311 int inactive, reset, expired; 5312 5313 if (ectrls == NULL) { 5314 (void) sprintf(errstr, gettext("Invalid ectrls parameter")); 5315 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5316 return (NS_LDAP_INVALID_PARAM); 5317 } 5318 5319 for (i = 0; ectrls[i] != NULL; i++) { 5320 if (strcmp(ectrls[i]->ldctl_oid, NS_LDAP_ACCOUNT_USABLE_CONTROL) 5321 == 0) { 5322 break; 5323 } 5324 } 5325 5326 if (ectrls[i] == NULL) { 5327 /* Ldap control is not found */ 5328 (void) sprintf(errstr, gettext("Account Usable Control " 5329 "not found")); 5330 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5331 return (NS_LDAP_NOTFOUND); 5332 } 5333 5334 /* Allocate a BER element from the control value and parse it. */ 5335 if ((ber = ber_init(&ectrls[i]->ldctl_value)) == NULL) 5336 return (NS_LDAP_MEMORY); 5337 5338 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 5339 /* Ldap decoding error */ 5340 (void) sprintf(errstr, gettext("Error decoding 1st tag")); 5341 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5342 ber_free(ber, 1); 5343 return (NS_LDAP_INTERNAL); 5344 } 5345 5346 switch (tag) { 5347 case DS52p4_USABLE: 5348 case NEW_USABLE: 5349 acctResp->choice = 0; 5350 if (ber_scanf(ber, "i", &seconds_before_expiry) 5351 == LBER_ERROR) { 5352 /* Ldap decoding error */ 5353 (void) sprintf(errstr, gettext("Can not get " 5354 "seconds_before_expiry")); 5355 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5356 rc = NS_LDAP_INTERNAL; 5357 break; 5358 } 5359 /* ber_scanf() succeeded */ 5360 (acctResp->AcctUsableResp).seconds_before_expiry = 5361 seconds_before_expiry; 5362 break; 5363 5364 case DS52p4_NOT_USABLE: 5365 acctResp->choice = 1; 5366 if (ber_scanf(ber, "{bbb", &inactive, &reset, &expired) 5367 == LBER_ERROR) { 5368 /* Ldap decoding error */ 5369 (void) sprintf(errstr, gettext("Can not get " 5370 "inactive/reset/expired")); 5371 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5372 rc = NS_LDAP_INTERNAL; 5373 break; 5374 } 5375 /* ber_scanf() succeeded */ 5376 (acctResp->AcctUsableResp).more_info.inactive = 5377 ((inactive == 0) ? 0 : 1); 5378 (acctResp->AcctUsableResp).more_info.reset = 5379 ((reset == 0) ? 0 : 1); 5380 (acctResp->AcctUsableResp).more_info.expired = 5381 ((expired == 0) ? 0 : 1); 5382 (acctResp->AcctUsableResp).more_info.rem_grace = 0; 5383 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 0; 5384 5385 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 5386 /* this is a success case, break to exit */ 5387 (void) sprintf(errstr, gettext("No optional data")); 5388 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5389 break; 5390 } 5391 5392 /* 5393 * Look at what optional more_info BER element is/are 5394 * left to be decoded. 5395 */ 5396 rc = get_old_acct_opt_more_info(tag, ber, acctResp); 5397 break; 5398 5399 case NEW_NOT_USABLE: 5400 acctResp->choice = 1; 5401 /* 5402 * Recent LDAP servers won't code more_info data for default 5403 * values (see above comments on ASN1 description for what 5404 * fields have default values & what fields are optional). 5405 */ 5406 (acctResp->AcctUsableResp).more_info.inactive = 0; 5407 (acctResp->AcctUsableResp).more_info.reset = 0; 5408 (acctResp->AcctUsableResp).more_info.expired = 0; 5409 (acctResp->AcctUsableResp).more_info.rem_grace = 0; 5410 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 0; 5411 5412 if (len == 0) { 5413 /* 5414 * Nothing else to decode; this is valid and we 5415 * use default values set above. 5416 */ 5417 (void) sprintf(errstr, gettext("more_info is " 5418 "empty, using default values")); 5419 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5420 break; 5421 } 5422 5423 /* 5424 * Look at what more_info BER element is/are left to 5425 * be decoded. 5426 */ 5427 rc = get_new_acct_more_info(ber, acctResp); 5428 break; 5429 5430 default: 5431 (void) sprintf(errstr, gettext("unknwon coding style " 5432 "(tag: 0x%x)"), tag); 5433 syslog(LOG_DEBUG, "libsldap: %s", errstr); 5434 rc = NS_LDAP_INTERNAL; 5435 break; 5436 } 5437 5438 ber_free(ber, 1); 5439 return (rc); 5440 } 5441 5442 /* 5443 * internal function for __ns_ldap_getAcctMgmt() 5444 */ 5445 static int 5446 getAcctMgmt(const char *user, AcctUsableResponse_t *acctResp, 5447 ns_conn_user_t *conn_user) 5448 { 5449 int scope, rc; 5450 char ldapfilter[1024]; 5451 ns_ldap_cookie_t *cookie; 5452 ns_ldap_search_desc_t **sdlist = NULL; 5453 ns_ldap_search_desc_t *dptr; 5454 ns_ldap_error_t *error = NULL; 5455 char **dns = NULL; 5456 char service[] = "shadow"; 5457 5458 if (user == NULL || acctResp == NULL) 5459 return (NS_LDAP_INVALID_PARAM); 5460 5461 /* Initialize State machine cookie */ 5462 cookie = init_search_state_machine(); 5463 if (cookie == NULL) 5464 return (NS_LDAP_MEMORY); 5465 cookie->conn_user = conn_user; 5466 5467 /* see if need to follow referrals */ 5468 rc = __s_api_toFollowReferrals(0, 5469 &cookie->followRef, &error); 5470 if (rc != NS_LDAP_SUCCESS) { 5471 (void) __ns_ldap_freeError(&error); 5472 goto out; 5473 } 5474 5475 /* get the service descriptor - or create a default one */ 5476 rc = __s_api_get_SSD_from_SSDtoUse_service(service, 5477 &sdlist, &error); 5478 if (rc != NS_LDAP_SUCCESS) { 5479 (void) __ns_ldap_freeError(&error); 5480 goto out; 5481 } 5482 5483 if (sdlist == NULL) { 5484 /* Create default service Desc */ 5485 sdlist = (ns_ldap_search_desc_t **)calloc(2, 5486 sizeof (ns_ldap_search_desc_t *)); 5487 if (sdlist == NULL) { 5488 rc = NS_LDAP_MEMORY; 5489 goto out; 5490 } 5491 dptr = (ns_ldap_search_desc_t *) 5492 calloc(1, sizeof (ns_ldap_search_desc_t)); 5493 if (dptr == NULL) { 5494 free(sdlist); 5495 rc = NS_LDAP_MEMORY; 5496 goto out; 5497 } 5498 sdlist[0] = dptr; 5499 5500 /* default base */ 5501 rc = __s_api_getDNs(&dns, service, &cookie->errorp); 5502 if (rc != NS_LDAP_SUCCESS) { 5503 if (dns) { 5504 __s_api_free2dArray(dns); 5505 dns = NULL; 5506 } 5507 (void) __ns_ldap_freeError(&(cookie->errorp)); 5508 cookie->errorp = NULL; 5509 goto out; 5510 } 5511 dptr->basedn = strdup(dns[0]); 5512 if (dptr->basedn == NULL) { 5513 free(sdlist); 5514 free(dptr); 5515 if (dns) { 5516 __s_api_free2dArray(dns); 5517 dns = NULL; 5518 } 5519 rc = NS_LDAP_MEMORY; 5520 goto out; 5521 } 5522 __s_api_free2dArray(dns); 5523 dns = NULL; 5524 5525 /* default scope */ 5526 scope = 0; 5527 rc = __s_api_getSearchScope(&scope, &cookie->errorp); 5528 dptr->scope = scope; 5529 } 5530 5531 cookie->sdlist = sdlist; 5532 5533 cookie->service = strdup(service); 5534 if (cookie->service == NULL) { 5535 rc = NS_LDAP_MEMORY; 5536 goto out; 5537 } 5538 5539 /* search for entries for this particular uid */ 5540 (void) snprintf(ldapfilter, sizeof (ldapfilter), "(uid=%s)", user); 5541 cookie->i_filter = strdup(ldapfilter); 5542 if (cookie->i_filter == NULL) { 5543 rc = NS_LDAP_MEMORY; 5544 goto out; 5545 } 5546 5547 /* create the control request */ 5548 if ((rc = setup_acctmgmt_params(cookie)) != NS_LDAP_SUCCESS) 5549 goto out; 5550 5551 /* Process search */ 5552 rc = search_state_machine(cookie, GET_ACCT_MGMT_INFO, 0); 5553 5554 /* Copy results back to user */ 5555 rc = cookie->err_rc; 5556 if (rc != NS_LDAP_SUCCESS) 5557 (void) __ns_ldap_freeError(&(cookie->errorp)); 5558 5559 if (cookie->result == NULL) 5560 goto out; 5561 5562 if ((rc = parse_acct_cont_resp_msg(cookie->resultctrl, acctResp)) 5563 != NS_LDAP_SUCCESS) 5564 goto out; 5565 5566 rc = NS_LDAP_SUCCESS; 5567 5568 out: 5569 delete_search_cookie(cookie); 5570 5571 return (rc); 5572 } 5573 5574 /* 5575 * __ns_ldap_getAcctMgmt() is called from pam account management stack 5576 * for retrieving accounting information of users with no user password - 5577 * eg. rlogin, rsh, etc. This function uses the account management control 5578 * request to do a search on the server for the user in question. The 5579 * response control returned from the server is got from the cookie. 5580 * Input params: username of whose account mgmt information is to be got 5581 * pointer to hold the parsed account management information 5582 * Return values: NS_LDAP_SUCCESS on success or appropriate error 5583 * code on failure 5584 */ 5585 int 5586 __ns_ldap_getAcctMgmt(const char *user, AcctUsableResponse_t *acctResp) 5587 { 5588 ns_conn_user_t *cu = NULL; 5589 int try_cnt = 0; 5590 int rc = NS_LDAP_SUCCESS; 5591 ns_ldap_error_t *error = NULL; 5592 5593 for (;;) { 5594 if (__s_api_setup_retry_search(&cu, NS_CONN_USER_SEARCH, 5595 &try_cnt, &rc, &error) == 0) 5596 break; 5597 rc = getAcctMgmt(user, acctResp, cu); 5598 } 5599 return (rc); 5600 } 5601