1 16 17 package org.apache.jetspeed.services.security.ldap; 18 19 import java.security.Principal ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Vector ; 23 import javax.naming.NamingEnumeration ; 24 import javax.naming.directory.Attributes ; 25 import javax.naming.directory.BasicAttributes ; 26 import javax.naming.directory.DirContext ; 27 import javax.naming.directory.SearchResult ; 28 import javax.servlet.ServletConfig ; 29 30 import org.apache.jetspeed.om.profile.Profile; 32 import org.apache.jetspeed.om.security.JetspeedUser; 33 import org.apache.jetspeed.om.security.UserNamePrincipal; 34 import org.apache.jetspeed.om.security.ldap.LDAPUser; 35 import org.apache.jetspeed.services.JetspeedLDAP; 36 import org.apache.jetspeed.services.JetspeedSecurity; 37 import org.apache.jetspeed.services.Profiler; 38 import org.apache.jetspeed.services.PsmlManager; 39 import org.apache.jetspeed.services.ldap.LDAPURL; 40 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 41 import org.apache.jetspeed.services.logging.JetspeedLogger; 42 import org.apache.jetspeed.services.rundata.JetspeedRunData; 43 import org.apache.jetspeed.services.rundata.JetspeedRunDataService; 44 import org.apache.jetspeed.services.security.CredentialsManagement; 45 import org.apache.jetspeed.services.security.JetspeedSecurityException; 46 import org.apache.jetspeed.services.security.JetspeedSecurityService; 47 import org.apache.jetspeed.services.security.NotUniqueUserException; 48 import org.apache.jetspeed.services.security.UnknownUserException; 49 import org.apache.jetspeed.services.security.UserException; 50 import org.apache.jetspeed.services.security.UserManagement; 51 52 import org.apache.turbine.services.InitializationException; 54 import org.apache.turbine.services.TurbineBaseService; 55 import org.apache.turbine.services.TurbineServices; 56 import org.apache.turbine.services.resources.ResourceService; 57 import org.apache.turbine.services.rundata.RunDataService; 58 59 67 public class LDAPUserManagement extends TurbineBaseService 68 implements UserManagement, 69 CredentialsManagement 70 { 71 74 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(LDAPUserManagement.class.getName()); 75 76 private final static String CONFIG_SECURE_PASSWORDS_KEY = "secure.passwords"; 78 private final static String CONFIG_SECURE_PASSWORDS_ALGORITHM = "secure.passwords.algorithm"; 79 private final static String CONFIG_SECURE_PASSWORDS_SUFFIX = "secure.passwords.suffix"; 80 private final static String CONFIG_NEWUSER_ROLES = "newuser.roles"; 81 private final static String [] DEFAULT_CONFIG_NEWUSER_ROLES = { "user" }; 82 83 private final static String [] ATTRS = { "ou", "userPassword", "uid", "mail", "sn", "givenName", 84 "uidNumber", "name", "objectdata", "objectClass", 85 "usergrouprole", "lastlogindate", "lastmodifieddate", 86 "creationdate", "confirm", "disabled" }; 87 88 protected static boolean securePasswords = false; 90 protected static String passwordsAlgorithm = "crypt"; 91 protected static String passwordsSuffix = "{crypt}"; 92 93 protected JetspeedRunDataService runDataService = null; 95 protected String roles[] = null; 96 97 100 102 120 public JetspeedUser getUser(Principal principal) 121 throws JetspeedSecurityException 122 { 123 BasicAttributes attr = new BasicAttributes (); 124 Vector userurls = new Vector (); 125 LDAPUser user = null; 126 127 try 128 { 129 userurls = JetspeedLDAP.search(JetspeedLDAP.buildURL("ou=users"), 130 "(&(uid="+principal.getName()+")(objectclass=jetspeeduser))", ATTRS, true); 131 } 132 catch (Exception e) 133 { 134 logger.error( "Failed to retrieve user '" + principal.getName() + "'", e ); 135 throw new UserException("Failed to retrieve user '" + principal.getName() + "'", e); 136 } 137 138 if (userurls.size() == 1) 139 { 140 user = new LDAPUser((LDAPURL) ((Vector )userurls.elementAt(0)).firstElement()); 141 return user; 142 } 143 else if(userurls.size() > 1) 144 { 145 throw new UserException("Multiple Users with same username '" + principal.getName() + "'"); 146 } 147 else 148 { 149 throw new UnknownUserException("Unknown user '" + principal.getName() + "'"); 150 } 151 } 152 153 162 public Iterator getUsers() 163 throws JetspeedSecurityException 164 { 165 String filter = "(objectclass=jetspeeduser)"; 166 return getUsersUsingLDAPSpecificFilter(filter, null); 167 } 168 169 180 public Iterator getUsers(String filter) 181 throws JetspeedSecurityException 182 { 183 return getUsersUsingLDAPSpecificFilter(filter, null); 185 } 186 187 197 protected Iterator getUsersUsingLDAPSpecificFilter(String filter, String [] attributesToFetch) 198 throws JetspeedSecurityException 199 { 200 String baseDN = "ou=users"; 201 NamingEnumeration userEnum = null; 202 List resultList = new Vector (1024); 203 204 try 205 { 206 LDAPURL url = JetspeedLDAP.buildURL( baseDN ); 207 DirContext ctx = JetspeedLDAP.getService().connect(url); 208 userEnum = JetspeedLDAP.search(ctx, url.getDN(), filter, attributesToFetch, JetspeedLDAP.getService().SUB); 209 210 while (userEnum.hasMoreElements()) 211 { 212 LDAPUser user = buildUser(((SearchResult )userEnum.nextElement()).getAttributes()); 213 resultList.add( user ); 214 } 215 216 JetspeedLDAP.getService().checkAndCloseContext(ctx); 217 } 218 catch ( Exception e ) 219 { 220 logger.error( "Failed to retrieve user with filter:" + filter, e ); 221 throw new UserException( "Failed to retrieve user with filter:" + filter, e ); 222 } 223 224 return ( resultList.iterator() ); 225 } 226 227 protected LDAPUser buildUser(Attributes attributes) 228 { 229 return new LDAPUser(attributes); 230 } 231 232 241 public void saveUser(JetspeedUser user) 242 throws JetspeedSecurityException 243 { 244 if(!accountExists(user, true)) 245 { 246 throw new UnknownUserException("Cannot save user '" + user.getUserName() + 247 "', User doesn't exist"); 248 } 249 try 250 { 251 ((LDAPUser)user).update(false); 252 } 253 catch(Exception e) 254 { 255 logger.error( "Failed to save user object ", e); 256 throw new UserException("Failed to save user object ", e); 257 } 258 } 259 260 272 public void addUser(JetspeedUser user) 273 throws JetspeedSecurityException 274 { 275 if(accountExists(user)) 276 { 277 throw new NotUniqueUserException("The account '" + 278 user.getUserName() + "' already exists"); 279 } 280 281 String initialPassword = user.getPassword(); 282 String encrypted = JetspeedSecurity.encryptPassword(initialPassword); 283 user.setPassword(encrypted); 284 ((LDAPUser)user).update(true); 285 286 addDefaultPSML(user); 287 } 288 289 296 protected void addDefaultPSML(JetspeedUser user) 297 throws JetspeedSecurityException 298 { 299 for (int ix = 0; ix < roles.length; ix++) 300 { 301 try 302 { 303 304 JetspeedSecurity.grantRole(user.getUserName(), 305 JetspeedSecurity.getRole(roles[ix]).getName()); 306 } 307 catch(Exception e) 308 { 309 logger.error("Could not grant role: " + roles[ix] + " to user " + user.getUserName(), e); 310 } 311 } 312 try 313 { 314 JetspeedRunData rundata = getRunData(); 315 if (rundata != null) 316 { 317 Profile profile = Profiler.createProfile(); 318 profile.setUser(user); 319 profile.setMediaType("html"); 320 Profiler.createProfile(getRunData(), profile); 321 } 322 } 323 catch (Exception e) 324 { 325 logger.error( "Failed to create profile for new user ", e ); 326 removeUser(new UserNamePrincipal(user.getUserName())); 327 throw new UserException("Failed to create profile for new user ", e); 328 } 329 } 330 331 342 public void removeUser(Principal principal) 343 throws JetspeedSecurityException 344 { 345 BasicAttributes attr= new BasicAttributes (); 346 Vector userurls = new Vector (); 347 LDAPUser user = (LDAPUser)getUser(principal); 348 349 try 350 { 351 JetspeedLDAP.deleteEntry(user.getldapurl()); 352 PsmlManager.removeUserDocuments(user); 353 } 354 catch(Exception e) 355 { 356 logger.error( "Failed to remove account '" + user.getUserName() + "'", e ); 357 throw new UserException("Failed to remove account '" + 358 user.getUserName() + "'", e); 359 } 360 361 } 362 363 367 378 public void changePassword( JetspeedUser user, 379 String oldPassword, 380 String newPassword ) 381 throws JetspeedSecurityException 382 { 383 oldPassword = JetspeedSecurity.convertPassword(oldPassword); 384 newPassword = JetspeedSecurity.convertPassword(newPassword); 385 386 if(!accountExists(user)) 387 { 388 throw new UnknownUserException("The account '" + 389 user.getUserName() + "' does not exist"); 390 } 391 else if (!passwordsMatch(user, oldPassword)) 392 { 393 throw new UserException( 394 "The supplied old password for '" + user.getUserName() + 395 "' was incorrect"); 396 } 397 398 String encrypted = JetspeedSecurity.encryptPassword( newPassword ); 399 user.setPassword(encrypted); 400 401 saveUser(user); 405 } 406 407 425 public void forcePassword( JetspeedUser user, String password ) 426 throws JetspeedSecurityException 427 { 428 if(!accountExists(user)) 429 { 430 throw new UnknownUserException("The account '" + 431 user.getUserName() + "' does not exist"); 432 } 433 434 String encrypted = JetspeedSecurity.encryptPassword( password ); 435 user.setPassword(encrypted); 436 437 438 saveUser(user); 442 } 443 444 459 public String encryptPassword( String password ) 460 throws JetspeedSecurityException 461 { 462 if (securePasswords == false) 463 { 464 return password; 465 } 466 else if(password == null) 467 { 468 return null; 469 } 470 else if(password.startsWith(passwordsSuffix)) 471 { 472 return password; 474 } 475 476 return passwordsSuffix + UnixCrypt.crypt(password); 477 } 478 479 489 public static boolean passwordsMatch(JetspeedUser user, String suppliedPassword) 490 { 491 if (securePasswords == false) 492 { 493 return user.getPassword().equals(suppliedPassword); 494 } 495 else 496 { 497 return UnixCrypt.matches(user.getPassword().substring(passwordsSuffix.length()), suppliedPassword); 498 } 499 } 500 501 505 512 public synchronized void init(ServletConfig conf) 513 throws InitializationException 514 { 515 if (getInit()) return; 516 517 super.init(conf); 518 519 ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance()) 521 .getResources(JetspeedSecurityService.SERVICE_NAME); 522 523 securePasswords = serviceConf.getBoolean(CONFIG_SECURE_PASSWORDS_KEY, 524 securePasswords); 525 passwordsAlgorithm = serviceConf.getString(CONFIG_SECURE_PASSWORDS_ALGORITHM, 526 passwordsAlgorithm); 527 passwordsSuffix = serviceConf.getString(CONFIG_SECURE_PASSWORDS_SUFFIX, 528 passwordsSuffix); 529 530 try 531 { 532 roles = serviceConf.getStringArray(CONFIG_NEWUSER_ROLES); 533 } 534 catch (Exception e) 535 { 536 } 537 538 if (null == roles || roles.length == 0) 539 { 540 roles = DEFAULT_CONFIG_NEWUSER_ROLES; 541 } 542 543 this.runDataService = (JetspeedRunDataService)TurbineServices.getInstance() 544 .getService(RunDataService.SERVICE_NAME); 545 546 setInit(true); 547 } 548 549 553 568 protected boolean accountExists( JetspeedUser user ) 569 throws UserException 570 { 571 return accountExists(user, false); 572 } 573 574 protected boolean accountExists( JetspeedUser user, boolean checkUniqueId ) 575 throws UserException 576 { 577 UserNamePrincipal principal = new UserNamePrincipal(user.getUserName()); 578 579 try 580 { 581 getUser(principal); 582 return true; 583 } 584 catch (Exception e) 585 { 586 return false; 587 } 588 } 589 590 protected JetspeedRunData getRunData() 591 { 592 JetspeedRunData rundata = null; 593 594 if (this.runDataService != null) 595 { 596 rundata = this.runDataService.getCurrentRunData(); 597 } 598 599 return rundata; 600 } 601 602 } 603 | Popular Tags |