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 23 import org.alfresco.filesys.netbios.NetBIOSName; 24 import org.alfresco.filesys.netbios.NetBIOSNameList; 25 import org.alfresco.filesys.netbios.NetBIOSSession; 26 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol; 27 import org.alfresco.filesys.server.auth.PasswordEncryptor; 28 import org.alfresco.filesys.smb.DataType; 29 import org.alfresco.filesys.smb.Dialect; 30 import org.alfresco.filesys.smb.DialectSelector; 31 import org.alfresco.filesys.smb.NetworkSession; 32 import org.alfresco.filesys.smb.PCShare; 33 import org.alfresco.filesys.smb.PacketType; 34 import org.alfresco.filesys.smb.Protocol; 35 import org.alfresco.filesys.smb.SMBException; 36 import org.alfresco.filesys.util.IPAddress; 37 import org.alfresco.filesys.util.StringList; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 47 public final class AuthSessionFactory 48 { 49 50 52 private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol.auth"); 53 54 56 private static final int BROADCAST_LOOKUP_TIMEOUT = 4000; 58 60 private static DialectSelector m_defDialects; 61 62 64 private static int m_sessIdx = 1; 65 66 68 private static String m_localDomain = null; 69 70 72 private static String m_localBrowseMaster = null; 73 74 76 private static int m_defPktSize = 4096 + RFCNetBIOSProtocol.HEADER_LEN; 77 78 80 private static InetAddress [] m_localAddrList; 81 82 84 private static PasswordEncryptor m_encryptor = new PasswordEncryptor(); 85 86 static 87 { 88 89 91 m_defDialects = new DialectSelector(); 92 m_defDialects.AddDialect(Dialect.Core); 93 m_defDialects.AddDialect(Dialect.CorePlus); 94 m_defDialects.AddDialect(Dialect.DOSLanMan1); 95 m_defDialects.AddDialect(Dialect.DOSLanMan2); 96 m_defDialects.AddDialect(Dialect.LanMan1); 97 m_defDialects.AddDialect(Dialect.LanMan2); 98 m_defDialects.AddDialect(Dialect.LanMan2_1); 99 m_defDialects.AddDialect(Dialect.NT); 100 } 101 102 104 private static String m_defUserName = ""; 105 private static String m_defPassword = ""; 106 private static String m_defDomain = "?"; 107 108 110 private static int m_primaryProto = Protocol.TCPNetBIOS; 111 private static int m_secondaryProto = Protocol.NativeSMB; 112 113 115 private static int m_netbiosPort = RFCNetBIOSProtocol.PORT; 116 117 119 private static String m_netBIOSScopeId = null; 120 121 123 private static boolean m_useExtendedSec = true; 124 125 133 134 private final static StringList BuildNegotiatePacket(SMBPacket pkt, DialectSelector dlct, int pid) 135 { 136 137 139 pkt.setCommand(PacketType.Negotiate); 140 pkt.setProcessId(pid); 141 142 144 int flags2 = 0; 145 146 if (dlct.hasDialect(Dialect.NT)) 147 flags2 += SMBPacket.FLG2_UNICODE; 148 149 if ( useExtendedSecurity()) 150 flags2 += SMBPacket.FLG2_EXTENDEDSECURITY; 151 152 pkt.setFlags2(flags2); 153 154 156 StringBuilder dia = new StringBuilder (); 157 StringList diaList = new StringList(); 158 159 162 int d = Dialect.Core; 163 164 while (d < Dialect.Max) 165 { 166 167 169 if (dlct.hasDialect(d)) 170 { 171 172 175 for (int i = 0; i < Dialect.NumberOfDialects(); i++) 176 { 177 178 180 if (Dialect.DialectType(i) == d) 181 { 182 183 185 String curDialect = Dialect.DialectString(i); 186 diaList.addString(curDialect); 187 188 190 dia.append(DataType.Dialect); 191 dia.append(curDialect); 192 dia.append((char) 0x00); 193 } 194 } 195 } 196 197 199 d++; 200 } 201 202 204 pkt.setBytes(dia.toString().getBytes()); 205 206 208 return diaList; 209 } 210 211 216 protected final static int DefaultPacketSize() 217 { 218 return m_defPktSize; 219 } 220 221 226 public final static DialectSelector getDefaultDialects() 227 { 228 return m_defDialects; 229 } 230 231 236 public static String getDefaultDomain() 237 { 238 return m_defDomain; 239 } 240 241 246 public static String getDefaultPassword() 247 { 248 return m_defPassword; 249 } 250 251 256 public static String getDefaultUserName() 257 { 258 return m_defUserName; 259 } 260 261 266 public static String getNetBIOSNameScope() 267 { 268 return m_netBIOSScopeId; 269 } 270 271 276 public static int getNetBIOSPort() 277 { 278 return m_netbiosPort; 279 } 280 281 286 public static final int getPrimaryProtocol() 287 { 288 return m_primaryProto; 289 } 290 291 297 public static final int getSecondaryProtocol() 298 { 299 return m_secondaryProto; 300 } 301 302 307 private static synchronized int getSessionId() 308 { 309 int sessId = m_sessIdx++ + (NetBIOSSession.getJVMIndex() * 100); 310 return sessId; 311 } 312 313 318 private static synchronized InetAddress [] getLocalTcpipAddresses() 319 { 320 321 323 if (m_localAddrList == null) 324 { 325 try 326 { 327 m_localAddrList = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()); 328 } 329 catch (UnknownHostException ex) 330 { 331 } 332 } 333 334 336 return m_localAddrList; 337 } 338 339 344 public final static boolean hasNetBIOSNameScope() 345 { 346 return m_netBIOSScopeId != null ? true : false; 347 } 348 349 354 public static final boolean useExtendedSecurity() 355 { 356 return m_useExtendedSec; 357 } 358 359 371 public static AuthenticateSession OpenAuthenticateSession(PCShare shr, int tmo) throws java.io.IOException , 372 java.net.UnknownHostException , SMBException 373 { 374 375 377 return OpenAuthenticateSession(shr, tmo, null); 378 } 379 380 393 public static AuthenticateSession OpenAuthenticateSession(PCShare shr, int tmo, DialectSelector dia) 394 throws java.io.IOException , java.net.UnknownHostException , SMBException 395 { 396 397 399 int pid = getSessionId(); 400 401 StringBuilder nameBuf = new StringBuilder (InetAddress.getLocalHost().getHostName() + "_" + pid); 402 String localName = nameBuf.toString(); 403 404 406 if (logger.isDebugEnabled()) 407 logger.debug("New auth session from " + localName + " to " + shr.toString()); 408 409 411 NetworkSession netSession = null; 412 413 switch (getPrimaryProtocol()) 414 { 415 416 418 case Protocol.TCPNetBIOS: 419 netSession = connectNetBIOSSession(shr.getNodeName(), localName, tmo); 420 break; 421 422 424 case Protocol.NativeSMB: 425 netSession = connectNativeSMBSession(shr.getNodeName(), localName, tmo); 426 break; 427 } 428 429 432 if (netSession == null) 433 { 434 435 437 switch (getSecondaryProtocol()) 438 { 439 440 442 case Protocol.TCPNetBIOS: 443 netSession = connectNetBIOSSession(shr.getNodeName(), localName, tmo); 444 break; 445 446 448 case Protocol.NativeSMB: 449 netSession = connectNativeSMBSession(shr.getNodeName(), localName, tmo); 450 break; 451 } 452 } 453 454 456 if (netSession == null) 457 throw new IOException ("Failed to connect to host, " + shr.getNodeName()); 458 459 461 if (logger.isDebugEnabled()) 462 logger.debug("Connected session, protocol : " + netSession.getProtocolName()); 463 464 467 SMBPacket pkt = new SMBPacket(); 468 DialectSelector selDialect = dia; 469 470 if (selDialect == null) 471 { 472 473 475 selDialect = new DialectSelector(); 476 selDialect.copyFrom(m_defDialects); 477 } 478 479 481 StringList diaList = BuildNegotiatePacket(pkt, selDialect, pid); 482 pkt.ExchangeLowLevelSMB(netSession, pkt, true); 483 484 486 String diaStr = diaList.getStringAt(pkt.getParameter(0)); 487 int dialectId = Dialect.DialectType(diaStr); 488 489 491 if (logger.isDebugEnabled()) 492 logger.debug("SessionFactory: Negotiated SMB dialect " + diaStr); 493 494 if (dialectId == Dialect.Unknown) 495 throw new java.io.IOException ("Unknown SMB dialect"); 496 497 499 AuthenticateSession authSess = new AuthenticateSession(shr, netSession, dialectId, pkt); 500 return authSess; 501 } 502 503 508 public static void setDefaultDomain(String domain) 509 { 510 m_defDomain = domain; 511 } 512 513 518 public static void setDefaultPassword(String pwd) 519 { 520 m_defPassword = pwd; 521 } 522 523 528 public static void setDefaultUserName(String user) 529 { 530 m_defUserName = user; 531 } 532 533 538 public static final void setExtendedSecurity(boolean ena) 539 { 540 m_useExtendedSec = ena; 541 } 542 543 549 public static void setNetBIOSPort(int port) 550 { 551 m_netbiosPort = port; 552 } 553 554 559 public static void setNetBIOSNameScope(String scope) 560 { 561 if (scope != null && scope.startsWith(".")) 562 m_netBIOSScopeId = scope.substring(1); 563 else 564 m_netBIOSScopeId = scope; 565 } 566 567 574 public static final boolean setProtocolOrder(int pri, int sec) 575 { 576 577 579 if (pri != Protocol.TCPNetBIOS && pri != Protocol.NativeSMB) 580 return false; 581 582 584 if (pri == sec) 585 return false; 586 587 589 m_primaryProto = pri; 590 m_secondaryProto = sec; 591 592 return true; 593 } 594 595 601 public final static void setSubnetMask(String subnet) 602 { 603 NetBIOSSession.setSubnetMask(subnet); 604 } 605 606 609 private static void SetupDefaultDialects() 610 { 611 612 614 if (m_defDialects != null) 615 m_defDialects = new DialectSelector(); 616 else 617 m_defDialects.ClearAll(); 618 619 621 m_defDialects.AddDialect(Dialect.Core); 622 m_defDialects.AddDialect(Dialect.CorePlus); 623 m_defDialects.AddDialect(Dialect.DOSLanMan1); 624 m_defDialects.AddDialect(Dialect.DOSLanMan2); 625 m_defDialects.AddDialect(Dialect.LanMan1); 626 m_defDialects.AddDialect(Dialect.LanMan2); 627 m_defDialects.AddDialect(Dialect.LanMan2_1); 628 m_defDialects.AddDialect(Dialect.NT); 629 } 630 631 640 private static final NetworkSession connectNetBIOSSession(String toName, String fromName, int tmo) 641 throws IOException 642 { 643 644 646 NetBIOSSession nbSession = new NetBIOSSession(tmo, getNetBIOSPort()); 647 648 650 String toAddr = null; 651 NetBIOSName nbName = null; 652 653 if (IPAddress.isNumericAddress(toName)) 654 { 655 656 try 657 { 658 659 661 toAddr = toName; 662 NetBIOSNameList nameList = NetBIOSSession.FindNamesForAddress(toAddr); 663 664 666 nbName = nameList.findName(NetBIOSName.FileServer, false); 667 if (nbName == null) 668 throw new IOException ("Server service not running"); 669 670 672 toName = nbName.getName(); 673 } 674 catch (UnknownHostException ex) 675 { 676 return null; 677 } 678 } 679 else 680 { 681 IOException savedException = null; 682 683 try 684 { 685 686 688 nbName = NetBIOSSession.FindName(toName, NetBIOSName.FileServer, 500); 689 } 690 catch (IOException ex) 691 { 692 savedException = ex; 693 } 694 695 697 if (nbName == null) 698 { 699 700 702 NetBIOSNameList localList = NetBIOSSession.FindNamesForAddress(InetAddress.getLocalHost() 703 .getHostAddress()); 704 if (localList != null) 705 { 706 nbName = localList.findName(toName, NetBIOSName.FileServer, false); 707 if (nbName != null) 708 nbName.addIPAddress(InetAddress.getLocalHost().getAddress()); 709 else 710 throw savedException; 711 } 712 } 713 } 714 715 718 if (hasNetBIOSNameScope()) 719 { 720 721 723 toName = toName + "." + getNetBIOSNameScope(); 724 fromName = fromName + "." + getNetBIOSNameScope(); 725 } 726 727 732 if (nbName.numberOfAddresses() > 1) 733 { 734 735 738 InetAddress [] addrList = getLocalTcpipAddresses(); 739 int addrIdx = nbName.findBestMatchAddress(addrList); 740 741 if (addrIdx != -1) 742 { 743 744 try 745 { 746 747 749 String ipAddr = nbName.getIPAddressString(addrIdx); 750 751 753 if (logger.isDebugEnabled()) 754 logger.debug("Server is multi-homed, trying to connect to " + ipAddr); 755 756 758 nbSession.Open(toName, fromName, ipAddr); 759 760 762 if (nbSession.isConnected() == false) 763 { 764 765 767 try 768 { 769 nbSession.Close(); 770 } 771 catch (Exception ex) 772 { 773 } 774 } 775 else if (logger.isDebugEnabled() && nbSession.isConnected()) 776 logger.debug("Connected to address " + ipAddr); 777 } 778 catch (IOException ex) 779 { 780 } 781 } 782 } 783 784 786 if (logger.isDebugEnabled() && nbSession.isConnected() == false 787 && nbName.numberOfAddresses() > 1) 788 logger.debug("Server is multi-homed, trying all addresses"); 789 790 793 IOException lastException = null; 794 int addrIdx = 0; 795 796 while (nbSession.isConnected() == false && addrIdx < nbName.numberOfAddresses()) 797 { 798 799 try 800 { 801 802 804 String ipAddr = nbName.getIPAddressString(addrIdx++); 805 806 808 if (logger.isDebugEnabled()) 809 logger.debug("Trying address " + ipAddr); 810 811 813 nbSession.Open(toName, fromName, ipAddr); 814 815 817 if (nbSession.isConnected() == false) 818 { 819 820 822 try 823 { 824 nbSession.Close(); 825 } 826 catch (Exception ex) 827 { 828 } 829 } 830 else if (logger.isDebugEnabled() && nbSession.isConnected()) 831 logger.debug("Connected to address " + ipAddr); 832 } 833 catch (IOException ex) 834 { 835 836 838 lastException = ex; 839 } 840 } 841 842 844 if (nbSession.isConnected() == false) 845 { 846 847 849 if (lastException != null) 850 throw lastException; 851 852 854 return null; 855 } 856 857 859 return nbSession; 860 } 861 862 871 private static final NetworkSession connectNativeSMBSession(String toName, String fromName, int tmo) 872 throws IOException 873 { 874 875 877 TcpipSMBNetworkSession tcpSession = new TcpipSMBNetworkSession(tmo); 878 879 try 880 { 881 882 884 tcpSession.Open(toName, fromName, null); 885 886 888 if (tcpSession.isConnected() == false) 889 { 890 891 893 try 894 { 895 tcpSession.Close(); 896 } 897 catch (Exception ex) 898 { 899 } 900 901 903 return null; 904 } 905 } 906 catch (Exception ex) 907 { 908 try 909 { 910 tcpSession.Close(); 911 } 912 catch (Exception ex2) 913 { 914 } 915 tcpSession = null; 916 } 917 918 920 return tcpSession; 921 } 922 } | Popular Tags |