Reputation: 111
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
Upvotes: 0
Views: 6334
Reputation: 21
Setting the .withEncryptData(true) code worked for me. Just check whether your client supports the protocols , whatever the SMB server is asking. And then enforce your client to support the same. You can list the dialects and security modes, your SMB server supports and your client supports. Try to match your client config as much as possible. You can get those details by printing ConnectionContext.
System.out.println(serverConnection.getConnectionContext());
Our SMB server was not understanding what my client was sending (because I did not set .withEncryptData(true) in the smbconfig), hence it was sending STATUS_ACCESS_DENIED response (which is very generic and weird. It should atleast respond with a different response that is easy to troubleshoot).
In my case, our SMB server supported only 3.1.1 . Also ,it was allowing only encrypted data. Just try adding those configs and see which works for you.
SmbConfig smbConfig = SmbConfig.builder().withDialects(SMB2Dialect.SMB_3_1_1).withEncryptData(true).build();
SMBClient client = new SMBClient(smbConfig);
Connection serverConnection = client.connect(server);
System.out.println(serverConnection.getConnectionContext());// provides client and server supported protocols and other information
Session session = serverConnection.authenticate(new AuthenticationContext(username, password.toCharArray(), domain));
DiskShare share = (DiskShare) session.connectShare("my_shared_folder");
Upvotes: 2
Reputation: 118
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.
Upvotes: 1