I try to save file on a Samba server but get the error:
jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.
at jcifs.smb.SmbTransportImpl.checkStatus2(SmbTransportImpl.java:1464)
at jcifs.smb.SmbTransportImpl.checkStatus(SmbTransportImpl.java:1607)
at jcifs.smb.SmbTransportImpl.sendrecv(SmbTransportImpl.java:1014)
at jcifs.smb.SmbTransportImpl.send(SmbTransportImpl.java:1578)
at jcifs.smb.SmbSessionImpl.sessionSetupSMB2(SmbSessionImpl.java:557)
at jcifs.smb.SmbSessionImpl.sessionSetup(SmbSessionImpl.java:491)
at jcifs.smb.SmbSessionImpl.send(SmbSessionImpl.java:369)
at jcifs.smb.SmbSessionImpl.send(SmbSessionImpl.java:347)
at jcifs.smb.SmbTreeImpl.treeConnect(SmbTreeImpl.java:611)
at jcifs.smb.SmbTreeConnection.connectTree(SmbTreeConnection.java:614)
at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:568)
at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:489)
at jcifs.smb.SmbTreeConnection.connect(SmbTreeConnection.java:465)
at jcifs.smb.SmbTreeConnection.connectWrapException(SmbTreeConnection.java:426)
at jcifs.smb.SmbFile.ensureTreeConnected(SmbFile.java:573)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:112)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:95)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:76)
at com.example.test_jcif.TestJcifApplication.test(TestJcifApplication.java:88)
at com.example.test_jcif.TestJcifApplication.main(TestJcifApplication.java:26)
Process finished with exit code 0
Steps to reproduce:
- run docker-compose file:
services:
samba:
image: instantlinux/samba-dc:latest
container_name: samba-dc
cap_add:
- CAP_SYS_ADMIN
hostname: my.company
environment:
DOMAIN_ACTION: provision
REALM: my.company
volumes:
- etc:/etc/samba
- lib:/var/lib/samba
ports:
- "53:53"
- "53:53/udp"
- "88:88"
- "88:88/udp"
- "389:389"
- "139:139"
- "446:445"
secrets:
- samba-admin-password
Go to container terminal and execute command:
samba-tool user setpassword Administrator --newpassword=myPassword
Add dependency
org.codelibs jcifs 2.1.35Run code:
String user="Administrator"; String pass="myPassword"; String path="smb://localhost/outputdir/outputfile.txt"; InputStream winfis = new ByteArrayInputStream("qwerty".getBytes()); try { SingletonContext baseContext = SingletonContext.getInstance(); Credentials credentials=new NtlmPasswordAuthenticator(null, user, pass); CIFSContext testCtx = baseContext.withCredentials(credentials); try { SmbFile smbFile = new SmbFile(path, testCtx); SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile); try { IOUtils.copy(winfis, smbfos); } catch (IOException ex) { ex.printStackTrace(); } finally { try { smbfos.close(); } catch (IOException ex) { ex.printStackTrace(); } } } catch (MalformedURLException ex) { ex.printStackTrace(); } } catch (CIFSException ex) { ex.printStackTrace(); } finally { try { winfis.close(); } catch (IOException ex) { ex.printStackTrace(); } }
}
As soon as I run the code you receive the error above
Update: my codebase after Sametcey advice:
public static void main(String[] args) throws IOException {
writeFiletoSAMBAShareIMPORT(new StringWriter(12), "qwerty");
}
public static String writeFiletoSAMBAShareIMPORT(StringWriter xmlString, String xmlFileName) {
try {
BaseContext baseCxt = null;
Properties jcifsProperties = new Properties();
jcifsProperties.setProperty("jcifs.smb.client.enableSMB2", "true");
jcifsProperties.setProperty("jcifs.smb.client.dfs.disabled", "true");
Configuration config = new PropertyConfiguration(jcifsProperties);
baseCxt = new BaseContext(config);
CIFSContext auth = baseCxt.withCredentials(new NtlmPasswordAuthenticator("localhost", "my.company\\Administrator", "myPassword"));
SmbFile smbRemoteFile = new SmbFile("smb://localhost/XMLImport/" + xmlFileName + "", auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(smbRemoteFile);
sfos.write(xmlString.toString().getBytes());
sfos.close();
auth.close();
} catch (Exception e) {
System.out.println(e.getMessage());
return e.getMessage();
}
return "true";
}
I get this error:
Logon failure: unknown user name or bad password.