Search in sources :

Example 1 with UnbindRequestProtocolOp

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

the class UNBOUNDIDTESTServer method run.

/**
 * Performs the processing for this server.
 */
@Override()
public void run() {
    try {
        serverSocket = new ServerSocket(0);
        listenPort = serverSocket.getLocalPort();
        while (!stopRequested.get()) {
            // Accept a connection from a client.
            clientSocket = serverSocket.accept();
            final InputStream inputStream = clientSocket.getInputStream();
            final OutputStream outputStream = clientSocket.getOutputStream();
            final ASN1StreamReader asn1Reader = new ASN1StreamReader(inputStream, 0);
            // The client must first send an UNBOUNDID-TEST bind request with no
            // credentials.
            LDAPMessage requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            BindRequestProtocolOp bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNull(bindRequestOp.getSASLCredentials());
            // Return a "SASL bind in progress" response.
            LDAPMessage responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SASL_BIND_IN_PROGRESS_INT_VALUE, null, null, null, null));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // The next request must be an UNBOUNDID-TEST bind request with
            // credentials.  We won't do anything to validate the credentials, but
            // we will look at the third element to see what QoP the client
            // requested.
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNotNull(bindRequestOp.getSASLCredentials());
            final ASN1Sequence credSequence = ASN1Sequence.decodeAsSequence(bindRequestOp.getSASLCredentials().getValue());
            final ASN1Element[] credElements = credSequence.elements();
            final SASLQualityOfProtection qop = SASLQualityOfProtection.forName(ASN1OctetString.decodeAsOctetString(credElements[2]).stringValue());
            assertNotNull(qop);
            final boolean qopEncode = ((qop == SASLQualityOfProtection.AUTH_INT) || (qop == SASLQualityOfProtection.AUTH_CONF));
            // Return a "success" response.  Include server SASL credentials with
            // the requested QoP.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null, new ASN1OctetString(qop.toString())));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final SearchRequestProtocolOp searchRequestOp = requestMessage.getSearchRequestProtocolOp();
            assertEquals(searchRequestOp.getBaseDN(), "");
            assertEquals(searchRequestOp.getScope(), SearchScope.BASE);
            assertEquals(searchRequestOp.getFilter(), Filter.createPresenceFilter("objectClass"));
            assertEquals(searchRequestOp.getAttributes(), Arrays.asList("1.1"));
            // Return a search result entry message with a DN but no attributes.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultEntryProtocolOp("", Collections.<Attribute>emptyList()));
            byte[] messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // Return a "success" search result done message.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultDoneProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null));
            messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // The next request should be an unbind request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final UnbindRequestProtocolOp unbindRequestOp = requestMessage.getUnbindRequestProtocolOp();
            // Close the connection.
            try {
                asn1Reader.close();
            } catch (final Exception e) {
            }
            try {
                outputStream.close();
            } catch (final Exception e) {
            }
            try {
                clientSocket.close();
            } catch (final Exception e) {
            }
            clientSocket = null;
        }
    } catch (final Exception e) {
        stopServer();
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) ServerSocket(java.net.ServerSocket) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) UnbindRequestProtocolOp(com.unboundid.ldap.protocol.UnbindRequestProtocolOp)

Example 2 with UnbindRequestProtocolOp

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

the class LDAPConnectionTestCase method testSendMessageNotConnected.

/**
 * Tests the {@code sendMessage} method with a connection that is not
 * established.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testSendMessageNotConnected() throws Exception {
    LDAPConnection conn = new LDAPConnection();
    assertFalse(conn.isConnected());
    assertNull(conn.getConnectedAddress());
    assertTrue(conn.getConnectedPort() < 0);
    assertNotNull(conn.toString());
    conn.sendMessage(new LDAPMessage(conn.nextMessageID(), new UnbindRequestProtocolOp()), 10000L);
}
Also used : LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) UnbindRequestProtocolOp(com.unboundid.ldap.protocol.UnbindRequestProtocolOp) Test(org.testng.annotations.Test)

Example 3 with UnbindRequestProtocolOp

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

the class LDAPConnection method terminate.

/**
 * Unbinds from the server and closes the connection, optionally including the
 * provided set of controls in the unbind request.  This method is only
 * intended for internal use, since it does not make any attempt to release
 * the connection back to its associated connection pool, if there is one.
 *
 * @param  controls  The set of controls to include in the unbind request.  It
 *                   may be {@code null} if there are not to be any controls
 *                   sent in the unbind request.
 */
void terminate(@Nullable final Control[] controls) {
    if (isConnected() && (!unbindRequestSent)) {
        try {
            unbindRequestSent = true;
            setDisconnectInfo(DisconnectType.UNBIND, null, null);
            final int messageID = nextMessageID();
            if (Debug.debugEnabled(DebugType.LDAP)) {
                Debug.debugLDAPRequest(Level.INFO, createUnbindRequestString(controls), messageID, this);
            }
            final LDAPConnectionLogger logger = connectionOptions.getConnectionLogger();
            if (logger != null) {
                final List<Control> controlList;
                if (controls == null) {
                    controlList = Collections.emptyList();
                } else {
                    controlList = Arrays.asList(controls);
                }
                logger.logUnbindRequest(this, messageID, controlList);
            }
            connectionStatistics.incrementNumUnbindRequests();
            sendMessage(new LDAPMessage(messageID, new UnbindRequestProtocolOp(), controls), connectionOptions.getResponseTimeoutMillis(OperationType.UNBIND));
        } catch (final Exception e) {
            Debug.debugException(e);
        }
    }
    setClosed();
}
Also used : RetainIdentityRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RetainIdentityRequestControl) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) UnbindRequestProtocolOp(com.unboundid.ldap.protocol.UnbindRequestProtocolOp) LDIFException(com.unboundid.ldif.LDIFException)

Aggregations

LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)3 UnbindRequestProtocolOp (com.unboundid.ldap.protocol.UnbindRequestProtocolOp)3 ASN1Element (com.unboundid.asn1.ASN1Element)1 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)1 ASN1StreamReader (com.unboundid.asn1.ASN1StreamReader)1 BindRequestProtocolOp (com.unboundid.ldap.protocol.BindRequestProtocolOp)1 BindResponseProtocolOp (com.unboundid.ldap.protocol.BindResponseProtocolOp)1 SearchRequestProtocolOp (com.unboundid.ldap.protocol.SearchRequestProtocolOp)1 SearchResultDoneProtocolOp (com.unboundid.ldap.protocol.SearchResultDoneProtocolOp)1 SearchResultEntryProtocolOp (com.unboundid.ldap.protocol.SearchResultEntryProtocolOp)1 RetainIdentityRequestControl (com.unboundid.ldap.sdk.unboundidds.controls.RetainIdentityRequestControl)1 LDIFException (com.unboundid.ldif.LDIFException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 Test (org.testng.annotations.Test)1