SMB2FileStore.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Copyright (C) 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 SMBLibrary.SMB2;
  10. using Utilities;
  11. namespace SMBLibrary.Client
  12. {
  13. public class SMB2FileStore : ISMBFileStore
  14. {
  15. private SMB2Client m_client;
  16. private uint m_treeID;
  17. public SMB2FileStore(SMB2Client client, uint treeID)
  18. {
  19. m_client = client;
  20. m_treeID = treeID;
  21. }
  22. public NTStatus CreateFile(out object handle, out FileStatus fileStatus, string path, AccessMask desiredAccess, FileAttributes fileAttributes, ShareAccess shareAccess, CreateDisposition createDisposition, CreateOptions createOptions, SecurityContext securityContext)
  23. {
  24. handle = null;
  25. fileStatus = FileStatus.FILE_DOES_NOT_EXIST;
  26. CreateRequest request = new CreateRequest();
  27. request.Name = path;
  28. request.DesiredAccess = desiredAccess;
  29. request.FileAttributes = fileAttributes;
  30. request.ShareAccess = shareAccess;
  31. request.CreateDisposition = createDisposition;
  32. request.CreateOptions = createOptions;
  33. request.ImpersonationLevel = ImpersonationLevel.Impersonation;
  34. TrySendCommand(request);
  35. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.Create);
  36. if (response != null)
  37. {
  38. if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is CreateResponse)
  39. {
  40. CreateResponse createResponse = ((CreateResponse)response);
  41. handle = createResponse.FileId;
  42. fileStatus = ToFileStatus(createResponse.CreateAction);
  43. }
  44. return response.Header.Status;
  45. }
  46. return NTStatus.STATUS_INVALID_SMB;
  47. }
  48. public NTStatus CloseFile(object handle)
  49. {
  50. CloseRequest request = new CloseRequest();
  51. request.FileId = (FileID)handle;
  52. TrySendCommand(request);
  53. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.Close);
  54. if (response != null)
  55. {
  56. return response.Header.Status;
  57. }
  58. return NTStatus.STATUS_INVALID_SMB;
  59. }
  60. public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
  61. {
  62. data = null;
  63. ReadRequest request = new ReadRequest();
  64. request.FileId = (FileID)handle;
  65. request.Offset = (ulong)offset;
  66. request.ReadLength = (uint)maxCount;
  67. TrySendCommand(request);
  68. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.Read);
  69. if (response != null)
  70. {
  71. if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is ReadResponse)
  72. {
  73. data = ((ReadResponse)response).Data;
  74. }
  75. return response.Header.Status;
  76. }
  77. return NTStatus.STATUS_INVALID_SMB;
  78. }
  79. public NTStatus WriteFile(out int numberOfBytesWritten, object handle, long offset, byte[] data)
  80. {
  81. numberOfBytesWritten = 0;
  82. WriteRequest request = new WriteRequest();
  83. request.FileId = (FileID)handle;
  84. request.Offset = (ulong)offset;
  85. request.Data = data;
  86. TrySendCommand(request);
  87. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.Write);
  88. if (response != null)
  89. {
  90. if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is WriteResponse)
  91. {
  92. numberOfBytesWritten = (int)((WriteResponse)response).Count;
  93. }
  94. return response.Header.Status;
  95. }
  96. return NTStatus.STATUS_INVALID_SMB;
  97. }
  98. public NTStatus FlushFileBuffers(object handle)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. public NTStatus LockFile(object handle, long byteOffset, long length, bool exclusiveLock)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. public NTStatus UnlockFile(object handle, long byteOffset, long length)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. public NTStatus QueryDirectory(out List<QueryDirectoryFileInformation> result, object handle, string fileName, FileInformationClass informationClass)
  111. {
  112. result = new List<QueryDirectoryFileInformation>();
  113. QueryDirectoryRequest request = new QueryDirectoryRequest();
  114. request.FileInformationClass = informationClass;
  115. request.Reopen = true;
  116. request.FileId = (FileID)handle;
  117. request.OutputBufferLength = m_client.MaxTransactSize;
  118. request.FileName = fileName;
  119. TrySendCommand(request);
  120. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.QueryDirectory);
  121. if (response != null)
  122. {
  123. while (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryDirectoryResponse)
  124. {
  125. List<QueryDirectoryFileInformation> page = ((QueryDirectoryResponse)response).GetFileInformationList(informationClass);
  126. result.AddRange(page);
  127. request.Reopen = false;
  128. TrySendCommand(request);
  129. response = m_client.WaitForCommand(SMB2CommandName.QueryDirectory);
  130. }
  131. return response.Header.Status;
  132. }
  133. return NTStatus.STATUS_INVALID_SMB;
  134. }
  135. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  136. {
  137. result = null;
  138. QueryInfoRequest request = new QueryInfoRequest();
  139. request.InfoType = InfoType.File;
  140. request.FileInformationClass = informationClass;
  141. request.OutputBufferLength = 4096;
  142. request.FileId = (FileID)handle;
  143. TrySendCommand(request);
  144. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.QueryInfo);
  145. if (response != null)
  146. {
  147. if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryInfoResponse)
  148. {
  149. result = ((QueryInfoResponse)response).GetFileInformation(informationClass);
  150. }
  151. return response.Header.Status;
  152. }
  153. return NTStatus.STATUS_INVALID_SMB;
  154. }
  155. public NTStatus SetFileInformation(object handle, FileInformation information)
  156. {
  157. throw new NotImplementedException();
  158. }
  159. public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
  160. {
  161. result = null;
  162. object fileHandle;
  163. FileStatus fileStatus;
  164. NTStatus status = CreateFile(out fileHandle, out fileStatus, String.Empty, (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY | (AccessMask)DirectoryAccessMask.FILE_READ_ATTRIBUTES | AccessMask.SYNCHRONIZE, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null);
  165. if (status != NTStatus.STATUS_SUCCESS)
  166. {
  167. return status;
  168. }
  169. status = GetFileSystemInformation(out result, fileHandle, informationClass);
  170. CloseFile(fileHandle);
  171. return status;
  172. }
  173. public NTStatus GetFileSystemInformation(out FileSystemInformation result, object handle, FileSystemInformationClass informationClass)
  174. {
  175. result = null;
  176. QueryInfoRequest request = new QueryInfoRequest();
  177. request.InfoType = InfoType.FileSystem;
  178. request.FileSystemInformationClass = informationClass;
  179. request.OutputBufferLength = 4096;
  180. request.FileId = (FileID)handle;
  181. TrySendCommand(request);
  182. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.QueryInfo);
  183. if (response != null)
  184. {
  185. if (response.Header.Status == NTStatus.STATUS_SUCCESS && response is QueryInfoResponse)
  186. {
  187. result = ((QueryInfoResponse)response).GetFileSystemInformation(informationClass);
  188. }
  189. return response.Header.Status;
  190. }
  191. return NTStatus.STATUS_INVALID_SMB;
  192. }
  193. public NTStatus SetFileSystemInformation(FileSystemInformation information)
  194. {
  195. throw new NotImplementedException();
  196. }
  197. public NTStatus GetSecurityInformation(out SecurityDescriptor result, object handle, SecurityInformation securityInformation)
  198. {
  199. result = null;
  200. return NTStatus.STATUS_NOT_SUPPORTED;
  201. }
  202. public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
  203. {
  204. return NTStatus.STATUS_NOT_SUPPORTED;
  205. }
  206. public NTStatus NotifyChange(out object ioRequest, object handle, NotifyChangeFilter completionFilter, bool watchTree, int outputBufferSize, OnNotifyChangeCompleted onNotifyChangeCompleted, object context)
  207. {
  208. throw new NotImplementedException();
  209. }
  210. public NTStatus Cancel(object ioRequest)
  211. {
  212. throw new NotImplementedException();
  213. }
  214. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  215. {
  216. output = null;
  217. IOCtlRequest request = new IOCtlRequest();
  218. request.CtlCode = ctlCode;
  219. request.IsFSCtl = true;
  220. request.FileId = (FileID)handle;
  221. request.Input = input;
  222. request.MaxOutputResponse = (uint)maxOutputLength;
  223. TrySendCommand(request);
  224. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.IOCtl);
  225. if (response != null)
  226. {
  227. if ((response.Header.Status == NTStatus.STATUS_SUCCESS || response.Header.Status == NTStatus.STATUS_BUFFER_OVERFLOW) && response is IOCtlResponse)
  228. {
  229. output = ((IOCtlResponse)response).Output;
  230. }
  231. return response.Header.Status;
  232. }
  233. return NTStatus.STATUS_INVALID_SMB;
  234. }
  235. public NTStatus Disconnect()
  236. {
  237. TreeDisconnectRequest request = new TreeDisconnectRequest();
  238. TrySendCommand(request);
  239. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.TreeDisconnect);
  240. if (response != null)
  241. {
  242. return response.Header.Status;
  243. }
  244. return NTStatus.STATUS_INVALID_SMB;
  245. }
  246. private void TrySendCommand(SMB2Command request)
  247. {
  248. request.Header.TreeID = m_treeID;
  249. m_client.TrySendCommand(request);
  250. }
  251. public uint MaxReadSize
  252. {
  253. get
  254. {
  255. return m_client.MaxReadSize;
  256. }
  257. }
  258. public uint MaxWriteSize
  259. {
  260. get
  261. {
  262. return m_client.MaxWriteSize;
  263. }
  264. }
  265. private static FileStatus ToFileStatus(CreateAction createAction)
  266. {
  267. switch (createAction)
  268. {
  269. case CreateAction.FILE_SUPERSEDED:
  270. return FileStatus.FILE_SUPERSEDED;
  271. case CreateAction.FILE_OPENED:
  272. return FileStatus.FILE_OPENED;
  273. case CreateAction.FILE_CREATED:
  274. return FileStatus.FILE_CREATED;
  275. case CreateAction.FILE_OVERWRITTEN:
  276. return FileStatus.FILE_OVERWRITTEN;
  277. default:
  278. return FileStatus.FILE_OPENED;
  279. }
  280. }
  281. }
  282. }