Skip to content Skip to sidebar Skip to footer

Get Smb Shared Files Name And Permissions In Java

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

Solution 1:

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();

Solution 2:

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):

PasswordCredentialscr=newPasswordCredentials("userName", "password", "domain");
Mountmt=newMount("IpAddress","ShareName", cr);
Directorydir=newDirectory(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);

Solution 3:

We had the same problem, and found jcifs-ng to best answer our requirements using java (We tested it only on v1 and v2 though, but the newest version has some support for v3 as well). Your code would look like:

Configurationconfig=newPropertyConfiguration(newProperties());
CIFSContextcontext=newBaseContext(config);
context = context.withCredentials(newNtlmPasswordAuthentication(null, domain, userName, password));
Stringshare="smb://HOSTNAME/SHARENAME/";
try (SmbFileshare=newSmbFile(url, context)) {
    for (SmbFile file : share.listFiles()) {
        ACE[] groups = file.getSecurity();
        // Do something..
    }
}

Where ACE is an Access Control Entry, which is an element that controls or monitors an access to an object (is short, a permission).

Notice that the newest version might not be on Maven or Gradle yet, so you'll have to clone the repo and build it by yourself.

Solution 4:

If you are running Windows and the user running the program has access to the share, you could do it just by using java.nio. Java.nio allows you to access SMB shares.

Path path = Paths.get(<SharedFile>);
AclFileAttributeView aclAttribute = Files.getFileAttributeView(path, AclFileAttributeView.class);

Then you can use aclAttribute.getAcls(); to get the users and their permissions.

Post a Comment for "Get Smb Shared Files Name And Permissions In Java"