Search in sources :

Example 1 with NbtAddress

use of jcifs.netbios.NbtAddress in project cas by apereo.

the class NtlmAuthenticationHandler method doAuthentication.

@Override
protected HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
    final SpnegoCredential ntlmCredential = (SpnegoCredential) credential;
    final byte[] src = ntlmCredential.getInitToken();
    final UniAddress dc;
    boolean success = false;
    try {
        if (this.loadBalance) {
            // find the first dc that matches the includepattern
            if (StringUtils.isNotBlank(this.includePattern)) {
                final NbtAddress[] dcs = NbtAddress.getAllByName(this.domainController, NBT_ADDRESS_TYPE, null, null);
                dc = Arrays.stream(dcs).filter(dc2 -> dc2.getHostAddress().matches(this.includePattern)).findFirst().map(UniAddress::new).orElse(null);
            } 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());
}
Also used : Type2Message(jcifs.ntlmssp.Type2Message) Type3Message(jcifs.ntlmssp.Type3Message) GeneralSecurityException(java.security.GeneralSecurityException) FailedLoginException(javax.security.auth.login.FailedLoginException) PreventedException(org.apereo.cas.authentication.PreventedException) SmbAuthException(jcifs.smb.SmbAuthException) UniAddress(jcifs.UniAddress) Type1Message(jcifs.ntlmssp.Type1Message) SpnegoCredential(org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential) SmbAuthException(jcifs.smb.SmbAuthException) FailedLoginException(javax.security.auth.login.FailedLoginException) NtlmPasswordAuthentication(jcifs.smb.NtlmPasswordAuthentication) DefaultHandlerResult(org.apereo.cas.authentication.DefaultHandlerResult) BasicCredentialMetaData(org.apereo.cas.authentication.BasicCredentialMetaData) NbtAddress(jcifs.netbios.NbtAddress)

Example 2 with NbtAddress

use of jcifs.netbios.NbtAddress in project opennms by OpenNMS.

the class SmbMonitor method poll.

@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    // Extract the address
    //
    InetAddress ipAddr = svc.getAddress();
    // Default is a failed status
    //
    PollStatus serviceStatus = PollStatus.unavailable();
    // Attempt to retrieve NetBIOS name of this interface in order
    // to determine if SMB is supported.
    //
    NbtAddress nbtAddr = null;
    /*
         * This try block was updated to reflect the behavior of the plugin.
         */
    final String hostAddress = InetAddressUtils.str(ipAddr);
    final boolean doNodeStatus = ParameterMap.getKeyedBoolean(parameters, DO_NODE_STATUS, DO_NODE_STATUS_DEFAULT);
    try {
        nbtAddr = NbtAddress.getByName(hostAddress);
        if (doNodeStatus) {
            nbtAddr.getNodeType();
        }
        if (!nbtAddr.getHostName().equals(hostAddress))
            serviceStatus = PollStatus.available();
    } catch (UnknownHostException uhE) {
        String reason = "Unknown host exception generated for " + hostAddress + ", reason: " + uhE.getLocalizedMessage();
        LOG.debug(reason);
        serviceStatus = PollStatus.unavailable(reason);
    } catch (RuntimeException rE) {
        LOG.debug("Unexpected runtime exception", rE);
        serviceStatus = PollStatus.unavailable("Unexpected runtime exception");
    } catch (Throwable e) {
        LOG.debug("Unexpected exception", e);
        serviceStatus = PollStatus.unavailable("Unexpected exception");
    }
    //
    return serviceStatus;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) NbtAddress(jcifs.netbios.NbtAddress)

Aggregations

NbtAddress (jcifs.netbios.NbtAddress)2 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 UniAddress (jcifs.UniAddress)1 Type1Message (jcifs.ntlmssp.Type1Message)1 Type2Message (jcifs.ntlmssp.Type2Message)1 Type3Message (jcifs.ntlmssp.Type3Message)1 NtlmPasswordAuthentication (jcifs.smb.NtlmPasswordAuthentication)1 SmbAuthException (jcifs.smb.SmbAuthException)1 BasicCredentialMetaData (org.apereo.cas.authentication.BasicCredentialMetaData)1 DefaultHandlerResult (org.apereo.cas.authentication.DefaultHandlerResult)1 PreventedException (org.apereo.cas.authentication.PreventedException)1 SpnegoCredential (org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1