TreeConnectHelper.cs 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright (C) 2014-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 System.Text;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. internal class TreeConnectHelper
  15. {
  16. internal static SMB2Command GetTreeConnectResponse(TreeConnectRequest request, SMB2ConnectionState state, NamedPipeShare services, SMBShareCollection shares)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. TreeConnectResponse response = new TreeConnectResponse();
  20. string shareName = ServerPathUtils.GetShareName(request.Path);
  21. ISMBShare share;
  22. ShareType shareType;
  23. ShareFlags shareFlags;
  24. if (String.Equals(shareName, NamedPipeShare.NamedPipeShareName, StringComparison.OrdinalIgnoreCase))
  25. {
  26. share = services;
  27. shareType = ShareType.Pipe;
  28. shareFlags = ShareFlags.NoCaching;
  29. }
  30. else
  31. {
  32. share = shares.GetShareFromName(shareName);
  33. if (share == null)
  34. {
  35. return new ErrorResponse(request.CommandName, NTStatus.STATUS_OBJECT_PATH_NOT_FOUND);
  36. }
  37. shareType = ShareType.Disk;
  38. shareFlags = GetShareCachingFlags(((FileSystemShare)share).CachingPolicy);
  39. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  40. {
  41. state.LogToServer(Severity.Verbose, "Tree Connect to '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  42. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  43. }
  44. }
  45. uint? treeID = session.AddConnectedTree(share);
  46. if (!treeID.HasValue)
  47. {
  48. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INSUFF_SERVER_RESOURCES);
  49. }
  50. state.LogToServer(Severity.Information, "Tree Connect: User '{0}' connected to '{1}'", session.UserName, share.Name);
  51. response.Header.TreeID = treeID.Value;
  52. response.ShareType = shareType;
  53. response.ShareFlags = shareFlags;
  54. response.MaximalAccess.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  55. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  56. FileAccessMask.FILE_EXECUTE |
  57. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  58. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  59. return response;
  60. }
  61. private static ShareFlags GetShareCachingFlags(CachingPolicy cachingPolicy)
  62. {
  63. switch (cachingPolicy)
  64. {
  65. case CachingPolicy.ManualCaching:
  66. return ShareFlags.ManualCaching;
  67. case CachingPolicy.AutoCaching:
  68. return ShareFlags.AutoCaching;
  69. case CachingPolicy.VideoCaching:
  70. return ShareFlags.VdoCaching;
  71. default:
  72. return ShareFlags.NoCaching;
  73. }
  74. }
  75. internal static SMB2Command GetTreeDisconnectResponse(TreeDisconnectRequest request, ISMBShare share, SMB2ConnectionState state)
  76. {
  77. SMB2Session session = state.GetSession(request.Header.SessionID);
  78. session.DisconnectTree(request.Header.TreeID);
  79. state.LogToServer(Severity.Information, "Tree Disconnect: User '{0}' disconnected from '{1}'", session.UserName, share.Name);
  80. return new TreeDisconnectResponse();
  81. }
  82. }
  83. }