Get SMB Shared Files Name and Permissions In Java

不打扰是莪最后的温柔 提交于 2020-01-05 05:37:10

问题


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?


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



回答2:


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.




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/48277886/get-smb-shared-files-name-and-permissions-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!