SetInfoHelper.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. 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).HasWriteAccess(session.SecurityContext, openFile.Path))
  29. {
  30. state.LogToServer(Severity.Verbose, "SetFileInformation 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 information;
  35. try
  36. {
  37. information = FileInformation.GetFileInformation(request.Buffer, 0, request.FileInformationClass);
  38. }
  39. catch (UnsupportedInformationLevelException)
  40. {
  41. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_INVALID_INFO_CLASS", share.Name, openFile.Path, request.FileInformationClass);
  42. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_INFO_CLASS);
  43. }
  44. catch (NotImplementedException)
  45. {
  46. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_NOT_SUPPORTED", share.Name, openFile.Path, request.FileInformationClass);
  47. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  48. }
  49. catch (Exception)
  50. {
  51. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: STATUS_INVALID_PARAMETER", share.Name, openFile.Path, request.FileInformationClass);
  52. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  53. }
  54. NTStatus status = share.FileStore.SetFileInformation(openFile.Handle, information);
  55. if (status != NTStatus.STATUS_SUCCESS)
  56. {
  57. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}{1}' failed. Information class: {2}, NTStatus: {3}", share.Name, openFile.Path, request.FileInformationClass, status);
  58. return new ErrorResponse(request.CommandName, status);
  59. }
  60. state.LogToServer(Severity.Information, "SetFileInformation on '{0}{1}' succeeded. Information class: {2}", share.Name, openFile.Path, request.FileInformationClass);
  61. return new SetInfoResponse();
  62. }
  63. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  64. }
  65. }
  66. }