hierynomus / smbj

Server Message Block (SMB2, SMB3) implementation in Java
Other
707 stars 180 forks source link

cannot connect share folder(Authentication failed) #454

Closed edwin20022 closed 5 years ago

edwin20022 commented 5 years ago

anybody can help? use smbj connect to share folder throw : Authentication failed i can make sure the "doman", "username", "password" correct. smjb version : 0.7.0 My code as below:

SmbConfig config = SmbConfig.builder().withTimeout(timeOut, TimeUnit.SECONDS) .withSoTimeout(socketTimeOut, TimeUnit.SECONDS).build();

  SMBClient client = new SMBClient(config);

   Connection connection = client.connect(smbHost);
  AuthenticationContext ac = new AuthenticationContext(smbuser, password.toCharArray(),
   domain);
  Session session = connection.authenticate(ac);

  DiskShare share = (DiskShare) session.connectShare(shareName);

  return share.openFile(subFolders + "/" + fileNameOnly, EnumSet.of(AccessMask.GENERIC_READ),
      EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL), SMB2ShareAccess.ALL,
      SMB2CreateDisposition.FILE_OPEN, EnumSet.noneOf(SMB2CreateOptions.class));

when run at Session session = connection.authenticate(ac);

my service throw : java.lang.RuntimeException: com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'user' using com.hierynomus.smbj.auth.NtlmAuthenticator@59773866 at hkjc.cap.ptn.config.FileStoreHandlerConfig.buildFileStoreReadAllClient(FileStoreHandlerConfig.java:226) at hkjc.cap.ptn.services.OpReadAllProcessor.readAll(OpReadAllProcessor.java:109) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:481) at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:300) at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:273) at org.apache.camel.component.bean.AbstractBeanProcessor.process(AbstractBeanProcessor.java:198) at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:53) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:117) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:117) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:113) at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:736) at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:696) at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674) at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:318) at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:257) at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1189) at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1179) at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1076) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Suppressed: java.lang.RuntimeException: com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'user' using com.hierynomus.smbj.auth.NtlmAuthenticator@7b2d6062 ... 35 more Suppressed: java.lang.RuntimeException: com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'user' using com.hierynomus.smbj.auth.NtlmAuthenticator@35344f57 ... 35 more Caused by: com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'user' using com.hierynomus.smbj.auth.NtlmAuthenticator@35344f57 at com.hierynomus.smbj.connection.Connection.authenticate(Connection.java:176) at hkjc.cap.ptn.config.FileStoreHandlerConfig.buildFileStoreReadAllClient(FileStoreHandlerConfig.java:217) ... 34 more Caused by: com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'user' using com.hierynomus.smbj.auth.NtlmAuthenticator@7b2d6062 at com.hierynomus.smbj.connection.Connection.authenticate(Connection.java:176) at hkjc.cap.ptn.config.FileStoreHandlerConfig.buildFileStoreReadAllClient(FileStoreHandlerConfig.java:217) ... 34 more Caused by: com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'user' using com.hierynomus.smbj.auth.NtlmAuthenticator@59773866 at com.hierynomus.smbj.connection.Connection.authenticate(Connection.java:176) at hkjc.cap.ptn.config.FileStoreHandlerConfig.buildFileStoreReadAllClient(FileStoreHandlerConfig.java:217) ... 34 more

edwin20022 commented 5 years ago

update: use smbclient command successfully connect smb share folder, but smbj still fail.

ecki commented 5 years ago

What parameters did you specify for smbclient, what is the server software/version providing this share. Is DFS, Kerberos, Signing or AD membership involved? Any GPO settings to restrict protocols? What’s logged on the server? What type of user is this (local, Domain, trusted domain, Service, ....), does the password have any strange characters, what’s the value of the domain variable in your sample code?

hierynomus commented 5 years ago

Thanks for asking the questions @ecki! The status you're getting is sent by the server amd indicates that one of the authentication parameters is wrong. Have you tried uppercasing the domain?

edwin20022 commented 5 years ago

@ecki Hello, i found that smbj handle special symbol faild, how can i handle this password: "2345432!" ?

ecki commented 5 years ago

Are you saying it works with a different password but fails with this special one? Hm, looks ASCII only, so no idea where the trouble is.

Just to Ben sure: Does the password include or exclude the quotes?

Did you try to set a different password (one with a leading letter and without !). If you are sure what the exact is (I.e. the !? Let us know here). And also please make sure the Java variable for password actually contains the characters, it could well be that the source of it (like command line parsing) already fails for !

edwin20022 commented 5 years ago

Are you saying it works with a different password but fails with this special one? Hm, looks ASCII only, so no idea where the trouble is.

Just to Ben sure: Does the password include or exclude the quotes?

Did you try to set a different password (one with a leading letter and without !). If you are sure what the exact is (I.e. the !? Let us know here). And also please make sure the Java variable for password actually contains the characters, it could well be that the source of it (like command line parsing) already fails for !

