1 17 package org.alfresco.filesys.server.auth.passthru; 18 19 import java.util.Hashtable ; 20 21 import javax.transaction.UserTransaction ; 22 23 import org.alfresco.config.ConfigElement; 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.filesys.server.SessionListener; 26 import org.alfresco.filesys.server.SrvSession; 27 import org.alfresco.filesys.server.auth.ClientInfo; 28 import org.alfresco.filesys.server.auth.SrvAuthenticator; 29 import org.alfresco.filesys.server.auth.UserAccount; 30 import org.alfresco.filesys.server.config.InvalidConfigurationException; 31 import org.alfresco.filesys.server.config.ServerConfiguration; 32 import org.alfresco.filesys.server.core.SharedDevice; 33 import org.alfresco.filesys.smb.server.SMBServer; 34 import org.alfresco.filesys.smb.server.SMBSrvSession; 35 import org.alfresco.filesys.util.HexDump; 36 import org.alfresco.model.ContentModel; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 49 public class PassthruAuthenticator extends SrvAuthenticator implements SessionListener 50 { 51 53 private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol.auth"); 54 55 57 public final static int DefaultSessionTmo = 5000; public final static int MinSessionTmo = 2000; public final static int MaxSessionTmo = 30000; 61 63 private PassthruServers m_passthruServers; 64 65 67 private SMBServer m_server; 68 69 71 private Hashtable <String , PassthruDetails> m_sessions; 72 73 78 public PassthruAuthenticator() 79 { 80 setAccessMode(SrvAuthenticator.USER_MODE); 81 setEncryptedPasswords(true); 82 83 85 m_sessions = new Hashtable <String , PassthruDetails>(); 86 } 87 88 98 public int authenticateShareConnect(ClientInfo client, SharedDevice share, String sharePwd, SrvSession sess) 99 { 100 return SrvAuthenticator.Writeable; 101 } 102 103 111 public int authenticateUser(ClientInfo client, SrvSession sess, int alg) 112 { 113 117 if (client.isNullSession() && sess instanceof SMBSrvSession) 118 { 119 121 if ( logger.isDebugEnabled()) 122 logger.debug("Null CIFS logon allowed"); 123 124 return SrvAuthenticator.AUTH_ALLOW; 125 } 126 127 129 if ( client.getAuthenticationToken() != null && client.getLogonType() != ClientInfo.LogonNull) 130 { 131 133 m_authComponent.setCurrentUser(client.getUserName()); 134 135 137 if ( logger.isDebugEnabled()) 138 logger.debug("Re-using existing authentication token"); 139 140 142 return client.getLogonType() != ClientInfo.LogonGuest ? AUTH_ALLOW : AUTH_GUEST; 143 } 144 145 147 int authSts = AUTH_DISALLOW; 148 149 if ( client.isGuest() || client.getUserName().equalsIgnoreCase(getGuestUserName())) 150 { 151 153 if ( allowGuest() == false) 154 return AUTH_DISALLOW; 155 156 158 doGuestLogon( client, sess); 159 160 162 authSts = AUTH_GUEST; 163 164 166 if ( logger.isDebugEnabled()) 167 logger.debug("Authenticated user " + client.getUserName() + " sts=" + getStatusAsString(authSts)); 168 169 171 return authSts; 172 } 173 174 176 PassthruDetails passDetails = m_sessions.get(sess.getUniqueId()); 177 178 if (passDetails != null) 179 { 180 181 try 182 { 183 184 187 AuthenticateSession authSess = passDetails.getAuthenticateSession(); 188 authSess.doSessionSetup(client.getUserName(), client.getANSIPassword(), client.getPassword()); 189 190 192 if (authSess.isGuest()) 193 { 194 195 197 if (allowGuest() == true) 198 { 199 201 doGuestLogon( client, sess); 202 203 205 authSts = SrvAuthenticator.AUTH_GUEST; 206 207 209 if (logger.isDebugEnabled()) 210 logger.debug("Passthru authenticate user=" + client.getUserName() + ", GUEST"); 211 } 212 } 213 else 214 { 215 217 UserTransaction tx = m_transactionService.getUserTransaction( true); 218 219 try 220 { 221 223 tx.begin(); 224 225 227 String username = client.getUserName(); 228 NodeRef userNode = m_personService.getPerson( username); 229 230 if ( userNode != null) 231 { 232 234 String personName = (String ) m_nodeService.getProperty(userNode, ContentModel.PROP_USERNAME); 235 m_authComponent.setCurrentUser(personName); 236 237 239 if ( logger.isDebugEnabled()) 240 logger.debug("Setting current user using person " + personName + " (username " + username + ")"); 241 } 242 else 243 { 244 246 if ( m_personService.getUserNamesAreCaseSensitive() == false) 247 username = username.toLowerCase(); 248 m_authComponent.setCurrentUser( username); 249 250 252 if ( logger.isDebugEnabled()) 253 logger.debug("Setting current user using username " + username); 254 } 255 256 258 authSts = SrvAuthenticator.AUTH_ALLOW; 259 260 262 if (logger.isDebugEnabled()) 263 logger.debug("Passthru authenticate user=" + client.getUserName() + ", FULL"); 264 } 265 finally 266 { 267 269 if ( tx != null) 270 { 271 try { 272 tx.commit(); 273 } 274 catch (Exception ex) 275 { 276 } 278 } 279 } 280 } 281 } 282 catch (Exception ex) 283 { 284 285 287 logger.error(ex.getMessage()); 288 } 289 290 293 if ((sess instanceof SMBSrvSession) == false) 294 { 295 296 298 m_sessions.remove(sess.getUniqueId()); 299 300 302 try 303 { 304 305 307 AuthenticateSession authSess = passDetails.getAuthenticateSession(); 308 authSess.CloseSession(); 309 310 312 if (logger.isDebugEnabled()) 313 logger.debug("Closed auth session, sessId=" + authSess.getSessionId()); 314 } 315 catch (Exception ex) 316 { 317 318 320 logger.error("Passthru error closing session (auth user)", ex); 321 } 322 } 323 } 324 else 325 { 326 327 329 if (logger.isDebugEnabled()) 330 logger.debug(" No PassthruDetails for " + sess.getUniqueId()); 331 } 332 333 335 return authSts; 336 } 337 338 344 public UserAccount getUserDetails(String user) 345 { 346 347 349 return null; 350 } 351 352 358 public byte[] getChallengeKey(SrvSession sess) 359 { 360 361 363 byte[] chKey = null; 364 365 367 if ( sess.hasClientInformation() && sess.getClientInformation().getAuthenticationToken() != null && 368 sess.getClientInformation().getLogonType() != ClientInfo.LogonNull) 369 { 370 372 if ( logger.isDebugEnabled()) 373 logger.debug("Re-using existing challenge, already authenticated"); 374 375 377 return sess.getChallengeKey(); 378 } 379 else if (sess instanceof SMBSrvSession) 380 { 381 382 384 if (m_server == null) 385 { 386 387 390 SMBSrvSession smbSess = (SMBSrvSession) sess; 391 m_server = smbSess.getSMBServer(); 392 393 m_server.addSessionListener(this); 394 } 395 } 396 397 try 398 { 399 400 402 AuthenticateSession authSess = m_passthruServers.openSession(); 403 if (authSess != null) 404 { 405 406 408 PassthruDetails passDetails = new PassthruDetails(sess, authSess); 409 m_sessions.put(sess.getUniqueId(), passDetails); 410 411 413 chKey = authSess.getEncryptionKey(); 414 415 417 if (logger.isDebugEnabled()) 418 logger.debug("Passthru sessId=" + authSess.getSessionId() + ", negotiate key=[" 419 + HexDump.hexString(chKey) + "]"); 420 } 421 } 422 catch (Exception ex) 423 { 424 425 427 logger.error("Passthru error getting challenge", ex); 428 } 429 430 432 return chKey; 433 } 434 435 442 public void initialize(ServerConfiguration config, ConfigElement params) throws InvalidConfigurationException 443 { 444 445 447 super.initialize(config, params); 448 449 451 m_passthruServers = new PassthruServers(); 452 453 455 ConfigElement sessTmoElem = params.getChild("Timeout"); 456 if (sessTmoElem != null) 457 { 458 459 try 460 { 461 462 464 int sessTmo = Integer.parseInt(sessTmoElem.getValue()); 465 466 468 if ( sessTmo < MinSessionTmo || sessTmo > MaxSessionTmo) 469 throw new InvalidConfigurationException("Invalid session timeout, valid range is " + 470 MinSessionTmo + " to " + MaxSessionTmo); 471 472 474 m_passthruServers.setConnectionTimeout( sessTmo); 475 } 476 catch (NumberFormatException ex) 477 { 478 throw new InvalidConfigurationException("Invalid timeout value specified"); 479 } 480 } 481 482 484 String srvList = null; 485 486 if (params.getChild("LocalServer") != null) 487 { 488 489 491 srvList = config.getLocalServerName(true); 492 if(srvList == null) 493 throw new AlfrescoRuntimeException("Passthru authenticator failed to get local server name"); 494 } 495 496 498 ConfigElement srvNamesElem = params.getChild("Server"); 499 500 if (srvNamesElem != null && srvNamesElem.getValue().length() > 0) 501 { 502 503 505 if (srvList != null) 506 throw new AlfrescoRuntimeException("Set passthru server via local server or specify name"); 507 508 510 srvList = srvNamesElem.getValue(); 511 } 512 513 515 if (srvList != null) 516 { 517 519 m_passthruServers.setServerList(srvList); 520 } 521 else 522 { 523 524 526 String domainName = null; 527 528 530 if (params.getChild("LocalDomain") != null) 531 { 532 534 domainName = config.getLocalDomainName(); 535 } 536 537 539 ConfigElement domNameElem = params.getChild("Domain"); 540 541 if (domNameElem != null && domNameElem.getValue().length() > 0) 542 { 543 544 546 if (srvList != null) 547 throw new AlfrescoRuntimeException("Specify server or domain name for passthru authentication"); 548 549 domainName = domNameElem.getValue(); 550 } 551 552 554 if (domainName != null) 555 { 556 558 m_passthruServers.setDomain(domainName); 559 } 560 } 561 562 564 if (m_passthruServers.getTotalServerCount() == 0) 565 throw new AlfrescoRuntimeException("No valid authentication servers found for passthru"); 566 } 567 568 571 public void closeAuthenticator() 572 { 573 575 if ( m_passthruServers != null) 576 m_passthruServers.shutdown(); 577 } 578 579 584 public void sessionClosed(SrvSession sess) 585 { 586 587 590 PassthruDetails passDetails = m_sessions.get(sess.getUniqueId()); 591 592 if (passDetails != null) 593 { 594 595 597 m_sessions.remove(sess.getUniqueId()); 598 599 601 try 602 { 603 604 606 AuthenticateSession authSess = passDetails.getAuthenticateSession(); 607 authSess.CloseSession(); 608 609 611 if (logger.isDebugEnabled()) 612 logger.debug("Closed auth session, sessId=" + authSess.getSessionId()); 613 } 614 catch (Exception ex) 615 { 616 617 619 logger.error("Passthru error closing session (closed)", ex); 620 } 621 } 622 } 623 624 629 public void sessionCreated(SrvSession sess) 630 { 631 } 632 633 638 public void sessionLoggedOn(SrvSession sess) 639 { 640 641 645 if (sess.hasClientInformation() && sess.getClientInformation().getUserName() != null 646 && sess.getClientInformation().getUserName().length() > 0) 647 { 648 649 652 PassthruDetails passDetails = m_sessions.get(sess.getUniqueId()); 653 654 if (passDetails != null) 655 { 656 657 659 m_sessions.remove(sess.getUniqueId()); 660 661 663 try 664 { 665 666 668 AuthenticateSession authSess = passDetails.getAuthenticateSession(); 669 authSess.CloseSession(); 670 671 673 if (logger.isDebugEnabled()) 674 logger.debug("Closed auth session, sessId=" + authSess.getSessionId()); 675 } 676 catch (Exception ex) 677 { 678 679 681 logger.error("Passthru error closing session (logon)", ex); 682 } 683 } 684 } 685 } 686 } 687 | Popular Tags |