Commit 49db6756 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Make sure to return an IQ error if an unhandled exception occurs. JM-638

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3760 b35dd754-fafc-0310-a699-88a17e54d16e
parent 16ba5ac1
......@@ -83,86 +83,81 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
}
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
ClientSession session = sessionManager.getSession(packet.getFrom());
// If no session was found then answer an error (if possible)
if (session == null) {
Log.error("Error during authentication. Session not found in " +
sessionManager.getPreAuthenticatedKeys() +
" for key " +
packet.getFrom());
// This error packet will probably won't make it through
IQ reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(PacketError.Condition.internal_server_error);
return reply;
}
IQ response = null;
try {
ClientSession session = sessionManager.getSession(packet.getFrom());
// If no session was found then answer an error (if possible)
if (session == null) {
Log.error("Error during authentication. Session not found in " +
sessionManager.getPreAuthenticatedKeys() +
" for key " +
packet.getFrom());
// This error packet will probably won't make it through
IQ reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(PacketError.Condition.internal_server_error);
return reply;
Element iq = packet.getElement();
Element query = iq.element("query");
Element queryResponse = probeResponse.createCopy();
if (IQ.Type.get == packet.getType()) {
String username = query.elementTextTrim("username");
if (username != null) {
queryResponse.element("username").setText(username);
}
response = IQ.createResultIQ(packet);
response.setChildElement(queryResponse);
// This is a workaround. Since we don't want to have an incorrect TO attribute
// value we need to clean up the TO attribute and send directly the response.
// The TO attribute will contain an incorrect value since we are setting a fake
// JID until the user actually authenticates with the server.
if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
response.setTo((JID)null);
}
}
IQ response = null;
try {
Element iq = packet.getElement();
Element query = iq.element("query");
Element queryResponse = probeResponse.createCopy();
if (IQ.Type.get == packet.getType()) {
String username = query.elementTextTrim("username");
if (username != null) {
queryResponse.element("username").setText(username);
}
response = IQ.createResultIQ(packet);
response.setChildElement(queryResponse);
// This is a workaround. Since we don't want to have an incorrect TO attribute
// value we need to clean up the TO attribute and send directly the response.
// The TO attribute will contain an incorrect value since we are setting a fake
// JID until the user actually authenticates with the server.
if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
response.setTo((JID)null);
}
// Otherwise set query
else {
if (query.elements().isEmpty()) {
// Anonymous authentication
response = anonymousLogin(session, packet);
}
// Otherwise set query
else {
if (query.elements().isEmpty()) {
// Anonymous authentication
response = anonymousLogin(session, packet);
String username = query.elementTextTrim("username");
// Login authentication
String password = query.elementTextTrim("password");
String digest = null;
if (query.element("digest") != null) {
digest = query.elementTextTrim("digest").toLowerCase();
}
else {
String username = query.elementTextTrim("username");
// Login authentication
String password = query.elementTextTrim("password");
String digest = null;
if (query.element("digest") != null) {
digest = query.elementTextTrim("digest").toLowerCase();
}
// If we're already logged in, this is a password reset
if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
response = passwordReset(password, packet, username, session);
}
else {
// it is an auth attempt
response =
login(username, query, packet, response, password, session,
digest);
}
// If we're already logged in, this is a password reset
if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
response = passwordReset(password, packet, username, session);
}
else {
// it is an auth attempt
response =
login(username, query, packet, response, password, session,
digest);
}
}
}
catch (UserNotFoundException e) {
response = IQ.createResultIQ(packet);
response.setChildElement(packet.getChildElement().createCopy());
response.setError(PacketError.Condition.not_authorized);
}
catch (UnauthorizedException e) {
response = IQ.createResultIQ(packet);
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
// resource is incorrect.
session.process(response);
}
catch (Exception e) {
Log.error("Error handling authentication IQ packet", e);
catch (UserNotFoundException e) {
response = IQ.createResultIQ(packet);
response.setChildElement(packet.getChildElement().createCopy());
response.setError(PacketError.Condition.not_authorized);
}
catch (UnauthorizedException e) {
response = IQ.createResultIQ(packet);
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
// resource is incorrect.
session.process(response);
return null;
}
......@@ -189,7 +184,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
// If a session already exists with the requested JID, then check to see
// if we should kick it off or refuse the new connection
if (sessionManager.isActiveRoute(username, resource)) {
ClientSession oldSession = null;
ClientSession oldSession;
try {
String domain = localServer.getServerInfo().getName();
oldSession = sessionManager.getSession(username, domain, resource);
......@@ -260,7 +255,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
}
private IQ anonymousLogin(ClientSession session, IQ packet) {
IQ response = IQ.createResultIQ(packet);;
IQ response = IQ.createResultIQ(packet);
if (anonymousAllowed) {
session.setAnonymousAuth();
response.setTo(session.getAddress());
......
......@@ -11,11 +11,11 @@
package org.jivesoftware.wildfire.handler;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
......@@ -45,9 +45,9 @@ public abstract class IQHandler extends BasicModule implements ChannelHandler {
public void process(Packet packet) throws PacketException {
IQ iq = (IQ) packet;
try {
iq = handleIQ(iq);
if (iq != null) {
deliverer.deliver(iq);
IQ reply = handleIQ(iq);
if (reply != null) {
deliverer.deliver(reply);
}
}
catch (org.jivesoftware.wildfire.auth.UnauthorizedException e) {
......@@ -56,10 +56,7 @@ public abstract class IQHandler extends BasicModule implements ChannelHandler {
IQ response = IQ.createResultIQ(iq);
response.setChildElement(iq.getChildElement().createCopy());
response.setError(PacketError.Condition.not_authorized);
Session session = sessionManager.getSession(iq.getFrom());
if (!session.getConnection().isClosed()) {
session.process(response);
}
sessionManager.getSession(iq.getFrom()).process(response);
}
catch (Exception de) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), de);
......@@ -69,6 +66,15 @@ public abstract class IQHandler extends BasicModule implements ChannelHandler {
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
try {
IQ response = IQ.createResultIQ(iq);
response.setChildElement(iq.getChildElement().createCopy());
response.setError(PacketError.Condition.internal_server_error);
sessionManager.getSession(iq.getFrom()).process(response);
}
catch (Exception e1) {
// Do nothing
}
}
}
......
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