Search in sources :

Example 1 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserFalseWillReturnFalseForUnknownRemoteUsers.

@Test
public void isRegisteredUserFalseWillReturnFalseForUnknownRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        final Element childElement = iq.getChildElement();
        final IQ response = IQ.createResultIQ(iq);
        response.setChildElement(childElement.createCopy());
        response.setError(new PacketError(PacketError.Condition.item_not_found, PacketError.Condition.item_not_found.getDefaultType()));
        iqListener.get().receivedAnswer(response);
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null), false);
    assertThat(result, is(false));
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketError(org.xmpp.packet.PacketError) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserFalseWillReturnFalseForRemoteUsers.

@Test
public void isRegisteredUserFalseWillReturnFalseForRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        iqListener.get().receivedAnswer(Fixtures.iqFrom(USER_FOUND_RESULT));
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null), false);
    assertThat(result, is(false));
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserWillReturnFalseForUnknownRemoteUsers.

@Test
public void isRegisteredUserWillReturnFalseForUnknownRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        final IQ iq = invocationOnMock.getArgument(0);
        final Element childElement = iq.getChildElement();
        final IQ response = IQ.createResultIQ(iq);
        response.setChildElement(childElement.createCopy());
        response.setError(new PacketError(PacketError.Condition.item_not_found, PacketError.Condition.item_not_found.getDefaultType()));
        iqListener.get().receivedAnswer(response);
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null));
    assertThat(result, is(false));
    verify(iqRouter).route(any());
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketError(org.xmpp.packet.PacketError) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 4 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class UserManagerTest method isRegisteredUserWillReturnTrueForRemoteUsers.

@Test
public void isRegisteredUserWillReturnTrueForRemoteUsers() {
    final AtomicReference<IQResultListener> iqListener = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        final IQResultListener listener = invocationOnMock.getArgument(1);
        iqListener.set(listener);
        return null;
    }).when(iqRouter).addIQResultListener(any(), any(), anyLong());
    doAnswer(invocationOnMock -> {
        iqListener.get().receivedAnswer(Fixtures.iqFrom(USER_FOUND_RESULT));
        return null;
    }).when(iqRouter).route(any());
    final boolean result = userManager.isRegisteredUser(new JID(USER_ID, REMOTE_XMPP_DOMAIN, null));
    assertThat(result, is(true));
    verify(iqRouter).route(any());
}
Also used : IQResultListener(org.xmpp.component.IQResultListener) JID(org.xmpp.packet.JID) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 5 with IQResultListener

use of org.xmpp.component.IQResultListener in project Openfire by igniterealtime.

the class IQRouter method handle.

