QueryDirectoryHelper.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 SMBLibrary.Authentication;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. internal class QueryDirectoryHelper
  15. {
  16. internal static SMB2Command GetQueryDirectoryResponse(QueryDirectoryRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. OpenFileObject openFile = session.GetOpenFileObject(request.FileId);
  20. if (openFile == null)
  21. {
  22. state.LogToServer(Severity.Verbose, "Query Directory failed. Invalid FileId.");
  23. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  24. }
  25. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  26. {
  27. state.LogToServer(Severity.Verbose, "Query Directory on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  28. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  29. }
  30. FileSystemShare fileSystemShare = (FileSystemShare)share;
  31. FileID fileID = request.FileId;
  32. OpenSearch openSearch = session.GetOpenSearch(fileID);
  33. if (openSearch == null || request.Reopen)
  34. {
  35. if (request.Reopen)
  36. {
  37. session.RemoveOpenSearch(fileID);
  38. }
  39. List<QueryDirectoryFileInformation> entries;
  40. NTStatus searchStatus = share.FileStore.QueryDirectory(out entries, openFile.Handle, request.FileName, request.FileInformationClass);
  41. if (searchStatus != NTStatus.STATUS_SUCCESS)
  42. {
  43. state.LogToServer(Severity.Verbose, "Query Directory on '{0}{1}', Searched for '{2}', NTStatus: {3}", share.Name, openFile.Path, request.FileName, searchStatus.ToString());
  44. return new ErrorResponse(request.CommandName, searchStatus);
  45. }
  46. state.LogToServer(Severity.Information, "Query Directory on '{0}{1}', Searched for '{2}', found {3} matching entries", share.Name, openFile.Path, request.FileName, entries.Count);
  47. openSearch = session.AddOpenSearch(fileID, entries, 0);
  48. }
  49. if (request.Restart || request.Reopen)
  50. {
  51. openSearch.EnumerationLocation = 0;
  52. }
  53. if (openSearch.Entries.Count == 0)
  54. {
  55. // [MS-SMB2] If there are no entries to return [..] the server MUST fail the request with STATUS_NO_SUCH_FILE.
  56. session.RemoveOpenSearch(fileID);
  57. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NO_SUCH_FILE);
  58. }
  59. if (openSearch.EnumerationLocation == openSearch.Entries.Count)
  60. {
  61. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NO_MORE_FILES);
  62. }
  63. List<QueryDirectoryFileInformation> page = new List<QueryDirectoryFileInformation>();
  64. int pageLength = 0;
  65. for (int index = openSearch.EnumerationLocation; index < openSearch.Entries.Count; index++)
  66. {
  67. QueryDirectoryFileInformation fileInformation = openSearch.Entries[index];
  68. if (fileInformation.FileInformationClass != request.FileInformationClass)
  69. {
  70. // We do not support changing FileInformationClass during a search (unless SMB2_REOPEN is set).
  71. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  72. }
  73. int entryLength = fileInformation.Length;
  74. if (pageLength + entryLength <= request.OutputBufferLength)
  75. {
  76. page.Add(fileInformation);
  77. int paddedLength = (int)Math.Ceiling((double)entryLength / 8) * 8;
  78. pageLength += paddedLength;
  79. openSearch.EnumerationLocation = index + 1;
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. if (request.ReturnSingleEntry)
  86. {
  87. break;
  88. }
  89. }
  90. QueryDirectoryResponse response = new QueryDirectoryResponse();
  91. response.SetFileInformationList(page);
  92. return response;
  93. }
  94. }
  95. }