NamedPipeStore.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 System.IO;
  10. using SMBLibrary.RPC;
  11. using SMBLibrary.Services;
  12. using Utilities;
  13. namespace SMBLibrary
  14. {
  15. public class NamedPipeStore : INTFileStore
  16. {
  17. private List<RemoteService> m_services;
  18. public NamedPipeStore(List<RemoteService> services)
  19. {
  20. m_services = services;
  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. fileStatus = FileStatus.FILE_DOES_NOT_EXIST;
  25. // It is possible to have a named pipe that does not use RPC (e.g. MS-WSP),
  26. // However this is not currently needed by our implementation.
  27. RemoteService service = GetService(path);
  28. if (service != null)
  29. {
  30. // All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles,
  31. // and provides a separate conduit for client/server communication.
  32. RPCPipeStream stream = new RPCPipeStream(service);
  33. handle = new FileHandle(path, false, stream, false);
  34. fileStatus = FileStatus.FILE_OPENED;
  35. return NTStatus.STATUS_SUCCESS;
  36. }
  37. handle = null;
  38. return NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
  39. }
  40. public NTStatus CloseFile(object handle)
  41. {
  42. FileHandle fileHandle = (FileHandle)handle;
  43. if (fileHandle.Stream != null)
  44. {
  45. fileHandle.Stream.Close();
  46. }
  47. return NTStatus.STATUS_SUCCESS;
  48. }
  49. private RemoteService GetService(string path)
  50. {
  51. if (path.StartsWith(@"\"))
  52. {
  53. path = path.Substring(1);
  54. }
  55. foreach (RemoteService service in m_services)
  56. {
  57. if (String.Equals(path, service.PipeName, StringComparison.OrdinalIgnoreCase))
  58. {
  59. return service;
  60. }
  61. }
  62. return null;
  63. }
  64. public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
  65. {
  66. Stream stream = ((FileHandle)handle).Stream;
  67. data = new byte[maxCount];
  68. int bytesRead = stream.Read(data, 0, maxCount);
  69. if (bytesRead < maxCount)
  70. {
  71. // EOF, we must trim the response data array
  72. data = ByteReader.ReadBytes(data, 0, bytesRead);
  73. }
  74. return NTStatus.STATUS_SUCCESS;
  75. }
  76. public NTStatus WriteFile(out int numberOfBytesWritten, object handle, long offset, byte[] data)
  77. {
  78. Stream stream = ((FileHandle)handle).Stream;
  79. stream.Write(data, 0, data.Length);
  80. numberOfBytesWritten = data.Length;
  81. return NTStatus.STATUS_SUCCESS;
  82. }
  83. public NTStatus FlushFileBuffers(object handle)
  84. {
  85. FileHandle fileHandle = (FileHandle)handle;
  86. if (fileHandle.Stream != null)
  87. {
  88. fileHandle.Stream.Flush();
  89. }
  90. return NTStatus.STATUS_SUCCESS;
  91. }
  92. public NTStatus LockFile(object handle, long byteOffset, long length, bool exclusiveLock)
  93. {
  94. return NTStatus.STATUS_NOT_SUPPORTED;
  95. }
  96. public NTStatus UnlockFile(object handle, long byteOffset, long length)
  97. {
  98. return NTStatus.STATUS_NOT_SUPPORTED;
  99. }
  100. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  101. {
  102. output = null;
  103. if (ctlCode == (uint)IoControlCode.FSCTL_PIPE_WAIT)
  104. {
  105. PipeWaitRequest request;
  106. try
  107. {
  108. request = new PipeWaitRequest(input, 0);
  109. }
  110. catch
  111. {
  112. return NTStatus.STATUS_INVALID_PARAMETER;
  113. }
  114. RemoteService service = GetService(request.Name);
  115. if (service == null)
  116. {
  117. return NTStatus.STATUS_OBJECT_NAME_NOT_FOUND;
  118. }
  119. output = new byte[0];
  120. return NTStatus.STATUS_SUCCESS;
  121. }
  122. else if (ctlCode == (uint)IoControlCode.FSCTL_PIPE_TRANSCEIVE)
  123. {
  124. int numberOfBytesWritten;
  125. NTStatus writeStatus = WriteFile(out numberOfBytesWritten, handle, 0, input);
  126. if (writeStatus != NTStatus.STATUS_SUCCESS)
  127. {
  128. return writeStatus;
  129. }
  130. int messageLength = ((RPCPipeStream)((FileHandle)handle).Stream).MessageLength;
  131. NTStatus readStatus = ReadFile(out output, handle, 0, maxOutputLength);
  132. if (readStatus != NTStatus.STATUS_SUCCESS)
  133. {
  134. return readStatus;
  135. }
  136. if (output.Length < messageLength)
  137. {
  138. return NTStatus.STATUS_BUFFER_OVERFLOW;
  139. }
  140. else
  141. {
  142. return NTStatus.STATUS_SUCCESS;
  143. }
  144. }
  145. return NTStatus.STATUS_NOT_SUPPORTED;
  146. }
  147. public NTStatus QueryDirectory(out List<QueryDirectoryFileInformation> result, object directoryHandle, string fileName, FileInformationClass informationClass)
  148. {
  149. result = null;
  150. return NTStatus.STATUS_NOT_SUPPORTED;
  151. }
  152. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  153. {
  154. switch (informationClass)
  155. {
  156. case FileInformationClass.FileBasicInformation:
  157. {
  158. FileBasicInformation information = new FileBasicInformation();
  159. information.FileAttributes = FileAttributes.Temporary;
  160. result = information;
  161. return NTStatus.STATUS_SUCCESS;
  162. }
  163. case FileInformationClass.FileStandardInformation:
  164. {
  165. FileStandardInformation information = new FileStandardInformation();
  166. information.DeletePending = false;
  167. result = information;
  168. return NTStatus.STATUS_SUCCESS;
  169. }
  170. case FileInformationClass.FileNetworkOpenInformation:
  171. {
  172. FileNetworkOpenInformation information = new FileNetworkOpenInformation();
  173. information.FileAttributes = FileAttributes.Temporary;
  174. result = information;
  175. return NTStatus.STATUS_SUCCESS;
  176. }
  177. default:
  178. result = null;
  179. return NTStatus.STATUS_INVALID_INFO_CLASS;
  180. }
  181. }
  182. public NTStatus SetFileInformation(object handle, FileInformation information)
  183. {
  184. return NTStatus.STATUS_NOT_SUPPORTED;
  185. }
  186. public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
  187. {
  188. result = null;
  189. return NTStatus.STATUS_NOT_SUPPORTED;
  190. }
  191. public NTStatus SetFileSystemInformation(FileSystemInformation information)
  192. {
  193. return NTStatus.STATUS_NOT_SUPPORTED;
  194. }
  195. public NTStatus GetSecurityInformation(out SecurityDescriptor result, object handle, SecurityInformation securityInformation)
  196. {
  197. result = null;
  198. return NTStatus.STATUS_NOT_SUPPORTED;
  199. }
  200. public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
  201. {
  202. return NTStatus.STATUS_NOT_SUPPORTED;
  203. }
  204. public NTStatus NotifyChange(out object ioRequest, object handle, NotifyChangeFilter completionFilter, bool watchTree, int outputBufferSize, OnNotifyChangeCompleted onNotifyChangeCompleted, object context)
  205. {
  206. ioRequest = null;
  207. return NTStatus.STATUS_NOT_SUPPORTED;
  208. }
  209. public NTStatus Cancel(object ioRequest)
  210. {
  211. return NTStatus.STATUS_NOT_SUPPORTED;
  212. }
  213. }
  214. }