SMB1Session.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 SMB1Session
  14. {
  15. private const int MaxSearches = 2048; // Windows servers initialize Server.MaxSearches to 2048.
  16. private SMB1ConnectionState m_connection;
  17. private ushort m_userID;
  18. private SecurityContext m_securityContext;
  19. // Key is TID
  20. private Dictionary<ushort, ISMBShare> m_connectedTrees = new Dictionary<ushort, ISMBShare>();
  21. // Key is FID
  22. private Dictionary<ushort, OpenFileObject> m_openFiles = new Dictionary<ushort, OpenFileObject>();
  23. // Key is search handle a.k.a. Search ID
  24. private Dictionary<ushort, OpenSearch> m_openSearches = new Dictionary<ushort, OpenSearch>();
  25. private ushort m_nextSearchHandle = 1;
  26. public SMB1Session(SMB1ConnectionState connection, ushort userID, string userName, string machineName)
  27. {
  28. m_connection = connection;
  29. m_userID = userID;
  30. m_securityContext = new SecurityContext(userName, machineName, connection.ClientEndPoint);
  31. }
  32. public ushort? AddConnectedTree(ISMBShare share)
  33. {
  34. ushort? treeID = m_connection.AllocateTreeID();
  35. if (treeID.HasValue)
  36. {
  37. m_connectedTrees.Add(treeID.Value, share);
  38. }
  39. return treeID;
  40. }
  41. public ISMBShare GetConnectedTree(ushort treeID)
  42. {
  43. ISMBShare share;
  44. m_connectedTrees.TryGetValue(treeID, out share);
  45. return share;
  46. }
  47. public void RemoveConnectedTree(ushort treeID)
  48. {
  49. m_connectedTrees.Remove(treeID);
  50. }
  51. public bool IsTreeConnected(ushort treeID)
  52. {
  53. return m_connectedTrees.ContainsKey(treeID);
  54. }
  55. /// <param name="relativePath">Should include the path relative to the share</param>
  56. /// <returns>FileID</returns>
  57. public ushort? AddOpenFile(string relativePath)
  58. {
  59. return AddOpenFile(relativePath, null);
  60. }
  61. public ushort? AddOpenFile(string relativePath, object handle)
  62. {
  63. ushort? fileID = m_connection.AllocateFileID();
  64. if (fileID.HasValue)
  65. {
  66. m_openFiles.Add(fileID.Value, new OpenFileObject(relativePath, handle));
  67. }
  68. return fileID;
  69. }
  70. public OpenFileObject GetOpenFileObject(ushort fileID)
  71. {
  72. OpenFileObject openFile;
  73. m_openFiles.TryGetValue(fileID, out openFile);
  74. return openFile;
  75. }
  76. public void RemoveOpenFile(ushort fileID)
  77. {
  78. m_openFiles.Remove(fileID);
  79. }
  80. private ushort? AllocateSearchHandle()
  81. {
  82. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  83. {
  84. ushort searchHandle = (ushort)(m_nextSearchHandle + offset);
  85. if (searchHandle == 0 || searchHandle == 0xFFFF)
  86. {
  87. continue;
  88. }
  89. if (!m_openSearches.ContainsKey(searchHandle))
  90. {
  91. m_nextSearchHandle = (ushort)(searchHandle + 1);
  92. return searchHandle;
  93. }
  94. }
  95. return null;
  96. }
  97. public ushort? AddOpenSearch(List<QueryDirectoryFileInformation> entries, int enumerationLocation)
  98. {
  99. ushort? searchHandle = AllocateSearchHandle();
  100. if (searchHandle.HasValue)
  101. {
  102. OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
  103. m_openSearches.Add(searchHandle.Value, openSearch);
  104. }
  105. return searchHandle;
  106. }
  107. public OpenSearch GetOpenSearch(ushort searchHandle)
  108. {
  109. OpenSearch openSearch;
  110. m_openSearches.TryGetValue(searchHandle, out openSearch);
  111. return openSearch;
  112. }
  113. public void RemoveOpenSearch(ushort searchHandle)
  114. {
  115. m_openSearches.Remove(searchHandle);
  116. }
  117. public ushort UserID
  118. {
  119. get
  120. {
  121. return m_userID;
  122. }
  123. }
  124. public SecurityContext SecurityContext
  125. {
  126. get
  127. {
  128. return m_securityContext;
  129. }
  130. }
  131. public string UserName
  132. {
  133. get
  134. {
  135. return m_securityContext.UserName;
  136. }
  137. }
  138. }
  139. }