SMB1ConnectionState.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (C) 2014-2019 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. internal class SMB1ConnectionState : ConnectionState
  14. {
  15. public int MaxBufferSize;
  16. public bool LargeRead;
  17. public bool LargeWrite;
  18. // Key is UID
  19. private Dictionary<ushort, SMB1Session> m_sessions = new Dictionary<ushort, SMB1Session>();
  20. private ushort m_nextUID = 1; // UID MUST be unique within an SMB connection
  21. private ushort m_nextTID = 1; // TID MUST be unique within an SMB connection
  22. private ushort m_nextFID = 1; // FID MUST be unique within an SMB connection
  23. // Key is PID (PID MUST be unique within an SMB connection)
  24. private Dictionary<uint, ProcessStateObject> m_processStateList = new Dictionary<uint, ProcessStateObject>();
  25. private List<SMB1AsyncContext> m_pendingRequests = new List<SMB1AsyncContext>();
  26. public SMB1ConnectionState(ConnectionState state) : base(state)
  27. {
  28. }
  29. /// <summary>
  30. /// An open UID MUST be unique within an SMB connection.
  31. /// The value of 0xFFFE SHOULD NOT be used as a valid UID. All other possible values for a UID, excluding zero (0x0000), are valid.
  32. /// </summary>
  33. public ushort? AllocateUserID()
  34. {
  35. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  36. {
  37. ushort userID = (ushort)(m_nextUID + offset);
  38. if (userID == 0 || userID == 0xFFFE || userID == 0xFFFF)
  39. {
  40. continue;
  41. }
  42. if (!m_sessions.ContainsKey(userID))
  43. {
  44. m_nextUID = (ushort)(userID + 1);
  45. return userID;
  46. }
  47. }
  48. return null;
  49. }
  50. public SMB1Session CreateSession(ushort userID, string userName, string machineName, byte[] sessionKey, object accessToken)
  51. {
  52. SMB1Session session = new SMB1Session(this, userID, userName, machineName, sessionKey, accessToken);
  53. lock (m_sessions)
  54. {
  55. m_sessions.Add(userID, session);
  56. }
  57. return session;
  58. }
  59. /// <returns>null if all UserID values have already been allocated</returns>
  60. public SMB1Session CreateSession(string userName, string machineName, byte[] sessionKey, object accessToken)
  61. {
  62. ushort? userID = AllocateUserID();
  63. if (userID.HasValue)
  64. {
  65. return CreateSession(userID.Value, userName, machineName, sessionKey, accessToken);
  66. }
  67. return null;
  68. }
  69. public SMB1Session GetSession(ushort userID)
  70. {
  71. SMB1Session session;
  72. m_sessions.TryGetValue(userID, out session);
  73. return session;
  74. }
  75. public void RemoveSession(ushort userID)
  76. {
  77. SMB1Session session;
  78. m_sessions.TryGetValue(userID, out session);
  79. if (session != null)
  80. {
  81. session.Close();
  82. lock (m_sessions)
  83. {
  84. m_sessions.Remove(userID);
  85. }
  86. }
  87. }
  88. public override void CloseSessions()
  89. {
  90. lock (m_sessions)
  91. {
  92. foreach (SMB1Session session in m_sessions.Values)
  93. {
  94. session.Close();
  95. }
  96. m_sessions.Clear();
  97. }
  98. }
  99. public override List<SessionInformation> GetSessionsInformation()
  100. {
  101. List<SessionInformation> result = new List<SessionInformation>();
  102. lock (m_sessions)
  103. {
  104. foreach (SMB1Session session in m_sessions.Values)
  105. {
  106. result.Add(new SessionInformation(this.ClientEndPoint, this.Dialect, session.UserName, session.MachineName, session.GetOpenFilesInformation(), session.CreationDT));
  107. }
  108. }
  109. return result;
  110. }
  111. /// <summary>
  112. /// An open TID MUST be unique within an SMB connection.
  113. /// The value 0xFFFF MUST NOT be used as a valid TID. All other possible values for TID, including zero (0x0000), are valid.
  114. /// </summary>
  115. public ushort? AllocateTreeID()
  116. {
  117. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  118. {
  119. ushort treeID = (ushort)(m_nextTID + offset);
  120. if (treeID == 0 || treeID == 0xFFFF)
  121. {
  122. continue;
  123. }
  124. if (!IsTreeIDAllocated(treeID))
  125. {
  126. m_nextTID = (ushort)(treeID + 1);
  127. return treeID;
  128. }
  129. }
  130. return null;
  131. }
  132. private bool IsTreeIDAllocated(ushort treeID)
  133. {
  134. foreach (SMB1Session session in m_sessions.Values)
  135. {
  136. if (session.GetConnectedTree(treeID) != null)
  137. {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. /// <summary>
  144. /// A FID returned from an Open or Create operation MUST be unique within an SMB connection.
  145. /// The value 0xFFFF MUST NOT be used as a valid FID. All other possible values for FID, including zero (0x0000) are valid.
  146. /// </summary>
  147. /// <returns></returns>
  148. public ushort? AllocateFileID()
  149. {
  150. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  151. {
  152. ushort fileID = (ushort)(m_nextFID + offset);
  153. if (fileID == 0 || fileID == 0xFFFF)
  154. {
  155. continue;
  156. }
  157. if (!IsFileIDAllocated(fileID))
  158. {
  159. m_nextFID = (ushort)(fileID + 1);
  160. return fileID;
  161. }
  162. }
  163. return null;
  164. }
  165. private bool IsFileIDAllocated(ushort fileID)
  166. {
  167. foreach (SMB1Session session in m_sessions.Values)
  168. {
  169. if (session.GetOpenFileObject(fileID) != null)
  170. {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. public ProcessStateObject CreateProcessState(uint processID)
  177. {
  178. ProcessStateObject processState = new ProcessStateObject();
  179. m_processStateList[processID] = processState;
  180. return processState;
  181. }
  182. public ProcessStateObject GetProcessState(uint processID)
  183. {
  184. if (m_processStateList.ContainsKey(processID))
  185. {
  186. return m_processStateList[processID];
  187. }
  188. else
  189. {
  190. return null;
  191. }
  192. }
  193. public void RemoveProcessState(uint processID)
  194. {
  195. m_processStateList.Remove(processID);
  196. }
  197. public SMB1AsyncContext CreateAsyncContext(ushort userID, ushort treeID, uint processID, ushort multiplexID, ushort fileID, SMB1ConnectionState connection)
  198. {
  199. SMB1AsyncContext context = new SMB1AsyncContext();
  200. context.UID = userID;
  201. context.TID = treeID;
  202. context.MID = multiplexID;
  203. context.PID = processID;
  204. context.FileID = fileID;
  205. context.Connection = connection;
  206. lock (m_pendingRequests)
  207. {
  208. m_pendingRequests.Add(context);
  209. }
  210. return context;
  211. }
  212. public SMB1AsyncContext GetAsyncContext(ushort userID, ushort treeID, uint processID, ushort multiplexID)
  213. {
  214. lock (m_pendingRequests)
  215. {
  216. int index = IndexOfAsyncContext(userID, treeID, processID, multiplexID);
  217. if (index >= 0)
  218. {
  219. return m_pendingRequests[index];
  220. }
  221. }
  222. return null;
  223. }
  224. public void RemoveAsyncContext(SMB1AsyncContext context)
  225. {
  226. lock (m_pendingRequests)
  227. {
  228. int index = IndexOfAsyncContext(context.UID, context.TID, context.PID, context.MID);
  229. if (index >= 0)
  230. {
  231. m_pendingRequests.RemoveAt(index);
  232. }
  233. }
  234. }
  235. private int IndexOfAsyncContext(ushort userID, ushort treeID, uint processID, ushort multiplexID)
  236. {
  237. for (int index = 0; index < m_pendingRequests.Count; index++)
  238. {
  239. SMB1AsyncContext context = m_pendingRequests[index];
  240. if (context.UID == userID &&
  241. context.TID == treeID &&
  242. context.PID == processID &&
  243. context.MID == multiplexID)
  244. {
  245. return index;
  246. }
  247. }
  248. return -1;
  249. }
  250. }
  251. }