SMBServer: API: Added GetSessionsInformation method

This commit is contained in:
Tal Aloni 2017-03-04 15:06:56 +02:00
parent d742a22756
commit 15f09eb751
9 changed files with 169 additions and 24 deletions

View file

@ -195,6 +195,7 @@
<Compile Include="Server\Enums\SMBDialect.cs" />
<Compile Include="Server\Helpers\ServerPathUtils.cs" />
<Compile Include="Server\NameServer.cs" />
<Compile Include="Server\SessionInformation.cs" />
<Compile Include="Server\Shares\FileSystemShare.cs" />
<Compile Include="Server\Shares\ISMBShare.cs" />
<Compile Include="Server\Shares\NamedPipeShare.cs" />

View file

@ -43,5 +43,19 @@ namespace SMBLibrary.Server
connection.CloseSessions();
RemoveConnection(connection);
}
public List<SessionInformation> GetSessionsInformation()
{
List<SessionInformation> result = new List<SessionInformation>();
lock (m_activeConnections)
{
foreach (ConnectionState connection in m_activeConnections)
{
List<SessionInformation> sessions = connection.GetSessionsInformation();
result.AddRange(sessions);
}
}
return result;
}
}
}

View file

@ -50,6 +50,11 @@ namespace SMBLibrary.Server
{
}
public virtual List<SessionInformation> GetSessionsInformation()
{
return new List<SessionInformation>();
}
public void LogToServer(Severity severity, string message)
{
message = String.Format("[{0}] {1}", ConnectionIdentifier, message);

View file

@ -55,7 +55,10 @@ namespace SMBLibrary.Server
public SMB1Session CreateSession(ushort userID, string userName, string machineName, byte[] sessionKey, object accessToken)
{
SMB1Session session = new SMB1Session(this, userID, userName, machineName, sessionKey, accessToken);
m_sessions.Add(userID, session);
lock (m_sessions)
{
m_sessions.Add(userID, session);
}
return session;
}
@ -84,20 +87,39 @@ namespace SMBLibrary.Server
if (session != null)
{
session.Close();
m_sessions.Remove(userID);
lock (m_sessions)
{
m_sessions.Remove(userID);
}
}
}
public override void CloseSessions()
{
foreach (SMB1Session session in m_sessions.Values)
lock (m_sessions)
{
session.Close();
foreach (SMB1Session session in m_sessions.Values)
{
session.Close();
}
}
m_sessions.Clear();
}
public override List<SessionInformation> GetSessionsInformation()
{
List<SessionInformation> result = new List<SessionInformation>();
lock (m_sessions)
{
foreach (SMB1Session session in m_sessions.Values)
{
result.Add(new SessionInformation(this.ClientEndPoint, this.Dialect, session.UserName, session.MachineName, session.ListOpenFiles(), session.CreationDT));
}
}
return result;
}
/// <summary>
/// An open TID MUST be unique within an SMB connection.
/// The value 0xFFFF MUST NOT be used as a valid TID. All other possible values for TID, including zero (0x0000), are valid.

View file

@ -63,14 +63,17 @@ namespace SMBLibrary.Server
m_connectedTrees.TryGetValue(treeID, out share);
if (share != null)
{
List<ushort> fileIDList = new List<ushort>(m_openFiles.Keys);
foreach (ushort fileID in fileIDList)
lock (m_openFiles)
{
OpenFileObject openFile = m_openFiles[fileID];
if (openFile.TreeID == treeID)
List<ushort> fileIDList = new List<ushort>(m_openFiles.Keys);
foreach (ushort fileID in fileIDList)
{
share.FileStore.CloseFile(openFile.Handle);
m_openFiles.Remove(fileID);
OpenFileObject openFile = m_openFiles[fileID];
if (openFile.TreeID == treeID)
{
share.FileStore.CloseFile(openFile.Handle);
m_openFiles.Remove(fileID);
}
}
}
m_connectedTrees.Remove(treeID);
@ -94,7 +97,10 @@ namespace SMBLibrary.Server
ushort? fileID = m_connection.AllocateFileID();
if (fileID.HasValue)
{
m_openFiles.Add(fileID.Value, new OpenFileObject(treeID, relativePath, handle));
lock (m_openFiles)
{
m_openFiles.Add(fileID.Value, new OpenFileObject(treeID, relativePath, handle));
}
}
return fileID;
}
@ -108,7 +114,23 @@ namespace SMBLibrary.Server
public void RemoveOpenFile(ushort fileID)
{
m_openFiles.Remove(fileID);
lock (m_openFiles)
{
m_openFiles.Remove(fileID);
}
}
public List<string> ListOpenFiles()
{
List<string> result = new List<string>();
lock (m_openFiles)
{
foreach (OpenFileObject openFile in m_openFiles.Values)
{
result.Add(openFile.Path);
}
}
return result;
}
private ushort? AllocateSearchHandle()