private void handle(IQ packet) {
    JID recipientJID = packet.getTo();
    // Check if the packet was sent to the server hostname
    if (recipientJID != null && recipientJID.getNode() == null && recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) {
        Element childElement = packet.getChildElement();
        if (childElement != null && childElement.element("addresses") != null) {
            // Packet includes multicast processing instructions. Ask the multicastRouter
            // to route this packet
            multicastRouter.route(packet);
            return;
        }
    }
    if (packet.getID() != null && (IQ.Type.result == packet.getType() || IQ.Type.error == packet.getType())) {
        // The server got an answer to an IQ packet that was sent from the server
        // If there's a listener for this result at all, then it's likely that that listener had been registered
        // on this cluster node. For efficiency, try the local cluster node before triggering tasks in the rest
        // of the cluster.
        IQResultListener iqResultListener = resultListeners.remove(packet.getID());
        if (iqResultListener != null) {
            resultTimeout.remove(packet.getID());
            resultPending.remove(packet.getID());
            try {
                iqResultListener.receivedAnswer(packet);
            } catch (Exception e) {
                Log.error("Error processing answer of remote entity. Answer: " + packet.toXML(), e);
            }
            return;
        } else if (ClusterManager.isClusteringStarted()) {
            // Only do lookups in the cluster, after it's determined that the local node cannot process the result.
            // remove it, to reduce the risk of this packet being sent back and forth.
            final NodeID nodeID = resultPending.remove(packet.getID());
            if (nodeID != null && !XMPPServer.getInstance().getNodeID().equals(nodeID)) {
                CacheFactory.doClusterTask(new IQResultListenerTask(packet), nodeID.toByteArray());
                return;
            }
        }
    }
    try {
        // they are not allowed in s2s traffic.
        if (packet.getFrom() == null && !XMPPServer.getInstance().isLocal(recipientJID)) {
            // Stanzas that originate from clients _always_ have a 'from' attribute (as that attribute value is set/
            // overwritten by Openfire upon receiving the stanza, to prevent abuse where a user tries to impersonate
            // someone else). That means that, if we're processing a stanza without a 'from' attribute, that the
            // stanza is very likely to originate from Openfire's code. If we have code that generates a stanza
            // without a 'from' address but addressed to a remote domain, this simply is a bug that we should very
            // verbosely warn about.
            Log.error("Unable to process a stanza that has no 'from' attribute, addressed to a remote entity. Stanza is being dropped: {}", packet.toXML());
            return;
        }
        if (recipientJID != null && (routingTable.hasComponentRoute(recipientJID) || (packet.getFrom() != null && routingTable.hasServerRoute(new DomainPair(packet.getFrom().getDomain(), recipientJID.getDomain()))))) {
            // A component/service/remote server was found that can handle the Packet
            routingTable.routePacket(recipientJID, packet, false);
            return;
        }
        if (isLocalServer(recipientJID)) {
            // Let the server handle the Packet
            Element childElement = packet.getChildElement();
            String namespace = null;
            if (childElement != null) {
                namespace = childElement.getNamespaceURI();
            }
            if (namespace == null) {
                if (packet.getType() != IQ.Type.result && packet.getType() != IQ.Type.error) {
                    // Do nothing. We can't handle queries outside of a valid namespace
                    Log.warn("Unknown packet " + packet.toXML());
                }
            } else {
                // Check if communication to local users is allowed
                if (recipientJID != null && userManager.isRegisteredUser(recipientJID, false)) {
                    PrivacyList list = PrivacyListManager.getInstance().getDefaultPrivacyList(recipientJID.getNode());
                    if (list != null && list.shouldBlockPacket(packet)) {
                        // Communication is blocked
                        if (IQ.Type.set == packet.getType() || IQ.Type.get == packet.getType()) {
                            // Answer that the service is unavailable
                            sendErrorPacket(packet, PacketError.Condition.service_unavailable);
                        }
                        return;
                    }
                }
                IQHandler handler = getHandler(namespace);
                if (handler == null) {
                    if (recipientJID == null) {
                        // Answer an error since the server can't handle the requested namespace
                        sendErrorPacket(packet, PacketError.Condition.service_unavailable);
                    } else if (recipientJID.getNode() == null || "".equals(recipientJID.getNode())) {
                        // Answer an error if JID is of the form <domain>
                        sendErrorPacket(packet, PacketError.Condition.feature_not_implemented);
                    } else {
                        // JID is of the form <node@domain>
                        // Answer an error since the server can't handle packets sent to a node
                        sendErrorPacket(packet, PacketError.Condition.service_unavailable);
                    }
                } else {
                    handler.process(packet);
                }
            }
        } else {
            // If the user account identified by the 'to' attribute does not exist, how the stanza is processed depends on the stanza type.
            if (recipientJID != null && recipientJID.getNode() != null && serverName.equals(recipientJID.getDomain()) && !userManager.isRegisteredUser(recipientJID, false) && sessionManager.getSession(recipientJID) == null && (IQ.Type.set == packet.getType() || IQ.Type.get == packet.getType())) {
                // For an IQ stanza, the server MUST return a <service-unavailable/> stanza error to the sender.
                sendErrorPacket(packet, PacketError.Condition.service_unavailable);
                return;
            }
            ClientSession session = sessionManager.getSession(packet.getFrom());
            boolean isAcceptable = true;
            if (session instanceof LocalClientSession) {
                // Check if we could process IQ stanzas from the recipient.
                // If not, return a not-acceptable error as per XEP-0016:
                // If the user attempts to send an outbound stanza to a contact and that stanza type is blocked, the user's server MUST NOT route the stanza to the contact but instead MUST return a <not-acceptable/> error
                IQ dummyIQ = packet.createCopy();
                dummyIQ.setFrom(packet.getTo());
                dummyIQ.setTo(packet.getFrom());
                if (!((LocalClientSession) session).canProcess(dummyIQ)) {
                    packet.setTo(session.getAddress());
                    packet.setFrom((JID) null);
                    packet.setError(PacketError.Condition.not_acceptable);
                    session.process(packet);
                    isAcceptable = false;
                }
            }
            if (isAcceptable) {
                // JID is of the form <node@domain/resource> or belongs to a remote server
                // or to an uninstalled component
                routingTable.routePacket(recipientJID, packet, false);
            }
        }
    } catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
        Session session = sessionManager.getSession(packet.getFrom());
        if (session != null) {
            IQ reply = IQ.createResultIQ(packet);
            reply.setError(PacketError.Condition.internal_server_error);
            session.process(reply);
        }
    }
}
Also used : IQHandler(org.jivesoftware.openfire.handler.IQHandler) Element(org.dom4j.Element) PrivacyList(org.jivesoftware.openfire.privacy.PrivacyList) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) IQResultListener(org.xmpp.component.IQResultListener) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) DomainPair(org.jivesoftware.openfire.session.DomainPair) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession) NodeID(org.jivesoftware.openfire.cluster.NodeID) IQResultListenerTask(org.jivesoftware.openfire.cluster.IQResultListenerTask) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession) Session(org.jivesoftware.openfire.session.Session)

Aggregations

IQResultListener (org.xmpp.component.IQResultListener)10 JID (org.xmpp.packet.JID)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Test (org.junit.Test)8 IQ (org.xmpp.packet.IQ)6 Element (org.dom4j.Element)5 PacketError (org.xmpp.packet.PacketError)3 Iterator (java.util.Iterator)1 Semaphore (java.util.concurrent.Semaphore)1 IQRouter (org.jivesoftware.openfire.IQRouter)1 IQResultListenerTask (org.jivesoftware.openfire.cluster.IQResultListenerTask)1 NodeID (org.jivesoftware.openfire.cluster.NodeID)1 IQHandler (org.jivesoftware.openfire.handler.IQHandler)1 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)1 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)1 ClientSession (org.jivesoftware.openfire.session.ClientSession)1 DomainPair (org.jivesoftware.openfire.session.DomainPair)1 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)1 Session (org.jivesoftware.openfire.session.Session)1