mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-05-15 17:06:59 +02:00
SMB Server application: Use NTDirectoryFileSystem by default
This commit is contained in:
parent
9fe283762a
commit
0cf1ac07bd
5 changed files with 2 additions and 511 deletions
|
@ -1,328 +0,0 @@
|
|||
/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
||||
*
|
||||
* You can redistribute this program and/or modify it under the terms of
|
||||
* the GNU Lesser Public License as published by the Free Software Foundation,
|
||||
* either version 3 of the License, or (at your option) any later version.
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Utilities;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace SMBServer
|
||||
{
|
||||
public class DirectoryFileSystem : FileSystem
|
||||
{
|
||||
private DirectoryInfo m_directory;
|
||||
// If a FileStream is already opened, calling File.SetCreationTime(path, ..) will result in ERROR_SHARING_VIOLATION,
|
||||
// So we must keep track of the opened file handles and use SetFileTime(handle, ..)
|
||||
private Dictionary<string, SafeFileHandle> m_openHandles = new Dictionary<string, SafeFileHandle>();
|
||||
|
||||
public DirectoryFileSystem(string path) : this(new DirectoryInfo(path))
|
||||
{
|
||||
}
|
||||
|
||||
public DirectoryFileSystem(DirectoryInfo directory)
|
||||
{
|
||||
m_directory = directory;
|
||||
}
|
||||
|
||||
public override FileSystemEntry GetEntry(string path)
|
||||
{
|
||||
ValidatePath(path);
|
||||
string fullPath = m_directory.FullName + path;
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
FileInfo file = new FileInfo(fullPath);
|
||||
bool isHidden = (file.Attributes & FileAttributes.Hidden) > 0;
|
||||
bool isReadonly = (file.Attributes & FileAttributes.ReadOnly) > 0;
|
||||
bool isArchived = (file.Attributes & FileAttributes.Archive) > 0;
|
||||
return new FileSystemEntry(path, file.Name, false, (ulong)file.Length, file.CreationTime, file.LastWriteTime, file.LastAccessTime, isHidden, isReadonly, isArchived);
|
||||
}
|
||||
else if (Directory.Exists(fullPath))
|
||||
{
|
||||
DirectoryInfo directory = new DirectoryInfo(fullPath);
|
||||
string fullName = FileSystem.GetDirectoryPath(path);
|
||||
bool isHidden = (directory.Attributes & FileAttributes.Hidden) > 0;
|
||||
bool isReadonly = (directory.Attributes & FileAttributes.ReadOnly) > 0;
|
||||
bool isArchived = (directory.Attributes & FileAttributes.Archive) > 0;
|
||||
return new FileSystemEntry(fullName, directory.Name, true, 0, directory.CreationTime, directory.LastWriteTime, directory.LastAccessTime, isHidden, isReadonly, isArchived);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public override FileSystemEntry CreateFile(string path)
|
||||
{
|
||||
ValidatePath(path);
|
||||
string fullPath = m_directory.FullName + path;
|
||||
FileStream stream = File.Create(fullPath);
|
||||
stream.Close();
|
||||
return GetEntry(path);
|
||||
}
|
||||
|
||||
public override FileSystemEntry CreateDirectory(string path)
|
||||
{
|
||||
ValidatePath(path);
|
||||
string fullPath = m_directory.FullName + path;
|
||||
Directory.CreateDirectory(fullPath);
|
||||
|
||||
return GetEntry(path);
|
||||
}
|
||||
|
||||
public override void Move(string source, string destination)
|
||||
{
|
||||
ValidatePath(source);
|
||||
ValidatePath(destination);
|
||||
string sourcePath = m_directory.FullName + source;
|
||||
string destinationPath = m_directory.FullName + destination;
|
||||
if (File.Exists(sourcePath))
|
||||
{
|
||||
File.Move(sourcePath, destinationPath);
|
||||
}
|
||||
else // Entry is a directory
|
||||
{
|
||||
Directory.Move(sourcePath, destinationPath);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Delete(string path)
|
||||
{
|
||||
ValidatePath(path);
|
||||
string fullPath = m_directory.FullName + path;
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
File.Delete(fullPath);
|
||||
}
|
||||
else if (Directory.Exists(fullPath))
|
||||
{
|
||||
Directory.Delete(fullPath, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
public override List<FileSystemEntry> ListEntriesInDirectory(string path)
|
||||
{
|
||||
ValidatePath(path);
|
||||
if (path == String.Empty)
|
||||
{
|
||||
path = @"\";
|
||||
}
|
||||
string fullPath = m_directory.FullName + path;
|
||||
DirectoryInfo directory = new DirectoryInfo(fullPath);
|
||||
List<FileSystemEntry> result = new List<FileSystemEntry>();
|
||||
foreach (DirectoryInfo subDirectory in directory.GetDirectories())
|
||||
{
|
||||
string fullName = GetRelativeDirectoryPath(subDirectory.FullName);
|
||||
bool isHidden = (subDirectory.Attributes & FileAttributes.Hidden) > 0;
|
||||
bool isReadonly = (subDirectory.Attributes & FileAttributes.ReadOnly) > 0;
|
||||
bool isArchived = (subDirectory.Attributes & FileAttributes.Archive) > 0;
|
||||
result.Add(new FileSystemEntry(fullName, subDirectory.Name, true, 0, subDirectory.CreationTime, subDirectory.LastWriteTime, subDirectory.LastAccessTime, isHidden, isReadonly, isArchived));
|
||||
}
|
||||
foreach (FileInfo file in directory.GetFiles())
|
||||
{
|
||||
string fullName = GetRelativePath(file.FullName);
|
||||
bool isHidden = (file.Attributes & FileAttributes.Hidden) > 0;
|
||||
bool isReadonly = (file.Attributes & FileAttributes.ReadOnly) > 0;
|
||||
bool isArchived = (file.Attributes & FileAttributes.Archive) > 0;
|
||||
result.Add(new FileSystemEntry(fullName, file.Name, false, (ulong)file.Length, file.CreationTime, file.LastWriteTime, file.LastAccessTime, isHidden, isReadonly, isArchived));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public override Stream OpenFile(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options)
|
||||
{
|
||||
ValidatePath(path);
|
||||
string fullPath = m_directory.FullName + path;
|
||||
const int DefaultBufferSize = 4096;
|
||||
FileStream fileStream = new FileStream(fullPath, mode, access, share, DefaultBufferSize, options);
|
||||
if (!m_openHandles.ContainsKey(fullPath.ToLower()))
|
||||
{
|
||||
m_openHandles.Add(fullPath.ToLower(), fileStream.SafeFileHandle);
|
||||
}
|
||||
StreamWatcher watcher = new StreamWatcher(fileStream);
|
||||
watcher.Closed += new EventHandler(Stream_Closed);
|
||||
return watcher;
|
||||
}
|
||||
|
||||
private void Stream_Closed(object sender, EventArgs e)
|
||||
{
|
||||
StreamWatcher watcher = (StreamWatcher)sender;
|
||||
FileStream fileStream = (FileStream)watcher.Stream;
|
||||
m_openHandles.Remove(fileStream.Name.ToLower());
|
||||
}
|
||||
|
||||
public override void SetAttributes(string path, bool? isHidden, bool? isReadonly, bool? isArchived)
|
||||
{
|
||||
ValidatePath(path);
|
||||
string fullPath = m_directory.FullName + path;
|
||||
if (File.Exists(fullPath) || Directory.Exists(fullPath))
|
||||
{
|
||||
FileAttributes attributes = File.GetAttributes(fullPath);
|
||||
if (isHidden.HasValue)
|
||||
{
|
||||
if (isHidden.Value)
|
||||
{
|
||||
attributes |= FileAttributes.Hidden;
|
||||
}
|
||||
else
|
||||
{
|
||||
attributes &= ~FileAttributes.Hidden;
|
||||
}
|
||||
}
|
||||
|
||||
if (isReadonly.HasValue)
|
||||
{
|
||||
if (isReadonly.Value)
|
||||
{
|
||||
attributes |= FileAttributes.ReadOnly;
|
||||
}
|
||||
else
|
||||
{
|
||||
attributes &= ~FileAttributes.ReadOnly;
|
||||
}
|
||||
}
|
||||
|
||||
if (isArchived.HasValue)
|
||||
{
|
||||
if (isArchived.Value)
|
||||
{
|
||||
attributes |= FileAttributes.Archive;
|
||||
}
|
||||
else
|
||||
{
|
||||
attributes &= ~FileAttributes.Archive;
|
||||
}
|
||||
}
|
||||
|
||||
File.SetAttributes(fullPath, attributes);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetDates(string path, DateTime? creationDT, DateTime? lastWriteDT, DateTime? lastAccessDT)
|
||||
{
|
||||
ValidatePath(path);
|
||||
string fullPath = m_directory.FullName + path;
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
SafeFileHandle openHandle;
|
||||
if (m_openHandles.TryGetValue(fullPath.ToLower(), out openHandle))
|
||||
{
|
||||
if (creationDT.HasValue)
|
||||
{
|
||||
Win32Native.SetCreationTime(openHandle, creationDT.Value);
|
||||
}
|
||||
|
||||
if (lastWriteDT.HasValue)
|
||||
{
|
||||
Win32Native.SetLastWriteTime(openHandle, lastWriteDT.Value);
|
||||
}
|
||||
|
||||
if (lastAccessDT.HasValue)
|
||||
{
|
||||
Win32Native.SetLastAccessTime(openHandle, lastAccessDT.Value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (creationDT.HasValue)
|
||||
{
|
||||
File.SetCreationTime(fullPath, creationDT.Value);
|
||||
}
|
||||
|
||||
if (lastWriteDT.HasValue)
|
||||
{
|
||||
File.SetLastWriteTime(fullPath, lastWriteDT.Value);
|
||||
}
|
||||
|
||||
if (lastAccessDT.HasValue)
|
||||
{
|
||||
File.SetLastAccessTime(fullPath, lastAccessDT.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (Directory.Exists(fullPath))
|
||||
{
|
||||
if (creationDT.HasValue)
|
||||
{
|
||||
Directory.SetCreationTime(fullPath, creationDT.Value);
|
||||
}
|
||||
|
||||
if (lastWriteDT.HasValue)
|
||||
{
|
||||
Directory.SetLastWriteTime(fullPath, lastWriteDT.Value);
|
||||
}
|
||||
|
||||
if (lastAccessDT.HasValue)
|
||||
{
|
||||
Directory.SetLastAccessTime(fullPath, lastAccessDT.Value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidatePath(string path)
|
||||
{
|
||||
if (path != String.Empty && !path.StartsWith(@"\"))
|
||||
{
|
||||
throw new ArgumentException("Path must start with a backslash");
|
||||
}
|
||||
|
||||
if (path.Contains(@"\..\"))
|
||||
{
|
||||
throw new ArgumentException("Given path is not allowed");
|
||||
}
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
DriveInfo drive = new DriveInfo(m_directory.FullName.Substring(0, 2));
|
||||
return drive.DriveFormat;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Size
|
||||
{
|
||||
get
|
||||
{
|
||||
DriveInfo drive = new DriveInfo(m_directory.FullName.Substring(0, 2));
|
||||
return drive.TotalSize;
|
||||
}
|
||||
}
|
||||
|
||||
public override long FreeSpace
|
||||
{
|
||||
get
|
||||
{
|
||||
DriveInfo drive = new DriveInfo(m_directory.FullName.Substring(0, 2));
|
||||
return drive.AvailableFreeSpace;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetRelativePath(string fullPath)
|
||||
{
|
||||
return fullPath.Substring(m_directory.FullName.Length);
|
||||
}
|
||||
|
||||
private string GetRelativeDirectoryPath(string fullPath)
|
||||
{
|
||||
return GetRelativePath(GetDirectoryPath(fullPath));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,113 +0,0 @@
|
|||
/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
||||
*
|
||||
* You can redistribute this program and/or modify it under the terms of
|
||||
* the GNU Lesser Public License as published by the Free Software Foundation,
|
||||
* either version 3 of the License, or (at your option) any later version.
|
||||
*/
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SMBServer
|
||||
{
|
||||
/// <summary>
|
||||
/// A wrapper for the stream class that notify when the stream is closed
|
||||
/// </summary>
|
||||
public class StreamWatcher : Stream
|
||||
{
|
||||
private Stream m_stream;
|
||||
|
||||
public event EventHandler Closed;
|
||||
|
||||
public StreamWatcher(Stream stream)
|
||||
{
|
||||
m_stream = stream;
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
return m_stream.Read(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
m_stream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
m_stream.Close();
|
||||
EventHandler handler = Closed;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
m_stream.Flush();
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return m_stream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
m_stream.SetLength(value);
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_stream.CanSeek;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_stream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_stream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_stream.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_stream.Position;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_stream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Stream Stream
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
||||
*
|
||||
* You can redistribute this program and/or modify it under the terms of
|
||||
* the GNU Lesser Public License as published by the Free Software Foundation,
|
||||
* either version 3 of the License, or (at your option) any later version.
|
||||
*/
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace SMBServer
|
||||
{
|
||||
public class Win32Native
|
||||
{
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool SetFileTime(SafeFileHandle hFile, ref long lpCreationTime, IntPtr lpLastAccessTime, IntPtr lpLastWriteTime);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool SetFileTime(SafeFileHandle hFile, IntPtr lpCreationTime, ref long lpLastAccessTime, IntPtr lpLastWriteTime);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool SetFileTime(SafeFileHandle hFile, IntPtr lpCreationTime, IntPtr lpLastAccessTime, ref long lpLastWriteTime);
|
||||
|
||||
internal static void SetCreationTime(SafeFileHandle hFile, DateTime creationTime)
|
||||
{
|
||||
long fileTime = creationTime.ToFileTime();
|
||||
bool success = SetFileTime(hFile, ref fileTime, IntPtr.Zero, IntPtr.Zero);
|
||||
if (!success)
|
||||
{
|
||||
uint error = (uint)Marshal.GetLastWin32Error();
|
||||
throw new IOException("Win32 error: " + error);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void SetLastAccessTime(SafeFileHandle hFile, DateTime lastAccessTime)
|
||||
{
|
||||
long fileTime = lastAccessTime.ToFileTime();
|
||||
bool success = SetFileTime(hFile, IntPtr.Zero, ref fileTime, IntPtr.Zero);
|
||||
if (!success)
|
||||
{
|
||||
uint error = (uint)Marshal.GetLastWin32Error();
|
||||
throw new IOException("Win32 error: " + error);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void SetLastWriteTime(SafeFileHandle hFile, DateTime lastWriteTime)
|
||||
{
|
||||
long fileTime = lastWriteTime.ToFileTime();
|
||||
bool success = SetFileTime(hFile, IntPtr.Zero, IntPtr.Zero, ref fileTime);
|
||||
if (!success)
|
||||
{
|
||||
uint error = (uint)Marshal.GetLastWin32Error();
|
||||
throw new IOException("Win32 error: " + error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -36,9 +36,6 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DirectoryFileSystem\DirectoryFileSystem.cs" />
|
||||
<Compile Include="DirectoryFileSystem\StreamWatcher.cs" />
|
||||
<Compile Include="DirectoryFileSystem\Win32Native.cs" />
|
||||
<Compile Include="LogWriter.cs" />
|
||||
<Compile Include="NetworkInterfaceHelper.cs" />
|
||||
<Compile Include="ServerUI.cs">
|
||||
|
|
|
@ -20,6 +20,7 @@ using SMBLibrary;
|
|||
using SMBLibrary.Authentication.GSSAPI;
|
||||
using SMBLibrary.Authentication.NTLM;
|
||||
using SMBLibrary.Server;
|
||||
using SMBLibrary.Win32;
|
||||
using SMBLibrary.Win32.Security;
|
||||
using Utilities;
|
||||
|
||||
|
@ -101,14 +102,6 @@ namespace SMBServer
|
|||
m_logWriter = new LogWriter();
|
||||
m_server.LogEntryAdded += new EventHandler<LogEntry>(m_logWriter.OnLogEntryAdded);
|
||||
|
||||
foreach (FileSystemShare share in shares)
|
||||
{
|
||||
if (share.FileStore is NTFileSystemAdapter)
|
||||
{
|
||||
((NTFileSystemAdapter)share.FileStore).LogEntryAdded += new EventHandler<LogEntry>(m_logWriter.OnLogEntryAdded);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
m_server.Start(serverAddress, transportType, chkSMB1.Checked, chkSMB2.Checked);
|
||||
|
@ -175,7 +168,7 @@ namespace SMBServer
|
|||
List<string> readAccess = ReadAccessList(readAccessNode);
|
||||
XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
|
||||
List<string> writeAccess = ReadAccessList(writeAccessNode);
|
||||
FileSystemShare share = new FileSystemShare(shareName, new DirectoryFileSystem(sharePath));
|
||||
FileSystemShare share = new FileSystemShare(shareName, new NTDirectoryFileSystem(sharePath));
|
||||
share.AccessRequested += delegate(object sender, AccessRequestArgs args)
|
||||
{
|
||||
bool hasReadAccess = Contains(readAccess, "Users") || Contains(readAccess, args.UserName);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue