123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- /* 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,
- * either version 3 of the License, or (at your option) any later version.
- */
- using System;
- using System.Collections.Generic;
- using System.IO;
- using SMBLibrary.SMB2;
- using Utilities;
- namespace SMBLibrary.Server
- {
- internal class SMB2Session
- {
- private SMB2ConnectionState m_connection;
- private ulong m_sessionID;
- private byte[] m_sessionKey;
- private SecurityContext m_securityContext;
- private DateTime m_creationDT;
- private bool m_signingRequired;
- // Key is TreeID
- private Dictionary<uint, ISMBShare> m_connectedTrees = new Dictionary<uint, ISMBShare>();
- private uint m_nextTreeID = 1; // TreeID uniquely identifies a tree connect within the scope of the session
- // Key is the volatile portion of the FileID
- private Dictionary<ulong, OpenFileObject> m_openFiles = new Dictionary<ulong, OpenFileObject>();
- private ulong m_nextVolatileFileID = 1;
- // Key is the volatile portion of the FileID
- private Dictionary<ulong, OpenSearch> m_openSearches = new Dictionary<ulong, OpenSearch>();
- public SMB2Session(SMB2ConnectionState connection, ulong sessionID, string userName, string machineName, byte[] sessionKey, object accessToken, bool signingRequired)
- {
- m_connection = connection;
- m_sessionID = sessionID;
- m_sessionKey = sessionKey;
- m_securityContext = new SecurityContext(userName, machineName, connection.ClientEndPoint, connection.AuthenticationContext, accessToken);
- m_creationDT = DateTime.Now;
- m_signingRequired = signingRequired;
- }
- private uint? AllocateTreeID()
- {
- for (uint offset = 0; offset < UInt32.MaxValue; offset++)
- {
- uint treeID = (uint)(m_nextTreeID + offset);
- if (treeID == 0 || treeID == 0xFFFFFFFF)
- {
- continue;
- }
- if (!m_connectedTrees.ContainsKey(treeID))
- {
- m_nextTreeID = (uint)(treeID + 1);
- return treeID;
- }
- }
- return null;
- }
- public uint? AddConnectedTree(ISMBShare share)
- {
- uint? treeID = AllocateTreeID();
- if (treeID.HasValue)
- {
- m_connectedTrees.Add(treeID.Value, share);
- }
- return treeID;
- }
- public ISMBShare GetConnectedTree(uint treeID)
- {
- if (m_connectedTrees.ContainsKey(treeID))
- {
- return m_connectedTrees[treeID];
- }
- else
- {
- return null;
- }
- }
- public void DisconnectTree(uint treeID)
- {
- ISMBShare share;
- m_connectedTrees.TryGetValue(treeID, out share);
- if (share != null)
- {
- lock (m_openFiles)
- {
- List<ulong> fileIDList = new List<ulong>(m_openFiles.Keys);
- foreach (ushort fileID in fileIDList)
- {
- OpenFileObject openFile = m_openFiles[fileID];
- if (openFile.TreeID == treeID)
- {
- share.FileStore.CloseFile(openFile.Handle);
- m_openFiles.Remove(fileID);
- }
- }
- }
- m_connectedTrees.Remove(treeID);
- }
- }
- public bool IsTreeConnected(uint treeID)
- {
- return m_connectedTrees.ContainsKey(treeID);
- }
- // VolatileFileID MUST be unique for all volatile handles within the scope of a session
- private ulong? AllocateVolatileFileID()
- {
- for (ulong offset = 0; offset < UInt64.MaxValue; offset++)
- {
- ulong volatileFileID = (ulong)(m_nextVolatileFileID + offset);
- if (volatileFileID == 0 || volatileFileID == 0xFFFFFFFFFFFFFFFF)
- {
- continue;
- }
- if (!m_openFiles.ContainsKey(volatileFileID))
- {
- m_nextVolatileFileID = (ulong)(volatileFileID + 1);
- return volatileFileID;
- }
- }
- return null;
- }
- public FileID? AddOpenFile(uint treeID, string shareName, string relativePath, object handle)
- {
- ulong? volatileFileID = AllocateVolatileFileID();
- if (volatileFileID.HasValue)
- {
- FileID fileID = new FileID();
- fileID.Volatile = volatileFileID.Value;
- // [MS-SMB2] FileId.Persistent MUST be set to Open.DurableFileId.
- // Note: We don't support durable handles so we use volatileFileID.
- fileID.Persistent = volatileFileID.Value;
- lock (m_openFiles)
- {
- m_openFiles.Add(volatileFileID.Value, new OpenFileObject(treeID, shareName, relativePath, handle));
- }
- return fileID;
- }
- return null;
- }
- public OpenFileObject GetOpenFileObject(FileID fileID)
- {
- OpenFileObject result;
- m_openFiles.TryGetValue(fileID.Volatile, out result);
- return result;
- }
- public void RemoveOpenFile(FileID fileID)
- {
- lock (m_openFiles)
- {
- m_openFiles.Remove(fileID.Volatile);
- }
- m_openSearches.Remove(fileID.Volatile);
- }
- 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(FileID fileID, List<QueryDirectoryFileInformation> entries, int enumerationLocation)
- {
- OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
- m_openSearches.Add(fileID.Volatile, openSearch);
- return openSearch;
- }
- public OpenSearch GetOpenSearch(FileID fileID)
- {
- OpenSearch openSearch;
- m_openSearches.TryGetValue(fileID.Volatile, out openSearch);
- return openSearch;
- }
- public void RemoveOpenSearch(FileID fileID)
- {
- m_openSearches.Remove(fileID.Volatile);
- }
- /// <summary>
- /// Free all resources used by this session
- /// </summary>
- public void Close()
- {
- List<uint> treeIDList = new List<uint>(m_connectedTrees.Keys);
- foreach (uint treeID in treeIDList)
- {
- DisconnectTree(treeID);
- }
- }
- public byte[] SessionKey
- {
- get
- {
- return m_sessionKey;
- }
- }
- public SecurityContext SecurityContext
- {
- get
- {
- return m_securityContext;
- }
- }
- public string UserName
- {
- get
- {
- return m_securityContext.UserName;
- }
- }
- public string MachineName
- {
- get
- {
- return m_securityContext.MachineName;
- }
- }
- public DateTime CreationDT
- {
- get
- {
- return m_creationDT;
- }
- }
- public bool SigningRequired
- {
- get
- {
- return m_signingRequired;
- }
- }
- }
- }
|