SMB1ConnectionState.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 Utilities;
  11. namespace SMBLibrary.Server
  12. {
  13. public class SMB1ConnectionState : ConnectionState
  14. {
  15. public int MaxBufferSize;
  16. public bool LargeRead;
  17. public bool LargeWrite;
  18. // Key is UID
  19. private Dictionary<ushort, string> m_connectedUsers = new Dictionary<ushort, string>();
  20. private ushort m_nextUID = 1;
  21. // Key is TID
  22. private Dictionary<ushort, ISMBShare> m_connectedTrees = new Dictionary<ushort, ISMBShare>();
  23. private ushort m_nextTID = 1;
  24. // Key is FID
  25. private Dictionary<ushort, OpenedFileObject> m_openedFiles = new Dictionary<ushort, OpenedFileObject>();
  26. private ushort m_nextFID = 1;
  27. // Key is PID
  28. public Dictionary<uint, ProcessStateObject> ProcessStateList = new Dictionary<uint, ProcessStateObject>();
  29. public const int MaxSearches = 2048; // Windows servers initialize Server.MaxSearches to 2048.
  30. public Dictionary<ushort, List<FileSystemEntry>> OpenSearches = new Dictionary<ushort, List<FileSystemEntry>>();
  31. private ushort m_nextSearchHandle = 1;
  32. public SMB1ConnectionState(ConnectionState state) : base(state)
  33. {
  34. }
  35. /// <summary>
  36. /// An open UID MUST be unique within an SMB connection.
  37. /// The value of 0xFFFE SHOULD NOT be used as a valid UID. All other possible values for a UID, excluding zero (0x0000), are valid.
  38. /// </summary>
  39. private ushort? AllocateUserID()
  40. {
  41. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  42. {
  43. ushort userID = (ushort)(m_nextUID + offset);
  44. if (userID == 0 || userID == 0xFFFE || userID == 0xFFFF)
  45. {
  46. continue;
  47. }
  48. if (!m_connectedUsers.ContainsKey(userID))
  49. {
  50. m_nextUID = (ushort)(userID + 1);
  51. return userID;
  52. }
  53. }
  54. return null;
  55. }
  56. public ushort? AddConnectedUser(string userName)
  57. {
  58. ushort? userID = AllocateUserID();
  59. if (userID.HasValue)
  60. {
  61. m_connectedUsers.Add(userID.Value, userName);
  62. }
  63. return userID;
  64. }
  65. public string GetConnectedUserName(ushort userID)
  66. {
  67. if (m_connectedUsers.ContainsKey(userID))
  68. {
  69. return m_connectedUsers[userID];
  70. }
  71. else
  72. {
  73. return null;
  74. }
  75. }
  76. public bool IsAuthenticated(ushort userID)
  77. {
  78. return m_connectedUsers.ContainsKey(userID);
  79. }
  80. public void RemoveConnectedUser(ushort userID)
  81. {
  82. m_connectedUsers.Remove(userID);
  83. }
  84. /// <summary>
  85. /// An open TID MUST be unique within an SMB connection.
  86. /// The value 0xFFFF MUST NOT be used as a valid TID. All other possible values for TID, including zero (0x0000), are valid.
  87. /// </summary>
  88. private ushort? AllocateTreeID()
  89. {
  90. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  91. {
  92. ushort treeID = (ushort)(m_nextTID + offset);
  93. if (treeID == 0 || treeID == 0xFFFF)
  94. {
  95. continue;
  96. }
  97. if (!m_connectedTrees.ContainsKey(treeID))
  98. {
  99. m_nextTID = (ushort)(treeID + 1);
  100. return treeID;
  101. }
  102. }
  103. return null;
  104. }
  105. public ushort? AddConnectedTree(ISMBShare share)
  106. {
  107. ushort? treeID = AllocateTreeID();
  108. if (treeID.HasValue)
  109. {
  110. m_connectedTrees.Add(treeID.Value, share);
  111. }
  112. return treeID;
  113. }
  114. public ISMBShare GetConnectedTree(ushort treeID)
  115. {
  116. if (m_connectedTrees.ContainsKey(treeID))
  117. {
  118. return m_connectedTrees[treeID];
  119. }
  120. else
  121. {
  122. return null;
  123. }
  124. }
  125. public void RemoveConnectedTree(ushort treeID)
  126. {
  127. m_connectedTrees.Remove(treeID);
  128. }
  129. public bool IsTreeConnected(ushort treeID)
  130. {
  131. return m_connectedTrees.ContainsKey(treeID);
  132. }
  133. public ProcessStateObject GetProcessState(uint processID)
  134. {
  135. if (ProcessStateList.ContainsKey(processID))
  136. {
  137. return ProcessStateList[processID];
  138. }
  139. else
  140. {
  141. return null;
  142. }
  143. }
  144. /// <summary>
  145. /// Get or Create process state
  146. /// </summary>
  147. public ProcessStateObject ObtainProcessState(uint processID)
  148. {
  149. if (ProcessStateList.ContainsKey(processID))
  150. {
  151. return ProcessStateList[processID];
  152. }
  153. else
  154. {
  155. ProcessStateObject processState = new ProcessStateObject();
  156. ProcessStateList[processID] = processState;
  157. return processState;
  158. }
  159. }
  160. /// <summary>
  161. /// The value 0xFFFF MUST NOT be used as a valid FID. All other possible values for FID, including zero (0x0000) are valid.
  162. /// </summary>
  163. /// <returns></returns>
  164. private ushort? AllocateFileID()
  165. {
  166. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  167. {
  168. ushort fileID = (ushort)(m_nextFID + offset);
  169. if (fileID == 0 || fileID == 0xFFFF)
  170. {
  171. continue;
  172. }
  173. if (!m_openedFiles.ContainsKey(fileID))
  174. {
  175. m_nextFID = (ushort)(fileID + 1);
  176. return fileID;
  177. }
  178. }
  179. return null;
  180. }
  181. /// <param name="relativePath">Should include the path relative to the file system</param>
  182. /// <returns>FileID</returns>
  183. public ushort? AddOpenedFile(string relativePath)
  184. {
  185. return AddOpenedFile(relativePath, null);
  186. }
  187. public ushort? AddOpenedFile(string relativePath, Stream stream)
  188. {
  189. return AddOpenedFile(relativePath, stream, false);
  190. }
  191. public ushort? AddOpenedFile(string relativePath, Stream stream, bool deleteOnClose)
  192. {
  193. ushort? fileID = AllocateFileID();
  194. if (fileID.HasValue)
  195. {
  196. m_openedFiles.Add(fileID.Value, new OpenedFileObject(relativePath, stream, deleteOnClose));
  197. }
  198. return fileID;
  199. }
  200. public string GetOpenedFilePath(ushort fileID)
  201. {
  202. if (m_openedFiles.ContainsKey(fileID))
  203. {
  204. return m_openedFiles[fileID].Path;
  205. }
  206. else
  207. {
  208. return null;
  209. }
  210. }
  211. public OpenedFileObject GetOpenedFileObject(ushort fileID)
  212. {
  213. if (m_openedFiles.ContainsKey(fileID))
  214. {
  215. return m_openedFiles[fileID];
  216. }
  217. else
  218. {
  219. return null;
  220. }
  221. }
  222. public bool IsFileOpen(ushort fileID)
  223. {
  224. return m_openedFiles.ContainsKey(fileID);
  225. }
  226. public void RemoveOpenedFile(ushort fileID)
  227. {
  228. Stream stream = m_openedFiles[fileID].Stream;
  229. if (stream != null)
  230. {
  231. LogToServer(Severity.Verbose, "Closing file '{0}'", m_openedFiles[fileID].Path);
  232. stream.Close();
  233. }
  234. m_openedFiles.Remove(fileID);
  235. }
  236. public uint? GetMaxDataCount(uint processID)
  237. {
  238. ProcessStateObject processState = GetProcessState(processID);
  239. if (processState != null)
  240. {
  241. return processState.MaxDataCount;
  242. }
  243. else
  244. {
  245. return null;
  246. }
  247. }
  248. public ushort? AllocateSearchHandle()
  249. {
  250. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  251. {
  252. ushort searchHandle = (ushort)(m_nextSearchHandle + offset);
  253. if (searchHandle == 0 || searchHandle == 0xFFFF)
  254. {
  255. continue;
  256. }
  257. if (!OpenSearches.ContainsKey(searchHandle))
  258. {
  259. m_nextSearchHandle = (ushort)(searchHandle + 1);
  260. return searchHandle;
  261. }
  262. }
  263. return null;
  264. }
  265. public void ReleaseSearchHandle(ushort searchHandle)
  266. {
  267. if (OpenSearches.ContainsKey(searchHandle))
  268. {
  269. OpenSearches.Remove(searchHandle);
  270. }
  271. }
  272. }
  273. }