View file

@ -47,7 +47,10 @@ namespace SMBLibrary.Server
public SMB2Session CreateSession(ulong sessionID, string userName, string machineName, byte[] sessionKey, object accessToken)
{
SMB2Session session = new SMB2Session(this, sessionID, userName, machineName, sessionKey, accessToken);
m_sessions.Add(sessionID, session);
lock (m_sessions)
{
m_sessions.Add(sessionID, session);
}
return session;
}
@ -65,18 +68,37 @@ namespace SMBLibrary.Server
if (session != null)
{
session.Close();
m_sessions.Remove(sessionID);
lock (m_sessions)
{
m_sessions.Remove(sessionID);
}
}
}
public override void CloseSessions()
{
foreach (SMB2Session session in m_sessions.Values)
lock (m_sessions)
{
session.Close();
foreach (SMB2Session session in m_sessions.Values)
{
session.Close();
}
}
m_sessions.Clear();
}
public override List<SessionInformation> GetSessionsInformation()
{
List<SessionInformation> result = new List<SessionInformation>();
lock (m_sessions)
{
foreach (SMB2Session session in m_sessions.Values)
{
result.Add(new SessionInformation(this.ClientEndPoint, this.Dialect, session.UserName, session.MachineName, session.ListOpenFiles(), session.CreationDT));
}
}
return result;
}
}
}

View file

@ -85,14 +85,17 @@ namespace SMBLibrary.Server
m_connectedTrees.TryGetValue(treeID, out share);
if (share != null)
{
List<ulong> fileIDList = new List<ulong>(m_openFiles.Keys);
foreach (ushort fileID in fileIDList)
lock (m_openFiles)
{
OpenFileObject openFile = m_openFiles[fileID];
if (openFile.TreeID == treeID)
List<ulong> fileIDList = new List<ulong>(m_openFiles.Keys);
foreach (ushort fileID in fileIDList)
{
share.FileStore.CloseFile(openFile.Handle);
m_openFiles.Remove(fileID);
OpenFileObject openFile = m_openFiles[fileID];
if (openFile.TreeID == treeID)
{
share.FileStore.CloseFile(openFile.Handle);
m_openFiles.Remove(fileID);
}
}
}
m_connectedTrees.Remove(treeID);
@ -110,7 +113,10 @@ namespace SMBLibrary.Server
ulong? persistentID = m_connection.AllocatePersistentFileID();
if (persistentID.HasValue)
{
m_openFiles.Add(persistentID.Value, new OpenFileObject(treeID, relativePath, handle));
lock (m_openFiles)
{
m_openFiles.Add(persistentID.Value, new OpenFileObject(treeID, relativePath, handle));
}
}
return persistentID;
}
@ -129,10 +135,26 @@ namespace SMBLibrary.Server
public void RemoveOpenFile(ulong fileID)
{
m_openFiles.Remove(fileID);
lock (m_openFiles)
{
m_openFiles.Remove(fileID);
}
m_openSearches.Remove(fileID);
}
public List<string> ListOpenFiles()
{
List<string> result = new List<string>();
lock (m_openFiles)
{
foreach (OpenFileObject openFile in m_openFiles.Values)
{
result.Add(openFile.Path);
}
}
return result;
}
public OpenSearch AddOpenSearch(ulong fileID, List<QueryDirectoryFileInformation> entries, int enumerationLocation)
{
OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);

View file

@ -360,6 +360,11 @@ namespace SMBLibrary.Server
}
}
public List<SessionInformation> GetSessionsInformation()
{
return m_connectionManager.GetSessionsInformation();
}
private void Log(Severity severity, string message)
{
// To be thread-safe we must capture the delegate reference first

View file

@ -0,0 +1,32 @@
/* 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;
using System.Collections.Generic;
using System.Net;
namespace SMBLibrary.Server
{
public class SessionInformation
{
public IPEndPoint ClientEndPoint;
public SMBDialect Dialect;
public string UserName;
public string MachineName;
public List<string> OpenFiles;
public DateTime CreationDT;
public SessionInformation(IPEndPoint clientEndPoint, SMBDialect dialect, string userName, string machineName, List<string> openFiles, DateTime creationDT)
{
ClientEndPoint = clientEndPoint;
Dialect = dialect;
UserName = userName;
MachineName = machineName;
OpenFiles = openFiles;
CreationDT = creationDT;
}
}
}