yes, for different password, i try "00000000" be successfully, change to "2345432!" will throw the STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'user' using com.hierynomus.smbj.auth.NtlmAuthenticator@361d3361

ecki commented 5 years ago

Ok, make sure the password in the Java variable is correct and that you don’t see a shell quoting problem from command arguments instead.

AbhayGpta commented 4 years ago

Why it is showing com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Even i am giving right domian / password / username I have tried it with both UPPERCASE and lowercase

Does SMBJ can be tested on my own laptop by making folder sharable

Please help urgent required

public class smbjcls {

    private static final String SHARE_DOMAIN = " anchor.abcdef.com";
    private static final String SHARE_USER = "pqr.xyz";
    private static final String SHARE_PASSWORD = "SmsStadium302017";
    private static final String SHARE_SRC_DIR = "E:\\share\\source";
    private static final String SHARE_DST_DIR = "E:\\reciever\\destination";

    public static void main(String[] args) {

        SmbConfig config = SmbConfig.builder().withTimeout(1200, TimeUnit.SECONDS)
                 .withTimeout(1200, TimeUnit.SECONDS) 
                        .withSoTimeout(1800, TimeUnit.SECONDS) 
                        .build();

        SMBClient client = new SMBClient(config);

        try {
                Connection connection = client.connect("192.***.*.**"); // For example: 123.123.123.123
                System.out.println("Connection is created1");
                AuthenticationContext ac = new AuthenticationContext(SHARE_USER, SHARE_PASSWORD.toCharArray(), SHARE_DOMAIN);
                System.out.println("AuthenticationContext is created2");
                Session session = connection.authenticate(ac);              //The java string toCharArray() method converts the given string into a sequence of characters.
                System.out.println("Session is created3");
                // Connect to a shared folder
                DiskShare share = (DiskShare) session.connectShare(SHARE_SRC_DIR);
                        //Connect to a share on the remote machine over the authenticated session.
                System.out.println("Session is created4");
                String folder = SHARE_SRC_DIR + SHARE_DST_DIR;
                String dstRoot = "The local folder path to save"; // For example: D:/smd2/

            for (FileIdBothDirectoryInformation f : share.list(SHARE_DST_DIR, "*.mp4")) {
                String filePath = folder + f.getFileName();
                String dstPath = dstRoot + f.getFileName();

                FileOutputStream fos = new FileOutputStream(dstPath);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                if (share.fileExists(filePath)) {
                     System.out.println("Downloading file:" + f.getFileName());

                    File smbFileRead = share.openFile(filePath, EnumSet.of(AccessMask.GENERIC_READ), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null);
                    InputStream in = smbFileRead.getInputStream();
                    byte[] buffer = new byte[4096];
                    int len = 0;
                    while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                        bos.write(buffer, 0, len);
                    }

                    bos.flush();
                    bos.close();

                     System.out.println("File Download Successful");
                    System.out.println("==========================");
                } else {
                     System.out.println("File does not exist");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                client.close();
            }
        }
    } 
}

Output===> 1948 [main] INFO com.hierynomus.smbj.connection.Connection - Successfully connected Connection is created1 AuthenticationContext is created2 com.hierynomus.mssmb2.SMBApiException: STATUS_LOGON_FAILURE (0xc000006d): Authentication failed for 'ABHAY.GUPTA' using com.hierynomus.smbj.auth.NtlmAuthenticator@2833cc44 at com.hierynomus.smbj.connection.Connection.authenticate(Connection.java:194) at test.smbjcls.main(smbjcls.java:106) 7303 [main] INFO com.hierynomus.smbj.SMBClient - Going to close all remaining connections 7315 [main] INFO com.hierynomus.smbj.connection.Connection - Closed connection to 192.168.1.18 7331 [Packet Reader for 192...] INFO com.hierynomus.smbj.transport.PacketReader - Thread[Packet Reader for 192.168.1.18,5,main] stopped.

AbhayGpta commented 4 years ago

@hierynomus help to resolve my problem

zubcevic commented 4 years ago

Hi there, I encountered a same situation with the message STATUS_LOGON_FAILURE (0xc000006d)

After some investigation I found out that the username was too long.

The username that was provided was 22 characters and apparently Windows uses only the first 20 of that in the SMB connection. I saw this by investigating the details of the share. So when I used the adjusted username and the same password, the connection was established.

I do not know if it applies to all windows versions but at least it was the answer to my issue.

I checked and saw the cut-off usernames using the following winrs command: winrs -r:fullyqualifiedservername -u:username@domain -p:password icacls d:\myshare

I am not sure if you can or should do anything about this in the smbj code. You could consider adding a warning when something fails and the username is longer than 20.

Hopes this helps others when dealing with this issue, which can take some time to discover.

diana-pure commented 3 years ago

I had STATUS_LOGON_FAILURE (0xc000006d) running app on macos(big sur)

In my case it was necessary to to go to system preferences go to shared access select shared files select ,y shared folder I created before choose parameters turn on give access via SMB and select the user.

Sorry, settings might have other english locale names than mine, tried translate it, hope that it's clear enough.