1 17 package org.alfresco.filesys.server.auth; 18 19 import java.security.NoSuchAlgorithmException ; 20 import java.util.Random ; 21 22 import javax.transaction.UserTransaction ; 23 24 import net.sf.acegisecurity.Authentication; 25 26 import org.alfresco.config.ConfigElement; 27 import org.alfresco.filesys.server.SrvSession; 28 import org.alfresco.filesys.server.config.InvalidConfigurationException; 29 import org.alfresco.filesys.server.config.ServerConfiguration; 30 import org.alfresco.filesys.server.core.SharedDevice; 31 import org.alfresco.filesys.server.filesys.DiskDeviceContext; 32 import org.alfresco.filesys.server.filesys.DiskInterface; 33 import org.alfresco.filesys.server.filesys.DiskSharedDevice; 34 import org.alfresco.filesys.server.filesys.SrvDiskInfo; 35 import org.alfresco.filesys.smb.server.repo.ContentContext; 36 import org.alfresco.model.ContentModel; 37 import org.alfresco.repo.security.authentication.AuthenticationComponent; 38 import org.alfresco.repo.security.authentication.MD4PasswordEncoder; 39 import org.alfresco.repo.security.authentication.MD4PasswordEncoderImpl; 40 import org.alfresco.repo.security.authentication.NTLMMode; 41 import org.alfresco.service.cmr.repository.NodeRef; 42 import org.alfresco.service.cmr.repository.NodeService; 43 import org.alfresco.service.cmr.security.AuthenticationService; 44 import org.alfresco.service.cmr.security.PersonService; 45 import org.alfresco.service.transaction.TransactionService; 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 49 54 public abstract class SrvAuthenticator 55 { 56 58 protected static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol.auth"); 59 60 62 public static final int LANMAN = PasswordEncryptor.LANMAN; 63 public static final int NTLM1 = PasswordEncryptor.NTLM1; 64 public static final int NTLM2 = PasswordEncryptor.NTLM2; 65 66 68 public static final int AUTH_ALLOW = 0; 69 public static final int AUTH_GUEST = 0x10000000; 70 public static final int AUTH_DISALLOW = -1; 71 public static final int AUTH_BADPASSWORD = -2; 72 public static final int AUTH_BADUSER = -3; 73 74 76 public static final int NoAccess = 0; 77 public static final int ReadOnly = 1; 78 public static final int Writeable = 2; 79 80 82 public static final int SHARE_MODE = 0; 83 public static final int USER_MODE = 1; 84 85 87 public static final int STANDARD_PASSWORD_LEN = 24; 88 89 91 protected static final String GUEST_USERNAME = "guest"; 92 93 95 private int m_accessMode = SHARE_MODE; 96 97 99 private boolean m_encryptPwd = false; 100 101 103 private PasswordEncryptor m_encryptor = new PasswordEncryptor(); 104 105 107 private boolean m_allowGuest; 108 private boolean m_mapToGuest; 109 110 112 private String m_guestUserName = GUEST_USERNAME; 113 114 116 protected Random m_random = new Random (System.currentTimeMillis()); 117 118 120 protected ServerConfiguration m_config; 121 122 124 protected AuthenticationComponent m_authComponent; 125 126 128 protected MD4PasswordEncoder m_md4Encoder = new MD4PasswordEncoderImpl(); 129 130 132 protected NodeService m_nodeService; 133 protected PersonService m_personService; 134 protected TransactionService m_transactionService; 135 protected AuthenticationService m_authenticationService; 136 137 147 public int authenticateShareConnect(ClientInfo client, SharedDevice share, String sharePwd, SrvSession sess) 148 { 149 153 return SrvAuthenticator.Writeable; 154 } 155 156 164 public abstract int authenticateUser(ClientInfo client, SrvSession sess, int alg); 165 166 172 public UserAccount getUserDetails(String user) 173 { 174 return null; 175 } 176 177 185 public final int authenticateUserPlainText(ClientInfo client, SrvSession sess) 186 { 187 188 190 sess.setChallengeKey(getChallengeKey(sess)); 191 192 if (sess.hasChallengeKey() == false) 193 return SrvAuthenticator.AUTH_DISALLOW; 194 195 197 String textPwd = client.getPasswordAsString(); 198 if (textPwd == null) 199 textPwd = client.getANSIPasswordAsString(); 200 201 203 byte[] encPwd = generateEncryptedPassword(textPwd, sess.getChallengeKey(), SrvAuthenticator.NTLM1); 204 client.setPassword(encPwd); 205 206 208 return authenticateUser(client, sess, SrvAuthenticator.NTLM1); 209 } 210 211 218 public void initialize(ServerConfiguration config, ConfigElement params) throws InvalidConfigurationException 219 { 220 222 m_config = config; 223 224 226 m_authComponent = m_config.getAuthenticationComponent(); 227 228 if ( m_authComponent == null) 229 throw new InvalidConfigurationException("Authentication component not available"); 230 231 233 m_nodeService = config.getNodeService(); 234 m_personService = config.getPersonService(); 235 m_transactionService = config.getTransactionService(); 236 m_authenticationService = config.getAuthenticationService(); 237 238 240 setGuestUserName( m_authComponent.getGuestUserName()); 241 242 244 if ( validateAuthenticationMode() == false) 245 throw new InvalidConfigurationException("Required authentication mode not available"); 246 } 247 248 253 protected boolean validateAuthenticationMode() 254 { 255 return true; 256 } 257 258 267 protected final byte[] generateEncryptedPassword(String plainPwd, byte[] encryptKey, int alg) 268 { 269 270 272 byte[] encPwd = null; 273 274 try 275 { 276 277 279 encPwd = m_encryptor.generateEncryptedPassword(plainPwd, encryptKey, alg); 280 } 281 catch (NoSuchAlgorithmException ex) 282 { 283 } 284 285 287 return encPwd; 288 } 289 290 295 public final int getAccessMode() 296 { 297 return m_accessMode; 298 } 299 300 306 public abstract byte[] getChallengeKey(SrvSession sess); 307 308 313 public final boolean hasEncryptPasswords() 314 { 315 return m_encryptPwd; 316 } 317 318 323 public final boolean allowGuest() 324 { 325 return m_allowGuest; 326 } 327 328 333 public final String getGuestUserName() 334 { 335 return m_guestUserName; 336 } 337 338 343 public final boolean mapUnknownUserToGuest() 344 { 345 return m_mapToGuest; 346 } 347 348 353 public final void setAccessMode(int mode) 354 { 355 m_accessMode = mode; 356 } 357 358 363 public final void setEncryptedPasswords(boolean encFlag) 364 { 365 m_encryptPwd = encFlag; 366 } 367 368 373 public final void setAllowGuest(boolean ena) 374 { 375 m_allowGuest = ena; 376 } 377 378 383 public final void setGuestUserName( String guest) 384 { 385 m_guestUserName = guest; 386 } 387 388 393 public final void setMapToGuest( boolean ena) 394 { 395 m_mapToGuest = ena; 396 } 397 398 401 public void closeAuthenticator() 402 { 403 } 405 406 416 protected final boolean validatePassword(String plainPwd, byte[] encryptedPwd, byte[] encryptKey, int alg) 417 { 418 419 421 byte[] encPwd = generateEncryptedPassword(plainPwd != null ? plainPwd : "", encryptKey, alg); 422 423 425 if (encPwd != null && encryptedPwd != null && encPwd.length == STANDARD_PASSWORD_LEN 426 && encryptedPwd.length == STANDARD_PASSWORD_LEN) 427 { 428 429 431 for (int i = 0; i < STANDARD_PASSWORD_LEN; i++) 432 if (encPwd[i] != encryptedPwd[i]) 433 return false; 434 435 437 return true; 438 } 439 440 442 return false; 443 } 444 445 451 452 protected final byte[] convertPassword(String pwd) 453 { 454 455 457 StringBuffer p14str = new StringBuffer (); 458 p14str.append(pwd); 459 if (p14str.length() > 14) 460 p14str.setLength(14); 461 else 462 { 463 while (p14str.length() < 14) 464 p14str.append((char) 0x00); 465 } 466 467 469 return p14str.toString().getBytes(); 470 } 471 472 477 protected final PasswordEncryptor getEncryptor() 478 { 479 return m_encryptor; 480 } 481 482 488 protected final String getStatusAsString(int sts) 489 { 490 String str = null; 491 492 switch ( sts) 493 { 494 case AUTH_ALLOW: 495 str = "Allow"; 496 break; 497 case AUTH_DISALLOW: 498 str = "Disallow"; 499 break; 500 case AUTH_GUEST: 501 str = "Guest"; 502 break; 503 case AUTH_BADPASSWORD: 504 str = "BadPassword"; 505 break; 506 case AUTH_BADUSER: 507 str = "BadUser"; 508 break; 509 } 510 511 return str; 512 } 513 514 520 protected final void doGuestLogon( ClientInfo client, SrvSession sess) 521 { 522 524 m_authenticationService.authenticateAsGuest(); 525 Authentication authToken = m_authComponent.getCurrentAuthentication(); 526 527 client.setAuthenticationToken( authToken); 528 529 531 client.setUserName( getGuestUserName()); 532 getHomeFolderForUser( client); 533 534 536 client.setGuest( true); 537 538 541 DiskInterface diskDrv = m_config.getDiskInterface(); 542 DiskDeviceContext diskCtx = new ContentContext("", "", client.getHomeFolder()); 543 544 546 diskCtx.setDiskInformation(new SrvDiskInfo(2560, 64, 512, 2304)); 547 548 550 sess.addDynamicShare( new DiskSharedDevice( client.getUserName(), diskDrv, diskCtx, SharedDevice.Temporary)); 551 } 552 553 558 protected final void getHomeFolderForUser(ClientInfo client) 559 { 560 562 UserTransaction tx = m_transactionService.getUserTransaction(); 563 NodeRef homeSpaceRef = null; 564 565 try 566 { 567 tx.begin(); 568 homeSpaceRef = (NodeRef) m_nodeService.getProperty(m_personService.getPerson(client.getUserName()), 569 ContentModel.PROP_HOMEFOLDER); 570 client.setHomeFolder( homeSpaceRef); 571 tx.commit(); 572 } 573 catch (Throwable ex) 574 { 575 try 576 { 577 tx.rollback(); 578 } 579 catch (Throwable ex2) 580 { 581 logger.error("Failed to rollback transaction", ex2); 582 } 583 584 if (ex instanceof RuntimeException ) 586 { 587 throw (RuntimeException ) ex; 588 } 589 else 590 { 591 throw new RuntimeException ("Error during execution of transaction.", ex); 592 } 593 } 594 } 595 596 } | Popular Tags |