Browse Source

Added ISMBShare interface, moved share implementations to Server\Shares

Tal Aloni 8 years ago
parent
commit
f340094946

+ 4 - 3
SMBLibrary/SMBLibrary.csproj

@@ -105,10 +105,8 @@
     <Compile Include="Server\Exceptions\EmptyPasswordNotAllowedException.cs" />
     <Compile Include="Server\Exceptions\InvalidRequestException.cs" />
     <Compile Include="Server\Exceptions\UnsupportedInformationLevelException.cs" />
-    <Compile Include="Server\FileSystemShare.cs" />
     <Compile Include="Server\IndependentUserCollection.cs" />
     <Compile Include="Server\INTLMAuthenticationProvider.cs" />
-    <Compile Include="Server\NamedPipeShare.cs" />
     <Compile Include="Server\NameServer.cs" />
     <Compile Include="Server\OpenedFileObject.cs" />
     <Compile Include="Server\ProcessStateObject.cs" />
@@ -126,7 +124,10 @@
     <Compile Include="Server\ResponseHelpers\TransactionHelper.cs" />
     <Compile Include="Server\ResponseHelpers\TransactionSubcommandHelper.cs" />
     <Compile Include="Server\ResponseHelpers\TreeConnectHelper.cs" />
-    <Compile Include="Server\ShareCollection.cs" />
+    <Compile Include="Server\Shares\FileSystemShare.cs" />
+    <Compile Include="Server\Shares\ISMBShare.cs" />
+    <Compile Include="Server\Shares\NamedPipeShare.cs" />
+    <Compile Include="Server\Shares\ShareCollection.cs" />
     <Compile Include="Server\SMBConnectionReceiveBuffer.cs" />
     <Compile Include="Server\SMBServer.cs" />
     <Compile Include="Server\StateObject.cs" />

