jcifs.smb.SmbAuthException Java Examples

The following examples show how to use jcifs.smb.SmbAuthException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CIFSContextCredentialWrapper.java    From jcifs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see jcifs.CIFSContext#renewCredentials(java.lang.String, java.lang.Throwable)
 */
@Override
public boolean renewCredentials ( String locationHint, Throwable error ) {
    Credentials cred = getCredentials();
    if ( cred instanceof SmbRenewableCredentials ) {
        SmbRenewableCredentials renewable = (SmbRenewableCredentials) cred;
        CredentialsInternal renewed = renewable.renew();
        if ( renewed != null ) {
            this.creds = renewed;
            return true;
        }
    }
    NtlmAuthenticator auth = NtlmAuthenticator.getDefault();
    if ( auth != null ) {
        NtlmPasswordAuthenticator newAuth = NtlmAuthenticator
                .requestNtlmPasswordAuthentication(auth, locationHint, ( error instanceof SmbAuthException ) ? (SmbAuthException) error : null);
        if ( newAuth != null ) {
            this.creds = newAuth;
            return true;
        }
    }
    return false;
}
 
Example #2
Source File: CIFSContextCredentialWrapper.java    From jcifs-ng with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see jcifs.CIFSContext#renewCredentials(java.lang.String, java.lang.Throwable)
 */
@Override
public boolean renewCredentials ( String locationHint, Throwable error ) {
    Credentials cred = getCredentials();
    if ( cred instanceof SmbRenewableCredentials ) {
        SmbRenewableCredentials renewable = (SmbRenewableCredentials) cred;
        CredentialsInternal renewed = renewable.renew();
        if ( renewed != null ) {
            this.creds = renewed;
            return true;
        }
    }
    NtlmAuthenticator auth = NtlmAuthenticator.getDefault();
    if ( auth != null ) {
        NtlmPasswordAuthenticator newAuth = NtlmAuthenticator
                .requestNtlmPasswordAuthentication(auth, locationHint, ( error instanceof SmbAuthException ) ? (SmbAuthException) error : null);
        if ( newAuth != null ) {
            this.creds = newAuth;
            return true;
        }
    }
    return false;
}
 
Example #3
Source File: JCifsTest.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void createDeleteNonExistingDirectory() throws IOException{
	assumeTrue(servicesAreReachable);
	URL url = new URL(prefixUrl + "test/");
	if (url.getUserInfo() == null) {
		// not authenticated
		thrown.expect(SmbAuthException.class);
	}
	URLConnection openConnection = url.openConnection();
	try (SmbFile smbFile = (SmbFile) openConnection) {
		assertFalse(smbFile.exists());
		smbFile.mkdir();
		assertTrue(smbFile.exists());
		smbFile.delete();
		assertFalse(smbFile.exists());
	}
}
 
Example #4
Source File: SessionTest.java    From jcifs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testAuthErrorNoConnLeak () throws Exception {
    CIFSContext cifsContext = getNewContext().withCredentials(new NtlmPasswordAuthenticator("wrong", "wrong", "wrong"));
    try ( SmbFile f = new SmbFile("smb://" + getTestServer() + "/IPC$/test", cifsContext); ) {
        f.connect();
        assertFalse(true);
    }
    catch ( SmbAuthException e ) {
        // ignore
    }

    assertFalse(cifsContext.close());
}
 
Example #5
Source File: SessionTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testAuthErrorNoConnLeak () throws Exception {
    CIFSContext cifsContext = getNewContext().withCredentials(new NtlmPasswordAuthenticator("wrong", "wrong", "wrong"));
    try ( SmbFile f = new SmbFile("smb://" + getTestServer() + "/IPC$/test", cifsContext); ) {
        f.connect();
        assertFalse(true);
    }
    catch ( SmbAuthException e ) {
        // ignore
    }

    assertFalse(cifsContext.close());
}
 
