Browse Source

TreeConnectHelper: Code refactoring and logging improvements

Tal Aloni 8 years ago
parent
commit
9cd6180e57

+ 4 - 7
SMBLibrary/Server/SMB1/TreeConnectHelper.cs

@@ -41,6 +41,7 @@ namespace SMBLibrary.Server.SMB1
 
                 if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
                 {
+                    state.LogToServer(Severity.Verbose, "Tree Connect to '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
                     header.Status = NTStatus.STATUS_ACCESS_DENIED;
                     return new ErrorResponse(request.CommandName);
                 }
@@ -51,6 +52,7 @@ namespace SMBLibrary.Server.SMB1
                 header.Status = NTStatus.STATUS_INSUFF_SERVER_RESOURCES;
                 return new ErrorResponse(request.CommandName);
             }
+            state.LogToServer(Severity.Information, "Tree Connect: User '{0}' connected to '{1}'", session.UserName, share.Name);
             header.TID = treeID.Value;
             if (isExtended)
             {
@@ -89,16 +91,11 @@ namespace SMBLibrary.Server.SMB1
             return response;
         }
 
-        internal static SMB1Command GetTreeDisconnectResponse(SMB1Header header, TreeDisconnectRequest request, SMB1ConnectionState state)
+        internal static SMB1Command GetTreeDisconnectResponse(SMB1Header header, TreeDisconnectRequest request, ISMBShare share, SMB1ConnectionState state)
         {
             SMB1Session session = state.GetSession(header.UID);
-            if (!session.IsTreeConnected(header.TID))
-            {
-                header.Status = NTStatus.STATUS_SMB_BAD_TID;
-                return new ErrorResponse(request.CommandName);
-            }
-
             session.DisconnectTree(header.TID);
+            state.LogToServer(Severity.Information, "Tree Disconnect: User '{0}' disconnected from '{1}'", session.UserName, share.Name);
             return new TreeDisconnectResponse();
         }
     }

+ 10 - 0
SMBLibrary/Server/SMB2/TreeConnectHelper.cs

@@ -40,6 +40,7 @@ namespace SMBLibrary.Server.SMB2
 
                 if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
                 {
+                    state.LogToServer(Severity.Verbose, "Tree Connect to '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
                     return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
                 }
             }
@@ -49,6 +50,7 @@ namespace SMBLibrary.Server.SMB2
             {
                 return new ErrorResponse(request.CommandName, NTStatus.STATUS_INSUFF_SERVER_RESOURCES);
             }
+            state.LogToServer(Severity.Information, "Tree Connect: User '{0}' connected to '{1}'", session.UserName, share.Name);
             response.Header.TreeID = treeID.Value;
             response.ShareType = shareType;
             response.ShareFlags = shareFlags;
@@ -59,5 +61,13 @@ namespace SMBLibrary.Server.SMB2
                                           FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
             return response;
         }
+
+        internal static SMB2Command GetTreeDisconnectResponse(TreeDisconnectRequest request, ISMBShare share, SMB2ConnectionState state)
+        {
+            SMB2Session session = state.GetSession(request.Header.SessionID);
+            session.DisconnectTree(request.Header.TreeID);
+            state.LogToServer(Severity.Information, "Tree Disconnect: User '{0}' disconnected from '{1}'", session.UserName, share.Name);
+            return new TreeDisconnectResponse();
+        }
     }
 }

+ 1 - 1
SMBLibrary/Server/SMBServer.SMB1.cs

@@ -252,7 +252,7 @@ namespace SMBLibrary.Server
                     else if (command is TreeDisconnectRequest)
                     {
                         TreeDisconnectRequest request = (TreeDisconnectRequest)command;
-                        return TreeConnectHelper.GetTreeDisconnectResponse(header, request, state);
+                        return TreeConnectHelper.GetTreeDisconnectResponse(header, request, share, state);
                     }
                     else if (command is TransactionRequest) // Both TransactionRequest and Transaction2Request
                     {

+ 1 - 2
SMBLibrary/Server/SMBServer.SMB2.cs

@@ -136,8 +136,7 @@ namespace SMBLibrary.Server
 
                     if (command is TreeDisconnectRequest)
                     {
-                        session.DisconnectTree(command.Header.TreeID);
-                        return new TreeDisconnectResponse();
+                        return TreeConnectHelper.GetTreeDisconnectResponse((TreeDisconnectRequest)command, share, state);
                     }
                     else if (command is CreateRequest)
                     {