Commit 9de18081 authored by guus's avatar guus

Make sure that the data is processed as it is provided by the client. Don't...

Make sure that the data is processed as it is provided by the client. Don't trim the data, as it will lead to very unexpected behavior later in the session (where that data is no longer trimmed). If data is invalid (e.g: spaces before or after the username), return an error. (JM-1481)

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@10840 b35dd754-fafc-0310-a699-88a17e54d16e
parent 5da9df32
/** /**
* $RCSfile$ * $RCSfile$
* $Revision: 2747 $ * $Revision: 2747 $
* $Date: 2005-08-31 15:12:28 -0300 (Wed, 31 Aug 2005) $ * $Date: 2005-08-31 15:12:28 -0300 (Wed, 31 Aug 2005) $
* *
* Copyright (C) 2005-2008 Jive Software. All rights reserved. * Copyright (C) 2005-2008 Jive Software. All rights reserved.
* *
* This software is published under the terms of the GNU Public License (GPL), * This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license * a copy of which is included in this distribution, or a commercial license
* agreement with Jive. * agreement with Jive.
*/ */
package org.jivesoftware.openfire.handler; package org.jivesoftware.openfire.handler;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.QName; import org.dom4j.QName;
import org.jivesoftware.openfire.*; import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.auth.*; import org.jivesoftware.openfire.auth.*;
import org.jivesoftware.openfire.event.SessionEventDispatcher; import org.jivesoftware.openfire.event.SessionEventDispatcher;
import org.jivesoftware.openfire.session.ClientSession; import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.session.LocalClientSession; import org.jivesoftware.openfire.session.LocalClientSession;
import org.jivesoftware.openfire.session.Session; import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.user.UserManager; import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException; import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.stringprep.StringprepException; import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.stringprep.StringprepException;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log; import org.jivesoftware.util.LocaleUtils;
import org.xmpp.packet.IQ; import org.jivesoftware.util.Log;
import org.xmpp.packet.JID; import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError; import org.xmpp.packet.JID;
import org.xmpp.packet.StreamError; import org.xmpp.packet.PacketError;
import org.xmpp.packet.StreamError;
import java.util.ArrayList;
import java.util.List; import java.util.ArrayList;
import java.net.UnknownHostException; import java.util.List;
import java.net.UnknownHostException;
/**
* Implements the TYPE_IQ jabber:iq:auth protocol (plain only). Clients /**
* use this protocol to authenticate with the server. A 'get' query * Implements the TYPE_IQ jabber:iq:auth protocol (plain only). Clients
* runs an authentication probe with a given user name. Return the * use this protocol to authenticate with the server. A 'get' query
* authentication form or an error indicating the user is not * runs an authentication probe with a given user name. Return the
* registered on the server.<p> * authentication form or an error indicating the user is not
* * registered on the server.<p>
* A 'set' query authenticates with information given in the *
* authentication form. An authenticated session may reset their * A 'set' query authenticates with information given in the
* authentication information using a 'set' query. * authentication form. An authenticated session may reset their
* * authentication information using a 'set' query.
* <h2>Assumptions</h2> *
* This handler assumes that the request is addressed to the server. * <h2>Assumptions</h2>
* An appropriate TYPE_IQ tag matcher should be placed in front of this * This handler assumes that the request is addressed to the server.
* one to route TYPE_IQ requests not addressed to the server to * An appropriate TYPE_IQ tag matcher should be placed in front of this
* another channel (probably for direct delivery to the recipient). * one to route TYPE_IQ requests not addressed to the server to
* * another channel (probably for direct delivery to the recipient).
* @author Iain Shigeoka *
*/ * @author Iain Shigeoka
public class IQAuthHandler extends IQHandler implements IQAuthInfo { */
public class IQAuthHandler extends IQHandler implements IQAuthInfo {
private boolean anonymousAllowed;
private boolean anonymousAllowed;
private Element probeResponse;
private IQHandlerInfo info; private Element probeResponse;
private IQHandlerInfo info;
private String serverName;
private UserManager userManager; private String serverName;
private RoutingTable routingTable; private UserManager userManager;
private RoutingTable routingTable;
/**
* Clients are not authenticated when accessing this handler. /**
*/ * Clients are not authenticated when accessing this handler.
public IQAuthHandler() { */
super("XMPP Authentication handler"); public IQAuthHandler() {
info = new IQHandlerInfo("query", "jabber:iq:auth"); super("XMPP Authentication handler");
info = new IQHandlerInfo("query", "jabber:iq:auth");
probeResponse = DocumentHelper.createElement(QName.get("query", "jabber:iq:auth"));
probeResponse.addElement("username"); probeResponse = DocumentHelper.createElement(QName.get("query", "jabber:iq:auth"));
if (AuthFactory.isPlainSupported()) { probeResponse.addElement("username");
probeResponse.addElement("password"); if (AuthFactory.isPlainSupported()) {
} probeResponse.addElement("password");
if (AuthFactory.isDigestSupported()) { }
probeResponse.addElement("digest"); if (AuthFactory.isDigestSupported()) {
} probeResponse.addElement("digest");
probeResponse.addElement("resource"); }
anonymousAllowed = JiveGlobals.getBooleanProperty("xmpp.auth.anonymous"); probeResponse.addElement("resource");
} anonymousAllowed = JiveGlobals.getBooleanProperty("xmpp.auth.anonymous");
}
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
LocalClientSession session = (LocalClientSession) sessionManager.getSession(packet.getFrom()); public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
// If no session was found then answer an error (if possible) LocalClientSession session = (LocalClientSession) sessionManager.getSession(packet.getFrom());
if (session == null) { // If no session was found then answer an error (if possible)
Log.error("Error during authentication. Session not found in " + if (session == null) {
sessionManager.getPreAuthenticatedKeys() + Log.error("Error during authentication. Session not found in " +
" for key " + sessionManager.getPreAuthenticatedKeys() +
packet.getFrom()); " for key " +
// This error packet will probably won't make it through packet.getFrom());
IQ reply = IQ.createResultIQ(packet); // This error packet will probably won't make it through
reply.setChildElement(packet.getChildElement().createCopy()); IQ reply = IQ.createResultIQ(packet);
reply.setError(PacketError.Condition.internal_server_error); reply.setChildElement(packet.getChildElement().createCopy());
return reply; reply.setError(PacketError.Condition.internal_server_error);
} return reply;
IQ response; }
boolean resourceBound = false; IQ response;
if (JiveGlobals.getBooleanProperty("xmpp.auth.iqauth",true)) { boolean resourceBound = false;
try { if (JiveGlobals.getBooleanProperty("xmpp.auth.iqauth",true)) {
Element iq = packet.getElement(); try {
Element query = iq.element("query"); Element iq = packet.getElement();
Element queryResponse = probeResponse.createCopy(); Element query = iq.element("query");
if (IQ.Type.get == packet.getType()) { Element queryResponse = probeResponse.createCopy();
String username = query.elementTextTrim("username"); if (IQ.Type.get == packet.getType()) {
if (username != null) { String username = query.elementText("username");
queryResponse.element("username").setText(username); if (username != null) {
} queryResponse.element("username").setText(username);
response = IQ.createResultIQ(packet); }
response.setChildElement(queryResponse); response = IQ.createResultIQ(packet);
// This is a workaround. Since we don't want to have an incorrect TO attribute response.setChildElement(queryResponse);
// value we need to clean up the TO attribute and send directly the response. // This is a workaround. Since we don't want to have an incorrect TO attribute
// The TO attribute will contain an incorrect value since we are setting a fake // value we need to clean up the TO attribute and send directly the response.
// JID until the user actually authenticates with the server. // The TO attribute will contain an incorrect value since we are setting a fake
if (session.getStatus() != Session.STATUS_AUTHENTICATED) { // JID until the user actually authenticates with the server.
response.setTo((JID)null); if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
} response.setTo((JID)null);
} }
// Otherwise set query }
else { // Otherwise set query
if (query.elements().isEmpty()) { else {
// Anonymous authentication if (query.elements().isEmpty()) {
response = anonymousLogin(session, packet); // Anonymous authentication
resourceBound = session.getStatus() == Session.STATUS_AUTHENTICATED; response = anonymousLogin(session, packet);
} resourceBound = session.getStatus() == Session.STATUS_AUTHENTICATED;
else { }
String username = query.elementTextTrim("username"); else {
// Login authentication String username = query.elementText("username");
String password = query.elementTextTrim("password"); // Login authentication
String digest = null; String password = query.elementText("password");
if (query.element("digest") != null) { String digest = null;
digest = query.elementTextTrim("digest").toLowerCase(); if (query.element("digest") != null) {
} digest = query.elementText("digest").toLowerCase();
}
// If we're already logged in, this is a password reset
if (session.getStatus() == Session.STATUS_AUTHENTICATED) { // If we're already logged in, this is a password reset
response = passwordReset(password, packet, username, session); if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
} response = passwordReset(password, packet, username, session);
else { }
// it is an auth attempt else {
response = login(username, query, packet, password, session, digest); // it is an auth attempt
resourceBound = session.getStatus() == Session.STATUS_AUTHENTICATED; response = login(username, query, packet, password, session, digest);
} resourceBound = session.getStatus() == Session.STATUS_AUTHENTICATED;
} }
} }
} }
catch (UserNotFoundException e) { }
response = IQ.createResultIQ(packet); catch (UserNotFoundException e) {
response.setChildElement(packet.getChildElement().createCopy()); response = IQ.createResultIQ(packet);
response.setError(PacketError.Condition.not_authorized); response.setChildElement(packet.getChildElement().createCopy());
} response.setError(PacketError.Condition.not_authorized);
catch (UnauthorizedException e) { }
response = IQ.createResultIQ(packet); catch (UnauthorizedException e) {
response.setChildElement(packet.getChildElement().createCopy()); response = IQ.createResultIQ(packet);
response.setError(PacketError.Condition.not_authorized); response.setChildElement(packet.getChildElement().createCopy());
} catch (ConnectionException e) { response.setError(PacketError.Condition.not_authorized);
response = IQ.createResultIQ(packet); } catch (ConnectionException e) {
response.setChildElement(packet.getChildElement().createCopy()); response = IQ.createResultIQ(packet);
response.setError(PacketError.Condition.internal_server_error); response.setChildElement(packet.getChildElement().createCopy());
} catch (InternalUnauthenticatedException e) { response.setError(PacketError.Condition.internal_server_error);
response = IQ.createResultIQ(packet); } catch (InternalUnauthenticatedException e) {
response.setChildElement(packet.getChildElement().createCopy()); response = IQ.createResultIQ(packet);
response.setError(PacketError.Condition.internal_server_error); response.setChildElement(packet.getChildElement().createCopy());
} response.setError(PacketError.Condition.internal_server_error);
} }
else { }
response = IQ.createResultIQ(packet); else {
response.setChildElement(packet.getChildElement().createCopy()); response = IQ.createResultIQ(packet);
response.setError(PacketError.Condition.not_authorized); response.setChildElement(packet.getChildElement().createCopy());
} response.setError(PacketError.Condition.not_authorized);
// Send the response directly since we want to be sure that we are sending it back }
// to the correct session. Any other session of the same user but with different // Send the response directly since we want to be sure that we are sending it back
// resource is incorrect. // to the correct session. Any other session of the same user but with different
session.process(response); // resource is incorrect.
if (resourceBound) { session.process(response);
// After the client has been informed, inform all listeners as well. if (resourceBound) {
SessionEventDispatcher.dispatchEvent(session, SessionEventDispatcher.EventType.resource_bound); // After the client has been informed, inform all listeners as well.
} SessionEventDispatcher.dispatchEvent(session, SessionEventDispatcher.EventType.resource_bound);
return null; }
} return null;
}
private IQ login(String username, Element iq, IQ packet, String password, LocalClientSession session, String digest)
throws UnauthorizedException, UserNotFoundException, ConnectionException, InternalUnauthenticatedException { private IQ login(String username, Element iq, IQ packet, String password, LocalClientSession session, String digest)
// Verify that specified resource is not violating any string prep rule throws UnauthorizedException, UserNotFoundException, ConnectionException, InternalUnauthenticatedException {
String resource = iq.elementTextTrim("resource"); // Verify the validity of the username
if (resource != null) { try {
try { Stringprep.nodeprep(username);
resource = JID.resourceprep(resource); } catch (StringprepException e) {
} throw new UnauthorizedException("Invalid username: " + username, e);
catch (StringprepException e) { }
throw new IllegalArgumentException("Invalid resource: " + resource);
} // Verify that specified resource is not violating any string prep rule
} String resource = iq.elementText("resource");
else { if (resource != null) {
// Answer a not_acceptable error since a resource was not supplied try {
IQ response = IQ.createResultIQ(packet); resource = JID.resourceprep(resource);
response.setChildElement(packet.getChildElement().createCopy()); }
response.setError(PacketError.Condition.not_acceptable); catch (StringprepException e) {
return response; throw new UnauthorizedException("Invalid resource: " + resource, e);
} }
if (! JiveGlobals.getBooleanProperty("xmpp.auth.iqauth",true)) { }
throw new UnauthorizedException(); else {
} // Answer a not_acceptable error since a resource was not supplied
username = username.toLowerCase(); IQ response = IQ.createResultIQ(packet);
// Verify that supplied username and password are correct (i.e. user authentication was successful) response.setChildElement(packet.getChildElement().createCopy());
AuthToken token = null; response.setError(PacketError.Condition.not_acceptable);
if (password != null && AuthFactory.isPlainSupported()) { return response;
token = AuthFactory.authenticate(username, password); }
} if (! JiveGlobals.getBooleanProperty("xmpp.auth.iqauth",true)) {
else if (digest != null && AuthFactory.isDigestSupported()) { throw new UnauthorizedException();
token = AuthFactory.authenticate(username, session.getStreamID().toString(), }
digest); username = username.toLowerCase();
} // Verify that supplied username and password are correct (i.e. user authentication was successful)
if (token == null) { AuthToken token = null;
throw new UnauthorizedException(); if (password != null && AuthFactory.isPlainSupported()) {
} token = AuthFactory.authenticate(username, password);
// Verify if there is a resource conflict between new resource and existing one. }
// Check if a session already exists with the requested full JID and verify if else if (digest != null && AuthFactory.isDigestSupported()) {
// we should kick it off or refuse the new connection token = AuthFactory.authenticate(username, session.getStreamID().toString(),
ClientSession oldSession = routingTable.getClientRoute(new JID(username, serverName, resource, true)); digest);
if (oldSession != null) { }
try { if (token == null) {
int conflictLimit = sessionManager.getConflictKickLimit(); throw new UnauthorizedException();
if (conflictLimit == SessionManager.NEVER_KICK) { }
IQ response = IQ.createResultIQ(packet); // Verify if there is a resource conflict between new resource and existing one.
response.setChildElement(packet.getChildElement().createCopy()); // Check if a session already exists with the requested full JID and verify if
response.setError(PacketError.Condition.forbidden); // we should kick it off or refuse the new connection
return response; ClientSession oldSession = routingTable.getClientRoute(new JID(username, serverName, resource, true));
} if (oldSession != null) {
try {
int conflictCount = oldSession.incrementConflictCount(); int conflictLimit = sessionManager.getConflictKickLimit();
if (conflictCount > conflictLimit) { if (conflictLimit == SessionManager.NEVER_KICK) {
// Send a stream:error before closing the old connection IQ response = IQ.createResultIQ(packet);
StreamError error = new StreamError(StreamError.Condition.conflict); response.setChildElement(packet.getChildElement().createCopy());
oldSession.deliverRawText(error.toXML()); response.setError(PacketError.Condition.forbidden);
oldSession.close(); return response;
} }
else {
IQ response = IQ.createResultIQ(packet); int conflictCount = oldSession.incrementConflictCount();
response.setChildElement(packet.getChildElement().createCopy()); if (conflictCount > conflictLimit) {
response.setError(PacketError.Condition.forbidden); // Send a stream:error before closing the old connection
return response; StreamError error = new StreamError(StreamError.Condition.conflict);
} oldSession.deliverRawText(error.toXML());
} oldSession.close();
catch (Exception e) { }
Log.error("Error during login", e); else {
} IQ response = IQ.createResultIQ(packet);
} response.setChildElement(packet.getChildElement().createCopy());
// Set that the new session has been authenticated successfully response.setError(PacketError.Condition.forbidden);
session.setAuthToken(token, resource); return response;
packet.setFrom(session.getAddress()); }
return IQ.createResultIQ(packet); }
} catch (Exception e) {
Log.error("Error during login", e);
private IQ passwordReset(String password, IQ packet, String username, Session session) }
throws UnauthorizedException }
{ // Set that the new session has been authenticated successfully
IQ response; session.setAuthToken(token, resource);
if (password == null || password.length() == 0) { packet.setFrom(session.getAddress());
throw new UnauthorizedException(); return IQ.createResultIQ(packet);
} }
else {
try { private IQ passwordReset(String password, IQ packet, String username, Session session)
userManager.getUser(username).setPassword(password); throws UnauthorizedException
response = IQ.createResultIQ(packet); {
List<String> params = new ArrayList<String>(); IQ response;
params.add(username); if (password == null || password.length() == 0) {
params.add(session.toString()); throw new UnauthorizedException();
Log.info(LocaleUtils.getLocalizedString("admin.password.update", params)); }
} else {
catch (UserNotFoundException e) { try {
throw new UnauthorizedException(); userManager.getUser(username).setPassword(password);
} response = IQ.createResultIQ(packet);
} List<String> params = new ArrayList<String>();
return response; params.add(username);
} params.add(session.toString());
Log.info(LocaleUtils.getLocalizedString("admin.password.update", params));
private IQ anonymousLogin(LocalClientSession session, IQ packet) { }
IQ response = IQ.createResultIQ(packet); catch (UserNotFoundException e) {
if (anonymousAllowed) { throw new UnauthorizedException();
// Verify that client can connect from his IP address }
boolean forbidAccess = false; }
try { return response;
String hostAddress = session.getConnection().getHostAddress(); }
if (!LocalClientSession.getAllowedAnonymIPs().isEmpty() &&
!LocalClientSession.getAllowedAnonymIPs().containsKey(hostAddress)) { private IQ anonymousLogin(LocalClientSession session, IQ packet) {
byte[] address = session.getConnection().getAddress(); IQ response = IQ.createResultIQ(packet);
String range1 = (address[0] & 0xff) + "." + (address[1] & 0xff) + "." + if (anonymousAllowed) {
(address[2] & 0xff) + // Verify that client can connect from his IP address
".*"; boolean forbidAccess = false;
String range2 = (address[0] & 0xff) + "." + (address[1] & 0xff) + ".*.*"; try {
String range3 = (address[0] & 0xff) + ".*.*.*"; String hostAddress = session.getConnection().getHostAddress();
if (!LocalClientSession.getAllowedAnonymIPs().containsKey(range1) && if (!LocalClientSession.getAllowedAnonymIPs().isEmpty() &&
!LocalClientSession.getAllowedAnonymIPs().containsKey(range2) && !LocalClientSession.getAllowedAnonymIPs().containsKey(hostAddress)) {
!LocalClientSession.getAllowedAnonymIPs().containsKey(range3)) { byte[] address = session.getConnection().getAddress();
forbidAccess = true; String range1 = (address[0] & 0xff) + "." + (address[1] & 0xff) + "." +
} (address[2] & 0xff) +
} ".*";
} catch (UnknownHostException e) { String range2 = (address[0] & 0xff) + "." + (address[1] & 0xff) + ".*.*";
forbidAccess = true; String range3 = (address[0] & 0xff) + ".*.*.*";
} if (!LocalClientSession.getAllowedAnonymIPs().containsKey(range1) &&
if (forbidAccess) { !LocalClientSession.getAllowedAnonymIPs().containsKey(range2) &&
// Connection forbidden from that IP address !LocalClientSession.getAllowedAnonymIPs().containsKey(range3)) {
response.setChildElement(packet.getChildElement().createCopy()); forbidAccess = true;
response.setError(PacketError.Condition.forbidden); }
} }
else { } catch (UnknownHostException e) {
// Anonymous authentication allowed forbidAccess = true;
session.setAnonymousAuth(); }
response.setTo(session.getAddress()); if (forbidAccess) {
Element auth = response.setChildElement("query", "jabber:iq:auth"); // Connection forbidden from that IP address
auth.addElement("resource").setText(session.getAddress().getResource()); response.setChildElement(packet.getChildElement().createCopy());
} response.setError(PacketError.Condition.forbidden);
} }
else { else {
// Anonymous authentication is not allowed // Anonymous authentication allowed
response.setChildElement(packet.getChildElement().createCopy()); session.setAnonymousAuth();
response.setError(PacketError.Condition.forbidden); response.setTo(session.getAddress());
} Element auth = response.setChildElement("query", "jabber:iq:auth");
return response; auth.addElement("resource").setText(session.getAddress().getResource());
} }
}
public boolean isAnonymousAllowed() { else {
return anonymousAllowed; // Anonymous authentication is not allowed
} response.setChildElement(packet.getChildElement().createCopy());
response.setError(PacketError.Condition.forbidden);
public void setAllowAnonymous(boolean isAnonymous) throws UnauthorizedException { }
anonymousAllowed = isAnonymous; return response;
JiveGlobals.setProperty("xmpp.auth.anonymous", Boolean.toString(anonymousAllowed)); }
}
public boolean isAnonymousAllowed() {
public void initialize(XMPPServer server) { return anonymousAllowed;
super.initialize(server); }
userManager = server.getUserManager();
routingTable = server.getRoutingTable(); public void setAllowAnonymous(boolean isAnonymous) throws UnauthorizedException {
serverName = server.getServerInfo().getXMPPDomain(); anonymousAllowed = isAnonymous;
} JiveGlobals.setProperty("xmpp.auth.anonymous", Boolean.toString(anonymousAllowed));
}
public IQHandlerInfo getInfo() {
return info; public void initialize(XMPPServer server) {
} super.initialize(server);
} userManager = server.getUserManager();
routingTable = server.getRoutingTable();
serverName = server.getServerInfo().getXMPPDomain();
}
public IQHandlerInfo getInfo() {
return info;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment