Transaction2SubcommandHelper.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 System.IO;
  10. using SMBLibrary.SMB1;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB1
  13. {
  14. internal class Transaction2SubcommandHelper
  15. {
  16. internal static Transaction2FindFirst2Response GetSubcommandResponse(SMB1Header header, uint maxDataCount, Transaction2FindFirst2Request subcommand, ISMBShare share, SMB1ConnectionState state)
  17. {
  18. SMB1Session session = state.GetSession(header.UID);
  19. string fileNamePattern = subcommand.FileName;
  20. if (!fileNamePattern.StartsWith(@"\"))
  21. {
  22. fileNamePattern = @"\" + fileNamePattern;
  23. }
  24. List<QueryDirectoryFileInformation> entries;
  25. FileInformationClass informationClass;
  26. try
  27. {
  28. informationClass = FindInformationHelper.ToFileInformationClass(subcommand.InformationLevel);
  29. }
  30. catch (UnsupportedInformationLevelException)
  31. {
  32. state.LogToServer(Severity.Verbose, "FindFirst2: Unsupported information level: {0}.", subcommand.InformationLevel);
  33. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  34. return null;
  35. }
  36. NTStatus searchStatus = SMB1FileStoreHelper.QueryDirectory(out entries, share.FileStore, fileNamePattern, informationClass, session.SecurityContext);
  37. if (searchStatus != NTStatus.STATUS_SUCCESS)
  38. {
  39. state.LogToServer(Severity.Verbose, "FindFirst2: Searched for '{0}{1}', NTStatus: {2}", share.Name, fileNamePattern, searchStatus.ToString());
  40. header.Status = searchStatus;
  41. return null;
  42. }
  43. // We ignore SearchAttributes
  44. state.LogToServer(Severity.Information, "FindFirst2: Searched for '{0}{1}', found {2} matching entries", share.Name, fileNamePattern, entries.Count);
  45. // [MS-CIFS] If no matching entries are found, the server SHOULD fail the request with STATUS_NO_SUCH_FILE.
  46. if (entries.Count == 0)
  47. {
  48. header.Status = NTStatus.STATUS_NO_SUCH_FILE;
  49. return null;
  50. }
  51. bool returnResumeKeys = (subcommand.Flags & FindFlags.SMB_FIND_RETURN_RESUME_KEYS) > 0;
  52. int entriesToReturn = Math.Min(subcommand.SearchCount, entries.Count);
  53. List<QueryDirectoryFileInformation> segment = entries.GetRange(0, entriesToReturn);
  54. int maxLength = (int)maxDataCount;
  55. FindInformationList findInformationList;
  56. try
  57. {
  58. findInformationList = FindInformationHelper.ToFindInformationList(segment, header.UnicodeFlag, maxLength);
  59. }
  60. catch (UnsupportedInformationLevelException)
  61. {
  62. state.LogToServer(Severity.Verbose, "FindFirst2: Unsupported information level: {0}.", subcommand.InformationLevel);
  63. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  64. return null;
  65. }
  66. int returnCount = findInformationList.Count;
  67. Transaction2FindFirst2Response response = new Transaction2FindFirst2Response();
  68. response.SetFindInformationList(findInformationList, header.UnicodeFlag);
  69. response.EndOfSearch = (returnCount == entries.Count);
  70. // If [..] the search fit within a single response and SMB_FIND_CLOSE_AT_EOS is set in the Flags field,
  71. // or if SMB_FIND_CLOSE_AFTER_REQUEST is set in the request,
  72. // the server SHOULD return a SID field value of zero.
  73. // This indicates that the search has been closed and is no longer active on the server.
  74. if ((response.EndOfSearch && subcommand.CloseAtEndOfSearch) || subcommand.CloseAfterRequest)
  75. {
  76. response.SID = 0;
  77. }
  78. else
  79. {
  80. ushort? searchHandle = session.AddOpenSearch(entries, returnCount);
  81. if (!searchHandle.HasValue)
  82. {
  83. header.Status = NTStatus.STATUS_OS2_NO_MORE_SIDS;
  84. return null;
  85. }
  86. response.SID = searchHandle.Value;
  87. }
  88. return response;
  89. }
  90. internal static Transaction2FindNext2Response GetSubcommandResponse(SMB1Header header, uint maxDataCount, Transaction2FindNext2Request subcommand, ISMBShare share, SMB1ConnectionState state)
  91. {
  92. SMB1Session session = state.GetSession(header.UID);
  93. OpenSearch openSearch = session.GetOpenSearch(subcommand.SID);
  94. if (openSearch == null)
  95. {
  96. state.LogToServer(Severity.Verbose, "FindNext2 failed. Invalid SID.");
  97. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  98. return null;
  99. }
  100. bool returnResumeKeys = (subcommand.Flags & FindFlags.SMB_FIND_RETURN_RESUME_KEYS) > 0;
  101. int maxLength = (int)maxDataCount;
  102. int maxCount = Math.Min(openSearch.Entries.Count - openSearch.EnumerationLocation, subcommand.SearchCount);
  103. List<QueryDirectoryFileInformation> segment = openSearch.Entries.GetRange(openSearch.EnumerationLocation, maxCount);
  104. FindInformationList findInformationList;
  105. try
  106. {
  107. findInformationList = FindInformationHelper.ToFindInformationList(segment, header.UnicodeFlag, maxLength);
  108. }
  109. catch (UnsupportedInformationLevelException)
  110. {
  111. state.LogToServer(Severity.Verbose, "FindNext2: Unsupported information level: {0}.", subcommand.InformationLevel);
  112. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  113. return null;
  114. }
  115. int returnCount = findInformationList.Count;
  116. Transaction2FindNext2Response response = new Transaction2FindNext2Response();
  117. response.SetFindInformationList(findInformationList, header.UnicodeFlag);
  118. openSearch.EnumerationLocation += returnCount;
  119. response.EndOfSearch = (openSearch.EnumerationLocation == openSearch.Entries.Count);
  120. if (response.EndOfSearch)
  121. {
  122. session.RemoveOpenSearch(subcommand.SID);
  123. }
  124. return response;
  125. }
  126. internal static Transaction2QueryFSInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2QueryFSInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  127. {
  128. SMB1Session session = state.GetSession(header.UID);
  129. if (share is FileSystemShare)
  130. {
  131. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  132. {
  133. state.LogToServer(Severity.Verbose, "QueryFileSystemInformation on '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  134. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  135. return null;
  136. }
  137. }
  138. Transaction2QueryFSInformationResponse response = new Transaction2QueryFSInformationResponse();
  139. if (subcommand.IsPassthroughInformationLevel)
  140. {
  141. FileSystemInformation fileSystemInfo;
  142. NTStatus status = share.FileStore.GetFileSystemInformation(out fileSystemInfo, subcommand.FileSystemInformationClass);
  143. if (status != NTStatus.STATUS_SUCCESS)
  144. {
  145. state.LogToServer(Severity.Verbose, "GetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: {2}", share.Name, subcommand.FileSystemInformationClass, status);
  146. header.Status = status;
  147. return null;
  148. }
  149. state.LogToServer(Severity.Information, "GetFileSystemInformation on '{0}' succeeded. Information class: {1}", share.Name, subcommand.FileSystemInformationClass);
  150. response.SetFileSystemInformation(fileSystemInfo);
  151. }
  152. else
  153. {
  154. QueryFSInformation queryFSInformation;
  155. NTStatus status = SMB1FileStoreHelper.GetFileSystemInformation(out queryFSInformation, share.FileStore, subcommand.QueryFSInformationLevel);
  156. if (status != NTStatus.STATUS_SUCCESS)
  157. {
  158. state.LogToServer(Severity.Verbose, "GetFileSystemInformation on '{0}' failed. Information level: {1}, NTStatus: {2}", share.Name, subcommand.QueryFSInformationLevel, status);
  159. header.Status = status;
  160. return null;
  161. }
  162. state.LogToServer(Severity.Information, "GetFileSystemInformation on '{0}' succeeded. Information level: {1}", share.Name, subcommand.QueryFSInformationLevel);
  163. response.SetQueryFSInformation(queryFSInformation, header.UnicodeFlag);
  164. }
  165. return response;
  166. }
  167. internal static Transaction2SetFSInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2SetFSInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  168. {
  169. SMB1Session session = state.GetSession(header.UID);
  170. if (share is FileSystemShare)
  171. {
  172. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, @"\"))
  173. {
  174. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  175. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  176. return null;
  177. }
  178. }
  179. if (!subcommand.IsPassthroughInformationLevel)
  180. {
  181. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. Not a pass-through information level.", share.Name);
  182. header.Status = NTStatus.STATUS_NOT_SUPPORTED;
  183. return null;
  184. }
  185. FileSystemInformation fileSystemInfo;
  186. try
  187. {
  188. fileSystemInfo = FileSystemInformation.GetFileSystemInformation(subcommand.InformationBytes, 0, subcommand.FileSystemInformationClass);
  189. }
  190. catch (UnsupportedInformationLevelException)
  191. {
  192. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: STATUS_OS2_INVALID_LEVEL.", share.Name, subcommand.FileSystemInformationClass);
  193. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  194. return null;
  195. }
  196. catch (Exception)
  197. {
  198. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: STATUS_INVALID_PARAMETER.", share.Name, subcommand.FileSystemInformationClass);
  199. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  200. return null;
  201. }
  202. NTStatus status = share.FileStore.SetFileSystemInformation(fileSystemInfo);
  203. if (status != NTStatus.STATUS_SUCCESS)
  204. {
  205. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: {2}.", share.Name, subcommand.FileSystemInformationClass, status);
  206. header.Status = status;
  207. return null;
  208. }
  209. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' succeeded. Information class: {1}.", share.Name, subcommand.FileSystemInformationClass);
  210. return new Transaction2SetFSInformationResponse();
  211. }
  212. internal static Transaction2QueryPathInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2QueryPathInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  213. {
  214. SMB1Session session = state.GetSession(header.UID);
  215. string path = subcommand.FileName;
  216. if (!path.StartsWith(@"\"))
  217. {
  218. path = @"\" + path;
  219. }
  220. if (share is FileSystemShare)
  221. {
  222. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, path))
  223. {
  224. state.LogToServer(Severity.Verbose, "QueryPathInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  225. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  226. return null;
  227. }
  228. }
  229. Transaction2QueryPathInformationResponse response = new Transaction2QueryPathInformationResponse();
  230. if (subcommand.IsPassthroughInformationLevel && subcommand.FileInformationClass != FileInformationClass.FileAllInformation)
  231. {
  232. FileInformation fileInfo;
  233. NTStatus status = SMB1FileStoreHelper.GetFileInformation(out fileInfo, share.FileStore, path, subcommand.FileInformationClass, session.SecurityContext);
  234. if (status != NTStatus.STATUS_SUCCESS)
  235. {
  236. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: {3}", share.Name, path, subcommand.FileInformationClass, status);
  237. header.Status = status;
  238. return null;
  239. }
  240. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information class: {2}", share.Name, path, subcommand.FileInformationClass);
  241. response.SetFileInformation(fileInfo);
  242. }
  243. else
  244. {
  245. // The FILE_ALL_INFORMATION structure described in [MS-FSCC], is NOT used by [MS-SMB]
  246. if (subcommand.IsPassthroughInformationLevel && subcommand.FileInformationClass == FileInformationClass.FileAllInformation)
  247. {
  248. subcommand.QueryInformationLevel = QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO;
  249. }
  250. QueryInformation queryInformation;
  251. NTStatus status = SMB1FileStoreHelper.GetFileInformation(out queryInformation, share.FileStore, path, subcommand.QueryInformationLevel, session.SecurityContext);
  252. if (status != NTStatus.STATUS_SUCCESS)
  253. {
  254. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: {3}", share.Name, path, subcommand.QueryInformationLevel, status);
  255. header.Status = status;
  256. return null;
  257. }
  258. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information level: {2}", share.Name, path, subcommand.QueryInformationLevel);
  259. response.SetQueryInformation(queryInformation);
  260. }
  261. return response;
  262. }
  263. internal static Transaction2QueryFileInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2QueryFileInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  264. {
  265. SMB1Session session = state.GetSession(header.UID);
  266. OpenFileObject openFile = session.GetOpenFileObject(subcommand.FID);
  267. if (openFile == null)
  268. {
  269. state.LogToServer(Severity.Verbose, "QueryFileInformation failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, subcommand.FID);
  270. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  271. return null;
  272. }
  273. if (share is FileSystemShare)
  274. {
  275. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  276. {
  277. state.LogToServer(Severity.Verbose, "QueryFileInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  278. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  279. return null;
  280. }
  281. }
  282. Transaction2QueryFileInformationResponse response = new Transaction2QueryFileInformationResponse();
  283. if (subcommand.IsPassthroughInformationLevel && subcommand.FileInformationClass != FileInformationClass.FileAllInformation)
  284. {
  285. FileInformation fileInfo;
  286. NTStatus status = share.FileStore.GetFileInformation(out fileInfo, openFile.Handle, subcommand.FileInformationClass);
  287. if (status != NTStatus.STATUS_SUCCESS)
  288. {
  289. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: {3}. (FID: {4})", share.Name, openFile.Path, subcommand.FileInformationClass, status, subcommand.FID);
  290. header.Status = status;
  291. return null;
  292. }
  293. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information class: {2}. (FID: {3})", share.Name, openFile.Path, subcommand.FileInformationClass, subcommand.FID);
  294. response.SetFileInformation(fileInfo);
  295. }
  296. else
  297. {
  298. // The FILE_ALL_INFORMATION structure described in [MS-FSCC], is NOT used by [MS-SMB]
  299. if (subcommand.IsPassthroughInformationLevel && subcommand.FileInformationClass == FileInformationClass.FileAllInformation)
  300. {
  301. subcommand.QueryInformationLevel = QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO;
  302. }
  303. QueryInformation queryInformation;
  304. NTStatus status = SMB1FileStoreHelper.GetFileInformation(out queryInformation, share.FileStore, openFile.Handle, subcommand.QueryInformationLevel);
  305. if (status != NTStatus.STATUS_SUCCESS)
  306. {
  307. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: {3}. (FID: {4})", share.Name, openFile.Path, subcommand.QueryInformationLevel, status, subcommand.FID);
  308. header.Status = status;
  309. return null;
  310. }
  311. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information level: {2}. (FID: {3})", share.Name, openFile.Path, subcommand.QueryInformationLevel, subcommand.FID);
  312. response.SetQueryInformation(queryInformation);
  313. }
  314. return response;
  315. }
  316. internal static Transaction2SetFileInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2SetFileInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  317. {
  318. SMB1Session session = state.GetSession(header.UID);
  319. OpenFileObject openFile = session.GetOpenFileObject(subcommand.FID);
  320. if (openFile == null)
  321. {
  322. state.LogToServer(Severity.Verbose, "SetFileInformation failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, subcommand.FID);
  323. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  324. return null;
  325. }
  326. if (share is FileSystemShare)
  327. {
  328. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  329. {
  330. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  331. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  332. return null;
  333. }
  334. }
  335. if (subcommand.IsPassthroughInformationLevel)
  336. {
  337. FileInformation fileInfo;
  338. try
  339. {
  340. fileInfo = FileInformation.GetFileInformation(subcommand.InformationBytes, 0, subcommand.FileInformationClass);
  341. }
  342. catch (UnsupportedInformationLevelException)
  343. {
  344. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_OS2_INVALID_LEVEL.", share.Name, openFile.Path, subcommand.FileInformationClass);
  345. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  346. return null;
  347. }
  348. catch (Exception)
  349. {
  350. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_INVALID_PARAMETER.", share.Name, openFile.Path, subcommand.FileInformationClass);
  351. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  352. return null;
  353. }
  354. NTStatus status = share.FileStore.SetFileInformation(openFile.Handle, fileInfo);
  355. if (status != NTStatus.STATUS_SUCCESS)
  356. {
  357. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: {3}. (FID: {4})", share.Name, openFile.Path, subcommand.FileInformationClass, status, subcommand.FID);
  358. header.Status = status;
  359. return null;
  360. }
  361. state.LogToServer(Severity.Information, "SetFileInformation on '{0}{1}' succeeded. Information class: {2}. (FID: {3})", share.Name, openFile.Path, subcommand.FileInformationClass, subcommand.FID);
  362. }
  363. else
  364. {
  365. SetInformation information;
  366. try
  367. {
  368. information = SetInformation.GetSetInformation(subcommand.InformationBytes, subcommand.SetInformationLevel);
  369. }
  370. catch (UnsupportedInformationLevelException)
  371. {
  372. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: STATUS_OS2_INVALID_LEVEL.", share.Name, openFile.Path, subcommand.SetInformationLevel);
  373. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  374. return null;
  375. }
  376. catch (Exception)
  377. {
  378. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: STATUS_INVALID_PARAMETER.", share.Name, openFile.Path, subcommand.SetInformationLevel);
  379. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  380. return null;
  381. }
  382. NTStatus status = SMB1FileStoreHelper.SetFileInformation(share.FileStore, openFile.Handle, information);
  383. if (status != NTStatus.STATUS_SUCCESS)
  384. {
  385. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: {3}. (FID: {4})", share.Name, openFile.Path, subcommand.SetInformationLevel, status, subcommand.FID);
  386. header.Status = status;
  387. return null;
  388. }
  389. state.LogToServer(Severity.Information, "SetFileInformation on '{0}{1}' succeeded. Information level: {2}. (FID: {3})", share.Name, openFile.Path, subcommand.SetInformationLevel, subcommand.FID);
  390. }
  391. Transaction2SetFileInformationResponse response = new Transaction2SetFileInformationResponse();
  392. return response;
  393. }
  394. }
  395. }