Transaction2SubcommandHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 System.Text;
  11. using SMBLibrary.SMB1;
  12. using Utilities;
  13. namespace SMBLibrary.Server.SMB1
  14. {
  15. internal class Transaction2SubcommandHelper
  16. {
  17. internal static Transaction2FindFirst2Response GetSubcommandResponse(SMB1Header header, Transaction2FindFirst2Request subcommand, ISMBShare share, SMB1ConnectionState state)
  18. {
  19. SMB1Session session = state.GetSession(header.UID);
  20. string fileNamePattern = subcommand.FileName;
  21. List<QueryDirectoryFileInformation> entries;
  22. FileInformationClass informationClass;
  23. try
  24. {
  25. informationClass = GetFileInformationClass(subcommand.InformationLevel);
  26. }
  27. catch (UnsupportedInformationLevelException)
  28. {
  29. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  30. return null;
  31. }
  32. NTStatus searchStatus = SMB1FileStoreHelper.QueryDirectory(out entries, share.FileStore, fileNamePattern, informationClass, session.SecurityContext);
  33. if (searchStatus != NTStatus.STATUS_SUCCESS)
  34. {
  35. state.LogToServer(Severity.Verbose, "FindFirst2: Searched for '{0}{1}', NTStatus: {2}", share.Name, fileNamePattern, searchStatus.ToString());
  36. header.Status = searchStatus;
  37. return null;
  38. }
  39. // We ignore SearchAttributes
  40. state.LogToServer(Severity.Information, "FindFirst2: Searched for '{0}{1}', found {2} matching entries", share.Name, fileNamePattern, entries.Count);
  41. // [MS-CIFS] If no matching entries are found, the server SHOULD fail the request with STATUS_NO_SUCH_FILE.
  42. if (entries.Count == 0)
  43. {
  44. header.Status = NTStatus.STATUS_NO_SUCH_FILE;
  45. return null;
  46. }
  47. bool returnResumeKeys = (subcommand.Flags & FindFlags.SMB_FIND_RETURN_RESUME_KEYS) > 0;
  48. int entriesToReturn = Math.Min(subcommand.SearchCount, entries.Count);
  49. List<QueryDirectoryFileInformation> segment = entries.GetRange(0, entriesToReturn);
  50. int maxLength = (int)state.GetMaxDataCount(header.PID).Value;
  51. FindInformationList findInformationList;
  52. try
  53. {
  54. findInformationList = SMB1FileStoreHelper.GetFindInformationList(segment, subcommand.InformationLevel, header.UnicodeFlag, returnResumeKeys, maxLength);
  55. }
  56. catch (UnsupportedInformationLevelException)
  57. {
  58. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  59. return null;
  60. }
  61. int returnCount = findInformationList.Count;
  62. Transaction2FindFirst2Response response = new Transaction2FindFirst2Response();
  63. response.SetFindInformationList(findInformationList, header.UnicodeFlag);
  64. response.EndOfSearch = (returnCount == entries.Count);
  65. // If [..] the search fit within a single response and SMB_FIND_CLOSE_AT_EOS is set in the Flags field,
  66. // or if SMB_FIND_CLOSE_AFTER_REQUEST is set in the request,
  67. // the server SHOULD return a SID field value of zero.
  68. // This indicates that the search has been closed and is no longer active on the server.
  69. if ((response.EndOfSearch && subcommand.CloseAtEndOfSearch) || subcommand.CloseAfterRequest)
  70. {
  71. response.SID = 0;
  72. }
  73. else
  74. {
  75. ushort? searchHandle = session.AddOpenSearch(entries, returnCount);
  76. if (!searchHandle.HasValue)
  77. {
  78. header.Status = NTStatus.STATUS_OS2_NO_MORE_SIDS;
  79. return null;
  80. }
  81. response.SID = searchHandle.Value;
  82. }
  83. return response;
  84. }
  85. internal static Transaction2FindNext2Response GetSubcommandResponse(SMB1Header header, Transaction2FindNext2Request subcommand, ISMBShare share, SMB1ConnectionState state)
  86. {
  87. SMB1Session session = state.GetSession(header.UID);
  88. OpenSearch openSearch = session.GetOpenSearch(subcommand.SID);
  89. if (openSearch == null)
  90. {
  91. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  92. return null;
  93. }
  94. bool returnResumeKeys = (subcommand.Flags & FindFlags.SMB_FIND_RETURN_RESUME_KEYS) > 0;
  95. int maxLength = (int)state.GetMaxDataCount(header.PID).Value;
  96. int maxCount = Math.Min(openSearch.Entries.Count - openSearch.EnumerationLocation, subcommand.SearchCount);
  97. List<QueryDirectoryFileInformation> segment = openSearch.Entries.GetRange(openSearch.EnumerationLocation, maxCount);
  98. FindInformationList findInformationList;
  99. try
  100. {
  101. findInformationList = SMB1FileStoreHelper.GetFindInformationList(segment, subcommand.InformationLevel, header.UnicodeFlag, returnResumeKeys, maxLength);
  102. }
  103. catch (UnsupportedInformationLevelException)
  104. {
  105. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  106. return null;
  107. }
  108. int returnCount = findInformationList.Count;
  109. Transaction2FindNext2Response response = new Transaction2FindNext2Response();
  110. response.SetFindInformationList(findInformationList, header.UnicodeFlag);
  111. openSearch.EnumerationLocation += returnCount;
  112. response.EndOfSearch = (openSearch.EnumerationLocation == openSearch.Entries.Count);
  113. if (response.EndOfSearch)
  114. {
  115. session.RemoveOpenSearch(subcommand.SID);
  116. }
  117. return response;
  118. }
  119. internal static Transaction2QueryFSInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2QueryFSInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  120. {
  121. SMB1Session session = state.GetSession(header.UID);
  122. if (share is FileSystemShare)
  123. {
  124. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  125. {
  126. state.LogToServer(Severity.Verbose, "QueryFileSystemInformation on '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  127. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  128. return null;
  129. }
  130. }
  131. Transaction2QueryFSInformationResponse response = new Transaction2QueryFSInformationResponse();
  132. QueryFSInformation queryFSInformation;
  133. NTStatus queryStatus = SMB1FileStoreHelper.GetFileSystemInformation(out queryFSInformation, share.FileStore, subcommand.InformationLevel);
  134. if (queryStatus != NTStatus.STATUS_SUCCESS)
  135. {
  136. state.LogToServer(Severity.Verbose, "GetFileSystemInformation on '{0}' failed. Information level: {1}, NTStatus: {2}", share.Name, subcommand.InformationLevel, queryStatus);
  137. header.Status = queryStatus;
  138. return null;
  139. }
  140. state.LogToServer(Severity.Information, "GetFileSystemInformation on '{0}' succeeded. Information level: {1}", share.Name, subcommand.InformationLevel);
  141. response.SetQueryFSInformation(queryFSInformation, header.UnicodeFlag);
  142. return response;
  143. }
  144. internal static Transaction2QueryPathInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2QueryPathInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  145. {
  146. SMB1Session session = state.GetSession(header.UID);
  147. string path = subcommand.FileName;
  148. if (share is FileSystemShare)
  149. {
  150. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, path))
  151. {
  152. state.LogToServer(Severity.Verbose, "QueryPathInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  153. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  154. return null;
  155. }
  156. }
  157. Transaction2QueryPathInformationResponse response = new Transaction2QueryPathInformationResponse();
  158. QueryInformation queryInformation;
  159. NTStatus queryStatus = SMB1FileStoreHelper.GetFileInformation(out queryInformation, share.FileStore, path, subcommand.InformationLevel, session.SecurityContext);
  160. if (queryStatus != NTStatus.STATUS_SUCCESS)
  161. {
  162. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: {3}", share.Name, path, subcommand.InformationLevel, queryStatus);
  163. header.Status = queryStatus;
  164. return null;
  165. }
  166. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information level: {2}", share.Name, path, subcommand.InformationLevel);
  167. response.SetQueryInformation(queryInformation);
  168. return response;
  169. }
  170. internal static Transaction2QueryFileInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2QueryFileInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  171. {
  172. SMB1Session session = state.GetSession(header.UID);
  173. OpenFileObject openFile = session.GetOpenFileObject(subcommand.FID);
  174. if (openFile == null)
  175. {
  176. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  177. return null;
  178. }
  179. if (share is FileSystemShare)
  180. {
  181. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  182. {
  183. state.LogToServer(Severity.Verbose, "QueryFileInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  184. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  185. return null;
  186. }
  187. }
  188. Transaction2QueryFileInformationResponse response = new Transaction2QueryFileInformationResponse();
  189. QueryInformation queryInformation;
  190. NTStatus queryStatus = SMB1FileStoreHelper.GetFileInformation(out queryInformation, share.FileStore, openFile.Handle, subcommand.InformationLevel);
  191. if (queryStatus != NTStatus.STATUS_SUCCESS)
  192. {
  193. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: {3}", share.Name, openFile.Path, subcommand.InformationLevel, queryStatus);
  194. header.Status = queryStatus;
  195. return null;
  196. }
  197. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information level: {2}", share.Name, openFile.Path, subcommand.InformationLevel);
  198. response.SetQueryInformation(queryInformation);
  199. return response;
  200. }
  201. internal static Transaction2SetFileInformationResponse GetSubcommandResponse(SMB1Header header, Transaction2SetFileInformationRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  202. {
  203. SMB1Session session = state.GetSession(header.UID);
  204. OpenFileObject openFile = session.GetOpenFileObject(subcommand.FID);
  205. if (openFile == null)
  206. {
  207. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  208. return null;
  209. }
  210. if (share is FileSystemShare)
  211. {
  212. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  213. {
  214. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  215. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  216. return null;
  217. }
  218. }
  219. SetInformation information;
  220. try
  221. {
  222. information = SetInformation.GetSetInformation(subcommand.InformationBytes, subcommand.InformationLevel);
  223. }
  224. catch(UnsupportedInformationLevelException)
  225. {
  226. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: STATUS_OS2_INVALID_LEVEL", share.Name, openFile.Path, subcommand.InformationLevel);
  227. header.Status = NTStatus.STATUS_OS2_INVALID_LEVEL;
  228. return null;
  229. }
  230. catch(Exception)
  231. {
  232. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: STATUS_INVALID_PARAMETER", share.Name, openFile.Path, subcommand.InformationLevel);
  233. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  234. return null;
  235. }
  236. NTStatus status = SMB1FileStoreHelper.SetFileInformation(share.FileStore, openFile.Handle, information);
  237. if (status != NTStatus.STATUS_SUCCESS)
  238. {
  239. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information level: {2}, NTStatus: {3}", share.Name, openFile.Path, subcommand.InformationLevel, status);
  240. header.Status = status;
  241. return null;
  242. }
  243. state.LogToServer(Severity.Information, "SetFileInformation on '{0}{1}' succeeded. Information level: {2}", share.Name, openFile.Path, subcommand.InformationLevel);
  244. Transaction2SetFileInformationResponse response = new Transaction2SetFileInformationResponse();
  245. return response;
  246. }
  247. private static FileInformationClass GetFileInformationClass(FindInformationLevel informationLevel)
  248. {
  249. switch (informationLevel)
  250. {
  251. case FindInformationLevel.SMB_INFO_STANDARD:
  252. return FileInformationClass.FileDirectoryInformation;
  253. case FindInformationLevel.SMB_INFO_QUERY_EA_SIZE:
  254. return FileInformationClass.FileFullDirectoryInformation;
  255. case FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
  256. return FileInformationClass.FileDirectoryInformation;
  257. case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
  258. return FileInformationClass.FileDirectoryInformation;
  259. case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
  260. return FileInformationClass.FileFullDirectoryInformation;
  261. case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
  262. return FileInformationClass.FileNamesInformation;
  263. case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
  264. return FileInformationClass.FileBothDirectoryInformation;
  265. default:
  266. throw new UnsupportedInformationLevelException();
  267. }
  268. }
  269. }
  270. }