mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-04-30 02:37:49 +02:00
SMBServer: API: Added GetSessionsInformation method
This commit is contained in:
parent
d742a22756
commit
15f09eb751
9 changed files with 169 additions and 24 deletions
|
@ -195,6 +195,7 @@
|
||||||
<Compile Include="Server\Enums\SMBDialect.cs" />
|
<Compile Include="Server\Enums\SMBDialect.cs" />
|
||||||
<Compile Include="Server\Helpers\ServerPathUtils.cs" />
|
<Compile Include="Server\Helpers\ServerPathUtils.cs" />
|
||||||
<Compile Include="Server\NameServer.cs" />
|
<Compile Include="Server\NameServer.cs" />
|
||||||
|
<Compile Include="Server\SessionInformation.cs" />
|
||||||
<Compile Include="Server\Shares\FileSystemShare.cs" />
|
<Compile Include="Server\Shares\FileSystemShare.cs" />
|
||||||
<Compile Include="Server\Shares\ISMBShare.cs" />
|
<Compile Include="Server\Shares\ISMBShare.cs" />
|
||||||
<Compile Include="Server\Shares\NamedPipeShare.cs" />
|
<Compile Include="Server\Shares\NamedPipeShare.cs" />
|
||||||
|
|
|
@ -43,5 +43,19 @@ namespace SMBLibrary.Server
|
||||||
connection.CloseSessions();
|
connection.CloseSessions();
|
||||||
RemoveConnection(connection);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,11 @@ namespace SMBLibrary.Server
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual List<SessionInformation> GetSessionsInformation()
|
||||||
|
{
|
||||||
|
return new List<SessionInformation>();
|
||||||
|
}
|
||||||
|
|
||||||
public void LogToServer(Severity severity, string message)
|
public void LogToServer(Severity severity, string message)
|
||||||
{
|
{
|
||||||
message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
|
message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
|
||||||
|
|
|
@ -55,7 +55,10 @@ namespace SMBLibrary.Server
|
||||||
public SMB1Session CreateSession(ushort userID, string userName, string machineName, byte[] sessionKey, object accessToken)
|
public SMB1Session CreateSession(ushort userID, string userName, string machineName, byte[] sessionKey, object accessToken)
|
||||||
{
|
{
|
||||||
SMB1Session session = new SMB1Session(this, userID, userName, machineName, sessionKey, accessToken);
|
SMB1Session session = new SMB1Session(this, userID, userName, machineName, sessionKey, accessToken);
|
||||||
|
lock (m_sessions)
|
||||||
|
{
|
||||||
m_sessions.Add(userID, session);
|
m_sessions.Add(userID, session);
|
||||||
|
}
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,20 +87,39 @@ namespace SMBLibrary.Server
|
||||||
if (session != null)
|
if (session != null)
|
||||||
{
|
{
|
||||||
session.Close();
|
session.Close();
|
||||||
|
lock (m_sessions)
|
||||||
|
{
|
||||||
m_sessions.Remove(userID);
|
m_sessions.Remove(userID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override void CloseSessions()
|
public override void CloseSessions()
|
||||||
|
{
|
||||||
|
lock (m_sessions)
|
||||||
{
|
{
|
||||||
foreach (SMB1Session session in m_sessions.Values)
|
foreach (SMB1Session session in m_sessions.Values)
|
||||||
{
|
{
|
||||||
session.Close();
|
session.Close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_sessions.Clear();
|
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>
|
/// <summary>
|
||||||
/// An open TID MUST be unique within an SMB connection.
|
/// 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.
|
/// The value 0xFFFF MUST NOT be used as a valid TID. All other possible values for TID, including zero (0x0000), are valid.
|
||||||
|
|
|
@ -62,6 +62,8 @@ namespace SMBLibrary.Server
|
||||||
ISMBShare share;
|
ISMBShare share;
|
||||||
m_connectedTrees.TryGetValue(treeID, out share);
|
m_connectedTrees.TryGetValue(treeID, out share);
|
||||||
if (share != null)
|
if (share != null)
|
||||||
|
{
|
||||||
|
lock (m_openFiles)
|
||||||
{
|
{
|
||||||
List<ushort> fileIDList = new List<ushort>(m_openFiles.Keys);
|
List<ushort> fileIDList = new List<ushort>(m_openFiles.Keys);
|
||||||
foreach (ushort fileID in fileIDList)
|
foreach (ushort fileID in fileIDList)
|
||||||
|
@ -73,6 +75,7 @@ namespace SMBLibrary.Server
|
||||||
m_openFiles.Remove(fileID);
|
m_openFiles.Remove(fileID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
m_connectedTrees.Remove(treeID);
|
m_connectedTrees.Remove(treeID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,9 +96,12 @@ namespace SMBLibrary.Server
|
||||||
{
|
{
|
||||||
ushort? fileID = m_connection.AllocateFileID();
|
ushort? fileID = m_connection.AllocateFileID();
|
||||||
if (fileID.HasValue)
|
if (fileID.HasValue)
|
||||||
|
{
|
||||||
|
lock (m_openFiles)
|
||||||
{
|
{
|
||||||
m_openFiles.Add(fileID.Value, new OpenFileObject(treeID, relativePath, handle));
|
m_openFiles.Add(fileID.Value, new OpenFileObject(treeID, relativePath, handle));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return fileID;
|
return fileID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,9 +113,25 @@ namespace SMBLibrary.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveOpenFile(ushort fileID)
|
public void RemoveOpenFile(ushort fileID)
|
||||||
|
{
|
||||||
|
lock (m_openFiles)
|
||||||
{
|
{
|
||||||
m_openFiles.Remove(fileID);
|
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()
|
private ushort? AllocateSearchHandle()
|
||||||
{
|
{
|
||||||
|
|
|
@ -47,7 +47,10 @@ namespace SMBLibrary.Server
|
||||||
public SMB2Session CreateSession(ulong sessionID, string userName, string machineName, byte[] sessionKey, object accessToken)
|
public SMB2Session CreateSession(ulong sessionID, string userName, string machineName, byte[] sessionKey, object accessToken)
|
||||||
{
|
{
|
||||||
SMB2Session session = new SMB2Session(this, sessionID, userName, machineName, sessionKey, accessToken);
|
SMB2Session session = new SMB2Session(this, sessionID, userName, machineName, sessionKey, accessToken);
|
||||||
|
lock (m_sessions)
|
||||||
|
{
|
||||||
m_sessions.Add(sessionID, session);
|
m_sessions.Add(sessionID, session);
|
||||||
|
}
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,18 +68,37 @@ namespace SMBLibrary.Server
|
||||||
if (session != null)
|
if (session != null)
|
||||||
{
|
{
|
||||||
session.Close();
|
session.Close();
|
||||||
|
lock (m_sessions)
|
||||||
|
{
|
||||||
m_sessions.Remove(sessionID);
|
m_sessions.Remove(sessionID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override void CloseSessions()
|
public override void CloseSessions()
|
||||||
|
{
|
||||||
|
lock (m_sessions)
|
||||||
{
|
{
|
||||||
foreach (SMB2Session session in m_sessions.Values)
|
foreach (SMB2Session session in m_sessions.Values)
|
||||||
{
|
{
|
||||||
session.Close();
|
session.Close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_sessions.Clear();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,6 +84,8 @@ namespace SMBLibrary.Server
|
||||||
ISMBShare share;
|
ISMBShare share;
|
||||||
m_connectedTrees.TryGetValue(treeID, out share);
|
m_connectedTrees.TryGetValue(treeID, out share);
|
||||||
if (share != null)
|
if (share != null)
|
||||||
|
{
|
||||||
|
lock (m_openFiles)
|
||||||
{
|
{
|
||||||
List<ulong> fileIDList = new List<ulong>(m_openFiles.Keys);
|
List<ulong> fileIDList = new List<ulong>(m_openFiles.Keys);
|
||||||
foreach (ushort fileID in fileIDList)
|
foreach (ushort fileID in fileIDList)
|
||||||
|
@ -95,6 +97,7 @@ namespace SMBLibrary.Server
|
||||||
m_openFiles.Remove(fileID);
|
m_openFiles.Remove(fileID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
m_connectedTrees.Remove(treeID);
|
m_connectedTrees.Remove(treeID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,9 +112,12 @@ namespace SMBLibrary.Server
|
||||||
{
|
{
|
||||||
ulong? persistentID = m_connection.AllocatePersistentFileID();
|
ulong? persistentID = m_connection.AllocatePersistentFileID();
|
||||||
if (persistentID.HasValue)
|
if (persistentID.HasValue)
|
||||||
|
{
|
||||||
|
lock (m_openFiles)
|
||||||
{
|
{
|
||||||
m_openFiles.Add(persistentID.Value, new OpenFileObject(treeID, relativePath, handle));
|
m_openFiles.Add(persistentID.Value, new OpenFileObject(treeID, relativePath, handle));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return persistentID;
|
return persistentID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,11 +134,27 @@ namespace SMBLibrary.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveOpenFile(ulong fileID)
|
public void RemoveOpenFile(ulong fileID)
|
||||||
|
{
|
||||||
|
lock (m_openFiles)
|
||||||
{
|
{
|
||||||
m_openFiles.Remove(fileID);
|
m_openFiles.Remove(fileID);
|
||||||
|
}
|
||||||
m_openSearches.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)
|
public OpenSearch AddOpenSearch(ulong fileID, List<QueryDirectoryFileInformation> entries, int enumerationLocation)
|
||||||
{
|
{
|
||||||
OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
|
OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
|
||||||
|
|
|
@ -360,6 +360,11 @@ namespace SMBLibrary.Server
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<SessionInformation> GetSessionsInformation()
|
||||||
|
{
|
||||||
|
return m_connectionManager.GetSessionsInformation();
|
||||||
|
}
|
||||||
|
|
||||||
private void Log(Severity severity, string message)
|
private void Log(Severity severity, string message)
|
||||||
{
|
{
|
||||||
// To be thread-safe we must capture the delegate reference first
|
// To be thread-safe we must capture the delegate reference first
|
||||||
|
|
32
SMBLibrary/Server/SessionInformation.cs
Normal file
32
SMBLibrary/Server/SessionInformation.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue