SMB2Session.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. public class SMB2Session
  15. {
  16. private SMB2ConnectionState m_connection;
  17. private ulong m_sessionID;
  18. private SecurityContext m_securityContext;
  19. // Key is TreeID
  20. private Dictionary<uint, ISMBShare> m_connectedTrees = new Dictionary<uint, ISMBShare>();
  21. private uint m_nextTreeID = 1; // TreeID uniquely identifies a tree connect within the scope of the session
  22. // Key is the persistent portion of the FileID
  23. private Dictionary<ulong, OpenFileObject> m_openFiles = new Dictionary<ulong, OpenFileObject>();
  24. // Key is the persistent portion of the FileID
  25. private Dictionary<ulong, OpenSearch> m_openSearches = new Dictionary<ulong, OpenSearch>();
  26. public SMB2Session(SMB2ConnectionState connecton, ulong sessionID, string userName, string machineName)
  27. {
  28. m_connection = connecton;
  29. m_sessionID = sessionID;
  30. m_securityContext = new SecurityContext(userName, machineName, connecton.ClientEndPoint);
  31. }
  32. private uint? AllocateTreeID()
  33. {
  34. for (uint offset = 0; offset < UInt32.MaxValue; offset++)
  35. {
  36. uint treeID = (uint)(m_nextTreeID + offset);
  37. if (treeID == 0 || treeID == 0xFFFFFFFF)
  38. {
  39. continue;
  40. }
  41. if (!m_connectedTrees.ContainsKey(treeID))
  42. {
  43. m_nextTreeID = (uint)(treeID + 1);
  44. return treeID;
  45. }
  46. }
  47. return null;
  48. }
  49. public uint? AddConnectedTree(ISMBShare share)
  50. {
  51. uint? treeID = AllocateTreeID();
  52. if (treeID.HasValue)
  53. {
  54. m_connectedTrees.Add(treeID.Value, share);
  55. }
  56. return treeID;
  57. }
  58. public ISMBShare GetConnectedTree(uint treeID)
  59. {
  60. if (m_connectedTrees.ContainsKey(treeID))
  61. {
  62. return m_connectedTrees[treeID];
  63. }
  64. else
  65. {
  66. return null;
  67. }
  68. }
  69. public void RemoveConnectedTree(uint treeID)
  70. {
  71. m_connectedTrees.Remove(treeID);
  72. }
  73. public void RemoveConnectedTrees()
  74. {
  75. m_connectedTrees.Clear();
  76. }
  77. public bool IsTreeConnected(uint treeID)
  78. {
  79. return m_connectedTrees.ContainsKey(treeID);
  80. }
  81. /// <returns>The persistent portion of the FileID</returns>
  82. public ulong? AddOpenFile(string relativePath, object handle)
  83. {
  84. ulong? persistentID = m_connection.AllocatePersistentFileID();
  85. if (persistentID.HasValue)
  86. {
  87. m_openFiles.Add(persistentID.Value, new OpenFileObject(relativePath, handle));
  88. }
  89. return persistentID;
  90. }
  91. public OpenFileObject GetOpenFileObject(ulong fileID)
  92. {
  93. if (m_openFiles.ContainsKey(fileID))
  94. {
  95. return m_openFiles[fileID];
  96. }
  97. else
  98. {
  99. return null;
  100. }
  101. }
  102. public void RemoveOpenFile(ulong fileID)
  103. {
  104. m_openFiles.Remove(fileID);
  105. m_openSearches.Remove(fileID);
  106. }
  107. public OpenSearch AddOpenSearch(ulong fileID, List<QueryDirectoryFileInformation> entries, int enumerationLocation)
  108. {
  109. OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
  110. m_openSearches.Add(fileID, openSearch);
  111. return openSearch;
  112. }
  113. public OpenSearch GetOpenSearch(ulong fileID)
  114. {
  115. OpenSearch openSearch;
  116. m_openSearches.TryGetValue(fileID, out openSearch);
  117. return openSearch;
  118. }
  119. public void RemoveOpenSearch(ulong fileID)
  120. {
  121. m_openSearches.Remove(fileID);
  122. }
  123. public SecurityContext SecurityContext
  124. {
  125. get
  126. {
  127. return m_securityContext;
  128. }
  129. }
  130. public string UserName
  131. {
  132. get
  133. {
  134. return m_securityContext.UserName;
  135. }
  136. }
  137. }
  138. }