SMB2Session.cs 8.4 KB

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