SMB1FileStore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 SMBLibrary.SMB1;
  10. using Utilities;
  11. namespace SMBLibrary.Client
  12. {
  13. public class SMB1FileStore : INTFileStore
  14. {
  15. private SMB1Client m_client;
  16. private ushort m_treeID;
  17. public SMB1FileStore(SMB1Client client, ushort 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. NTCreateAndXRequest request = new NTCreateAndXRequest();
  27. request.FileName = path;
  28. request.DesiredAccess = desiredAccess;
  29. request.ExtFileAttributes = ToExtendedFileAttributes(fileAttributes);
  30. request.ShareAccess = shareAccess;
  31. request.CreateDisposition = createDisposition;
  32. request.CreateOptions = createOptions;
  33. request.ImpersonationLevel = ImpersonationLevel.SEC_IMPERSONATE;
  34. TrySendMessage(request);
  35. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_CREATE_ANDX);
  36. if (reply != null)
  37. {
  38. if (reply.Commands[0] is NTCreateAndXResponse)
  39. {
  40. NTCreateAndXResponse response = reply.Commands[0] as NTCreateAndXResponse;
  41. handle = response.FID;
  42. fileStatus = ToFileStatus(response.CreateDisposition);
  43. return reply.Header.Status;
  44. }
  45. else if (reply.Commands[0] is ErrorResponse)
  46. {
  47. return reply.Header.Status;
  48. }
  49. }
  50. return NTStatus.STATUS_INVALID_SMB;
  51. }
  52. public NTStatus CloseFile(object handle)
  53. {
  54. CloseRequest request = new CloseRequest();
  55. request.FID = (ushort)handle;
  56. TrySendMessage(request);
  57. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_CLOSE);
  58. if (reply != null)
  59. {
  60. return reply.Header.Status;
  61. }
  62. return NTStatus.STATUS_INVALID_SMB;
  63. }
  64. public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
  65. {
  66. data = null;
  67. ReadAndXRequest request = new ReadAndXRequest();
  68. request.FID = (ushort)handle;
  69. request.Offset = (ulong)offset;
  70. request.MaxCountLarge = (uint)maxCount;
  71. TrySendMessage(request);
  72. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_READ_ANDX);
  73. if (reply != null)
  74. {
  75. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is ReadAndXResponse)
  76. {
  77. data = ((ReadAndXResponse)reply.Commands[0]).Data;
  78. }
  79. return reply.Header.Status;
  80. }
  81. return NTStatus.STATUS_INVALID_SMB;
  82. }
  83. public NTStatus WriteFile(out int numberOfBytesWritten, object handle, long offset, byte[] data)
  84. {
  85. numberOfBytesWritten = 0;
  86. WriteAndXRequest request = new WriteAndXRequest();
  87. request.FID = (ushort)handle;
  88. request.Offset = (ulong)offset;
  89. request.Data = data;
  90. TrySendMessage(request);
  91. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_WRITE_ANDX);
  92. if (reply != null)
  93. {
  94. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is WriteAndXResponse)
  95. {
  96. numberOfBytesWritten = (int)((WriteAndXResponse)reply.Commands[0]).Count;
  97. }
  98. return reply.Header.Status;
  99. }
  100. return NTStatus.STATUS_INVALID_SMB;
  101. }
  102. public NTStatus FlushFileBuffers(object handle)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. public NTStatus LockFile(object handle, long byteOffset, long length, bool exclusiveLock)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. public NTStatus UnlockFile(object handle, long byteOffset, long length)
  111. {
  112. throw new NotImplementedException();
  113. }
  114. public NTStatus QueryDirectory(out List<QueryDirectoryFileInformation> result, object handle, string fileName, FileInformationClass informationClass)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  119. {
  120. throw new NotImplementedException();
  121. }
  122. public NTStatus SetFileInformation(object handle, FileInformation information)
  123. {
  124. throw new NotImplementedException();
  125. }
  126. public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
  127. {
  128. throw new NotImplementedException();
  129. }
  130. public NTStatus NotifyChange(out object ioRequest, object handle, NotifyChangeFilter completionFilter, bool watchTree, int outputBufferSize, OnNotifyChangeCompleted onNotifyChangeCompleted, object context)
  131. {
  132. throw new NotImplementedException();
  133. }
  134. public NTStatus Cancel(object ioRequest)
  135. {
  136. throw new NotImplementedException();
  137. }
  138. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  139. {
  140. if ((IoControlCode)ctlCode == IoControlCode.FSCTL_PIPE_TRANSCEIVE)
  141. {
  142. return FsCtlPipeTranscieve(handle, input, out output, maxOutputLength);
  143. }
  144. output = null;
  145. NTTransactIOCTLRequest subcommand = new NTTransactIOCTLRequest();
  146. subcommand.FID = (ushort)handle;
  147. subcommand.FunctionCode = ctlCode;
  148. subcommand.IsFsctl = true;
  149. subcommand.Data = input;
  150. NTTransactRequest request = new NTTransactRequest();
  151. request.Function = subcommand.SubcommandName;
  152. request.Setup = subcommand.GetSetup();
  153. request.TransParameters = subcommand.GetParameters(true);
  154. request.TransData = subcommand.GetData();
  155. request.TotalDataCount = (uint)request.TransData.Length;
  156. request.TotalParameterCount = (uint)request.TransParameters.Length;
  157. request.MaxParameterCount = 0;
  158. request.MaxDataCount = (uint)maxOutputLength;
  159. TrySendMessage(request);
  160. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT);
  161. if (reply != null)
  162. {
  163. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is NTTransactResponse)
  164. {
  165. NTTransactResponse response = (NTTransactResponse)reply.Commands[0];
  166. NTTransactIOCTLResponse subcommandResponse = new NTTransactIOCTLResponse(response.Setup, response.TransData);
  167. output = subcommandResponse.Data;
  168. }
  169. return reply.Header.Status;
  170. }
  171. return NTStatus.STATUS_INVALID_SMB;
  172. }
  173. public NTStatus FsCtlPipeTranscieve(object handle, byte[] input, out byte[] output, int maxOutputLength)
  174. {
  175. output = null;
  176. TransactionTransactNamedPipeRequest subcommand = new TransactionTransactNamedPipeRequest();
  177. subcommand.FID = (ushort)handle;
  178. subcommand.WriteData = input;
  179. TransactionRequest request = new TransactionRequest();
  180. request.Setup = subcommand.GetSetup();
  181. request.TransParameters = subcommand.GetParameters(true);
  182. request.TransData = subcommand.GetData();
  183. request.TotalDataCount = (ushort)request.TransData.Length;
  184. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  185. request.MaxParameterCount = 0;
  186. request.MaxDataCount = (ushort)maxOutputLength;
  187. request.Name = @"\PIPE\";
  188. TrySendMessage(request);
  189. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION);
  190. if (reply != null)
  191. {
  192. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is TransactionResponse)
  193. {
  194. TransactionResponse response = (TransactionResponse)reply.Commands[0];
  195. TransactionTransactNamedPipeResponse subcommandResponse = new TransactionTransactNamedPipeResponse(response.TransData);
  196. output = subcommandResponse.ReadData;
  197. }
  198. return reply.Header.Status;
  199. }
  200. return NTStatus.STATUS_INVALID_SMB;
  201. }
  202. private static ExtendedFileAttributes ToExtendedFileAttributes(FileAttributes fileAttributes)
  203. {
  204. // We only return flags that can be used with NtCreateFile
  205. ExtendedFileAttributes extendedFileAttributes = ExtendedFileAttributes.ReadOnly |
  206. ExtendedFileAttributes.Hidden |
  207. ExtendedFileAttributes.System |
  208. ExtendedFileAttributes.Archive |
  209. ExtendedFileAttributes.Normal |
  210. ExtendedFileAttributes.Temporary |
  211. ExtendedFileAttributes.Offline |
  212. ExtendedFileAttributes.Encrypted;
  213. return (extendedFileAttributes & (ExtendedFileAttributes)fileAttributes);
  214. }
  215. private void TrySendMessage(SMB1Command request)
  216. {
  217. m_client.TrySendMessage(request, m_treeID);
  218. }
  219. private static FileStatus ToFileStatus(CreateDisposition createDisposition)
  220. {
  221. switch (createDisposition)
  222. {
  223. case CreateDisposition.FILE_SUPERSEDE:
  224. return FileStatus.FILE_SUPERSEDED;
  225. case CreateDisposition.FILE_OPEN:
  226. return FileStatus.FILE_OPENED;
  227. case CreateDisposition.FILE_CREATE:
  228. return FileStatus.FILE_CREATED;
  229. case CreateDisposition.FILE_OPEN_IF:
  230. return FileStatus.FILE_OVERWRITTEN;
  231. case CreateDisposition.FILE_OVERWRITE:
  232. return FileStatus.FILE_EXISTS;
  233. case CreateDisposition.FILE_OVERWRITE_IF:
  234. return FileStatus.FILE_DOES_NOT_EXIST;
  235. default:
  236. return FileStatus.FILE_OPENED;
  237. }
  238. }
  239. }
  240. }