8

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?

Sin5k4
  • 1,556
  • 7
  • 33
  • 57

6 Answers6

9

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);

Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37
peterb
  • 91
  • 1
  • 2
3

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.

IMJS
  • 863
  • 2
  • 13
  • 25
  • Hey, I tried the same but I get an error saying invalid user name or bad password. I have checked my user name, password and domain are correct. – Varun Mar 19 '20 at 12:17
2

Here is the solution for you I changed the code a little to make it more readable. Create a shared folder and put the shared folder name in the below variable(sharedFolder) if you don't know how to create shared folder on windows ...use google as always. Also, make sure this user that you are using has at least read access to that folder.

    String user = "your_user_name";
    String pass ="your_pass_word";

    String sharedFolder="shared";
    String path="smb://ip_address/"+sharedFolder+"/myfile.jar";
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
    SmbFile smbFile = new SmbFile(path,auth);
    SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
grepit
  • 21,260
  • 6
  • 105
  • 81
1

Try using IP address instead of server name and see if it connects or not. It probably cannot resolve the server name.

mr_tawan
  • 666
  • 1
  • 5
  • 8
1

Comment to "peterb"s answer: "...call must include the domain..."

I figured out that in my case the NtlmPasswordAuthentication( "domain", "username", "password" ) needs its inputs like this: domain is the long domain with path to the share:\xxxx.domain.xxxx.com\path. username is the username with domain: domain\username. password = password.

I hope this will help some one.

BEM

BEM
  • 11
  • 2
0

There are several constructors in jcifs-ng-2.1.6.jar.
My solution for that problem was using a default constructor for guest (anonymous) user
auth = baseCxt.withCredentials(new NtlmPasswordAuthenticator());

d0wn
  • 121
  • 4