Search in sources :

Example 26 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V13WireOperations method authReceiveResponse.

@Override
public void authReceiveResponse(FbWireAttachment.AcceptPacket acceptPacket, FbWireOperations.ProcessAttachCallback processAttachCallback) throws SQLException, IOException {
    assert acceptPacket == null || acceptPacket.operation == op_cond_accept : "Unexpected operation in AcceptPacket";
    final XdrInputStream xdrIn = getXdrIn();
    final XdrOutputStream xdrOut = getXdrOut();
    final ClientAuthBlock clientAuthBlock = getClientAuthBlock();
    final Encoding encoding = getEncoding();
    while (true) {
        String pluginName;
        byte[] data;
        if (acceptPacket != null) {
            data = acceptPacket.p_acpt_data;
            pluginName = acceptPacket.p_acpt_plugin;
            addServerKeys(acceptPacket.p_acpt_keys);
            log.debug(String.format("authReceiveResponse: cond_accept data=%d pluginName=%d '%s'", data.length, pluginName != null ? pluginName.length() : null, pluginName));
            // TODO handle compression
            acceptPacket = null;
        } else {
            int operation = readNextOperation();
            switch(operation) {
                case op_trusted_auth:
                    // p_trau_data
                    xdrIn.readBuffer();
                    throw new FbExceptionBuilder().nonTransientConnectionException(JaybirdErrorCodes.jb_receiveTrustedAuth_NotSupported).toFlatSQLException();
                case op_cont_auth:
                    // p_data
                    data = xdrIn.readBuffer();
                    // p_name
                    pluginName = xdrIn.readString(encoding);
                    // p_list (ignore?)
                    xdrIn.readBuffer();
                    // p_keys
                    addServerKeys(xdrIn.readBuffer());
                    log.debug(String.format("authReceiveResponse: cont_auth data=%d pluginName=%d '%s'", data.length, pluginName.length(), pluginName));
                    break;
                case op_cond_accept:
                    // Note this is the equivalent of handling the acceptPacket != null above
                    // p_acpt_version
                    xdrIn.readInt();
                    // p_acpt_architecture
                    xdrIn.readInt();
                    // p_acpt_type
                    xdrIn.readInt();
                    // p_acpt_data
                    data = xdrIn.readBuffer();
                    // p_acpt_plugin
                    pluginName = xdrIn.readString(encoding);
                    // p_acpt_authenticated
                    xdrIn.readInt();
                    // p_acpt_keys
                    addServerKeys(xdrIn.readBuffer());
                    log.debug(String.format("authReceiveResponse: cond_accept data=%d pluginName=%d '%s'", data.length, pluginName.length(), pluginName));
                    // TODO handle compression
                    break;
                case op_response:
                    GenericResponse response = (GenericResponse) readOperationResponse(operation, null);
                    boolean wasAuthComplete = clientAuthBlock.isAuthComplete();
                    clientAuthBlock.setAuthComplete(true);
                    processAttachCallback.processAttachResponse(response);
                    addServerKeys(response.getData());
                    WireCrypt wireCrypt = getAttachProperties().getWireCrypt();
                    if (!wasAuthComplete && wireCrypt != WireCrypt.DISABLED) {
                        tryKnownServerKeys();
                    }
                    return;
                default:
                    throw new SQLException(String.format("Unsupported operation code: %d", operation));
            }
        }
        if (pluginName != null && pluginName.length() > 0 && Objects.equals(pluginName, clientAuthBlock.getCurrentPluginName())) {
            pluginName = null;
        }
        if (pluginName != null && pluginName.length() > 0) {
            if (!clientAuthBlock.switchPlugin(pluginName)) {
                break;
            }
        }
        if (!clientAuthBlock.hasPlugin()) {
            break;
        }
        clientAuthBlock.setServerData(data);
        log.debug(String.format("receiveResponse: authenticate(%s)", clientAuthBlock.getCurrentPluginName()));
        clientAuthBlock.authenticate();
        xdrOut.writeInt(op_cont_auth);
        // TODO Move to ClientAuthBlock?
        // p_data
        xdrOut.writeBuffer(clientAuthBlock.getClientData());
        // p_name
        xdrOut.writeString(clientAuthBlock.getCurrentPluginName(), encoding);
        if (clientAuthBlock.isFirstTime()) {
            // p_list
            xdrOut.writeString(clientAuthBlock.getPluginNames(), encoding);
            clientAuthBlock.setFirstTime(false);
        } else {
            // p_list
            xdrOut.writeBuffer(null);
        }
        // p_keys
        xdrOut.writeBuffer(null);
        xdrOut.flush();
    }
    // If we have exited from the cycle, this mean auth failed
    throw new FbExceptionBuilder().exception(ISCConstants.isc_login).toFlatSQLException();
}
Also used : XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream) GenericResponse(org.firebirdsql.gds.ng.wire.GenericResponse) SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Encoding(org.firebirdsql.encodings.Encoding) WireCrypt(org.firebirdsql.gds.ng.WireCrypt) ClientAuthBlock(org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)

