QueryInfoHelper.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if (!((FileSystemShare)share).HasReadAccess(session.UserName, openFile.Path, state.ClientEndPoint))
  35. {
  36. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  37. }
  38. IFileSystem fileSystem = ((FileSystemShare)share).FileSystem;
  39. FileSystemEntry entry = fileSystem.GetEntry(openFile.Path);
  40. if (entry == null)
  41. {
  42. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NO_SUCH_FILE);
  43. }
  44. queryStatus = NTFileSystemHelper.GetFileInformation(out fileInformation, entry, openFile.DeleteOnClose, request.FileInformationClass);
  45. }
  46. if (queryStatus != NTStatus.STATUS_SUCCESS)
  47. {
  48. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}' failed. Information class: {1}, NTStatus: {2}", openFile.Path, request.FileInformationClass, queryStatus);
  49. return new ErrorResponse(request.CommandName, queryStatus);
  50. }
  51. QueryInfoResponse response = new QueryInfoResponse();
  52. response.SetFileInformation(fileInformation);
  53. return response;
  54. }
  55. else if (request.InfoType == InfoType.FileSystem)
  56. {
  57. if (share is FileSystemShare)
  58. {
  59. if (!((FileSystemShare)share).HasReadAccess(session.UserName, @"\", state.ClientEndPoint))
  60. {
  61. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  62. }
  63. IFileSystem fileSystem = ((FileSystemShare)share).FileSystem;
  64. FileSystemInformation fileSystemInformation;
  65. NTStatus queryStatus = NTFileSystemHelper.GetFileSystemInformation(out fileSystemInformation, request.FileSystemInformationClass, fileSystem);
  66. if (queryStatus != NTStatus.STATUS_SUCCESS)
  67. {
  68. state.LogToServer(Severity.Verbose, "GetFileSystemInformation failed. Information class: {0}, NTStatus: {1}", request.FileSystemInformationClass, queryStatus);
  69. return new ErrorResponse(request.CommandName, queryStatus);
  70. }
  71. QueryInfoResponse response = new QueryInfoResponse();
  72. response.SetFileSystemInformation(fileSystemInformation);
  73. return response;
  74. }
  75. }
  76. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  77. }
  78. }
  79. }