Search in sources :

Example 16 with StreamError

use of org.xmpp.packet.StreamError in project Openfire by igniterealtime.

the class XmppWebSocket method onError.

@OnWebSocketError
public void onError(Throwable error) {
    Log.error("Error detected; session: " + wsSession, error);
    closeStream(new StreamError(StreamError.Condition.internal_server_error));
    try {
        if (wsSession != null) {
            wsSession.disconnect();
        }
    } catch (Exception e) {
        Log.error("Error disconnecting websocket", e);
    }
}
Also used : StreamError(org.xmpp.packet.StreamError) IOException(java.io.IOException) UnknownStanzaException(org.jivesoftware.openfire.multiplex.UnknownStanzaException) OnWebSocketError(org.eclipse.jetty.websocket.api.annotations.OnWebSocketError)

Example 17 with StreamError

use of org.xmpp.packet.StreamError in project Openfire by igniterealtime.

the class ServerStanzaHandler method packetReceived.

/**
     * Make sure that the received packet has a TO and FROM values defined and that it was sent
     * from a previously validated domain. If the packet does not matches any of the above
     * conditions then a PacketRejectedException will be thrown.
     *
     * @param packet the received packet.
     * @throws UnauthorizedException if the packet does not include a TO or FROM or if the packet
     *                                 was sent from a domain that was not previously validated.
     */
private void packetReceived(Packet packet) throws UnauthorizedException {
    if (packet.getTo() == null || packet.getFrom() == null) {
        Log.debug("ServerStanzaHandler: Closing IncomingServerSession due to packet with no TO or FROM: " + packet.toXML());
        // Send a stream error saying that the packet includes no TO or FROM
        StreamError error = new StreamError(StreamError.Condition.improper_addressing);
        connection.deliverRawText(error.toXML());
        throw new UnauthorizedException("Packet with no TO or FROM attributes");
    } else if (!((LocalIncomingServerSession) session).isValidDomain(packet.getFrom().getDomain())) {
        Log.debug("ServerStanzaHandler: Closing IncomingServerSession due to packet with invalid domain: " + packet.toXML());
        // Send a stream error saying that the packet includes an invalid FROM
        StreamError error = new StreamError(StreamError.Condition.invalid_from);
        connection.deliverRawText(error.toXML());
        throw new UnauthorizedException("Packet with no TO or FROM attributes");
    }
}
Also used : LocalIncomingServerSession(org.jivesoftware.openfire.session.LocalIncomingServerSession) StreamError(org.xmpp.packet.StreamError) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException)

Example 18 with StreamError

use of org.xmpp.packet.StreamError in project Openfire by igniterealtime.

the class SocketReadingMode method negotiateTLS.

/**
     * Tries to secure the connection using TLS. If the connection is secured then reset
     * the parser to use the new secured reader. But if the connection failed to be secured
     * then send a <failure> stanza and close the connection.
     *
     * @return true if the connection was secured.
     */
