1 17 package org.alfresco.filesys.smb.server; 18 19 import java.io.FileNotFoundException ; 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.NetworkInterface ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 26 import org.alfresco.filesys.netbios.NetworkSettings; 27 import org.alfresco.filesys.server.ServerListener; 28 import org.alfresco.filesys.server.SrvSessionList; 29 import org.alfresco.filesys.server.auth.SrvAuthenticator; 30 import org.alfresco.filesys.server.auth.UserAccountList; 31 import org.alfresco.filesys.server.config.ServerConfiguration; 32 import org.alfresco.filesys.server.core.InvalidDeviceInterfaceException; 33 import org.alfresco.filesys.server.core.ShareType; 34 import org.alfresco.filesys.server.core.SharedDevice; 35 import org.alfresco.filesys.server.filesys.DiskInterface; 36 import org.alfresco.filesys.server.filesys.NetworkFileServer; 37 import org.alfresco.filesys.smb.DialectSelector; 38 import org.alfresco.filesys.smb.SMBException; 39 import org.alfresco.filesys.smb.ServerType; 40 import org.alfresco.filesys.smb.mailslot.HostAnnouncer; 41 import org.alfresco.filesys.smb.server.win32.Win32NetBIOSLanaMonitor; 42 import org.alfresco.filesys.smb.server.win32.Win32NetBIOSSessionSocketHandler; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 46 53 public class SMBServer extends NetworkFileServer implements Runnable 54 { 55 56 58 private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol"); 59 60 62 private static final String ServerVersion = "3.5.1"; 63 64 66 private Thread m_srvThread; 67 68 70 private Vector <SessionSocketHandler> m_sessionHandlers; 71 72 74 private Vector <HostAnnouncer> m_hostAnnouncers; 75 76 78 private SrvSessionList m_sessions; 79 80 82 private int m_srvType = ServerType.WorkStation + ServerType.Server; 83 84 86 private int m_sessId; 87 88 90 private boolean m_shutdown = false; 91 private boolean m_active = false; 92 93 99 public SMBServer(ServerConfiguration cfg) throws IOException 100 { 101 super("SMB", cfg); 102 103 CommonConstructor(); 105 } 106 107 113 public final synchronized boolean addShare(SharedDevice shr) 114 { 115 116 120 if (shr.getType() == ShareType.DISK) 121 { 122 123 125 checkReadOnly(shr); 126 } 127 128 130 boolean sts = getConfiguration().getShares().addShare(shr); 131 132 134 if (logger.isInfoEnabled()) 135 logger.info("[SMB] Add Share " + shr.toString() + " : " + sts); 136 137 139 return sts; 140 } 141 142 147 public final void addSessionHandler(SessionSocketHandler handler) 148 { 149 150 152 if (m_sessionHandlers == null) 153 m_sessionHandlers = new Vector <SessionSocketHandler>(); 154 155 157 m_sessionHandlers.addElement(handler); 158 } 159 160 165 public final void addHostAnnouncer(HostAnnouncer announcer) 166 { 167 168 170 if (m_hostAnnouncers == null) 171 m_hostAnnouncers = new Vector <HostAnnouncer>(); 172 173 175 m_hostAnnouncers.addElement(announcer); 176 } 177 178 183 public final void addSession(SMBSrvSession sess) 184 { 185 186 188 m_sessions.addSession(sess); 189 190 192 sess.setDebug(getConfiguration().getSessionDebugFlags()); 193 } 194 195 200 protected final void checkReadOnly(SharedDevice shr) 201 { 202 203 207 if (shr.getType() == ShareType.DISK) 208 { 209 210 212 try 213 { 214 215 217 DiskInterface disk = (DiskInterface) shr.getInterface(); 218 if (disk.isReadOnly(null, shr.getContext())) 219 { 220 221 223 int attr = shr.getAttributes(); 224 if ((attr & SharedDevice.ReadOnly) == 0) 225 attr += SharedDevice.ReadOnly; 226 shr.setAttributes(attr); 227 228 230 if (logger.isInfoEnabled()) 231 logger.info("[SMB] Add Share " + shr.toString() + " : isReadOnly"); 232 } 233 } 234 catch (InvalidDeviceInterfaceException ex) 235 { 236 237 239 if (logger.isInfoEnabled()) 240 logger.info("[SMB] Add Share " + shr.toString() + " : " + ex.toString()); 241 return; 242 } 243 catch (FileNotFoundException ex) 244 { 245 246 248 if (logger.isInfoEnabled()) 249 logger.info("[SMB] Add Share " + shr.toString() + " : " + ex.toString()); 250 return; 251 } 252 catch (IOException ex) 253 { 254 255 257 if (logger.isInfoEnabled()) 258 logger.info("[SMB] Add Share " + shr.toString() + " : " + ex.toString()); 259 return; 260 } 261 } 262 } 263 264 267 private void CommonConstructor() throws IOException 268 { 269 270 272 setVersion(ServerVersion); 273 274 276 m_sessionHandlers = new Vector <SessionSocketHandler>(); 277 278 280 m_sessions = new SrvSessionList(); 281 282 284 NetworkSettings.setDomain(getConfiguration().getDomainName()); 285 NetworkSettings.setBroadcastMask(getConfiguration().getBroadcastMask()); 286 } 287 288 291 protected void closeHostAnnouncers() 292 { 293 294 296 if (m_hostAnnouncers != null) 297 { 298 299 301 for (int i = 0; i < m_hostAnnouncers.size(); i++) 302 { 303 304 306 HostAnnouncer announcer = (HostAnnouncer) m_hostAnnouncers.elementAt(i); 307 308 310 announcer.shutdownAnnouncer(); 311 } 312 } 313 } 314 315 318 protected void closeSessionHandlers() 319 { 320 321 323 for (SessionSocketHandler handler : m_sessionHandlers) 324 { 325 326 328 handler.shutdownRequest(); 329 } 330 331 333 m_sessionHandlers.removeAllElements(); 334 } 335 336 342 public final synchronized SharedDevice deleteShare(String name) 343 { 344 return getConfiguration().getShares().deleteShare(name); 345 } 346 347 352 public final void deleteTemporaryShares(SMBSrvSession sess) 353 { 354 355 357 getConfiguration().getShareMapper().deleteShares(sess); 358 } 359 360 365 public final Enumeration enumerateShares() 366 { 367 return getConfiguration().getShares().enumerateShares(); 368 } 369 370 375 public final String getComment() 376 { 377 return getConfiguration().getComment(); 378 } 379 380 385 public final int getServerType() 386 { 387 return m_srvType; 388 } 389 390 393 public final int getSessionDebug() 394 { 395 return getConfiguration().getSessionDebugFlags(); 396 } 397 398 403 public final DialectSelector getSMBDialects() 404 { 405 return getConfiguration().getEnabledDialects(); 406 } 407 408 413 public final UserAccountList getUserAccountList() 414 { 415 return getConfiguration().getUserAccounts(); 416 } 417 418 423 public final SrvSessionList getSessions() 424 { 425 return m_sessions; 426 } 427 428 431 public void run() 432 { 433 434 436 setActive(true); 437 438 440 boolean isWindows = isWindowsNTOnwards(); 441 442 444 if (logger.isInfoEnabled()) 445 { 446 447 449 logger.info("[SMB] SMB Server " + getServerName() + " starting"); 450 logger.info("[SMB] Version " + isVersion()); 451 logger.info("[SMB] Java VM " + System.getProperty("java.vm.version")); 452 logger.info("[SMB] OS " + System.getProperty("os.name") + ", version " + System.getProperty("os.version")); 453 454 456 if (getAuthenticator() != null) 457 { 458 String mode = getAuthenticator().getAccessMode() == SrvAuthenticator.SHARE_MODE ? "SHARE" : "USER"; 459 logger.info("[SMB] Using authenticator " + getAuthenticator().getClass().getName() + ", mode=" + mode); 460 461 463 if (getUserAccountList() != null) 464 logger.info("[SMB] " + getUserAccountList().numberOfUsers() + " user accounts defined"); 465 else 466 logger.info("[SMB] No user accounts defined"); 467 } 468 469 471 if (getConfiguration().getTimeZone() != null) 472 logger.info("[SMB] Server timezone " + getConfiguration().getTimeZone() + ", offset from UTC = " 473 + getConfiguration().getTimeZoneOffset() / 60 + "hrs"); 474 else 475 logger.info("[SMB] Server timezone offset = " + getConfiguration().getTimeZoneOffset() / 60 + "hrs"); 476 477 479 logger.info("[SMB] Shares:"); 480 Enumeration <SharedDevice> enm = getFullShareList(getServerName(), null).enumerateShares(); 481 482 while (enm.hasMoreElements()) 483 { 484 SharedDevice share = enm.nextElement(); 485 logger.info("[SMB] " + share.toString() + " " + share.getContext().toString()); 486 } 487 } 488 489 491 try 492 { 493 494 496 AdminSharedDevice admShare = new AdminSharedDevice(); 497 addShare(admShare); 498 499 501 m_shutdown = false; 502 503 505 getServerIPAddresses(); 506 507 509 boolean sockDbg = false; 510 511 if ((getSessionDebug() & SMBSrvSession.DBG_SOCKET) != 0) 512 sockDbg = true; 513 514 516 if (getConfiguration().hasNetBIOSSMB()) 517 { 518 519 522 NetBIOSSessionSocketHandler.createSessionHandlers(this, sockDbg); 523 } 524 525 527 if (getConfiguration().hasTcpipSMB()) 528 { 529 530 532 TcpipSMBSessionSocketHandler.createSessionHandlers(this, sockDbg); 533 } 534 535 537 if (getConfiguration().hasWin32NetBIOS()) 538 { 539 540 542 if (isWindows == true) 543 { 544 545 547 Win32NetBIOSSessionSocketHandler.createSessionHandlers(this, sockDbg); 548 } 549 } 550 551 553 if (m_sessionHandlers.size() > 0 || getConfiguration().hasWin32NetBIOS()) 554 { 555 556 558 while (m_shutdown == false) 559 { 560 561 563 try 564 { 565 Thread.sleep(1000L); 566 } 567 catch (InterruptedException ex) 568 { 569 } 570 } 571 } 572 else if (logger.isInfoEnabled()) 573 { 574 575 577 logger.info("[SMB] No valid session handlers, server closing"); 578 } 579 } 580 catch (SMBException ex) 581 { 582 583 585 logger.error("SMB server error", ex); 586 587 589 setException(ex); 590 fireServerEvent(ServerListener.ServerError); 591 } 592 catch (Exception ex) 593 { 594 595 598 if (m_shutdown == false) 599 { 600 logger.error("[SMB] Server error : ", ex); 601 602 604 setException(ex); 605 fireServerEvent(ServerListener.ServerError); 606 } 607 } 608 609 611 if (logger.isInfoEnabled()) 612 logger.info("[SMB] SMB Server shutting down ..."); 613 614 616 closeHostAnnouncers(); 617 closeSessionHandlers(); 618 619 621 if (isWindows && Win32NetBIOSLanaMonitor.getLanaMonitor() != null) 622 Win32NetBIOSLanaMonitor.getLanaMonitor().shutdownRequest(); 623 624 626 setActive(false); 627 fireServerEvent(ServerListener.ServerShutdown); 628 } 629 630 635 protected final void sessionClosed(SMBSrvSession sess) 636 { 637 638 640 m_sessions.removeSession(sess); 641 642 644 fireSessionClosedEvent(sess); 645 } 646 647 652 protected final void sessionLoggedOn(SMBSrvSession sess) 653 { 654 655 657 fireSessionLoggedOnEvent(sess); 658 } 659 660 665 protected final void sessionOpened(SMBSrvSession sess) 666 { 667 668 670 fireSessionOpenEvent(sess); 671 } 672 673 678 public final void shutdownServer(boolean immediate) 679 { 680 681 683 m_shutdown = true; 684 685 try 686 { 687 688 690 closeSessionHandlers(); 691 } 692 catch (Exception ex) 693 { 694 } 695 696 698 Enumeration <Integer > enm = m_sessions.enumerate(); 699 700 while (enm.hasMoreElements()) 701 { 702 703 705 Integer sessId = enm.nextElement(); 706 SMBSrvSession sess = (SMBSrvSession) m_sessions.findSession(sessId); 707 708 710 fireSessionClosedEvent(sess); 711 712 714 sess.closeSession(); 715 } 716 717 719 if (m_srvThread != null) 720 { 721 722 try 723 { 724 m_srvThread.join(3000); 725 } 726 catch (Exception ex) 727 { 728 } 729 } 730 731 733 fireServerEvent(ServerListener.ServerShutdown); 734 } 735 736 739 public void startServer() 740 { 741 742 744 m_srvThread = new Thread (this); 745 m_srvThread.setName("SMB Server"); 746 m_srvThread.setDaemon(true); 747 748 m_srvThread.start(); 749 750 752 fireServerEvent(ServerListener.ServerStartup); 753 } 754 755 760 private final boolean isWindowsNTOnwards() 761 { 762 763 765 String osName = System.getProperty("os.name"); 766 767 if (osName.startsWith("Windows")) 768 { 769 if (osName.endsWith("95") || osName.endsWith("98") || osName.endsWith("ME")) 770 { 771 772 774 return false; 775 } 776 777 779 return true; 780 } 781 782 784 return false; 785 } 786 787 790 private final void getServerIPAddresses() 791 { 792 793 try 794 { 795 796 798 Enumeration <NetworkInterface > enm = NetworkInterface.getNetworkInterfaces(); 799 Vector <InetAddress > addrList = new Vector <InetAddress >(); 800 801 while (enm.hasMoreElements()) 802 { 803 804 806 NetworkInterface ni = enm.nextElement(); 807 808 810 Enumeration <InetAddress > addrs = ni.getInetAddresses(); 811 812 while (addrs.hasMoreElements()) 813 addrList.add(addrs.nextElement()); 814 } 815 816 818 if (addrList.size() > 0) 819 { 820 821 823 InetAddress [] inetAddrs = new InetAddress [addrList.size()]; 824 825 827 for (int i = 0; i < addrList.size(); i++) 828 inetAddrs[i] = (InetAddress ) addrList.elementAt(i); 829 830 832 setServerAddresses(inetAddrs); 833 } 834 } 835 catch (Exception ex) 836 { 837 838 840 logger.error("[SMB] Error getting local IP addresses", ex); 841 } 842 } 843 } | Popular Tags |