QueryInfoHelper.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 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);
  22. if (openFile == null)
  23. {
  24. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  25. }
  26. if (share is FileSystemShare)
  27. {
  28. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  29. {
  30. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  31. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  32. }
  33. }
  34. FileInformation fileInformation;
  35. NTStatus queryStatus = share.FileStore.GetFileInformation(out fileInformation, openFile.Handle, request.FileInformationClass);
  36. if (queryStatus != NTStatus.STATUS_SUCCESS)
  37. {
  38. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: {3}", share.Name, openFile.Path, request.FileInformationClass, queryStatus);
  39. return new ErrorResponse(request.CommandName, queryStatus);
  40. }
  41. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information class: {2}", share.Name, openFile.Path, request.FileInformationClass);
  42. QueryInfoResponse response = new QueryInfoResponse();
  43. response.SetFileInformation(fileInformation);
  44. return response;
  45. }
  46. else if (request.InfoType == InfoType.FileSystem)
  47. {
  48. if (share is FileSystemShare)
  49. {
  50. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  51. {
  52. state.LogToServer(Severity.Verbose, "GetFileSystemInformation on '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  53. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  54. }
  55. FileSystemInformation fileSystemInformation;
  56. NTStatus queryStatus = share.FileStore.GetFileSystemInformation(out fileSystemInformation, request.FileSystemInformationClass);
  57. if (queryStatus != NTStatus.STATUS_SUCCESS)
  58. {
  59. state.LogToServer(Severity.Verbose, "GetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: {2}", share.Name, request.FileSystemInformationClass, queryStatus);
  60. return new ErrorResponse(request.CommandName, queryStatus);
  61. }
  62. state.LogToServer(Severity.Information, "GetFileSystemInformation on '{0}' succeeded. Information class: {1}", share.Name, request.FileSystemInformationClass);
  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. }