| 1 29 30 package hero.session; 31 import javax.ejb.EJBException ; 32 import javax.ejb.SessionBean ; 33 import javax.ejb.SessionContext ; 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 import java.util.*; 38 import java.text.MessageFormat ; 39 import javax.naming.*; 41 import javax.naming.directory.*; 42 43 import javax.rmi.PortableRemoteObject ; 45 import hero.util.HeroException; 46 import javax.management.ObjectName ; 48 import javax.management.InstanceNotFoundException ; 49 import javax.management.Query ; 50 51 import javax.management.j2ee.ManagementHome ; 53 import javax.management.j2ee.Management ; 54 55 import java.rmi.RemoteException ; 57 import hero.interfaces.UserRegistrationLocalHome; 58 import hero.interfaces.UserRegistrationLocal; 59 import hero.interfaces.UserRegistrationUtil; 60 61 import hero.interfaces.ProjectSessionLocalHome; 62 import hero.interfaces.ProjectSessionLocal; 63 import hero.interfaces.ProjectSessionUtil; 64 65 import hero.interfaces.UserSessionLocalHome; 66 import hero.interfaces.UserSessionLocal; 67 import hero.interfaces.UserSessionUtil; 68 69 import hero.util.HeroException; 70 import javax.ejb.CreateException ; 71 72 73 101 102 public class BnLdapBean implements SessionBean { 103 104 private String ldapRealmName = null; 105 private SessionContext sessionContext = null; 106 107 String baseDn = ""; 108 String initialContextFactory = ""; 109 String providerUrl = ""; 110 String authenticationMode = ""; 111 String securityAuthentication = ""; 112 String securityPrincipal = ""; 113 String securityCredentials = ""; 114 String referral = ""; 115 String roleDN = ""; 116 String roleNameAttribute = ""; 117 String roleSearchFilter = ""; 118 String userDN = ""; 119 String userPasswordAttribute = ""; 120 String userRolesAttribute = ""; 121 String userSearchFilter = ""; 122 123 124 private DirContext dirContext = null; 125 126 129 public void setSessionContext(SessionContext ctx) { 130 sessionContext = ctx; 131 132 Context initialContext = null; 133 try{ 134 initialContext = new InitialContext (); 135 } catch (Exception e) {e.printStackTrace(); 136 System.err.println("Cannot get initial context for JNDI: " + e); 137 } 138 139 143 ManagementHome mgmtHome = null; 145 try { 146 mgmtHome = (ManagementHome ) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/mgmt/MEJB"), ManagementHome .class); 147 } 148 catch (Exception e) {e.printStackTrace(); 149 System.err.println("Cannot lookup java:comp/env/ejb/mgmt/MEJB: " + e); 150 return; 151 } 152 Management mgmt = null; 154 try { 155 mgmt = mgmtHome.create(); 156 System.out.println("MEJB created"); 157 } 158 catch (Exception e) {e.printStackTrace(); 159 System.err.println("Error creating MEJB: " + e ); 160 } 161 162 166 System.out.println("Access the J2EEDomain MBean"); 167 ObjectName objectName = null; 168 169 String defaultDomain =""; 171 try { 172 defaultDomain = mgmt.getDefaultDomain(); 173 } 174 catch (Exception e) { 175 System.err.println("Cannot create MEJB: " + e ); 176 } 177 178 try { 180 ObjectName searchpattern = new ObjectName (defaultDomain + ":type=securityfactory,subtype=ldap,*"); 181 Query q = new Query (); 182 Set managedObject = mgmt.queryNames(searchpattern,null); 183 184 if ((managedObject.size()) > 1) { 185 System.err.println("Error detected in JOnAS configuration:"); 186 System.err.println("More than 1 Ldap realms has been defined"); 187 Iterator itr = managedObject.iterator(); 188 while (itr.hasNext()) { 189 ObjectName name = (ObjectName )itr.next(); 190 System.out.println(name.toString()); 191 } 192 throw new RemoteException ("More than 1 Ldap realms has been defined"); 193 194 } else if ((managedObject.size()) == 0) { 196 System.err.println("Error detected in JOnAS configuration:"); 197 System.err.println("No ldap configuration has been defined."); 198 throw new RemoteException ("0 Ldap realms has been defined"); 199 } 200 else { 201 Iterator itr = managedObject.iterator(); 203 while (itr.hasNext()) { 204 objectName = (ObjectName )itr.next(); 205 System.out.println("1 Ldap realm name detected in JOnAS configuration:"); 206 System.out.println(objectName.toString()); 207 208 } 209 } 210 211 }catch (Exception e) { 212 System.err.println("Error checking ldap objectName: " + e ); 213 } 214 215 if (objectName != null){ 216 try { 218 boolean exists = mgmt.isRegistered(objectName); 219 if (exists) { 220 222 if (mgmt.getAttribute(objectName, "BaseDN")!=null) baseDn = mgmt.getAttribute(objectName, "BaseDN").toString(); 224 else baseDn =""; 225 227 if (mgmt.getAttribute(objectName, "InitialContextFactory")!=null) initialContextFactory = mgmt.getAttribute(objectName, "InitialContextFactory").toString(); 229 else initialContextFactory =""; 230 232 if (mgmt.getAttribute(objectName, "ProviderUrl")!=null) providerUrl = mgmt.getAttribute(objectName, "ProviderUrl").toString(); 234 else providerUrl =""; 235 237 if (mgmt.getAttribute(objectName, "SecurityAuthentication")!=null) securityAuthentication = mgmt.getAttribute(objectName, "SecurityAuthentication").toString(); 239 else securityAuthentication =""; 240 242 if (mgmt.getAttribute(objectName, "SecurityPrincipal")!=null) securityPrincipal = mgmt.getAttribute(objectName, "SecurityPrincipal").toString(); 244 else securityPrincipal =""; 245 247 if (mgmt.getAttribute(objectName, "SecurityCredentials")!=null) securityCredentials = mgmt.getAttribute(objectName, "SecurityCredentials").toString(); 249 else securityCredentials =""; 250 252 if (mgmt.getAttribute(objectName, "RoleDN")!=null) roleDN = mgmt.getAttribute(objectName, "RoleDN").toString(); 254 else roleDN =""; 255 257 if (mgmt.getAttribute(objectName, "RoleNameAttribute")!=null) roleNameAttribute = mgmt.getAttribute(objectName, "RoleNameAttribute").toString(); 259 else roleNameAttribute =""; 260 262 if (mgmt.getAttribute(objectName, "RoleSearchFilter")!=null) roleSearchFilter = mgmt.getAttribute(objectName, "RoleSearchFilter").toString(); 264 else roleSearchFilter =""; 265 267 if (mgmt.getAttribute(objectName, "UserDN")!=null) userDN = mgmt.getAttribute(objectName, "UserDN").toString(); 269 else userDN =""; 270 272 if (mgmt.getAttribute(objectName, "UserPasswordAttribute")!=null) userPasswordAttribute = mgmt.getAttribute(objectName, "UserPasswordAttribute").toString(); 274 else userPasswordAttribute =""; 275 277 if (mgmt.getAttribute(objectName, "UserRolesAttribute")!=null) userRolesAttribute = mgmt.getAttribute(objectName, "UserRolesAttribute").toString(); 279 else userRolesAttribute =""; 280 282 if (mgmt.getAttribute(objectName, "UserSearchFilter")!=null) userSearchFilter = mgmt.getAttribute(objectName, "UserSearchFilter").toString(); 284 else userSearchFilter =""; 285 287 if (mgmt.getAttribute(objectName, "Referral")!=null) referral = mgmt.getAttribute(objectName, "Referral").toString(); 289 else referral =""; 290 } 292 else { 293 System.out.println("Can't find J2EEDomain managed object having the above name in the current MBean server"); 294 return; 295 } 296 } 297 catch (Exception e) { 298 System.err.println("Error when using the J2EEDomain managed object: " + e); 299 return; 300 } 301 } 302 303 } 304 305 318 public void importLdapUsers()throws HeroException{ 319 320 321 String userId =""; 322 String email = ""; 323 UserRegistrationLocalHome userRegistrationh=null; 324 UserRegistrationLocal userRegistration=null; 325 326 UserSessionLocalHome userSessionh=null; 327 UserSessionLocal userSession=null; 328 329 Hashtable env = new Hashtable(); 332 333 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 334 env.put(Context.PROVIDER_URL, providerUrl); 335 env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication); 336 env.put(Context.SECURITY_PRINCIPAL, securityPrincipal); 337 env.put(Context.SECURITY_CREDENTIALS, securityCredentials); 338 env.put(Context.REFERRAL, referral); 339 340 try 341 { 342 dirContext = new InitialDirContext(env); 343 } 344 catch (NamingException e) 345 { 346 System.err.println("cannot initialize the initialContext of naming" + e); 347 throw new EJBException ("cannot initialize the initialContext of naming"); 348 } 349 350 Map map = null; 352 Collection bnUsersCol = null; 353 try { 354 map = getTreeMapUidMailLdap(); 355 } catch (Exception e){ 356 throw new HeroException("Error getting users from LDAP " + e.getMessage()); 357 } 358 try { 359 bnUsersCol = getAllSortedBnUsers(); 360 } catch(Exception e){ 361 throw new HeroException("Error getting users from bonita database " + e.getMessage()); 362 } 363 364 365 try { 366 userRegistrationh = (UserRegistrationLocalHome)UserRegistrationUtil.getLocalHome(); 367 userRegistration = userRegistrationh.create(); 368 } catch (Exception e){ 369 e.printStackTrace(); 370 System.err.println("Error creating userRegistration Bean" + e); 371 } 372 373 try { 374 userSessionh = (UserSessionLocalHome)UserSessionUtil.getLocalHome(); 375 userSession = userSessionh.create(); 376 377 } catch (Exception e){ 378 e.printStackTrace(); 379 System.err.println("Error creating userSession Bean" + e); 380 } 381 382 try { 383 Set entries = map.entrySet(); 385 Iterator iterator = entries.iterator(); 386 387 while (iterator.hasNext()) { 388 Map.Entry entry = (Map.Entry)iterator.next(); 389 userId = entry.getKey().toString(); 390 email = entry.getValue().toString(); 391 if (!bnUsersCol.contains(userId)){ 392 userRegistration.userCreate(userId, userId, email); 394 System.out.println("Create user in Bonita DB: " + entry.getKey() + " / " + entry.getValue()); 396 } 397 else { 398 System.out.println( "User already into Bonita database then set the mail of this user : " + entry.getKey() + " / " + entry.getValue()); 399 userSession.setUserMail(userId, email); 401 bnUsersCol.remove(userId); 403 } 404 } 405 406 if (bnUsersCol.contains("admin")) bnUsersCol.remove("admin"); 408 if (bnUsersCol.contains("admin2")) bnUsersCol.remove("admin2"); 409 if (bnUsersCol.contains("nobody")) bnUsersCol.remove("nobody"); 410 411 Iterator it=bnUsersCol.iterator(); 413 while (it.hasNext()) { 414 userId = (String )it.next(); 415 System.out.println( "Deleting user in Bonita DB: " + userId); 417 try { 419 userRegistration.deleteUser(userId); 420 } catch (HeroException e) { 421 System.out.println( "Cannot remove user: " + userId + e.getMessage()); 422 } 423 } 424 userRegistration.remove(); 426 userSession.remove(); 427 428 } catch (Exception e) { 429 e.printStackTrace(); 430 System.err.println("Error executing import of users from ldap into Bonita DB. " + e); 431 } 432 433 try { dirContext.close(); } catch (Exception e) {}; 435 436 } 437 438 447 public Collection getGroupMembers(String groupName) throws HeroException{ 448 449 Hashtable env = new Hashtable(); 452 453 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 454 env.put(Context.PROVIDER_URL, providerUrl); 455 env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication); 456 env.put(Context.SECURITY_PRINCIPAL, securityPrincipal); 457 env.put(Context.SECURITY_CREDENTIALS, securityCredentials); 458 env.put(Context.REFERRAL, referral); 459 try 460 { 461 dirContext = new InitialDirContext(env); 462 } 463 catch (NamingException e) 464 { 465 System.err.println("cannot initialize the initialContext of naming" + e); 466 throw new EJBException ("cannot initialize the initialContext of naming"); 467 } 468 ArrayList al = new ArrayList(); 469 470 String dn = ""; 475 String filter = "(&(objectclass=groupOfUniqueNames)(" + roleNameAttribute + "=" + groupName + "))"; 476 SearchControls constraints = new SearchControls(); 478 String [] attrID1s = {roleNameAttribute}; 479 String [] attrID2s = {"uniquemember"}; 480 481 482 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE) ; 483 String subTree= roleDN + "," + baseDn; 486 NamingEnumeration ne = null; 487 int taille = 0; 488 489 try { 491 constraints.setReturningAttributes(attrID1s); 492 ne = dirContext.search(subTree, filter, constraints); 493 }catch (NamingException e) { 494 throw new HeroException("Error searching groups in LDAP " + e); 495 } 496 try { 497 while (ne.hasMore()){ 498 SearchResult sr = (SearchResult)ne.next(); 499 Attributes attrs = sr.getAttributes(); 500 System.out.println("LDAP group searching : " + attrs.toString()); 502 503 taille = attrs.size(); 504 } 505 } catch (NamingException e) { 506 throw new HeroException("Error dirContext.search" + e); 507 } 508 509 510 if ( taille == 0 ) { 511 System.err.println("The group " + groupName + " does not exist in the LDAP"); 513 throw new HeroException("The group " + groupName + " does not exist in the LDAP "); 514 } 515 else { 516 try 517 { 518 constraints.setReturningAttributes(attrID2s); 519 ne = dirContext.search(subTree, filter, constraints); 520 }catch (NamingException e){ 521 throw new HeroException ("Error searching groups in LDAP " + e); 522 } 523 524 try { 526 527 while (ne.hasMore()){ 528 SearchResult sr = (SearchResult)ne.next(); 529 Attributes attrs = sr.getAttributes(); 531 532 for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) { 533 Attribute attr = (Attribute)ae.next(); 534 System.out.println("attribute: " + attr.getID()); 536 537 for (NamingEnumeration e = attr.getAll(); e.hasMore();){ 539 dn = (String ) e.next(); 540 541 System.out.println(" dn: " + dn); 543 544 String userId = getUserIdFromDn(dn) ; 552 553 System.out.println(" uid: " +userId); 555 556 if (!"".equals(userId)){ 557 al.add(userId); 558 } 559 } 560 } 561 } ne.close(); 563 } catch (NamingException e){ 564 e.printStackTrace(); 565 throw new HeroException("Error searching userId from group members in LDAP"); 566 } 567 } return al; 569 } 570 571 580 public String getUserIdFromPartialDn(String partialDN) throws HeroException{ 581 Hashtable env = new Hashtable(); 584 585 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 586 env.put(Context.PROVIDER_URL, providerUrl); 587 env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication); 588 env.put(Context.SECURITY_PRINCIPAL, securityPrincipal); 589 env.put(Context.SECURITY_CREDENTIALS, securityCredentials); 590 env.put(Context.REFERRAL, referral); 591 592 try 593 { 594 dirContext = new InitialDirContext(env); 595 } 596 catch (NamingException na) 597 { 598 throw new HeroException("cannot initialize the initialContext of naming" + na.getMessage(), na); 599 } 600 601 ArrayList al = new ArrayList(); 602 String userId = ""; 603 String filter = "uid=*"; 604 NamingEnumeration ne = null; 605 SearchControls constraints = new SearchControls(); 608 String [] attrIDs = {"uid"}; 609 constraints.setReturningAttributes(attrIDs); 610 constraints.setSearchScope(SearchControls.OBJECT_SCOPE) ; 612 613 try { 615 System.out.println("BnLdapBean/getUserIdFromPartialDn: partialDN = " + partialDN + " baseDn = " + baseDn ); 617 ne = dirContext.search(partialDN + "," + baseDn, filter, constraints); 618 while (ne.hasMore()) 619 { 620 SearchResult sr = (SearchResult)ne.next(); 621 Attributes attrs = sr.getAttributes(); 623 Attribute attr = attrs.get("uid"); 624 userId = (String )attr.get(); 625 } 626 ne.close(); 627 } catch (NamingException na) 628 { 629 throw new HeroException("Error searching directory :" + na.getMessage(), na); 630 } 631 return userId; 632 } 633 634 638 private Map getTreeMapUidMailLdap() { 639 TreeMap treeMap = new TreeMap(); 640 try 641 { 642 643 MessageFormat form = new MessageFormat (userSearchFilter); 645 Object [] testArgs = {"*"}; 647 String filter = form.format(testArgs); 648 649 System.out.println("Filter used to get user in Ldap: " + filter); 651 652 SearchControls constraints = new SearchControls(); 655 656 int indSep = userSearchFilter.indexOf('='); 658 StringBuffer sb = new StringBuffer (userSearchFilter); 659 String userParameter = sb.substring(0,indSep); 660 661 System.out.println("UserParameter from userSearchFilter = " + userParameter); 663 664 String [] attrIDs = {userParameter,"mail"}; 665 constraints.setReturningAttributes(attrIDs); 666 667 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE) ; 668 669 System.out.println("Naming CountLimit = " + constraints.getCountLimit()); 672 System.out.println("Naming TimeLimit = " + constraints.getTimeLimit()); 673 674 NamingEnumeration ne = dirContext.search(userDN + "," + baseDn, filter, constraints); 676 String userId =""; 677 String userMail =""; 678 Attribute attr = null; 679 while (ne.hasMore()) 680 { 681 SearchResult sr = (SearchResult)ne.next(); 682 683 684 Attributes attrs = sr.getAttributes(); 685 attr = attrs.get(userParameter); 687 userId = (String )attr.get(); 688 if (attrs.get("mail") != null) { 690 attr = attrs.get("mail"); 691 userMail = (String )attr.get(); 692 } 693 else { 694 userMail = ""; 695 } 696 697 treeMap.put(userId,userMail); 698 } 699 ne.close(); 700 701 System.out.println("------------------" + userParameter + " and mail found un Ldap -------------------------"); 703 printUidMailCollection(treeMap); 704 } catch (SizeLimitExceededException e) 706 { 707 System.err.println(e); 709 System.err.println(""); 710 System.err.println("Sizelimit Exceeded from the LDAP server."); 711 System.out.println("Please contact the ldap administrator to extend this limit,"); 712 System.out.println("or reduce the scope of the user searching."); 713 System.err.println(""); 714 e.printStackTrace(); 715 } 716 catch (NamingException e) 717 { 718 e.printStackTrace(); 719 System.err.println("Error reading directory" + e); 720 throw new EJBException ("Error reading directory"); 721 } 722 return treeMap; 723 } 724 725 726 727 731 private Collection getUidFromLdap() { 732 TreeSet sorter = new TreeSet(); 733 try 734 { 735 736 MessageFormat form = new MessageFormat (userSearchFilter); 738 Object [] testArgs = {"*"}; 740 String filter = form.format(testArgs); 741 742 System.out.println("Filter used to get user in Ldap: " + filter); 744 745 SearchControls constraints = new SearchControls(); 748 749 int indSep = userSearchFilter.indexOf('='); 751 StringBuffer sb = new StringBuffer (userSearchFilter); 752 String userParameter = sb.substring(0,indSep); 753 754 System.out.println("UserParameter from userSearchFilter = " + userParameter); 756 757 String [] attrIDs = {userParameter}; 758 constraints.setReturningAttributes(attrIDs); 759 760 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE) ; 761 762 System.out.println("Naming CountLimit = " + constraints.getCountLimit()); 765 System.out.println("Naming TimeLimit = " + constraints.getTimeLimit()); 766 767 NamingEnumeration ne = dirContext.search(userDN + "," + baseDn , filter, constraints); 769 770 while (ne.hasMore()) 771 { 772 SearchResult sr = (SearchResult)ne.next(); 773 Attributes attrs = sr.getAttributes(); 775 |