talaloni / smblibrary Goto Github PK
View Code? Open in Web Editor NEWFree, Open Source, User-Mode SMB 1.0/CIFS, SMB 2.0, SMB 2.1 and SMB 3.0 server and client library
License: GNU Lesser General Public License v3.0
Free, Open Source, User-Mode SMB 1.0/CIFS, SMB 2.0, SMB 2.1 and SMB 3.0 server and client library
License: GNU Lesser General Public License v3.0
Hi,
This may not be a valid issue but I figured I'd send some info anyways.
I am attempting to get the security information from a file using the SMBLibrary
.
My user is an admin and has full access to the file and the entire share.
I try to get the SACL:
status = tree.GetSecurityInformation(out SecurityDescriptor securityDescriptor,
handle,
SecurityInformation.SACL_SECURITY_INFORMATION)
When I try this, I get an error:
71 9.074730848 192.168.1.229 192.168.1.57 SMB2 174 GetInfo Request SEC_INFO/SMB2_SEC_INFO_00 File: nick - Copy (2).txt
72 9.082573875 192.168.1.57 192.168.1.229 SMB2 143 GetInfo Response, Error: STATUS_ACCESS_DENIED
Wireshark here: https://drive.google.com/file/d/15uC01XA_xMszqfcmHEFCOj--x83fG21d/view?usp=sharing
I can retrieve DACLs, Group ACL and User ACL just fine.
How can I write a large file, larger than MaxWriteSize
? I've tried braking the file into multiple chunks and calling store.WriteFile
multiple times, but it doesn't append to the file but overwrites it instead. I've tried using same and new file handle with no success. Here's my code:
var ms = new MemoryStream(content);
uint bufferSize = 4096;
var buffer = new byte[bufferSize];
long read = 0;
do
{
read = ms.Read(buffer, 0, (int)bufferSize);
var chunk = new byte[read];
Array.Copy(buffer, 0, chunk, 0, read);
store.CreateFile(out var handle, out var fileStatus, outputFile,
(AccessMask)(FileAccessMask.SYNCHRONIZE | FileAccessMask.FILE_APPEND_DATA),
FileAttributes.Normal, ShareAccess.None, CreateDisposition.FILE_OPEN,
CreateOptions.FILE_NON_DIRECTORY_FILE, null);
store.WriteFile(out var _, handle, 0, chunk);
store.CloseFile(handle);
} while (read > 0 && read == bufferSize);
what am I doing wrong?
Hi,
I'm trying to set the buffer to 1MB, but when using SMB2, I'm getting STATUS_INVALID_PARAMETER if I try to read more than 65k.
If I switch to SMB1, it doesn't fail, but returns me 65k even if I request more.
We're currently using SharpCifs.Std library, which is able to return 1MB (although it only supports SMB1), so it's probably not a server limitation, but I can't figure out what needs tweaking so I can get more than 65k in one read.
Hi Tal,
SMB1 got SMB1FileStoreHelper class which support rename using SetFileInformation.
It seems SMB2FileStore class doesn't implement SetFileInformation so file rename is not supported on SMB2, am i missing something ?
Thanks in advance for any help/pointers.
Hello,
I'm currently running into a consistent Not Enough Credits when using the SMB2Client against a share configured with Samba on a CentOS host.
When trying to copy ~260 96MB files from the samba share to a local directory all at once in parallel, I'm seeing the Not Enough Credits
exception fairly regularly. (Sometimes `STATUS_INVALID_SMB as well, but I have a feeling it's because of the amount/rate of which I'm requesting from the share)
I've tried changing the operation to copy in 5-10 file batches, and it's reduced occurrences of the exception, but I still receive the exception.
I think it's good to note that each operation of a file is it's own instance of SMB2Client
I've tried bumping up smb2 max credits
in my smb.conf to 15000
and it's significantly reduced the number of occurrences of the exception when operating in batches of 5.
Maybe it's still my lack of understanding of how credits work, but from reading MS-SMB specs and researching on SMB credits, I have a feeling I'm going about it the wrong way.
I'd like to be able to do a fire and forget, and copy all files in parallel or bump up my batch size to something larger, but I'd run the risk of running out of credits.
Is there a suggested/preferred way on how to go about utilizing/consuming SMB2Client in such a way that guards itself from running into Not Enough Credits
?
Hi.
Thank you for SMBLibrary.
I'm able to start the SMB Server on Windows 10 and browse the files and folders on a remote machine.
However, I'm unable to browse the SMB Server on the same machine.
My machine is connected to a domain.
I disabled the "Server" windows service.
I also disabled "File and Printer Sharing for Microsoft Networks" on all network adapters.
I started the SMB Server as follows:
I can connect to the SMB Server in Windows Explorer in Windows XP for example using \\machineName.
However, when I use the same machine, with \\machineName or \\IPAddress, I get one of the following errors:
Log:
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] New connection request accepted
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB1 message received: 1 requests, First request: SMB_COM_NEGOTIATE, Packet length: 73
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 228
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 179
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 241
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 500
Information 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] Session Setup: User 'rami.abughazaleh' failed authentication (Domain: 'SV', Workstation: 'AG-WS-RAMI3', OS version: '10.0.19041'), logged in as guest.
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30021] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 85
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] New connection request accepted
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 240
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 179
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 241
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 500
Information 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] Session Setup: User 'rami.abughazaleh' failed authentication (Domain: 'SV', Workstation: 'AG-WS-RAMI3', OS version: '10.0.19041'), logged in as guest.
Verbose 2020-07-21 13:41:20 [SMB Server] [10.0.0.1:30022] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 85
Settings.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<Shares>
<Share Name="Shared" Path="C:\Shared">
<ReadAccess Accounts="*"></ReadAccess>
<WriteAccess Accounts="Admin,Test"></WriteAccess>
</Share>
</Shares>
<Users>
<User AccountName="Admin" Password="admin"></User>
<User AccountName="Guest" Password=""></User>
<User AccountName="Test" Password="test"></User>
<User AccountName="SV\rami.abughazaleh" Password="MyPassword"></User>
</Users>
</Settings>
Any ideas?
Thank you.
I started the smbserver with default settings. I am using Direct TCP Transport (port 445)
and IP Any
.
Then on the same
machine, in file explorer, i am adding : Map network drive
. I get authentication prompt. When i type in \Admin
and admin
(or \Test
& test
) in username and password, i get wrong password error prompt.
How can i debug what is going wrong ?
Here are the logs :
Information 2019-03-03 20:03:59 [SMB Server] Starting server
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] New connection request
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] SMB1 message received: 1 requests, First request: SMB_COM_NEGOTIATE, Packet length: 73
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 183
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:04:10 [SMB Server] [127.0.0.1:53740] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 699
Information 2019-03-03 20:04:11 [SMB Server] [127.0.0.1:53740] Session Setup: User 'Test' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:04:11 [SMB Server] [127.0.0.1:53740] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] New connection request
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] SMB1 message received: 1 requests, First request: SMB_COM_NEGOTIATE, Packet length: 73
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 166
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:04:39 [SMB Server] [127.0.0.1:53742] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 699
Information 2019-03-03 20:04:40 [SMB Server] [127.0.0.1:53742] Session Setup: User 'Test' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:04:40 [SMB Server] [127.0.0.1:53742] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] New connection request
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 166
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 699
Information 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] Session Setup: User 'Test' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53744] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53745] New connection request
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53745] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53745] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53745] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 166
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53745] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:04:44 [SMB Server] [127.0.0.1:53745] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 699
Information 2019-03-03 20:04:45 [SMB Server] [127.0.0.1:53745] Session Setup: User 'Test' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:04:45 [SMB Server] [127.0.0.1:53745] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] New connection request
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB1 message received: 1 requests, First request: SMB_COM_NEGOTIATE, Packet length: 73
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 166
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 701
Information 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] Session Setup: User 'Admin' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:04:57 [SMB Server] [127.0.0.1:53746] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] New connection request
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] SMB1 message received: 1 requests, First request: SMB_COM_NEGOTIATE, Packet length: 73
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 183
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:05:43 [SMB Server] [127.0.0.1:53775] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 699
Information 2019-03-03 20:05:44 [SMB Server] [127.0.0.1:53775] Session Setup: User 'Test' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:05:44 [SMB Server] [127.0.0.1:53775] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] New connection request
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 166
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 699
Information 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] Session Setup: User 'Test' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53779] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53780] New connection request
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53780] SMB2 request chain received: 1 requests, First request: Negotiate, Packet length: 178
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53780] SMB2 response chain queued: Response count: 1, First response: Negotiate, Packet length: 162
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53780] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 166
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53780] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 413
Verbose 2019-03-03 20:05:54 [SMB Server] [127.0.0.1:53780] SMB2 request chain received: 1 requests, First request: SessionSetup, Packet length: 699
Information 2019-03-03 20:05:55 [SMB Server] [127.0.0.1:53780] Session Setup: User 'Test' failed authentication (Domain: 'FAREAST', Workstation: 'XXXXX-TMP', OS version: '10.0.17763'), NTStatus: STATUS_LOGON_FAILURE
Verbose 2019-03-03 20:05:55 [SMB Server] [127.0.0.1:53780] SMB2 response chain queued: Response count: 1, First response: SessionSetup, Packet length: 77
Information 2019-03-03 20:06:17 [SMB Server] Stopping server
If I'm interpreting MS Docs correctly, SMB 2.1 client should work when secure transfer is disabled in the storage account settings.
Connecting to an Azure File share over SMB without encryption fails when secure transfer is required for the storage account. Examples of insecure connections include those made over SMB 2.1, SMB 3.0 without encryption, or some versions of the Linux SMB client.
var client = new SMB2Client();
if (client.Connect(iPAddress, SMBTransportType.DirectTCPTransport)) {
NTStatus status = client.Login(domain, userName, password); // These are correct.
// status is NTStatus.STATUS_ACCESS_DENIED
}
Please make available via NuGet package manager.
There are no classes in the solution that implement IFileSystem
I was able to figure out how to create a class to implement IFileSystem and include it in the server, but it fails once I get a level down in tree. A simple example of IFileSystem class that just wraps System.Io.File, Directory for a demo class would be very helpful.
Write example and documentation on how to use the server and client in a target application
Hi @TalAloni
Regarding an closed issue here:
#4
Would you mind updating the Github license to LGPL on the github project, if it's not too much trouble?
Even though the license is on the source files, i'm getting push back about using it because the github project doesn't have a license on it, our Jenkins CI is spitting it back at us as "unlicensed." It checks the github project api for the license.
Really great work, thanks you!
It is possible to show different shares for each authenticated username?
In all the project you use byte[], which can be easy changed to Span. But, of course, this will work only for .NET Core.
For .NET 2.0+ you can make your own Span, which will look like
public struct Span<T>
{
private byte[] target;
private int offset;
private int size;
public Span<T> Slice(int offset) => new Slice(target, this.offset + offset, size - (this.offset + offset));
public Span<T> Slice(int offset, int size) => new Slice(target, this.offset + offset, size);
}
of course, with bounds checking.
In places of usage:
// for native Span:
Span<byte> buffer = stackalloc byte[NT ... .ParametersLength];
This will reduce all allocations to nothing.
This library would be a potential candidate for integrating SMBv2 support in WindowsCE based products. Is this something you'd be interested in to officially support? I already started a quick port at
https://github.com/EndlessDelirium/SMBLibrary/tree/features/WindowsCE
create winrar file failure in share folder
Have you already tried to implement virtual share tree based on DB content?
It is sufficient to make a new class based on INTFileStore for this?
This lib is really cool, great work!
Are you intending to support SMB 3.x versions with the library. Reading the SMB specifications its hard to determine what would be required.
Hello,
Do you have any plans to add SMB v3 support?
Thanks,
Alin G
Hi there, I'm interested in using SMBLibrary for a project I'm working on, but I'm a bit wary of running privileged code unnecessarily. Similarly, I'm not a huge fan of running my IDE as root to be able to debug my code. However, unless I do, attempts to listen to port 139/445 fail (I'm on Linux, though I imagine it's similar on other operating systems).
Is it possible to add some kind of mechanism for allowing a non-privileged mode? I appreciate that the SMB spec requires listening on those ports, but I'm wondering if it would work to allow listening on port 1139/1445 (say), and then forward data using something like xinetd.
In terms of implementation, it looks like this might be as straightforward as making SMBServer.Start a virtual method. Or maybe making NetBiosOverTCPPort and DirectTCPPort virtual readonly rather than const (or both! :))
Thoughts?
The issue looks like issue #27, but the status is (often, not always) returned by ListShares. Following calls to ListShares then return STATUS_NOT_SUPPORTED. The SMB server is a Synology NAS.
Hello
I wrote a custom file system trying to implement a SMB server in front of a REST api for a digital asset management system so the assets can be exposed via UNC share. I can list the folders and files just perfectly, except opening the file is not working. I initialy tried return the Stream - which is the download stream of the file on the web server - but it always complained the files was invalid. Next I tried to download the file to temp storage locally and then return the file via filestream, this either blows up, complains the file is locked or opens up the file. I had to change the NTFileSystemAdapter to allow the rewriting of the path so the GetEntry is called with a UNC path, it then performs the REST api call and downloads the file and I rewrite the path to the file that was downloaded locally. This works on and off...
Any tips or clues on how to get this working reliably?
For my tests , maybe the code shall be
int entryLength = DerEncodingHelper.ReadLength(buffer, ref offset);
int entryEndOffset = offset + entryLength;
while (offset < sequenceEndOffset)
{
tag = ByteReader.ReadByte(buffer, ref offset);
When connecting from a windows server 2016 machine to the smb server I get the following error: IOCTL failed. CTL Code: FSCTL_VALIDATE_NEGOTIATE_INFO. NTStatus: STATUS_NOT_SUPPORTED.
I'm not an expert in this protocol and on the web I found I should disable RequiresSecureNegotiate but the listed powershell command does not work it seems.
I trying to get the list of shares from a Windows Server. I can access the list of shares with Windows File Explorer but I get ACCESS_DENIED when using the library. Any hints?
var smb = new SMB2Client();
bool connected = smb.Connect(new IPAddress(addressBytes), SMBTransportType.DirectTCPTransport);
if (connected)
{
var status = smb.Login("myDomain", "myUserName", "myPassword", AuthenticationMethod.NTLMv2);
//status is STATUS_SUCCESS :)
//...
var shares = smb.ListShares(out status);
//status is STATUS_ACCESS_DENIED :(
//...
var fileStore = smb.TreeConnect("myShareName", out _status)
//status is STATUS_ACCESS_DENIED :(
}
I have the ports opened, I can connect via telnet over TCP, but I cannot get Windows to allow me to navigate to the share.
I have no idea what I am doing wrong.
Hi,
I am trying to use this library to connect to existing shares on windows machines in order to list/read/write files/directories.
The highlight seem to be creating a server side so i am not sure if the library do indeed support client implementations .
I was able to connect to a remote machine and list the available shares (available to my user) with the following code :
var smb = new SMB2Client();
var b = smb.Connect(new System.Net.IPAddress(new byte[] { 192, 168, 120, 10 }), SMBLibrary.SMBTransportType.DirectTCPTransport);
var status = smb.Login(string.Empty, Username, password);
SMBLibrary.NTStatus actionStatus;
var shares = smb.ListShares(out actionStatus);
shares variable contains the available shares, from here i could not find a way to list the share content, create a file , read an existing file.
My lead is that i need to use TreeConnect method on smb client :
var tree = smb.TreeConnect("shared", out status);
object handle;
SMBLibrary.FileStatus fs;
var x = tree.CreateFile(out handle, out fs, "zzz.txt", SMBLibrary.AccessMask.GENERIC_ALL, SMBLibrary.FileAttributes.Normal, SMBLibrary.ShareAccess.None, SMBLibrary.CreateDisposition.FILE_CREATE, 0, null);
But in this case i keep getting fs = FILE_DOES_NOT_EXIST and x = Access denied (i do have access).
Any chance for a working code examples ?
Thanks
Hi the tuesday patch (june 2020) breaks the SMB.
Uninstall KB4561666 on windows 2012R2 solve the problem temporarily...
Hello! Thanks for this amazing project.
I've got an issue when trying to get folder access information. As I understood I added ACCESS_SYSTEM_SECURITY
to access mask. But when I call .GetSecurityInformation
it throws this exception:
Unable to cast object of type 'System.Object' to type 'SMBLibrary.SMB2.FileID'.
at SMBLibrary.Client.SMB2FileStore.GetSecurityInformation(SecurityDescriptor& result, Object handle, SecurityInformation securityInformation)
using
SMBLibrary Version="1.4.1"
I looked at some examples and my code looks like this
var host = path.Split("xxx.xxx.xxx.xxx");
var dir = "Dir"
var client = new SMB2Client();
var success = client.Connect(IPAddress.Parse(host), SMBTransportType.DirectTCPTransport);
if (success)
{
var status = client.Login(domain, username, password);
// Success
SMB2FileStore fileStore = client.TreeConnect(dir, out status) as SMB2FileStore;
if (fileStore != null)
{
object handle = new object();
status = fileStore.CreateFile(out handle, out var fileStatus, Dir, AccessMask.ACCESS_SYSTEM_SECURITY | AccessMask.SYNCHRONIZE | (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null);
List<QueryDirectoryFileInformation> result = new List<QueryDirectoryFileInformation>();
//var tree = fileStore.QueryDirectory(out result, handle, "*", FileInformationClass.FileFullDirectoryInformation);
object secHandle = new object();
SMBLibrary.SecurityDescriptor secDescr;
var secStatus = fileStore.GetSecurityInformation(out secDescr, secHandle, SecurityInformation.GROUP_SECURITY_INFORMATION);
}
}
I am trying to use SMB2Client for downloading any file using a buffer size of 1K. At random, for large/medium files, the download halts midway, and any subsequent request starts getting STATUS_INVALID_SMB. For very small files, it works fine. My helper method for download goes below:
public static void DownloadFile(string shareUncPath, string localFullPath, string user, string password)
{
SMBLibrary.Client.SMB2Client smbClient = new SMBLibrary.Client.SMB2Client();
//Connect
string[] uncPathComponents = shareUncPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
if (uncPathComponents.Length < 3) throw new Exception("Need UNC Path to a file");
IPAddress ipAddr = Dns.GetHostEntry(uncPathComponents[0]).AddressList.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork);
if (ipAddr == null || smbClient.Connect(ipAddr, SMBTransportType.DirectTCPTransport) == false)
{
throw new Exception(NTStatus.STATUS_LOGON_FAILURE.ToString());
}
//Login
NTStatus status = smbClient.Login(string.Empty, user, password);
if (status != NTStatus.STATUS_SUCCESS)
{
throw new Exception(status.ToString());
}
//Tree connect to Share
SMBLibrary.Client.SMB2FileStore fileStore = smbClient.TreeConnect(uncPathComponents[1], out status) as SMBLibrary.Client.SMB2FileStore;
if (status != NTStatus.STATUS_SUCCESS || fileStore == null)
{
throw new Exception(status.ToString());
}
// Open existing file for reading
string srcPath = string.Join("\\", uncPathComponents, 2, uncPathComponents.Length - 2);
object handle;
FileStatus fileStatus;
status = fileStore.CreateFile(out handle, out fileStatus, srcPath, AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
if (status != NTStatus.STATUS_SUCCESS)
{
throw new Exception(status.ToString());
}
//Retrieve file length / size
FileInformation fileInfo;
status = fileStore.GetFileInformation(out fileInfo, handle, FileInformationClass.FileStandardInformation);
if (status != NTStatus.STATUS_SUCCESS)
{
throw new Exception(status.ToString());
}
FileStandardInformation fileStdInfo = fileInfo as FileStandardInformation;
long length = fileStdInfo.EndOfFile;
//Copy in chunks
int position = 0;
using (System.IO.FileStream output = new System.IO.FileStream(localFullPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
byte[] buffer;
do
{
int count = (int)Math.Min(1024, length - position);
status = fileStore.ReadFile(out buffer, handle, position, count);
if (status == NTStatus.STATUS_END_OF_FILE || buffer?.Length == 0)
{
//done
break;
}
else if (status != NTStatus.STATUS_SUCCESS)
{
throw new Exception(status.ToString());
}
output.Write(buffer, 0, buffer.Length);
position += buffer.Length;
}
while (position < length);
}
}
Using this method is as simple as below. Can be embedded in one of your test for quick check:
DownloadFile("\\\\localhost\\Share\\large_300mb_file.zip", "D:\\large_300mb_file.zip", "user", "password");
Debugging revealed that there is a response timeout in WaitForCommand(...)
and it returns null
, so STATUS_INVALID_SMB
is being returned from SMB2FileStore.ReadFile(…)
public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
{
…
TrySendCommand(request);
SMB2Command response = m_client.WaitForCommand(SMB2CommandName.Read);
if (response != null)
{
...
}
return NTStatus.STATUS_INVALID_SMB;
}
I have to use a chunked download of few KBs only as the code may run on low memory devices. This seem to be a problem with protocol handling. Can you please provide some pointers to a fix?
Can you help me? I want to get the connect user in method "GetNetrShareEnumResponse " of file "SMBLibrary\Services\ServerService\ServerService.cs"? Is there a way to get the userName anywhere, such as "session" in asp.net.
Thank you very much!
Hi,
first - thank you very much for publishing this interesting library.
I'm trying to use this library for client access using the code from issue #9.
I tried to connect to
I'm always ending up here:
In all cases, the tag is not DerEncodingTag.ByteArray but DerEncodingTag.Sequence instead.
I can't imagine an explanation for this. It's really weird that it happens in all 3 cases. These cases are different otherwise (different buffer length, different parsed mechanisms).
When I ignore the exception, all subsequent operations against the Samba machine are successful (but there's guest access enabled), but not for the Windows servers...
Any ideas?
Hello,
I used successfully SetFileInformation with a FileRenameInformation2 parameter to rename remote items using SMB2.
Is it possible to do the same with SMB1 ?
I tried the following with no success:
Have you got any suggestion about the way to proceed?
Hi. I am trying to get the security info for a file.
But i keep getting status = STATUS_NOT_SUPPORTED
when trying.
I'm trying to get the SIDs of the effective file acls.
Here is my example: https://github.com/nddipiazza/SambaFetcher/blob/SambaLibrary/SmbFetcher/SmbServerModule.cs
In particular here is where I'm trying to get the security information for a given file. https://github.com/nddipiazza/SambaFetcher/blob/SambaLibrary/SmbFetcher/SmbServerModule.cs#L123
I'm missing something. Can you help me figure out how to get access controls for a file?
Hi -
I don't see any licensing information. I would be great if you can clarify that. Or just add a license. Apache 2.0 or MIT are best if your intent is sharing.
thanks
We try to create a SMB server and access it from a mobile app - FE File Explorer.
iOS:
https://itunes.apple.com/app/id510282524
Android:
https://play.google.com/store/apps/details?id=com.skyjos.apps.fileexplorerfree
We're able to browse shares and download files in FE File Explorer, but we get failure when you try to upload files. Then we go back to SMBLibrary V1.3.9 and everything goes fine.
Any ideas?
Hi and thanks.
QUESTION:
how can i read/write/create a file in a subfolder ???
i.e.:
\SERVER\SHARENAME\FOLDER1\FILE.TXT
\SERVER\SHARENAME\FOLDER1\FOLDER2\FILE.TXT
I've been able to get file and dir loading to work, but when spaces are involved nothing I try fixes it.
My baseline code is:
status = fileStore.CreateFile(out handle, out fileStatus, directoryPath, AccessMask.SYNCHRONIZE | (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null);
I've tried directoryPath.Replace(" ", "%20"), surrounding directoryPath with double quotes, surrounding it with backslashes, and combining the above, and no matter what it returns FileStatus.FILE_DOES_NOT_EXIST. Is there something I'm missing with how to use this?
I try to get the list of the shares on mny Window 10 pro maschine. I do this:
smb = new SMBLibrary.Client.SMB1Client();
// the IP is correct 192.168...and so on
smb.Connect(NetHelper.GetHostIPAdr("HU-ENTWALT"), SMBTransportType.DirectTCPTransport);
// i must use myDomain because my LoginName is "myName@myDomain"
ntStatus = smb.Login(myDomain, myName, myPassword);
// the ntStatus is Success
Shares = smb.ListShares(out ntStatus);
// the Shares is null and if i debug the response header i see header.Status of 3221225661.
Whats wrong with my code and what is 3221225661??
Hi,
I really appreciate the work you have done and this nuget package has been very helpful. Thanks for that
I was wondering if this package will be upgraded to support SMBv3 as well. We are seeing a situation where we will need to access shared folders that only allow SMBv3.
Or are you aware of any other alternative library that we can try for SMBv3 for now.
Thanks,
Dipen
Windows 7 "Map Network Drive" fails with the "Invalid Parameter" popup.
The error happens as soon as SMBLibrary.Server.SMB2.SessionSetupHelper sends the STATUS_MORE_PROCESSING_REQUIRED response and socket connection is immediately closed by client.
I would like to use this in an x64 environment. It looks like the roadblock would be the Win32 project. Do you have any plans to port the IntegratedNTLMAuthenticationProvider to Managed code or port the Win32 to use x64 dll's ?
I cannot get the example to run, continually getting "An attempt was made to access a socket in a way forbidden by it's access permissions".
I've installed a Microsoft Loopback Adapter, given it the IP details of 192.168.50.1/24
I'm looking to run the program on port 445 so Windows Explorer will pick up on the share.
I've found and opened the Settings.xml document. It references a "C:\Shared" folder by default. I've made that folder and put a test text document in it. However I'm still not able to open "\192.168.50.1" in explorer.
When I run the SMB server, I'd like to not block the existing access for the local machine to run their own SMB shares through Windows. Is that possible?
Also, were there any working examples implementing an IFileSystem?
My apologies if I haven't been clear, feel free to ask me to clarify anything. Thank you.
Hello,
it seems that the library does not take in account CreateMessage with Response status STATUS_PENDING. It should be wait for the next answer with same PID. I have this status for large file requests.
From https://wiki.wireshark.org/SMB2
If a SMB2 command can not be completed immediately the server will respond immediately with STATUS_PENDING and specify a value for the PID that the client can use later to Cancel the request. This STATUS_PENDING reply has the P bit set to 1 to indicate that the PID is valid. Once the command completes later the server will send a second reply to the command, this time still keeping the P bit set to 1 and repeating the same PID as in the initial STATUS_PENDING reply.
I'm running on an app that uses the SMBLibrary with Mono framework on Mac OSX.
Mono JIT compiler version 5.10.1.47 (2017-12/8eb8f7d5e74 Fri Apr 13 20:18:12 EDT 2018)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
TLS: normal
SIGSEGV: altstack
Notification: kqueue
Architecture: amd64
Disabled: none
Misc: softdebug
Interpreter: yes
LLVM: yes(3.6.0svn-mono-master/8b1520c8aae)
GC: sgen (concurrent by default)
Hi. I have a little SambaFetcher web app that I use to connect to smb when the smbj
library doesn't work.
I have noticed that on a Windows Server 2016 default windows share, I cannot connect to the SmbTree.
Here is my code https://github.com/nddipiazza/SambaFetcher/tree/SambaLibrary
When I connect I get a blocking error:
STATUS_ACCESS_DENIED
I'm definitely entering the right credentials.
Is this because of Smb3? Is there some limitation that I'm hitting here with the SambaLibrary?
Here is a wireshark packet capture: https://drive.google.com/open?id=1fSxzgIjZX9mu77t4CbR1zHOEt7XnGNCw
First a response comes back:
NT Status: STATUS_MORE_PROCESSING_REQUIRED (0xc0000016)
then comes back a
NT Status: STATUS_ACCESS_DENIED (0xc0000022)
Trying to figure out what i need to do to get this to authenticate. When I was working on this a few months ago it was working fine, no sure what i'm messing up.
By only supporting a read to byte[], causes out of memory situations on large files.
we should provide a ReadStream
method that provides the stream to the data, or a WriteToOutputStream
method.
Hello @TalAloni,
I'm trying to get the list of shared folders from SMB server using following code but I'm getting "STATUS_USER_SESSION_DELETED" from ListShares() :
var client = new SMB2Client();
var success = client.Connect(System.Net.IPAddress.Parse("172.21.1.41"),
SMBTransportType.DirectTCPTransport);
// Success
if (success)
{
var status = client.Login(String.Empty, "CE", "pass");
// Success
if (status == NTStatus.STATUS_SUCCESS)
{
var shares = client.ListShares(out var actionStatus);
// **FAILURE : SMBLibrary.NTStatus.STATUS_USER_SESSION_DELETED**
foreach (var item in shares)
{
Console.WriteLine(item);
}
}
}
What I'm trying to do here is get the list of folders from SMB and if folder does not exists then create a new one.
Actual path from where I'm want to get directory list is as following :
\172.21.1.41\Data_test$\
I've also attached the WireShark packet capture and I'm using the latest version(1.3.7.0).
smbSpecific_20190419_1.zip
Please confirm.
Any help would be much appreciated.
Regards.
Hello,
I'm trying to build a client application that could perform CRUD operation on files stored on SMB server using .NET Core. I tried code from following issue but always fails, connection is successful though :
#9
Status Code returned is : SEC_E_INVALID_TOKEN
I'm trying to use SMB2Client from following fork :
https://github.com/j4m3z0r/SmbLibraryStd
Could you please share any sample code that I could use to fix this problem??
Hello, it would be good to have dotnet core support.
sadly that means that at least the libraries need to be "modern" projects and at least compile to .net standard.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.