QueryInfoHelper.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (C) 2017-2019 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. state.LogToServer(Severity.Verbose, "GetFileInformation failed. Invalid FileId. (SessionID: {0}, TreeID: {1}, FileId: {2})", request.Header.SessionID, request.Header.TreeID, request.FileId.Volatile);
  25. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  26. }
  27. if (share is FileSystemShare)
  28. {
  29. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  30. {
  31. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  32. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  33. }
  34. }
  35. FileInformation fileInformation;
  36. NTStatus queryStatus = share.FileStore.GetFileInformation(out fileInformation, openFile.Handle, request.FileInformationClass);
  37. if (queryStatus != NTStatus.STATUS_SUCCESS)
  38. {
  39. state.LogToServer(Severity.Verbose, "GetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: {3}. (FileId: {4})", share.Name, openFile.Path, request.FileInformationClass, queryStatus, request.FileId.Volatile);
  40. return new ErrorResponse(request.CommandName, queryStatus);
  41. }
  42. state.LogToServer(Severity.Information, "GetFileInformation on '{0}{1}' succeeded. Information class: {2}. (FileId: {3})", share.Name, openFile.Path, request.FileInformationClass, request.FileId.Volatile);
  43. QueryInfoResponse response = new QueryInfoResponse();
  44. response.SetFileInformation(fileInformation);
  45. if (response.OutputBuffer.Length > request.OutputBufferLength)
  46. {
  47. response.Header.Status = NTStatus.STATUS_BUFFER_OVERFLOW;
  48. response.OutputBuffer = ByteReader.ReadBytes(response.OutputBuffer, 0, (int)request.OutputBufferLength);
  49. }
  50. return response;
  51. }
  52. else if (request.InfoType == InfoType.FileSystem)
  53. {
  54. if (share is FileSystemShare)
  55. {
  56. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  57. {
  58. state.LogToServer(Severity.Verbose, "GetFileSystemInformation on '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  59. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  60. }
  61. FileSystemInformation fileSystemInformation;
  62. NTStatus queryStatus = share.FileStore.GetFileSystemInformation(out fileSystemInformation, request.FileSystemInformationClass);
  63. if (queryStatus != NTStatus.STATUS_SUCCESS)
  64. {
  65. state.LogToServer(Severity.Verbose, "GetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: {2}", share.Name, request.FileSystemInformationClass, queryStatus);
  66. return new ErrorResponse(request.CommandName, queryStatus);
  67. }
  68. state.LogToServer(Severity.Information, "GetFileSystemInformation on '{0}' succeeded. Information class: {1}", share.Name, request.FileSystemInformationClass);
  69. QueryInfoResponse response = new QueryInfoResponse();
  70. response.SetFileSystemInformation(fileSystemInformation);
  71. if (response.OutputBuffer.Length > request.OutputBufferLength)
  72. {
  73. response.Header.Status = NTStatus.STATUS_BUFFER_OVERFLOW;
  74. response.OutputBuffer = ByteReader.ReadBytes(response.OutputBuffer, 0, (int)request.OutputBufferLength);
  75. }
  76. return response;
  77. }
  78. }
  79. else if (request.InfoType == InfoType.Security)
  80. {
  81. OpenFileObject openFile = session.GetOpenFileObject(request.FileId);
  82. if (openFile == null)
  83. {
  84. state.LogToServer(Severity.Verbose, "GetSecurityInformation failed. Invalid FileId. (SessionID: {0}, TreeID: {1}, FileId: {2})", request.Header.SessionID, request.Header.TreeID, request.FileId.Volatile);
  85. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  86. }
  87. if (share is FileSystemShare)
  88. {
  89. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  90. {
  91. state.LogToServer(Severity.Verbose, "GetSecurityInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  92. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  93. }
  94. }
  95. SecurityDescriptor securityDescriptor;
  96. NTStatus queryStatus = share.FileStore.GetSecurityInformation(out securityDescriptor, openFile.Handle, request.SecurityInformation);
  97. if (queryStatus != NTStatus.STATUS_SUCCESS)
  98. {
  99. state.LogToServer(Severity.Verbose, "GetSecurityInformation on '{0}{1}' failed. Security information: 0x{2}, NTStatus: {3}. (FileId: {4})", share.Name, openFile.Path, request.SecurityInformation.ToString("X"), queryStatus, request.FileId.Volatile);
  100. return new ErrorResponse(request.CommandName, queryStatus);
  101. }
  102. if (securityDescriptor.Length > request.OutputBufferLength)
  103. {
  104. state.LogToServer(Severity.Information, "GetSecurityInformation on '{0}{1}' failed. Security information: 0x{2}, NTStatus: STATUS_BUFFER_TOO_SMALL. (FileId: {3})", share.Name, openFile.Path, request.SecurityInformation.ToString("X"), request.FileId.Volatile);
  105. byte[] errorData = LittleEndianConverter.GetBytes((uint)securityDescriptor.Length);
  106. return new ErrorResponse(request.CommandName, NTStatus.STATUS_BUFFER_TOO_SMALL, errorData);
  107. }
  108. state.LogToServer(Severity.Information, "GetSecurityInformation on '{0}{1}' succeeded. Security information: 0x{2}. (FileId: {3})", share.Name, openFile.Path, request.SecurityInformation.ToString("X"), request.FileId.Volatile);
  109. QueryInfoResponse response = new QueryInfoResponse();
  110. response.SetSecurityInformation(securityDescriptor);
  111. return response;
  112. }
  113. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  114. }
  115. }
  116. }