zuckermanori
zuckermanori

Reputation: 1755

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 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?

Upvotes: 4

Views: 15073

Answers (4)

RonyTorrico
RonyTorrico

Reputation: 51

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

Upvotes: 5

Norman
Norman

Reputation: 11

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:

Configuration config = new PropertyConfiguration(new Properties());
CIFSContext context = new BaseContext(config);
context = context.withCredentials(new NtlmPasswordAuthentication(null, domain, userName, password));
String share = "smb://HOSTNAME/SHARENAME/";
try (SmbFile share = new SmbFile(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.

Upvotes: 1

Eliad Cohen
Eliad Cohen

Reputation: 368

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

Upvotes: 1

lechbrush
lechbrush

Reputation: 66

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.

Upvotes: 0

Related Questions