1 package org.enhydra.shark.usergroup; 2 import org.enhydra.shark.api.internal.working.CallbackUtilities; 3 import org.enhydra.shark.utilities.LRUMap; 4 import java.util.*; 5 import javax.naming.*; 6 import javax.naming.directory.*; 7 8 14 public class LDAPClient { 15 16 private final String boundary=","; 17 18 private LDAPOptions ldapOptions; 19 20 private CallbackUtilities cus; 21 22 private java.util.Properties env = new java.util.Properties (); 23 24 private SearchControls constraints=new SearchControls(); 25 26 protected static LRUMap userAttributes=null; 27 28 protected static LRUMap groupAttributes=null; 29 30 33 public LDAPClient (CallbackUtilities cus) { 34 this.cus=cus; 36 ldapOptions=new LDAPOptions(cus); 37 41 env = new java.util.Properties (); 42 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 43 env.put(javax.naming.Context.REFERRAL, "throw"); 44 env.put(javax.naming.Context.PROVIDER_URL, "ldap://" + ldapOptions.getHost() + ":" + ldapOptions.getPort()); 45 env.put(javax.naming.Context.SECURITY_PRINCIPAL, ldapOptions.getUser()); 46 env.put(javax.naming.Context.SECURITY_CREDENTIALS, ldapOptions.getPassword()); 47 48 constraints.setSearchScope(ldapOptions.getSearchScope()); 50 constraints.setCountLimit(0); 51 if (LDAPClient.userAttributes==null) { 52 int uatts=0; 53 try { 54 uatts=Integer.parseInt(cus.getProperty("LDAPClient.userAttributesCacheSize","0")); 55 } catch (Exception ex) {} 56 LDAPClient.userAttributes=new LRUMap(uatts); 58 } 59 if (LDAPClient.groupAttributes==null) { 60 int gatts=0; 61 try { 62 gatts=Integer.parseInt(cus.getProperty("LDAPClient.groupAttributesCacheSize","0")); 63 } catch (Exception ex) {} 64 LDAPClient.groupAttributes=new LRUMap(gatts); 66 } 67 } 68 69 75 public LDAPOptions getLDAPOptions () { 76 return ldapOptions; 77 } 78 79 80 85 public java.util.List getAllOrganizationalUnitEntries () throws Throwable { 86 java.util.List entries=new java.util.ArrayList (); 88 String base = ldapOptions.getSearchBase(); 89 String filter=""; 90 String [] objClasses; 91 Iterator results; 92 SearchResult sr; 93 Attributes attributes; 94 String dn = null; 95 96 if (ldapOptions.getStructureType() == 1) { 97 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 98 if (objClasses!=null && objClasses.length>0) { 99 if (objClasses.length>1) { 100 filter+="(|"; 101 } 102 for (int i=0; i<objClasses.length; i++) { 103 filter+="(objectClass="+objClasses[i]+")"; 104 } 105 if (objClasses.length>1) { 106 filter+=")"; 107 } 108 } 109 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupGroupsName()+ ")"+filter+")"; 110 results=getEntries(filter).iterator(); 111 if (results.hasNext()) { 112 sr = (SearchResult) results.next(); 113 try { 115 dn = sr.getName(); 116 } 117 catch (Exception e){} 118 if (!dn.equals("")){ 119 if (!base.equals("")){ 120 dn = dn + "," + base; 121 } 122 } 123 else { 124 dn = base; 125 } 126 ldapOptions.setSearchBase(dn); 127 128 filter=""; 129 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 130 if (objClasses!=null && objClasses.length>0) { 131 if (objClasses.length>1) { 132 filter+="(|"; 133 } 134 for (int i=0; i<objClasses.length; i++) { 135 filter+="(objectClass="+objClasses[i]+")"; 136 } 137 if (objClasses.length>1) { 138 filter+=")"; 139 } 140 } 141 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=*)"+filter+")"; 142 results=getEntries(filter).iterator(); 143 ldapOptions.setSearchBase(base); 144 145 while (results.hasNext()) { 146 sr = (SearchResult) results.next(); 147 attributes = sr.getAttributes(); 148 try { 149 String id=(String )attributes.get(ldapOptions.getGroupUniqueAttributeName()).get(); 150 if (!id.equals(ldapOptions.getGroupGroupsName())) { 151 entries.add(id); 152 } 153 if (LDAPClient.groupAttributes.getMaximumSize()>0) { 154 LDAPClient.groupAttributes.put(id, attributes); 155 156 } 157 } 158 catch (Exception e){} 159 } } } else { 163 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 164 if (objClasses!=null && objClasses.length>0) { 165 if (objClasses.length>1) { 166 filter+="(|"; 167 } 168 for (int i=0; i<objClasses.length; i++) { 169 filter+="(objectClass="+objClasses[i]+")"; 170 } 171 if (objClasses.length>1) { 172 filter+=")"; 173 } 174 } 175 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=*)"+filter+")"; 176 results=getEntries(filter).iterator(); 177 178 while (results.hasNext()) { 179 sr = (SearchResult) results.next(); 180 attributes = sr.getAttributes(); 181 try { 182 String id=(String )attributes.get(ldapOptions.getGroupUniqueAttributeName()).get(); 183 entries.add(id); 184 if (LDAPClient.groupAttributes.getMaximumSize()>0) { 185 LDAPClient.groupAttributes.put(id, attributes); 186 } 187 } 188 catch (Exception ex) {} 189 } } return entries; 192 } 193 194 199 public java.util.List getAllUserEntries () throws Throwable { 200 java.util.List entries=new java.util.ArrayList (); 202 String base = ldapOptions.getSearchBase(); 203 String filter=""; 204 String [] objClasses; 205 Iterator results; 206 SearchResult sr; 207 Attributes attributes; 208 String dn = null; 209 210 if (ldapOptions.getStructureType() == 1) { 211 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 212 if (objClasses!=null && objClasses.length>0) { 213 if (objClasses.length>1) { 214 filter+="(|"; 215 } 216 for (int i=0; i<objClasses.length; i++) { 217 filter+="(objectClass="+objClasses[i]+")"; 218 } 219 if (objClasses.length>1) { 220 filter+=")"; 221 } 222 } 223 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupUsersName()+ ")"+filter+")"; 224 results=getEntries(filter).iterator(); 225 if (results.hasNext()) { 226 sr = (SearchResult) results.next(); 227 try { 229 dn = sr.getName(); 230 } 231 catch (Exception e){} 232 if (!dn.equals("")){ 233 if (!base.equals("")){ 234 dn = dn + "," + base; 235 } 236 } 237 else { 238 dn = base; 239 } 240 ldapOptions.setSearchBase(dn); 241 242 filter=""; 243 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 244 if (objClasses!=null && objClasses.length>0) { 245 if (objClasses.length>1) { 246 filter+="(|"; 247 } 248 for (int i=0; i<objClasses.length; i++) { 249 filter+="(objectClass="+objClasses[i]+")"; 250 } 251 if (objClasses.length>1) { 252 filter+=")"; 253 } 254 } 255 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"=*)"+filter+")"; 256 results=getEntries(filter).iterator(); 257 ldapOptions.setSearchBase(base); 258 259 while (results.hasNext()) { 260 sr = (SearchResult) results.next(); 261 attributes = sr.getAttributes(); 262 try { 263 String id=(String )attributes.get(ldapOptions.getUserUniqueAttributeName()).get(); 264 entries.add(id); 265 if (LDAPClient.userAttributes.getMaximumSize()>0) { 266 LDAPClient.userAttributes.put(id, attributes); 267 } 268 } 269 catch (Exception e){} 270 } } } else { 274 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 275 if (objClasses!=null && objClasses.length>0) { 276 if (objClasses.length>1) { 277 filter+="(|"; 278 } 279 for (int i=0; i<objClasses.length; i++) { 280 filter+="(objectClass="+objClasses[i]+")"; 281 } 282 if (objClasses.length>1) { 283 filter+=")"; 284 } 285 } 286 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"=*)"+filter+")"; 287 results=getEntries(filter).iterator(); 288 289 while (results.hasNext()) { 290 sr = (SearchResult) results.next(); 291 attributes = sr.getAttributes(); 292 try { 293 String id=(String )attributes.get(ldapOptions.getUserUniqueAttributeName()).get(); 294 entries.add(id); 295 if (LDAPClient.userAttributes.getMaximumSize()>0) { 296 LDAPClient.userAttributes.put(id, attributes); 297 } 298 } catch (Exception ex) {} 299 } 300 } return entries; 302 } 303 304 309 public java.util.List getAllUserEntries (String groupName) throws Throwable { 310 if (!doesGroupExist(groupName)){ 312 throw new Exception ("Group " + groupName + "does not exist."); 313 } 314 java.util.List entries=new java.util.ArrayList (); 315 String base = ldapOptions.getSearchBase(); 316 String filter=""; 317 String [] objClasses; 318 Iterator results; 319 SearchResult sr; 320 Attributes attributes; 321 String dn = null; 322 323 if (ldapOptions.getStructureType() == 1){ 324 entries = getAllImmediateUserEntries(groupName); 325 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 326 if (objClasses!=null && objClasses.length>0) { 327 if (objClasses.length>1) { 328 filter+="(|"; 329 } 330 for (int i=0; i<objClasses.length; i++) { 331 filter+="(objectClass="+objClasses[i]+")"; 332 } 333 if (objClasses.length>1) { 334 filter+=")"; 335 } 336 } 337 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupGroupRelationsName() + ")"+filter+")"; 338 results=getEntries(filter).iterator(); 339 if (results.hasNext()){ 340 sr = (SearchResult) results.next(); 341 try { 343 dn = sr.getName(); 344 } 345 catch (Exception e){} 346 if (!dn.equals("")){ 347 if (!base.equals("")){ 348 dn = dn + "," + base; 349 } 350 } 351 else { 352 dn = base; 353 } 354 ldapOptions.setSearchBase(dn); 355 constraints.setSearchScope(1); 356 357 filter=""; 358 objClasses=tokenize(ldapOptions.getRelationObjectClasses(),boundary); 359 if (objClasses!=null && objClasses.length>0) { 360 if (objClasses.length>1) { 361 filter+="(|"; 362 } 363 for (int i=0; i<objClasses.length; i++) { 364 filter+="(objectClass="+objClasses[i]+")"; 365 } 366 if (objClasses.length>1) { 367 filter+=")"; 368 } 369 } 370 filter="(&("+ldapOptions.getRelationUniqueAttributeName()+"="+groupName+")"+filter+")"; 371 results=getEntries(filter).iterator(); 372 ldapOptions.setSearchBase(dn); constraints.setSearchScope(2); 375 if (results.hasNext()) { 376 filter=""; 377 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 378 if (objClasses!=null && objClasses.length>0) { 379 if (objClasses.length>1) { 380 filter+="(|"; 381 } 382 for (int i=0; i<objClasses.length; i++) { 383 filter+="(objectClass="+objClasses[i]+")"; 384 } 385 if (objClasses.length>1) { 386 filter+=")"; 387 } 388 } 389 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=*)"+filter+")"; 390 sr = (SearchResult) results.next(); 391 attributes = sr.getAttributes(); 392 Attribute attribute = attributes.get("member"); 393 394 NamingEnumeration ne = attribute.getAll(); 395 while (ne.hasMore()){ 396 String member_dn = (String ) ne.next(); 397 ldapOptions.setSearchBase(member_dn); 398 constraints.setSearchScope(0); 399 Iterator elems = getEntries(filter).iterator(); 400 SearchResult sres = (SearchResult) elems.next(); 401 Attributes attrs = sres.getAttributes(); 402 try { 403 String id=(String )attrs.get(ldapOptions.getGroupUniqueAttributeName()).get(); 404 if (LDAPClient.userAttributes.getMaximumSize()>0) { 405 LDAPClient.userAttributes.put(id, attrs); 406 } 407 ldapOptions.setSearchBase(base); 408 constraints.setSearchScope(2); 409 List l = getAllUserEntries(id); 410 if (l!=null && !l.isEmpty()){ 411 for (int i=0;i<l.size();i++){ 412 if (!entries.contains((String )l.get(i))){ 413 entries.add((String )l.get(i)); 414 } 415 } 416 } 417 } 418 catch (Exception e){} 419 } } } } else { 424 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 425 if (objClasses!=null && objClasses.length>0) { 426 if (objClasses.length>1) { 427 filter+="(|"; 428 } 429 for (int i=0; i<objClasses.length; i++) { 430 filter+="(objectClass="+objClasses[i]+")"; 431 } 432 if (objClasses.length>1) { 433 filter+=")"; 434 } 435 } 436 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 437 results=getEntries(filter).iterator(); 439 if (results.hasNext()){ 440 sr = (SearchResult) results.next(); 441 try { 443 dn = sr.getName(); 444 } 445 catch (Exception e){} 446 if (!dn.equals("")){ 448 if (!base.equals("")) { 449 dn = dn + "," + base; 450 } 451 } 452 else { 453 dn = base; 454 } 455 ldapOptions.setSearchBase(dn); 457 filter=""; 458 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 459 if (objClasses!=null && objClasses.length>0) { 460 if (objClasses.length>1) { 461 filter+="(|"; 462 } 463 for (int i=0; i<objClasses.length; i++) { 464 filter+="(objectClass="+objClasses[i]+")"; 465 } 466 if (objClasses.length>1) { 467 filter+=")"; 468 } 469 } 470 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"="+"*)"+filter+")"; 471 results=getEntries(filter).iterator(); 473 while (results.hasNext()) { 474 sr = (SearchResult) results.next(); 475 attributes = sr.getAttributes(); 476 try { 477 String id=(String )attributes.get(ldapOptions.getUserUniqueAttributeName()).get(); 478 entries.add(id); 480 if (LDAPClient.userAttributes.getMaximumSize()>0) { 481 LDAPClient.userAttributes.put(id, attributes); 482 } 483 } catch (Exception ex) {} 484 } 485 } } ldapOptions.setSearchBase(base); 488 constraints.setSearchScope(2); 489 return entries; 490 } 491 492 497 public java.util.List getAllImmediateUserEntries (String groupName) throws Throwable { 498 if (!doesGroupExist(groupName)){ 500 throw new Exception ("Group " + groupName + "does not exist."); 501 } 502 java.util.List entries=new java.util.ArrayList (); 503 String base = ldapOptions.getSearchBase(); 504 String filter=""; 505 String [] objClasses; 506 Iterator results; 507 SearchResult sr; 508 Attributes attributes; 509 String dn = null; 510 511 if (ldapOptions.getStructureType() == 1){ 512 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 513 if (objClasses!=null && objClasses.length>0) { 514 if (objClasses.length>1) { 515 filter+="(|"; 516 } 517 for (int i=0; i<objClasses.length; i++) { 518 filter+="(objectClass="+objClasses[i]+")"; 519 } 520 if (objClasses.length>1) { 521 filter+=")"; 522 } 523 } 524 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupUserRelationsName() + ")"+filter+")"; 525 results=getEntries(filter).iterator(); 526 if (results.hasNext()) { 527 sr = (SearchResult) results.next(); 528 try { 530 dn = sr.getName(); 531 } 532 catch (Exception e){} 533 if (!dn.equals("")){ 534 if (!base.equals("")){ 535 dn = dn + "," + base; 536 } 537 } 538 else { 539 dn = base; 540 } 541 ldapOptions.setSearchBase(dn); 542 constraints.setSearchScope(1); 543 544 filter=""; 545 objClasses=tokenize(ldapOptions.getRelationObjectClasses(),boundary); 546 if (objClasses!=null && objClasses.length>0) { 547 if (objClasses.length>1) { 548 filter+="(|"; 549 } 550 for (int i=0; i<objClasses.length; i++) { 551 filter+="(objectClass="+objClasses[i]+")"; 552 } 553 if (objClasses.length>1) { 554 filter+=")"; 555 } 556 } 557 filter="(&("+ldapOptions.getRelationUniqueAttributeName()+"="+groupName+")"+filter+")"; 558 results=getEntries(filter).iterator(); 559 constraints.setSearchScope(2); 560 ldapOptions.setSearchBase(base); 561 562 if (results.hasNext()) { 563 filter=""; 564 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 565 if (objClasses!=null && objClasses.length>0) { 566 if (objClasses.length>1) { 567 filter+="(|"; 568 } 569 for (int i=0; i<objClasses.length; i++) { 570 filter+="(objectClass="+objClasses[i]+")"; 571 } 572 if (objClasses.length>1) { 573 filter+=")"; 574 } 575 } 576 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"=*)"+filter+")"; 577 578 sr = (SearchResult) results.next(); 579 attributes = sr.getAttributes(); 580 Attribute attribute = attributes.get("member"); 581 NamingEnumeration na = attribute.getAll(); 582 583 while (na.hasMore()){ 584 String member_dn = (String ) na.next(); 585 ldapOptions.setSearchBase(member_dn); 586 constraints.setSearchScope(0); 587 Iterator elems = getEntries(filter).iterator(); 588 SearchResult sres = (SearchResult) elems.next(); 589 Attributes attrs = sres.getAttributes(); 590 try { 591 String id=(String )attrs.get(ldapOptions.getUserUniqueAttributeName()).get(); 592 if (!entries.contains(id)){ 593 entries.add(id); 594 } 595 if (LDAPClient.userAttributes.getMaximumSize()>0) { 596 LDAPClient.userAttributes.put(id, attrs); 597 } 598 } 599 catch (Exception e){} 600 } } } } else { 605 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 606 if (objClasses!=null && objClasses.length>0) { 607 if (objClasses.length>1) { 608 filter+="(|"; 609 } 610 for (int i=0; i<objClasses.length; i++) { 611 filter+="(objectClass="+objClasses[i]+")"; 612 } 613 if (objClasses.length>1) { 614 filter+=")"; 615 } 616 } 617 filter = "(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 618 results=getEntries(filter).iterator(); 619 if (results.hasNext()){ 620 sr = (SearchResult) results.next(); 621 attributes = sr.getAttributes(); 622 try { 623 dn = sr.getName(); 624 } 625 catch (Exception e){} 626 constraints.setSearchScope(1); 627 if (!dn.equals("")){ 628 if (!base.equals("")) { 629 dn = dn + "," + base; 630 } 631 } 632 else { 633 dn = base; 634 } 635 ldapOptions.setSearchBase(dn); 636 637 filter = ""; 638 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 639 if (objClasses!=null && objClasses.length>0) { 640 if (objClasses.length>1) { 641 filter+="(|"; 642 } 643 for (int i=0; i<objClasses.length; i++) { 644 filter+="(objectClass="+objClasses[i]+")"; 645 } 646 if (objClasses.length>1) { 647 filter+=")"; 648 } 649 } 650 filter = "(&("+ldapOptions.getUserUniqueAttributeName()+"=*)"+filter+")"; 651 652 results=getEntries(filter).iterator(); 653 while (results.hasNext()) { 654 sr = (SearchResult) results.next(); 655 attributes = sr.getAttributes(); 656 try { 657 String id=(String )attributes.get(ldapOptions.getUserUniqueAttributeName()).get(); 658 entries.add(id); 659 if (LDAPClient.userAttributes.getMaximumSize()>0) { 660 LDAPClient.userAttributes.put(id, attributes); 661 } 662 } catch (Exception ex) {} 663 } 664 } } constraints.setSearchScope(2); 667 ldapOptions.setSearchBase(base); 668 return entries; 669 } 670 671 676 public java.util.List getAllSubOrganizationalUnitEntries (String groupName) throws Throwable { 677 if (!doesGroupExist(groupName)){ 679 throw new Exception ("Group " + groupName + "does not exist."); 680 } 681 java.util.List entries=new java.util.ArrayList (); 682 String base = ldapOptions.getSearchBase(); 683 int scope = constraints.getSearchScope(); 684 String filter=""; 685 String [] objClasses; 686 Iterator results; 687 SearchResult sr; 688 Attributes attributes; 689 String dn = null; 690 691 if (ldapOptions.getStructureType() == 1) { 692 List tempEntries = getAllImmediateSubOrganizationalUnitEntries(groupName); 693 if (tempEntries!=null && !tempEntries.isEmpty()){ 694 for (int i=0;i<tempEntries.size();i++){ 695 if (!entries.contains(tempEntries.get(i))){ 696 entries.add(tempEntries.get(i)); 697 List subResults = getAllSubOrganizationalUnitEntries((String )tempEntries.get(i)); 698 if (subResults!= null && !subResults.isEmpty()){ 699 for (int j=0;j<subResults.size();j++){ 700 if (!entries.contains((String )subResults.get(i))){ 701 entries.add((String )subResults.get(j)); 702 } 703 } 704 } 705 } 706 } 707 } 708 } else { 710 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 711 if (objClasses!=null && objClasses.length>0) { 712 if (objClasses.length>1) { 713 filter+="(|"; 714 } 715 for (int i=0; i<objClasses.length; i++) { 716 filter+="(objectClass="+objClasses[i]+")"; 717 } 718 if (objClasses.length>1) { 719 filter+=")"; 720 } 721 } 722 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 723 results=getEntries(filter).iterator(); 724 if (results.hasNext()){ 725 sr = (SearchResult) results.next(); 726 try { 728 dn = sr.getName(); 729 } 730 catch (Exception e){} 731 if (!dn.equals("")){ 732 if (!base.equals("")) { 733 dn = dn + "," + base; 734 } 735 } 736 else { 737 dn = base; 738 } 739 ldapOptions.setSearchBase(dn); 740 741 filter=""; 742 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 743 if (objClasses!=null && objClasses.length>0) { 744 if (objClasses.length>1) { 745 filter+="(|"; 746 } 747 for (int i=0; i<objClasses.length; i++) { 748 filter+="(objectClass="+objClasses[i]+")"; 749 } 750 if (objClasses.length>1) { 751 filter+=")"; 752 } 753 } 754 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+"*)"+filter+")"; 755 results=getEntries(filter).iterator(); 756 while (results.hasNext()) { 757 sr = (SearchResult) results.next(); 758 attributes = sr.getAttributes(); 759 try { 760 String id=(String )attributes.get(ldapOptions.getGroupUniqueAttributeName()).get(); 761 if (!id.equals(groupName)){ 762 entries.add(id); 763 } 764 if (LDAPClient.groupAttributes.getMaximumSize()>0) { 765 LDAPClient.groupAttributes.put(id, attributes); 766 } 767 } catch (Exception ex) {} 768 } 769 } ldapOptions.setSearchBase(base); 771 } return entries; 773 } 774 775 780 public java.util.List getAllImmediateSubOrganizationalUnitEntries (String groupName) throws Throwable { 781 if (!doesGroupExist(groupName)){ 783 throw new Exception ("Group " + groupName + "does not exist."); 784 } 785 java.util.List entries=new java.util.ArrayList (); 786 String base = ldapOptions.getSearchBase(); 787 String filter=""; 788 String [] objClasses; 789 Iterator results; 790 SearchResult sr; 791 Attributes attributes; 792 String dn = null; 793 794 if (ldapOptions.getStructureType() == 1) { 795 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 796 if (objClasses!=null && objClasses.length>0) { 797 if (objClasses.length>1) { 798 filter+="(|"; 799 } 800 for (int i=0; i<objClasses.length; i++) { 801 filter+="(objectClass="+objClasses[i]+")"; 802 } 803 if (objClasses.length>1) { 804 filter+=")"; 805 } 806 } 807 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupGroupRelationsName() + ")"+filter+")"; 808 results=getEntries(filter).iterator(); 809 if (results.hasNext()) { 810 sr = (SearchResult) results.next(); 811 attributes = sr.getAttributes(); 812 try { 813 dn = sr.getName(); 814 } 815 catch (Exception e){} 816 if (!dn.equals("")){ 817 if (!base.equals("")){ 818 dn = dn + "," + base; 819 } 820 } 821 else { 822 dn = base; 823 } 824 constraints.setSearchScope(1); 825 ldapOptions.setSearchBase(dn); 826 827 filter=""; 828 objClasses=tokenize(ldapOptions.getRelationObjectClasses(),boundary); 829 if (objClasses!=null && objClasses.length>0) { 830 if (objClasses.length>1) { 831 filter+="(|"; 832 } 833 for (int i=0; i<objClasses.length; i++) { 834 filter+="(objectClass="+objClasses[i]+")"; 835 } 836 if (objClasses.length>1) { 837 filter+=")"; 838 } 839 } 840 filter="(&("+ldapOptions.getRelationUniqueAttributeName()+"="+groupName+")"+filter+")"; 841 results=getEntries(filter).iterator(); 842 constraints.setSearchScope(2); 843 ldapOptions.setSearchBase(base); 844 845 if (results.hasNext()) { 846 filter=""; 847 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 848 if (objClasses!=null && objClasses.length>0) { 849 if (objClasses.length>1) { 850 filter+="(|"; 851 } 852 for (int i=0; i<objClasses.length; i++) { 853 filter+="(objectClass="+objClasses[i]+")"; 854 } 855 if (objClasses.length>1) { 856 filter+=")"; 857 } 858 } 859 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=*)"+filter+")"; 860 sr = (SearchResult) results.next(); 861 attributes = sr.getAttributes(); 862 Attribute attribute = attributes.get("member"); 863 NamingEnumeration ne = attribute.getAll(); 864 while (ne.hasMore()){ 865 String member_dn = (String ) ne.next(); 866 ldapOptions.setSearchBase(member_dn); 867 constraints.setSearchScope(0); 868 Iterator elems = getEntries(filter).iterator(); 869 SearchResult sres = (SearchResult) elems.next(); 870 Attributes attrs = sres.getAttributes(); 871 try { 872 String id=(String )attrs.get(ldapOptions.getGroupUniqueAttributeName()).get(); 873 if (!entries.contains(id)){ 874 entries.add(id); 875 } 876 if (LDAPClient.groupAttributes.getMaximumSize()>0) { 877 LDAPClient.groupAttributes.put(id, attrs); 878 } 879 } 880 catch (Exception e){} 881 } } } } else { 886 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 887 if (objClasses!=null && objClasses.length>0) { 888 if (objClasses.length>1) { 889 filter+="(|"; 890 } 891 for (int i=0; i<objClasses.length; i++) { 892 filter+="(objectClass="+objClasses[i]+")"; 893 } 894 if (objClasses.length>1) { 895 filter+=")"; 896 } 897 } 898 String groupSpecFilter = "(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 899 results=getEntries(groupSpecFilter).iterator(); 900 if (results.hasNext()){ 901 sr = (SearchResult) results.next(); 902 try { 904 dn = sr.getName(); 905 } 906 catch (Exception e){} 907 constraints.setSearchScope(1); 908 if (!dn.equals("")){ 909 if (!base.equals("")) { 910 dn = dn + "," + base; 911 } 912 } 913 else { 914 dn = base; 915 } 916 ldapOptions.setSearchBase(dn); 917 filter = "(&("+ldapOptions.getGroupUniqueAttributeName()+"=*)"+filter+")"; 918 results=getEntries(filter).iterator(); 919 while (results.hasNext()) { 920 sr = (SearchResult) results.next(); 921 attributes = sr.getAttributes(); 922 try { 923 String id=(String )attributes.get(ldapOptions.getGroupUniqueAttributeName()).get(); 924 entries.add(id); 925 if (LDAPClient.groupAttributes.getMaximumSize()>0) { 926 LDAPClient.groupAttributes.put(id, attributes); 927 } 928 } catch (Exception ex) {} 929 } 930 } } constraints.setSearchScope(2); 933 ldapOptions.setSearchBase(base); 934 return entries; 935 } 936 937 942 public String getUserAttribute (String username,String ldapAttrib) throws Throwable { 943 String ret=null; 945 if (LDAPClient.userAttributes.getMaximumSize()>0) { 946 Attributes attributes = (Attributes)LDAPClient.userAttributes.get(username); 947 if (attributes != null) { 948 Object o=attributes.get(ldapAttrib).get(); 949 if (o!=null) ret=o.toString(); 950 return ret; 951 } 952 } 953 954 String filter=""; 955 String [] objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 956 if (objClasses!=null && objClasses.length>0) { 957 if (objClasses.length>1) { 958 filter+="(|"; 959 } 960 for (int i=0; i<objClasses.length; i++) { 961 filter+="(objectClass="+objClasses[i]+")"; 962 } 963 if (objClasses.length>1) { 964 filter+=")"; 965 } 966 } 967 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"="+username+")"+filter+")"; 968 969 List l=getEntries(filter); 970 if (l.size()==0) throw new Exception (); 971 Iterator results=getEntries(filter).iterator(); 972 973 while (results.hasNext()) { 974 SearchResult sr = (SearchResult) results.next(); 975 Attributes attributes = sr.getAttributes(); 976 ret=(String )attributes.get(ldapAttrib).get(); 977 if (ret!=null) { 978 break; 979 } 980 } 981 return ret; 982 } 983 984 989 public String getGroupAttribute (String groupName,String ldapAttrib) throws Throwable { 990 String ret=null; 992 if (LDAPClient.groupAttributes.getMaximumSize()>0) { 993 Attributes attributes = (Attributes)LDAPClient.groupAttributes.get(groupName); 994 if (attributes != null) { 995 ret=(String )attributes.get(ldapAttrib).get(); 996 return ret; 997 } 998 } 999 String filter=""; 1000 String [] objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1001 if (objClasses!=null && objClasses.length>0) { 1002 if (objClasses.length>1) { 1003 filter+="(|"; 1004 } 1005 for (int i=0; i<objClasses.length; i++) { 1006 filter+="(objectClass="+objClasses[i]+")"; 1007 } 1008 if (objClasses.length>1) { 1009 filter+=")"; 1010 } 1011 } 1012 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 1013 1014 List l=getEntries(filter); 1015 if (l.size()==0) throw new Exception (); 1016 Iterator results=getEntries(filter).iterator(); 1017 while (results.hasNext()) { 1018 SearchResult sr = (SearchResult) results.next(); 1019 Attributes attributes = sr.getAttributes(); 1020 ret=(String )attributes.get(ldapAttrib).get(); 1021 if (ret!=null) { 1022 break; 1023 } 1024 } 1025 return ret; 1026 } 1027 1028 1033 public boolean doesGroupExist (String groupName) throws Throwable { 1034 String base = ldapOptions.getSearchBase(); 1036 String filter=""; 1037 String [] objClasses; 1038 Iterator results; 1039 SearchResult sr; 1040 String dn = null; 1041 int count = 0; 1042 1043 if (ldapOptions.getStructureType() == 1) { 1044 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1045 if (objClasses!=null && objClasses.length>0) { 1046 if (objClasses.length>1) { 1047 filter+="(|"; 1048 } 1049 for (int i=0; i<objClasses.length; i++) { 1050 filter+="(objectClass="+objClasses[i]+")"; 1051 } 1052 if (objClasses.length>1) { 1053 filter+=")"; 1054 } 1055 } 1056 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupGroupsName()+ ")"+filter+")"; 1057 results=getEntries(filter).iterator(); 1058 if (results.hasNext()) { 1059 sr = (SearchResult) results.next(); 1060 try { 1062 dn = sr.getName(); 1063 } 1064 catch (Exception e){} 1065 if (!dn.equals("")){ 1066 if (!base.equals("")){ 1067 dn = dn + "," + base; 1068 } 1069 } 1070 else { 1071 dn = base; 1072 } 1073 ldapOptions.setSearchBase(dn); 1074 filter = ""; 1075 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1076 if (objClasses!=null && objClasses.length>0) { 1077 if (objClasses.length>1) { 1078 filter+="(|"; 1079 } 1080 for (int i=0; i<objClasses.length; i++) { 1081 filter+="(objectClass="+objClasses[i]+")"; 1082 } 1083 if (objClasses.length>1) { 1084 filter+=")"; 1085 } 1086 } 1087 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 1088 count = getEntries(filter).size(); 1089 ldapOptions.setSearchBase(base); 1090 } 1091 } else { 1093 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1094 if (objClasses!=null && objClasses.length>0) { 1095 if (objClasses.length>1) { 1096 filter+="(|"; 1097 } 1098 for (int i=0; i<objClasses.length; i++) { 1099 filter+="(objectClass="+objClasses[i]+")"; 1100 } 1101 if (objClasses.length>1) { 1102 filter+=")"; 1103 } 1104 } 1105 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 1106 count = getEntries(filter).size(); 1107 } 1108 1109 return count>0; 1110 } 1111 1112 1117 public boolean doesUserExist (String username) throws Throwable { 1118 String base = ldapOptions.getSearchBase(); 1120 String filter=""; 1121 String [] objClasses; 1122 Iterator results; 1123 SearchResult sr; 1124 String dn = null; 1125 int count = 0; 1126 1127 if (ldapOptions.getStructureType() == 1) { 1128 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1129 if (objClasses!=null && objClasses.length>0) { 1130 if (objClasses.length>1) { 1131 filter+="(|"; 1132 } 1133 for (int i=0; i<objClasses.length; i++) { 1134 filter+="(objectClass="+objClasses[i]+")"; 1135 } 1136 if (objClasses.length>1) { 1137 filter+=")"; 1138 } 1139 } 1140 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupUsersName()+ ")"+filter+")"; 1141 results=getEntries(filter).iterator(); 1142 if (results.hasNext()) { 1143 sr = (SearchResult) results.next(); 1144 try { 1146 dn = sr.getName(); 1147 } 1148 catch (Exception e){} 1149 if (!dn.equals("")){ 1150 if (!base.equals("")){ 1151 dn = dn + "," + base; 1152 } 1153 } 1154 else { 1155 dn = base; 1156 } 1157 ldapOptions.setSearchBase(dn); 1158 filter = ""; 1159 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 1160 if (objClasses!=null && objClasses.length>0) { 1161 if (objClasses.length>1) { 1162 filter+="(|"; 1163 } 1164 for (int i=0; i<objClasses.length; i++) { 1165 filter+="(objectClass="+objClasses[i]+")"; 1166 } 1167 if (objClasses.length>1) { 1168 filter+=")"; 1169 } 1170 } 1171 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"="+username+")"+filter+")"; 1172 count = getEntries(filter).size(); 1173 ldapOptions.setSearchBase(base); 1174 } 1175 } 1176 else { objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 1178 if (objClasses!=null && objClasses.length>0) { 1179 if (objClasses.length>1) { 1180 filter+="(|"; 1181 } 1182 for (int i=0; i<objClasses.length; i++) { 1183 filter+="(objectClass="+objClasses[i]+")"; 1184 } 1185 if (objClasses.length>1) { 1186 filter+=")"; 1187 } 1188 } 1189 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"="+username+")"+filter+")"; 1190 count = getEntries(filter).size(); 1191 } 1192 1193 return count>0; 1194 } 1195 1196 1201 public boolean doesUserBelongToGroup (String groupName,String username) throws Throwable { 1202 if (!doesGroupExist(groupName)){ 1204 throw new Exception ("Group " + groupName + "does not exist."); 1205 } 1206 if (!doesUserExist(username)){ 1207 throw new Exception ("User " + username + "does not exist."); 1208 } 1209 String base = ldapOptions.getSearchBase(); 1210 String filter=""; 1211 String [] objClasses; 1212 Iterator results; 1213 SearchResult sr; 1214 Attributes attributes; 1215 String dn = null; 1216 1217 if (ldapOptions.getStructureType() == 1) { 1218 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1219 if (objClasses!=null && objClasses.length>0) { 1220 if (objClasses.length>1) { 1221 filter+="(|"; 1222 } 1223 for (int i=0; i<objClasses.length; i++) { 1224 filter+="(objectClass="+objClasses[i]+")"; 1225 } 1226 if (objClasses.length>1) { 1227 filter+=")"; 1228 } 1229 } 1230 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupUserRelationsName() +")"+filter+")"; 1231 results=getEntries(filter).iterator(); 1232 if (results.hasNext()) { 1233 sr = (SearchResult) results.next(); 1234 try { 1236 dn = sr.getName(); 1237 } 1238 catch (Exception e){} 1239 if (!dn.equals("")){ 1240 if (!base.equals("")){ 1241 dn = dn + "," + base; 1242 } 1243 } 1244 else { 1245 dn = base; 1246 } 1247 ldapOptions.setSearchBase(dn); 1248 constraints.setSearchScope(1); 1249 1250 filter=""; 1251 objClasses=tokenize(ldapOptions.getRelationObjectClasses(),boundary); 1252 if (objClasses!=null && objClasses.length>0) { 1253 if (objClasses.length>1) { 1254 filter+="(|"; 1255 } 1256 for (int i=0; i<objClasses.length; i++) { 1257 filter+="(objectClass="+objClasses[i]+")"; 1258 } 1259 if (objClasses.length>1) { 1260 filter+=")"; 1261 } 1262 } 1263 filter="(&("+ldapOptions.getRelationUniqueAttributeName()+"="+groupName+")"+filter+")"; 1264 results=getEntries(filter).iterator(); 1265 constraints.setSearchScope(2); 1266 ldapOptions.setSearchBase(base); 1267 1268 if (results.hasNext()) { 1269 filter=""; 1270 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 1271 if (objClasses!=null && objClasses.length>0) { 1272 if (objClasses.length>1) { 1273 filter+="(|"; 1274 } 1275 for (int i=0; i<objClasses.length; i++) { 1276 filter+="(objectClass="+objClasses[i]+")"; 1277 } 1278 if (objClasses.length>1) { 1279 filter+=")"; 1280 } 1281 } 1282 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"=*)"+filter+")"; 1283 1284 sr = (SearchResult) results.next(); 1285 attributes = sr.getAttributes(); 1286 Attribute attribute = attributes.get("member"); 1287 NamingEnumeration ne = attribute.getAll(); 1288 while (ne.hasMore()){ 1289 String member_dn = (String ) ne.next(); 1290 ldapOptions.setSearchBase(member_dn); 1291 constraints.setSearchScope(0); 1292 Iterator elems = getEntries(filter).iterator(); 1293 SearchResult sres = (SearchResult) elems.next(); 1294 Attributes attrs = sres.getAttributes(); 1295 try { 1296 String id=(String )attrs.get(ldapOptions.getUserUniqueAttributeName()).get(); 1297 if (id.equals(username)){ 1298 ldapOptions.setSearchBase(base); 1299 constraints.setSearchScope(2); 1300 return true; 1301 } 1302 if (LDAPClient.userAttributes.getMaximumSize()>0) { 1303 LDAPClient.userAttributes.put(id, attrs); 1304 } 1305 } 1306 catch (Exception e){} 1307 } } ldapOptions.setSearchBase(base); 1310 constraints.setSearchScope(2); 1311 } 1313 filter=""; 1314 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1315 if (objClasses!=null && objClasses.length>0) { 1316 if (objClasses.length>1) { 1317 filter+="(|"; 1318 } 1319 for (int i=0; i<objClasses.length; i++) { 1320 filter+="(objectClass="+objClasses[i]+")"; 1321 } 1322 if (objClasses.length>1) { 1323 filter+=")"; 1324 } 1325 } 1326 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupGroupRelationsName() + ")"+filter+")"; 1327 results=getEntries(filter).iterator(); 1328 if (results.hasNext()) { 1329 sr = (SearchResult) results.next(); 1330 attributes = sr.getAttributes(); 1331 try { 1332 dn = sr.getName(); 1333 } 1334 catch (Exception e){} 1335 if (!dn.equals("")){ 1336 if (!base.equals("")){ 1337 dn = dn + "," + base; 1338 } 1339 } 1340 else { 1341 dn = base; 1342 } 1343 constraints.setSearchScope(1); 1344 ldapOptions.setSearchBase(dn); 1345 filter=""; 1346 objClasses=tokenize(ldapOptions.getRelationObjectClasses(),boundary); 1347 if (objClasses!=null && objClasses.length>0) { 1348 if (objClasses.length>1) { 1349 filter+="(|"; 1350 } 1351 for (int i=0; i<objClasses.length; i++) { 1352 filter+="(objectClass="+objClasses[i]+")"; 1353 } 1354 if (objClasses.length>1) { 1355 filter+=")"; 1356 } 1357 } 1358 filter="(&("+ldapOptions.getRelationUniqueAttributeName()+"="+groupName+")"+filter+")"; 1359 results=getEntries(filter).iterator(); 1360 constraints.setSearchScope(2); 1361 ldapOptions.setSearchBase(base); 1362 1363 if (results.hasNext()) { 1364 filter=""; 1365 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1366 if (objClasses!=null && objClasses.length>0) { 1367 if (objClasses.length>1) { 1368 filter+="(|"; 1369 } 1370 for (int i=0; i<objClasses.length; i++) { 1371 filter+="(objectClass="+objClasses[i]+")"; 1372 } 1373 if (objClasses.length>1) { 1374 filter+=")"; 1375 } 1376 } 1377 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=*)"+filter+")"; 1378 1379 sr = (SearchResult) results.next(); 1380 attributes = sr.getAttributes(); 1381 Attribute attribute = attributes.get("member"); 1382 NamingEnumeration ne = attribute.getAll(); 1383 while (ne.hasMore()){ 1384 String member_dn = (String ) ne.next(); 1385 ldapOptions.setSearchBase(member_dn); 1386 constraints.setSearchScope(0); 1387 Iterator elems = getEntries(filter).iterator(); 1388 SearchResult sres = (SearchResult) elems.next(); 1389 Attributes attrs = sres.getAttributes(); 1390 ldapOptions.setSearchBase(base); 1391 constraints.setSearchScope(2); 1392 try { 1393 String id=(String )attrs.get(ldapOptions.getGroupUniqueAttributeName()).get(); 1394 if (LDAPClient.userAttributes.getMaximumSize()>0) { 1395 LDAPClient.userAttributes.put(id, attrs); 1396 } 1397 if (doesUserBelongToGroup(id,username)){ 1398 return true; 1399 } 1400 } 1401 catch (Exception e){} 1402 } } ldapOptions.setSearchBase(base); 1405 constraints.setSearchScope(2); 1406 } } else { 1409 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1410 if (objClasses!=null && objClasses.length>0) { 1411 if (objClasses.length>1) { 1412 filter+="(|"; 1413 } 1414 for (int i=0; i<objClasses.length; i++) { 1415 filter+="(objectClass="+objClasses[i]+")"; 1416 } 1417 if (objClasses.length>1) { 1418 filter+=")"; 1419 } 1420 } 1421 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 1422 results=getEntries(filter).iterator(); 1423 if (results.hasNext()){ 1424 sr = (SearchResult) results.next(); 1425 try { 1427 dn = sr.getName(); 1428 } 1429 catch (Exception e){} 1430 if (!dn.equals("")){ 1431 if (!base.equals("")) { 1432 dn = dn + "," + base; 1433 } 1434 } 1435 else { 1436 dn = base; 1437 } 1438 ldapOptions.setSearchBase(dn); 1439 1440 filter=""; 1441 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 1442 if (objClasses!=null && objClasses.length>0) { 1443 if (objClasses.length>1) { 1444 filter+="(|"; 1445 } 1446 for (int i=0; i<objClasses.length; i++) { 1447 filter+="(objectClass="+objClasses[i]+")"; 1448 } 1449 if (objClasses.length>1) { 1450 filter+=")"; 1451 } 1452 } 1453 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"="+username+")"+filter+")"; 1454 int size = getEntries(filter).size(); 1455 ldapOptions.setSearchBase(base); 1456 return size>0; 1457 } 1458 } return false; 1460 } 1461 1462 1467 public boolean doesGroupBelongToGroup (String groupName,String subgroupName) throws Throwable { 1468 if (!doesGroupExist(groupName)){ 1470 throw new Exception ("Group " + groupName + "does not exist."); 1471 } 1472 if (!doesGroupExist(subgroupName)){ 1473 throw new Exception ("Group " + subgroupName + "does not exist."); 1474 } 1475 String base = ldapOptions.getSearchBase(); 1476 String filter=""; 1477 String [] objClasses; 1478 Iterator results; 1479 SearchResult sr; 1480 Attributes attributes; 1481 String dn = null; 1482 1483 if (ldapOptions.getStructureType() == 1) { 1484 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1485 if (objClasses!=null && objClasses.length>0) { 1486 if (objClasses.length>1) { 1487 filter+="(|"; 1488 } 1489 for (int i=0; i<objClasses.length; i++) { 1490 filter+="(objectClass="+objClasses[i]+")"; 1491 } 1492 if (objClasses.length>1) { 1493 filter+=")"; 1494 } 1495 } 1496 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupGroupRelationsName() + ")"+filter+")"; 1497 results=getEntries(filter).iterator(); 1498 if (results.hasNext()) { 1499 sr = (SearchResult) results.next(); 1500 try { 1502 dn = sr.getName(); 1503 } 1504 catch (Exception e){} 1505 if (!dn.equals("")){ 1506 if (!base.equals("")){ 1507 dn = dn + "," + base; 1508 } 1509 } 1510 else { 1511 dn = base; 1512 } 1513 constraints.setSearchScope(1); 1514 ldapOptions.setSearchBase(dn); 1515 1516 filter=""; 1517 objClasses=tokenize(ldapOptions.getRelationObjectClasses(),boundary); 1518 if (objClasses!=null && objClasses.length>0) { 1519 if (objClasses.length>1) { 1520 filter+="(|"; 1521 } 1522 for (int i=0; i<objClasses.length; i++) { 1523 filter+="(objectClass="+objClasses[i]+")"; 1524 } 1525 if (objClasses.length>1) { 1526 filter+=")"; 1527 } 1528 } 1529 filter="(&("+ldapOptions.getRelationUniqueAttributeName()+"="+groupName+")"+filter+")"; 1530 results=getEntries(filter).iterator(); 1531 constraints.setSearchScope(2); 1532 ldapOptions.setSearchBase(base); 1533 1534 if (results.hasNext()) { 1535 filter=""; 1536 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1537 if (objClasses!=null && objClasses.length>0) { 1538 if (objClasses.length>1) { 1539 filter+="(|"; 1540 } 1541 for (int i=0; i<objClasses.length; i++) { 1542 filter+="(objectClass="+objClasses[i]+")"; 1543 } 1544 if (objClasses.length>1) { 1545 filter+=")"; 1546 } 1547 } 1548 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=*)"+filter+")"; 1549 sr = (SearchResult) results.next(); 1550 attributes = sr.getAttributes(); 1551 Attribute attribute = attributes.get("member"); 1552 1553 NamingEnumeration ne = attribute.getAll(); 1554 while (ne.hasMore()){ 1555 String member_dn = (String ) ne.next(); 1556 ldapOptions.setSearchBase(member_dn); 1557 constraints.setSearchScope(0); 1558 Iterator elems = getEntries(filter).iterator(); 1559 SearchResult sres = (SearchResult) elems.next(); 1560 Attributes attrs = sres.getAttributes(); 1561 ldapOptions.setSearchBase(base); 1562 constraints.setSearchScope(2); 1563 try { 1564 String id=(String )attrs.get(ldapOptions.getGroupUniqueAttributeName()).get(); 1565 if (LDAPClient.groupAttributes.getMaximumSize()>0) { 1566 LDAPClient.groupAttributes.put(id, attrs); 1567 } 1568 if (id.equals(subgroupName) || doesGroupBelongToGroup(id, subgroupName)){ 1569 return true; 1570 } 1571 } 1572 catch (Exception e){} 1573 } } ldapOptions.setSearchBase(base); 1576 constraints.setSearchScope(2); 1577 } } else { 1580 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1581 if (objClasses!=null && objClasses.length>0) { 1582 if (objClasses.length>1) { 1583 filter+="(|"; 1584 } 1585 for (int i=0; i<objClasses.length; i++) { 1586 filter+="(objectClass="+objClasses[i]+")"; 1587 } 1588 if (objClasses.length>1) { 1589 filter+=")"; 1590 } 1591 } 1592 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+groupName+")"+filter+")"; 1593 results=getEntries(filter).iterator(); 1594 if (results.hasNext()){ 1595 sr = (SearchResult) results.next(); 1596 try { 1598 dn = sr.getName(); 1599 } 1600 catch (Exception e){} 1601 if (!dn.equals("")){ 1602 if (!base.equals("")) { 1603 dn = dn + "," + base; 1604 } 1605 } 1606 else { 1607 dn = base; 1608 } 1609 ldapOptions.setSearchBase(dn); 1610 1611 filter=""; 1612 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 1613 if (objClasses!=null && objClasses.length>0) { 1614 if (objClasses.length>1) { 1615 filter+="(|"; 1616 } 1617 for (int i=0; i<objClasses.length; i++) { 1618 filter+="(objectClass="+objClasses[i]+")"; 1619 } 1620 if (objClasses.length>1) { 1621 filter+=")"; 1622 } 1623 } 1624 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"="+subgroupName+")"+filter+")"; 1625 int size = getEntries(filter).size(); 1626 ldapOptions.setSearchBase(base); 1627 return size>0; 1628 } } return false; 1631 } 1632 1633 1638 public java.util.List getEntries (String filter) throws Throwable { 1639 java.util.List entries=new java.util.ArrayList (); 1640 DirContext ctx=new InitialDirContext(env); 1641 try { 1642 1644 NamingEnumeration results = ctx.search( 1646 ldapOptions.getSearchBase(), 1647 filter, 1648 constraints); 1649 1650 while (results.hasMore()) { 1652 SearchResult sr = (SearchResult) results.next(); 1654 entries.add(sr); 1655 } 1656 return entries; 1658 } 1659 catch (Exception e){ 1660 e.printStackTrace(); 1661 } 1662 finally { 1663 ctx.close(); 1664 } 1665 return entries; 1666 } 1667 1668 1669 1675 static String [] tokenize(String input,String boundary) { 1676 if (input==null) input=""; 1677 Vector v = new Vector(); 1678 StringTokenizer t = new StringTokenizer(input,boundary); 1679 String cmd[]; 1680 1681 while (t.hasMoreTokens()) 1682 v.addElement(t.nextToken()); 1683 cmd = new String [v.size()]; 1684 for (int i = 0; i < cmd.length; i++) 1685 cmd[i] = (String )v.elementAt(i); 1686 1687 return cmd; 1688 } 1689 1690} 1691 | Popular Tags |