SMB2FileStore.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. throw new NotImplementedException();
  113. }
  114. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. public NTStatus SetFileInformation(object handle, FileInformation information)
  119. {
  120. throw new NotImplementedException();
  121. }
  122. public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
  123. {
  124. throw new NotImplementedException();
  125. }
  126. public NTStatus SetFileSystemInformation(FileSystemInformation information)
  127. {
  128. throw new NotImplementedException();
  129. }
  130. public NTStatus GetSecurityInformation(out SecurityDescriptor result, object handle, SecurityInformation securityInformation)
  131. {
  132. result = null;
  133. return NTStatus.STATUS_NOT_SUPPORTED;
  134. }
  135. public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
  136. {
  137. return NTStatus.STATUS_NOT_SUPPORTED;
  138. }
  139. public NTStatus NotifyChange(out object ioRequest, object handle, NotifyChangeFilter completionFilter, bool watchTree, int outputBufferSize, OnNotifyChangeCompleted onNotifyChangeCompleted, object context)
  140. {
  141. throw new NotImplementedException();
  142. }
  143. public NTStatus Cancel(object ioRequest)
  144. {
  145. throw new NotImplementedException();
  146. }
  147. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  148. {
  149. output = null;
  150. IOCtlRequest request = new IOCtlRequest();
  151. request.CtlCode = ctlCode;
  152. request.IsFSCtl = true;
  153. request.FileId = (FileID)handle;
  154. request.Input = input;
  155. request.MaxOutputResponse = (uint)maxOutputLength;
  156. TrySendCommand(request);
  157. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.IOCtl);
  158. if (response != null)
  159. {
  160. if ((response.Header.Status == NTStatus.STATUS_SUCCESS || response.Header.Status == NTStatus.STATUS_BUFFER_OVERFLOW) && response is IOCtlResponse)
  161. {
  162. output = ((IOCtlResponse)response).Output;
  163. }
  164. return response.Header.Status;
  165. }
  166. return NTStatus.STATUS_INVALID_SMB;
  167. }
  168. public NTStatus Disconnect()
  169. {
  170. TreeDisconnectRequest request = new TreeDisconnectRequest();
  171. TrySendCommand(request);
  172. SMB2Command response = m_client.WaitForCommand(SMB2CommandName.TreeDisconnect);
  173. if (response != null)
  174. {
  175. return response.Header.Status;
  176. }
  177. return NTStatus.STATUS_INVALID_SMB;
  178. }
  179. private void TrySendCommand(SMB2Command request)
  180. {
  181. request.Header.TreeID = m_treeID;
  182. m_client.TrySendCommand(request);
  183. }
  184. private static FileStatus ToFileStatus(CreateAction createAction)
  185. {
  186. switch (createAction)
  187. {
  188. case CreateAction.FILE_SUPERSEDED:
  189. return FileStatus.FILE_SUPERSEDED;
  190. case CreateAction.FILE_OPENED:
  191. return FileStatus.FILE_OPENED;
  192. case CreateAction.FILE_CREATED:
  193. return FileStatus.FILE_CREATED;
  194. case CreateAction.FILE_OVERWRITTEN:
  195. return FileStatus.FILE_OVERWRITTEN;
  196. default:
  197. return FileStatus.FILE_OPENED;
  198. }
  199. }
  200. }
  201. }