TreeConnectHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (request.Service != ServiceName.AnyType && request.Service != ServiceName.NamedPipe)
  27. {
  28. header.Status = NTStatus.STATUS_BAD_DEVICE_TYPE;
  29. return new ErrorResponse(request.CommandName);
  30. }
  31. share = services;
  32. serviceName = ServiceName.NamedPipe;
  33. supportFlags = OptionalSupportFlags.SMB_SUPPORT_SEARCH_BITS | OptionalSupportFlags.SMB_CSC_NO_CACHING;
  34. }
  35. else
  36. {
  37. share = shares.GetShareFromName(shareName);
  38. if (share == null)
  39. {
  40. header.Status = NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
  41. return new ErrorResponse(request.CommandName);
  42. }
  43. if (request.Service != ServiceName.AnyType && request.Service != ServiceName.DiskShare)
  44. {
  45. header.Status = NTStatus.STATUS_BAD_DEVICE_TYPE;
  46. return new ErrorResponse(request.CommandName);
  47. }
  48. serviceName = ServiceName.DiskShare;
  49. supportFlags = OptionalSupportFlags.SMB_SUPPORT_SEARCH_BITS | GetCachingSupportFlags(((FileSystemShare)share).CachingPolicy);
  50. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
  51. {
  52. state.LogToServer(Severity.Verbose, "Tree Connect to '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
  53. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  54. return new ErrorResponse(request.CommandName);
  55. }
  56. }
  57. ushort? treeID = session.AddConnectedTree(share);
  58. if (!treeID.HasValue)
  59. {
  60. header.Status = NTStatus.STATUS_INSUFF_SERVER_RESOURCES;
  61. return new ErrorResponse(request.CommandName);
  62. }
  63. state.LogToServer(Severity.Information, "Tree Connect: User '{0}' connected to '{1}' (UID: {2}, TID: {3})", session.UserName, share.Name, header.UID, treeID.Value);
  64. header.TID = treeID.Value;
  65. if (isExtended)
  66. {
  67. return CreateTreeConnectResponseExtended(serviceName, supportFlags);
  68. }
  69. else
  70. {
  71. return CreateTreeConnectResponse(serviceName, supportFlags);
  72. }
  73. }
  74. private static OptionalSupportFlags GetCachingSupportFlags(CachingPolicy cachingPolicy)
  75. {
  76. switch (cachingPolicy)
  77. {
  78. case CachingPolicy.ManualCaching:
  79. return OptionalSupportFlags.SMB_CSC_CACHE_MANUAL_REINT;
  80. case CachingPolicy.AutoCaching:
  81. return OptionalSupportFlags.SMB_CSC_CACHE_AUTO_REINT;
  82. case CachingPolicy.VideoCaching:
  83. return OptionalSupportFlags.SMB_CSC_CACHE_VDO;
  84. default:
  85. return OptionalSupportFlags.SMB_CSC_NO_CACHING;
  86. }
  87. }
  88. private static TreeConnectAndXResponse CreateTreeConnectResponse(ServiceName serviceName, OptionalSupportFlags supportFlags)
  89. {
  90. TreeConnectAndXResponse response = new TreeConnectAndXResponse();
  91. response.OptionalSupport = supportFlags;
  92. response.NativeFileSystem = String.Empty;
  93. response.Service = serviceName;
  94. return response;
  95. }
  96. private static TreeConnectAndXResponseExtended CreateTreeConnectResponseExtended(ServiceName serviceName, OptionalSupportFlags supportFlags)
  97. {
  98. TreeConnectAndXResponseExtended response = new TreeConnectAndXResponseExtended();
  99. response.OptionalSupport = supportFlags;
  100. response.MaximalShareAccessRights = (AccessMask)(FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  101. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  102. FileAccessMask.FILE_EXECUTE |
  103. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES) |
  104. AccessMask.DELETE | AccessMask.READ_CONTROL | AccessMask.WRITE_DAC | AccessMask.WRITE_OWNER | AccessMask.SYNCHRONIZE;
  105. response.GuestMaximalShareAccessRights = (AccessMask)(FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  106. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  107. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES) |
  108. AccessMask.READ_CONTROL | AccessMask.SYNCHRONIZE;
  109. response.NativeFileSystem = String.Empty;
  110. response.Service = serviceName;
  111. return response;
  112. }
  113. internal static SMB1Command GetTreeDisconnectResponse(SMB1Header header, TreeDisconnectRequest request, ISMBShare share, SMB1ConnectionState state)
  114. {
  115. SMB1Session session = state.GetSession(header.UID);
  116. session.DisconnectTree(header.TID);
  117. state.LogToServer(Severity.Information, "Tree Disconnect: User '{0}' disconnected from '{1}' (UID: {2}, TID: {3})", session.UserName, share.Name, header.UID, header.TID);
  118. return new TreeDisconnectResponse();
  119. }
  120. }
  121. }