Example 27 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Service method internalDetach.

@Override
protected void internalDetach() throws SQLException {
    synchronized (getSynchronizationObject()) {
        try {
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                if (isAttached()) {
                    xdrOut.writeInt(op_service_detach);
                    xdrOut.writeInt(getHandle());
                }
                xdrOut.writeInt(op_disconnect);
                xdrOut.flush();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            if (isAttached()) {
                try {
                    // Consume op_detach response
                    wireOperations.readResponse(null);
                } catch (IOException ex) {
                    throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
                }
            }
            try {
                closeConnection();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
        } catch (SQLException ex) {
            try {
                closeConnection();
            } catch (Exception ex2) {
            // ignore
            }
            throw ex;
        } finally {
            setDetached();
        }
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 28 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Service method getServiceInfo.

@Override
public byte[] getServiceInfo(ServiceParameterBuffer serviceParameterBuffer, ServiceRequestBuffer serviceRequestBuffer, int maxBufferLength) throws SQLException {
    try {
        checkAttached();
        synchronized (getSynchronizationObject()) {
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                xdrOut.writeInt(op_service_info);
                xdrOut.writeInt(getHandle());
                // incarnation
                xdrOut.writeInt(0);
                xdrOut.writeBuffer(serviceParameterBuffer != null ? serviceParameterBuffer.toBytes() : null);
                xdrOut.writeBuffer(serviceRequestBuffer.toBytes());
                xdrOut.writeInt(maxBufferLength);
                xdrOut.flush();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            try {
                GenericResponse genericResponse = readGenericResponse(null);
                return genericResponse.getData();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
            }
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 29 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Service method sendAttachToBuffer.

/**
 * Sends the buffer for op_service_attach
 *
 * @param spb
 *         Service parameter buffer
 * @throws SQLException
 *         If the connection is not open
 * @throws IOException
 *         For errors writing to the connection
 */
protected void sendAttachToBuffer(ServiceParameterBuffer spb) throws SQLException, IOException {
    final XdrOutputStream xdrOut = getXdrOut();
    xdrOut.writeInt(op_service_attach);
    // Service object ID
    xdrOut.writeInt(0);
    xdrOut.writeString(connection.getAttachObjectName(), getEncoding());
    xdrOut.writeTyped(spb);
}
Also used : XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream)

Example 30 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Service method startServiceAction.

@Override
public void startServiceAction(ServiceRequestBuffer serviceRequestBuffer) throws SQLException {
    try {
        checkAttached();
        synchronized (getSynchronizationObject()) {
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                xdrOut.writeInt(op_service_start);
                xdrOut.writeInt(getHandle());
                // incarnation
                xdrOut.writeInt(0);
                xdrOut.writeBuffer(serviceRequestBuffer.toBytes());
                xdrOut.flush();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            try {
                readGenericResponse(null);
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
            }
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Aggregations

XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)41 SQLException (java.sql.SQLException)25 IOException (java.io.IOException)24 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)22 SimpleServer (org.firebirdsql.common.SimpleServer)4 Encoding (org.firebirdsql.encodings.Encoding)3 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)2 GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 BitSet (java.util.BitSet)1 WireCrypt (org.firebirdsql.gds.ng.WireCrypt)1 ClientAuthBlock (org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)1 EncryptionIdentifier (org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)1