Search in sources :

Example 1 with StreamError

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

the class SessionController method removeUserSessions.

/**
	 * Removes the user sessions.
	 *
	 * @param username the username
	 * @throws ServiceException the service exception
	 */
public void removeUserSessions(String username) throws ServiceException {
    final StreamError error = new StreamError(StreamError.Condition.not_authorized);
    for (ClientSession session : SessionManager.getInstance().getSessions(username)) {
        session.deliverRawText(error.toXML());
        session.close();
    }
}
Also used : StreamError(org.xmpp.packet.StreamError) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession)

Example 2 with StreamError

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

the class JustMarriedController method deleteUser.

/**
	 * Delete user.
	 *
	 * @param oldUser
	 *            the old user
	 */
private static void deleteUser(User oldUser) {
    UserManager.getInstance().deleteUser(oldUser);
    LockOutManager.getInstance().enableAccount(oldUser.getUsername());
    final StreamError error = new StreamError(StreamError.Condition.not_authorized);
    for (ClientSession sess : SessionManager.getInstance().getSessions(oldUser.getUsername())) {
        sess.deliverRawText(error.toXML());
        sess.close();
    }
}
Also used : StreamError(org.xmpp.packet.StreamError) ClientSession(org.jivesoftware.openfire.session.ClientSession)

Example 3 with StreamError

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

the class SparkManager method closeSession.

/**
     * Sends an unsupported version error and name of client the user attempted to connect with.
     *
     * @param session    the users current session.
     * @param clientName the name of the client they were connecting with.
     */
private void closeSession(final Session session, String clientName) {
    // Increase the number of logins not allowed by 1.
    disconnects.incrementAndGet();
    Log.debug("Closed connection to client attempting to connect from " + clientName);
    // Send message information user.
    final Message message = new Message();
    message.setFrom(serviceName + "." + componentManager.getServerName());
    message.setTo(session.getAddress());
    message.setBody("You are using an invalid client, and therefore will be disconnected. " + "Please ask your system administrator for client choices.");
    // Send Message
    sendPacket(message);
    // Disconnect user after 5 seconds.
    taskEngine.schedule(new TimerTask() {

        @Override
        public void run() {
            // Include the not-authorized error in the response
            StreamError error = new StreamError(StreamError.Condition.policy_violation);
            session.deliverRawText(error.toXML());
            // Close the underlying connection
            session.close();
        }
    }, 5000);
}
Also used : StreamError(org.xmpp.packet.StreamError) Message(org.xmpp.packet.Message) TimerTask(java.util.TimerTask)

Example 4 with StreamError

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

the class ServerDialback method createOutgoingSession.

/**
     * Creates a new connection from the Originating Server to the Receiving Server for
     * authenticating the specified domain.
     *
     * @param localDomain domain of the Originating Server to authenticate with the Receiving Server.
     * @param remoteDomain IP address or hostname of the Receiving Server.
     * @param port port of the Receiving Server.
     * @return an OutgoingServerSession if the domain was authenticated or <tt>null</tt> if none.
     */
