SMB1ConnectionState.cs 10 KB

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