Search in sources :

Example 6 with ExtendedResponseProtocolOp

use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.

the class InMemoryOperationInterceptorRequestHandler method processExtendedRequest.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public LDAPMessage processExtendedRequest(final int messageID, @NotNull final ExtendedRequestProtocolOp request, @NotNull final List<Control> controls) {
    final InterceptedExtendedOperation op = new InterceptedExtendedOperation(connection, messageID, request, toArray(controls));
    activeOperations.put(messageID, op);
    try {
        for (final InMemoryOperationInterceptor i : interceptors) {
            try {
                i.processExtendedRequest(op);
            } catch (final LDAPException le) {
                Debug.debugException(le);
                return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(le.toLDAPResult()), le.getResponseControls());
            } catch (final Exception e) {
                Debug.debugException(e);
                return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_REQUEST_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null, null));
            }
        }
        final LDAPMessage resultMessage = wrappedHandler.processExtendedRequest(messageID, new ExtendedRequestProtocolOp(op.getRequest()), op.getRequest().getControlList());
        op.setResult(resultMessage.getExtendedResponseProtocolOp().toExtendedResult(toArray(resultMessage.getControls())));
        for (final InMemoryOperationInterceptor i : interceptors) {
            try {
                i.processExtendedResult(op);
            } catch (final Exception e) {
                Debug.debugException(e);
                return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_RESULT_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null, null));
            }
        }
        return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(op.getResult()), op.getResult().getResponseControls());
    } finally {
        activeOperations.remove(messageID);
    }
}
Also used : ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) LDAPException(com.unboundid.ldap.sdk.LDAPException) ExtendedRequestProtocolOp(com.unboundid.ldap.protocol.ExtendedRequestProtocolOp) NotNull(com.unboundid.util.NotNull)

Example 7 with ExtendedResponseProtocolOp

use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.

the class AccessLogRequestHandler method processExtendedRequest.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public LDAPMessage processExtendedRequest(final int messageID, @NotNull final ExtendedRequestProtocolOp request, @NotNull final List<Control> controls) {
    final long opID = nextOperationID.getAndIncrement();
    final StringBuilder b = getRequestHeader("EXTENDED", opID, messageID);
    b.append(" requestOID=\"");
    b.append(request.getOID());
    b.append('"');
    logHandler.publish(new LogRecord(Level.INFO, b.toString()));
    logHandler.flush();
    final long startTimeNanos = System.nanoTime();
    final LDAPMessage responseMessage = requestHandler.processExtendedRequest(messageID, request, controls);
    final long eTimeNanos = System.nanoTime() - startTimeNanos;
    final ExtendedResponseProtocolOp protocolOp = responseMessage.getExtendedResponseProtocolOp();
    generateResponse(b, "EXTENDED", opID, messageID, protocolOp.getResultCode(), protocolOp.getDiagnosticMessage(), protocolOp.getMatchedDN(), protocolOp.getReferralURLs(), eTimeNanos);
    final String responseOID = protocolOp.getResponseOID();
    if (responseOID != null) {
        b.append(" responseOID=\"");
        b.append(responseOID);
        b.append('"');
    }
    logHandler.publish(new LogRecord(Level.INFO, b.toString()));
    logHandler.flush();
    return responseMessage;
}
Also used : ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) LogRecord(java.util.logging.LogRecord) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) NotNull(com.unboundid.util.NotNull)

Example 8 with ExtendedResponseProtocolOp

use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.

the class ManageCertificatesServerCertificateCollector method run.

/**
 * Performs the core processing for this thread.  It will establish a TCP
 * connection to the specified server, optionally perform the LDAP StartTLS
 * operation, and initiate TLS negotiation so that the server's certificate
 * chain can be
 */
