SetInfoHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if (request.InfoType == InfoType.File)
  38. {
  39. FileInformation information;
  40. try
  41. {
  42. information = FileInformation.GetFileInformation(request.Buffer, 0, request.FileInformationClass);
  43. }
  44. catch (UnsupportedInformationLevelException)
  45. {
  46. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_INVALID_INFO_CLASS.", share.Name, openFile.Path, request.FileInformationClass);
  47. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_INFO_CLASS);
  48. }
  49. catch (NotImplementedException)
  50. {
  51. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_NOT_SUPPORTED.", share.Name, openFile.Path, request.FileInformationClass);
  52. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  53. }
  54. catch (Exception)
  55. {
  56. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_INVALID_PARAMETER.", share.Name, openFile.Path, request.FileInformationClass);
  57. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  58. }
  59. if ((share is FileSystemShare) && (information is FileRenameInformationType2))
  60. {
  61. string newFileName = ((FileRenameInformationType2)information).FileName;
  62. if (!newFileName.StartsWith(@"\"))
  63. {
  64. newFileName = @"\" + newFileName;
  65. }
  66. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, newFileName))
  67. {
  68. state.LogToServer(Severity.Verbose, "SetFileInformation: Rename '{0}{1}' to '{0}{2}' failed. User '{3}' was denied access.", share.Name, openFile.Path, newFileName, session.UserName);
  69. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  70. }
  71. }
  72. NTStatus status = share.FileStore.SetFileInformation(openFile.Handle, information);
  73. if (status != NTStatus.STATUS_SUCCESS)
  74. {
  75. 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);
  76. return new ErrorResponse(request.CommandName, status);
  77. }
  78. if (information is FileRenameInformationType2)
  79. {
  80. string newFileName = ((FileRenameInformationType2)information).FileName;
  81. if (!newFileName.StartsWith(@"\"))
  82. {
  83. newFileName = @"\" + newFileName;
  84. }
  85. state.LogToServer(Severity.Verbose, "SetFileInformation: Rename '{0}{1}' to '{0}{2}' succeeded. (FileId: {3})", share.Name, openFile.Path, newFileName, request.FileId.Volatile);
  86. openFile.Path = newFileName;
  87. }
  88. else
  89. {
  90. state.LogToServer(Severity.Information, "SetFileInformation on '{0}{1}' succeeded. Information class: {2}. (FileId: {3})", share.Name, openFile.Path, request.FileInformationClass, request.FileId.Volatile);
  91. }
  92. return new SetInfoResponse();
  93. }
  94. else if (request.InfoType == InfoType.Security)
  95. {
  96. SecurityDescriptor securityDescriptor;
  97. try
  98. {
  99. securityDescriptor = new SecurityDescriptor(request.Buffer, 0);
  100. }
  101. catch
  102. {
  103. state.LogToServer(Severity.Verbose, "SetSecurityInformation on '{0}{1}' failed. NTStatus: STATUS_INVALID_PARAMETER.", share.Name, openFile.Path);
  104. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  105. }
  106. NTStatus status = share.FileStore.SetSecurityInformation(openFile, request.SecurityInformation, securityDescriptor);
  107. if (status != NTStatus.STATUS_SUCCESS)
  108. {
  109. 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);
  110. return new ErrorResponse(request.CommandName, status);
  111. }
  112. 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);
  113. return new SetInfoResponse();
  114. }
  115. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  116. }
  117. }
  118. }