AgNO3 / jcifs-ng

A cleaned-up and improved version of the jCIFS library
GNU Lesser General Public License v2.1
313 stars 104 forks source link

SmbTransportPool#logon() does not throw exception on bad credentials #68

Closed mdzhigarov closed 6 years ago

mdzhigarov commented 6 years ago
NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator("sub","test", "wrong-password");
CIFSContext context = SingletonContext.getInstance().withCredentials(auth);

Address address = null;
try
{
    address = context.getNameServiceClient().getByName("dzhigarovm03");
}
catch (UnknownHostException e)
{
    ...
}

try
{
    context.getTransportPool().logon(context, address);
}  
catch (SmbAuthException e) 
{ 
    ...
}
...

I expect to see SmbAuthException here... Instead, the logon method returns without throwing anything even though the credentials are wrong. Later, when I try to access a file on the FS I get SmbAuthException, i.e:

Caused by: jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password. at jcifs.smb.SmbTransportImpl.checkStatus2(SmbTransportImpl.java:1400) at jcifs.smb.SmbTransportImpl.checkStatus(SmbTransportImpl.java:1529) at jcifs.smb.SmbTransportImpl.sendrecv(SmbTransportImpl.java:1001) at jcifs.smb.SmbTransportImpl.send(SmbTransportImpl.java:1500) at jcifs.smb.SmbSessionImpl.sessionSetupSMB2(SmbSessionImpl.java:544) at jcifs.smb.SmbSessionImpl.sessionSetup(SmbSessionImpl.java:478) at jcifs.smb.SmbSessionImpl.send(SmbSessionImpl.java:364) at jcifs.smb.SmbSessionImpl.send(SmbSessionImpl.java:342) at jcifs.smb.SmbTreeImpl.treeConnect(SmbTreeImpl.java:607) at jcifs.smb.SmbTreeConnection.connectTree(SmbTreeConnection.java:609) at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:563) at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:484) at jcifs.smb.SmbTreeConnection.connect(SmbTreeConnection.java:460) at jcifs.smb.SmbTreeConnection.connectWrapException(SmbTreeConnection.java:421) at jcifs.smb.SmbFile.ensureTreeConnected(SmbFile.java:550) at jcifs.smb.SmbFile.exists(SmbFile.java:826) ...

mbechler commented 6 years ago

Uh, that is indeed a nasty one. By accident swallowed all exceptions there. Let's hope nobody was using that without further testing.

NarayanaKodavati commented 4 years ago

context.getTransportPool().logon(context, address); usign thsi method for LDAP authentication, i am unable to find this method with credetial login or not.please help me we are migrating jcifs to jcifs-ng for supporting SMB2 protocol

mbechler commented 4 years ago

What are you trying to achieve? I generally would not advise to use this as a generic authentication mechanism (lot's of overhead).

NarayanaKodavati commented 4 years ago

we have eSignature logon for authentication with LDAP, we are using SMBSession.logon method to authenticate through JCIFS lib, now my LDAP servers are upgrading to latest Windows servers and LDAP, now its not supporting SMB1 protocol, its due to Microsoft deprecated SMB1 protocol in windows 10 systems. need to migrate to SMB2 protocl for authentication. please help me to achieve with JCIFS-NG.

NarayanaKodavati commented 4 years ago

My code: simple JAVA class:

package com.jnj;

import java.net.UnknownHostException; import java.util.Properties;

import org.apache.log4j.PropertyConfigurator; import org.mockito.internal.stubbing.answers.ThrowsException; import org.slf4j.Logger; import org.slf4j.LoggerFactory;

import jcifs.Address; import jcifs.CIFSContext; import jcifs.CIFSException; import jcifs.SmbSession; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.context.SingletonContext; import jcifs.internal.smb2.nego.Smb2NegotiateRequest; import jcifs.internal.smb2.session.Smb2SessionSetupRequest; import jcifs.internal.smb2.session.Smb2SessionSetupResponse; import jcifs.netbios.UniAddress; import jcifs.smb.NtlmPasswordAuthentication; import jcifs.smb.SmbAuthException; import jcifs.smb.SmbSessionInternal;

public class logonng {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //System properties
    CIFSContext context =null;
    Address address = null;
    UniAddress dc = null;
    Logger logg = LoggerFactory.getLogger(logonng.class);
     Properties prop = new Properties(); 
     prop.setProperty("log4j.rootLogger", "WARN");
     PropertyConfigurator.configure(prop);
    logg.info("Log message");
    try
    {
        System.setProperty("jcifs.resolveOrder", "LMHOSTS,WINS,DNS");
        System.setProperty("jcifs.lmCompatibility", "0");
        System.setProperty("jcifs.smb.client.useExtendedSecurity", "false");
        System.setProperty("jcifs.netbios.hostname", "HOST_NAME");
        System.setProperty("log4j.rootLogger", "WARN");
        BaseContext bsContext = new BaseContext(new PropertyConfiguration(System.getProperties()));

// dc = getTransportContext().getNameServiceClient().getByName(, true);

        NtlmPasswordAuthentication credentials = new NtlmPasswordAuthentication(bsContext, "DOMAIN", "USERID", "PASSWORD");
        context = SingletonContext.getInstance().withCredentials(credentials);
        //na.jnj.com or itsusranadc02.na.jnj.com

        try{
             address = context.getNameServiceClient().getByName("HOST_NAME");
             context.getTransportPool().logon(context, address, 389); 

        }catch (CIFSException  e) {
            // TODO: handle exception

            System.out.println(e.getMessage());
            System.out.println("print stack trace  :");
            e.printStackTrace();
        }

        System.out.println("address :"+address);

        System.out.println("credentials :"+credentials);
        System.out.println("context :"+context.getNameServiceClient().getLocalName());
    }catch (UnknownHostException e)
    {
      System.out.println(e.getMessage());
    }

// try // { //
// }catch (SmbAuthException smbex){ // System.out.println(smbex.getMessage()); // } catch (CIFSException ce) { // TODO: handle exception System.out.println(ce.getMessage()); }

}

} unable to get the exception for wrong credentials.

mbechler commented 4 years ago

Are you trying to establish a SMB connection with a LDAP server (port 389)? This definitely should cause some kind of error.