FileSystemShare: Added ability to specify client-side caching policy

This commit is contained in:
Tal Aloni 2017-08-27 12:59:10 +03:00
parent c4fa4a9f15
commit 64ac337308
6 changed files with 87 additions and 8 deletions

View file

@ -202,6 +202,7 @@
<Compile Include="Server\NameServer.cs" />
<Compile Include="Server\SessionInformation.cs" />
<Compile Include="Server\Shares\AccessRequestArgs.cs" />
<Compile Include="Server\Shares\Enums\CachingPolicy.cs" />
<Compile Include="Server\Shares\FileSystemShare.cs" />
<Compile Include="Server\Shares\ISMBShare.cs" />
<Compile Include="Server\Shares\NamedPipeShare.cs" />

View file

@ -31,14 +31,15 @@ namespace SMBLibrary.Server.SMB1
else
{
share = shares.GetShareFromName(shareName);
serviceName = ServiceName.DiskShare;
supportFlags = OptionalSupportFlags.SMB_SUPPORT_SEARCH_BITS | OptionalSupportFlags.SMB_CSC_CACHE_MANUAL_REINT;
if (share == null)
{
header.Status = NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
return new ErrorResponse(request.CommandName);
}
serviceName = ServiceName.DiskShare;
supportFlags = OptionalSupportFlags.SMB_SUPPORT_SEARCH_BITS | GetCachingSupportFlags(((FileSystemShare)share).CachingPolicy);
if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
{
state.LogToServer(Severity.Verbose, "Tree Connect to '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
@ -64,6 +65,21 @@ namespace SMBLibrary.Server.SMB1
}
}
private static OptionalSupportFlags GetCachingSupportFlags(CachingPolicy cachingPolicy)
{
switch (cachingPolicy)
{
case CachingPolicy.ManualCaching:
return OptionalSupportFlags.SMB_CSC_CACHE_MANUAL_REINT;
case CachingPolicy.AutoCaching:
return OptionalSupportFlags.SMB_CSC_CACHE_AUTO_REINT;
case CachingPolicy.VideoCaching:
return OptionalSupportFlags.SMB_CSC_CACHE_VDO;
default:
return OptionalSupportFlags.SMB_CSC_NO_CACHING;
}
}
private static TreeConnectAndXResponse CreateTreeConnectResponse(ServiceName serviceName, OptionalSupportFlags supportFlags)
{
TreeConnectAndXResponse response = new TreeConnectAndXResponse();

View file

@ -31,13 +31,13 @@ namespace SMBLibrary.Server.SMB2
else
{
share = shares.GetShareFromName(shareName);
shareType = ShareType.Disk;
shareFlags = ShareFlags.ManualCaching;
if (share == null)
{
return new ErrorResponse(request.CommandName, NTStatus.STATUS_OBJECT_PATH_NOT_FOUND);
}
shareType = ShareType.Disk;
shareFlags = GetShareCachingFlags(((FileSystemShare)share).CachingPolicy);
if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, @"\"))
{
state.LogToServer(Severity.Verbose, "Tree Connect to '{0}' failed. User '{1}' was denied access.", share.Name, session.UserName);
@ -62,6 +62,21 @@ namespace SMBLibrary.Server.SMB2
return response;
}
private static ShareFlags GetShareCachingFlags(CachingPolicy cachingPolicy)
{
switch (cachingPolicy)
{
case CachingPolicy.ManualCaching:
return ShareFlags.ManualCaching;
case CachingPolicy.AutoCaching:
return ShareFlags.AutoCaching;
case CachingPolicy.VideoCaching:
return ShareFlags.VdoCaching;
default:
return ShareFlags.NoCaching;
}
}
internal static SMB2Command GetTreeDisconnectResponse(TreeDisconnectRequest request, ISMBShare share, SMB2ConnectionState state)
{
SMB2Session session = state.GetSession(request.Header.SessionID);

View file

@ -0,0 +1,32 @@
namespace SMBLibrary.Server
{
/// <summary>
/// Client-Side Caching Policy
/// </summary>
public enum CachingPolicy
{
/// <summary>
/// The client can cache files that are explicitly selected by the user for offline use.
/// Automatic file-by-file reintegration is not allowed.
/// </summary>
ManualCaching,
/// <summary>
/// The client can automatically cache files that are used by the user for offline access.
/// Automatic file-by-file reintegration is allowed.
/// </summary>
AutoCaching,
/// <summary>
/// The client can automatically cache files that are used by the user for offline access.
/// Clients are permitted to work from their local cache even while online.
/// </summary>
VideoCaching,
/// <summary>
/// No offline caching is allowed for this share.
/// </summary>
NoCaching,
}
}

View file

@ -15,19 +15,26 @@ namespace SMBLibrary.Server
{
private string m_name;
private INTFileStore m_fileSystem;
private CachingPolicy m_cachingPolicy;
public event EventHandler<AccessRequestArgs> AccessRequested;
public FileSystemShare(string shareName, INTFileStore fileSystem)
public FileSystemShare(string shareName, INTFileStore fileSystem) : this(shareName, fileSystem, CachingPolicy.ManualCaching)
{
}
public FileSystemShare(string shareName, INTFileStore fileSystem, CachingPolicy cachingPolicy)
{
m_name = shareName;
m_fileSystem = fileSystem;
m_cachingPolicy = cachingPolicy;
}
public FileSystemShare(string shareName, IFileSystem fileSystem)
public FileSystemShare(string shareName, IFileSystem fileSystem, CachingPolicy cachingPolicy)
{
m_name = shareName;
m_fileSystem = new NTFileSystemAdapter(fileSystem);
m_cachingPolicy = cachingPolicy;
}
public bool HasReadAccess(SecurityContext securityContext, string path)
@ -68,5 +75,13 @@ namespace SMBLibrary.Server
return m_fileSystem;
}
}
public CachingPolicy CachingPolicy
{
get
{
return m_cachingPolicy;
}
}
}
}

View file

@ -13,9 +13,9 @@ namespace SMBLibrary.Server
{
public class SMBShareCollection : List<FileSystemShare>
{
public void Add(string shareName, IFileSystem fileSystem)
public void Add(string shareName, IFileSystem fileSystem, CachingPolicy cachingPolicy)
{
FileSystemShare share = new FileSystemShare(shareName, fileSystem);
FileSystemShare share = new FileSystemShare(shareName, fileSystem, cachingPolicy);
this.Add(share);
}