Example #6
Source File: VFSUtils.java    From otroslogviewer with Apache License 2.0 5 votes vote down vote up
public static boolean checkForWrongCredentials(Throwable exception) {
  if (exception.getCause() != null) {
    return checkForWrongCredentials(exception.getCause());
  } else {
    String message = exception.getMessage();
    return
      exception instanceof SmbAuthException && message.contains("The specified network password is not correct") ||
        exception instanceof JSchException && message.contains("Auth fail");
  }


}
 
Example #7
Source File: JCifsTest.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void createWriteReadDeleteNonExistingFile() throws IOException{
	assumeTrue(servicesAreReachable);
	String nowString = LocalDateTime.now().toString();
	URL url = new URL(prefixUrl + "test.txt");
	if (url.getUserInfo() == null) {
		// not authenticated
		thrown.expect(SmbAuthException.class);
	}
	URLConnection openConnection = url.openConnection();
	try (SmbFile smbFile = (SmbFile) openConnection) {
		assertFalse(smbFile.exists());
		OutputStream os = openConnection.getOutputStream();
		IOUtils.write(nowString, os, Charset.defaultCharset());
		os.close();
		
		assertTrue(smbFile.exists());
		
		InputStream is = openConnection.getInputStream();
		String string = IOUtils.toString(is, "UTF-8");
		is.close();
		assertEquals(nowString, string);
		
		smbFile.delete();
		assertFalse(smbFile.exists());
	}
}
 
Example #8
Source File: NtlmAuthenticationHandler.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
@Override
protected final HandlerResult doAuthentication(
        final Credential credential) throws GeneralSecurityException, PreventedException {

    final SpnegoCredential ntlmCredential = (SpnegoCredential) credential;
    final byte[] src = ntlmCredential.getInitToken();

    UniAddress dc = null;

    boolean success = false;
    try {
        if (this.loadBalance) {
            // find the first dc that matches the includepattern
            if (this.includePattern != null) {
                final NbtAddress[] dcs= NbtAddress.getAllByName(this.domainController, NBT_ADDRESS_TYPE, null, null);
                for (final NbtAddress dc2 : dcs) {
                    if(dc2.getHostAddress().matches(this.includePattern)){
                        dc = new UniAddress(dc2);
                        break;
                    }
                }
            } else {
                dc = new UniAddress(NbtAddress.getByName(this.domainController, NBT_ADDRESS_TYPE, null));
            }
        } else {
            dc = UniAddress.getByName(this.domainController, true);
        }
        final byte[] challenge = SmbSession.getChallenge(dc);

        switch (src[NTLM_TOKEN_TYPE_FIELD_INDEX]) {
            case NTLM_TOKEN_TYPE_ONE:
                logger.debug("Type 1 received");
                final Type1Message type1 = new Type1Message(src);
                final Type2Message type2 = new Type2Message(type1,
                        challenge, null);
                logger.debug("Type 2 returned. Setting next token.");
                ntlmCredential.setNextToken(type2.toByteArray());
                break;
            case NTLM_TOKEN_TYPE_THREE:
                logger.debug("Type 3 received");
                final Type3Message type3 = new Type3Message(src);
                final byte[] lmResponse = type3.getLMResponse() == null ? new byte[0] : type3.getLMResponse();
                final byte[] ntResponse = type3.getNTResponse() == null ? new byte[0] : type3.getNTResponse();
                final NtlmPasswordAuthentication ntlm = new NtlmPasswordAuthentication(
                        type3.getDomain(), type3.getUser(), challenge,
                        lmResponse, ntResponse);
                logger.debug("Trying to authenticate {} with domain controller", type3.getUser());
                try {
                    SmbSession.logon(dc, ntlm);
                    ntlmCredential.setPrincipal(this.principalFactory.createPrincipal(type3.getUser()));
                    success = true;
                } catch (final SmbAuthException sae) {
                    throw new FailedLoginException(sae.getMessage());
                }
                break;
            default:
                logger.debug("Unknown type: {}", src[NTLM_TOKEN_TYPE_FIELD_INDEX]);
        }
    } catch (final Exception e) {
        throw new FailedLoginException(e.getMessage());
    }

    if (!success) {
        throw new FailedLoginException();
    }
    return new DefaultHandlerResult(this, new BasicCredentialMetaData(ntlmCredential), ntlmCredential.getPrincipal());
}
 
