1 17 package org.alfresco.filesys.server.auth.passthru; 18 19 import java.io.IOException ; 20 import java.net.InetAddress ; 21 import java.net.UnknownHostException ; 22 import java.util.*; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.filesys.netbios.NetBIOSName; 26 import org.alfresco.filesys.netbios.NetBIOSNameList; 27 import org.alfresco.filesys.netbios.NetBIOSSession; 28 import org.alfresco.filesys.smb.PCShare; 29 import org.alfresco.filesys.util.IPAddress; 30 import org.apache.log4j.*; 31 32 45 public class PassthruServers 46 { 47 49 private static final Logger logger = Logger.getLogger("org.alfresco.smb.protocol.auth"); 50 51 53 private static final int DefaultConnectTimeout = 5000; 55 57 private static final long DefaultOfflineCheckInterval = 5 * 60000; 59 61 private List<PassthruServerDetails> m_onlineList; 62 private List<PassthruServerDetails> m_offlineList; 63 64 66 private int m_tmo = DefaultConnectTimeout; 67 68 70 private String m_domain; 71 72 74 private long m_offlineCheckInterval = DefaultOfflineCheckInterval; 75 76 78 PassthruOfflineChecker m_offlineChecker; 79 80 83 class PassthruOfflineChecker extends Thread 84 { 85 87 private boolean m_ishutdown; 88 89 92 PassthruOfflineChecker() 93 { 94 setDaemon(true); 95 setName("PassthruOfflineChecker"); 96 start(); 97 } 98 99 102 public void run() 103 { 104 106 m_ishutdown = false; 107 108 while ( m_ishutdown == false) 109 { 110 112 try 113 { 114 sleep( m_offlineCheckInterval); 115 } 116 catch ( InterruptedException ex) 117 { 118 } 119 120 122 if( m_ishutdown == true) 123 continue; 124 125 127 if ( getOfflineServerCount() > 0) 128 { 129 131 int idx = 0; 132 PassthruServerDetails offlineServer = null; 133 PCShare authShare = new PCShare("", "IPC$", "", ""); 134 AuthenticateSession authSess = null; 135 136 while ( idx < getOfflineServerCount()) 137 { 138 140 offlineServer = m_offlineList.get(idx); 141 142 if ( offlineServer != null) 143 { 144 try 145 { 146 148 authShare.setNodeName(offlineServer.getAddress().getHostAddress()); 149 150 152 authSess = AuthSessionFactory.OpenAuthenticateSession( authShare, getConnectionTimeout()); 153 154 156 try 157 { 158 authSess.CloseSession(); 159 } 160 catch ( Exception ex) 161 { 162 } 163 164 166 serverOnline(offlineServer); 167 } 168 catch ( Exception ex) 169 { 170 172 if ( logger.isDebugEnabled()) 173 logger.debug("Passthru offline check failed for " + offlineServer.getName()); 174 } 175 176 178 if ( offlineServer.isOnline() == false) 179 idx++; 180 } 181 } 182 } 183 } 184 185 187 if( logger.isDebugEnabled()) 188 logger.debug("Passthru offline checker thread closed"); 189 } 190 191 194 public final void shutdownRequest() 195 { 196 m_ishutdown = true; 197 this.interrupt(); 198 } 199 200 203 public final void processOfflineServers() 204 { 205 this.interrupt(); 206 } 207 } 208 209 212 public PassthruServers() 213 { 214 216 m_onlineList = new ArrayList<PassthruServerDetails>(); 217 m_offlineList = new ArrayList<PassthruServerDetails>(); 218 219 221 m_offlineChecker = new PassthruOfflineChecker(); 222 } 223 224 229 public final int getOnlineServerCount() 230 { 231 return m_onlineList.size(); 232 } 233 234 239 public final int getOfflineServerCount() 240 { 241 return m_offlineList.size(); 242 } 243 244 249 public final int getTotalServerCount() 250 { 251 return m_onlineList.size() + m_offlineList.size(); 252 } 253 254 259 public final boolean hasOnlineServers() 260 { 261 return m_onlineList.size() > 0 ? true : false; 262 } 263 264 269 public final int getConnectionTimeout() 270 { 271 return m_tmo; 272 } 273 274 279 public final boolean isDomainAuthentication() 280 { 281 return m_domain != null ? true : false; 282 } 283 284 289 public final String getDomain() 290 { 291 return m_domain; 292 } 293 294 299 public final AuthenticateSession openSession() 300 { 301 return openSession( 0); 302 } 303 304 310 public final AuthenticateSession openSession(int extFlags) 311 { 312 314 PassthruServerDetails passthruServer = getAuthenticationServer(); 315 if ( passthruServer == null) 316 return null; 317 318 320 if ( logger.isDebugEnabled()) 321 logger.debug("Open authenticate session to " + passthruServer); 322 323 325 PCShare authShare = new PCShare(passthruServer.getAddress().getHostAddress(), "IPC$", "", ""); 326 authShare.setExtendedSecurityFlags( extFlags); 327 328 AuthenticateSession authSess = null; 329 330 while ( authSess == null && passthruServer != null && hasOnlineServers()) { 331 332 try 333 { 334 336 authSess = AuthSessionFactory.OpenAuthenticateSession( authShare, getConnectionTimeout()); 337 338 340 passthruServer.incrementAuthenticationCount(); 341 } 342 catch ( Exception ex) 343 { 344 346 if ( logger.isDebugEnabled()) 347 logger.debug("Failed to connect to " + passthruServer + " : " + ex.getMessage()); 348 349 351 serverOffline(passthruServer); 352 } 353 354 356 if ( authSess == null) 357 { 358 360 passthruServer = getAuthenticationServer(); 361 362 364 if(logger.isDebugEnabled()) 365 logger.debug("Trying authentication server " + passthruServer); 366 } 367 } 368 369 371 return authSess; 372 } 373 374 379 protected PassthruServerDetails getAuthenticationServer() 380 { 381 383 PassthruServerDetails passthruServer = null; 384 385 synchronized ( m_onlineList) 386 { 387 if ( m_onlineList.size() > 1) 388 m_onlineList.add(m_onlineList.remove(0)); 389 if ( m_onlineList.size() > 0) 390 passthruServer = m_onlineList.get(0); 391 } 392 393 return passthruServer; 394 } 395 396 401 protected final void serverOffline(PassthruServerDetails server) 402 { 403 405 server.setOnline(false); 406 407 409 synchronized( m_onlineList) 410 { 411 m_onlineList.remove(server); 412 } 413 414 416 synchronized( m_offlineList) 417 { 418 m_offlineList.add( server); 419 } 420 421 423 if ( logger.isDebugEnabled()) 424 logger.debug("Passthru server offline, " + server); 425 } 426 427 432 protected final void serverOnline(PassthruServerDetails server) 433 { 434 436 server.setOnline(true); 437 438 440 synchronized( m_offlineList) 441 { 442 m_offlineList.remove(server); 443 } 444 445 447 synchronized( m_onlineList) 448 { 449 m_onlineList.add( server); 450 } 451 452 454 if ( logger.isDebugEnabled()) 455 logger.debug("Passthru server online, " + server); 456 } 457 458 463 public final void setConnectionTimeout(int tmo) 464 { 465 m_tmo = tmo; 466 } 467 468 473 public final void setOfflineCheckInterval(long interval) 474 { 475 m_offlineCheckInterval = interval * 1000L; 476 } 477 478 484 public final void setServerList(String servers) 485 { 486 488 StringTokenizer tokens = new StringTokenizer(servers, ","); 489 490 while ( tokens.hasMoreTokens()) 491 { 492 494 String srvName = tokens.nextToken().trim(); 495 496 499 InetAddress srvAddr = null; 500 501 if ( IPAddress.isNumericAddress(srvName)) 502 { 503 505 try 506 { 507 509 srvAddr = InetAddress.getByName(srvName); 510 srvName = srvAddr.getHostName(); 511 } 512 catch ( UnknownHostException ex) 513 { 514 516 if ( logger.isDebugEnabled()) 517 logger.debug("Passthru failed to find name/address for " + srvName); 518 } 519 } 520 else 521 { 522 524 try 525 { 526 srvAddr = InetAddress.getByName(srvName); 527 } 528 catch ( UnknownHostException ex) 529 { 530 532 if ( logger.isDebugEnabled()) 533 logger.debug("Passthru failed to find address for " + srvName); 534 } 535 } 536 537 539 if ( srvName != null && srvAddr != null) 540 { 541 543 PassthruServerDetails passthruServer = new PassthruServerDetails(srvName, null, srvAddr, false); 544 m_offlineList.add( passthruServer); 545 546 548 if ( logger.isDebugEnabled()) 549 logger.debug("Added passthru server " + passthruServer); 550 } 551 } 552 553 556 m_offlineChecker.processOfflineServers(); 557 } 558 559 564 public final void setDomain(String domain) 565 { 566 567 569 if (logger.isDebugEnabled()) 570 logger.debug("Passthru finding domain controller for " + domain + " ..."); 571 572 574 NetBIOSName nbName = null; 575 576 try 577 { 578 580 nbName = NetBIOSSession.FindName(domain, NetBIOSName.DomainControllers, getConnectionTimeout()); 581 582 584 if (logger.isDebugEnabled()) 585 logger.debug(" Found " + nbName.numberOfAddresses() + " domain controller(s)"); 586 } 587 catch (IOException ex) 588 { 589 } 590 591 593 if ( nbName == null) { 594 595 try 596 { 597 599 nbName = NetBIOSSession.FindName( domain, NetBIOSName.MasterBrowser, getConnectionTimeout()); 600 601 603 if (logger.isDebugEnabled()) 604 logger.debug(" Found browse master at " + nbName.getIPAddressString(0)); 605 } 606 catch (IOException ex) 607 { 608 throw new AlfrescoRuntimeException("Failed to find domain controller or browse master for " + domain); 609 } 610 } 611 612 616 for ( int i = 0; i < nbName.numberOfAddresses(); i++) 617 { 618 620 InetAddress dcAddr = null; 621 String dcName = null; 622 623 try 624 { 625 627 dcAddr = InetAddress.getByName(nbName.getIPAddressString(i)); 628 629 631 NetBIOSNameList nameList = NetBIOSSession.FindNamesForAddress(dcAddr.getHostAddress()); 632 NetBIOSName dcNBName = nameList.findName(NetBIOSName.FileServer, false); 633 634 if ( dcNBName != null) 635 dcName = dcNBName.getName(); 636 } 637 catch (UnknownHostException ex) 638 { 639 641 if ( logger.isDebugEnabled()) 642 logger.debug("Invalid address for server " + nbName.getIPAddressString(i)); 643 } 644 catch (Exception ex) 645 { 646 648 dcName = dcAddr.getHostAddress(); 649 650 652 if ( logger.isDebugEnabled()) 653 logger.debug("Failed to get NetBIOS name for server " + dcAddr); 654 } 655 656 658 if ( dcAddr != null) 659 { 660 662 PassthruServerDetails passthruServer = new PassthruServerDetails(dcName, domain, dcAddr, false); 663 m_offlineList.add(passthruServer); 664 665 667 if ( logger.isDebugEnabled()) 668 logger.debug("Added passthru server " + passthruServer); 669 } 670 } 671 672 675 m_offlineChecker.processOfflineServers(); 676 } 677 678 681 public final void shutdown() 682 { 683 685 m_offlineChecker.shutdownRequest(); 686 687 689 m_onlineList.clear(); 690 m_offlineList.clear(); 691 } 692 693 698 public String toString() 699 { 700 StringBuilder str = new StringBuilder (); 701 702 str.append("["); 703 704 if ( isDomainAuthentication()) 705 { 706 str.append("Domain:"); 707 str.append(getDomain()); 708 } 709 else 710 str.append("Servers:"); 711 712 str.append(",Online="); 713 str.append(getOnlineServerCount()); 714 str.append(",Offline="); 715 str.append(getOfflineServerCount()); 716 str.append("]"); 717 718 return str.toString(); 719 } 720 } 721 | Popular Tags |