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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <stdio.h> 29 #include <sys/types.h> 30 #include <stdlib.h> 31 #include <libintl.h> 32 #include <ctype.h> 33 #include <syslog.h> 34 #include <sys/stat.h> 35 #include <fcntl.h> 36 #include <unistd.h> 37 #include <string.h> 38 #include <strings.h> 39 40 #include "ns_sldap.h" 41 #include "ns_internal.h" 42 #include "ns_cache_door.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 if (from == NULL || from[0] == '\0' || to == NULL) 866 return (-1); 867 868 *to = NULL; 869 (void) memset(space.s_b, 0, DOORBUFFERSIZE); 870 871 space.s_d.ldap_call.ldap_callnumber = GETCACHE; 872 (void) snprintf(space.s_d.ldap_call.ldap_u.domainname, 873 DOORBUFFERSIZE - sizeof (space.s_d.ldap_call.ldap_callnumber), 874 "%s%s%s", 875 type, 876 DOORLINESEP, 877 from); 878 ndata = sizeof (space); 879 adata = sizeof (ldap_call_t) + 880 strlen(space.s_d.ldap_call.ldap_u.domainname) + 1; 881 sptr = &space.s_d; 882 883 rc = __ns_ldap_trydoorcall(&sptr, &ndata, &adata); 884 if (rc != SUCCESS) 885 return (-1); 886 else 887 *to = strdup(sptr->ldap_ret.ldap_u.buff); 888 return (NS_LDAP_SUCCESS); 889 } 890 891 static int 892 __s_api_set_cachemgr_data(const char *type, 893 const char *from, const char *to) 894 { 895 union { 896 ldap_data_t s_d; 897 char s_b[DOORBUFFERSIZE]; 898 } space; 899 ldap_data_t *sptr; 900 int ndata; 901 int adata; 902 int rc; 903 904 #ifdef DEBUG 905 (void) fprintf(stderr, "__s_api_set_cachemgr_data START\n"); 906 #endif 907 908 if ((from == NULL) || (from[0] == '\0') || 909 (to == NULL) || (to[0] == '\0')) 910 return (-1); 911 912 (void) memset(space.s_b, 0, DOORBUFFERSIZE); 913 914 space.s_d.ldap_call.ldap_callnumber = SETCACHE; 915 (void) snprintf(space.s_d.ldap_call.ldap_u.domainname, 916 DOORBUFFERSIZE - sizeof (space.s_d.ldap_call.ldap_callnumber), 917 "%s%s%s%s%s", 918 type, 919 DOORLINESEP, 920 from, 921 DOORLINESEP, 922 to); 923 924 ndata = sizeof (space); 925 adata = sizeof (ldap_call_t) + 926 strlen(space.s_d.ldap_call.ldap_u.domainname) + 1; 927 sptr = &space.s_d; 928 929 rc = __ns_ldap_trydoorcall(&sptr, &ndata, &adata); 930 if (rc != SUCCESS) 931 return (-1); 932 933 return (NS_LDAP_SUCCESS); 934 } 935 936 937 static char * 938 __s_api_remove_rdn_space(char *rdn) 939 { 940 char *tf, *tl, *vf, *vl, *eqsign; 941 942 /* if no space(s) to remove, return */ 943 if (strchr(rdn, SPACETOK) == NULL) 944 return (rdn); 945 946 /* if no '=' separator, return */ 947 eqsign = strchr(rdn, '='); 948 if (eqsign == NULL) 949 return (rdn); 950 951 tf = rdn; 952 tl = eqsign - 1; 953 vf = eqsign + 1; 954 vl = rdn + strlen(rdn) - 1; 955 956 /* now two strings, type and value */ 957 *eqsign = '\0'; 958 959 /* remove type's leading spaces */ 960 while (tf < tl && *tf == SPACETOK) 961 tf++; 962 /* remove type's trailing spaces */ 963 while (tf < tl && *tl == SPACETOK) 964 tl--; 965 /* add '=' separator back */ 966 *(++tl) = '='; 967 /* remove value's leading spaces */ 968 while (vf < vl && *vf == SPACETOK) 969 vf++; 970 /* remove value's trailing spaces */ 971 while (vf < vl && *vl == SPACETOK) 972 *vl-- = '\0'; 973 974 /* move value up if necessary */ 975 if (vf != tl + 1) 976 (void) strcpy(tl + 1, vf); 977 978 return (tf); 979 } 980 981 static 982 ns_ldap_cookie_t * 983 init_search_state_machine() 984 { 985 ns_ldap_cookie_t *cookie; 986 ns_config_t *cfg; 987 988 cookie = (ns_ldap_cookie_t *)calloc(1, sizeof (ns_ldap_cookie_t)); 989 if (cookie == NULL) 990 return (NULL); 991 cookie->state = INIT; 992 /* assign other state variables */ 993 cfg = __s_api_loadrefresh_config(); 994 cookie->connectionId = -1; 995 if (cfg == NULL || 996 cfg->paramList[NS_LDAP_SEARCH_TIME_P].ns_ptype == NS_UNKNOWN) { 997 cookie->search_timeout.tv_sec = NS_DEFAULT_SEARCH_TIMEOUT; 998 } else { 999 cookie->search_timeout.tv_sec = 1000 cfg->paramList[NS_LDAP_SEARCH_TIME_P].ns_i; 1001 } 1002 if (cfg != NULL) 1003 __s_api_release_config(cfg); 1004 cookie->search_timeout.tv_usec = 0; 1005 1006 return (cookie); 1007 } 1008 1009 static void 1010 delete_search_cookie(ns_ldap_cookie_t *cookie) 1011 { 1012 if (cookie == NULL) 1013 return; 1014 if (cookie->connectionId > -1) 1015 DropConnection(cookie->connectionId, cookie->i_flags); 1016 if (cookie->filter) 1017 free(cookie->filter); 1018 if (cookie->i_filter) 1019 free(cookie->i_filter); 1020 if (cookie->service) 1021 free(cookie->service); 1022 if (cookie->sdlist) 1023 (void) __ns_ldap_freeSearchDescriptors(&(cookie->sdlist)); 1024 if (cookie->result) 1025 (void) __ns_ldap_freeResult(&cookie->result); 1026 if (cookie->attribute) 1027 __s_api_free2dArray(cookie->attribute); 1028 if (cookie->errorp) 1029 (void) __ns_ldap_freeError(&cookie->errorp); 1030 if (cookie->reflist) 1031 __s_api_deleteRefInfo(cookie->reflist); 1032 if (cookie->basedn) 1033 free(cookie->basedn); 1034 if (cookie->ctrlCookie) 1035 ber_bvfree(cookie->ctrlCookie); 1036 _freeControlList(&cookie->p_serverctrls); 1037 if (cookie->resultctrl) 1038 ldap_controls_free(cookie->resultctrl); 1039 free(cookie); 1040 } 1041 1042 static int 1043 get_mapped_filter(ns_ldap_cookie_t *cookie, char **new_filter) 1044 { 1045 1046 typedef struct filter_mapping_info { 1047 char oc_or_attr; 1048 char *name_start; 1049 char *name_end; 1050 char *veq_pos; 1051 char *from_name; 1052 char *to_name; 1053 char **mapping; 1054 } filter_mapping_info_t; 1055 1056 char *c, *last_copied; 1057 char *filter_c, *filter_c_next; 1058 char *key, *tail, *head; 1059 char errstr[MAXERROR]; 1060 int num_eq = 0, num_veq = 0; 1061 int in_quote = FALSE; 1062 int is_value = FALSE; 1063 int i, j, oc_len, len; 1064 int at_least_one = FALSE; 1065 filter_mapping_info_t **info, *info1; 1066 char **mapping; 1067 char *service, *filter, *err; 1068 int auto_service = FALSE; 1069 1070 if (cookie == NULL || new_filter == NULL) 1071 return (NS_LDAP_INVALID_PARAM); 1072 1073 *new_filter = NULL; 1074 service = cookie->service; 1075 filter = cookie->filter; 1076 1077 /* 1078 * count the number of '=' char 1079 */ 1080 for (c = filter; *c; c++) { 1081 if (*c == TOKENSEPARATOR) 1082 num_eq++; 1083 } 1084 1085 if (service != NULL && strncasecmp(service, "auto_", 5) == 0) 1086 auto_service = TRUE; 1087 1088 /* 1089 * See if schema mapping existed for the given service. 1090 * If not, just return success. 1091 */ 1092 mapping = __ns_ldap_getOrigAttribute(service, 1093 NS_HASH_SCHEMA_MAPPING_EXISTED); 1094 1095 if (mapping == NULL && auto_service) 1096 /* 1097 * if service == auto_* and no 1098 * schema mapping found 1099 * then try automount 1100 */ 1101 mapping = __ns_ldap_getOrigAttribute( 1102 "automount", NS_HASH_SCHEMA_MAPPING_EXISTED); 1103 1104 if (mapping) 1105 __s_api_free2dArray(mapping); 1106 else 1107 return (NS_LDAP_SUCCESS); 1108 1109 /* 1110 * no '=' sign, just say OK and return nothing 1111 */ 1112 if (num_eq == 0) 1113 return (NS_LDAP_SUCCESS); 1114 1115 /* 1116 * Make a copy of the filter string 1117 * for saving the name of the objectclasses or 1118 * attributes that need to be passed to the 1119 * objectclass or attribute mapping functions. 1120 * pointer "info->from_name" points to the locations 1121 * within this string. 1122 * 1123 * The input filter string, filter, will be used 1124 * to indicate where these names start and end. 1125 * pointers "info->name_start" and "info->name_end" 1126 * point to locations within the input filter string, 1127 * and are used at the end of this function to 1128 * merge the original filter data with the 1129 * mapped objectclass or attribute names. 1130 */ 1131 filter_c = strdup(filter); 1132 if (filter_c == NULL) 1133 return (NS_LDAP_MEMORY); 1134 filter_c_next = filter_c; 1135 1136 /* 1137 * get memory for info arrays 1138 */ 1139 info = (filter_mapping_info_t **)calloc(num_eq + 1, 1140 sizeof (filter_mapping_info_t *)); 1141 1142 if (info == NULL) { 1143 free(filter_c); 1144 return (NS_LDAP_MEMORY); 1145 } 1146 1147 /* 1148 * find valid '=' for further processing, 1149 * ignore the "escaped =" (.i.e. "\="), or 1150 * "=" in quoted string 1151 */ 1152 for (c = filter_c; *c; c++) { 1153 1154 switch (*c) { 1155 case TOKENSEPARATOR: 1156 if (!in_quote && !is_value) { 1157 info1 = (filter_mapping_info_t *)calloc(1, 1158 sizeof (filter_mapping_info_t)); 1159 if (!info1) { 1160 free(filter_c); 1161 for (i = 0; i < num_veq; i++) 1162 free(info[i]); 1163 free(info); 1164 return (NS_LDAP_MEMORY); 1165 } 1166 info[num_veq] = info1; 1167 1168 /* 1169 * remember the location of this "=" 1170 */ 1171 info[num_veq++]->veq_pos = c; 1172 1173 /* 1174 * skip until the end of the attribute value 1175 */ 1176 is_value = TRUE; 1177 } 1178 break; 1179 case CPARATOK: 1180 /* 1181 * mark the end of the attribute value 1182 */ 1183 if (!in_quote) 1184 is_value = FALSE; 1185 break; 1186 case QUOTETOK: 1187 /* 1188 * switch on/off the in_quote mode 1189 */ 1190 in_quote = (in_quote == FALSE); 1191 break; 1192 case '\\': 1193 /* 1194 * ignore escape characters 1195 * don't skip if next char is '\0' 1196 */ 1197 if (!in_quote) 1198 if (*(++c) == '\0') 1199 c--; 1200 break; 1201 } 1202 1203 } 1204 1205 /* 1206 * for each valid "=" found, get the name to 1207 * be mapped 1208 */ 1209 oc_len = strlen("objectclass"); 1210 for (i = 0; i < num_veq; i++) { 1211 1212 /* 1213 * look at the left side of "=" to see 1214 * if assertion is "objectclass=<ocname>" 1215 * or "<attribute name>=<attribute value>" 1216 * 1217 * first skip spaces before "=". 1218 * Note that filter_c_next may not point to the 1219 * start of the filter string. For i > 0, 1220 * it points to the end of the last name processed + 2 1221 */ 1222 for (tail = info[i]->veq_pos; (tail > filter_c_next) && 1223 (*(tail - 1) == SPACETOK); tail--) 1224 ; 1225 1226 /* 1227 * mark the end of the left side string (the key) 1228 */ 1229 *tail = '\0'; 1230 info[i]->name_end = tail - filter_c - 1 + filter; 1231 1232 /* 1233 * find the start of the key 1234 */ 1235 key = filter_c_next; 1236 for (c = tail; filter_c_next <= c; c--) { 1237 /* OPARATOK is '(' */ 1238 if (*c == OPARATOK || 1239 *c == SPACETOK) { 1240 key = c + 1; 1241 break; 1242 } 1243 } 1244 info[i]->name_start = key - filter_c + filter; 1245 1246 if ((key + oc_len) <= tail) { 1247 if (strncasecmp(key, "objectclass", 1248 oc_len) == 0) { 1249 /* 1250 * assertion is "objectclass=ocname", 1251 * ocname is the one needs to be mapped 1252 * 1253 * skip spaces after "=" to find start 1254 * of the ocname 1255 */ 1256 head = info[i]->veq_pos; 1257 for (head = info[i]->veq_pos + 1; 1258 *head && *head == SPACETOK; head++) 1259 ; 1260 1261 /* ignore empty ocname */ 1262 if (!(*head)) 1263 continue; 1264 1265 info[i]->name_start = head - filter_c + 1266 filter; 1267 1268 /* 1269 * now find the end of the ocname 1270 */ 1271 for (c = head; ; c++) { 1272 /* CPARATOK is ')' */ 1273 if (*c == CPARATOK || 1274 *c == '\0' || 1275 *c == SPACETOK) { 1276 *c = '\0'; 1277 info[i]->name_end = 1278 c - filter_c - 1 + 1279 filter; 1280 filter_c_next = c + 1; 1281 info[i]->oc_or_attr = 'o'; 1282 info[i]->from_name = head; 1283 break; 1284 } 1285 } 1286 } 1287 } 1288 1289 /* 1290 * assertion is not "objectclass=ocname", 1291 * assume assertion is "<key> = <value>", 1292 * <key> is the one needs to be mapped 1293 */ 1294 if (info[i]->from_name == NULL && strlen(key) > 0) { 1295 info[i]->oc_or_attr = 'a'; 1296 info[i]->from_name = key; 1297 } 1298 } 1299 1300 /* perform schema mapping */ 1301 for (i = 0; i < num_veq; i++) { 1302 if (info[i]->from_name == NULL) 1303 continue; 1304 1305 if (info[i]->oc_or_attr == 'a') 1306 info[i]->mapping = 1307 __ns_ldap_getMappedAttributes(service, 1308 info[i]->from_name); 1309 else 1310 info[i]->mapping = 1311 __ns_ldap_getMappedObjectClass(service, 1312 info[i]->from_name); 1313 1314 if (info[i]->mapping == NULL && auto_service) { 1315 /* 1316 * If no mapped attribute/objectclass is found 1317 * and service == auto* 1318 * try to find automount's 1319 * mapped attribute/objectclass 1320 */ 1321 if (info[i]->oc_or_attr == 'a') 1322 info[i]->mapping = 1323 __ns_ldap_getMappedAttributes("automount", 1324 info[i]->from_name); 1325 else 1326 info[i]->mapping = 1327 __ns_ldap_getMappedObjectClass("automount", 1328 info[i]->from_name); 1329 } 1330 1331 if (info[i]->mapping == NULL || 1332 info[i]->mapping[0] == NULL) { 1333 info[i]->to_name = NULL; 1334 } else if (info[i]->mapping[1] == NULL) { 1335 info[i]->to_name = info[i]->mapping[0]; 1336 at_least_one = TRUE; 1337 } else { 1338 __s_api_free2dArray(info[i]->mapping); 1339 /* 1340 * multiple mapping 1341 * not allowed 1342 */ 1343 (void) sprintf(errstr, 1344 gettext( 1345 "Multiple attribute or objectclass " 1346 "mapping for '%s' in filter " 1347 "'%s' not allowed."), 1348 info[i]->from_name, filter); 1349 err = strdup(errstr); 1350 if (err) 1351 MKERROR(LOG_WARNING, cookie->errorp, 1352 NS_CONFIG_SYNTAX, 1353 err, NULL); 1354 1355 free(filter_c); 1356 for (j = 0; j < num_veq; j++) { 1357 if (info[j]->mapping) 1358 __s_api_free2dArray( 1359 info[j]->mapping); 1360 free(info[j]); 1361 } 1362 free(info); 1363 return (NS_LDAP_CONFIG); 1364 } 1365 } 1366 1367 1368 if (at_least_one) { 1369 1370 len = strlen(filter); 1371 last_copied = filter - 1; 1372 1373 for (i = 0; i < num_veq; i++) { 1374 if (info[i]->to_name) 1375 len += strlen(info[i]->to_name); 1376 } 1377 1378 *new_filter = (char *)calloc(1, len); 1379 if (*new_filter == NULL) { 1380 free(filter_c); 1381 for (j = 0; j < num_veq; j++) { 1382 if (info[j]->mapping) 1383 __s_api_free2dArray( 1384 info[j]->mapping); 1385 free(info[j]); 1386 } 1387 free(info); 1388 return (NS_LDAP_MEMORY); 1389 } 1390 1391 for (i = 0; i < num_veq; i++) { 1392 if (info[i]->to_name != NULL && 1393 info[i]->to_name != NULL) { 1394 1395 /* 1396 * copy the original filter data 1397 * between the last name and current 1398 * name 1399 */ 1400 if ((last_copied + 1) != info[i]->name_start) 1401 (void) strncat(*new_filter, 1402 last_copied + 1, 1403 info[i]->name_start - 1404 last_copied - 1); 1405 1406 /* the data is copied */ 1407 last_copied = info[i]->name_end; 1408 1409 /* 1410 * replace the name with 1411 * the mapped name 1412 */ 1413 (void) strcat(*new_filter, info[i]->to_name); 1414 } 1415 1416 /* copy the filter data after the last name */ 1417 if (i == (num_veq -1) && 1418 info[i]->name_end < 1419 (filter + strlen(filter))) 1420 (void) strncat(*new_filter, last_copied + 1, 1421 filter + strlen(filter) - 1422 last_copied - 1); 1423 } 1424 1425 } 1426 1427 /* free memory */ 1428 free(filter_c); 1429 for (j = 0; j < num_veq; j++) { 1430 if (info[j]->mapping) 1431 __s_api_free2dArray(info[j]->mapping); 1432 free(info[j]); 1433 } 1434 free(info); 1435 1436 return (NS_LDAP_SUCCESS); 1437 } 1438 1439 static int 1440 setup_next_search(ns_ldap_cookie_t *cookie) 1441 { 1442 ns_ldap_search_desc_t *dptr; 1443 int scope; 1444 char *filter, *str; 1445 int baselen; 1446 int rc; 1447 void **param; 1448 1449 dptr = *cookie->sdpos; 1450 scope = cookie->i_flags & (NS_LDAP_SCOPE_BASE | 1451 NS_LDAP_SCOPE_ONELEVEL | 1452 NS_LDAP_SCOPE_SUBTREE); 1453 if (scope) 1454 cookie->scope = scope; 1455 else 1456 cookie->scope = dptr->scope; 1457 switch (cookie->scope) { 1458 case NS_LDAP_SCOPE_BASE: 1459 cookie->scope = LDAP_SCOPE_BASE; 1460 break; 1461 case NS_LDAP_SCOPE_ONELEVEL: 1462 cookie->scope = LDAP_SCOPE_ONELEVEL; 1463 break; 1464 case NS_LDAP_SCOPE_SUBTREE: 1465 cookie->scope = LDAP_SCOPE_SUBTREE; 1466 break; 1467 } 1468 1469 filter = NULL; 1470 if (cookie->use_filtercb && cookie->init_filter_cb && 1471 dptr->filter && strlen(dptr->filter) > 0) { 1472 (*cookie->init_filter_cb)(dptr, &filter, 1473 cookie->userdata); 1474 } 1475 if (filter == NULL) { 1476 if (cookie->i_filter == NULL) { 1477 cookie->err_rc = NS_LDAP_INVALID_PARAM; 1478 return (-1); 1479 } else { 1480 if (cookie->filter) 1481 free(cookie->filter); 1482 cookie->filter = strdup(cookie->i_filter); 1483 if (cookie->filter == NULL) { 1484 cookie->err_rc = NS_LDAP_MEMORY; 1485 return (-1); 1486 } 1487 } 1488 } else { 1489 if (cookie->filter) 1490 free(cookie->filter); 1491 cookie->filter = strdup(filter); 1492 free(filter); 1493 if (cookie->filter == NULL) { 1494 cookie->err_rc = NS_LDAP_MEMORY; 1495 return (-1); 1496 } 1497 } 1498 1499 /* 1500 * perform attribute/objectclass mapping on filter 1501 */ 1502 filter = NULL; 1503 1504 if (cookie->service) { 1505 rc = get_mapped_filter(cookie, &filter); 1506 if (rc != NS_LDAP_SUCCESS) { 1507 cookie->err_rc = rc; 1508 return (-1); 1509 } else { 1510 /* 1511 * get_mapped_filter returns 1512 * NULL filter pointer, if 1513 * no mapping was done 1514 */ 1515 if (filter) { 1516 free(cookie->filter); 1517 cookie->filter = filter; 1518 } 1519 } 1520 } 1521 1522 /* 1523 * validate filter to make sure it's legal 1524 * [remove redundant ()'s] 1525 */ 1526 rc = validate_filter(cookie); 1527 if (rc != NS_LDAP_SUCCESS) { 1528 cookie->err_rc = rc; 1529 return (-1); 1530 } 1531 1532 baselen = strlen(dptr->basedn); 1533 if (baselen > 0 && dptr->basedn[baselen-1] == COMMATOK) { 1534 rc = __ns_ldap_getParam(NS_LDAP_SEARCH_BASEDN_P, 1535 (void ***)¶m, &cookie->errorp); 1536 if (rc != NS_LDAP_SUCCESS) { 1537 cookie->err_rc = rc; 1538 return (-1); 1539 } 1540 str = ((char **)param)[0]; 1541 baselen += strlen(str)+1; 1542 if (cookie->basedn) 1543 free(cookie->basedn); 1544 cookie->basedn = (char *)malloc(baselen); 1545 if (cookie->basedn == NULL) { 1546 cookie->err_rc = NS_LDAP_MEMORY; 1547 return (-1); 1548 } 1549 (void) strcpy(cookie->basedn, dptr->basedn); 1550 (void) strcat(cookie->basedn, str); 1551 (void) __ns_ldap_freeParam(¶m); 1552 } else { 1553 if (cookie->basedn) 1554 free(cookie->basedn); 1555 cookie->basedn = strdup(dptr->basedn); 1556 } 1557 return (0); 1558 } 1559 1560 static int 1561 setup_referral_search(ns_ldap_cookie_t *cookie) 1562 { 1563 ns_referral_info_t *ref; 1564 1565 ref = cookie->refpos; 1566 cookie->scope = ref->refScope; 1567 if (cookie->filter) { 1568 free(cookie->filter); 1569 } 1570 cookie->filter = strdup(ref->refFilter); 1571 if (cookie->basedn) { 1572 free(cookie->basedn); 1573 } 1574 cookie->basedn = strdup(ref->refDN); 1575 if (cookie->filter == NULL || cookie->basedn == NULL) { 1576 cookie->err_rc = NS_LDAP_MEMORY; 1577 return (-1); 1578 } 1579 return (0); 1580 } 1581 1582 static int 1583 get_current_session(ns_ldap_cookie_t *cookie) 1584 { 1585 ConnectionID connectionId = -1; 1586 Connection *conp = NULL; 1587 int rc; 1588 int fail_if_new_pwd_reqd = 1; 1589 1590 rc = __s_api_getConnection(NULL, cookie->i_flags, 1591 cookie->i_auth, &connectionId, &conp, 1592 &cookie->errorp, fail_if_new_pwd_reqd, 1593 cookie->nopasswd_acct_mgmt); 1594 1595 /* 1596 * If password control attached in *cookie->errorp, 1597 * e.g. rc == NS_LDAP_SUCCESS_WITH_INFO, 1598 * free the error structure (we do not need 1599 * the sec_to_expired info). 1600 * Reset rc to NS_LDAP_SUCCESS. 1601 */ 1602 if (rc == NS_LDAP_SUCCESS_WITH_INFO) { 1603 (void) __ns_ldap_freeError( 1604 &cookie->errorp); 1605 cookie->errorp = NULL; 1606 rc = NS_LDAP_SUCCESS; 1607 } 1608 1609 if (rc != NS_LDAP_SUCCESS) { 1610 cookie->err_rc = rc; 1611 return (-1); 1612 } 1613 cookie->conn = conp; 1614 cookie->connectionId = connectionId; 1615 1616 return (0); 1617 } 1618 1619 static int 1620 get_next_session(ns_ldap_cookie_t *cookie) 1621 { 1622 ConnectionID connectionId = -1; 1623 Connection *conp = NULL; 1624 int rc; 1625 int fail_if_new_pwd_reqd = 1; 1626 1627 if (cookie->connectionId > -1) { 1628 DropConnection(cookie->connectionId, cookie->i_flags); 1629 cookie->connectionId = -1; 1630 } 1631 1632 rc = __s_api_getConnection(NULL, cookie->i_flags, 1633 cookie->i_auth, &connectionId, &conp, 1634 &cookie->errorp, fail_if_new_pwd_reqd, 1635 cookie->nopasswd_acct_mgmt); 1636 1637 /* 1638 * If password control attached in *cookie->errorp, 1639 * e.g. rc == NS_LDAP_SUCCESS_WITH_INFO, 1640 * free the error structure (we do not need 1641 * the sec_to_expired info). 1642 * Reset rc to NS_LDAP_SUCCESS. 1643 */ 1644 if (rc == NS_LDAP_SUCCESS_WITH_INFO) { 1645 (void) __ns_ldap_freeError( 1646 &cookie->errorp); 1647 cookie->errorp = NULL; 1648 rc = NS_LDAP_SUCCESS; 1649 } 1650 1651 if (rc != NS_LDAP_SUCCESS) { 1652 cookie->err_rc = rc; 1653 return (-1); 1654 } 1655 cookie->conn = conp; 1656 cookie->connectionId = connectionId; 1657 return (0); 1658 } 1659 1660 static int 1661 get_referral_session(ns_ldap_cookie_t *cookie) 1662 { 1663 ConnectionID connectionId = -1; 1664 Connection *conp = NULL; 1665 int rc; 1666 int fail_if_new_pwd_reqd = 1; 1667 1668 if (cookie->connectionId > -1) { 1669 DropConnection(cookie->connectionId, cookie->i_flags); 1670 cookie->connectionId = -1; 1671 } 1672 1673 rc = __s_api_getConnection(cookie->refpos->refHost, 0, 1674 cookie->i_auth, &connectionId, &conp, 1675 &cookie->errorp, fail_if_new_pwd_reqd, 1676 cookie->nopasswd_acct_mgmt); 1677 1678 /* 1679 * If password control attached in *cookie->errorp, 1680 * e.g. rc == NS_LDAP_SUCCESS_WITH_INFO, 1681 * free the error structure (we do not need 1682 * the sec_to_expired info). 1683 * Reset rc to NS_LDAP_SUCCESS. 1684 */ 1685 if (rc == NS_LDAP_SUCCESS_WITH_INFO) { 1686 (void) __ns_ldap_freeError( 1687 &cookie->errorp); 1688 cookie->errorp = NULL; 1689 rc = NS_LDAP_SUCCESS; 1690 } 1691 1692 if (rc != NS_LDAP_SUCCESS) { 1693 cookie->err_rc = rc; 1694 return (-1); 1695 } 1696 cookie->conn = conp; 1697 cookie->connectionId = connectionId; 1698 return (0); 1699 } 1700 1701 static int 1702 paging_supported(ns_ldap_cookie_t *cookie) 1703 { 1704 int rc; 1705 1706 cookie->listType = 0; 1707 rc = __s_api_isCtrlSupported(cookie->conn, 1708 LDAP_CONTROL_VLVREQUEST); 1709 if (rc == NS_LDAP_SUCCESS) { 1710 cookie->listType = VLVCTRLFLAG; 1711 return (1); 1712 } 1713 rc = __s_api_isCtrlSupported(cookie->conn, 1714 LDAP_CONTROL_SIMPLE_PAGE); 1715 if (rc == NS_LDAP_SUCCESS) { 1716 cookie->listType = SIMPLEPAGECTRLFLAG; 1717 return (1); 1718 } 1719 return (0); 1720 } 1721 1722 static int 1723 setup_vlv_params(ns_ldap_cookie_t *cookie) 1724 { 1725 LDAPControl **ctrls; 1726 LDAPsortkey **sortkeylist; 1727 LDAPControl *sortctrl = NULL; 1728 LDAPControl *vlvctrl = NULL; 1729 LDAPVirtualList vlist; 1730 int rc; 1731 1732 _freeControlList(&cookie->p_serverctrls); 1733 1734 rc = ldap_create_sort_keylist(&sortkeylist, SORTKEYLIST); 1735 if (rc != LDAP_SUCCESS) { 1736 (void) ldap_get_option(cookie->conn->ld, 1737 LDAP_OPT_ERROR_NUMBER, &rc); 1738 return (rc); 1739 } 1740 rc = ldap_create_sort_control(cookie->conn->ld, 1741 sortkeylist, 1, &sortctrl); 1742 ldap_free_sort_keylist(sortkeylist); 1743 if (rc != LDAP_SUCCESS) { 1744 (void) ldap_get_option(cookie->conn->ld, 1745 LDAP_OPT_ERROR_NUMBER, &rc); 1746 return (rc); 1747 } 1748 1749 vlist.ldvlist_index = cookie->index; 1750 vlist.ldvlist_size = 0; 1751 1752 vlist.ldvlist_before_count = 0; 1753 vlist.ldvlist_after_count = LISTPAGESIZE-1; 1754 vlist.ldvlist_attrvalue = NULL; 1755 vlist.ldvlist_extradata = NULL; 1756 1757 rc = ldap_create_virtuallist_control(cookie->conn->ld, 1758 &vlist, &vlvctrl); 1759 if (rc != LDAP_SUCCESS) { 1760 ldap_control_free(sortctrl); 1761 (void) ldap_get_option(cookie->conn->ld, LDAP_OPT_ERROR_NUMBER, 1762 &rc); 1763 return (rc); 1764 } 1765 1766 ctrls = (LDAPControl **)calloc(3, sizeof (LDAPControl *)); 1767 if (ctrls == NULL) { 1768 ldap_control_free(sortctrl); 1769 ldap_control_free(vlvctrl); 1770 return (LDAP_NO_MEMORY); 1771 } 1772 1773 ctrls[0] = sortctrl; 1774 ctrls[1] = vlvctrl; 1775 1776 cookie->p_serverctrls = ctrls; 1777 return (LDAP_SUCCESS); 1778 } 1779 1780 static int 1781 setup_simplepg_params(ns_ldap_cookie_t *cookie) 1782 { 1783 LDAPControl **ctrls; 1784 LDAPControl *pgctrl = NULL; 1785 int rc; 1786 1787 _freeControlList(&cookie->p_serverctrls); 1788 1789 rc = ldap_create_page_control(cookie->conn->ld, LISTPAGESIZE, 1790 cookie->ctrlCookie, (char)0, &pgctrl); 1791 if (rc != LDAP_SUCCESS) { 1792 (void) ldap_get_option(cookie->conn->ld, LDAP_OPT_ERROR_NUMBER, 1793 &rc); 1794 return (rc); 1795 } 1796 1797 ctrls = (LDAPControl **)calloc(2, sizeof (LDAPControl *)); 1798 if (ctrls == NULL) { 1799 ldap_control_free(pgctrl); 1800 return (LDAP_NO_MEMORY); 1801 } 1802 ctrls[0] = pgctrl; 1803 cookie->p_serverctrls = ctrls; 1804 return (LDAP_SUCCESS); 1805 } 1806 1807 static void 1808 proc_result_referrals(ns_ldap_cookie_t *cookie) 1809 { 1810 int errCode, i, rc; 1811 char **referrals = NULL; 1812 1813 /* 1814 * Only follow one level of referrals, i.e. 1815 * if already in referral mode, do nothing 1816 */ 1817 if (cookie->refpos == NULL) { 1818 cookie->new_state = END_RESULT; 1819 rc = ldap_parse_result(cookie->conn->ld, 1820 cookie->resultMsg, 1821 &errCode, NULL, 1822 NULL, &referrals, 1823 NULL, 0); 1824 if (rc != NS_LDAP_SUCCESS) { 1825 (void) ldap_get_option(cookie->conn->ld, 1826 LDAP_OPT_ERROR_NUMBER, 1827 &cookie->err_rc); 1828 cookie->new_state = LDAP_ERROR; 1829 return; 1830 } 1831 if (errCode == LDAP_REFERRAL) { 1832 for (i = 0; referrals[i] != NULL; 1833 i++) { 1834 /* add to referral list */ 1835 rc = __s_api_addRefInfo( 1836 &cookie->reflist, 1837 referrals[i], 1838 cookie->basedn, 1839 &cookie->scope, 1840 cookie->filter, 1841 cookie->conn->ld); 1842 if (rc != NS_LDAP_SUCCESS) { 1843 cookie->new_state = 1844 ERROR; 1845 break; 1846 } 1847 } 1848 ldap_value_free(referrals); 1849 } 1850 } 1851 } 1852 1853 static void 1854 proc_search_references(ns_ldap_cookie_t *cookie) 1855 { 1856 char **refurls = NULL; 1857 int i, rc; 1858 1859 /* 1860 * Only follow one level of referrals, i.e. 1861 * if already in referral mode, do nothing 1862 */ 1863 if (cookie->refpos == NULL) { 1864 refurls = ldap_get_reference_urls( 1865 cookie->conn->ld, 1866 cookie->resultMsg); 1867 if (refurls == NULL) { 1868 (void) ldap_get_option(cookie->conn->ld, 1869 LDAP_OPT_ERROR_NUMBER, 1870 &cookie->err_rc); 1871 cookie->new_state = LDAP_ERROR; 1872 return; 1873 } 1874 for (i = 0; refurls[i] != NULL; i++) { 1875 /* add to referral list */ 1876 rc = __s_api_addRefInfo( 1877 &cookie->reflist, 1878 refurls[i], 1879 cookie->basedn, 1880 &cookie->scope, 1881 cookie->filter, 1882 cookie->conn->ld); 1883 if (rc != NS_LDAP_SUCCESS) { 1884 cookie->new_state = 1885 ERROR; 1886 break; 1887 } 1888 } 1889 /* free allocated storage */ 1890 for (i = 0; refurls[i] != NULL; i++) 1891 free(refurls[i]); 1892 } 1893 } 1894 1895 static ns_state_t 1896 multi_result(ns_ldap_cookie_t *cookie) 1897 { 1898 char errstr[MAXERROR]; 1899 char *err; 1900 ns_ldap_error_t **errorp = NULL; 1901 LDAPControl **retCtrls = NULL; 1902 int i, rc; 1903 int errCode; 1904 int finished = 0; 1905 unsigned long target_posp = 0; 1906 unsigned long list_size = 0; 1907 unsigned int count = 0; 1908 char **referrals = NULL; 1909 1910 if (cookie->listType == VLVCTRLFLAG) { 1911 rc = ldap_parse_result(cookie->conn->ld, cookie->resultMsg, 1912 &errCode, NULL, NULL, &referrals, &retCtrls, 0); 1913 if (rc != LDAP_SUCCESS) { 1914 (void) ldap_get_option(cookie->conn->ld, 1915 LDAP_OPT_ERROR_NUMBER, 1916 &cookie->err_rc); 1917 (void) sprintf(errstr, 1918 gettext("LDAP ERROR (%d): %s.\n"), 1919 cookie->err_rc, 1920 gettext(ldap_err2string(cookie->err_rc))); 1921 err = strdup(errstr); 1922 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 1923 NULL); 1924 cookie->err_rc = NS_LDAP_INTERNAL; 1925 cookie->errorp = *errorp; 1926 return (LDAP_ERROR); 1927 } 1928 if (errCode == LDAP_REFERRAL) { 1929 for (i = 0; referrals[i] != NULL; 1930 i++) { 1931 /* add to referral list */ 1932 rc = __s_api_addRefInfo( 1933 &cookie->reflist, 1934 referrals[i], 1935 cookie->basedn, 1936 &cookie->scope, 1937 cookie->filter, 1938 cookie->conn->ld); 1939 if (rc != NS_LDAP_SUCCESS) { 1940 ldap_value_free( 1941 referrals); 1942 if (retCtrls) 1943 ldap_controls_free( 1944 retCtrls); 1945 return (ERROR); 1946 } 1947 } 1948 ldap_value_free(referrals); 1949 if (retCtrls) 1950 ldap_controls_free(retCtrls); 1951 return (END_RESULT); 1952 } 1953 if (retCtrls) { 1954 rc = ldap_parse_virtuallist_control( 1955 cookie->conn->ld, retCtrls, 1956 &target_posp, &list_size, &errCode); 1957 if (rc == LDAP_SUCCESS) { 1958 cookie->index = target_posp + LISTPAGESIZE; 1959 if (cookie->index > list_size) { 1960 finished = 1; 1961 } 1962 } 1963 ldap_controls_free(retCtrls); 1964 retCtrls = NULL; 1965 } 1966 else 1967 finished = 1; 1968 } else if (cookie->listType == SIMPLEPAGECTRLFLAG) { 1969 rc = ldap_parse_result(cookie->conn->ld, cookie->resultMsg, 1970 &errCode, NULL, NULL, &referrals, &retCtrls, 0); 1971 if (rc != LDAP_SUCCESS) { 1972 (void) ldap_get_option(cookie->conn->ld, 1973 LDAP_OPT_ERROR_NUMBER, 1974 &cookie->err_rc); 1975 (void) sprintf(errstr, 1976 gettext("LDAP ERROR (%d): %s.\n"), 1977 cookie->err_rc, 1978 gettext(ldap_err2string(cookie->err_rc))); 1979 err = strdup(errstr); 1980 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 1981 NULL); 1982 cookie->err_rc = NS_LDAP_INTERNAL; 1983 cookie->errorp = *errorp; 1984 return (LDAP_ERROR); 1985 } 1986 if (errCode == LDAP_REFERRAL) { 1987 for (i = 0; referrals[i] != NULL; 1988 i++) { 1989 /* add to referral list */ 1990 rc = __s_api_addRefInfo( 1991 &cookie->reflist, 1992 referrals[i], 1993 cookie->basedn, 1994 &cookie->scope, 1995 cookie->filter, 1996 cookie->conn->ld); 1997 if (rc != NS_LDAP_SUCCESS) { 1998 ldap_value_free( 1999 referrals); 2000 if (retCtrls) 2001 ldap_controls_free( 2002 retCtrls); 2003 return (ERROR); 2004 } 2005 } 2006 ldap_value_free(referrals); 2007 if (retCtrls) 2008 ldap_controls_free(retCtrls); 2009 return (END_RESULT); 2010 } 2011 if (retCtrls) { 2012 if (cookie->ctrlCookie) 2013 ber_bvfree(cookie->ctrlCookie); 2014 cookie->ctrlCookie = NULL; 2015 rc = ldap_parse_page_control( 2016 cookie->conn->ld, retCtrls, 2017 &count, &cookie->ctrlCookie); 2018 if (rc == LDAP_SUCCESS) { 2019 if ((cookie->ctrlCookie == NULL) || 2020 (cookie->ctrlCookie->bv_val == NULL) || 2021 (cookie->ctrlCookie->bv_len == 0)) 2022 finished = 1; 2023 } 2024 ldap_controls_free(retCtrls); 2025 retCtrls = NULL; 2026 } 2027 else 2028 finished = 1; 2029 } 2030 if (!finished && cookie->listType == VLVCTRLFLAG) 2031 return (NEXT_VLV); 2032 if (!finished && cookie->listType == SIMPLEPAGECTRLFLAG) 2033 return (NEXT_PAGE); 2034 if (finished) 2035 return (END_RESULT); 2036 return (ERROR); 2037 } 2038 2039 /* 2040 * This state machine performs one or more LDAP searches to a given 2041 * directory server using service search descriptors and schema 2042 * mapping as appropriate. The approximate pseudocode for 2043 * this routine is the following: 2044 * Given the current configuration [set/reset connection etc.] 2045 * and the current service search descriptor list 2046 * or default search filter parameters 2047 * foreach (service search filter) { 2048 * initialize the filter [via filter_init if appropriate] 2049 * get a valid session/connection (preferably the current one) 2050 * Recover if the connection is lost 2051 * perform the search 2052 * foreach (result entry) { 2053 * process result [via callback if appropriate] 2054 * save result for caller if accepted. 2055 * exit and return all collected if allResults found; 2056 * } 2057 * } 2058 * return collected results and exit 2059 */ 2060 2061 static 2062 ns_state_t 2063 search_state_machine(ns_ldap_cookie_t *cookie, ns_state_t state, int cycle) 2064 { 2065 char errstr[MAXERROR]; 2066 char *err; 2067 int rc, ret; 2068 ns_ldap_entry_t *nextEntry; 2069 ns_ldap_error_t *error = NULL; 2070 ns_ldap_error_t **errorp; 2071 2072 errorp = &error; 2073 cookie->state = state; 2074 errstr[0] = '\0'; 2075 2076 for (;;) { 2077 switch (cookie->state) { 2078 case CLEAR_RESULTS: 2079 if (cookie->conn != NULL && cookie->conn->ld != NULL && 2080 cookie->connectionId != -1 && cookie->msgId != 0) { 2081 (void) ldap_abandon_ext(cookie->conn->ld, 2082 cookie->msgId, NULL, NULL); 2083 cookie->msgId = 0; 2084 } 2085 cookie->new_state = EXIT; 2086 break; 2087 case GET_ACCT_MGMT_INFO: 2088 /* 2089 * Set the flag to get ldap account management controls. 2090 */ 2091 cookie->nopasswd_acct_mgmt = 1; 2092 cookie->new_state = INIT; 2093 break; 2094 case EXIT: 2095 /* state engine/connection cleaned up in delete */ 2096 if (cookie->attribute) { 2097 __s_api_free2dArray(cookie->attribute); 2098 cookie->attribute = NULL; 2099 } 2100 if (cookie->reflist) { 2101 __s_api_deleteRefInfo(cookie->reflist); 2102 cookie->reflist = NULL; 2103 } 2104 return (EXIT); 2105 case INIT: 2106 cookie->sdpos = NULL; 2107 cookie->new_state = NEXT_SEARCH_DESCRIPTOR; 2108 if (cookie->attribute) { 2109 __s_api_free2dArray(cookie->attribute); 2110 cookie->attribute = NULL; 2111 } 2112 if ((cookie->i_flags & NS_LDAP_NOMAP) == 0 && 2113 cookie->i_attr) { 2114 cookie->attribute = 2115 __ns_ldap_mapAttributeList( 2116 cookie->service, 2117 cookie->i_attr); 2118 } 2119 break; 2120 case NEXT_SEARCH_DESCRIPTOR: 2121 /* get next search descriptor */ 2122 if (cookie->sdpos == NULL) { 2123 cookie->sdpos = cookie->sdlist; 2124 cookie->new_state = GET_SESSION; 2125 } else { 2126 cookie->sdpos++; 2127 cookie->new_state = NEXT_SEARCH; 2128 } 2129 if (*cookie->sdpos == NULL) 2130 cookie->new_state = EXIT; 2131 break; 2132 case GET_SESSION: 2133 if (get_current_session(cookie) < 0) 2134 cookie->new_state = NEXT_SESSION; 2135 else 2136 cookie->new_state = NEXT_SEARCH; 2137 break; 2138 case NEXT_SESSION: 2139 if (get_next_session(cookie) < 0) 2140 cookie->new_state = RESTART_SESSION; 2141 else 2142 cookie->new_state = NEXT_SEARCH; 2143 break; 2144 case RESTART_SESSION: 2145 if (cookie->i_flags & NS_LDAP_HARD) { 2146 cookie->new_state = NEXT_SESSION; 2147 break; 2148 } 2149 (void) sprintf(errstr, 2150 gettext("Session error no available conn.\n"), 2151 state); 2152 err = strdup(errstr); 2153 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 2154 NULL); 2155 cookie->err_rc = NS_LDAP_INTERNAL; 2156 cookie->errorp = *errorp; 2157 cookie->new_state = EXIT; 2158 break; 2159 case NEXT_SEARCH: 2160 /* setup referrals search if necessary */ 2161 if (cookie->refpos) { 2162 if (setup_referral_search(cookie) < 0) { 2163 cookie->new_state = EXIT; 2164 break; 2165 } 2166 } else if (setup_next_search(cookie) < 0) { 2167 cookie->new_state = EXIT; 2168 break; 2169 } 2170 /* only do VLV/PAGE on scopes onelevel/subtree */ 2171 if (paging_supported(cookie)) { 2172 if (cookie->use_paging && 2173 (cookie->scope != LDAP_SCOPE_BASE)) { 2174 cookie->index = 1; 2175 if (cookie->listType == VLVCTRLFLAG) 2176 cookie->new_state = NEXT_VLV; 2177 else 2178 cookie->new_state = NEXT_PAGE; 2179 break; 2180 } 2181 } 2182 cookie->new_state = ONE_SEARCH; 2183 break; 2184 case NEXT_VLV: 2185 rc = setup_vlv_params(cookie); 2186 if (rc != LDAP_SUCCESS) { 2187 cookie->err_rc = rc; 2188 cookie->new_state = LDAP_ERROR; 2189 break; 2190 } 2191 cookie->next_state = MULTI_RESULT; 2192 cookie->new_state = DO_SEARCH; 2193 break; 2194 case NEXT_PAGE: 2195 rc = setup_simplepg_params(cookie); 2196 if (rc != LDAP_SUCCESS) { 2197 cookie->err_rc = rc; 2198 cookie->new_state = LDAP_ERROR; 2199 break; 2200 } 2201 cookie->next_state = MULTI_RESULT; 2202 cookie->new_state = DO_SEARCH; 2203 break; 2204 case ONE_SEARCH: 2205 cookie->next_state = NEXT_RESULT; 2206 cookie->new_state = DO_SEARCH; 2207 break; 2208 case DO_SEARCH: 2209 rc = ldap_search_ext(cookie->conn->ld, 2210 cookie->basedn, 2211 cookie->scope, 2212 cookie->filter, 2213 cookie->attribute, 2214 0, 2215 cookie->p_serverctrls, 2216 NULL, 2217 &cookie->search_timeout, 0, 2218 &cookie->msgId); 2219 if (rc != LDAP_SUCCESS) { 2220 if (rc == LDAP_BUSY || 2221 rc == LDAP_UNAVAILABLE || 2222 rc == LDAP_UNWILLING_TO_PERFORM || 2223 rc == LDAP_CONNECT_ERROR || 2224 rc == LDAP_SERVER_DOWN) { 2225 2226 cookie->new_state = NEXT_SESSION; 2227 2228 /* 2229 * If not able to reach the 2230 * server, inform the ldap 2231 * cache manager that the 2232 * server should be removed 2233 * from it's server list. 2234 * Thus, the manager will not 2235 * return this server on the next 2236 * get-server request and will 2237 * also reduce the server list 2238 * refresh TTL, so that it will 2239 * find out sooner when the server 2240 * is up again. 2241 */ 2242 if (rc == LDAP_CONNECT_ERROR || 2243 rc == LDAP_SERVER_DOWN) { 2244 ret = __s_api_removeServer( 2245 cookie->conn->serverAddr); 2246 if (ret == NOSERVER && 2247 cookie->conn_auth_type 2248 == NS_LDAP_AUTH_NONE) { 2249 /* 2250 * Couldn't remove 2251 * server from server 2252 * list. 2253 * Exit to avoid 2254 * potential infinite 2255 * loop. 2256 */ 2257 cookie->err_rc = rc; 2258 cookie->new_state = 2259 LDAP_ERROR; 2260 } 2261 if (cookie->connectionId > -1) { 2262 /* 2263 * NS_LDAP_NEW_CONN 2264 * indicates that the 2265 * connection should 2266 * be deleted, not 2267 * kept alive 2268 */ 2269 DropConnection( 2270 cookie-> 2271 connectionId, 2272 NS_LDAP_NEW_CONN); 2273 cookie->connectionId = 2274 -1; 2275 } 2276 } 2277 break; 2278 } 2279 cookie->err_rc = rc; 2280 cookie->new_state = LDAP_ERROR; 2281 break; 2282 } 2283 cookie->new_state = cookie->next_state; 2284 break; 2285 case NEXT_RESULT: 2286 rc = ldap_result(cookie->conn->ld, cookie->msgId, 2287 LDAP_MSG_ONE, 2288 (struct timeval *)&cookie->search_timeout, 2289 &cookie->resultMsg); 2290 if (rc == LDAP_RES_SEARCH_RESULT) { 2291 cookie->new_state = END_RESULT; 2292 /* check and process referrals info */ 2293 if (cookie->followRef) 2294 proc_result_referrals( 2295 cookie); 2296 (void) ldap_msgfree(cookie->resultMsg); 2297 cookie->resultMsg = NULL; 2298 break; 2299 } 2300 /* handle referrals if necessary */ 2301 if (rc == LDAP_RES_SEARCH_REFERENCE) { 2302 if (cookie->followRef) 2303 proc_search_references(cookie); 2304 (void) ldap_msgfree(cookie->resultMsg); 2305 cookie->resultMsg = NULL; 2306 break; 2307 } 2308 if (rc != LDAP_RES_SEARCH_ENTRY) { 2309 switch (rc) { 2310 case 0: 2311 rc = LDAP_TIMEOUT; 2312 break; 2313 case -1: 2314 rc = ldap_get_lderrno(cookie->conn->ld, 2315 NULL, NULL); 2316 break; 2317 default: 2318 rc = ldap_result2error(cookie->conn->ld, 2319 cookie->resultMsg, 1); 2320 break; 2321 } 2322 if (rc == LDAP_TIMEOUT || 2323 rc == LDAP_SERVER_DOWN) { 2324 if (rc == LDAP_TIMEOUT) 2325 (void) __s_api_removeServer( 2326 cookie->conn->serverAddr); 2327 if (cookie->connectionId > -1) { 2328 DropConnection( 2329 cookie->connectionId, 2330 NS_LDAP_NEW_CONN); 2331 cookie->connectionId = -1; 2332 } 2333 cookie->err_from_result = 1; 2334 } 2335 (void) ldap_msgfree(cookie->resultMsg); 2336 cookie->resultMsg = NULL; 2337 if (rc == LDAP_BUSY || 2338 rc == LDAP_UNAVAILABLE || 2339 rc == LDAP_UNWILLING_TO_PERFORM) { 2340 cookie->new_state = NEXT_SESSION; 2341 break; 2342 } 2343 cookie->err_rc = rc; 2344 cookie->new_state = LDAP_ERROR; 2345 break; 2346 } 2347 /* else LDAP_RES_SEARCH_ENTRY */ 2348 /* get account management response control */ 2349 if (cookie->nopasswd_acct_mgmt == 1) { 2350 rc = ldap_get_entry_controls(cookie->conn->ld, 2351 cookie->resultMsg, 2352 &(cookie->resultctrl)); 2353 if (rc != LDAP_SUCCESS) { 2354 cookie->new_state = LDAP_ERROR; 2355 cookie->err_rc = rc; 2356 break; 2357 } 2358 } 2359 rc = __s_api_getEntry(cookie); 2360 (void) ldap_msgfree(cookie->resultMsg); 2361 cookie->resultMsg = NULL; 2362 if (rc != NS_LDAP_SUCCESS) { 2363 cookie->new_state = LDAP_ERROR; 2364 break; 2365 } 2366 cookie->new_state = PROCESS_RESULT; 2367 cookie->next_state = NEXT_RESULT; 2368 break; 2369 case MULTI_RESULT: 2370 rc = ldap_result(cookie->conn->ld, cookie->msgId, 2371 LDAP_MSG_ONE, 2372 (struct timeval *)&cookie->search_timeout, 2373 &cookie->resultMsg); 2374 if (rc == LDAP_RES_SEARCH_RESULT) { 2375 rc = ldap_result2error(cookie->conn->ld, 2376 cookie->resultMsg, 0); 2377 if (rc != LDAP_SUCCESS) { 2378 cookie->err_rc = rc; 2379 cookie->new_state = LDAP_ERROR; 2380 (void) ldap_msgfree(cookie->resultMsg); 2381 break; 2382 } 2383 cookie->new_state = multi_result(cookie); 2384 (void) ldap_msgfree(cookie->resultMsg); 2385 cookie->resultMsg = NULL; 2386 break; 2387 } 2388 /* handle referrals if necessary */ 2389 if (rc == LDAP_RES_SEARCH_REFERENCE && 2390 cookie->followRef) { 2391 proc_search_references(cookie); 2392 (void) ldap_msgfree(cookie->resultMsg); 2393 cookie->resultMsg = NULL; 2394 break; 2395 } 2396 if (rc != LDAP_RES_SEARCH_ENTRY) { 2397 switch (rc) { 2398 case 0: 2399 rc = LDAP_TIMEOUT; 2400 break; 2401 case -1: 2402 rc = ldap_get_lderrno(cookie->conn->ld, 2403 NULL, NULL); 2404 break; 2405 default: 2406 rc = ldap_result2error(cookie->conn->ld, 2407 cookie->resultMsg, 1); 2408 break; 2409 } 2410 if (rc == LDAP_TIMEOUT || 2411 rc == LDAP_SERVER_DOWN) { 2412 if (rc == LDAP_TIMEOUT) 2413 (void) __s_api_removeServer( 2414 cookie->conn->serverAddr); 2415 if (cookie->connectionId > -1) { 2416 DropConnection( 2417 cookie->connectionId, 2418 NS_LDAP_NEW_CONN); 2419 cookie->connectionId = -1; 2420 } 2421 cookie->err_from_result = 1; 2422 } 2423 (void) ldap_msgfree(cookie->resultMsg); 2424 cookie->resultMsg = NULL; 2425 if (rc == LDAP_BUSY || 2426 rc == LDAP_UNAVAILABLE || 2427 rc == LDAP_UNWILLING_TO_PERFORM) { 2428 cookie->new_state = NEXT_SESSION; 2429 break; 2430 } 2431 cookie->err_rc = rc; 2432 cookie->new_state = LDAP_ERROR; 2433 break; 2434 } 2435 /* else LDAP_RES_SEARCH_ENTRY */ 2436 rc = __s_api_getEntry(cookie); 2437 (void) ldap_msgfree(cookie->resultMsg); 2438 cookie->resultMsg = NULL; 2439 if (rc != NS_LDAP_SUCCESS) { 2440 cookie->new_state = LDAP_ERROR; 2441 break; 2442 } 2443 cookie->new_state = PROCESS_RESULT; 2444 cookie->next_state = MULTI_RESULT; 2445 break; 2446 case PROCESS_RESULT: 2447 /* NOTE THIS STATE MAY BE PROCESSED BY CALLER */ 2448 if (cookie->use_usercb && cookie->callback) { 2449 rc = 0; 2450 for (nextEntry = cookie->result->entry; 2451 nextEntry != NULL; 2452 nextEntry = nextEntry->next) { 2453 rc = (*cookie->callback)(nextEntry, 2454 cookie->userdata); 2455 2456 if (rc == NS_LDAP_CB_DONE) { 2457 /* cb doesn't want any more data */ 2458 rc = NS_LDAP_PARTIAL; 2459 cookie->err_rc = rc; 2460 break; 2461 } else if (rc != NS_LDAP_CB_NEXT) { 2462 /* invalid return code */ 2463 rc = NS_LDAP_OP_FAILED; 2464 cookie->err_rc = rc; 2465 break; 2466 } 2467 } 2468 (void) __ns_ldap_freeResult(&cookie->result); 2469 cookie->result = NULL; 2470 } 2471 if (rc != 0) { 2472 cookie->new_state = EXIT; 2473 break; 2474 } 2475 /* NOTE PREVIOUS STATE SPECIFIES NEXT STATE */ 2476 cookie->new_state = cookie->next_state; 2477 break; 2478 case END_PROCESS_RESULT: 2479 cookie->new_state = cookie->next_state; 2480 break; 2481 case END_RESULT: 2482 /* 2483 * XXX DO WE NEED THIS CASE? 2484 * if (search is complete) { 2485 * cookie->new_state = EXIT; 2486 * } else 2487 */ 2488 /* 2489 * entering referral mode if necessary 2490 */ 2491 if (cookie->followRef && cookie->reflist) 2492 cookie->new_state = 2493 NEXT_REFERRAL; 2494 else 2495 cookie->new_state = 2496 NEXT_SEARCH_DESCRIPTOR; 2497 break; 2498 case NEXT_REFERRAL: 2499 /* get next referral info */ 2500 if (cookie->refpos == NULL) 2501 cookie->refpos = 2502 cookie->reflist; 2503 else 2504 cookie->refpos = 2505 cookie->refpos->next; 2506 /* check see if done with all referrals */ 2507 if (cookie->refpos != NULL) 2508 cookie->new_state = 2509 GET_REFERRAL_SESSION; 2510 else { 2511 __s_api_deleteRefInfo(cookie->reflist); 2512 cookie->reflist = NULL; 2513 cookie->new_state = 2514 NEXT_SEARCH_DESCRIPTOR; 2515 } 2516 break; 2517 case GET_REFERRAL_SESSION: 2518 if (get_referral_session(cookie) < 0) 2519 cookie->new_state = EXIT; 2520 else { 2521 cookie->new_state = NEXT_SEARCH; 2522 } 2523 break; 2524 case LDAP_ERROR: 2525 if (cookie->err_from_result) { 2526 if (cookie->err_rc == LDAP_SERVER_DOWN) { 2527 (void) sprintf(errstr, 2528 gettext("LDAP ERROR (%d): " 2529 "Error occurred during" 2530 " receiving results. " 2531 "This may be due to a " 2532 "stalled connection."), 2533 cookie->err_rc); 2534 } else if (cookie->err_rc == LDAP_TIMEOUT) { 2535 (void) sprintf(errstr, 2536 gettext("LDAP ERROR (%d): " 2537 "Error occurred during" 2538 " receiving results. %s" 2539 "."), cookie->err_rc, 2540 ldap_err2string( 2541 cookie->err_rc)); 2542 } 2543 } else 2544 (void) sprintf(errstr, 2545 gettext("LDAP ERROR (%d): %s."), 2546 cookie->err_rc, 2547 ldap_err2string(cookie->err_rc)); 2548 err = strdup(errstr); 2549 if (cookie->err_from_result) { 2550 MKERROR(LOG_WARNING, *errorp, cookie->err_rc, 2551 err, NULL); 2552 } else { 2553 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, 2554 err, NULL); 2555 } 2556 cookie->err_rc = NS_LDAP_INTERNAL; 2557 cookie->errorp = *errorp; 2558 return (ERROR); 2559 default: 2560 case ERROR: 2561 (void) sprintf(errstr, 2562 gettext("Internal State machine exit (%d).\n"), 2563 cookie->state); 2564 err = strdup(errstr); 2565 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, 2566 NULL); 2567 cookie->err_rc = NS_LDAP_INTERNAL; 2568 cookie->errorp = *errorp; 2569 return (ERROR); 2570 } 2571 2572 if (cycle == ONE_STEP) { 2573 return (cookie->new_state); 2574 } 2575 cookie->state = cookie->new_state; 2576 } 2577 /*NOTREACHED*/ 2578 #if 0 2579 (void) sprintf(errstr, 2580 gettext("Unexpected State machine error.\n")); 2581 err = strdup(errstr); 2582 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, err, NULL); 2583 cookie->err_rc = NS_LDAP_INTERNAL; 2584 cookie->errorp = *errorp; 2585 return (ERROR); 2586 #endif 2587 } 2588 2589 2590 2591 /* 2592 * __ns_ldap_list performs one or more LDAP searches to a given 2593 * directory server using service search descriptors and schema 2594 * mapping as appropriate. 2595 */ 2596 2597 int 2598 __ns_ldap_list( 2599 const char *service, 2600 const char *filter, 2601 int (*init_filter_cb)(const ns_ldap_search_desc_t *desc, 2602 char **realfilter, const void *userdata), 2603 const char * const *attribute, 2604 const ns_cred_t *auth, 2605 const int flags, 2606 ns_ldap_result_t **rResult, /* return result entries */ 2607 ns_ldap_error_t **errorp, 2608 int (*callback)(const ns_ldap_entry_t *entry, const void *userdata), 2609 const void *userdata) 2610 { 2611 ns_ldap_cookie_t *cookie; 2612 ns_ldap_search_desc_t **sdlist = NULL; 2613 ns_ldap_search_desc_t *dptr; 2614 ns_ldap_error_t *error = NULL; 2615 char **dns = NULL; 2616 int scope; 2617 int rc; 2618 int from_result; 2619 2620 *errorp = NULL; 2621 2622 /* Initialize State machine cookie */ 2623 cookie = init_search_state_machine(); 2624 if (cookie == NULL) { 2625 return (NS_LDAP_MEMORY); 2626 } 2627 2628 /* see if need to follow referrals */ 2629 rc = __s_api_toFollowReferrals(flags, 2630 &cookie->followRef, errorp); 2631 if (rc != NS_LDAP_SUCCESS) { 2632 delete_search_cookie(cookie); 2633 return (rc); 2634 } 2635 2636 /* get the service descriptor - or create a default one */ 2637 rc = __s_api_get_SSD_from_SSDtoUse_service(service, 2638 &sdlist, errorp); 2639 if (rc != NS_LDAP_SUCCESS) { 2640 delete_search_cookie(cookie); 2641 *errorp = error; 2642 return (rc); 2643 } 2644 2645 if (sdlist == NULL) { 2646 /* Create default service Desc */ 2647 sdlist = (ns_ldap_search_desc_t **)calloc(2, 2648 sizeof (ns_ldap_search_desc_t *)); 2649 if (sdlist == NULL) { 2650 delete_search_cookie(cookie); 2651 cookie = NULL; 2652 return (NS_LDAP_MEMORY); 2653 } 2654 dptr = (ns_ldap_search_desc_t *) 2655 calloc(1, sizeof (ns_ldap_search_desc_t)); 2656 if (dptr == NULL) { 2657 free(sdlist); 2658 delete_search_cookie(cookie); 2659 cookie = NULL; 2660 return (NS_LDAP_MEMORY); 2661 } 2662 sdlist[0] = dptr; 2663 2664 /* default base */ 2665 rc = __s_api_getDNs(&dns, service, &cookie->errorp); 2666 if (rc != NS_LDAP_SUCCESS) { 2667 if (dns) { 2668 __s_api_free2dArray(dns); 2669 dns = NULL; 2670 } 2671 *errorp = cookie->errorp; 2672 cookie->errorp = NULL; 2673 delete_search_cookie(cookie); 2674 cookie = NULL; 2675 return (rc); 2676 } 2677 dptr->basedn = strdup(dns[0]); 2678 __s_api_free2dArray(dns); 2679 dns = NULL; 2680 2681 /* default scope */ 2682 scope = 0; 2683 rc = __s_api_getSearchScope(&scope, &cookie->errorp); 2684 dptr->scope = scope; 2685 } 2686 2687 cookie->sdlist = sdlist; 2688 2689 /* 2690 * use VLV/PAGE control only if NS_LDAP_PAGE_CTRL is set 2691 */ 2692 if (flags & NS_LDAP_PAGE_CTRL) 2693 cookie->use_paging = TRUE; 2694 else 2695 cookie->use_paging = FALSE; 2696 2697 /* Set up other arguments */ 2698 cookie->userdata = userdata; 2699 if (init_filter_cb != NULL) { 2700 cookie->init_filter_cb = init_filter_cb; 2701 cookie->use_filtercb = 1; 2702 } 2703 if (callback != NULL) { 2704 cookie->callback = callback; 2705 cookie->use_usercb = 1; 2706 } 2707 if (service) { 2708 cookie->service = strdup(service); 2709 if (cookie->service == NULL) { 2710 delete_search_cookie(cookie); 2711 cookie = NULL; 2712 return (NS_LDAP_MEMORY); 2713 } 2714 } 2715 2716 cookie->i_filter = strdup(filter); 2717 cookie->i_attr = attribute; 2718 cookie->i_auth = auth; 2719 cookie->i_flags = flags; 2720 2721 /* Process search */ 2722 rc = search_state_machine(cookie, INIT, 0); 2723 2724 /* Copy results back to user */ 2725 rc = cookie->err_rc; 2726 if (rc != NS_LDAP_SUCCESS) 2727 *errorp = cookie->errorp; 2728 *rResult = cookie->result; 2729 from_result = cookie->err_from_result; 2730 2731 cookie->errorp = NULL; 2732 cookie->result = NULL; 2733 delete_search_cookie(cookie); 2734 cookie = NULL; 2735 2736 if (from_result == 0 && *rResult == NULL) 2737 rc = NS_LDAP_NOTFOUND; 2738 return (rc); 2739 } 2740 2741 /* 2742 * __s_api_find_domainname performs one or more LDAP searches to 2743 * find the value of the nisdomain attribute associated with 2744 * the input DN 2745 */ 2746 2747 static int 2748 __s_api_find_domainname( 2749 const char *dn, 2750 char **domainname, 2751 const ns_cred_t *cred, 2752 ns_ldap_error_t **errorp) 2753 { 2754 2755 ns_ldap_cookie_t *cookie; 2756 ns_ldap_search_desc_t **sdlist; 2757 ns_ldap_search_desc_t *dptr; 2758 int rc; 2759 char **value; 2760 int flags = 0; 2761 2762 *domainname = NULL; 2763 *errorp = NULL; 2764 2765 /* Initialize State machine cookie */ 2766 cookie = init_search_state_machine(); 2767 if (cookie == NULL) { 2768 return (NS_LDAP_MEMORY); 2769 } 2770 2771 /* see if need to follow referrals */ 2772 rc = __s_api_toFollowReferrals(flags, 2773 &cookie->followRef, errorp); 2774 if (rc != NS_LDAP_SUCCESS) { 2775 delete_search_cookie(cookie); 2776 return (rc); 2777 } 2778 2779 /* Create default service Desc */ 2780 sdlist = (ns_ldap_search_desc_t **)calloc(2, 2781 sizeof (ns_ldap_search_desc_t *)); 2782 if (sdlist == NULL) { 2783 delete_search_cookie(cookie); 2784 cookie = NULL; 2785 return (NS_LDAP_MEMORY); 2786 } 2787 dptr = (ns_ldap_search_desc_t *) 2788 calloc(1, sizeof (ns_ldap_search_desc_t)); 2789 if (dptr == NULL) { 2790 free(sdlist); 2791 delete_search_cookie(cookie); 2792 cookie = NULL; 2793 return (NS_LDAP_MEMORY); 2794 } 2795 sdlist[0] = dptr; 2796 2797 /* search base is dn */ 2798 dptr->basedn = strdup(dn); 2799 2800 /* search scope is base */ 2801 dptr->scope = NS_LDAP_SCOPE_BASE; 2802 2803 /* search filter is "nisdomain=*" */ 2804 dptr->filter = strdup(_NIS_FILTER); 2805 2806 cookie->sdlist = sdlist; 2807 cookie->i_filter = strdup(dptr->filter); 2808 cookie->i_attr = nis_domain_attrs; 2809 cookie->i_auth = cred; 2810 cookie->i_flags = 0; 2811 2812 /* Process search */ 2813 rc = search_state_machine(cookie, INIT, 0); 2814 2815 /* Copy domain name if found */ 2816 rc = cookie->err_rc; 2817 if (rc != NS_LDAP_SUCCESS) 2818 *errorp = cookie->errorp; 2819 if (cookie->result == NULL) 2820 rc = NS_LDAP_NOTFOUND; 2821 if (rc == NS_LDAP_SUCCESS) { 2822 value = __ns_ldap_getAttr(cookie->result->entry, 2823 _NIS_DOMAIN); 2824 if (value[0]) 2825 *domainname = strdup(value[0]); 2826 else 2827 rc = NS_LDAP_NOTFOUND; 2828 } 2829 if (cookie->result != NULL) 2830 (void) __ns_ldap_freeResult(&cookie->result); 2831 cookie->errorp = NULL; 2832 delete_search_cookie(cookie); 2833 cookie = NULL; 2834 return (rc); 2835 } 2836 2837 int 2838 __ns_ldap_firstEntry( 2839 const char *service, 2840 const char *filter, 2841 int (*init_filter_cb)(const ns_ldap_search_desc_t *desc, 2842 char **realfilter, const void *userdata), 2843 const char * const *attribute, 2844 const ns_cred_t *auth, 2845 const int flags, 2846 void **vcookie, 2847 ns_ldap_result_t **result, 2848 ns_ldap_error_t ** errorp, 2849 const void *userdata) 2850 { 2851 ns_ldap_cookie_t *cookie = NULL; 2852 ns_ldap_error_t *error = NULL; 2853 ns_state_t state; 2854 ns_ldap_search_desc_t **sdlist; 2855 ns_ldap_search_desc_t *dptr; 2856 char **dns = NULL; 2857 int scope; 2858 int rc; 2859 2860 *errorp = NULL; 2861 *result = NULL; 2862 2863 /* get the service descriptor - or create a default one */ 2864 rc = __s_api_get_SSD_from_SSDtoUse_service(service, 2865 &sdlist, errorp); 2866 if (rc != NS_LDAP_SUCCESS) { 2867 *errorp = error; 2868 return (rc); 2869 } 2870 if (sdlist == NULL) { 2871 /* Create default service Desc */ 2872 sdlist = (ns_ldap_search_desc_t **)calloc(2, 2873 sizeof (ns_ldap_search_desc_t *)); 2874 if (sdlist == NULL) { 2875 return (NS_LDAP_MEMORY); 2876 } 2877 dptr = (ns_ldap_search_desc_t *) 2878 calloc(1, sizeof (ns_ldap_search_desc_t)); 2879 if (dptr == NULL) { 2880 free(sdlist); 2881 return (NS_LDAP_MEMORY); 2882 } 2883 sdlist[0] = dptr; 2884 2885 /* default base */ 2886 rc = __s_api_getDNs(&dns, service, &error); 2887 if (rc != NS_LDAP_SUCCESS) { 2888 if (dns) { 2889 __s_api_free2dArray(dns); 2890 dns = NULL; 2891 } 2892 if (sdlist) { 2893 (void) __ns_ldap_freeSearchDescriptors( 2894 &sdlist); 2895 2896 sdlist = NULL; 2897 } 2898 *errorp = error; 2899 return (rc); 2900 } 2901 dptr->basedn = strdup(dns[0]); 2902 __s_api_free2dArray(dns); 2903 dns = NULL; 2904 2905 /* default scope */ 2906 scope = 0; 2907 cookie = init_search_state_machine(); 2908 if (cookie == NULL) { 2909 if (sdlist) { 2910 (void) __ns_ldap_freeSearchDescriptors(&sdlist); 2911 sdlist = NULL; 2912 } 2913 return (NS_LDAP_MEMORY); 2914 } 2915 rc = __s_api_getSearchScope(&scope, &cookie->errorp); 2916 dptr->scope = scope; 2917 } 2918 2919 /* Initialize State machine cookie */ 2920 if (cookie == NULL) 2921 cookie = init_search_state_machine(); 2922 if (cookie == NULL) { 2923 if (sdlist) { 2924 (void) __ns_ldap_freeSearchDescriptors(&sdlist); 2925 sdlist = NULL; 2926 } 2927 return (NS_LDAP_MEMORY); 2928 } 2929 2930 cookie->sdlist = sdlist; 2931 2932 /* see if need to follow referrals */ 2933 rc = __s_api_toFollowReferrals(flags, 2934 &cookie->followRef, errorp); 2935 if (rc != NS_LDAP_SUCCESS) { 2936 delete_search_cookie(cookie); 2937 return (rc); 2938 } 2939 2940 /* 2941 * use VLV/PAGE control only if NS_LDAP_NO_PAGE_CTRL is not set 2942 */ 2943 if (flags & NS_LDAP_NO_PAGE_CTRL) 2944 cookie->use_paging = FALSE; 2945 else 2946 cookie->use_paging = TRUE; 2947 2948 /* Set up other arguments */ 2949 cookie->userdata = userdata; 2950 if (init_filter_cb != NULL) { 2951 cookie->init_filter_cb = init_filter_cb; 2952 cookie->use_filtercb = 1; 2953 } 2954 cookie->use_usercb = 0; 2955 if (service) { 2956 cookie->service = strdup(service); 2957 if (cookie->service == NULL) { 2958 delete_search_cookie(cookie); 2959 return (NS_LDAP_MEMORY); 2960 } 2961 } 2962 2963 cookie->i_filter = strdup(filter); 2964 cookie->i_attr = attribute; 2965 cookie->i_auth = auth; 2966 cookie->i_flags = flags; 2967 2968 state = INIT; 2969 for (;;) { 2970 state = search_state_machine(cookie, state, ONE_STEP); 2971 switch (state) { 2972 case PROCESS_RESULT: 2973 *result = cookie->result; 2974 cookie->result = NULL; 2975 *vcookie = (void *)cookie; 2976 return (NS_LDAP_SUCCESS); 2977 case LDAP_ERROR: 2978 state = search_state_machine(cookie, state, ONE_STEP); 2979 state = search_state_machine(cookie, CLEAR_RESULTS, 2980 ONE_STEP); 2981 /* FALLTHROUGH */ 2982 case ERROR: 2983 rc = cookie->err_rc; 2984 *errorp = cookie->errorp; 2985 cookie->errorp = NULL; 2986 delete_search_cookie(cookie); 2987 return (rc); 2988 case EXIT: 2989 rc = cookie->err_rc; 2990 if (rc != NS_LDAP_SUCCESS) { 2991 *errorp = cookie->errorp; 2992 cookie->errorp = NULL; 2993 } else { 2994 rc = NS_LDAP_NOTFOUND; 2995 } 2996 2997 delete_search_cookie(cookie); 2998 return (rc); 2999 3000 default: 3001 break; 3002 } 3003 } 3004 } 3005 3006 /*ARGSUSED2*/ 3007 int 3008 __ns_ldap_nextEntry( 3009 void *vcookie, 3010 ns_ldap_result_t **result, 3011 ns_ldap_error_t ** errorp) 3012 { 3013 ns_ldap_cookie_t *cookie; 3014 ns_state_t state; 3015 int rc; 3016 3017 cookie = (ns_ldap_cookie_t *)vcookie; 3018 cookie->result = NULL; 3019 *result = NULL; 3020 3021 state = END_PROCESS_RESULT; 3022 for (;;) { 3023 /* 3024 * if the ldap connection being used is shared, 3025 * ensure the thread-specific data area for setting 3026 * status is allocated 3027 */ 3028 if (cookie->conn->shared > 0) { 3029 rc = __s_api_check_MTC_tsd(); 3030 if (rc != NS_LDAP_SUCCESS) 3031 return (rc); 3032 } 3033 3034 state = search_state_machine(cookie, state, ONE_STEP); 3035 switch (state) { 3036 case PROCESS_RESULT: 3037 *result = cookie->result; 3038 cookie->result = NULL; 3039 return (NS_LDAP_SUCCESS); 3040 case LDAP_ERROR: 3041 state = search_state_machine(cookie, state, ONE_STEP); 3042 state = search_state_machine(cookie, CLEAR_RESULTS, 3043 ONE_STEP); 3044 /* FALLTHROUGH */ 3045 case ERROR: 3046 rc = cookie->err_rc; 3047 *errorp = cookie->errorp; 3048 cookie->errorp = NULL; 3049 return (rc); 3050 case EXIT: 3051 return (NS_LDAP_SUCCESS); 3052 } 3053 } 3054 } 3055 3056 int 3057 __ns_ldap_endEntry( 3058 void **vcookie, 3059 ns_ldap_error_t ** errorp) 3060 { 3061 ns_ldap_cookie_t *cookie; 3062 int rc; 3063 3064 if (*vcookie == NULL) 3065 return (NS_LDAP_INVALID_PARAM); 3066 3067 cookie = (ns_ldap_cookie_t *)(*vcookie); 3068 cookie->result = NULL; 3069 3070 /* Complete search */ 3071 rc = search_state_machine(cookie, CLEAR_RESULTS, 0); 3072 3073 /* Copy results back to user */ 3074 rc = cookie->err_rc; 3075 if (rc != NS_LDAP_SUCCESS) 3076 *errorp = cookie->errorp; 3077 3078 cookie->errorp = NULL; 3079 delete_search_cookie(cookie); 3080 cookie = NULL; 3081 *vcookie = NULL; 3082 3083 return (rc); 3084 } 3085 3086 3087 int 3088 __ns_ldap_freeResult(ns_ldap_result_t **result) 3089 { 3090 3091 ns_ldap_entry_t *curEntry = NULL; 3092 ns_ldap_entry_t *delEntry = NULL; 3093 int i; 3094 ns_ldap_result_t *res = *result; 3095 3096 #ifdef DEBUG 3097 (void) fprintf(stderr, "__ns_ldap_freeResult START\n"); 3098 #endif 3099 if (res == NULL) 3100 return (NS_LDAP_INVALID_PARAM); 3101 3102 if (res->entry != NULL) 3103 curEntry = res->entry; 3104 3105 for (i = 0; i < res->entries_count; i++) { 3106 if (curEntry != NULL) { 3107 delEntry = curEntry; 3108 curEntry = curEntry->next; 3109 __ns_ldap_freeEntry(delEntry); 3110 } 3111 } 3112 3113 free(res); 3114 *result = NULL; 3115 return (NS_LDAP_SUCCESS); 3116 } 3117 3118 /*ARGSUSED*/ 3119 int 3120 __ns_ldap_auth(const ns_cred_t *auth, 3121 const int flags, 3122 ns_ldap_error_t **errorp, 3123 LDAPControl **serverctrls, 3124 LDAPControl **clientctrls) 3125 { 3126 3127 ConnectionID connectionId = -1; 3128 Connection *conp; 3129 int rc = 0; 3130 int do_not_fail_if_new_pwd_reqd = 0; 3131 int nopasswd_acct_mgmt = 0; 3132 3133 3134 #ifdef DEBUG 3135 (void) fprintf(stderr, "__ns_ldap_auth START\n"); 3136 #endif 3137 3138 *errorp = NULL; 3139 if (!auth) 3140 return (NS_LDAP_INVALID_PARAM); 3141 3142 rc = __s_api_getConnection(NULL, flags | NS_LDAP_NEW_CONN, 3143 auth, &connectionId, &conp, errorp, 3144 do_not_fail_if_new_pwd_reqd, nopasswd_acct_mgmt); 3145 if (rc == NS_LDAP_OP_FAILED && *errorp) 3146 (void) __ns_ldap_freeError(errorp); 3147 3148 if (connectionId > -1) 3149 DropConnection(connectionId, flags); 3150 return (rc); 3151 } 3152 3153 char ** 3154 __ns_ldap_getAttr(const ns_ldap_entry_t *entry, const char *attrname) 3155 { 3156 int i; 3157 3158 if (entry == NULL) 3159 return (NULL); 3160 for (i = 0; i < entry->attr_count; i++) { 3161 if (strcasecmp(entry->attr_pair[i]->attrname, attrname) == NULL) 3162 return (entry->attr_pair[i]->attrvalue); 3163 } 3164 return (NULL); 3165 } 3166 3167 ns_ldap_attr_t * 3168 __ns_ldap_getAttrStruct(const ns_ldap_entry_t *entry, const char *attrname) 3169 { 3170 int i; 3171 3172 if (entry == NULL) 3173 return (NULL); 3174 for (i = 0; i < entry->attr_count; i++) { 3175 if (strcasecmp(entry->attr_pair[i]->attrname, attrname) == NULL) 3176 return (entry->attr_pair[i]); 3177 } 3178 return (NULL); 3179 } 3180 3181 3182 /*ARGSUSED*/ 3183 int 3184 __ns_ldap_uid2dn(const char *uid, 3185 char **userDN, 3186 const ns_cred_t *cred, /* cred is ignored */ 3187 ns_ldap_error_t **errorp) 3188 { 3189 ns_ldap_result_t *result = NULL; 3190 char *filter, *userdata; 3191 char errstr[MAXERROR]; 3192 char **value; 3193 int rc = 0; 3194 int i = 0; 3195 size_t len; 3196 3197 *errorp = NULL; 3198 *userDN = NULL; 3199 if ((uid == NULL) || (uid[0] == '\0')) 3200 return (NS_LDAP_INVALID_PARAM); 3201 3202 while (uid[i] != '\0') { 3203 if (uid[i] == '=') { 3204 *userDN = strdup(uid); 3205 return (NS_LDAP_SUCCESS); 3206 } 3207 i++; 3208 } 3209 i = 0; 3210 while ((uid[i] != '\0') && (isdigit(uid[i]))) 3211 i++; 3212 if (uid[i] == '\0') { 3213 len = strlen(UIDNUMFILTER) + strlen(uid) + 1; 3214 filter = (char *)malloc(len); 3215 if (filter == NULL) { 3216 *userDN = NULL; 3217 return (NS_LDAP_MEMORY); 3218 } 3219 (void) snprintf(filter, len, UIDNUMFILTER, uid); 3220 3221 len = strlen(UIDNUMFILTER_SSD) + strlen(uid) + 1; 3222 userdata = (char *)malloc(len); 3223 if (userdata == NULL) { 3224 *userDN = NULL; 3225 return (NS_LDAP_MEMORY); 3226 } 3227 (void) snprintf(userdata, len, UIDNUMFILTER_SSD, uid); 3228 } else { 3229 len = strlen(UIDFILTER) + strlen(uid) + 1; 3230 filter = (char *)malloc(len); 3231 if (filter == NULL) { 3232 *userDN = NULL; 3233 return (NS_LDAP_MEMORY); 3234 } 3235 (void) snprintf(filter, len, UIDFILTER, uid); 3236 3237 len = strlen(UIDFILTER_SSD) + strlen(uid) + 1; 3238 userdata = (char *)malloc(len); 3239 if (userdata == NULL) { 3240 *userDN = NULL; 3241 return (NS_LDAP_MEMORY); 3242 } 3243 (void) snprintf(userdata, len, UIDFILTER_SSD, uid); 3244 } 3245 3246 /* 3247 * we want to retrieve the DN as it appears in LDAP 3248 * hence the use of NS_LDAP_NOT_CVT_DN in flags 3249 */ 3250 rc = __ns_ldap_list("passwd", filter, 3251 __s_api_merge_SSD_filter, 3252 NULL, cred, NS_LDAP_NOT_CVT_DN, 3253 &result, errorp, NULL, 3254 userdata); 3255 free(filter); 3256 filter = NULL; 3257 free(userdata); 3258 userdata = NULL; 3259 if (rc != NS_LDAP_SUCCESS) { 3260 if (result) { 3261 (void) __ns_ldap_freeResult(&result); 3262 result = NULL; 3263 } 3264 return (rc); 3265 } 3266 if (result->entries_count > 1) { 3267 (void) __ns_ldap_freeResult(&result); 3268 result = NULL; 3269 *userDN = NULL; 3270 (void) sprintf(errstr, 3271 gettext("Too many entries are returned for %s"), uid); 3272 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, strdup(errstr), 3273 NULL); 3274 return (NS_LDAP_INTERNAL); 3275 } 3276 3277 value = __ns_ldap_getAttr(result->entry, "dn"); 3278 *userDN = strdup(value[0]); 3279 (void) __ns_ldap_freeResult(&result); 3280 result = NULL; 3281 return (NS_LDAP_SUCCESS); 3282 } 3283 3284 3285 /*ARGSUSED*/ 3286 int 3287 __ns_ldap_host2dn(const char *host, 3288 const char *domain, 3289 char **hostDN, 3290 const ns_cred_t *cred, /* cred is ignored */ 3291 ns_ldap_error_t **errorp) 3292 { 3293 ns_ldap_result_t *result = NULL; 3294 char *filter, *userdata; 3295 char errstr[MAXERROR]; 3296 char **value; 3297 int rc; 3298 size_t len; 3299 3300 /* 3301 * XXX 3302 * the domain parameter needs to be used in case domain is not local, if 3303 * this routine is to support multi domain setups, it needs lots of work... 3304 */ 3305 *errorp = NULL; 3306 *hostDN = NULL; 3307 if ((host == NULL) || (host[0] == '\0')) 3308 return (NS_LDAP_INVALID_PARAM); 3309 3310 len = strlen(HOSTFILTER) + strlen(host) + 1; 3311 filter = (char *)malloc(len); 3312 if (filter == NULL) { 3313 return (NS_LDAP_MEMORY); 3314 } 3315 (void) snprintf(filter, len, HOSTFILTER, host); 3316 3317 len = strlen(HOSTFILTER_SSD) + strlen(host) + 1; 3318 userdata = (char *)malloc(len); 3319 if (userdata == NULL) { 3320 return (NS_LDAP_MEMORY); 3321 } 3322 (void) snprintf(userdata, len, HOSTFILTER_SSD, host); 3323 3324 /* 3325 * we want to retrieve the DN as it appears in LDAP 3326 * hence the use of NS_LDAP_NOT_CVT_DN in flags 3327 */ 3328 rc = __ns_ldap_list("hosts", filter, 3329 __s_api_merge_SSD_filter, 3330 NULL, cred, NS_LDAP_NOT_CVT_DN, &result, 3331 errorp, NULL, 3332 userdata); 3333 free(filter); 3334 filter = NULL; 3335 free(userdata); 3336 userdata = NULL; 3337 if (rc != NS_LDAP_SUCCESS) { 3338 if (result) { 3339 (void) __ns_ldap_freeResult(&result); 3340 result = NULL; 3341 } 3342 return (rc); 3343 } 3344 3345 if (result->entries_count > 1) { 3346 (void) __ns_ldap_freeResult(&result); 3347 result = NULL; 3348 *hostDN = NULL; 3349 (void) sprintf(errstr, 3350 gettext("Too many entries are returned for %s"), host); 3351 MKERROR(LOG_WARNING, *errorp, NS_LDAP_INTERNAL, strdup(errstr), 3352 NULL); 3353 return (NS_LDAP_INTERNAL); 3354 } 3355 3356 value = __ns_ldap_getAttr(result->entry, "dn"); 3357 *hostDN = strdup(value[0]); 3358 (void) __ns_ldap_freeResult(&result); 3359 result = NULL; 3360 return (NS_LDAP_SUCCESS); 3361 } 3362 3363 /*ARGSUSED*/ 3364 int 3365 __ns_ldap_dn2domain(const char *dn, 3366 char **domain, 3367 const ns_cred_t *cred, 3368 ns_ldap_error_t **errorp) 3369 { 3370 int rc, pnum, i, j, len = 0; 3371 char *newdn, **rdns = NULL; 3372 char **dns, *dn1; 3373 3374 *errorp = NULL; 3375 3376 if (domain == NULL) 3377 return (NS_LDAP_INVALID_PARAM); 3378 else 3379 *domain = NULL; 3380 3381 if ((dn == NULL) || (dn[0] == '\0')) 3382 return (NS_LDAP_INVALID_PARAM); 3383 3384 /* 3385 * break dn into rdns 3386 */ 3387 dn1 = strdup(dn); 3388 if (dn1 == NULL) 3389 return (NS_LDAP_MEMORY); 3390 rdns = ldap_explode_dn(dn1, 0); 3391 free(dn1); 3392 if (rdns == NULL || *rdns == NULL) 3393 return (NS_LDAP_INVALID_PARAM); 3394 3395 for (i = 0; rdns[i]; i++) 3396 len += strlen(rdns[i]) + 1; 3397 pnum = i; 3398 3399 newdn = (char *)malloc(len + 1); 3400 dns = (char **)calloc(pnum, sizeof (char *)); 3401 if (newdn == NULL || dns == NULL) { 3402 if (newdn) 3403 free(newdn); 3404 ldap_value_free(rdns); 3405 return (NS_LDAP_MEMORY); 3406 } 3407 3408 /* construct a semi-normalized dn, newdn */ 3409 *newdn = '\0'; 3410 for (i = 0; rdns[i]; i++) { 3411 dns[i] = newdn + strlen(newdn); 3412 (void) strcat(newdn, 3413 __s_api_remove_rdn_space(rdns[i])); 3414 (void) strcat(newdn, ","); 3415 } 3416 /* remove the last ',' */ 3417 newdn[strlen(newdn) - 1] = '\0'; 3418 ldap_value_free(rdns); 3419 3420 /* 3421 * loop and find the domain name associated with newdn, 3422 * removing rdn one by one from left to right 3423 */ 3424 for (i = 0; i < pnum; i++) { 3425 3426 if (*errorp) 3427 (void) __ns_ldap_freeError(errorp); 3428 3429 /* 3430 * try cache manager first 3431 */ 3432 rc = __s_api_get_cachemgr_data(NS_CACHE_DN2DOMAIN, 3433 dns[i], domain); 3434 if (rc != NS_LDAP_SUCCESS) { 3435 /* 3436 * try ldap server second 3437 */ 3438 rc = __s_api_find_domainname(dns[i], domain, 3439 cred, errorp); 3440 } else { 3441 /* 3442 * skip the last one, 3443 * since it is already cached by ldap_cachemgr 3444 */ 3445 i--; 3446 } 3447 if (rc == NS_LDAP_SUCCESS) { 3448 if (__s_api_nscd_proc()) { 3449 /* 3450 * If it's nscd, ask cache manager to save the 3451 * dn to domain mapping(s) 3452 */ 3453 for (j = 0; j <= i; j++) { 3454 (void) __s_api_set_cachemgr_data( 3455 NS_CACHE_DN2DOMAIN, 3456 dns[j], 3457 *domain); 3458 } 3459 } 3460 break; 3461 } 3462 } 3463 3464 free(dns); 3465 free(newdn); 3466 if (rc != NS_LDAP_SUCCESS) 3467 rc = NS_LDAP_NOTFOUND; 3468 return (rc); 3469 } 3470 3471 /*ARGSUSED*/ 3472 int 3473 __ns_ldap_getServiceAuthMethods(const char *service, 3474 ns_auth_t ***auth, 3475 ns_ldap_error_t **errorp) 3476 { 3477 char errstr[MAXERROR]; 3478 int rc, i, done = 0; 3479 int slen; 3480 void **param; 3481 char **sam, *srv, *send; 3482 ns_auth_t **authpp = NULL, *ap; 3483 int cnt, max; 3484 ns_config_t *cfg; 3485 ns_ldap_error_t *error = NULL; 3486 3487 if (errorp == NULL) 3488 return (NS_LDAP_INVALID_PARAM); 3489 *errorp = NULL; 3490 3491 if ((service == NULL) || (service[0] == '\0') || 3492 (auth == NULL)) 3493 return (NS_LDAP_INVALID_PARAM); 3494 3495 *auth = NULL; 3496 rc = __ns_ldap_getParam(NS_LDAP_SERVICE_AUTH_METHOD_P, ¶m, &error); 3497 if (rc != NS_LDAP_SUCCESS || param == NULL) { 3498 *errorp = error; 3499 return (rc); 3500 } 3501 sam = (char **)param; 3502 3503 cfg = __s_api_get_default_config(); 3504 cnt = 0; 3505 3506 slen = strlen(service); 3507 3508 for (; *sam; sam++) { 3509 srv = *sam; 3510 if (strncasecmp(service, srv, slen) != 0) 3511 continue; 3512 srv += slen; 3513 if (*srv != COLONTOK) 3514 continue; 3515 send = srv; 3516 srv++; 3517 for (max = 1; (send = strchr(++send, SEMITOK)) != NULL; 3518 max++) {} 3519 authpp = (ns_auth_t **)calloc(++max, sizeof (ns_auth_t *)); 3520 if (authpp == NULL) { 3521 (void) __ns_ldap_freeParam(¶m); 3522 __s_api_release_config(cfg); 3523 return (NS_LDAP_MEMORY); 3524 } 3525 while (!done) { 3526 send = strchr(srv, SEMITOK); 3527 if (send != NULL) { 3528 *send = '\0'; 3529 send++; 3530 } 3531 i = __s_get_enum_value(cfg, srv, NS_LDAP_AUTH_P); 3532 if (i == -1) { 3533 (void) __ns_ldap_freeParam(¶m); 3534 (void) sprintf(errstr, 3535 gettext("Unsupported " 3536 "serviceAuthenticationMethod: %s.\n"), srv); 3537 MKERROR(LOG_WARNING, *errorp, NS_CONFIG_SYNTAX, 3538 strdup(errstr), NULL); 3539 __s_api_release_config(cfg); 3540 return (NS_LDAP_CONFIG); 3541 } 3542 ap = __s_api_AuthEnumtoStruct((EnumAuthType_t)i); 3543 if (ap == NULL) { 3544 (void) __ns_ldap_freeParam(¶m); 3545 __s_api_release_config(cfg); 3546 return (NS_LDAP_MEMORY); 3547 } 3548 authpp[cnt++] = ap; 3549 if (send == NULL) 3550 done = TRUE; 3551 else 3552 srv = send; 3553 } 3554 } 3555 3556 *auth = authpp; 3557 (void) __ns_ldap_freeParam(¶m); 3558 __s_api_release_config(cfg); 3559 return (NS_LDAP_SUCCESS); 3560 } 3561 3562 /* 3563 * This routine is called when certain scenario occurs 3564 * e.g. 3565 * service == auto_home 3566 * SSD = automount: ou = mytest, 3567 * NS_LDAP_MAPATTRIBUTE= auto_home: automountMapName=AAA 3568 * NS_LDAP_OBJECTCLASSMAP= auto_home:automountMap=MynisMap 3569 * NS_LDAP_OBJECTCLASSMAP= auto_home:automount=MynisObject 3570 * 3571 * The automountMapName is prepended implicitely but is mapped 3572 * to AAA. So dn could appers as 3573 * dn: AAA=auto_home,ou=bar,dc=foo,dc=com 3574 * dn: automountKey=user_01,AAA=auto_home,ou=bar,dc=foo,dc=com 3575 * dn: automountKey=user_02,AAA=auto_home,ou=bar,dc=foo,dc=com 3576 * in the directory. 3577 * This function is called to covert the mapped attr back to 3578 * orig attr when the entries are searched and returned 3579 */ 3580 3581 int 3582 __s_api_convert_automountmapname(const char *service, char **dn, 3583 ns_ldap_error_t **errp) { 3584 3585 char **mapping = NULL; 3586 char *mapped_attr = NULL; 3587 char *automountmapname = "automountMapName"; 3588 char *buffer = NULL; 3589 int rc = NS_LDAP_SUCCESS; 3590 char errstr[MAXERROR]; 3591 3592 /* 3593 * dn is an input/out parameter, check it first 3594 */ 3595 3596 if (service == NULL || dn == NULL || *dn == NULL) 3597 return (NS_LDAP_INVALID_PARAM); 3598 3599 /* 3600 * Check to see if there is a mapped attribute for auto_xxx 3601 */ 3602 3603 mapping = __ns_ldap_getMappedAttributes(service, automountmapname); 3604 3605 /* 3606 * if no mapped attribute for auto_xxx, try automount 3607 */ 3608 3609 if (mapping == NULL) 3610 mapping = __ns_ldap_getMappedAttributes( 3611 "automount", automountmapname); 3612 3613 /* 3614 * if no mapped attribute is found, return SUCCESS (no op) 3615 */ 3616 3617 if (mapping == NULL) 3618 return (NS_LDAP_SUCCESS); 3619 3620 /* 3621 * if the mapped attribute is found and attr is not empty, 3622 * copy it 3623 */ 3624 3625 if (mapping[0] != NULL) { 3626 mapped_attr = strdup(mapping[0]); 3627 __s_api_free2dArray(mapping); 3628 if (mapped_attr == NULL) { 3629 return (NS_LDAP_MEMORY); 3630 } 3631 } else { 3632 __s_api_free2dArray(mapping); 3633 3634 (void) snprintf(errstr, (2 * MAXERROR), 3635 gettext( 3636 "Attribute nisMapName is mapped to an " 3637 "empty string.\n")); 3638 3639 MKERROR(LOG_ERR, *errp, NS_CONFIG_SYNTAX, 3640 strdup(errstr), NULL); 3641 3642 return (NS_LDAP_CONFIG); 3643 } 3644 3645 /* 3646 * Locate the mapped attribute in the dn 3647 * and replace it if it exists 3648 */ 3649 3650 rc = __s_api_replace_mapped_attr_in_dn( 3651 (const char *) automountmapname, (const char *) mapped_attr, 3652 (const char *) *dn, &buffer); 3653 3654 /* clean up */ 3655 3656 free(mapped_attr); 3657 3658 /* 3659 * If mapped attr is found(buffer != NULL) 3660 * a new dn is returned 3661 * If no mapped attribute is in dn, 3662 * return NS_LDAP_SUCCESS (no op) 3663 * If no memory, 3664 * return NS_LDAP_MEMORY (no op) 3665 */ 3666 3667 if (buffer != NULL) { 3668 free(*dn); 3669 *dn = buffer; 3670 } 3671 3672 return (rc); 3673 } 3674 3675 /* 3676 * If the mapped attr is found in the dn, 3677 * return NS_LDAP_SUCCESS and a new_dn. 3678 * If no mapped attr is found, 3679 * return NS_LDAP_SUCCESS and *new_dn == NULL 3680 * If there is not enough memory, 3681 * return NS_LDAP_MEMORY and *new_dn == NULL 3682 */ 3683 3684 int 3685 __s_api_replace_mapped_attr_in_dn( 3686 const char *orig_attr, const char *mapped_attr, 3687 const char *dn, char **new_dn) { 3688 3689 char **dnArray = NULL; 3690 char *cur = NULL, *start = NULL; 3691 int i = 0, found = 0; 3692 int len = 0, orig_len = 0, mapped_len = 0; 3693 int dn_len = 0, tmp_len = 0; 3694 3695 *new_dn = NULL; 3696 3697 /* 3698 * seperate dn into individual componets 3699 * e.g. 3700 * "automountKey=user_01" , "automountMapName_test=auto_home", ... 3701 */ 3702 dnArray = ldap_explode_dn(dn, 0); 3703 3704 /* 3705 * This will find "mapped attr=value" in dn. 3706 * It won't find match if mapped attr appears 3707 * in the value. 3708 */ 3709 for (i = 0; dnArray[i] != NULL; i++) { 3710 /* 3711 * This function is called when reading from 3712 * the directory so assume each component has "=". 3713 * Any ill formatted dn should be rejected 3714 * before adding to the directory 3715 */ 3716 cur = strchr(dnArray[i], '='); 3717 *cur = '\0'; 3718 if (strcasecmp(mapped_attr, dnArray[i]) == 0) 3719 found = 1; 3720 *cur = '='; 3721 if (found) break; 3722 } 3723 3724 if (!found) { 3725 __s_api_free2dArray(dnArray); 3726 *new_dn = NULL; 3727 return (NS_LDAP_SUCCESS); 3728 } 3729 /* 3730 * The new length is *dn length + (difference between 3731 * orig attr and mapped attr) + 1 ; 3732 * e.g. 3733 * automountKey=aa,automountMapName_test=auto_home,dc=foo,dc=com 3734 * ==> 3735 * automountKey=aa,automountMapName=auto_home,dc=foo,dc=com 3736 */ 3737 mapped_len = strlen(mapped_attr); 3738 orig_len = strlen(orig_attr); 3739 dn_len = strlen(dn); 3740 len = dn_len + orig_len - mapped_len + 1; 3741 *new_dn = (char *)calloc(1, len); 3742 if (*new_dn == NULL) { 3743 __s_api_free2dArray(dnArray); 3744 return (NS_LDAP_MEMORY); 3745 } 3746 3747 /* 3748 * Locate the mapped attr in the dn. 3749 * Use dnArray[i] instead of mapped_attr 3750 * because mapped_attr could appear in 3751 * the value 3752 */ 3753 3754 cur = strstr(dn, dnArray[i]); 3755 __s_api_free2dArray(dnArray); 3756 /* copy the portion before mapped attr in dn */ 3757 start = *new_dn; 3758 tmp_len = cur - dn; 3759 (void) memcpy((void *) start, (const void*) dn, tmp_len); 3760 3761 /* 3762 * Copy the orig_attr. e.g. automountMapName 3763 * This replaces mapped attr with orig attr 3764 */ 3765 start = start + (cur - dn); /* move cursor in buffer */ 3766 (void) memcpy((void *) start, (const void*) orig_attr, orig_len); 3767 3768 /* 3769 * Copy the portion after mapped attr in dn 3770 */ 3771 cur = cur + mapped_len; /* move cursor in dn */ 3772 start = start + orig_len; /* move cursor in buffer */ 3773 (void) strcpy(start, cur); 3774 3775 return (NS_LDAP_SUCCESS); 3776 } 3777 3778 /* 3779 * Validate Filter functions 3780 */ 3781 3782 /* ***** Start of modified libldap.so.5 filter parser ***** */ 3783 3784 /* filter parsing routine forward references */ 3785 static int adj_filter_list(char *str); 3786 static int adj_simple_filter(char *str); 3787 static int unescape_filterval(char *val); 3788 static int hexchar2int(char c); 3789 static int adj_substring_filter(char *val); 3790 3791 3792 /* 3793 * assumes string manipulation is in-line 3794 * and all strings are sufficient in size 3795 * return value is the position after 'c' 3796 */ 3797 3798 static char * 3799 resync_str(char *str, char *next, char c) 3800 { 3801 char *ret; 3802 3803 ret = str + strlen(str); 3804 *next = c; 3805 if (ret == next) 3806 return (ret); 3807 (void) strcat(str, next); 3808 return (ret); 3809 } 3810 3811 static char * 3812 find_right_paren(char *s) 3813 { 3814 int balance, escape; 3815 3816 balance = 1; 3817 escape = 0; 3818 while (*s && balance) { 3819 if (escape == 0) { 3820 if (*s == '(') 3821 balance++; 3822 else if (*s == ')') 3823 balance--; 3824 } 3825 if (*s == '\\' && ! escape) 3826 escape = 1; 3827 else 3828 escape = 0; 3829 if (balance) 3830 s++; 3831 } 3832 3833 return (*s ? s : NULL); 3834 } 3835 3836 static char * 3837 adj_complex_filter(char *str) 3838 { 3839 char *next; 3840 3841 /* 3842 * We have (x(filter)...) with str sitting on 3843 * the x. We have to find the paren matching 3844 * the one before the x and put the intervening 3845 * filters by calling adj_filter_list(). 3846 */ 3847 3848 str++; 3849 if ((next = find_right_paren(str)) == NULL) 3850 return (NULL); 3851 3852 *next = '\0'; 3853 if (adj_filter_list(str) == -1) 3854 return (NULL); 3855 next = resync_str(str, next, ')'); 3856 next++; 3857 3858 return (next); 3859 } 3860 3861 static int 3862 adj_filter(char *str) 3863 { 3864 char *next; 3865 int parens, balance, escape; 3866 char *np, *cp, *dp; 3867 3868 parens = 0; 3869 while (*str) { 3870 switch (*str) { 3871 case '(': 3872 str++; 3873 parens++; 3874 switch (*str) { 3875 case '&': 3876 if ((str = adj_complex_filter(str)) == NULL) 3877 return (-1); 3878 3879 parens--; 3880 break; 3881 3882 case '|': 3883 if ((str = adj_complex_filter(str)) == NULL) 3884 return (-1); 3885 3886 parens--; 3887 break; 3888 3889 case '!': 3890 if ((str = adj_complex_filter(str)) == NULL) 3891 return (-1); 3892 3893 parens--; 3894 break; 3895 3896 case '(': 3897 /* illegal ((case - generated by conversion */ 3898 3899 /* find missing close) */ 3900 np = find_right_paren(str+1); 3901 3902 /* error if not found */ 3903 if (np == NULL) 3904 return (-1); 3905 3906 /* remove redundant (and) */ 3907 for (dp = str, cp = str+1; cp < np; ) { 3908 *dp++ = *cp++; 3909 } 3910 cp++; 3911 while (*cp) 3912 *dp++ = *cp++; 3913 *dp = '\0'; 3914 3915 /* re-start test at original ( */ 3916 parens--; 3917 str--; 3918 break; 3919 3920 default: 3921 balance = 1; 3922 escape = 0; 3923 next = str; 3924 while (*next && balance) { 3925 if (escape == 0) { 3926 if (*next == '(') 3927 balance++; 3928 else if (*next == ')') 3929 balance--; 3930 } 3931 if (*next == '\\' && ! escape) 3932 escape = 1; 3933 else 3934 escape = 0; 3935 if (balance) 3936 next++; 3937 } 3938 if (balance != 0) 3939 return (-1); 3940 3941 *next = '\0'; 3942 if (adj_simple_filter(str) == -1) { 3943 return (-1); 3944 } 3945 next = resync_str(str, next, ')'); 3946 next++; 3947 str = next; 3948 parens--; 3949 break; 3950 } 3951 break; 3952 3953 case ')': 3954 str++; 3955 parens--; 3956 break; 3957 3958 case ' ': 3959 str++; 3960 break; 3961 3962 default: /* assume it's a simple type=value filter */ 3963 next = strchr(str, '\0'); 3964 if (adj_simple_filter(str) == -1) { 3965 return (-1); 3966 } 3967 str = next; 3968 break; 3969 } 3970 } 3971 3972 return (parens ? -1 : 0); 3973 } 3974 3975 3976 /* 3977 * Put a list of filters like this "(filter1)(filter2)..." 3978 */ 3979 3980 static int 3981 adj_filter_list(char *str) 3982 { 3983 char *next; 3984 char save; 3985 3986 while (*str) { 3987 while (*str && isspace(*str)) 3988 str++; 3989 if (*str == '\0') 3990 break; 3991 3992 if ((next = find_right_paren(str + 1)) == NULL) 3993 return (-1); 3994 save = *++next; 3995 3996 /* now we have "(filter)" with str pointing to it */ 3997 *next = '\0'; 3998 if (adj_filter(str) == -1) 3999 return (-1); 4000 next = resync_str(str, next, save); 4001 4002 str = next; 4003 } 4004 4005 return (0); 4006 } 4007 4008 4009 /* 4010 * is_valid_attr - returns 1 if a is a syntactically valid left-hand side 4011 * of a filter expression, 0 otherwise. A valid string may contain only 4012 * letters, numbers, hyphens, semi-colons, colons and periods. examples: 4013 * cn 4014 * cn;lang-fr 4015 * 1.2.3.4;binary;dynamic 4016 * mail;dynamic 4017 * cn:dn:1.2.3.4 4018 * 4019 * For compatibility with older servers, we also allow underscores in 4020 * attribute types, even through they are not allowed by the LDAPv3 RFCs. 4021 */ 4022 static int 4023 is_valid_attr(char *a) 4024 { 4025 for (; *a; a++) { 4026 if (!isascii(*a)) { 4027 return (0); 4028 } else if (!isalnum(*a)) { 4029 switch (*a) { 4030 case '-': 4031 case '.': 4032 case ';': 4033 case ':': 4034 case '_': 4035 break; /* valid */ 4036 default: 4037 return (0); 4038 } 4039 } 4040 } 4041 return (1); 4042 } 4043 4044 static char * 4045 find_star(char *s) 4046 { 4047 for (; *s; ++s) { 4048 switch (*s) { 4049 case '*': 4050 return (s); 4051 case '\\': 4052 ++s; 4053 if (hexchar2int(s[0]) >= 0 && hexchar2int(s[1]) >= 0) 4054 ++s; 4055 default: 4056 break; 4057 } 4058 } 4059 return (NULL); 4060 } 4061 4062 static int 4063 adj_simple_filter(char *str) 4064 { 4065 char *s, *s2, *s3, filterop; 4066 char *value; 4067 int ftype = 0; 4068 int rc; 4069 4070 rc = -1; /* pessimistic */ 4071 4072 if ((str = strdup(str)) == NULL) { 4073 return (rc); 4074 } 4075 4076 if ((s = strchr(str, '=')) == NULL) { 4077 goto free_and_return; 4078 } 4079 value = s + 1; 4080 *s-- = '\0'; 4081 filterop = *s; 4082 if (filterop == '<' || filterop == '>' || filterop == '~' || 4083 filterop == ':') { 4084 *s = '\0'; 4085 } 4086 4087 if (! is_valid_attr(str)) { 4088 goto free_and_return; 4089 } 4090 4091 switch (filterop) { 4092 case '<': /* LDAP_FILTER_LE */ 4093 case '>': /* LDAP_FILTER_GE */ 4094 case '~': /* LDAP_FILTER_APPROX */ 4095 break; 4096 case ':': /* extended filter - v3 only */ 4097 /* 4098 * extended filter looks like this: 4099 * 4100 * [type][':dn'][':'oid]':='value 4101 * 4102 * where one of type or :oid is required. 4103 * 4104 */ 4105 s2 = s3 = NULL; 4106 if ((s2 = strrchr(str, ':')) == NULL) { 4107 goto free_and_return; 4108 } 4109 if (strcasecmp(s2, ":dn") == 0) { 4110 *s2 = '\0'; 4111 } else { 4112 *s2 = '\0'; 4113 if ((s3 = strrchr(str, ':')) != NULL) { 4114 if (strcasecmp(s3, ":dn") != 0) { 4115 goto free_and_return; 4116 } 4117 *s3 = '\0'; 4118 } 4119 } 4120 if (unescape_filterval(value) < 0) { 4121 goto free_and_return; 4122 } 4123 rc = 0; 4124 goto free_and_return; 4125 /* break; */ 4126 default: 4127 if (find_star(value) == NULL) { 4128 ftype = 0; /* LDAP_FILTER_EQUALITY */ 4129 } else if (strcmp(value, "*") == 0) { 4130 ftype = 1; /* LDAP_FILTER_PRESENT */ 4131 } else { 4132 rc = adj_substring_filter(value); 4133 goto free_and_return; 4134 } 4135 break; 4136 } 4137 4138 if (ftype != 0) { /* == LDAP_FILTER_PRESENT */ 4139 rc = 0; 4140 } else if (unescape_filterval(value) >= 0) { 4141 rc = 0; 4142 } 4143 if (rc != -1) { 4144 rc = 0; 4145 } 4146 4147 free_and_return: 4148 free(str); 4149 return (rc); 4150 } 4151 4152 4153 /* 4154 * Check in place both LDAPv2 (RFC-1960) and LDAPv3 (hexadecimal) escape 4155 * sequences within the null-terminated string 'val'. 4156 * 4157 * If 'val' contains invalid escape sequences we return -1. 4158 * Otherwise return 1 4159 */ 4160 static int 4161 unescape_filterval(char *val) 4162 { 4163 int escape, firstdigit; 4164 char *s; 4165 4166 firstdigit = 0; 4167 escape = 0; 4168 for (s = val; *s; s++) { 4169 if (escape) { 4170 /* 4171 * first try LDAPv3 escape (hexadecimal) sequence 4172 */ 4173 if (hexchar2int(*s) < 0) { 4174 if (firstdigit) { 4175 /* 4176 * LDAPv2 (RFC1960) escape sequence 4177 */ 4178 escape = 0; 4179 } else { 4180 return (-1); 4181 } 4182 } 4183 if (firstdigit) { 4184 firstdigit = 0; 4185 } else { 4186 escape = 0; 4187 } 4188 4189 } else if (*s != '\\') { 4190 escape = 0; 4191 4192 } else { 4193 escape = 1; 4194 firstdigit = 1; 4195 } 4196 } 4197 4198 return (1); 4199 } 4200 4201 4202 /* 4203 * convert character 'c' that represents a hexadecimal digit to an integer. 4204 * if 'c' is not a hexidecimal digit [0-9A-Fa-f], -1 is returned. 4205 * otherwise the converted value is returned. 4206 */ 4207 static int 4208 hexchar2int(char c) 4209 { 4210 if (c >= '0' && c <= '9') { 4211 return (c - '0'); 4212 } 4213 if (c >= 'A' && c <= 'F') { 4214 return (c - 'A' + 10); 4215 } 4216 if (c >= 'a' && c <= 'f') { 4217 return (c - 'a' + 10); 4218 } 4219 return (-1); 4220 } 4221 4222 static int 4223 adj_substring_filter(char *val) 4224 { 4225 char *nextstar; 4226 4227 for (; val != NULL; val = nextstar) { 4228 if ((nextstar = find_star(val)) != NULL) { 4229 *nextstar++ = '\0'; 4230 } 4231 4232 if (*val != '\0') { 4233 if (unescape_filterval(val) < 0) { 4234 return (-1); 4235 } 4236 } 4237 } 4238 4239 return (0); 4240 } 4241 4242 /* ***** End of modified libldap.so.5 filter parser ***** */ 4243 4244 4245 /* 4246 * Walk filter, remove redundant parentheses in-line 4247 * verify that the filter is reasonable 4248 */ 4249 static int 4250 validate_filter(ns_ldap_cookie_t *cookie) 4251 { 4252 char *filter = cookie->filter; 4253 int rc; 4254 4255 /* Parse filter looking for illegal values */ 4256 4257 rc = adj_filter(filter); 4258 if (rc != 0) { 4259 return (NS_LDAP_OP_FAILED); 4260 } 4261 4262 /* end of filter checking */ 4263 4264 return (NS_LDAP_SUCCESS); 4265 } 4266 4267 /* 4268 * Set the account management request control that needs to be sent to server. 4269 * This control is required to get the account management information of 4270 * a user to do local account checking. 4271 */ 4272 static int 4273 setup_acctmgmt_params(ns_ldap_cookie_t *cookie) 4274 { 4275 LDAPControl *req = NULL, **requestctrls; 4276 4277 req = (LDAPControl *)malloc(sizeof (LDAPControl)); 4278 4279 if (req == NULL) 4280 return (NS_LDAP_MEMORY); 4281 4282 /* fill in the fields of this new control */ 4283 req->ldctl_iscritical = 1; 4284 req->ldctl_oid = strdup(NS_LDAP_ACCOUNT_USABLE_CONTROL); 4285 if (req->ldctl_oid == NULL) { 4286 free(req); 4287 return (NS_LDAP_MEMORY); 4288 } 4289 req->ldctl_value.bv_len = 0; 4290 req->ldctl_value.bv_val = NULL; 4291 4292 requestctrls = (LDAPControl **)calloc(2, sizeof (LDAPControl *)); 4293 if (requestctrls == NULL) { 4294 ldap_control_free(req); 4295 return (NS_LDAP_MEMORY); 4296 } 4297 4298 requestctrls[0] = req; 4299 4300 cookie->p_serverctrls = requestctrls; 4301 4302 return (NS_LDAP_SUCCESS); 4303 } 4304 4305 /* 4306 * int get_new_acct_more_info(BerElement *ber, 4307 * AcctUsableResponse_t *acctResp) 4308 * 4309 * Decode the more_info data from an Account Management control response, 4310 * when the account is not usable and when code style is from recent LDAP 4311 * servers (see below comments for parse_acct_cont_resp_msg() to get more 4312 * details on coding styles and ASN1 description). 4313 * 4314 * Expected BER encoding: {tbtbtbtiti} 4315 * +t: tag is 0 4316 * +b: TRUE if inactive due to account inactivation 4317 * +t: tag is 1 4318 * +b: TRUE if password has been reset 4319 * +t: tag is 2 4320 * +b: TRUE if password is expired 4321 * +t: tag is 3 4322 * +i: contains num of remaining grace, 0 means no grace 4323 * +t: tag is 4 4324 * +i: contains num of seconds before auto-unlock. -1 means acct is locked 4325 * forever (i.e. until reset) 4326 * 4327 * Asumptions: 4328 * - ber is not null 4329 * - acctResp is not null and is initialized with default values for the 4330 * fields in its AcctUsableResp.more_info structure 4331 * - the ber stream is received in the correct order, per the ASN1 description. 4332 * We do not check this order and make the asumption that it is correct. 4333 * Note that the ber stream may not (and will not in most cases) contain 4334 * all fields. 4335 */ 4336 static int 4337 get_new_acct_more_info(BerElement *ber, AcctUsableResponse_t *acctResp) 4338 { 4339 int rc = NS_LDAP_SUCCESS; 4340 char errstr[MAXERROR]; 4341 ber_tag_t rTag = LBER_DEFAULT; 4342 ber_len_t rLen = 0; 4343 ber_int_t rValue; 4344 char *last; 4345 int berRC = 0; 4346 4347 /* 4348 * Look at what more_info BER element is/are left to be decoded. 4349 * look at each of them 1 by 1, without checking on their order 4350 * and possible multi values. 4351 */ 4352 for (rTag = ber_first_element(ber, &rLen, &last); 4353 rTag != LBER_END_OF_SEQORSET; 4354 rTag = ber_next_element(ber, &rLen, last)) { 4355 4356 berRC = 0; 4357 switch (rTag) { 4358 case 0 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4359 /* inactive */ 4360 berRC = ber_scanf(ber, "b", &rValue); 4361 if (berRC != LBER_ERROR) { 4362 (acctResp->AcctUsableResp).more_info. 4363 inactive = (rValue != 0) ? 1 : 0; 4364 } 4365 break; 4366 4367 case 1 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4368 /* reset */ 4369 berRC = ber_scanf(ber, "b", &rValue); 4370 if (berRC != LBER_ERROR) { 4371 (acctResp->AcctUsableResp).more_info.reset 4372 = (rValue != 0) ? 1 : 0; 4373 } 4374 break; 4375 4376 case 2 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4377 /* expired */ 4378 berRC = ber_scanf(ber, "b", &rValue); 4379 if (berRC != LBER_ERROR) { 4380 (acctResp->AcctUsableResp).more_info.expired 4381 = (rValue != 0) ? 1 : 0; 4382 } 4383 break; 4384 4385 case 3 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4386 /* remaining grace */ 4387 berRC = ber_scanf(ber, "i", &rValue); 4388 if (berRC != LBER_ERROR) { 4389 (acctResp->AcctUsableResp).more_info.rem_grace 4390 = rValue; 4391 } 4392 break; 4393 4394 case 4 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4395 /* seconds before unlock */ 4396 berRC = ber_scanf(ber, "i", &rValue); 4397 if (berRC != LBER_ERROR) { 4398 (acctResp->AcctUsableResp).more_info. 4399 sec_b4_unlock = rValue; 4400 } 4401 break; 4402 4403 default : 4404 (void) sprintf(errstr, 4405 gettext("invalid reason tag 0x%x"), rTag); 4406 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4407 rc = NS_LDAP_INTERNAL; 4408 break; 4409 } 4410 if (berRC == LBER_ERROR) { 4411 (void) sprintf(errstr, 4412 gettext("error 0x%x decoding value for " 4413 "tag 0x%x"), berRC, rTag); 4414 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4415 rc = NS_LDAP_INTERNAL; 4416 } 4417 if (rc != NS_LDAP_SUCCESS) { 4418 /* exit the for loop */ 4419 break; 4420 } 4421 } 4422 4423 return (rc); 4424 } 4425 4426 /* 4427 * int get_old_acct_opt_more_info(BerElement *ber, 4428 * AcctUsableResponse_t *acctResp) 4429 * 4430 * Decode the optional more_info data from an Account Management control 4431 * response, when the account is not usable and when code style is from LDAP 4432 * server 5.2p4 (see below comments for parse_acct_cont_resp_msg() to get more 4433 * details on coding styles and ASN1 description). 4434 * 4435 * Expected BER encoding: titi} 4436 * +t: tag is 2 4437 * +i: contains num of remaining grace, 0 means no grace 4438 * +t: tag is 3 4439 * +i: contains num of seconds before auto-unlock. -1 means acct is locked 4440 * forever (i.e. until reset) 4441 * 4442 * Asumptions: 4443 * - ber is a valid BER element 4444 * - acctResp is initialized for the fields in its AcctUsableResp.more_info 4445 * structure 4446 */ 4447 static int 4448 get_old_acct_opt_more_info(ber_tag_t tag, BerElement *ber, 4449 AcctUsableResponse_t *acctResp) 4450 { 4451 int rc = NS_LDAP_SUCCESS; 4452 char errstr[MAXERROR]; 4453 ber_len_t len; 4454 int rem_grace, sec_b4_unlock; 4455 4456 switch (tag) { 4457 case 2: 4458 /* decode and maybe 3 is following */ 4459 if ((tag = ber_scanf(ber, "i", &rem_grace)) == LBER_ERROR) { 4460 (void) sprintf(errstr, gettext("Can not get " 4461 "rem_grace")); 4462 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4463 rc = NS_LDAP_INTERNAL; 4464 break; 4465 } 4466 (acctResp->AcctUsableResp).more_info.rem_grace = rem_grace; 4467 4468 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 4469 /* this is a success case, break to exit */ 4470 (void) sprintf(errstr, gettext("No more " 4471 "optional data")); 4472 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4473 break; 4474 } 4475 4476 if (tag == 3) { 4477 if (ber_scanf(ber, "i", &sec_b4_unlock) == LBER_ERROR) { 4478 (void) sprintf(errstr, 4479 gettext("Can not get sec_b4_unlock " 4480 "- 1st case")); 4481 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4482 rc = NS_LDAP_INTERNAL; 4483 break; 4484 } 4485 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 4486 sec_b4_unlock; 4487 } else { /* unknown tag */ 4488 (void) sprintf(errstr, gettext("Unknown tag " 4489 "- 1st case")); 4490 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4491 rc = NS_LDAP_INTERNAL; 4492 break; 4493 } 4494 break; 4495 4496 case 3: 4497 if (ber_scanf(ber, "i", &sec_b4_unlock) == LBER_ERROR) { 4498 (void) sprintf(errstr, gettext("Can not get " 4499 "sec_b4_unlock - 2nd case")); 4500 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4501 rc = NS_LDAP_INTERNAL; 4502 break; 4503 } 4504 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 4505 sec_b4_unlock; 4506 break; 4507 4508 default: /* unknown tag */ 4509 (void) sprintf(errstr, gettext("Unknown tag - 2nd case")); 4510 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4511 rc = NS_LDAP_INTERNAL; 4512 break; 4513 } 4514 4515 return (rc); 4516 } 4517 4518 /* 4519 * **** This function needs to be moved to libldap library **** 4520 * parse_acct_cont_resp_msg() parses the message received by server according to 4521 * following format (ASN1 notation): 4522 * 4523 * ACCOUNT_USABLE_RESPONSE::= CHOICE { 4524 * is_available [0] INTEGER, 4525 * ** seconds before expiration ** 4526 * is_not_available [1] more_info 4527 * } 4528 * more_info::= SEQUENCE { 4529 * inactive [0] BOOLEAN DEFAULT FALSE, 4530 * reset [1] BOOLEAN DEFAULT FALSE, 4531 * expired [2] BOOLEAN DEFAULT FALSE, 4532 * remaining_grace [3] INTEGER OPTIONAL, 4533 * seconds_before_unlock [4] INTEGER OPTIONAL 4534 * } 4535 */ 4536 /* 4537 * #define used to make the difference between coding style as done 4538 * by LDAP server 5.2p4 and newer LDAP servers. There are 4 values: 4539 * - DS52p4_USABLE: 5.2p4 coding style, account is usable 4540 * - DS52p4_NOT_USABLE: 5.2p4 coding style, account is not usable 4541 * - NEW_USABLE: newer LDAP servers coding style, account is usable 4542 * - NEW_NOT_USABLE: newer LDAP servers coding style, account is not usable 4543 * 4544 * An account would be considered not usable if for instance: 4545 * - it's been made inactive in the LDAP server 4546 * - or its password was reset in the LDAP server database 4547 * - or its password expired 4548 * - or the account has been locked, possibly forever 4549 */ 4550 #define DS52p4_USABLE 0x00 4551 #define DS52p4_NOT_USABLE 0x01 4552 #define NEW_USABLE 0x00 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE 4553 #define NEW_NOT_USABLE 0x01 | LBER_CLASS_CONTEXT | LBER_CONSTRUCTED 4554 static int 4555 parse_acct_cont_resp_msg(LDAPControl **ectrls, AcctUsableResponse_t *acctResp) 4556 { 4557 int rc = NS_LDAP_SUCCESS; 4558 BerElement *ber; 4559 ber_tag_t tag; 4560 ber_len_t len; 4561 int i; 4562 char errstr[MAXERROR]; 4563 /* used for any coding style when account is usable */ 4564 int seconds_before_expiry; 4565 /* used for 5.2p4 coding style when account is not usable */ 4566 int inactive, reset, expired; 4567 4568 if (ectrls == NULL) { 4569 (void) sprintf(errstr, gettext("Invalid ectrls parameter")); 4570 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4571 return (NS_LDAP_INVALID_PARAM); 4572 } 4573 4574 for (i = 0; ectrls[i] != NULL; i++) { 4575 if (strcmp(ectrls[i]->ldctl_oid, NS_LDAP_ACCOUNT_USABLE_CONTROL) 4576 == 0) { 4577 break; 4578 } 4579 } 4580 4581 if (ectrls[i] == NULL) { 4582 /* Ldap control is not found */ 4583 (void) sprintf(errstr, gettext("Account Usable Control " 4584 "not found")); 4585 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4586 return (NS_LDAP_NOTFOUND); 4587 } 4588 4589 /* Allocate a BER element from the control value and parse it. */ 4590 if ((ber = ber_init(&ectrls[i]->ldctl_value)) == NULL) 4591 return (NS_LDAP_MEMORY); 4592 4593 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 4594 /* Ldap decoding error */ 4595 (void) sprintf(errstr, gettext("Error decoding 1st tag")); 4596 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4597 ber_free(ber, 1); 4598 return (NS_LDAP_INTERNAL); 4599 } 4600 4601 switch (tag) { 4602 case DS52p4_USABLE: 4603 case NEW_USABLE: 4604 acctResp->choice = 0; 4605 if (ber_scanf(ber, "i", &seconds_before_expiry) 4606 == LBER_ERROR) { 4607 /* Ldap decoding error */ 4608 (void) sprintf(errstr, gettext("Can not get " 4609 "seconds_before_expiry")); 4610 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4611 rc = NS_LDAP_INTERNAL; 4612 break; 4613 } 4614 /* ber_scanf() succeeded */ 4615 (acctResp->AcctUsableResp).seconds_before_expiry = 4616 seconds_before_expiry; 4617 break; 4618 4619 case DS52p4_NOT_USABLE: 4620 acctResp->choice = 1; 4621 if (ber_scanf(ber, "{bbb", &inactive, &reset, &expired) 4622 == LBER_ERROR) { 4623 /* Ldap decoding error */ 4624 (void) sprintf(errstr, gettext("Can not get " 4625 "inactive/reset/expired")); 4626 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4627 rc = NS_LDAP_INTERNAL; 4628 break; 4629 } 4630 /* ber_scanf() succeeded */ 4631 (acctResp->AcctUsableResp).more_info.inactive = 4632 ((inactive == 0) ? 0 : 1); 4633 (acctResp->AcctUsableResp).more_info.reset = 4634 ((reset == 0) ? 0 : 1); 4635 (acctResp->AcctUsableResp).more_info.expired = 4636 ((expired == 0) ? 0 : 1); 4637 (acctResp->AcctUsableResp).more_info.rem_grace = 0; 4638 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 0; 4639 4640 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 4641 /* this is a success case, break to exit */ 4642 (void) sprintf(errstr, gettext("No optional data")); 4643 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4644 break; 4645 } 4646 4647 /* 4648 * Look at what optional more_info BER element is/are 4649 * left to be decoded. 4650 */ 4651 rc = get_old_acct_opt_more_info(tag, ber, acctResp); 4652 break; 4653 4654 case NEW_NOT_USABLE: 4655 acctResp->choice = 1; 4656 /* 4657 * Recent LDAP servers won't code more_info data for default 4658 * values (see above comments on ASN1 description for what 4659 * fields have default values & what fields are optional). 4660 */ 4661 (acctResp->AcctUsableResp).more_info.inactive = 0; 4662 (acctResp->AcctUsableResp).more_info.reset = 0; 4663 (acctResp->AcctUsableResp).more_info.expired = 0; 4664 (acctResp->AcctUsableResp).more_info.rem_grace = 0; 4665 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 0; 4666 4667 if (len == 0) { 4668 /* 4669 * Nothing else to decode; this is valid and we 4670 * use default values set above. 4671 */ 4672 (void) sprintf(errstr, gettext("more_info is " 4673 "empty, using default values")); 4674 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4675 break; 4676 } 4677 4678 /* 4679 * Look at what more_info BER element is/are left to 4680 * be decoded. 4681 */ 4682 rc = get_new_acct_more_info(ber, acctResp); 4683 break; 4684 4685 default: 4686 (void) sprintf(errstr, gettext("unknwon coding style " 4687 "(tag: 0x%x)"), tag); 4688 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4689 rc = NS_LDAP_INTERNAL; 4690 break; 4691 } 4692 4693 ber_free(ber, 1); 4694 return (rc); 4695 } 4696 4697 /* 4698 * __ns_ldap_getAcctMgmt() is called from pam account management stack 4699 * for retrieving accounting information of users with no user password - 4700 * eg. rlogin, rsh, etc. This function uses the account management control 4701 * request to do a search on the server for the user in question. The 4702 * response control returned from the server is got from the cookie. 4703 * Input params: username of whose account mgmt information is to be got 4704 * pointer to hold the parsed account management information 4705 * Return values: NS_LDAP_SUCCESS on success or appropriate error 4706 * code on failure 4707 */ 4708 int 4709 __ns_ldap_getAcctMgmt(const char *user, AcctUsableResponse_t *acctResp) 4710 { 4711 int scope, rc; 4712 char ldapfilter[1024]; 4713 ns_ldap_cookie_t *cookie; 4714 ns_ldap_search_desc_t **sdlist = NULL; 4715 ns_ldap_search_desc_t *dptr; 4716 ns_ldap_error_t *error = NULL; 4717 char **dns = NULL; 4718 char service[] = "shadow"; 4719 4720 if (user == NULL || acctResp == NULL) 4721 return (NS_LDAP_INVALID_PARAM); 4722 4723 /* Initialize State machine cookie */ 4724 cookie = init_search_state_machine(); 4725 if (cookie == NULL) 4726 return (NS_LDAP_MEMORY); 4727 4728 /* see if need to follow referrals */ 4729 rc = __s_api_toFollowReferrals(0, 4730 &cookie->followRef, &error); 4731 if (rc != NS_LDAP_SUCCESS) { 4732 (void) __ns_ldap_freeError(&error); 4733 goto out; 4734 } 4735 4736 /* get the service descriptor - or create a default one */ 4737 rc = __s_api_get_SSD_from_SSDtoUse_service(service, 4738 &sdlist, &error); 4739 if (rc != NS_LDAP_SUCCESS) { 4740 (void) __ns_ldap_freeError(&error); 4741 goto out; 4742 } 4743 4744 if (sdlist == NULL) { 4745 /* Create default service Desc */ 4746 sdlist = (ns_ldap_search_desc_t **)calloc(2, 4747 sizeof (ns_ldap_search_desc_t *)); 4748 if (sdlist == NULL) { 4749 rc = NS_LDAP_MEMORY; 4750 goto out; 4751 } 4752 dptr = (ns_ldap_search_desc_t *) 4753 calloc(1, sizeof (ns_ldap_search_desc_t)); 4754 if (dptr == NULL) { 4755 free(sdlist); 4756 rc = NS_LDAP_MEMORY; 4757 goto out; 4758 } 4759 sdlist[0] = dptr; 4760 4761 /* default base */ 4762 rc = __s_api_getDNs(&dns, service, &cookie->errorp); 4763 if (rc != NS_LDAP_SUCCESS) { 4764 if (dns) { 4765 __s_api_free2dArray(dns); 4766 dns = NULL; 4767 } 4768 (void) __ns_ldap_freeError(&(cookie->errorp)); 4769 cookie->errorp = NULL; 4770 goto out; 4771 } 4772 dptr->basedn = strdup(dns[0]); 4773 if (dptr->basedn == NULL) { 4774 free(sdlist); 4775 free(dptr); 4776 if (dns) { 4777 __s_api_free2dArray(dns); 4778 dns = NULL; 4779 } 4780 rc = NS_LDAP_MEMORY; 4781 goto out; 4782 } 4783 __s_api_free2dArray(dns); 4784 dns = NULL; 4785 4786 /* default scope */ 4787 scope = 0; 4788 rc = __s_api_getSearchScope(&scope, &cookie->errorp); 4789 dptr->scope = scope; 4790 } 4791 4792 cookie->sdlist = sdlist; 4793 4794 cookie->service = strdup(service); 4795 if (cookie->service == NULL) { 4796 rc = NS_LDAP_MEMORY; 4797 goto out; 4798 } 4799 4800 /* search for entries for this particular uid */ 4801 (void) snprintf(ldapfilter, sizeof (ldapfilter), "(uid=%s)", user); 4802 cookie->i_filter = strdup(ldapfilter); 4803 if (cookie->i_filter == NULL) { 4804 rc = NS_LDAP_MEMORY; 4805 goto out; 4806 } 4807 4808 /* create the control request */ 4809 if ((rc = setup_acctmgmt_params(cookie)) != NS_LDAP_SUCCESS) 4810 goto out; 4811 4812 /* Process search */ 4813 rc = search_state_machine(cookie, GET_ACCT_MGMT_INFO, 0); 4814 4815 /* Copy results back to user */ 4816 rc = cookie->err_rc; 4817 if (rc != NS_LDAP_SUCCESS) 4818 (void) __ns_ldap_freeError(&(cookie->errorp)); 4819 4820 if (cookie->result == NULL) 4821 goto out; 4822 4823 if ((rc = parse_acct_cont_resp_msg(cookie->resultctrl, acctResp)) 4824 != NS_LDAP_SUCCESS) 4825 goto out; 4826 4827 rc = NS_LDAP_SUCCESS; 4828 4829 out: 4830 delete_search_cookie(cookie); 4831 4832 return (rc); 4833 } 4834