TreeConnectHelper.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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, ShareCollection 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.InvariantCultureIgnoreCase))
  25. {
  26. share = services;
  27. shareType = ShareType.Pipe;
  28. shareFlags = ShareFlags.NoCaching;
  29. }
  30. else
  31. {
  32. share = shares.GetShareFromName(shareName);
  33. shareType = ShareType.Disk;
  34. shareFlags = ShareFlags.ManualCaching;
  35. if (share == null)
  36. {
  37. return new ErrorResponse(request.CommandName, NTStatus.STATUS_OBJECT_PATH_NOT_FOUND);
  38. }
  39. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  40. {
  41. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  42. }
  43. }
  44. uint? treeID = session.AddConnectedTree(share);
  45. if (!treeID.HasValue)
  46. {
  47. return new ErrorResponse(request.CommandName, NTStatus.STATUS_INSUFF_SERVER_RESOURCES);
  48. }
  49. response.Header.TreeID = treeID.Value;
  50. response.ShareType = shareType;
  51. response.ShareFlags = shareFlags;
  52. response.MaximalAccess.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  53. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  54. FileAccessMask.FILE_EXECUTE |
  55. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  56. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  57. return response;
  58. }
  59. }
  60. }