protected boolean negotiateTLS() {
    if (socketReader.connection.getTlsPolicy() == Connection.TLSPolicy.disabled) {
        // Set the not_authorized error
        StreamError error = new StreamError(StreamError.Condition.not_authorized);
        // Deliver stanza
        socketReader.connection.deliverRawText(error.toXML());
        // Close the underlying connection
        socketReader.connection.close();
        // Log a warning so that admins can track this case from the server side
        Log.warn("TLS requested by initiator when TLS was never offered by server. " + "Closing connection : " + socketReader.connection);
        return false;
    }
    // Client requested to secure the connection using TLS. Negotiate TLS.
    try {
        // This code is only used for s2s
        socketReader.connection.startTLS(false);
    } catch (SSLHandshakeException e) {
        // RFC3620, section 5.4.3.2 "STARTTLS Failure" - close the socket *without* sending any more data (<failure/> nor </stream>).
        Log.info("STARTTLS negotiation (with: {}) failed.", socketReader.connection, e);
        socketReader.connection.forceClose();
        return false;
    } catch (IOException | RuntimeException e) {
        // RFC3620, section 5.4.2.2 "Failure case" - Send a <failure/> element, then close the socket.
        Log.warn("An exception occurred while performing STARTTLS negotiation (with: {})", socketReader.connection, e);
        socketReader.connection.deliverRawText("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>");
        socketReader.connection.close();
        return false;
    }
    return true;
}
Also used : StreamError(org.xmpp.packet.StreamError) IOException(java.io.IOException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 19 with StreamError

use of org.xmpp.packet.StreamError in project Openfire by igniterealtime.

the class ConnectionHandler method exceptionCaught.

@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
    Log.warn("Closing connection due to exception in session: " + session, cause);
    try {
        // OF-524: Determine stream:error message.
        final StreamError error;
        if (cause != null && (cause instanceof XMLNotWellFormedException || (cause.getCause() != null && cause.getCause() instanceof XMLNotWellFormedException))) {
            error = new StreamError(StreamError.Condition.not_well_formed);
        } else {
            error = new StreamError(StreamError.Condition.internal_server_error);
        }
        final Connection connection = (Connection) session.getAttribute(CONNECTION);
        connection.deliverRawText(error.toXML());
    } finally {
        final Connection connection = (Connection) session.getAttribute(CONNECTION);
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : StreamError(org.xmpp.packet.StreamError) Connection(org.jivesoftware.openfire.Connection)

Example 20 with StreamError

use of org.xmpp.packet.StreamError in project Openfire by igniterealtime.

the class ServerDialback method createIncomingSession.

/**
     * Returns a new {@link IncomingServerSession} with a domain validated by the Authoritative
     * Server. New domains may be added to the returned IncomingServerSession after they have
     * been validated. See
     * {@link LocalIncomingServerSession#validateSubsequentDomain(org.dom4j.Element)}. The remote
     * server will be able to send packets through this session whose domains were previously
     * validated.<p>
     *
     * When acting as an Authoritative Server this method will verify the requested key
     * and will return null since the underlying TCP connection will be closed after sending the
     * response to the Receiving Server.<p>
     *
     * @param reader reader of DOM documents on the connection to the remote server.
     * @return an IncomingServerSession that was previously validated against the remote server.
     * @throws IOException if an I/O error occurs while communicating with the remote server.
     * @throws XmlPullParserException if an error occurs while parsing XML packets.
     */
public LocalIncomingServerSession createIncomingSession(XMPPPacketReader reader) throws IOException, XmlPullParserException {
    XmlPullParser xpp = reader.getXPPParser();
    StringBuilder sb;
    if ("jabber:server:dialback".equals(xpp.getNamespace("db"))) {
        Log.debug("ServerDialback: Processing incoming session.");
        StreamID streamID = sessionManager.nextStreamID();
        sb = new StringBuilder();
        sb.append("<stream:stream");
        sb.append(" xmlns:stream=\"http://etherx.jabber.org/streams\"");
        sb.append(" xmlns=\"jabber:server\" xmlns:db=\"jabber:server:dialback\"");
        sb.append(" id=\"");
        sb.append(streamID.toString());
        sb.append("\">");
        connection.deliverRawText(sb.toString());
        try {
            Element doc = reader.parseDocument().getRootElement();
            if ("db".equals(doc.getNamespacePrefix()) && "result".equals(doc.getName())) {
                String hostname = doc.attributeValue("from");
                String recipient = doc.attributeValue("to");
                Log.debug("ServerDialback: RS - Validating remote domain for incoming session from {} to {}", hostname, recipient);
                if (validateRemoteDomain(doc, streamID)) {
                    Log.debug("ServerDialback: RS - Validation of remote domain for incoming session from {} to {} was successful.", hostname, recipient);
                    // Create a server Session for the remote server
                    LocalIncomingServerSession session = sessionManager.createIncomingServerSession(connection, streamID, hostname);
                    // Add the validated domain as a valid domain
                    session.addValidatedDomain(hostname);
                    // Set the domain or subdomain of the local server used when
                    // validating the session
                    session.setLocalDomain(recipient);
                    return session;
                } else {
                    Log.debug("ServerDialback: RS - Validation of remote domain for incoming session from {} to {} was not successful.", hostname, recipient);
                    return null;
                }
            } else if ("db".equals(doc.getNamespacePrefix()) && "verify".equals(doc.getName())) {
                // When acting as an Authoritative Server the Receiving Server will send a
                // db:verify packet for verifying a key that was previously sent by this
                // server when acting as the Originating Server
                verifyReceivedKey(doc, connection);
                // Close the underlying connection
                connection.close();
                String verifyFROM = doc.attributeValue("from");
                String id = doc.attributeValue("id");
                Log.debug("ServerDialback: AS - Connection closed for host: " + verifyFROM + " id: " + id);
                return null;
            } else {
                Log.debug("ServerDialback: Received an invalid/unknown packet while trying to process an incoming session: {}", doc.asXML());
                // The remote server sent an invalid/unknown packet
                connection.deliverRawText(new StreamError(StreamError.Condition.invalid_xml).toXML());
                // Close the underlying connection
                connection.close();
                return null;
            }
        } catch (Exception e) {
            Log.error("An error occured while creating a server session", e);
            // Close the underlying connection
            connection.close();
            return null;
        }
    } else {
        Log.debug("ServerDialback: Received a stanza in an invalid namespace while trying to process an incoming session: {}", xpp.getNamespace("db"));
        connection.deliverRawText(new StreamError(StreamError.Condition.invalid_namespace).toXML());
        // Close the underlying connection
        connection.close();
        return null;
    }
}
Also used : LocalIncomingServerSession(org.jivesoftware.openfire.session.LocalIncomingServerSession) StreamError(org.xmpp.packet.StreamError) Element(org.dom4j.Element) XmlPullParser(org.xmlpull.v1.XmlPullParser) DocumentException(org.dom4j.DocumentException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

StreamError (org.xmpp.packet.StreamError)25 ClientSession (org.jivesoftware.openfire.session.ClientSession)7 IOException (java.io.IOException)6 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)6 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)6 JID (org.xmpp.packet.JID)5 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 DocumentException (org.dom4j.DocumentException)4 Element (org.dom4j.Element)4 LocalIncomingServerSession (org.jivesoftware.openfire.session.LocalIncomingServerSession)4 XmlPullParser (org.xmlpull.v1.XmlPullParser)4 StringprepException (gnu.inet.encoding.StringprepException)3 PacketException (org.jivesoftware.openfire.PacketException)3 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)3 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)3 Logger (org.slf4j.Logger)3 IQ (org.xmpp.packet.IQ)3 InputStreamReader (java.io.InputStreamReader)2 Socket (java.net.Socket)2 Connection (org.jivesoftware.openfire.Connection)2