+ 2 - 2
SMBLibrary/Server/ResponseHelpers/NTCreateHelper.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -16,7 +16,7 @@ namespace SMBLibrary.Server
 {
     public class NTCreateHelper
     {
-        internal static SMBCommand GetNTCreateResponse(SMBHeader header, NTCreateAndXRequest request, object share, StateObject state)
+        internal static SMBCommand GetNTCreateResponse(SMBHeader header, NTCreateAndXRequest request, ISMBShare share, StateObject state)
         {
             bool isExtended = (request.Flags & NTCreateFlags.NT_CREATE_REQUEST_EXTENDED_RESPONSE) > 0;
             string path = request.FileName;

+ 3 - 3
SMBLibrary/Server/ResponseHelpers/NTTransactHelper.cs

@@ -17,7 +17,7 @@ namespace SMBLibrary.Server
         /// <summary>
         /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
         /// </summary>
-        internal static SMBCommand GetNTTransactResponse(SMBHeader header, NTTransactRequest request, object share, StateObject state, List<SMBCommand> sendQueue)
+        internal static SMBCommand GetNTTransactResponse(SMBHeader header, NTTransactRequest request, ISMBShare share, StateObject state, List<SMBCommand> sendQueue)
         {
             if (request.TransParameters.Length < request.TotalParameterCount ||
                 request.TransData.Length < request.TotalDataCount)
@@ -45,7 +45,7 @@ namespace SMBLibrary.Server
         /// There are no secondary response messages.
         /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
         /// </summary>
-        internal static SMBCommand GetNTTransactResponse(SMBHeader header, NTTransactSecondaryRequest request, object share, StateObject state, List<SMBCommand> sendQueue)
+        internal static SMBCommand GetNTTransactResponse(SMBHeader header, NTTransactSecondaryRequest request, ISMBShare share, StateObject state, List<SMBCommand> sendQueue)
         {
             ProcessStateObject processState = state.GetProcessState(header.PID);
             if (processState == null)
@@ -69,7 +69,7 @@ namespace SMBLibrary.Server
             }
         }
 
-        internal static SMBCommand GetCompleteNTTransactResponse(SMBHeader header, NTTransactSubcommandName subcommandName, byte[] requestSetup, byte[] requestParameters, byte[] requestData, object share, StateObject state, List<SMBCommand> sendQueue)
+        internal static SMBCommand GetCompleteNTTransactResponse(SMBHeader header, NTTransactSubcommandName subcommandName, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, StateObject state, List<SMBCommand> sendQueue)
         {
             NTTransactSubcommand subcommand = NTTransactSubcommand.GetSubcommandRequest(subcommandName, requestSetup, requestParameters, requestData, header.UnicodeFlag);
             NTTransactSubcommand subcommandResponse = null;

+ 2 - 2
SMBLibrary/Server/ResponseHelpers/OpenAndXHelper.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -16,7 +16,7 @@ namespace SMBLibrary.Server
 {
     public class OpenAndXHelper
     {
-        internal static SMBCommand GetOpenAndXResponse(SMBHeader header, OpenAndXRequest request, object share, StateObject state)
+        internal static SMBCommand GetOpenAndXResponse(SMBHeader header, OpenAndXRequest request, ISMBShare share, StateObject state)
         {
             bool isExtended = (request.Flags & OpenFlags.SMB_OPEN_EXTENDED_RESPONSE) > 0;
             string path = request.FileName;

+ 8 - 8
SMBLibrary/Server/ResponseHelpers/ReadWriteResponseHelper.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -17,7 +17,7 @@ namespace SMBLibrary.Server
 {
     public class ReadWriteResponseHelper
     {
-        internal static SMBCommand GetReadResponse(SMBHeader header, ReadRequest request, object share, StateObject state)
+        internal static SMBCommand GetReadResponse(SMBHeader header, ReadRequest request, ISMBShare share, StateObject state)
         {
             byte[] data = PerformRead(header, share, request.FID, request.ReadOffsetInBytes, request.CountOfBytesToRead, state);
             if (header.Status != NTStatus.STATUS_SUCCESS)
@@ -31,7 +31,7 @@ namespace SMBLibrary.Server
             return response;
         }
 
-        internal static SMBCommand GetReadResponse(SMBHeader header, ReadAndXRequest request, object share, StateObject state)
+        internal static SMBCommand GetReadResponse(SMBHeader header, ReadAndXRequest request, ISMBShare share, StateObject state)
         {
             uint maxCount = request.MaxCount;
             if ((share is FileSystemShare) && state.LargeRead)
@@ -54,7 +54,7 @@ namespace SMBLibrary.Server
             return response;
         }
 
-        public static byte[] PerformRead(SMBHeader header, object share, ushort FID, ulong offset, uint maxCount, StateObject state)
+        public static byte[] PerformRead(SMBHeader header, ISMBShare share, ushort FID, ulong offset, uint maxCount, StateObject state)
         {
             if (offset > Int64.MaxValue || maxCount > Int32.MaxValue)
             {
@@ -63,7 +63,7 @@ namespace SMBLibrary.Server
             return PerformRead(header, share, FID, (long)offset, (int)maxCount, state);
         }
 
-        public static byte[] PerformRead(SMBHeader header, object share, ushort FID, long offset, int maxCount, StateObject state)
+        public static byte[] PerformRead(SMBHeader header, ISMBShare share, ushort FID, long offset, int maxCount, StateObject state)
         {
             OpenedFileObject openedFile = state.GetOpenedFileObject(FID);
             if (openedFile == null)
@@ -134,7 +134,7 @@ namespace SMBLibrary.Server
             }
         }
 
-        internal static SMBCommand GetWriteResponse(SMBHeader header, WriteRequest request, object share, StateObject state)
+        internal static SMBCommand GetWriteResponse(SMBHeader header, WriteRequest request, ISMBShare share, StateObject state)
         {
             ushort bytesWritten = (ushort)PerformWrite(header, share, request.FID, request.WriteOffsetInBytes, request.Data, state);
             if (header.Status != NTStatus.STATUS_SUCCESS)
@@ -147,7 +147,7 @@ namespace SMBLibrary.Server
             return response;
         }
 
-        internal static SMBCommand GetWriteResponse(SMBHeader header, WriteAndXRequest request, object share, StateObject state)
+        internal static SMBCommand GetWriteResponse(SMBHeader header, WriteAndXRequest request, ISMBShare share, StateObject state)
         {
             uint bytesWritten = PerformWrite(header, share, request.FID, request.Offset, request.Data, state);
             if (header.Status != NTStatus.STATUS_SUCCESS)
@@ -165,7 +165,7 @@ namespace SMBLibrary.Server
             return response;
         }
 
-        public static uint PerformWrite(SMBHeader header, object share, ushort FID, ulong offset, byte[] data, StateObject state)
+        public static uint PerformWrite(SMBHeader header, ISMBShare share, ushort FID, ulong offset, byte[] data, StateObject state)
         {
             OpenedFileObject openedFile = state.GetOpenedFileObject(FID);
             if (openedFile == null)

+ 1 - 1
SMBLibrary/Server/ResponseHelpers/ServerResponseHelper.cs

@@ -15,7 +15,7 @@ namespace SMBLibrary.Server
 {
     public partial class ServerResponseHelper
     {
-        internal static SMBCommand GetCloseResponse(SMBHeader header, CloseRequest request, object share, StateObject state)
+        internal static SMBCommand GetCloseResponse(SMBHeader header, CloseRequest request, ISMBShare share, StateObject state)
         {
             OpenedFileObject openedFile = state.GetOpenedFileObject(request.FID);
             if (openedFile == null)

+ 5 - 5
SMBLibrary/Server/ResponseHelpers/TransactionHelper.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -21,7 +21,7 @@ namespace SMBLibrary.Server
         /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
         /// The server MUST respond to the transaction request as a whole.
         /// </summary>
-        internal static SMBCommand GetTransactionResponse(SMBHeader header, TransactionRequest request, object share, StateObject state, List<SMBCommand> sendQueue)
+        internal static SMBCommand GetTransactionResponse(SMBHeader header, TransactionRequest request, ISMBShare share, StateObject state, List<SMBCommand> sendQueue)
         {
             ProcessStateObject processState = state.ObtainProcessState(header.PID);
             processState.MaxDataCount = request.MaxDataCount;
@@ -58,7 +58,7 @@ namespace SMBLibrary.Server
         /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
         /// The server MUST respond to the transaction request as a whole.
         /// </summary>
-        internal static SMBCommand GetTransactionResponse(SMBHeader header, TransactionSecondaryRequest request, object share, StateObject state, List<SMBCommand> sendQueue)
+        internal static SMBCommand GetTransactionResponse(SMBHeader header, TransactionSecondaryRequest request, ISMBShare share, StateObject state, List<SMBCommand> sendQueue)
         {
             ProcessStateObject processState = state.GetProcessState(header.PID);
             if (processState == null)
@@ -89,7 +89,7 @@ namespace SMBLibrary.Server
             }
         }
 
-        internal static SMBCommand GetCompleteTransactionResponse(SMBHeader header, byte[] requestSetup, byte[] requestParameters, byte[] requestData, object share, StateObject state, List<SMBCommand> sendQueue)
+        internal static SMBCommand GetCompleteTransactionResponse(SMBHeader header, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, StateObject state, List<SMBCommand> sendQueue)
         {
             TransactionSubcommand subcommand = TransactionSubcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
             TransactionSubcommand subcommandResponse = null;
@@ -162,7 +162,7 @@ namespace SMBLibrary.Server
             return response;
         }
 
-        internal static SMBCommand GetCompleteTransaction2Response(SMBHeader header, byte[] requestSetup, byte[] requestParameters, byte[] requestData, object share, StateObject state, List<SMBCommand> sendQueue)
+        internal static SMBCommand GetCompleteTransaction2Response(SMBHeader header, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, StateObject state, List<SMBCommand> sendQueue)
         {
             Transaction2Subcommand subcommand = Transaction2Subcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
             Transaction2Subcommand subcommandResponse = null;

+ 1 - 1
SMBLibrary/Server/SMBServer.cs

@@ -313,7 +313,7 @@ namespace SMBLibrary.Server
                 else if (state.IsTreeConnected(header.TID))
                 {
                     string rootPath = state.GetConnectedTreePath(header.TID);
-                    object share;
+                    ISMBShare share;
                     if (state.IsIPC(header.TID))
                     {
                         share = m_services;

+ 16 - 3
SMBLibrary/Server/FileSystemShare.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -11,13 +11,18 @@ using Utilities;
 
 namespace SMBLibrary.Server
 {
-    public class FileSystemShare
+    public class FileSystemShare : ISMBShare
     {
-        public string Name;
+        private string m_name;
         public List<string> ReadAccess;
         public List<string> WriteAccess;
         public IFileSystem FileSystem;
 
+        public FileSystemShare(string shareName)
+        {
+            m_name = shareName;
+        }
+
         public bool HasReadAccess(string userName)
         {
             return Contains(ReadAccess, userName);
@@ -44,5 +49,13 @@ namespace SMBLibrary.Server
             }
             return -1;
         }
+
+        public string Name
+        {
+            get
+            {
+                return m_name;
+            }
+        }
     }
 }

+ 18 - 0
SMBLibrary/Server/Shares/ISMBShare.cs

@@ -0,0 +1,18 @@
+/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+ * 
+ * You can redistribute this program and/or modify it under the terms of
+ * the GNU Lesser Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ */
+using System;
+
+namespace SMBLibrary.Server
+{
+    public interface ISMBShare
+    {
+        string Name
+        {
+            get;
+        }
+    }
+}

+ 13 - 2
SMBLibrary/Server/NamedPipeShare.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -12,8 +12,11 @@ using SMBLibrary.Services;
 
 namespace SMBLibrary.Server
 {
-    public class NamedPipeShare : List<RemoteService>
+    public class NamedPipeShare : List<RemoteService>, ISMBShare
     {
+        // A pipe share, as defined by the SMB Protocol, MUST always have the name "IPC$".
+        public const string NamedPipeShareName = "IPC$";
+
         public NamedPipeShare(List<string> shareList)
         {
             this.Add(new ServerService(Environment.MachineName, shareList));
@@ -31,5 +34,13 @@ namespace SMBLibrary.Server
             }
             return null;
         }
+
+        public string Name
+        {
+            get
+            {
+                return NamedPipeShareName;
+            }
+        }
     }
 }

+ 2 - 3
SMBLibrary/Server/ShareCollection.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -15,8 +15,7 @@ namespace SMBLibrary.Server
     {
         public void Add(string shareName, List<string> readAccess, List<string> writeAccess, IFileSystem fileSystem)
         {
-            FileSystemShare share = new FileSystemShare();
-            share.Name = shareName;
+            FileSystemShare share = new FileSystemShare(shareName);
             share.ReadAccess = readAccess;
             share.WriteAccess = writeAccess;
             share.FileSystem = fileSystem;