ClientStanzaHandler.java 4.04 KB
Newer Older
1
/**
2
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
15 16
 */

17
package org.jivesoftware.openfire.net;
18 19

import org.dom4j.Element;
20 21 22
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.PacketRouter;
import org.jivesoftware.openfire.auth.UnauthorizedException;
23
import org.jivesoftware.openfire.session.ConnectionSettings;
24 25
import org.jivesoftware.openfire.session.LocalClientSession;
import org.jivesoftware.util.JiveGlobals;
26 27 28 29 30 31
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;

32
/** Handler of XML stanzas sent by clients connected directly to the server. Received packet will
33 34 35 36 37 38 39 40 41 42 43 44
 * have their FROM attribute overriden to avoid spoofing.<p>
 *
 * By default the hostname specified in the stream header sent by clients will not be validated.
 * When validated the TO attribute of the stream header has to match the server name or a valid
 * subdomain. If the value of the 'to' attribute is not valid then a host-unknown error
 * will be returned. To enable the validation set the system property
 * <b>xmpp.client.validate.host</b> to true.
 *
 * @author Gaston Dombiak
 */
public class ClientStanzaHandler extends StanzaHandler {

45 46 47 48 49
    public ClientStanzaHandler(PacketRouter router, Connection connection) {
        super(router, connection);
    }

    @Deprecated
50
    public ClientStanzaHandler(PacketRouter router, String serverName, Connection connection) {
51
        super(router, connection);
52 53 54 55 56 57 58 59 60
    }

    /**
     * Only packets of type Message, Presence and IQ can be processed by this class. Any other
     * type of packet is unknown and thus rejected generating the connection to be closed.
     *
     * @param doc the unknown DOM element that was received
     * @return always false.
     */
61 62
    @Override
	boolean processUnknowPacket(Element doc) {
63 64 65
        return false;
    }

66 67
    @Override
	String getNamespace() {
68 69 70
        return "jabber:client";
    }

71 72
    @Override
	boolean validateHost() {
73 74 75
        return JiveGlobals.getBooleanProperty("xmpp.client.validate.host",false);
    }

76 77
    @Override
	boolean validateJIDs() {
78 79 80
        return true;
    }

81 82
    @Override
	boolean createSession(String namespace, String serverName, XmlPullParser xpp, Connection connection)
83 84 85
            throws XmlPullParserException {
        if ("jabber:client".equals(namespace)) {
            // The connected client is a regular client so create a ClientSession
86
            session = LocalClientSession.createSession(serverName, xpp, connection);
87 88 89 90 91
            return true;
        }
        return false;
    }

92 93
    @Override
	protected void processIQ(IQ packet) throws UnauthorizedException {
94 95 96 97 98
        // Overwrite the FROM attribute to avoid spoofing
        packet.setFrom(session.getAddress());
        super.processIQ(packet);
    }

99 100
    @Override
	protected void processPresence(Presence packet) throws UnauthorizedException {
101 102 103 104 105
        // Overwrite the FROM attribute to avoid spoofing
        packet.setFrom(session.getAddress());
        super.processPresence(packet);
    }

106 107
    @Override
	protected void processMessage(Message packet) throws UnauthorizedException {
108 109 110 111
        // Overwrite the FROM attribute to avoid spoofing
        packet.setFrom(session.getAddress());
        super.processMessage(packet);
    }
112

113 114
    @Override
	void startTLS() throws Exception {
115
        connection.startTLS(false);
116
    }
117
}