javasmbsmbj

Getting com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022) exception while reading files from shared drive


I am trying to access files from a shared drive using SMB Java Library.

I am trying it in two ways

a) SMB JCIFS b) hierynomus

but both the ways I am getting exception, below is the code and exception I am getting

JCIFS

try {
            String path = "smb://" + remote_machine_name + "/" + sharedFolder+"/";
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", remote_user_id, remote_user_password);
            SmbFile smbFile = new SmbFile(path, auth);
            System.out.println("Connected");
            SmbFile[] smbFileList = smbFile.listFiles();
            for (SmbFile file : smbFileList) {
                System.out.println(file.getName());
            }
            System.out.println("Done");
        } catch (SmbException se) {
            se.printStackTrace();
        }

Exception:

jcifs.smb1.smb1.SmbException: Failed to connect: <hostname>/<ip address>
jcifs.smb1.util.transport.TransportException
java.net.SocketException: Connection reset

hierynomus smbj

SmbConfig cfg = SmbConfig.builder().withSecurityProvider(new BCSecurityProvider()).build();
        SMBClient client = new SMBClient(cfg);
        Connection connection;
        try {
            connection = client.connect(remote_machine_name);
            Session session = connection
                    .authenticate(new AuthenticationContext(remote_user_id, remote_user_password.toCharArray(), null));
            DiskShare share = (DiskShare) session.connectShare(sharedFolder);
            Set<FileAttributes> fileAttributes = new HashSet<>();
            fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_DIRECTORY);
            Set<SMB2ShareAccess> shareAccesses = new HashSet<SMB2ShareAccess>();
            shareAccesses.add(SMB2ShareAccess.FILE_SHARE_READ);
            for (FileIdBothDirectoryInformation f : share.openDirectory(
                    "folder1/folder11/folder111",
                    EnumSet.of(AccessMask.FILE_READ_DATA), fileAttributes, shareAccesses,
                    SMB2CreateDisposition.FILE_OPEN, null)) {
                System.out.println(f.getFileName());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

Exception(complete stack trace):

[main] INFO com.hierynomus.smbj.connection.Connection - Successfully connected to: **<hostname>**
[main] INFO com.hierynomus.smbj.connection.SMBSessionBuilder - Successfully authenticated karthik on **<hostname>**, session is -12346324324
[main] INFO com.hierynomus.smbj.session.Session - Connecting to **\\hostname\proj** on session -12346324324
Exception in thread "main" com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022): Create failed for \\hostname\proj\Folder1\Folder11\Folder111
    at com.hierynomus.smbj.share.Share.receive(Share.java:397)
    at com.hierynomus.smbj.share.Share.sendReceive(Share.java:377)
    at com.hierynomus.smbj.share.Share.createFile(Share.java:159)
    at com.hierynomus.smbj.share.DiskShare.createFileAndResolve(DiskShare.java:75)
    at com.hierynomus.smbj.share.DiskShare.access$100(DiskShare.java:55)
    at com.hierynomus.smbj.share.DiskShare$2.apply(DiskShare.java:109)

I manually mapped the network drive to my system and verified that I have admin rights, but for some reasons I am getting the above error.

Can someone please help me to resolve it


Solution

  • I have a possible solution for the SMB JCIFS way from my another answer!

    SmbException failed to connect hostname/IP_address throwing with proper credentials in Java

    I think the problem is you don't have SMB2 enabled in your system (thats necessary for windows 10) and you need to create a CIFSContext object to build the proper connection between your local pc and the shared folder.