Search in sources :

Example 6 with LocalIncomingServerSession

use of org.jivesoftware.openfire.session.LocalIncomingServerSession 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 7 with LocalIncomingServerSession

use of org.jivesoftware.openfire.session.LocalIncomingServerSession 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

LocalIncomingServerSession (org.jivesoftware.openfire.session.LocalIncomingServerSession)7 StreamError (org.xmpp.packet.StreamError)3 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)2 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)2 LocalConnectionMultiplexerSession (org.jivesoftware.openfire.session.LocalConnectionMultiplexerSession)2 LocalSession (org.jivesoftware.openfire.session.LocalSession)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Lock (java.util.concurrent.locks.Lock)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 DocumentException (org.dom4j.DocumentException)1 Element (org.dom4j.Element)1 HttpSession (org.jivesoftware.openfire.http.HttpSession)1 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)1 ClientSession (org.jivesoftware.openfire.session.ClientSession)1 ComponentSession (org.jivesoftware.openfire.session.ComponentSession)1 ConnectionMultiplexerSession (org.jivesoftware.openfire.session.ConnectionMultiplexerSession)1 IncomingServerSession (org.jivesoftware.openfire.session.IncomingServerSession)1 LocalComponentSession (org.jivesoftware.openfire.session.LocalComponentSession)1 LocalOutgoingServerSession (org.jivesoftware.openfire.session.LocalOutgoingServerSession)1