public LocalOutgoingServerSession createOutgoingSession(String localDomain, String remoteDomain, int port) {
    final Logger log = LoggerFactory.getLogger(Log.getName() + "[Acting as Originating Server: Create Outgoing Session from: " + localDomain + " to RS at: " + remoteDomain + " (port: " + port + ")]");
    log.debug("Creating new outgoing session...");
    String hostname = null;
    int realPort = port;
    try {
        // Establish a TCP connection to the Receiving Server
        final Socket socket = SocketUtil.createSocketToXmppDomain(remoteDomain, port);
        if (socket == null) {
            log.info("Unable to create new outgoing session: Cannot create a plain socket connection with any applicable remote host.");
            return null;
        }
        connection = new SocketConnection(XMPPServer.getInstance().getPacketDeliverer(), socket, false);
        log.debug("Send the stream header and wait for response...");
        StringBuilder stream = new StringBuilder();
        stream.append("<stream:stream");
        stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\"");
        stream.append(" xmlns=\"jabber:server\"");
        stream.append(" to=\"").append(remoteDomain).append("\"");
        stream.append(" from=\"").append(localDomain).append("\"");
        stream.append(" xmlns:db=\"jabber:server:dialback\"");
        stream.append(">");
        connection.deliverRawText(stream.toString());
        // Set a read timeout (of 5 seconds) so we don't keep waiting forever
        int soTimeout = socket.getSoTimeout();
        socket.setSoTimeout(RemoteServerManager.getSocketTimeout());
        XMPPPacketReader reader = new XMPPPacketReader();
        reader.setXPPFactory(FACTORY);
        reader.getXPPParser().setInput(new InputStreamReader(ServerTrafficCounter.wrapInputStream(socket.getInputStream()), CHARSET));
        // Get the answer from the Receiving Server
        XmlPullParser xpp = reader.getXPPParser();
        for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG; ) {
            eventType = xpp.next();
        }
        log.debug("Got a response. Check if the remote server supports dialback...");
        if ("jabber:server:dialback".equals(xpp.getNamespace("db"))) {
            log.debug("Dialback seems to be supported by the remote server.");
            // Restore default timeout
            socket.setSoTimeout(soTimeout);
            String id = xpp.getAttributeValue("", "id");
            OutgoingServerSocketReader socketReader = new OutgoingServerSocketReader(reader);
            if (authenticateDomain(socketReader, localDomain, remoteDomain, id)) {
                log.debug("Successfully authenticated the connection with dialback.");
                // Domain was validated so create a new OutgoingServerSession
                StreamID streamID = BasicStreamIDFactory.createStreamID(id);
                LocalOutgoingServerSession session = new LocalOutgoingServerSession(localDomain, connection, socketReader, streamID);
                connection.init(session);
                // Set the hostname as the address of the session
                session.setAddress(new JID(null, remoteDomain, null));
                log.debug("Successfully created new outgoing session!");
                return session;
            } else {
                log.debug("Failed to authenticate the connection with dialback.");
                // Close the connection
                connection.close();
            }
        } else {
            log.debug("Error! Invalid namespace in packet: '{}'. Closing connection.", xpp.getText());
            // Send an invalid-namespace stream error condition in the response
            connection.deliverRawText(new StreamError(StreamError.Condition.invalid_namespace).toXML());
            // Close the connection
            connection.close();
        }
    } catch (Exception e) {
        log.error("An exception occurred while creating outgoing session to remote server:", e);
        // Close the connection
        if (connection != null) {
            connection.close();
        }
    }
    log.warn("Unable to create a new outgoing session");
    return null;
}
Also used : XMPPPacketReader(org.dom4j.io.XMPPPacketReader) InputStreamReader(java.io.InputStreamReader) JID(org.xmpp.packet.JID) XmlPullParser(org.xmlpull.v1.XmlPullParser) Logger(org.slf4j.Logger) DocumentException(org.dom4j.DocumentException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StreamError(org.xmpp.packet.StreamError) LocalOutgoingServerSession(org.jivesoftware.openfire.session.LocalOutgoingServerSession) Socket(java.net.Socket)

Example 5 with StreamError

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

the class XmppWebSocket method processStanza.

// helper/utility methods
/*
	 * Process stream headers/footers and authentication stanzas locally;
	 * otherwise delegate stanza handling to the session packet router.
	 */
private void processStanza(Element stanza) {
    try {
        String tag = stanza.getName();
        if (STREAM_FOOTER.equals(tag)) {
            closeStream(null);
        } else if ("auth".equals(tag)) {
            // User is trying to authenticate using SASL
            startedSASL = true;
            // Process authentication stanza
            xmppSession.incrementClientPacketCount();
            saslStatus = SASLAuthentication.handle(xmppSession, stanza);
        } else if (startedSASL && "response".equals(tag) || "abort".equals(tag)) {
            // User is responding to SASL challenge. Process response
            xmppSession.incrementClientPacketCount();
            saslStatus = SASLAuthentication.handle(xmppSession, stanza);
        } else if (STREAM_HEADER.equals(tag)) {
            // restart the stream
            openStream(stanza.attributeValue(QName.get("lang", XMLConstants.XML_NS_URI), "en"), stanza.attributeValue("from"));
            configureStream();
        } else if (Status.authenticated.equals(saslStatus)) {
            if (router == null) {
                if (isStreamManagementAvailable()) {
                    router = new StreamManagementPacketRouter(xmppSession);
                } else {
                    // fall back for older Openfire installations
                    router = new SessionPacketRouter(xmppSession);
                }
            }
            router.route(stanza);
        } else {
            // require authentication
            Log.warn("Not authorized: " + stanza.asXML());
            sendPacketError(stanza, PacketError.Condition.not_authorized);
        }
    } catch (UnknownStanzaException use) {
        Log.warn("Received invalid stanza: " + stanza.asXML());
        sendPacketError(stanza, PacketError.Condition.bad_request);
    } catch (Exception ex) {
        Log.error("Failed to process incoming stanza: " + stanza.asXML(), ex);
        closeStream(new StreamError(StreamError.Condition.internal_server_error));
    }
}
Also used : StreamError(org.xmpp.packet.StreamError) SessionPacketRouter(org.jivesoftware.openfire.SessionPacketRouter) IOException(java.io.IOException) UnknownStanzaException(org.jivesoftware.openfire.multiplex.UnknownStanzaException) UnknownStanzaException(org.jivesoftware.openfire.multiplex.UnknownStanzaException)

Aggregations

StreamError (org.xmpp.packet.StreamError)26 ClientSession (org.jivesoftware.openfire.session.ClientSession)7 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)7 JID (org.xmpp.packet.JID)6 IOException (java.io.IOException)5 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)5 DocumentException (org.dom4j.DocumentException)5 Element (org.dom4j.Element)5 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)5 XmlPullParser (org.xmlpull.v1.XmlPullParser)5 Logger (org.slf4j.Logger)4 Socket (java.net.Socket)3 SSLException (javax.net.ssl.SSLException)3 PacketException (org.jivesoftware.openfire.PacketException)3 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)3 IQ (org.xmpp.packet.IQ)3 StringprepException (gnu.inet.encoding.StringprepException)2 XMPPPacketReader (org.dom4j.io.XMPPPacketReader)2 AuthToken (org.jivesoftware.openfire.auth.AuthToken)2 UnknownStanzaException (org.jivesoftware.openfire.multiplex.UnknownStanzaException)2