@Override()
public void run() {
    // Establish a non-secure connection to the target server.
    final String hostPort = hostname + ':' + port;
    if (verbose) {
        manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_CONNECTING.get(hostPort));
    }
    final Socket nonSecureSocket;
    try {
        nonSecureSocket = new Socket();
        final InetAddress address = LDAPConnectionOptions.DEFAULT_NAME_RESOLVER.getByName(hostname);
        nonSecureSocket.connect(new InetSocketAddress(address, port), 60_000);
        if (verbose) {
            manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_CONNECTED.get());
        }
    } catch (final Exception e) {
        Debug.debugException(e);
        final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_CONNECT_FAILED.get(hostPort);
        manageCertificates.err();
        manageCertificates.wrapErr(0, WRAP_COLUMN, message);
        e.printStackTrace(manageCertificates.getErr());
        queue.offer(new CertException(message, e));
        return;
    }
    try {
        // If we should send an LDAP StartTLS extended request, then do that now.
        if (useLDAPStartTLS) {
            if (verbose) {
                manageCertificates.out();
                manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_SENDING_START_TLS.get());
            }
            final LDAPMessage startTLSRequestMessage = new LDAPMessage(1, new ExtendedRequestProtocolOp(StartTLSExtendedRequest.STARTTLS_REQUEST_OID, null));
            try {
                nonSecureSocket.getOutputStream().write(startTLSRequestMessage.encode().encode());
                nonSecureSocket.getOutputStream().flush();
                final ASN1StreamReader asn1Reader = new ASN1StreamReader(nonSecureSocket.getInputStream());
                final LDAPMessage startTLSResponseMessage = LDAPMessage.readFrom(asn1Reader, true);
                if (startTLSResponseMessage == null) {
                    // This could happen if the server terminated the connection for
                    // some reason (e.g., it's not an LDAP server, or the user specified
                    // an already-secure port).
                    final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_FAILED.get();
                    manageCertificates.wrapErr(0, WRAP_COLUMN, message);
                    queue.offer(new CertException(message));
                    return;
                }
                final ExtendedResponseProtocolOp startTLSResponse = startTLSResponseMessage.getExtendedResponseProtocolOp();
                if (startTLSResponse.getResultCode() == ResultCode.SUCCESS_INT_VALUE) {
                    if (verbose) {
                        manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_SUCCESSFUL.get());
                    }
                } else {
                    final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_FAILED.get();
                    manageCertificates.wrapErr(0, WRAP_COLUMN, message);
                    final String[] referralURLArray = startTLSResponse.getReferralURLs().toArray(StaticUtils.NO_STRINGS);
                    final Control[] responseControlArray = startTLSResponseMessage.getControls().toArray(StaticUtils.NO_CONTROLS);
                    final ExtendedResult extendedResult = new ExtendedResult(startTLSRequestMessage.getMessageID(), ResultCode.valueOf(startTLSResponse.getResultCode()), startTLSResponse.getDiagnosticMessage(), startTLSResponse.getMatchedDN(), referralURLArray, startTLSResponse.getResponseOID(), startTLSResponse.getResponseValue(), responseControlArray);
                    for (final String line : ResultUtils.formatResult(extendedResult, false, 0, WRAP_COLUMN)) {
                        manageCertificates.err(line);
                    }
                    queue.offer(new CertException(message));
                    return;
                }
            } catch (final Exception e) {
                final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_FAILED.get();
                manageCertificates.wrapErr(0, WRAP_COLUMN, message);
                e.printStackTrace(manageCertificates.getErr());
                queue.offer(new CertException(message));
                return;
            }
        }
        // Convert the non-secure Socket to an SSLSocket and begin TLS
        // negotiation.
        final SSLSocket sslSocket;
        try {
            if (verbose) {
                manageCertificates.out();
                manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_BEGINNING_TLS_NEGOTIATION.get());
            }
            final SSLUtil sslUtil = new SSLUtil(this);
            sslSocket = (SSLSocket) sslUtil.createSSLSocketFactory().createSocket(nonSecureSocket, hostname, port, true);
            sslSocket.startHandshake();
            sslSocket.setSoTimeout(1000);
        } catch (final Exception e) {
            Debug.debugException(e);
            final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_ERROR_STARTING_TLS_NEGOTIATION.get();
            manageCertificates.wrapErr(0, WRAP_COLUMN, message);
            e.printStackTrace(manageCertificates.getErr());
            queue.offer(new CertException(message, e));
            return;
        }
        try {
            final long stopWaitingTime = System.currentTimeMillis() + 60_000L;
            while ((System.currentTimeMillis() < stopWaitingTime) && (!gotCertificateChain)) {
                try {
                    final int bytesRead = sslSocket.getInputStream().read();
                    if ((bytesRead < 0) && gotCertificateChain) {
                        // to the queue, so we don't need to add anything here.
                        return;
                    }
                } catch (final Exception e) {
                    Debug.debugException(e);
                }
            }
            if (!gotCertificateChain) {
                // If we have gotten here, then it should mean that we timed out
                // without having gotten the certificate chain.
                final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_NO_CERT_CHAIN_RECEIVED.get(hostPort);
                manageCertificates.wrapErr(0, WRAP_COLUMN, message);
                queue.offer(new CertException(message));
                return;
            }
            if (verbose) {
                final SSLSession sslSession = sslSocket.getSession();
                final String negotiatedProtocol = sslSession.getProtocol();
                if (negotiatedProtocol != null) {
                    manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_NEGOTIATED_TLS_PROTOCOL.get(negotiatedProtocol));
                }
                final String negotiatedCipherSuite = sslSession.getCipherSuite();
                if (negotiatedCipherSuite != null) {
                    manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_NEGOTIATED_TLS_SUITE.get(negotiatedCipherSuite));
                }
            }
        } finally {
            try {
                sslSocket.close();
            } catch (final Exception e) {
                Debug.debugException(e);
            }
        }
    } finally {
        try {
            nonSecureSocket.close();
        } catch (final Exception e) {
            Debug.debugException(e);
        }
    }
}
Also used : ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SSLSession(javax.net.ssl.SSLSession) CertificateException(java.security.cert.CertificateException) ExtendedRequestProtocolOp(com.unboundid.ldap.protocol.ExtendedRequestProtocolOp) SSLUtil(com.unboundid.util.ssl.SSLUtil) Control(com.unboundid.ldap.sdk.Control) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) InetAddress(java.net.InetAddress) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 9 with ExtendedResponseProtocolOp

