Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.

Tags:

java

jcifs

planning to read a file over a Windows from Ubuntu in Java using jcifs.Tried a simple approach using:

String user = "mydomain;myuser:mypassword";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
SmbFile remotefile = new SmbFile("smb://myserver/myfolder/myfile.jar",auth);

Knowing that the server works and the login values are correct,all i get is a logon failure,what could be the problem here?

like image 626
Sin5k4 Avatar asked Jul 09 '12 06:07

Sin5k4


2 Answers

Not sure if you got this to work. But after much pain and anguish, I figured the NtlmPasswordAuthentication call must include the domain. So if you're using the code @user717630 posted, you'll just have to change the NtlmPasswordAuthentication call to: NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("mydomain",user, pass);

like image 125
peterb Avatar answered Oct 06 '22 23:10

peterb


The following program authenticates and writes a file on the protected share folder:

import java.util.Properties;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;


public class ProtectFolderTest {
private String USER_NAME = null;
private String PASSWORD = null;
private String DOMAIN = null;
private String NETWORK_FOLDER = null;

public static void main(String args[]) {
    try {
        String fileContent = "Hi, This is the SmbFile.";
        new ProtectFolderTest().copyFiles(fileContent, "SmbFile1.text");
    } catch (Exception e) {
        System.err.println("Exception caught. Cause: " + e.getMessage());
    }
}

public boolean copyFiles(String fileContent, String fileName) {
    boolean successful = false;
    String path = null;
    NtlmPasswordAuthentication auth = null;
    SmbFile sFile = null;
    SmbFileOutputStream sfos = null;
    try {
        USER_NAME = "username";
        PASSWORD = "password";
        DOMAIN = "domain";
        NETWORK_FOLDER = "smb://machineName/network_folder/";
        auth = new NtlmPasswordAuthentication(
                DOMAIN, USER_NAME, PASSWORD);
        path = NETWORK_FOLDER + fileName;
        sFile = new SmbFile(path, auth);
        sfos = new SmbFileOutputStream(sFile);
        sfos.write(fileContent.getBytes());
        successful = true;
        System.out.println("File successfully created.");
    } catch (Exception e) {
        successful = false;
        System.err.println("Unable to create file. Cause: "
                + e.getMessage());
    }
    return successful;
}
}

Hope this is useful. Expecting feedback on this.

Thanks,

Marshal.

like image 21
IMJS Avatar answered Oct 07 '22 00:10

IMJS