SMB2Session.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server
  13. {
  14. internal class SMB2Session
  15. {
  16. private SMB2ConnectionState m_connection;
  17. private ulong m_sessionID;
  18. private byte[] m_sessionKey;
  19. private SecurityContext m_securityContext;
  20. private DateTime m_creationDT;
  21. // Key is TreeID
  22. private Dictionary<uint, ISMBShare> m_connectedTrees = new Dictionary<uint, ISMBShare>();
  23. private uint m_nextTreeID = 1; // TreeID uniquely identifies a tree connect within the scope of the session
  24. // Key is the persistent portion of the FileID
  25. private Dictionary<ulong, OpenFileObject> m_openFiles = new Dictionary<ulong, OpenFileObject>();
  26. // Key is the persistent portion of the FileID
  27. private Dictionary<ulong, OpenSearch> m_openSearches = new Dictionary<ulong, OpenSearch>();
  28. public SMB2Session(SMB2ConnectionState connection, ulong sessionID, string userName, string machineName, byte[] sessionKey, object accessToken)
  29. {
  30. m_connection = connection;
  31. m_sessionID = sessionID;
  32. m_sessionKey = sessionKey;
  33. m_securityContext = new SecurityContext(userName, machineName, connection.ClientEndPoint, connection.AuthenticationContext, accessToken);
  34. m_creationDT = DateTime.Now;
  35. }
  36. private uint? AllocateTreeID()
  37. {
  38. for (uint offset = 0; offset < UInt32.MaxValue; offset++)
  39. {
  40. uint treeID = (uint)(m_nextTreeID + offset);
  41. if (treeID == 0 || treeID == 0xFFFFFFFF)
  42. {
  43. continue;
  44. }
  45. if (!m_connectedTrees.ContainsKey(treeID))
  46. {
  47. m_nextTreeID = (uint)(treeID + 1);
  48. return treeID;
  49. }
  50. }
  51. return null;
  52. }
  53. public uint? AddConnectedTree(ISMBShare share)
  54. {
  55. uint? treeID = AllocateTreeID();
  56. if (treeID.HasValue)
  57. {
  58. m_connectedTrees.Add(treeID.Value, share);
  59. }
  60. return treeID;
  61. }
  62. public ISMBShare GetConnectedTree(uint treeID)
  63. {
  64. if (m_connectedTrees.ContainsKey(treeID))
  65. {
  66. return m_connectedTrees[treeID];
  67. }
  68. else
  69. {
  70. return null;
  71. }
  72. }
  73. public void DisconnectTree(uint treeID)
  74. {
  75. ISMBShare share;
  76. m_connectedTrees.TryGetValue(treeID, out share);
  77. if (share != null)
  78. {
  79. lock (m_openFiles)
  80. {
  81. List<ulong> fileIDList = new List<ulong>(m_openFiles.Keys);
  82. foreach (ushort fileID in fileIDList)
  83. {
  84. OpenFileObject openFile = m_openFiles[fileID];
  85. if (openFile.TreeID == treeID)
  86. {
  87. share.FileStore.CloseFile(openFile.Handle);
  88. m_openFiles.Remove(fileID);
  89. }
  90. }
  91. }
  92. m_connectedTrees.Remove(treeID);
  93. }
  94. }
  95. public bool IsTreeConnected(uint treeID)
  96. {
  97. return m_connectedTrees.ContainsKey(treeID);
  98. }
  99. /// <returns>The persistent portion of the FileID</returns>
  100. public ulong? AddOpenFile(uint treeID, string relativePath, object handle)
  101. {
  102. ulong? persistentID = m_connection.AllocatePersistentFileID();
  103. if (persistentID.HasValue)
  104. {
  105. lock (m_openFiles)
  106. {
  107. m_openFiles.Add(persistentID.Value, new OpenFileObject(treeID, relativePath, handle));
  108. }
  109. }
  110. return persistentID;
  111. }
  112. public OpenFileObject GetOpenFileObject(ulong fileID)
  113. {
  114. if (m_openFiles.ContainsKey(fileID))
  115. {
  116. return m_openFiles[fileID];
  117. }
  118. else
  119. {
  120. return null;
  121. }
  122. }
  123. public void RemoveOpenFile(ulong fileID)
  124. {
  125. lock (m_openFiles)
  126. {
  127. m_openFiles.Remove(fileID);
  128. }
  129. m_openSearches.Remove(fileID);
  130. }
  131. public List<string> ListOpenFiles()
  132. {
  133. List<string> result = new List<string>();
  134. lock (m_openFiles)
  135. {
  136. foreach (OpenFileObject openFile in m_openFiles.Values)
  137. {
  138. result.Add(openFile.Path);
  139. }
  140. }
  141. return result;
  142. }
  143. public OpenSearch AddOpenSearch(ulong fileID, List<QueryDirectoryFileInformation> entries, int enumerationLocation)
  144. {
  145. OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
  146. m_openSearches.Add(fileID, openSearch);
  147. return openSearch;
  148. }
  149. public OpenSearch GetOpenSearch(ulong fileID)
  150. {
  151. OpenSearch openSearch;
  152. m_openSearches.TryGetValue(fileID, out openSearch);
  153. return openSearch;
  154. }
  155. public void RemoveOpenSearch(ulong fileID)
  156. {
  157. m_openSearches.Remove(fileID);
  158. }
  159. /// <summary>
  160. /// Free all resources used by this session
  161. /// </summary>
  162. public void Close()
  163. {
  164. List<uint> treeIDList = new List<uint>(m_connectedTrees.Keys);
  165. foreach (uint treeID in treeIDList)
  166. {
  167. DisconnectTree(treeID);
  168. }
  169. }
  170. public byte[] SessionKey
  171. {
  172. get
  173. {
  174. return m_sessionKey;
  175. }
  176. }
  177. public SecurityContext SecurityContext
  178. {
  179. get
  180. {
  181. return m_securityContext;
  182. }
  183. }
  184. public string UserName
  185. {
  186. get
  187. {
  188. return m_securityContext.UserName;
  189. }
  190. }
  191. public string MachineName
  192. {
  193. get
  194. {
  195. return m_securityContext.MachineName;
  196. }
  197. }
  198. public DateTime CreationDT
  199. {
  200. get
  201. {
  202. return m_creationDT;
  203. }
  204. }
  205. }
  206. }