SetInfoHelper.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 SetInfoHelper
  15. {
  16. internal static SMB2Command GetSetInfoResponse(SetInfoRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. OpenFileObject openFile = null;
  20. if (request.InfoType == InfoType.File || request.InfoType == InfoType.Security)
  21. {
  22. openFile = session.GetOpenFileObject(request.FileId);
  23. if (openFile == null)
  24. {
  25. state.LogToServer(Severity.Verbose, "SetFileInformation failed. Invalid FileId. (SessionID: {0}, TreeID: {1}, FileId: {2})", request.Header.SessionID, request.Header.TreeID, request.FileId.Volatile);
  26. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  27. }
  28. if (share is FileSystemShare)
  29. {
  30. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  31. {
  32. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  33. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  34. }
  35. }
  36. }
  37. else if (request.InfoType == InfoType.FileSystem)
  38. {
  39. if (share is FileSystemShare)
  40. {
  41. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, @"\"))
  42. {
  43. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  44. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  45. }
  46. }
  47. }
  48. if (request.InfoType == InfoType.File)
  49. {
  50. FileInformation information;
  51. try
  52. {
  53. information = FileInformation.GetFileInformation(request.Buffer, 0, request.FileInformationClass);
  54. }
  55. catch (UnsupportedInformationLevelException)
  56. {
  57. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_INVALID_INFO_CLASS.", share.Name, openFile.Path, request.FileInformationClass);
  58. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_INFO_CLASS);
  59. }
  60. catch (NotImplementedException)
  61. {
  62. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_NOT_SUPPORTED.", share.Name, openFile.Path, request.FileInformationClass);
  63. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  64. }
  65. catch (Exception)
  66. {
  67. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_INVALID_PARAMETER.", share.Name, openFile.Path, request.FileInformationClass);
  68. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  69. }
  70. if ((share is FileSystemShare) && (information is FileRenameInformationType2))
  71. {
  72. string newFileName = ((FileRenameInformationType2)information).FileName;
  73. if (!newFileName.StartsWith(@"\"))
  74. {
  75. newFileName = @"\" + newFileName;
  76. }
  77. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, newFileName))
  78. {
  79. state.LogToServer(Severity.Verbose, "SetFileInformation: Rename '{0}{1}' to '{0}{2}' failed. User '{3}' was denied access.", share.Name, openFile.Path, newFileName, session.UserName);
  80. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  81. }
  82. }
  83. NTStatus status = share.FileStore.SetFileInformation(openFile.Handle, information);
  84. if (status != NTStatus.STATUS_SUCCESS)
  85. {
  86. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: {3}. (FileId: {4})", share.Name, openFile.Path, request.FileInformationClass, status, request.FileId.Volatile);
  87. return new ErrorResponse(request.CommandName, status);
  88. }
  89. if (information is FileRenameInformationType2)
  90. {
  91. string newFileName = ((FileRenameInformationType2)information).FileName;
  92. if (!newFileName.StartsWith(@"\"))
  93. {
  94. newFileName = @"\" + newFileName;
  95. }
  96. state.LogToServer(Severity.Verbose, "SetFileInformation: Rename '{0}{1}' to '{0}{2}' succeeded. (FileId: {3})", share.Name, openFile.Path, newFileName, request.FileId.Volatile);
  97. openFile.Path = newFileName;
  98. }
  99. else
  100. {
  101. state.LogToServer(Severity.Information, "SetFileInformation on '{0}{1}' succeeded. Information class: {2}. (FileId: {3})", share.Name, openFile.Path, request.FileInformationClass, request.FileId.Volatile);
  102. }
  103. return new SetInfoResponse();
  104. }
  105. else if (request.InfoType == InfoType.FileSystem)
  106. {
  107. FileSystemInformation fileSystemInformation;
  108. try
  109. {
  110. fileSystemInformation = FileSystemInformation.GetFileSystemInformation(request.Buffer, 0, request.FileSystemInformationClass);
  111. }
  112. catch (UnsupportedInformationLevelException)
  113. {
  114. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: STATUS_INVALID_INFO_CLASS.", share.Name, request.FileSystemInformationClass);
  115. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_INFO_CLASS);
  116. }
  117. catch (Exception)
  118. {
  119. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: STATUS_INVALID_PARAMETER.", share.Name, request.FileSystemInformationClass);
  120. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  121. }
  122. NTStatus status = share.FileStore.SetFileSystemInformation(fileSystemInformation);
  123. if (status != NTStatus.STATUS_SUCCESS)
  124. {
  125. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' failed. Information class: {1}, NTStatus: {2}.", share.Name, request.FileSystemInformationClass, status);
  126. return new ErrorResponse(request.CommandName, status);
  127. }
  128. state.LogToServer(Severity.Verbose, "SetFileSystemInformation on '{0}' succeeded. Information class: {1}.", share.Name, request.FileSystemInformationClass);
  129. return new SetInfoResponse();
  130. }
  131. else if (request.InfoType == InfoType.Security)
  132. {
  133. SecurityDescriptor securityDescriptor;
  134. try
  135. {
  136. securityDescriptor = new SecurityDescriptor(request.Buffer, 0);
  137. }
  138. catch
  139. {
  140. state.LogToServer(Severity.Verbose, "SetSecurityInformation on '{0}{1}' failed. NTStatus: STATUS_INVALID_PARAMETER.", share.Name, openFile.Path);
  141. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  142. }
  143. NTStatus status = share.FileStore.SetSecurityInformation(openFile, request.SecurityInformation, securityDescriptor);
  144. if (status != NTStatus.STATUS_SUCCESS)
  145. {
  146. state.LogToServer(Severity.Verbose, "SetSecurityInformation on '{0}{1}' failed. Security information: 0x{2}, NTStatus: {3}. (FileId: {4})", share.Name, openFile.Path, request.SecurityInformation.ToString("X"), status, request.FileId.Volatile);
  147. return new ErrorResponse(request.CommandName, status);
  148. }
  149. state.LogToServer(Severity.Information, "SetSecurityInformation on '{0}{1}' succeeded. Security information: 0x{2}. (FileId: {3})", share.Name, openFile.Path, request.SecurityInformation.ToString("X"), request.FileId.Volatile);
  150. return new SetInfoResponse();
  151. }
  152. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  153. }
  154. }
  155. }