SMB1FileStore.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* Copyright (C) 2014-2019 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 : ISMBFileStore
  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.Impersonation;
  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 QueryDirectory(out List<FindInformation> result, string fileName, FindInformationLevel informationLevel)
  119. {
  120. result = null;
  121. int maxOutputLength = 4096;
  122. Transaction2FindFirst2Request subcommand = new Transaction2FindFirst2Request();
  123. subcommand.SearchAttributes = SMBFileAttributes.Hidden | SMBFileAttributes.System | SMBFileAttributes.Directory;
  124. subcommand.SearchCount = UInt16.MaxValue;
  125. subcommand.Flags = FindFlags.SMB_FIND_CLOSE_AT_EOS;
  126. subcommand.InformationLevel = informationLevel;
  127. subcommand.FileName = fileName;
  128. Transaction2Request request = new Transaction2Request();
  129. request.Setup = subcommand.GetSetup();
  130. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  131. request.TransData = subcommand.GetData(m_client.Unicode);
  132. request.TotalDataCount = (ushort)request.TransData.Length;
  133. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  134. request.MaxParameterCount = Transaction2FindFirst2Response.ParametersLength;
  135. request.MaxDataCount = (ushort)maxOutputLength;
  136. TrySendMessage(request);
  137. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  138. if (reply != null)
  139. {
  140. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  141. {
  142. result = new List<FindInformation>();
  143. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  144. Transaction2FindFirst2Response subcommandResponse = new Transaction2FindFirst2Response(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
  145. FindInformationList findInformationList = subcommandResponse.GetFindInformationList(subcommand.InformationLevel, reply.Header.UnicodeFlag);
  146. result.AddRange(findInformationList);
  147. bool endOfSearch = subcommandResponse.EndOfSearch;
  148. while (!endOfSearch)
  149. {
  150. Transaction2FindNext2Request nextSubcommand = new Transaction2FindNext2Request();
  151. nextSubcommand.SID = subcommandResponse.SID;
  152. nextSubcommand.SearchCount = UInt16.MaxValue;
  153. nextSubcommand.Flags = FindFlags.SMB_FIND_CLOSE_AT_EOS | FindFlags.SMB_FIND_CONTINUE_FROM_LAST;
  154. nextSubcommand.InformationLevel = informationLevel;
  155. nextSubcommand.FileName = fileName;
  156. request = new Transaction2Request();
  157. request.Setup = nextSubcommand.GetSetup();
  158. request.TransParameters = nextSubcommand.GetParameters(m_client.Unicode);
  159. request.TransData = nextSubcommand.GetData(m_client.Unicode);
  160. request.TotalDataCount = (ushort)request.TransData.Length;
  161. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  162. request.MaxParameterCount = Transaction2FindNext2Response.ParametersLength;
  163. request.MaxDataCount = (ushort)maxOutputLength;
  164. TrySendMessage(request);
  165. reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  166. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  167. {
  168. response = (Transaction2Response)reply.Commands[0];
  169. Transaction2FindNext2Response nextSubcommandResponse = new Transaction2FindNext2Response(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
  170. findInformationList = nextSubcommandResponse.GetFindInformationList(subcommand.InformationLevel, reply.Header.UnicodeFlag);
  171. result.AddRange(findInformationList);
  172. endOfSearch = nextSubcommandResponse.EndOfSearch;
  173. }
  174. else
  175. {
  176. endOfSearch = true;
  177. }
  178. }
  179. }
  180. return reply.Header.Status;
  181. }
  182. return NTStatus.STATUS_INVALID_SMB;
  183. }
  184. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  185. {
  186. result = null;
  187. if (m_client.InfoLevelPassthrough)
  188. {
  189. int maxOutputLength = 4096;
  190. Transaction2QueryFileInformationRequest subcommand = new Transaction2QueryFileInformationRequest();
  191. subcommand.FID = (ushort)handle;
  192. subcommand.FileInformationClass = informationClass;
  193. Transaction2Request request = new Transaction2Request();
  194. request.Setup = subcommand.GetSetup();
  195. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  196. request.TransData = subcommand.GetData(m_client.Unicode);
  197. request.TotalDataCount = (ushort)request.TransData.Length;
  198. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  199. request.MaxParameterCount = Transaction2QueryFileInformationResponse.ParametersLength;
  200. request.MaxDataCount = (ushort)maxOutputLength;
  201. TrySendMessage(request);
  202. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  203. if (reply != null)
  204. {
  205. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  206. {
  207. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  208. Transaction2QueryFileInformationResponse subcommandResponse = new Transaction2QueryFileInformationResponse(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
  209. if (informationClass == FileInformationClass.FileAllInformation)
  210. {
  211. // Windows implementations return SMB_QUERY_FILE_ALL_INFO when a client specifies native NT passthrough level "FileAllInformation".
  212. QueryInformation queryFileAllInfo = subcommandResponse.GetQueryInformation(QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO);
  213. result = QueryInformationHelper.ToFileInformation(queryFileAllInfo);
  214. }
  215. else
  216. {
  217. result = subcommandResponse.GetFileInformation(informationClass);
  218. }
  219. }
  220. return reply.Header.Status;
  221. }
  222. return NTStatus.STATUS_INVALID_SMB;
  223. }
  224. else
  225. {
  226. QueryInformationLevel informationLevel = QueryInformationHelper.ToFileInformationLevel(informationClass);
  227. QueryInformation queryInformation;
  228. NTStatus status = GetFileInformation(out queryInformation, handle, informationLevel);
  229. if (status == NTStatus.STATUS_SUCCESS)
  230. {
  231. result = QueryInformationHelper.ToFileInformation(queryInformation);
  232. }
  233. return status;
  234. }
  235. }
  236. public NTStatus GetFileInformation(out QueryInformation result, object handle, QueryInformationLevel informationLevel)
  237. {
  238. result = null;
  239. int maxOutputLength = 4096;
  240. Transaction2QueryFileInformationRequest subcommand = new Transaction2QueryFileInformationRequest();
  241. subcommand.FID = (ushort)handle;
  242. subcommand.QueryInformationLevel = informationLevel;
  243. Transaction2Request request = new Transaction2Request();
  244. request.Setup = subcommand.GetSetup();
  245. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  246. request.TransData = subcommand.GetData(m_client.Unicode);
  247. request.TotalDataCount = (ushort)request.TransData.Length;
  248. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  249. request.MaxParameterCount = Transaction2QueryFileInformationResponse.ParametersLength;
  250. request.MaxDataCount = (ushort)maxOutputLength;
  251. TrySendMessage(request);
  252. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  253. if (reply != null)
  254. {
  255. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  256. {
  257. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  258. Transaction2QueryFileInformationResponse subcommandResponse = new Transaction2QueryFileInformationResponse(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
  259. result = subcommandResponse.GetQueryInformation(informationLevel);
  260. }
  261. return reply.Header.Status;
  262. }
  263. return NTStatus.STATUS_INVALID_SMB;
  264. }
  265. public NTStatus SetFileInformation(object handle, FileInformation information)
  266. {
  267. if (m_client.InfoLevelPassthrough)
  268. {
  269. if (information is FileRenameInformationType2)
  270. {
  271. FileRenameInformationType1 informationType1 = new FileRenameInformationType1();
  272. informationType1.FileName = ((FileRenameInformationType2)information).FileName;
  273. informationType1.ReplaceIfExists = ((FileRenameInformationType2)information).ReplaceIfExists;
  274. informationType1.RootDirectory = (uint)((FileRenameInformationType2)information).RootDirectory;
  275. information = informationType1;
  276. }
  277. int maxOutputLength = 4096;
  278. Transaction2SetFileInformationRequest subcommand = new Transaction2SetFileInformationRequest();
  279. subcommand.FID = (ushort)handle;
  280. subcommand.SetInformation(information);
  281. Transaction2Request request = new Transaction2Request();
  282. request.Setup = subcommand.GetSetup();
  283. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  284. request.TransData = subcommand.GetData(m_client.Unicode);
  285. request.TotalDataCount = (ushort)request.TransData.Length;
  286. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  287. request.MaxParameterCount = Transaction2SetFileInformationResponse.ParametersLength;
  288. request.MaxDataCount = (ushort)maxOutputLength;
  289. TrySendMessage(request);
  290. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  291. if (reply != null)
  292. {
  293. return reply.Header.Status;
  294. }
  295. return NTStatus.STATUS_INVALID_SMB;
  296. }
  297. else
  298. {
  299. throw new NotSupportedException("Server does not support InfoLevelPassthrough");
  300. }
  301. }
  302. public NTStatus SetFileInformation(object handle, SetInformation information)
  303. {
  304. int maxOutputLength = 4096;
  305. Transaction2SetFileInformationRequest subcommand = new Transaction2SetFileInformationRequest();
  306. subcommand.FID = (ushort)handle;
  307. subcommand.SetInformation(information);
  308. Transaction2Request request = new Transaction2Request();
  309. request.Setup = subcommand.GetSetup();
  310. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  311. request.TransData = subcommand.GetData(m_client.Unicode);
  312. request.TotalDataCount = (ushort)request.TransData.Length;
  313. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  314. request.MaxParameterCount = Transaction2SetFileInformationResponse.ParametersLength;
  315. request.MaxDataCount = (ushort)maxOutputLength;
  316. TrySendMessage(request);
  317. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  318. if (reply != null)
  319. {
  320. return reply.Header.Status;
  321. }
  322. return NTStatus.STATUS_INVALID_SMB;
  323. }
  324. public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
  325. {
  326. if (m_client.InfoLevelPassthrough)
  327. {
  328. result = null;
  329. int maxOutputLength = 4096;
  330. Transaction2QueryFSInformationRequest subcommand = new Transaction2QueryFSInformationRequest();
  331. subcommand.FileSystemInformationClass = informationClass;
  332. Transaction2Request request = new Transaction2Request();
  333. request.Setup = subcommand.GetSetup();
  334. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  335. request.TransData = subcommand.GetData(m_client.Unicode);
  336. request.TotalDataCount = (ushort)request.TransData.Length;
  337. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  338. request.MaxParameterCount = Transaction2QueryFSInformationResponse.ParametersLength;
  339. request.MaxDataCount = (ushort)maxOutputLength;
  340. TrySendMessage(request);
  341. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  342. if (reply != null)
  343. {
  344. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  345. {
  346. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  347. Transaction2QueryFSInformationResponse subcommandResponse = new Transaction2QueryFSInformationResponse(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
  348. result = subcommandResponse.GetFileSystemInformation(informationClass);
  349. }
  350. return reply.Header.Status;
  351. }
  352. return NTStatus.STATUS_INVALID_SMB;
  353. }
  354. else
  355. {
  356. throw new NotSupportedException("Server does not support InfoLevelPassthrough");
  357. }
  358. }
  359. public NTStatus GetFileSystemInformation(out QueryFSInformation result, QueryFSInformationLevel informationLevel)
  360. {
  361. result = null;
  362. int maxOutputLength = 4096;
  363. Transaction2QueryFSInformationRequest subcommand = new Transaction2QueryFSInformationRequest();
  364. subcommand.QueryFSInformationLevel = informationLevel;
  365. Transaction2Request request = new Transaction2Request();
  366. request.Setup = subcommand.GetSetup();
  367. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  368. request.TransData = subcommand.GetData(m_client.Unicode);
  369. request.TotalDataCount = (ushort)request.TransData.Length;
  370. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  371. request.MaxParameterCount = Transaction2QueryFSInformationResponse.ParametersLength;
  372. request.MaxDataCount = (ushort)maxOutputLength;
  373. TrySendMessage(request);
  374. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION2);
  375. if (reply != null)
  376. {
  377. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is Transaction2Response)
  378. {
  379. Transaction2Response response = (Transaction2Response)reply.Commands[0];
  380. Transaction2QueryFSInformationResponse subcommandResponse = new Transaction2QueryFSInformationResponse(response.TransParameters, response.TransData, reply.Header.UnicodeFlag);
  381. result = subcommandResponse.GetQueryFSInformation(informationLevel, reply.Header.UnicodeFlag);
  382. }
  383. return reply.Header.Status;
  384. }
  385. return NTStatus.STATUS_INVALID_SMB;
  386. }
  387. public NTStatus SetFileSystemInformation(FileSystemInformation information)
  388. {
  389. throw new NotImplementedException();
  390. }
  391. public NTStatus GetSecurityInformation(out SecurityDescriptor result, object handle, SecurityInformation securityInformation)
  392. {
  393. result = null;
  394. int maxOutputLength = 4096;
  395. NTTransactQuerySecurityDescriptorRequest subcommand = new NTTransactQuerySecurityDescriptorRequest();
  396. subcommand.FID = (ushort)handle;
  397. subcommand.SecurityInfoFields = securityInformation;
  398. NTTransactRequest request = new NTTransactRequest();
  399. request.Function = subcommand.SubcommandName;
  400. request.Setup = subcommand.GetSetup();
  401. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  402. request.TransData = subcommand.GetData();
  403. request.TotalDataCount = (uint)request.TransData.Length;
  404. request.TotalParameterCount = (uint)request.TransParameters.Length;
  405. request.MaxParameterCount = NTTransactQuerySecurityDescriptorResponse.ParametersLength;
  406. request.MaxDataCount = (uint)maxOutputLength;
  407. TrySendMessage(request);
  408. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT);
  409. if (reply != null)
  410. {
  411. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is NTTransactResponse)
  412. {
  413. NTTransactResponse response = (NTTransactResponse)reply.Commands[0];
  414. NTTransactQuerySecurityDescriptorResponse subcommandResponse = new NTTransactQuerySecurityDescriptorResponse(response.TransParameters, response.TransData);
  415. result = subcommandResponse.SecurityDescriptor;
  416. }
  417. return reply.Header.Status;
  418. }
  419. return NTStatus.STATUS_INVALID_SMB;
  420. }
  421. public NTStatus SetSecurityInformation(object handle, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
  422. {
  423. return NTStatus.STATUS_NOT_SUPPORTED;
  424. }
  425. public NTStatus NotifyChange(out object ioRequest, object handle, NotifyChangeFilter completionFilter, bool watchTree, int outputBufferSize, OnNotifyChangeCompleted onNotifyChangeCompleted, object context)
  426. {
  427. throw new NotImplementedException();
  428. }
  429. public NTStatus Cancel(object ioRequest)
  430. {
  431. throw new NotImplementedException();
  432. }
  433. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  434. {
  435. if ((IoControlCode)ctlCode == IoControlCode.FSCTL_PIPE_TRANSCEIVE)
  436. {
  437. return FsCtlPipeTranscieve(handle, input, out output, maxOutputLength);
  438. }
  439. output = null;
  440. NTTransactIOCTLRequest subcommand = new NTTransactIOCTLRequest();
  441. subcommand.FID = (ushort)handle;
  442. subcommand.FunctionCode = ctlCode;
  443. subcommand.IsFsctl = true;
  444. subcommand.Data = input;
  445. NTTransactRequest request = new NTTransactRequest();
  446. request.Function = subcommand.SubcommandName;
  447. request.Setup = subcommand.GetSetup();
  448. request.TransParameters = subcommand.GetParameters(m_client.Unicode);
  449. request.TransData = subcommand.GetData();
  450. request.TotalDataCount = (uint)request.TransData.Length;
  451. request.TotalParameterCount = (uint)request.TransParameters.Length;
  452. request.MaxParameterCount = NTTransactIOCTLResponse.ParametersLength;
  453. request.MaxDataCount = (uint)maxOutputLength;
  454. TrySendMessage(request);
  455. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_NT_TRANSACT);
  456. if (reply != null)
  457. {
  458. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is NTTransactResponse)
  459. {
  460. NTTransactResponse response = (NTTransactResponse)reply.Commands[0];
  461. NTTransactIOCTLResponse subcommandResponse = new NTTransactIOCTLResponse(response.Setup, response.TransData);
  462. output = subcommandResponse.Data;
  463. }
  464. return reply.Header.Status;
  465. }
  466. return NTStatus.STATUS_INVALID_SMB;
  467. }
  468. public NTStatus FsCtlPipeTranscieve(object handle, byte[] input, out byte[] output, int maxOutputLength)
  469. {
  470. output = null;
  471. TransactionTransactNamedPipeRequest subcommand = new TransactionTransactNamedPipeRequest();
  472. subcommand.FID = (ushort)handle;
  473. subcommand.WriteData = input;
  474. TransactionRequest request = new TransactionRequest();
  475. request.Setup = subcommand.GetSetup();
  476. request.TransParameters = subcommand.GetParameters();
  477. request.TransData = subcommand.GetData(m_client.Unicode);
  478. request.TotalDataCount = (ushort)request.TransData.Length;
  479. request.TotalParameterCount = (ushort)request.TransParameters.Length;
  480. request.MaxParameterCount = TransactionTransactNamedPipeResponse.ParametersLength;
  481. request.MaxDataCount = (ushort)maxOutputLength;
  482. request.Name = @"\PIPE\";
  483. TrySendMessage(request);
  484. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TRANSACTION);
  485. if (reply != null)
  486. {
  487. if (reply.Header.Status == NTStatus.STATUS_SUCCESS && reply.Commands[0] is TransactionResponse)
  488. {
  489. TransactionResponse response = (TransactionResponse)reply.Commands[0];
  490. TransactionTransactNamedPipeResponse subcommandResponse = new TransactionTransactNamedPipeResponse(response.TransData);
  491. output = subcommandResponse.ReadData;
  492. }
  493. return reply.Header.Status;
  494. }
  495. return NTStatus.STATUS_INVALID_SMB;
  496. }
  497. public NTStatus Disconnect()
  498. {
  499. TreeDisconnectRequest request = new TreeDisconnectRequest();
  500. TrySendMessage(request);
  501. SMB1Message reply = m_client.WaitForMessage(CommandName.SMB_COM_TREE_DISCONNECT);
  502. if (reply != null)
  503. {
  504. return reply.Header.Status;
  505. }
  506. return NTStatus.STATUS_INVALID_SMB;
  507. }
  508. private void TrySendMessage(SMB1Command request)
  509. {
  510. m_client.TrySendMessage(request, m_treeID);
  511. }
  512. public uint MaxReadSize
  513. {
  514. get
  515. {
  516. return m_client.MaxReadSize;
  517. }
  518. }
  519. public uint MaxWriteSize
  520. {
  521. get
  522. {
  523. return m_client.MaxWriteSize;
  524. }
  525. }
  526. private static ExtendedFileAttributes ToExtendedFileAttributes(FileAttributes fileAttributes)
  527. {
  528. // We only return flags that can be used with NtCreateFile
  529. ExtendedFileAttributes extendedFileAttributes = ExtendedFileAttributes.ReadOnly |
  530. ExtendedFileAttributes.Hidden |
  531. ExtendedFileAttributes.System |
  532. ExtendedFileAttributes.Archive |
  533. ExtendedFileAttributes.Normal |
  534. ExtendedFileAttributes.Temporary |
  535. ExtendedFileAttributes.Offline |
  536. ExtendedFileAttributes.Encrypted;
  537. return (extendedFileAttributes & (ExtendedFileAttributes)fileAttributes);
  538. }
  539. private static FileStatus ToFileStatus(CreateDisposition createDisposition)
  540. {
  541. switch (createDisposition)
  542. {
  543. case CreateDisposition.FILE_SUPERSEDE:
  544. return FileStatus.FILE_SUPERSEDED;
  545. case CreateDisposition.FILE_OPEN:
  546. return FileStatus.FILE_OPENED;
  547. case CreateDisposition.FILE_CREATE:
  548. return FileStatus.FILE_CREATED;
  549. case CreateDisposition.FILE_OPEN_IF:
  550. return FileStatus.FILE_OVERWRITTEN;
  551. case CreateDisposition.FILE_OVERWRITE:
  552. return FileStatus.FILE_EXISTS;
  553. case CreateDisposition.FILE_OVERWRITE_IF:
  554. return FileStatus.FILE_DOES_NOT_EXIST;
  555. default:
  556. return FileStatus.FILE_OPENED;
  557. }
  558. }
  559. }
  560. }