Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SMB Shared Files Name and Permissions In Java

Tags:

java

python

smb

I want to connect to an SMB server and browse through its files, and for a given path, to be able to retrieve a list of files and folders, with the names and permissions.

I need to support all SMB dialects, and to be able to do it from my code.

The code would like roughly as follows:

smbClient.connect(serverInfo);
info = smbClient.getShare(shareName);
for(File file : info.getFiles) {
    List<permission> permissions = file.getPermissions();
    //do something
}

I've tried a few options such as smbj, impacket, nmap, samba but none of them seem to fill my requirements above.

Is there any way to achieve the above, using Java, Python, or any linux CLI which i can call from my Java code?

like image 710
zuckermanori Avatar asked Oct 15 '25 12:10

zuckermanori


2 Answers

I guess it can help you to improve in jcifs-ng.

**// Option 1 - SMB2 and SMB3:**
Properties prop = new Properties();
prop.put( "jcifs.smb.client.enableSMB2", "true");
prop.put( "jcifs.smb.client.disableSMB1", "false");
prop.put( "jcifs.traceResources", "true" );
Configuration config = new PropertyConfiguration(prop);
CIFSContext baseContext = new BaseContext(config);
CIFSContext contextWithCred = baseContext.withCredentials(new NtlmPasswordAuthentication(baseContext, domain, fileSystemInfo.getUsername(), fileSystemInfo.getPassword()));
SmbFile share = new SmbFile(fullPath.replace('\', '/'), contextWithCred);
if (!share.exists())
{
    share.mkdirs();
}
share.close();

// Option 2 - SMB1 and CIFS:

SingletonContext context = SingletonContext.getInstance();
CIFSContext testCtx = context.withCredentials(
    new NtlmPasswordAuthentication(
        context, domain, fileSystemInfo.getUsername(), fileSystemInfo.getPassword()
    )
);
SmbFile smbFile = new SmbFile(fullPath.replace('\', '/'), testCtx);
if (!smbFile.exists())
{
    smbFile.mkdirs();
}
smbFile.close();
like image 199
RonyTorrico Avatar answered Oct 18 '25 02:10

RonyTorrico


I don't think that there is any open source Java library that support all you need.

There is a non open source library called jNQ by "Visuality Systems"

This library support all the SMB dialects (SMB1 to SMB3.1.1)

In the link there is a code example for browsing (and you can get the security descriptor for each file in the list):

PasswordCredentials cr = new PasswordCredentials("userName", "password", "domain");
Mount mt = new Mount("IpAddress","ShareName", cr);
Directory dir = new Directory(mt, "dir1");
Directory.Entry entry;
System.out.println(DIR + " scan:");
do {
    entry = dir.next();
    if (null != entry)
        System.out.println(entry.name + " : size = " + entry.info.eof);
} while (entry != null);
like image 31
Eliad Cohen Avatar answered Oct 18 '25 01:10

Eliad Cohen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!