SMB1FileStoreHelper.QueryDirectory.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.Text;
  10. using SMBLibrary.SMB1;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB1
  13. {
  14. internal partial class SMB1FileStoreHelper
  15. {
  16. // Filename pattern examples:
  17. // '\Directory' - Get the directory entry
  18. // '\Directory\*' - List the directory files
  19. // '\Directory\s*' - List the directory files starting with s (cmd.exe will use this syntax when entering 's' and hitting tab for autocomplete)
  20. // '\Directory\<.inf' (Update driver will use this syntax)
  21. // '\Directory\exefile"*' (cmd.exe will use this syntax when entering an exe without its extension, explorer will use this opening a directory from the run menu)
  22. /// <param name="fileNamePattern">The filename pattern to search for. This field MAY contain wildcard characters</param>
  23. /// <exception cref="System.UnauthorizedAccessException"></exception>
  24. public static NTStatus QueryDirectory(out List<QueryDirectoryFileInformation> result, INTFileStore fileStore, string fileNamePattern, FileInformationClass fileInformation, SecurityContext securityContext)
  25. {
  26. int separatorIndex = fileNamePattern.LastIndexOf('\\');
  27. if (separatorIndex >= 0)
  28. {
  29. string path = fileNamePattern.Substring(0, separatorIndex + 1);
  30. string fileName = fileNamePattern.Substring(separatorIndex + 1);
  31. object handle;
  32. FileStatus fileStatus;
  33. DirectoryAccessMask accessMask = DirectoryAccessMask.FILE_LIST_DIRECTORY | DirectoryAccessMask.FILE_TRAVERSE | DirectoryAccessMask.SYNCHRONIZE;
  34. CreateOptions createOptions = CreateOptions.FILE_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT;
  35. NTStatus status = fileStore.CreateFile(out handle, out fileStatus, path, (AccessMask)accessMask, 0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, createOptions, securityContext);
  36. if (status != NTStatus.STATUS_SUCCESS)
  37. {
  38. result = null;
  39. return status;
  40. }
  41. status = fileStore.QueryDirectory(out result, handle, fileName, fileInformation);
  42. fileStore.CloseFile(handle);
  43. return status;
  44. }
  45. else
  46. {
  47. result = null;
  48. return NTStatus.STATUS_INVALID_PARAMETER;
  49. }
  50. }
  51. }
  52. }