SetInfoHelper.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. public 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.Persistent);
  22. if (openFile == null)
  23. {
  24. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  25. }
  26. if (share is FileSystemShare)
  27. {
  28. IFileSystem fileSystem = ((FileSystemShare)share).FileSystem;
  29. if (!((FileSystemShare)share).HasWriteAccess(session.UserName))
  30. {
  31. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  32. }
  33. FileInformation information;
  34. try
  35. {
  36. information = FileInformation.GetFileInformation(request.Buffer, 0, request.FileInformationClass);
  37. }
  38. catch (UnsupportedInformationLevelException)
  39. {
  40. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_INFO_CLASS);
  41. }
  42. catch (NotImplementedException)
  43. {
  44. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  45. }
  46. catch (Exception)
  47. {
  48. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INVALID_PARAMETER);
  49. }
  50. NTStatus status = NTFileSystemHelper.SetFileInformation(fileSystem, openFile, information, state);
  51. if (status != NTStatus.STATUS_SUCCESS)
  52. {
  53. state.LogToServer(Severity.Verbose, "SetFileInformation on '{0}' failed. Information class: {1}, NTStatus: {2}", openFile.Path, information.FileInformationClass, status);
  54. return new ErrorResponse(request.CommandName, status);
  55. }
  56. return new SetInfoResponse();
  57. }
  58. }
  59. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  60. }
  61. }
  62. }