jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password [Samba]
Asked Answered
W

1

0

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:

  1. 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
  1. Go to container terminal and execute command:

     samba-tool user setpassword Administrator --newpassword=myPassword
    
  2. Add dependency

    org.codelibs jcifs 2.1.35
  3. Run 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.

Wurster answered 30/6, 2023 at 20:9 Comment(0)
P
0

this is how i did it, maybe it will help you:

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("192.168.10.10", "DOMAIN\\USER", "PASSWORT"));
            SmbFile smbRemoteFile = new SmbFile("smb://192.168.10/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 use this Library jcifs-ng-2.1.8.jar

and i import:

import jcifs.CIFSContext;
import jcifs.Configuration;
import jcifs.config.PropertyConfiguration;
import jcifs.context.BaseContext;
import jcifs.smb.NtlmPasswordAuthenticator;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

if you have a windows Domain you must Sahre the Folder and give the user Domain/User Permissions to read/write to the Share!

Provencal answered 30/6, 2023 at 21:8 Comment(4)
What dependency have you used? Have you configured anything on Samba side ?Wurster
What is DOMAIN\\USER ?Wurster
I have edit my Answer. for SMB 2.0 or SMB 3.0 you need the library jcifs-ng-2.1.8.jar. If you dont have a Windows Domain you can try only with local Username of the User which have the permission to write on the Sahre. so your outputdir folder must be a shareProvencal
could you please take a look my topic update. I've described result after attempt to apply your adviceWurster

© 2022 - 2024 — McMap. All rights reserved.