Example #9
Source File: NtlmAuthenticationHandler.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
@Override
protected final HandlerResult doAuthentication(
        final Credential credential) throws GeneralSecurityException, PreventedException {

    final SpnegoCredential ntlmCredential = (SpnegoCredential) credential;
    final byte[] src = ntlmCredential.getInitToken();

    UniAddress dc = null;

    boolean success = false;
    try {
        if (this.loadBalance) {
            // find the first dc that matches the includepattern
            if(this.includePattern != null){
                NbtAddress [] dcs  = NbtAddress.getAllByName(this.domainController, 0x1C, null, null);
                for (NbtAddress dc2 : dcs) {
                    if(dc2.getHostAddress().matches(this.includePattern)){
                        dc = new UniAddress(dc2);
                        break;
                    }
                }
            } else {
                dc = new UniAddress(NbtAddress.getByName(this.domainController,
                        0x1C, null));
            }
        } else {
            dc = UniAddress.getByName(this.domainController, true);
        }
        final byte[] challenge = SmbSession.getChallenge(dc);

        switch (src[8]) {
            case 1:
                logger.debug("Type 1 received");
                final Type1Message type1 = new Type1Message(src);
                final Type2Message type2 = new Type2Message(type1,
                        challenge, null);
                logger.debug("Type 2 returned. Setting next token.");
                ntlmCredential.setNextToken(type2.toByteArray());
            case 3:
                logger.debug("Type 3 received");
                final Type3Message type3 = new Type3Message(src);
                final byte[] lmResponse = type3.getLMResponse() == null ? new byte[0] : type3.getLMResponse();
                byte[] ntResponse = type3.getNTResponse() == null ? new byte[0] : type3.getNTResponse();
                final NtlmPasswordAuthentication ntlm = new NtlmPasswordAuthentication(
                        type3.getDomain(), type3.getUser(), challenge,
                        lmResponse, ntResponse);
                logger.debug("Trying to authenticate {} with domain controller", type3.getUser());
                try {
                    SmbSession.logon(dc, ntlm);
                    ntlmCredential.setPrincipal(new SimplePrincipal(type3.getUser()));
                    success = true;
                } catch (final SmbAuthException sae) {
                    throw new FailedLoginException(sae.getMessage());
                }
            default:
                logger.debug("Unknown type: {}", src[8]);
        }
    } catch (final Exception e) {
        throw new FailedLoginException(e.getMessage());
    }

    if (!success) {
        throw new FailedLoginException();
    }
    return new HandlerResult(this, new BasicCredentialMetaData(ntlmCredential), ntlmCredential.getPrincipal());
}
 
Example #10
Source File: SessionTest.java    From jcifs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@SuppressWarnings ( "deprecation" )
@Test ( expected = SmbAuthException.class )
public void testPoolLogonInvalid () throws CIFSException, UnknownHostException {
    CIFSContext ctx = getContext().withCredentials(new NtlmPasswordAuthenticator(getTestUserDomain(), getTestUser(), "invalid"));
    ctx.getTransportPool().logon(ctx, ctx.getNameServiceClient().getByName(getTestServer()));
}
 
Example #11
Source File: SessionTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 4 votes vote down vote up
@SuppressWarnings ( "deprecation" )
@Test ( expected = SmbAuthException.class )
public void testPoolLogonInvalid () throws CIFSException, UnknownHostException {
    CIFSContext ctx = getContext().withCredentials(new NtlmPasswordAuthenticator(getTestUserDomain(), getTestUser(), "invalid"));
    ctx.getTransportPool().logon(ctx, ctx.getNameServiceClient().getByName(getTestServer()));
}