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 /* 3449 * ask cache manager to save the 3450 * dn to domain mapping(s) 3451 */ 3452 for (j = 0; j <= i; j++) { 3453 (void) __s_api_set_cachemgr_data( 3454 NS_CACHE_DN2DOMAIN, 3455 dns[j], 3456 *domain); 3457 } 3458 break; 3459 } 3460 } 3461 3462 free(dns); 3463 free(newdn); 3464 if (rc != NS_LDAP_SUCCESS) 3465 rc = NS_LDAP_NOTFOUND; 3466 return (rc); 3467 } 3468 3469 /*ARGSUSED*/ 3470 int 3471 __ns_ldap_getServiceAuthMethods(const char *service, 3472 ns_auth_t ***auth, 3473 ns_ldap_error_t **errorp) 3474 { 3475 char errstr[MAXERROR]; 3476 int rc, i, done = 0; 3477 int slen; 3478 void **param; 3479 char **sam, *srv, *send; 3480 ns_auth_t **authpp = NULL, *ap; 3481 int cnt, max; 3482 ns_config_t *cfg; 3483 ns_ldap_error_t *error = NULL; 3484 3485 if (errorp == NULL) 3486 return (NS_LDAP_INVALID_PARAM); 3487 *errorp = NULL; 3488 3489 if ((service == NULL) || (service[0] == '\0') || 3490 (auth == NULL)) 3491 return (NS_LDAP_INVALID_PARAM); 3492 3493 *auth = NULL; 3494 rc = __ns_ldap_getParam(NS_LDAP_SERVICE_AUTH_METHOD_P, ¶m, &error); 3495 if (rc != NS_LDAP_SUCCESS || param == NULL) { 3496 *errorp = error; 3497 return (rc); 3498 } 3499 sam = (char **)param; 3500 3501 cfg = __s_api_get_default_config(); 3502 cnt = 0; 3503 3504 slen = strlen(service); 3505 3506 for (; *sam; sam++) { 3507 srv = *sam; 3508 if (strncasecmp(service, srv, slen) != 0) 3509 continue; 3510 srv += slen; 3511 if (*srv != COLONTOK) 3512 continue; 3513 send = srv; 3514 srv++; 3515 for (max = 1; (send = strchr(++send, SEMITOK)) != NULL; 3516 max++) {} 3517 authpp = (ns_auth_t **)calloc(++max, sizeof (ns_auth_t *)); 3518 if (authpp == NULL) { 3519 (void) __ns_ldap_freeParam(¶m); 3520 __s_api_release_config(cfg); 3521 return (NS_LDAP_MEMORY); 3522 } 3523 while (!done) { 3524 send = strchr(srv, SEMITOK); 3525 if (send != NULL) { 3526 *send = '\0'; 3527 send++; 3528 } 3529 i = __s_get_enum_value(cfg, srv, NS_LDAP_AUTH_P); 3530 if (i == -1) { 3531 (void) __ns_ldap_freeParam(¶m); 3532 (void) sprintf(errstr, 3533 gettext("Unsupported " 3534 "serviceAuthenticationMethod: %s.\n"), srv); 3535 MKERROR(LOG_WARNING, *errorp, NS_CONFIG_SYNTAX, 3536 strdup(errstr), NULL); 3537 __s_api_release_config(cfg); 3538 return (NS_LDAP_CONFIG); 3539 } 3540 ap = __s_api_AuthEnumtoStruct((EnumAuthType_t)i); 3541 if (ap == NULL) { 3542 (void) __ns_ldap_freeParam(¶m); 3543 __s_api_release_config(cfg); 3544 return (NS_LDAP_MEMORY); 3545 } 3546 authpp[cnt++] = ap; 3547 if (send == NULL) 3548 done = TRUE; 3549 else 3550 srv = send; 3551 } 3552 } 3553 3554 *auth = authpp; 3555 (void) __ns_ldap_freeParam(¶m); 3556 __s_api_release_config(cfg); 3557 return (NS_LDAP_SUCCESS); 3558 } 3559 3560 /* 3561 * This routine is called when certain scenario occurs 3562 * e.g. 3563 * service == auto_home 3564 * SSD = automount: ou = mytest, 3565 * NS_LDAP_MAPATTRIBUTE= auto_home: automountMapName=AAA 3566 * NS_LDAP_OBJECTCLASSMAP= auto_home:automountMap=MynisMap 3567 * NS_LDAP_OBJECTCLASSMAP= auto_home:automount=MynisObject 3568 * 3569 * The automountMapName is prepended implicitely but is mapped 3570 * to AAA. So dn could appers as 3571 * dn: AAA=auto_home,ou=bar,dc=foo,dc=com 3572 * dn: automountKey=user_01,AAA=auto_home,ou=bar,dc=foo,dc=com 3573 * dn: automountKey=user_02,AAA=auto_home,ou=bar,dc=foo,dc=com 3574 * in the directory. 3575 * This function is called to covert the mapped attr back to 3576 * orig attr when the entries are searched and returned 3577 */ 3578 3579 int 3580 __s_api_convert_automountmapname(const char *service, char **dn, 3581 ns_ldap_error_t **errp) { 3582 3583 char **mapping = NULL; 3584 char *mapped_attr = NULL; 3585 char *automountmapname = "automountMapName"; 3586 char *buffer = NULL; 3587 int rc = NS_LDAP_SUCCESS; 3588 char errstr[MAXERROR]; 3589 3590 /* 3591 * dn is an input/out parameter, check it first 3592 */ 3593 3594 if (service == NULL || dn == NULL || *dn == NULL) 3595 return (NS_LDAP_INVALID_PARAM); 3596 3597 /* 3598 * Check to see if there is a mapped attribute for auto_xxx 3599 */ 3600 3601 mapping = __ns_ldap_getMappedAttributes(service, automountmapname); 3602 3603 /* 3604 * if no mapped attribute for auto_xxx, try automount 3605 */ 3606 3607 if (mapping == NULL) 3608 mapping = __ns_ldap_getMappedAttributes( 3609 "automount", automountmapname); 3610 3611 /* 3612 * if no mapped attribute is found, return SUCCESS (no op) 3613 */ 3614 3615 if (mapping == NULL) 3616 return (NS_LDAP_SUCCESS); 3617 3618 /* 3619 * if the mapped attribute is found and attr is not empty, 3620 * copy it 3621 */ 3622 3623 if (mapping[0] != NULL) { 3624 mapped_attr = strdup(mapping[0]); 3625 __s_api_free2dArray(mapping); 3626 if (mapped_attr == NULL) { 3627 return (NS_LDAP_MEMORY); 3628 } 3629 } else { 3630 __s_api_free2dArray(mapping); 3631 3632 (void) snprintf(errstr, (2 * MAXERROR), 3633 gettext( 3634 "Attribute nisMapName is mapped to an " 3635 "empty string.\n")); 3636 3637 MKERROR(LOG_ERR, *errp, NS_CONFIG_SYNTAX, 3638 strdup(errstr), NULL); 3639 3640 return (NS_LDAP_CONFIG); 3641 } 3642 3643 /* 3644 * Locate the mapped attribute in the dn 3645 * and replace it if it exists 3646 */ 3647 3648 rc = __s_api_replace_mapped_attr_in_dn( 3649 (const char *) automountmapname, (const char *) mapped_attr, 3650 (const char *) *dn, &buffer); 3651 3652 /* clean up */ 3653 3654 free(mapped_attr); 3655 3656 /* 3657 * If mapped attr is found(buffer != NULL) 3658 * a new dn is returned 3659 * If no mapped attribute is in dn, 3660 * return NS_LDAP_SUCCESS (no op) 3661 * If no memory, 3662 * return NS_LDAP_MEMORY (no op) 3663 */ 3664 3665 if (buffer != NULL) { 3666 free(*dn); 3667 *dn = buffer; 3668 } 3669 3670 return (rc); 3671 } 3672 3673 /* 3674 * If the mapped attr is found in the dn, 3675 * return NS_LDAP_SUCCESS and a new_dn. 3676 * If no mapped attr is found, 3677 * return NS_LDAP_SUCCESS and *new_dn == NULL 3678 * If there is not enough memory, 3679 * return NS_LDAP_MEMORY and *new_dn == NULL 3680 */ 3681 3682 int 3683 __s_api_replace_mapped_attr_in_dn( 3684 const char *orig_attr, const char *mapped_attr, 3685 const char *dn, char **new_dn) { 3686 3687 char **dnArray = NULL; 3688 char *cur = NULL, *start = NULL; 3689 int i = 0, found = 0; 3690 int len = 0, orig_len = 0, mapped_len = 0; 3691 int dn_len = 0, tmp_len = 0; 3692 3693 *new_dn = NULL; 3694 3695 /* 3696 * seperate dn into individual componets 3697 * e.g. 3698 * "automountKey=user_01" , "automountMapName_test=auto_home", ... 3699 */ 3700 dnArray = ldap_explode_dn(dn, 0); 3701 3702 /* 3703 * This will find "mapped attr=value" in dn. 3704 * It won't find match if mapped attr appears 3705 * in the value. 3706 */ 3707 for (i = 0; dnArray[i] != NULL; i++) { 3708 /* 3709 * This function is called when reading from 3710 * the directory so assume each component has "=". 3711 * Any ill formatted dn should be rejected 3712 * before adding to the directory 3713 */ 3714 cur = strchr(dnArray[i], '='); 3715 *cur = '\0'; 3716 if (strcasecmp(mapped_attr, dnArray[i]) == 0) 3717 found = 1; 3718 *cur = '='; 3719 if (found) break; 3720 } 3721 3722 if (!found) { 3723 __s_api_free2dArray(dnArray); 3724 *new_dn = NULL; 3725 return (NS_LDAP_SUCCESS); 3726 } 3727 /* 3728 * The new length is *dn length + (difference between 3729 * orig attr and mapped attr) + 1 ; 3730 * e.g. 3731 * automountKey=aa,automountMapName_test=auto_home,dc=foo,dc=com 3732 * ==> 3733 * automountKey=aa,automountMapName=auto_home,dc=foo,dc=com 3734 */ 3735 mapped_len = strlen(mapped_attr); 3736 orig_len = strlen(orig_attr); 3737 dn_len = strlen(dn); 3738 len = dn_len + orig_len - mapped_len + 1; 3739 *new_dn = (char *)calloc(1, len); 3740 if (*new_dn == NULL) { 3741 __s_api_free2dArray(dnArray); 3742 return (NS_LDAP_MEMORY); 3743 } 3744 3745 /* 3746 * Locate the mapped attr in the dn. 3747 * Use dnArray[i] instead of mapped_attr 3748 * because mapped_attr could appear in 3749 * the value 3750 */ 3751 3752 cur = strstr(dn, dnArray[i]); 3753 __s_api_free2dArray(dnArray); 3754 /* copy the portion before mapped attr in dn */ 3755 start = *new_dn; 3756 tmp_len = cur - dn; 3757 (void) memcpy((void *) start, (const void*) dn, tmp_len); 3758 3759 /* 3760 * Copy the orig_attr. e.g. automountMapName 3761 * This replaces mapped attr with orig attr 3762 */ 3763 start = start + (cur - dn); /* move cursor in buffer */ 3764 (void) memcpy((void *) start, (const void*) orig_attr, orig_len); 3765 3766 /* 3767 * Copy the portion after mapped attr in dn 3768 */ 3769 cur = cur + mapped_len; /* move cursor in dn */ 3770 start = start + orig_len; /* move cursor in buffer */ 3771 (void) strcpy(start, cur); 3772 3773 return (NS_LDAP_SUCCESS); 3774 } 3775 3776 /* 3777 * Validate Filter functions 3778 */ 3779 3780 /* ***** Start of modified libldap.so.5 filter parser ***** */ 3781 3782 /* filter parsing routine forward references */ 3783 static int adj_filter_list(char *str); 3784 static int adj_simple_filter(char *str); 3785 static int unescape_filterval(char *val); 3786 static int hexchar2int(char c); 3787 static int adj_substring_filter(char *val); 3788 3789 3790 /* 3791 * assumes string manipulation is in-line 3792 * and all strings are sufficient in size 3793 * return value is the position after 'c' 3794 */ 3795 3796 static char * 3797 resync_str(char *str, char *next, char c) 3798 { 3799 char *ret; 3800 3801 ret = str + strlen(str); 3802 *next = c; 3803 if (ret == next) 3804 return (ret); 3805 (void) strcat(str, next); 3806 return (ret); 3807 } 3808 3809 static char * 3810 find_right_paren(char *s) 3811 { 3812 int balance, escape; 3813 3814 balance = 1; 3815 escape = 0; 3816 while (*s && balance) { 3817 if (escape == 0) { 3818 if (*s == '(') 3819 balance++; 3820 else if (*s == ')') 3821 balance--; 3822 } 3823 if (*s == '\\' && ! escape) 3824 escape = 1; 3825 else 3826 escape = 0; 3827 if (balance) 3828 s++; 3829 } 3830 3831 return (*s ? s : NULL); 3832 } 3833 3834 static char * 3835 adj_complex_filter(char *str) 3836 { 3837 char *next; 3838 3839 /* 3840 * We have (x(filter)...) with str sitting on 3841 * the x. We have to find the paren matching 3842 * the one before the x and put the intervening 3843 * filters by calling adj_filter_list(). 3844 */ 3845 3846 str++; 3847 if ((next = find_right_paren(str)) == NULL) 3848 return (NULL); 3849 3850 *next = '\0'; 3851 if (adj_filter_list(str) == -1) 3852 return (NULL); 3853 next = resync_str(str, next, ')'); 3854 next++; 3855 3856 return (next); 3857 } 3858 3859 static int 3860 adj_filter(char *str) 3861 { 3862 char *next; 3863 int parens, balance, escape; 3864 char *np, *cp, *dp; 3865 3866 parens = 0; 3867 while (*str) { 3868 switch (*str) { 3869 case '(': 3870 str++; 3871 parens++; 3872 switch (*str) { 3873 case '&': 3874 if ((str = adj_complex_filter(str)) == NULL) 3875 return (-1); 3876 3877 parens--; 3878 break; 3879 3880 case '|': 3881 if ((str = adj_complex_filter(str)) == NULL) 3882 return (-1); 3883 3884 parens--; 3885 break; 3886 3887 case '!': 3888 if ((str = adj_complex_filter(str)) == NULL) 3889 return (-1); 3890 3891 parens--; 3892 break; 3893 3894 case '(': 3895 /* illegal ((case - generated by conversion */ 3896 3897 /* find missing close) */ 3898 np = find_right_paren(str+1); 3899 3900 /* error if not found */ 3901 if (np == NULL) 3902 return (-1); 3903 3904 /* remove redundant (and) */ 3905 for (dp = str, cp = str+1; cp < np; ) { 3906 *dp++ = *cp++; 3907 } 3908 cp++; 3909 while (*cp) 3910 *dp++ = *cp++; 3911 *dp = '\0'; 3912 3913 /* re-start test at original ( */ 3914 parens--; 3915 str--; 3916 break; 3917 3918 default: 3919 balance = 1; 3920 escape = 0; 3921 next = str; 3922 while (*next && balance) { 3923 if (escape == 0) { 3924 if (*next == '(') 3925 balance++; 3926 else if (*next == ')') 3927 balance--; 3928 } 3929 if (*next == '\\' && ! escape) 3930 escape = 1; 3931 else 3932 escape = 0; 3933 if (balance) 3934 next++; 3935 } 3936 if (balance != 0) 3937 return (-1); 3938 3939 *next = '\0'; 3940 if (adj_simple_filter(str) == -1) { 3941 return (-1); 3942 } 3943 next = resync_str(str, next, ')'); 3944 next++; 3945 str = next; 3946 parens--; 3947 break; 3948 } 3949 break; 3950 3951 case ')': 3952 str++; 3953 parens--; 3954 break; 3955 3956 case ' ': 3957 str++; 3958 break; 3959 3960 default: /* assume it's a simple type=value filter */ 3961 next = strchr(str, '\0'); 3962 if (adj_simple_filter(str) == -1) { 3963 return (-1); 3964 } 3965 str = next; 3966 break; 3967 } 3968 } 3969 3970 return (parens ? -1 : 0); 3971 } 3972 3973 3974 /* 3975 * Put a list of filters like this "(filter1)(filter2)..." 3976 */ 3977 3978 static int 3979 adj_filter_list(char *str) 3980 { 3981 char *next; 3982 char save; 3983 3984 while (*str) { 3985 while (*str && isspace(*str)) 3986 str++; 3987 if (*str == '\0') 3988 break; 3989 3990 if ((next = find_right_paren(str + 1)) == NULL) 3991 return (-1); 3992 save = *++next; 3993 3994 /* now we have "(filter)" with str pointing to it */ 3995 *next = '\0'; 3996 if (adj_filter(str) == -1) 3997 return (-1); 3998 next = resync_str(str, next, save); 3999 4000 str = next; 4001 } 4002 4003 return (0); 4004 } 4005 4006 4007 /* 4008 * is_valid_attr - returns 1 if a is a syntactically valid left-hand side 4009 * of a filter expression, 0 otherwise. A valid string may contain only 4010 * letters, numbers, hyphens, semi-colons, colons and periods. examples: 4011 * cn 4012 * cn;lang-fr 4013 * 1.2.3.4;binary;dynamic 4014 * mail;dynamic 4015 * cn:dn:1.2.3.4 4016 * 4017 * For compatibility with older servers, we also allow underscores in 4018 * attribute types, even through they are not allowed by the LDAPv3 RFCs. 4019 */ 4020 static int 4021 is_valid_attr(char *a) 4022 { 4023 for (; *a; a++) { 4024 if (!isascii(*a)) { 4025 return (0); 4026 } else if (!isalnum(*a)) { 4027 switch (*a) { 4028 case '-': 4029 case '.': 4030 case ';': 4031 case ':': 4032 case '_': 4033 break; /* valid */ 4034 default: 4035 return (0); 4036 } 4037 } 4038 } 4039 return (1); 4040 } 4041 4042 static char * 4043 find_star(char *s) 4044 { 4045 for (; *s; ++s) { 4046 switch (*s) { 4047 case '*': 4048 return (s); 4049 case '\\': 4050 ++s; 4051 if (hexchar2int(s[0]) >= 0 && hexchar2int(s[1]) >= 0) 4052 ++s; 4053 default: 4054 break; 4055 } 4056 } 4057 return (NULL); 4058 } 4059 4060 static int 4061 adj_simple_filter(char *str) 4062 { 4063 char *s, *s2, *s3, filterop; 4064 char *value; 4065 int ftype = 0; 4066 int rc; 4067 4068 rc = -1; /* pessimistic */ 4069 4070 if ((str = strdup(str)) == NULL) { 4071 return (rc); 4072 } 4073 4074 if ((s = strchr(str, '=')) == NULL) { 4075 goto free_and_return; 4076 } 4077 value = s + 1; 4078 *s-- = '\0'; 4079 filterop = *s; 4080 if (filterop == '<' || filterop == '>' || filterop == '~' || 4081 filterop == ':') { 4082 *s = '\0'; 4083 } 4084 4085 if (! is_valid_attr(str)) { 4086 goto free_and_return; 4087 } 4088 4089 switch (filterop) { 4090 case '<': /* LDAP_FILTER_LE */ 4091 case '>': /* LDAP_FILTER_GE */ 4092 case '~': /* LDAP_FILTER_APPROX */ 4093 break; 4094 case ':': /* extended filter - v3 only */ 4095 /* 4096 * extended filter looks like this: 4097 * 4098 * [type][':dn'][':'oid]':='value 4099 * 4100 * where one of type or :oid is required. 4101 * 4102 */ 4103 s2 = s3 = NULL; 4104 if ((s2 = strrchr(str, ':')) == NULL) { 4105 goto free_and_return; 4106 } 4107 if (strcasecmp(s2, ":dn") == 0) { 4108 *s2 = '\0'; 4109 } else { 4110 *s2 = '\0'; 4111 if ((s3 = strrchr(str, ':')) != NULL) { 4112 if (strcasecmp(s3, ":dn") != 0) { 4113 goto free_and_return; 4114 } 4115 *s3 = '\0'; 4116 } 4117 } 4118 if (unescape_filterval(value) < 0) { 4119 goto free_and_return; 4120 } 4121 rc = 0; 4122 goto free_and_return; 4123 /* break; */ 4124 default: 4125 if (find_star(value) == NULL) { 4126 ftype = 0; /* LDAP_FILTER_EQUALITY */ 4127 } else if (strcmp(value, "*") == 0) { 4128 ftype = 1; /* LDAP_FILTER_PRESENT */ 4129 } else { 4130 rc = adj_substring_filter(value); 4131 goto free_and_return; 4132 } 4133 break; 4134 } 4135 4136 if (ftype != 0) { /* == LDAP_FILTER_PRESENT */ 4137 rc = 0; 4138 } else if (unescape_filterval(value) >= 0) { 4139 rc = 0; 4140 } 4141 if (rc != -1) { 4142 rc = 0; 4143 } 4144 4145 free_and_return: 4146 free(str); 4147 return (rc); 4148 } 4149 4150 4151 /* 4152 * Check in place both LDAPv2 (RFC-1960) and LDAPv3 (hexadecimal) escape 4153 * sequences within the null-terminated string 'val'. 4154 * 4155 * If 'val' contains invalid escape sequences we return -1. 4156 * Otherwise return 1 4157 */ 4158 static int 4159 unescape_filterval(char *val) 4160 { 4161 int escape, firstdigit; 4162 char *s; 4163 4164 firstdigit = 0; 4165 escape = 0; 4166 for (s = val; *s; s++) { 4167 if (escape) { 4168 /* 4169 * first try LDAPv3 escape (hexadecimal) sequence 4170 */ 4171 if (hexchar2int(*s) < 0) { 4172 if (firstdigit) { 4173 /* 4174 * LDAPv2 (RFC1960) escape sequence 4175 */ 4176 escape = 0; 4177 } else { 4178 return (-1); 4179 } 4180 } 4181 if (firstdigit) { 4182 firstdigit = 0; 4183 } else { 4184 escape = 0; 4185 } 4186 4187 } else if (*s != '\\') { 4188 escape = 0; 4189 4190 } else { 4191 escape = 1; 4192 firstdigit = 1; 4193 } 4194 } 4195 4196 return (1); 4197 } 4198 4199 4200 /* 4201 * convert character 'c' that represents a hexadecimal digit to an integer. 4202 * if 'c' is not a hexidecimal digit [0-9A-Fa-f], -1 is returned. 4203 * otherwise the converted value is returned. 4204 */ 4205 static int 4206 hexchar2int(char c) 4207 { 4208 if (c >= '0' && c <= '9') { 4209 return (c - '0'); 4210 } 4211 if (c >= 'A' && c <= 'F') { 4212 return (c - 'A' + 10); 4213 } 4214 if (c >= 'a' && c <= 'f') { 4215 return (c - 'a' + 10); 4216 } 4217 return (-1); 4218 } 4219 4220 static int 4221 adj_substring_filter(char *val) 4222 { 4223 char *nextstar; 4224 4225 for (; val != NULL; val = nextstar) { 4226 if ((nextstar = find_star(val)) != NULL) { 4227 *nextstar++ = '\0'; 4228 } 4229 4230 if (*val != '\0') { 4231 if (unescape_filterval(val) < 0) { 4232 return (-1); 4233 } 4234 } 4235 } 4236 4237 return (0); 4238 } 4239 4240 /* ***** End of modified libldap.so.5 filter parser ***** */ 4241 4242 4243 /* 4244 * Walk filter, remove redundant parentheses in-line 4245 * verify that the filter is reasonable 4246 */ 4247 static int 4248 validate_filter(ns_ldap_cookie_t *cookie) 4249 { 4250 char *filter = cookie->filter; 4251 int rc; 4252 4253 /* Parse filter looking for illegal values */ 4254 4255 rc = adj_filter(filter); 4256 if (rc != 0) { 4257 return (NS_LDAP_OP_FAILED); 4258 } 4259 4260 /* end of filter checking */ 4261 4262 return (NS_LDAP_SUCCESS); 4263 } 4264 4265 /* 4266 * Set the account management request control that needs to be sent to server. 4267 * This control is required to get the account management information of 4268 * a user to do local account checking. 4269 */ 4270 static int 4271 setup_acctmgmt_params(ns_ldap_cookie_t *cookie) 4272 { 4273 LDAPControl *req = NULL, **requestctrls; 4274 4275 req = (LDAPControl *)malloc(sizeof (LDAPControl)); 4276 4277 if (req == NULL) 4278 return (NS_LDAP_MEMORY); 4279 4280 /* fill in the fields of this new control */ 4281 req->ldctl_iscritical = 1; 4282 req->ldctl_oid = strdup(NS_LDAP_ACCOUNT_USABLE_CONTROL); 4283 if (req->ldctl_oid == NULL) { 4284 free(req); 4285 return (NS_LDAP_MEMORY); 4286 } 4287 req->ldctl_value.bv_len = 0; 4288 req->ldctl_value.bv_val = NULL; 4289 4290 requestctrls = (LDAPControl **)calloc(2, sizeof (LDAPControl *)); 4291 if (requestctrls == NULL) { 4292 ldap_control_free(req); 4293 return (NS_LDAP_MEMORY); 4294 } 4295 4296 requestctrls[0] = req; 4297 4298 cookie->p_serverctrls = requestctrls; 4299 4300 return (NS_LDAP_SUCCESS); 4301 } 4302 4303 /* 4304 * int get_new_acct_more_info(BerElement *ber, 4305 * AcctUsableResponse_t *acctResp) 4306 * 4307 * Decode the more_info data from an Account Management control response, 4308 * when the account is not usable and when code style is from recent LDAP 4309 * servers (see below comments for parse_acct_cont_resp_msg() to get more 4310 * details on coding styles and ASN1 description). 4311 * 4312 * Expected BER encoding: {tbtbtbtiti} 4313 * +t: tag is 0 4314 * +b: TRUE if inactive due to account inactivation 4315 * +t: tag is 1 4316 * +b: TRUE if password has been reset 4317 * +t: tag is 2 4318 * +b: TRUE if password is expired 4319 * +t: tag is 3 4320 * +i: contains num of remaining grace, 0 means no grace 4321 * +t: tag is 4 4322 * +i: contains num of seconds before auto-unlock. -1 means acct is locked 4323 * forever (i.e. until reset) 4324 * 4325 * Asumptions: 4326 * - ber is not null 4327 * - acctResp is not null and is initialized with default values for the 4328 * fields in its AcctUsableResp.more_info structure 4329 * - the ber stream is received in the correct order, per the ASN1 description. 4330 * We do not check this order and make the asumption that it is correct. 4331 * Note that the ber stream may not (and will not in most cases) contain 4332 * all fields. 4333 */ 4334 static int 4335 get_new_acct_more_info(BerElement *ber, AcctUsableResponse_t *acctResp) 4336 { 4337 int rc = NS_LDAP_SUCCESS; 4338 char errstr[MAXERROR]; 4339 ber_tag_t rTag = LBER_DEFAULT; 4340 ber_len_t rLen = 0; 4341 ber_int_t rValue; 4342 char *last; 4343 int berRC = 0; 4344 4345 /* 4346 * Look at what more_info BER element is/are left to be decoded. 4347 * look at each of them 1 by 1, without checking on their order 4348 * and possible multi values. 4349 */ 4350 for (rTag = ber_first_element(ber, &rLen, &last); 4351 rTag != LBER_END_OF_SEQORSET; 4352 rTag = ber_next_element(ber, &rLen, last)) { 4353 4354 berRC = 0; 4355 switch (rTag) { 4356 case 0 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4357 /* inactive */ 4358 berRC = ber_scanf(ber, "b", &rValue); 4359 if (berRC != LBER_ERROR) { 4360 (acctResp->AcctUsableResp).more_info. 4361 inactive = (rValue != 0) ? 1 : 0; 4362 } 4363 break; 4364 4365 case 1 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4366 /* reset */ 4367 berRC = ber_scanf(ber, "b", &rValue); 4368 if (berRC != LBER_ERROR) { 4369 (acctResp->AcctUsableResp).more_info.reset 4370 = (rValue != 0) ? 1 : 0; 4371 } 4372 break; 4373 4374 case 2 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4375 /* expired */ 4376 berRC = ber_scanf(ber, "b", &rValue); 4377 if (berRC != LBER_ERROR) { 4378 (acctResp->AcctUsableResp).more_info.expired 4379 = (rValue != 0) ? 1 : 0; 4380 } 4381 break; 4382 4383 case 3 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4384 /* remaining grace */ 4385 berRC = ber_scanf(ber, "i", &rValue); 4386 if (berRC != LBER_ERROR) { 4387 (acctResp->AcctUsableResp).more_info.rem_grace 4388 = rValue; 4389 } 4390 break; 4391 4392 case 4 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE: 4393 /* seconds before unlock */ 4394 berRC = ber_scanf(ber, "i", &rValue); 4395 if (berRC != LBER_ERROR) { 4396 (acctResp->AcctUsableResp).more_info. 4397 sec_b4_unlock = rValue; 4398 } 4399 break; 4400 4401 default : 4402 (void) sprintf(errstr, 4403 gettext("invalid reason tag 0x%x"), rTag); 4404 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4405 rc = NS_LDAP_INTERNAL; 4406 break; 4407 } 4408 if (berRC == LBER_ERROR) { 4409 (void) sprintf(errstr, 4410 gettext("error 0x%x decoding value for " 4411 "tag 0x%x"), berRC, rTag); 4412 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4413 rc = NS_LDAP_INTERNAL; 4414 } 4415 if (rc != NS_LDAP_SUCCESS) { 4416 /* exit the for loop */ 4417 break; 4418 } 4419 } 4420 4421 return (rc); 4422 } 4423 4424 /* 4425 * int get_old_acct_opt_more_info(BerElement *ber, 4426 * AcctUsableResponse_t *acctResp) 4427 * 4428 * Decode the optional more_info data from an Account Management control 4429 * response, when the account is not usable and when code style is from LDAP 4430 * server 5.2p4 (see below comments for parse_acct_cont_resp_msg() to get more 4431 * details on coding styles and ASN1 description). 4432 * 4433 * Expected BER encoding: titi} 4434 * +t: tag is 2 4435 * +i: contains num of remaining grace, 0 means no grace 4436 * +t: tag is 3 4437 * +i: contains num of seconds before auto-unlock. -1 means acct is locked 4438 * forever (i.e. until reset) 4439 * 4440 * Asumptions: 4441 * - ber is a valid BER element 4442 * - acctResp is initialized for the fields in its AcctUsableResp.more_info 4443 * structure 4444 */ 4445 static int 4446 get_old_acct_opt_more_info(ber_tag_t tag, BerElement *ber, 4447 AcctUsableResponse_t *acctResp) 4448 { 4449 int rc = NS_LDAP_SUCCESS; 4450 char errstr[MAXERROR]; 4451 ber_len_t len; 4452 int rem_grace, sec_b4_unlock; 4453 4454 switch (tag) { 4455 case 2: 4456 /* decode and maybe 3 is following */ 4457 if ((tag = ber_scanf(ber, "i", &rem_grace)) == LBER_ERROR) { 4458 (void) sprintf(errstr, gettext("Can not get " 4459 "rem_grace")); 4460 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4461 rc = NS_LDAP_INTERNAL; 4462 break; 4463 } 4464 (acctResp->AcctUsableResp).more_info.rem_grace = rem_grace; 4465 4466 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 4467 /* this is a success case, break to exit */ 4468 (void) sprintf(errstr, gettext("No more " 4469 "optional data")); 4470 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4471 break; 4472 } 4473 4474 if (tag == 3) { 4475 if (ber_scanf(ber, "i", &sec_b4_unlock) == LBER_ERROR) { 4476 (void) sprintf(errstr, 4477 gettext("Can not get sec_b4_unlock " 4478 "- 1st case")); 4479 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4480 rc = NS_LDAP_INTERNAL; 4481 break; 4482 } 4483 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 4484 sec_b4_unlock; 4485 } else { /* unknown tag */ 4486 (void) sprintf(errstr, gettext("Unknown tag " 4487 "- 1st case")); 4488 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4489 rc = NS_LDAP_INTERNAL; 4490 break; 4491 } 4492 break; 4493 4494 case 3: 4495 if (ber_scanf(ber, "i", &sec_b4_unlock) == LBER_ERROR) { 4496 (void) sprintf(errstr, gettext("Can not get " 4497 "sec_b4_unlock - 2nd case")); 4498 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4499 rc = NS_LDAP_INTERNAL; 4500 break; 4501 } 4502 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 4503 sec_b4_unlock; 4504 break; 4505 4506 default: /* unknown tag */ 4507 (void) sprintf(errstr, gettext("Unknown tag - 2nd case")); 4508 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4509 rc = NS_LDAP_INTERNAL; 4510 break; 4511 } 4512 4513 return (rc); 4514 } 4515 4516 /* 4517 * **** This function needs to be moved to libldap library **** 4518 * parse_acct_cont_resp_msg() parses the message received by server according to 4519 * following format (ASN1 notation): 4520 * 4521 * ACCOUNT_USABLE_RESPONSE::= CHOICE { 4522 * is_available [0] INTEGER, 4523 * ** seconds before expiration ** 4524 * is_not_available [1] more_info 4525 * } 4526 * more_info::= SEQUENCE { 4527 * inactive [0] BOOLEAN DEFAULT FALSE, 4528 * reset [1] BOOLEAN DEFAULT FALSE, 4529 * expired [2] BOOLEAN DEFAULT FALSE, 4530 * remaining_grace [3] INTEGER OPTIONAL, 4531 * seconds_before_unlock [4] INTEGER OPTIONAL 4532 * } 4533 */ 4534 /* 4535 * #define used to make the difference between coding style as done 4536 * by LDAP server 5.2p4 and newer LDAP servers. There are 4 values: 4537 * - DS52p4_USABLE: 5.2p4 coding style, account is usable 4538 * - DS52p4_NOT_USABLE: 5.2p4 coding style, account is not usable 4539 * - NEW_USABLE: newer LDAP servers coding style, account is usable 4540 * - NEW_NOT_USABLE: newer LDAP servers coding style, account is not usable 4541 * 4542 * An account would be considered not usable if for instance: 4543 * - it's been made inactive in the LDAP server 4544 * - or its password was reset in the LDAP server database 4545 * - or its password expired 4546 * - or the account has been locked, possibly forever 4547 */ 4548 #define DS52p4_USABLE 0x00 4549 #define DS52p4_NOT_USABLE 0x01 4550 #define NEW_USABLE 0x00 | LBER_CLASS_CONTEXT | LBER_PRIMITIVE 4551 #define NEW_NOT_USABLE 0x01 | LBER_CLASS_CONTEXT | LBER_CONSTRUCTED 4552 static int 4553 parse_acct_cont_resp_msg(LDAPControl **ectrls, AcctUsableResponse_t *acctResp) 4554 { 4555 int rc = NS_LDAP_SUCCESS; 4556 BerElement *ber; 4557 ber_tag_t tag; 4558 ber_len_t len; 4559 int i; 4560 char errstr[MAXERROR]; 4561 /* used for any coding style when account is usable */ 4562 int seconds_before_expiry; 4563 /* used for 5.2p4 coding style when account is not usable */ 4564 int inactive, reset, expired; 4565 4566 if (ectrls == NULL) { 4567 (void) sprintf(errstr, gettext("Invalid ectrls parameter")); 4568 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4569 return (NS_LDAP_INVALID_PARAM); 4570 } 4571 4572 for (i = 0; ectrls[i] != NULL; i++) { 4573 if (strcmp(ectrls[i]->ldctl_oid, NS_LDAP_ACCOUNT_USABLE_CONTROL) 4574 == 0) { 4575 break; 4576 } 4577 } 4578 4579 if (ectrls[i] == NULL) { 4580 /* Ldap control is not found */ 4581 (void) sprintf(errstr, gettext("Account Usable Control " 4582 "not found")); 4583 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4584 return (NS_LDAP_NOTFOUND); 4585 } 4586 4587 /* Allocate a BER element from the control value and parse it. */ 4588 if ((ber = ber_init(&ectrls[i]->ldctl_value)) == NULL) 4589 return (NS_LDAP_MEMORY); 4590 4591 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 4592 /* Ldap decoding error */ 4593 (void) sprintf(errstr, gettext("Error decoding 1st tag")); 4594 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4595 ber_free(ber, 1); 4596 return (NS_LDAP_INTERNAL); 4597 } 4598 4599 switch (tag) { 4600 case DS52p4_USABLE: 4601 case NEW_USABLE: 4602 acctResp->choice = 0; 4603 if (ber_scanf(ber, "i", &seconds_before_expiry) 4604 == LBER_ERROR) { 4605 /* Ldap decoding error */ 4606 (void) sprintf(errstr, gettext("Can not get " 4607 "seconds_before_expiry")); 4608 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4609 rc = NS_LDAP_INTERNAL; 4610 break; 4611 } 4612 /* ber_scanf() succeeded */ 4613 (acctResp->AcctUsableResp).seconds_before_expiry = 4614 seconds_before_expiry; 4615 break; 4616 4617 case DS52p4_NOT_USABLE: 4618 acctResp->choice = 1; 4619 if (ber_scanf(ber, "{bbb", &inactive, &reset, &expired) 4620 == LBER_ERROR) { 4621 /* Ldap decoding error */ 4622 (void) sprintf(errstr, gettext("Can not get " 4623 "inactive/reset/expired")); 4624 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4625 rc = NS_LDAP_INTERNAL; 4626 break; 4627 } 4628 /* ber_scanf() succeeded */ 4629 (acctResp->AcctUsableResp).more_info.inactive = 4630 ((inactive == 0) ? 0 : 1); 4631 (acctResp->AcctUsableResp).more_info.reset = 4632 ((reset == 0) ? 0 : 1); 4633 (acctResp->AcctUsableResp).more_info.expired = 4634 ((expired == 0) ? 0 : 1); 4635 (acctResp->AcctUsableResp).more_info.rem_grace = 0; 4636 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 0; 4637 4638 if ((tag = ber_peek_tag(ber, &len)) == LBER_ERROR) { 4639 /* this is a success case, break to exit */ 4640 (void) sprintf(errstr, gettext("No optional data")); 4641 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4642 break; 4643 } 4644 4645 /* 4646 * Look at what optional more_info BER element is/are 4647 * left to be decoded. 4648 */ 4649 rc = get_old_acct_opt_more_info(tag, ber, acctResp); 4650 break; 4651 4652 case NEW_NOT_USABLE: 4653 acctResp->choice = 1; 4654 /* 4655 * Recent LDAP servers won't code more_info data for default 4656 * values (see above comments on ASN1 description for what 4657 * fields have default values & what fields are optional). 4658 */ 4659 (acctResp->AcctUsableResp).more_info.inactive = 0; 4660 (acctResp->AcctUsableResp).more_info.reset = 0; 4661 (acctResp->AcctUsableResp).more_info.expired = 0; 4662 (acctResp->AcctUsableResp).more_info.rem_grace = 0; 4663 (acctResp->AcctUsableResp).more_info.sec_b4_unlock = 0; 4664 4665 if (len == 0) { 4666 /* 4667 * Nothing else to decode; this is valid and we 4668 * use default values set above. 4669 */ 4670 (void) sprintf(errstr, gettext("more_info is " 4671 "empty, using default values")); 4672 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4673 break; 4674 } 4675 4676 /* 4677 * Look at what more_info BER element is/are left to 4678 * be decoded. 4679 */ 4680 rc = get_new_acct_more_info(ber, acctResp); 4681 break; 4682 4683 default: 4684 (void) sprintf(errstr, gettext("unknwon coding style " 4685 "(tag: 0x%x)"), tag); 4686 syslog(LOG_DEBUG, "libsldap: %s", errstr); 4687 rc = NS_LDAP_INTERNAL; 4688 break; 4689 } 4690 4691 ber_free(ber, 1); 4692 return (rc); 4693 } 4694 4695 /* 4696 * __ns_ldap_getAcctMgmt() is called from pam account management stack 4697 * for retrieving accounting information of users with no user password - 4698 * eg. rlogin, rsh, etc. This function uses the account management control 4699 * request to do a search on the server for the user in question. The 4700 * response control returned from the server is got from the cookie. 4701 * Input params: username of whose account mgmt information is to be got 4702 * pointer to hold the parsed account management information 4703 * Return values: NS_LDAP_SUCCESS on success or appropriate error 4704 * code on failure 4705 */ 4706 int 4707 __ns_ldap_getAcctMgmt(const char *user, AcctUsableResponse_t *acctResp) 4708 { 4709 int scope, rc; 4710 char ldapfilter[1024]; 4711 ns_ldap_cookie_t *cookie; 4712 ns_ldap_search_desc_t **sdlist = NULL; 4713 ns_ldap_search_desc_t *dptr; 4714 ns_ldap_error_t *error = NULL; 4715 char **dns = NULL; 4716 char service[] = "shadow"; 4717 4718 if (user == NULL || acctResp == NULL) 4719 return (NS_LDAP_INVALID_PARAM); 4720 4721 /* Initialize State machine cookie */ 4722 cookie = init_search_state_machine(); 4723 if (cookie == NULL) 4724 return (NS_LDAP_MEMORY); 4725 4726 /* see if need to follow referrals */ 4727 rc = __s_api_toFollowReferrals(0, 4728 &cookie->followRef, &error); 4729 if (rc != NS_LDAP_SUCCESS) { 4730 (void) __ns_ldap_freeError(&error); 4731 goto out; 4732 } 4733 4734 /* get the service descriptor - or create a default one */ 4735 rc = __s_api_get_SSD_from_SSDtoUse_service(service, 4736 &sdlist, &error); 4737 if (rc != NS_LDAP_SUCCESS) { 4738 (void) __ns_ldap_freeError(&error); 4739 goto out; 4740 } 4741 4742 if (sdlist == NULL) { 4743 /* Create default service Desc */ 4744 sdlist = (ns_ldap_search_desc_t **)calloc(2, 4745 sizeof (ns_ldap_search_desc_t *)); 4746 if (sdlist == NULL) { 4747 rc = NS_LDAP_MEMORY; 4748 goto out; 4749 } 4750 dptr = (ns_ldap_search_desc_t *) 4751 calloc(1, sizeof (ns_ldap_search_desc_t)); 4752 if (dptr == NULL) { 4753 free(sdlist); 4754 rc = NS_LDAP_MEMORY; 4755 goto out; 4756 } 4757 sdlist[0] = dptr; 4758 4759 /* default base */ 4760 rc = __s_api_getDNs(&dns, service, &cookie->errorp); 4761 if (rc != NS_LDAP_SUCCESS) { 4762 if (dns) { 4763 __s_api_free2dArray(dns); 4764 dns = NULL; 4765 } 4766 (void) __ns_ldap_freeError(&(cookie->errorp)); 4767 cookie->errorp = NULL; 4768 goto out; 4769 } 4770 dptr->basedn = strdup(dns[0]); 4771 if (dptr->basedn == NULL) { 4772 free(sdlist); 4773 free(dptr); 4774 if (dns) { 4775 __s_api_free2dArray(dns); 4776 dns = NULL; 4777 } 4778 rc = NS_LDAP_MEMORY; 4779 goto out; 4780 } 4781 __s_api_free2dArray(dns); 4782 dns = NULL; 4783 4784 /* default scope */ 4785 scope = 0; 4786 rc = __s_api_getSearchScope(&scope, &cookie->errorp); 4787 dptr->scope = scope; 4788 } 4789 4790 cookie->sdlist = sdlist; 4791 4792 cookie->service = strdup(service); 4793 if (cookie->service == NULL) { 4794 rc = NS_LDAP_MEMORY; 4795 goto out; 4796 } 4797 4798 /* search for entries for this particular uid */ 4799 (void) snprintf(ldapfilter, sizeof (ldapfilter), "(uid=%s)", user); 4800 cookie->i_filter = strdup(ldapfilter); 4801 if (cookie->i_filter == NULL) { 4802 rc = NS_LDAP_MEMORY; 4803 goto out; 4804 } 4805 4806 /* create the control request */ 4807 if ((rc = setup_acctmgmt_params(cookie)) != NS_LDAP_SUCCESS) 4808 goto out; 4809 4810 /* Process search */ 4811 rc = search_state_machine(cookie, GET_ACCT_MGMT_INFO, 0); 4812 4813 /* Copy results back to user */ 4814 rc = cookie->err_rc; 4815 if (rc != NS_LDAP_SUCCESS) 4816 (void) __ns_ldap_freeError(&(cookie->errorp)); 4817 4818 if (cookie->result == NULL) 4819 goto out; 4820 4821 if ((rc = parse_acct_cont_resp_msg(cookie->resultctrl, acctResp)) 4822 != NS_LDAP_SUCCESS) 4823 goto out; 4824 4825 rc = NS_LDAP_SUCCESS; 4826 4827 out: 4828 delete_search_cookie(cookie); 4829 4830 return (rc); 4831 } 4832