SMB1ConnectionState.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. 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. public SMB1ConnectionState(ConnectionState state) : base(state)
  26. {
  27. }
  28. /// <summary>
  29. /// An open UID MUST be unique within an SMB connection.
  30. /// The value of 0xFFFE SHOULD NOT be used as a valid UID. All other possible values for a UID, excluding zero (0x0000), are valid.
  31. /// </summary>
  32. public ushort? AllocateUserID()
  33. {
  34. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  35. {
  36. ushort userID = (ushort)(m_nextUID + offset);
  37. if (userID == 0 || userID == 0xFFFE || userID == 0xFFFF)
  38. {
  39. continue;
  40. }
  41. if (!m_sessions.ContainsKey(userID))
  42. {
  43. m_nextUID = (ushort)(userID + 1);
  44. return userID;
  45. }
  46. }
  47. return null;
  48. }
  49. public SMB1Session CreateSession(ushort userID, string userName, string machineName, byte[] sessionKey, object accessToken)
  50. {
  51. SMB1Session session = new SMB1Session(this, userID, userName, machineName, sessionKey, accessToken);
  52. lock (m_sessions)
  53. {
  54. m_sessions.Add(userID, session);
  55. }
  56. return session;
  57. }
  58. /// <returns>null if all UserID values have already been allocated</returns>
  59. public SMB1Session CreateSession(string userName, string machineName, byte[] sessionKey, object accessToken)
  60. {
  61. ushort? userID = AllocateUserID();
  62. if (userID.HasValue)
  63. {
  64. return CreateSession(userID.Value, userName, machineName, sessionKey, accessToken);
  65. }
  66. return null;
  67. }
  68. public SMB1Session GetSession(ushort userID)
  69. {
  70. SMB1Session session;
  71. m_sessions.TryGetValue(userID, out session);
  72. return session;
  73. }
  74. public void RemoveSession(ushort userID)
  75. {
  76. SMB1Session session;
  77. m_sessions.TryGetValue(userID, out session);
  78. if (session != null)
  79. {
  80. session.Close();
  81. lock (m_sessions)
  82. {
  83. m_sessions.Remove(userID);
  84. }
  85. }
  86. }
  87. public override void CloseSessions()
  88. {
  89. lock (m_sessions)
  90. {
  91. foreach (SMB1Session session in m_sessions.Values)
  92. {
  93. session.Close();
  94. }
  95. }
  96. m_sessions.Clear();
  97. }
  98. public override List<SessionInformation> GetSessionsInformation()
  99. {
  100. List<SessionInformation> result = new List<SessionInformation>();
  101. lock (m_sessions)
  102. {
  103. foreach (SMB1Session session in m_sessions.Values)
  104. {
  105. result.Add(new SessionInformation(this.ClientEndPoint, this.Dialect, session.UserName, session.MachineName, session.ListOpenFiles(), session.CreationDT));
  106. }
  107. }
  108. return result;
  109. }
  110. /// <summary>
  111. /// An open TID MUST be unique within an SMB connection.
  112. /// The value 0xFFFF MUST NOT be used as a valid TID. All other possible values for TID, including zero (0x0000), are valid.
  113. /// </summary>
  114. public ushort? AllocateTreeID()
  115. {
  116. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  117. {
  118. ushort treeID = (ushort)(m_nextTID + offset);
  119. if (treeID == 0 || treeID == 0xFFFF)
  120. {
  121. continue;
  122. }
  123. if (!IsTreeIDAllocated(treeID))
  124. {
  125. m_nextTID = (ushort)(treeID + 1);
  126. return treeID;
  127. }
  128. }
  129. return null;
  130. }
  131. private bool IsTreeIDAllocated(ushort treeID)
  132. {
  133. foreach (SMB1Session session in m_sessions.Values)
  134. {
  135. if (session.GetConnectedTree(treeID) != null)
  136. {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. /// <summary>
  143. /// A FID returned from an Open or Create operation MUST be unique within an SMB connection.
  144. /// The value 0xFFFF MUST NOT be used as a valid FID. All other possible values for FID, including zero (0x0000) are valid.
  145. /// </summary>
  146. /// <returns></returns>
  147. public ushort? AllocateFileID()
  148. {
  149. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  150. {
  151. ushort fileID = (ushort)(m_nextFID + offset);
  152. if (fileID == 0 || fileID == 0xFFFF)
  153. {
  154. continue;
  155. }
  156. if (!IsFileIDAllocated(fileID))
  157. {
  158. m_nextFID = (ushort)(fileID + 1);
  159. return fileID;
  160. }
  161. }
  162. return null;
  163. }
  164. private bool IsFileIDAllocated(ushort fileID)
  165. {
  166. foreach (SMB1Session session in m_sessions.Values)
  167. {
  168. if (session.GetOpenFileObject(fileID) != null)
  169. {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. public ProcessStateObject CreateProcessState(uint processID)
  176. {
  177. ProcessStateObject processState = new ProcessStateObject();
  178. m_processStateList[processID] = processState;
  179. return processState;
  180. }
  181. public ProcessStateObject GetProcessState(uint processID)
  182. {
  183. if (m_processStateList.ContainsKey(processID))
  184. {
  185. return m_processStateList[processID];
  186. }
  187. else
  188. {
  189. return null;
  190. }
  191. }
  192. public void RemoveProcessState(uint processID)
  193. {
  194. m_processStateList.Remove(processID);
  195. }
  196. }
  197. }