TreeConnectHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.SMB1;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB1
  13. {
  14. internal class TreeConnectHelper
  15. {
  16. internal static SMB1Command GetTreeConnectResponse(SMB1Header header, TreeConnectAndXRequest request, SMB1ConnectionState state, NamedPipeShare services, SMBShareCollection shares)
  17. {
  18. SMB1Session session = state.GetSession(header.UID);
  19. bool isExtended = (request.Flags & TreeConnectFlags.ExtendedResponse) > 0;
  20. string shareName = ServerPathUtils.GetShareName(request.Path);
  21. ISMBShare share;
  22. ServiceName serviceName;
  23. OptionalSupportFlags supportFlags;
  24. if (String.Equals(shareName, NamedPipeShare.NamedPipeShareName, StringComparison.OrdinalIgnoreCase))
  25. {
  26. share = services;
  27. serviceName = ServiceName.NamedPipe;
  28. supportFlags = OptionalSupportFlags.SMB_SUPPORT_SEARCH_BITS | OptionalSupportFlags.SMB_CSC_NO_CACHING;
  29. }
  30. else
  31. {
  32. share = shares.GetShareFromName(shareName);
  33. if (share == null)
  34. {
  35. header.Status = NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
  36. return new ErrorResponse(request.CommandName);
  37. }
  38. serviceName = ServiceName.DiskShare;
  39. supportFlags = OptionalSupportFlags.SMB_SUPPORT_SEARCH_BITS | GetCachingSupportFlags(((FileSystemShare)share).CachingPolicy);
  40. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  41. {
  42. state.LogToServer(Severity.Verbose, "Tree Connect to '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  43. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  44. return new ErrorResponse(request.CommandName);
  45. }
  46. }
  47. ushort? treeID = session.AddConnectedTree(share);
  48. if (!treeID.HasValue)
  49. {
  50. header.Status = NTStatus.STATUS_INSUFF_SERVER_RESOURCES;
  51. return new ErrorResponse(request.CommandName);
  52. }
  53. state.LogToServer(Severity.Information, "Tree Connect: User '{0}' connected to '{1}' (UID: {2}, TID: {3})", session.UserName, share.Name, header.UID, treeID.Value);
  54. header.TID = treeID.Value;
  55. if (isExtended)
  56. {
  57. return CreateTreeConnectResponseExtended(serviceName, supportFlags);
  58. }
  59. else
  60. {
  61. return CreateTreeConnectResponse(serviceName, supportFlags);
  62. }
  63. }
  64. private static OptionalSupportFlags GetCachingSupportFlags(CachingPolicy cachingPolicy)
  65. {
  66. switch (cachingPolicy)
  67. {
  68. case CachingPolicy.ManualCaching:
  69. return OptionalSupportFlags.SMB_CSC_CACHE_MANUAL_REINT;
  70. case CachingPolicy.AutoCaching:
  71. return OptionalSupportFlags.SMB_CSC_CACHE_AUTO_REINT;
  72. case CachingPolicy.VideoCaching:
  73. return OptionalSupportFlags.SMB_CSC_CACHE_VDO;
  74. default:
  75. return OptionalSupportFlags.SMB_CSC_NO_CACHING;
  76. }
  77. }
  78. private static TreeConnectAndXResponse CreateTreeConnectResponse(ServiceName serviceName, OptionalSupportFlags supportFlags)
  79. {
  80. TreeConnectAndXResponse response = new TreeConnectAndXResponse();
  81. response.OptionalSupport = supportFlags;
  82. response.NativeFileSystem = String.Empty;
  83. response.Service = serviceName;
  84. return response;
  85. }
  86. private static TreeConnectAndXResponseExtended CreateTreeConnectResponseExtended(ServiceName serviceName, OptionalSupportFlags supportFlags)
  87. {
  88. TreeConnectAndXResponseExtended response = new TreeConnectAndXResponseExtended();
  89. response.OptionalSupport = supportFlags;
  90. response.MaximalShareAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  91. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  92. FileAccessMask.FILE_EXECUTE |
  93. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  94. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  95. response.GuestMaximalShareAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  96. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  97. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  98. FileAccessMask.READ_CONTROL | FileAccessMask.SYNCHRONIZE;
  99. response.NativeFileSystem = String.Empty;
  100. response.Service = serviceName;
  101. return response;
  102. }
  103. internal static SMB1Command GetTreeDisconnectResponse(SMB1Header header, TreeDisconnectRequest request, ISMBShare share, SMB1ConnectionState state)
  104. {
  105. SMB1Session session = state.GetSession(header.UID);
  106. session.DisconnectTree(header.TID);
  107. state.LogToServer(Severity.Information, "Tree Disconnect: User '{0}' disconnected from '{1}' (UID: {2}, TID: {3})", session.UserName, share.Name, header.UID, header.TID);
  108. return new TreeDisconnectResponse();
  109. }
  110. }
  111. }