use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.

the class LDAPDebuggerTestCase method testSuccessfulExtendedOperation.

/**
 * Provides test coverage for a successful extended operation.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSuccessfulExtendedOperation() throws Exception {
    TestRequestHandler.setReturnOp(new ExtendedResponseProtocolOp(0, null, null, null, null, null));
    final ExtendedRequest r = new ExtendedRequest("1.2.3.4", new ASN1OctetString("foo"), new Control[] { new Control("1.2.3.5") });
    conn.processExtendedOperation(r);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) ExtendedRequest(com.unboundid.ldap.sdk.ExtendedRequest) Test(org.testng.annotations.Test)

Example 10 with ExtendedResponseProtocolOp

use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.

the class LDAPDebuggerTestCase method testFailedExtendedOperation.

/**
 * Provides test coverage for a failed extended operation.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testFailedExtendedOperation() throws Exception {
    TestRequestHandler.setReturnOp(new ExtendedResponseProtocolOp(32, "dc=example,dc=com", "msg", Arrays.asList("ldap://server1.example.com/dc=example,dc=com", "ldap://server2.example.com/dc=example,dc=com"), "1.2.3.5", new ASN1OctetString("baz")));
    TestRequestHandler.setReturnIntermediateResponses(new IntermediateResponseProtocolOp("5.6.7.8", new ASN1OctetString("a")), new IntermediateResponseProtocolOp("5.6.7.9", new ASN1OctetString("b")));
    try {
        conn.processExtendedOperation("1.2.3.4", new ASN1OctetString("bar"));
    } finally {
        TestRequestHandler.setReturnIntermediateResponses();
        TestRequestHandler.setControls();
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) IntermediateResponseProtocolOp(com.unboundid.ldap.protocol.IntermediateResponseProtocolOp) Test(org.testng.annotations.Test)

Aggregations

ExtendedResponseProtocolOp (com.unboundid.ldap.protocol.ExtendedResponseProtocolOp)14 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)11 Control (com.unboundid.ldap.sdk.Control)8 NotNull (com.unboundid.util.NotNull)8 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)7 LDAPException (com.unboundid.ldap.sdk.LDAPException)7 ExtendedRequestProtocolOp (com.unboundid.ldap.protocol.ExtendedRequestProtocolOp)4 ExtendedRequest (com.unboundid.ldap.sdk.ExtendedRequest)4 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)4 AddResponseProtocolOp (com.unboundid.ldap.protocol.AddResponseProtocolOp)3 DeleteResponseProtocolOp (com.unboundid.ldap.protocol.DeleteResponseProtocolOp)3 ModifyDNResponseProtocolOp (com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp)3 ModifyResponseProtocolOp (com.unboundid.ldap.protocol.ModifyResponseProtocolOp)3 LogRecord (java.util.logging.LogRecord)3 Test (org.testng.annotations.Test)3 BindResponseProtocolOp (com.unboundid.ldap.protocol.BindResponseProtocolOp)2 CompareResponseProtocolOp (com.unboundid.ldap.protocol.CompareResponseProtocolOp)2 SearchResultDoneProtocolOp (com.unboundid.ldap.protocol.SearchResultDoneProtocolOp)2 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)2 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)2