SMB2Session.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. private bool m_signingRequired;
  22. // Key is TreeID
  23. private Dictionary<uint, ISMBShare> m_connectedTrees = new Dictionary<uint, ISMBShare>();
  24. private uint m_nextTreeID = 1; // TreeID uniquely identifies a tree connect within the scope of the session
  25. // Key is the volatile portion of the FileID
  26. private Dictionary<ulong, OpenFileObject> m_openFiles = new Dictionary<ulong, OpenFileObject>();
  27. private ulong m_nextVolatileFileID = 1;
  28. // Key is the volatile portion of the FileID
  29. private Dictionary<ulong, OpenSearch> m_openSearches = new Dictionary<ulong, OpenSearch>();
  30. public SMB2Session(SMB2ConnectionState connection, ulong sessionID, string userName, string machineName, byte[] sessionKey, object accessToken, bool signingRequired)
  31. {
  32. m_connection = connection;
  33. m_sessionID = sessionID;
  34. m_sessionKey = sessionKey;
  35. m_securityContext = new SecurityContext(userName, machineName, connection.ClientEndPoint, connection.AuthenticationContext, accessToken);
  36. m_creationDT = DateTime.Now;
  37. m_signingRequired = signingRequired;
  38. }
  39. private uint? AllocateTreeID()
  40. {
  41. for (uint offset = 0; offset < UInt32.MaxValue; offset++)
  42. {
  43. uint treeID = (uint)(m_nextTreeID + offset);
  44. if (treeID == 0 || treeID == 0xFFFFFFFF)
  45. {
  46. continue;
  47. }
  48. if (!m_connectedTrees.ContainsKey(treeID))
  49. {
  50. m_nextTreeID = (uint)(treeID + 1);
  51. return treeID;
  52. }
  53. }
  54. return null;
  55. }
  56. public uint? AddConnectedTree(ISMBShare share)
  57. {
  58. uint? treeID = AllocateTreeID();
  59. if (treeID.HasValue)
  60. {
  61. m_connectedTrees.Add(treeID.Value, share);
  62. }
  63. return treeID;
  64. }
  65. public ISMBShare GetConnectedTree(uint treeID)
  66. {
  67. if (m_connectedTrees.ContainsKey(treeID))
  68. {
  69. return m_connectedTrees[treeID];
  70. }
  71. else
  72. {
  73. return null;
  74. }
  75. }
  76. public void DisconnectTree(uint treeID)
  77. {
  78. ISMBShare share;
  79. m_connectedTrees.TryGetValue(treeID, out share);
  80. if (share != null)
  81. {
  82. lock (m_openFiles)
  83. {
  84. List<ulong> fileIDList = new List<ulong>(m_openFiles.Keys);
  85. foreach (ushort fileID in fileIDList)
  86. {
  87. OpenFileObject openFile = m_openFiles[fileID];
  88. if (openFile.TreeID == treeID)
  89. {
  90. share.FileStore.CloseFile(openFile.Handle);
  91. m_openFiles.Remove(fileID);
  92. }
  93. }
  94. }
  95. m_connectedTrees.Remove(treeID);
  96. }
  97. }
  98. public bool IsTreeConnected(uint treeID)
  99. {
  100. return m_connectedTrees.ContainsKey(treeID);
  101. }
  102. // VolatileFileID MUST be unique for all volatile handles within the scope of a session
  103. private ulong? AllocateVolatileFileID()
  104. {
  105. for (ulong offset = 0; offset < UInt64.MaxValue; offset++)
  106. {
  107. ulong volatileFileID = (ulong)(m_nextVolatileFileID + offset);
  108. if (volatileFileID == 0 || volatileFileID == 0xFFFFFFFFFFFFFFFF)
  109. {
  110. continue;
  111. }
  112. if (!m_openFiles.ContainsKey(volatileFileID))
  113. {
  114. m_nextVolatileFileID = (ulong)(volatileFileID + 1);
  115. return volatileFileID;
  116. }
  117. }
  118. return null;
  119. }
  120. public FileID? AddOpenFile(uint treeID, string shareName, string relativePath, object handle)
  121. {
  122. ulong? volatileFileID = AllocateVolatileFileID();
  123. if (volatileFileID.HasValue)
  124. {
  125. FileID fileID = new FileID();
  126. fileID.Volatile = volatileFileID.Value;
  127. // [MS-SMB2] FileId.Persistent MUST be set to Open.DurableFileId.
  128. // Note: We don't support durable handles so we use volatileFileID.
  129. fileID.Persistent = volatileFileID.Value;
  130. lock (m_openFiles)
  131. {
  132. m_openFiles.Add(volatileFileID.Value, new OpenFileObject(treeID, shareName, relativePath, handle));
  133. }
  134. return fileID;
  135. }
  136. return null;
  137. }
  138. public OpenFileObject GetOpenFileObject(FileID fileID)
  139. {
  140. OpenFileObject result;
  141. m_openFiles.TryGetValue(fileID.Volatile, out result);
  142. return result;
  143. }
  144. public void RemoveOpenFile(FileID fileID)
  145. {
  146. lock (m_openFiles)
  147. {
  148. m_openFiles.Remove(fileID.Volatile);
  149. }
  150. m_openSearches.Remove(fileID.Volatile);
  151. }
  152. public List<string> ListOpenFiles()
  153. {
  154. List<string> result = new List<string>();
  155. lock (m_openFiles)
  156. {
  157. foreach (OpenFileObject openFile in m_openFiles.Values)
  158. {
  159. result.Add(openFile.Path);
  160. }
  161. }
  162. return result;
  163. }
  164. public OpenSearch AddOpenSearch(FileID fileID, List<QueryDirectoryFileInformation> entries, int enumerationLocation)
  165. {
  166. OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
  167. m_openSearches.Add(fileID.Volatile, openSearch);
  168. return openSearch;
  169. }
  170. public OpenSearch GetOpenSearch(FileID fileID)
  171. {
  172. OpenSearch openSearch;
  173. m_openSearches.TryGetValue(fileID.Volatile, out openSearch);
  174. return openSearch;
  175. }
  176. public void RemoveOpenSearch(FileID fileID)
  177. {
  178. m_openSearches.Remove(fileID.Volatile);
  179. }
  180. /// <summary>
  181. /// Free all resources used by this session
  182. /// </summary>
  183. public void Close()
  184. {
  185. List<uint> treeIDList = new List<uint>(m_connectedTrees.Keys);
  186. foreach (uint treeID in treeIDList)
  187. {
  188. DisconnectTree(treeID);
  189. }
  190. }
  191. public byte[] SessionKey
  192. {
  193. get
  194. {
  195. return m_sessionKey;
  196. }
  197. }
  198. public SecurityContext SecurityContext
  199. {
  200. get
  201. {
  202. return m_securityContext;
  203. }
  204. }
  205. public string UserName
  206. {
  207. get
  208. {
  209. return m_securityContext.UserName;
  210. }
  211. }
  212. public string MachineName
  213. {
  214. get
  215. {
  216. return m_securityContext.MachineName;
  217. }
  218. }
  219. public DateTime CreationDT
  220. {
  221. get
  222. {
  223. return m_creationDT;
  224. }
  225. }
  226. public bool SigningRequired
  227. {
  228. get
  229. {
  230. return m_signingRequired;
  231. }
  232. }
  233. }
  234. }