1 package org.apache.turbine.services.security.ldap; 2 3 18 19 import java.util.List ; 20 import java.util.Hashtable ; 21 import java.util.Vector ; 22 23 import javax.naming.AuthenticationException ; 24 import javax.naming.Context ; 25 import javax.naming.NamingEnumeration ; 26 import javax.naming.NamingException ; 27 import javax.naming.directory.Attributes ; 28 import javax.naming.directory.DirContext ; 29 import javax.naming.directory.SearchControls ; 30 import javax.naming.directory.SearchResult ; 31 32 import org.apache.commons.configuration.Configuration; 33 34 import org.apache.torque.util.Criteria; 35 36 import org.apache.turbine.om.security.User; 37 import org.apache.turbine.services.security.TurbineSecurity; 38 import org.apache.turbine.services.security.UserManager; 39 import org.apache.turbine.util.security.DataBackendException; 40 import org.apache.turbine.util.security.EntityExistsException; 41 import org.apache.turbine.util.security.PasswordMismatchException; 42 import org.apache.turbine.util.security.UnknownEntityException; 43 44 65 public class LDAPUserManager implements UserManager 66 { 67 72 public void init(Configuration conf) 73 { 74 } 76 77 86 public boolean accountExists(User user) throws DataBackendException 87 { 88 return accountExists(user.getName()); 89 } 90 91 100 public boolean accountExists(String username) 101 throws DataBackendException 102 { 103 try 104 { 105 User ldapUser = retrieve(username); 106 } 107 catch (UnknownEntityException ex) 108 { 109 return false; 110 } 111 112 return true; 113 } 114 115 125 public User retrieve(String username) 126 throws UnknownEntityException, DataBackendException 127 { 128 try 129 { 130 DirContext ctx = bindAsAdmin(); 131 132 135 String userBaseSearch = LDAPSecurityConstants.getBaseSearch(); 136 String filter = LDAPSecurityConstants.getNameAttribute(); 137 138 filter = "(" + filter + "=" + username + ")"; 139 140 143 SearchControls ctls = new SearchControls (); 144 145 NamingEnumeration answer = 146 ctx.search(userBaseSearch, filter, ctls); 147 148 if (answer.hasMore()) 149 { 150 SearchResult sr = (SearchResult ) answer.next(); 151 Attributes attribs = sr.getAttributes(); 152 LDAPUser ldapUser = createLDAPUser(); 153 154 ldapUser.setLDAPAttributes(attribs); 155 ldapUser.setTemp("turbine.user", ldapUser); 156 157 return ldapUser; 158 } 159 else 160 { 161 throw new UnknownEntityException("The given user: " 162 + username + "\n does not exist."); 163 } 164 } 165 catch (NamingException ex) 166 { 167 throw new DataBackendException( 168 "The LDAP server specified is unavailable", ex); 169 } 170 } 171 172 182 public User retrieveById(Object key) 183 throws UnknownEntityException, DataBackendException 184 { 185 try 186 { 187 DirContext ctx = bindAsAdmin(); 188 189 192 StringBuffer userBaseSearch = new StringBuffer (); 193 userBaseSearch.append(LDAPSecurityConstants.getUserIdAttribute()); 194 userBaseSearch.append("="); 195 userBaseSearch.append(String.valueOf(key)); 196 userBaseSearch.append(","); 197 userBaseSearch.append(LDAPSecurityConstants.getBaseSearch()); 198 199 202 NamingEnumeration answer = 203 ctx.search(userBaseSearch.toString(), (Attributes )null); 204 205 if (answer.hasMore()) 206 { 207 SearchResult sr = (SearchResult ) answer.next(); 208 Attributes attribs = sr.getAttributes(); 209 LDAPUser ldapUser = createLDAPUser(); 210 211 ldapUser.setLDAPAttributes(attribs); 212 ldapUser.setTemp("turbine.user", ldapUser); 213 214 return ldapUser; 215 } 216 else 217 { 218 throw new UnknownEntityException("No user exists for the key: " 219 + String.valueOf(key) + "\n"); 220 } 221 } 222 catch (NamingException ex) 223 { 224 throw new DataBackendException( 225 "The LDAP server specified is unavailable", ex); 226 } 227 } 228 229 246 public User[] retrieve(Criteria criteria) 247 throws DataBackendException 248 { 249 return (User []) retrieveList(criteria).toArray(new User[0]); 250 } 251 252 266 public List retrieveList(Criteria criteria) 267 throws DataBackendException 268 { 269 List users = new Vector (0); 270 271 try 272 { 273 DirContext ctx = bindAsAdmin(); 274 275 String userBaseSearch = LDAPSecurityConstants.getBaseSearch(); 276 String filter = LDAPSecurityConstants.getNameAttribute(); 277 278 filter = "(" + filter + "=*)"; 279 280 283 SearchControls ctls = new SearchControls (); 284 285 NamingEnumeration answer = 286 ctx.search(userBaseSearch, filter, ctls); 287 288 while (answer.hasMore()) 289 { 290 SearchResult sr = (SearchResult ) answer.next(); 291 Attributes attribs = sr.getAttributes(); 292 LDAPUser ldapUser = createLDAPUser(); 293 294 ldapUser.setLDAPAttributes(attribs); 295 ldapUser.setTemp("turbine.user", ldapUser); 296 users.add(ldapUser); 297 } 298 } 299 catch (NamingException ex) 300 { 301 throw new DataBackendException( 302 "The LDAP server specified is unavailable", ex); 303 } 304 return users; 305 } 306 307 322 public User retrieve(String username, String password) 323 throws PasswordMismatchException, 324 UnknownEntityException, DataBackendException 325 { 326 User user = retrieve(username); 327 328 authenticate(user, password); 329 return user; 330 } 331 332 342 public void store(User user) 343 throws UnknownEntityException, DataBackendException 344 { 345 if (!accountExists(user)) 346 { 347 throw new UnknownEntityException("The account '" 348 + user.getName() + "' does not exist"); 349 } 350 351 try 352 { 353 LDAPUser ldapUser = (LDAPUser) user; 354 Attributes attrs = ldapUser.getLDAPAttributes(); 355 String name = ldapUser.getDN(); 356 357 DirContext ctx = bindAsAdmin(); 358 359 ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, attrs); 360 } 361 catch (NamingException ex) 362 { 363 throw new DataBackendException("NamingException caught", ex); 364 } 365 } 366 367 380 public void saveOnSessionUnbind(User user) 381 throws UnknownEntityException, DataBackendException 382 { 383 if (!accountExists(user)) 384 { 385 throw new UnknownEntityException("The account '" + 386 user.getName() + "' does not exist"); 387 } 388 } 389 390 403 public void authenticate(User user, String password) 404 throws PasswordMismatchException, 405 UnknownEntityException, 406 DataBackendException 407 { 408 LDAPUser ldapUser = (LDAPUser) user; 409 410 try 411 { 412 bind(ldapUser.getDN(), password); 413 } 414 catch (AuthenticationException ex) 415 { 416 throw new PasswordMismatchException( 417 "The given password for: " 418 + ldapUser.getDN() + " is invalid\n"); 419 } 420 catch (NamingException ex) 421 { 422 throw new DataBackendException( 423 "NamingException caught:", ex); 424 } 425 } 426 427 440 public void changePassword(User user, String oldPass, String newPass) 441 throws PasswordMismatchException, 442 UnknownEntityException, DataBackendException 443 { 444 throw new DataBackendException( 445 "The method changePassword has no implementation."); 446 } 447 448 463 public void forcePassword(User user, String password) 464 throws UnknownEntityException, DataBackendException 465 { 466 throw new DataBackendException( 467 "The method forcePassword has no implementation."); 468 } 469 470 478 public void createAccount(User user, String initialPassword) 479 throws EntityExistsException, DataBackendException 480 { 481 if (accountExists(user)) 482 { 483 throw new EntityExistsException("The account '" 484 + user.getName() + "' already exist"); 485 } 486 487 try 488 { 489 LDAPUser ldapUser = (LDAPUser) user; 490 Attributes attrs = ldapUser.getLDAPAttributes(); 491 String name = ldapUser.getDN(); 492 493 DirContext ctx = bindAsAdmin(); 494 495 ctx.bind(name, null, attrs); 496 } 497 catch (NamingException ex) 498 { 499 throw new DataBackendException("NamingException caught", ex); 500 } 501 } 502 503 510 public void removeAccount(User user) 511 throws UnknownEntityException, DataBackendException 512 { 513 if (!accountExists(user)) 514 { 515 throw new UnknownEntityException("The account '" 516 + user.getName() + "' does not exist"); 517 } 518 519 try 520 { 521 LDAPUser ldapUser = (LDAPUser) user; 522 String name = ldapUser.getDN(); 523 524 DirContext ctx = bindAsAdmin(); 525 526 ctx.unbind(name); 527 } 528 catch (NamingException ex) 529 { 530 throw new DataBackendException("NamingException caught", ex); 531 } 532 } 533 534 540 public static DirContext bindAsAdmin() 541 throws NamingException 542 { 543 String adminUser = LDAPSecurityConstants.getAdminUsername(); 544 String adminPassword = LDAPSecurityConstants.getAdminPassword(); 545 546 return bind(adminUser, adminPassword); 547 } 548 549 557 public static DirContext bind(String username, String password) 558 throws NamingException 559 { 560 String host = LDAPSecurityConstants.getLDAPHost(); 561 String port = LDAPSecurityConstants.getLDAPPort(); 562 String providerURL = new String ("ldap://" + host + ":" + port); 563 String ldapProvider = LDAPSecurityConstants.getLDAPProvider(); 564 String authentication = LDAPSecurityConstants.getLDAPAuthentication(); 565 566 570 Hashtable env = new Hashtable (); 571 572 env.put(Context.INITIAL_CONTEXT_FACTORY, ldapProvider); 573 env.put(Context.PROVIDER_URL, providerURL); 574 env.put(Context.SECURITY_AUTHENTICATION, authentication); 575 env.put(Context.SECURITY_PRINCIPAL, username); 576 env.put(Context.SECURITY_CREDENTIALS, password); 577 578 DirContext ctx = new javax.naming.directory.InitialDirContext (env); 579 580 return ctx; 581 } 582 583 589 private LDAPUser createLDAPUser() 590 throws DataBackendException 591 { 592 try 593 { 594 return (LDAPUser) TurbineSecurity.getUserInstance(); 595 } 596 catch (ClassCastException ex) 597 { 598 throw new DataBackendException("ClassCastException:", ex); 599 } 600 catch (UnknownEntityException ex) 601 { 602 throw new DataBackendException("UnknownEntityException:", ex); 603 } 604 } 605 606 } 607 | Popular Tags |