QueryInfoHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. public class QueryInfoHelper
  15. {
  16. internal static SMB2Command GetQueryInfoResponse(QueryInfoRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. if (request.InfoType == InfoType.File)
  20. {
  21. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  22. if (openFile == null)
  23. {
  24. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  25. }
  26. FileInformation fileInformation;
  27. NTStatus queryStatus;
  28. if (share is NamedPipeShare)
  29. {
  30. queryStatus = NTFileSystemHelper.GetNamedPipeInformation(out fileInformation, request.FileInformationClass);
  31. }
  32. else // FileSystemShare
  33. {
  34. IFileSystem fileSystem = ((FileSystemShare)share).FileSystem;
  35. FileSystemEntry entry = fileSystem.GetEntry(openFile.Path);
  36. if (entry == null)
  37. {
  38. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NO_SUCH_FILE);
  39. }
  40. queryStatus = NTFileSystemHelper.GetFileInformation(out fileInformation, entry, openFile.DeleteOnClose, request.FileInformationClass);
  41. }
  42. if (queryStatus != NTStatus.STATUS_SUCCESS)
  43. {
  44. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}' failed. Information class: {1}, NTStatus: {2}", openFile.Path, request.FileInformationClass, queryStatus);
  45. return new ErrorResponse(request.CommandName, queryStatus);
  46. }
  47. QueryInfoResponse response = new QueryInfoResponse();
  48. response.SetFileInformation(fileInformation);
  49. return response;
  50. }
  51. else if (request.InfoType == InfoType.FileSystem)
  52. {
  53. if (share is FileSystemShare)
  54. {
  55. IFileSystem fileSystem = ((FileSystemShare)share).FileSystem;
  56. FileSystemInformation fileSystemInformation;
  57. NTStatus queryStatus = NTFileSystemHelper.GetFileSystemInformation(out fileSystemInformation, request.FileSystemInformationClass, fileSystem);
  58. if (queryStatus != NTStatus.STATUS_SUCCESS)
  59. {
  60. state.LogToServer(Severity.Verbose, "GetFileSystemInformation failed. Information class: {0}, NTStatus: {1}", request.FileSystemInformationClass, queryStatus);
  61. return new ErrorResponse(request.CommandName, queryStatus);
  62. }
  63. QueryInfoResponse response = new QueryInfoResponse();
  64. response.SetFileSystemInformation(fileSystemInformation);
  65. return response;
  66. }
  67. }
  68. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  69. }
  70. }
  71. }