Merge branch 'SamWhited-verifyServerIqs' into development

This commit is contained in:
iNPUTmice 2014-12-23 18:09:40 +01:00
commit 58437b6f06
5 changed files with 102 additions and 124 deletions

View File

@ -22,7 +22,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
super(service); super(service);
} }
public void rosterItems(final Account account, final Element query) { private void rosterItems(final Account account, final Element query) {
final String version = query.getAttribute("ver"); final String version = query.getAttribute("ver");
if (version != null) { if (version != null) {
account.getRoster().setVersion(version); account.getRoster().setVersion(version);
@ -71,103 +71,88 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
return super.avatarData(items); return super.avatarData(items);
} }
public static boolean fromServer(final Account account, final IqPacket packet) {
return packet.getFrom() == null || packet.getFrom().equals(account.getServer()) || packet.getFrom().equals(account.getJid().toBareJid());
}
@Override @Override
public void onIqPacketReceived(final Account account, final IqPacket packet) { public void onIqPacketReceived(final Account account, final IqPacket packet) {
if (packet.hasChild("query", "jabber:iq:roster")) { if (packet.hasChild("query", Xmlns.ROSTER) && fromServer(account, packet)) {
final Jid from = packet.getFrom(); final Element query = packet.findChild("query");
if ((from == null) || (from.equals(account.getJid().toBareJid()))) { // If this is in response to a query for the whole roster:
final Element query = packet.findChild("query"); if (packet.getType() == IqPacket.TYPE_RESULT) {
this.rosterItems(account, query); account.getRoster().markAllAsNotInRoster();
} }
} else if (packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) { this.rosterItems(account, query);
// Only accept block list changes from the server. } else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
// The server should probably prevent other people from faking a blocklist push, fromServer(account, packet)) {
// but just in case let's prevent it client side as well. // Block list or block push.
final Jid from = packet.getFrom(); Log.d(Config.LOGTAG, "Received blocklist update from server");
if (from == null || from.equals(account.getServer()) || from.equals(account.getJid().toBareJid())) { final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
Log.d(Config.LOGTAG, "Received blocklist update from server"); final Element block = packet.findChild("block", Xmlns.BLOCKING);
final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING); final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
final Element block = packet.findChild("block", Xmlns.BLOCKING); (block != null ? block.getChildren() : null);
final Collection<Element> items = blocklist != null ? blocklist.getChildren() : // If this is a response to a blocklist query, clear the block list and replace with the new one.
(block != null ? block.getChildren() : null); // Otherwise, just update the existing blocklist.
// If this is a response to a blocklist query, clear the block list and replace with the new one. if (packet.getType() == IqPacket.TYPE_RESULT) {
// Otherwise, just update the existing blocklist. account.clearBlocklist();
if (packet.getType() == IqPacket.TYPE_RESULT) { }
account.clearBlocklist(); if (items != null) {
} final Collection<Jid> jids = new ArrayList<>(items.size());
if (items != null) { // Create a collection of Jids from the packet
final Collection<Jid> jids = new ArrayList<>(items.size()); for (final Element item : items) {
// Create a collection of Jids from the packet if (item.getName().equals("item")) {
for (final Element item : items) { final Jid jid = item.getAttributeAsJid("jid");
if (item.getName().equals("item")) { if (jid != null) {
final Jid jid = item.getAttributeAsJid("jid"); jids.add(jid);
if (jid != null) {
jids.add(jid);
}
} }
} }
account.getBlocklist().addAll(jids);
} }
// Update the UI account.getBlocklist().addAll(jids);
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
} else {
Log.d(Config.LOGTAG, "Received blocklist update from invalid jid: " + from.toString());
} }
} else if (packet.hasChild("unblock", Xmlns.BLOCKING)) { // Update the UI
final Jid from = packet.getFrom(); mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
if ((from == null || from.equals(account.getServer()) || from.equals(account.getJid().toBareJid())) && } else if (packet.hasChild("unblock", Xmlns.BLOCKING) &&
packet.getType() == IqPacket.TYPE_SET) { fromServer(account, packet) && packet.getType() == IqPacket.TYPE_SET) {
Log.d(Config.LOGTAG, "Received unblock update from server"); Log.d(Config.LOGTAG, "Received unblock update from server");
final Collection<Element> items = packet.getChildren().get(0).getChildren(); final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
if (items.size() == 0) { if (items.size() == 0) {
// No children to unblock == unblock all // No children to unblock == unblock all
account.getBlocklist().clear(); account.getBlocklist().clear();
} else { } else {
final Collection<Jid> jids = new ArrayList<>(items.size()); final Collection<Jid> jids = new ArrayList<>(items.size());
for (final Element item : items) { for (final Element item : items) {
if (item.getName().equals("item")) { if (item.getName().equals("item")) {
final Jid jid = item.getAttributeAsJid("jid"); final Jid jid = item.getAttributeAsJid("jid");
if (jid != null) { if (jid != null) {
jids.add(jid); jids.add(jid);
}
} }
} }
account.getBlocklist().removeAll(jids);
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
}
} else {
if (packet.getType() == IqPacket.TYPE_SET) {
Log.d(Config.LOGTAG, "Received unblock update from invalid jid " + from.toString());
} else {
Log.d(Config.LOGTAG, "Received unblock update with invalid type " + packet.getType());
} }
account.getBlocklist().removeAll(jids);
} }
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
|| packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
mXmppConnectionService.getJingleConnectionManager()
.deliverIbbPacket(account, packet);
} else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
final IqPacket response = mXmppConnectionService.getIqGenerator()
.discoResponse(packet);
account.getXmppConnection().sendIqPacket(response, null);
} else if (packet.hasChild("ping", "urn:xmpp:ping")) {
final IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
mXmppConnectionService.sendIqPacket(account, response, null);
} else { } else {
if (packet.getFrom() == null) { if ((packet.getType() == IqPacket.TYPE_GET)
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": received iq with invalid from "+packet.toString()); || (packet.getType() == IqPacket.TYPE_SET)) {
return; final IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR);
} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb") final Element error = response.addChild("error");
|| packet.hasChild("data", "http://jabber.org/protocol/ibb")) { error.setAttribute("type", "cancel");
mXmppConnectionService.getJingleConnectionManager() error.addChild("feature-not-implemented",
.deliverIbbPacket(account, packet); "urn:ietf:params:xml:ns:xmpp-stanzas");
} else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
final IqPacket response = mXmppConnectionService.getIqGenerator()
.discoResponse(packet);
account.getXmppConnection().sendIqPacket(response, null); account.getXmppConnection().sendIqPacket(response, null);
} else if (packet.hasChild("ping", "urn:xmpp:ping")) { }
final IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
mXmppConnectionService.sendIqPacket(account, response, null);
} else {
if ((packet.getType() == IqPacket.TYPE_GET)
|| (packet.getType() == IqPacket.TYPE_SET)) {
final IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR);
final Element error = response.addChild("error");
error.setAttribute("type", "cancel");
error.addChild("feature-not-implemented",
"urn:ietf:params:xml:ns:xmpp-stanzas");
account.getXmppConnection().sendIqPacket(response, null);
}
}
} }
} }

View File

@ -74,6 +74,7 @@ import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener; import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener;
import eu.siacs.conversations.utils.PRNGFixes; import eu.siacs.conversations.utils.PRNGFixes;
import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.utils.Xmlns;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnBindListener; import eu.siacs.conversations.xmpp.OnBindListener;
import eu.siacs.conversations.xmpp.OnContactStatusChanged; import eu.siacs.conversations.xmpp.OnContactStatusChanged;
@ -483,11 +484,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
this.mMemorizingTrustManager = new MemorizingTrustManager( this.mMemorizingTrustManager = new MemorizingTrustManager(
getApplicationContext()); getApplicationContext());
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
int cacheSize = maxMemory / 8; final int cacheSize = maxMemory / 8;
this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) { this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) {
@Override @Override
protected int sizeOf(String key, Bitmap bitmap) { protected int sizeOf(final String key, final Bitmap bitmap) {
return bitmap.getByteCount() / 1024; return bitmap.getByteCount() / 1024;
} }
}; };
@ -495,7 +496,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext()); this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
this.accounts = databaseBackend.getAccounts(); this.accounts = databaseBackend.getAccounts();
for (Account account : this.accounts) { for (final Account account : this.accounts) {
account.initOtrEngine(this); account.initOtrEngine(this);
this.databaseBackend.readRoster(account.getRoster()); this.databaseBackend.readRoster(account.getRoster());
} }
@ -521,7 +522,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
@Override @Override
public void onTaskRemoved(Intent rootIntent) { public void onTaskRemoved(final Intent rootIntent) {
super.onTaskRemoved(rootIntent); super.onTaskRemoved(rootIntent);
if (!getPreferences().getBoolean("keep_foreground_service",false)) { if (!getPreferences().getBoolean("keep_foreground_service",false)) {
this.logoutAndSave(); this.logoutAndSave();
@ -529,7 +530,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
private void logoutAndSave() { private void logoutAndSave() {
for (Account account : accounts) { for (final Account account : accounts) {
databaseBackend.writeRoster(account.getRoster()); databaseBackend.writeRoster(account.getRoster());
if (account.getXmppConnection() != null) { if (account.getXmppConnection() != null) {
disconnect(account, false); disconnect(account, false);
@ -791,21 +792,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} else { } else {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching roster"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching roster");
} }
iqPacket.query("jabber:iq:roster").setAttribute("ver", iqPacket.query(Xmlns.ROSTER).setAttribute("ver",
account.getRosterVersion()); account.getRosterVersion());
account.getXmppConnection().sendIqPacket(iqPacket, account.getXmppConnection().sendIqPacket(iqPacket, mIqParser);
new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(final Account account,
final IqPacket packet) {
final Element query = packet.findChild("query");
if (query != null) {
account.getRoster().markAllAsNotInRoster();
mIqParser.rosterItems(account, query);
}
}
});
} }
public void fetchBookmarks(final Account account) { public void fetchBookmarks(final Account account) {
@ -1433,7 +1422,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void createAdhocConference(final Account account, final Iterable<Jid> jids, final UiCallback<Conversation> callback) { public void createAdhocConference(final Account account, final Iterable<Jid> jids, final UiCallback<Conversation> callback) {
Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": creating adhoc conference with "+ jids.toString()); Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": creating adhoc conference with " + jids.toString());
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
try { try {
String server = findConferenceServer(account); String server = findConferenceServer(account);
@ -1637,17 +1626,17 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return false; return false;
} }
public void pushContactToServer(Contact contact) { public void pushContactToServer(final Contact contact) {
contact.resetOption(Contact.Options.DIRTY_DELETE); contact.resetOption(Contact.Options.DIRTY_DELETE);
contact.setOption(Contact.Options.DIRTY_PUSH); contact.setOption(Contact.Options.DIRTY_PUSH);
Account account = contact.getAccount(); final Account account = contact.getAccount();
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
boolean ask = contact.getOption(Contact.Options.ASKING); final boolean ask = contact.getOption(Contact.Options.ASKING);
boolean sendUpdates = contact final boolean sendUpdates = contact
.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST) .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
&& contact.getOption(Contact.Options.PREEMPTIVE_GRANT); && contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
IqPacket iq = new IqPacket(IqPacket.TYPE_SET); final IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
iq.query("jabber:iq:roster").addChild(contact.asElement()); iq.query(Xmlns.ROSTER).addChild(contact.asElement());
account.getXmppConnection().sendIqPacket(iq, null); account.getXmppConnection().sendIqPacket(iq, null);
if (sendUpdates) { if (sendUpdates) {
sendPresencePacket(account, sendPresencePacket(account,
@ -1660,7 +1649,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
public void publishAvatar(Account account, Uri image, public void publishAvatar(final Account account,
final Uri image,
final UiCallback<Avatar> callback) { final UiCallback<Avatar> callback) {
final Bitmap.CompressFormat format = Config.AVATAR_FORMAT; final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
final int size = Config.AVATAR_SIZE; final int size = Config.AVATAR_SIZE;
@ -1680,13 +1670,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
callback.error(R.string.error_saving_avatar, avatar); callback.error(R.string.error_saving_avatar, avatar);
return; return;
} }
IqPacket packet = this.mIqGenerator.publishAvatar(avatar); final IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
this.sendIqPacket(account, packet, new OnIqPacketReceived() { this.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket result) { public void onIqPacketReceived(Account account, IqPacket result) {
if (result.getType() == IqPacket.TYPE_RESULT) { if (result.getType() == IqPacket.TYPE_RESULT) {
IqPacket packet = XmppConnectionService.this.mIqGenerator final IqPacket packet = XmppConnectionService.this.mIqGenerator
.publishAvatarMetadata(avatar); .publishAvatarMetadata(avatar);
sendIqPacket(account, packet, new OnIqPacketReceived() { sendIqPacket(account, packet, new OnIqPacketReceived() {
@ -1819,7 +1809,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
Account account = contact.getAccount(); Account account = contact.getAccount();
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
IqPacket iq = new IqPacket(IqPacket.TYPE_SET); IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
Element item = iq.query("jabber:iq:roster").addChild("item"); Element item = iq.query(Xmlns.ROSTER).addChild("item");
item.setAttribute("jid", contact.getJid().toString()); item.setAttribute("jid", contact.getJid().toString());
item.setAttribute("subscription", "remove"); item.setAttribute("subscription", "remove");
account.getXmppConnection().sendIqPacket(iq, null); account.getXmppConnection().sendIqPacket(iq, null);
@ -2027,12 +2017,12 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public List<String> getKnownHosts() { public List<String> getKnownHosts() {
List<String> hosts = new ArrayList<>(); final List<String> hosts = new ArrayList<>();
for (Account account : getAccounts()) { for (final Account account : getAccounts()) {
if (!hosts.contains(account.getServer().toString())) { if (!hosts.contains(account.getServer().toString())) {
hosts.add(account.getServer().toString()); hosts.add(account.getServer().toString());
} }
for (Contact contact : account.getRoster().getContacts()) { for (final Contact contact : account.getRoster().getContacts()) {
if (contact.showInRoster()) { if (contact.showInRoster()) {
final String server = contact.getServer().toString(); final String server = contact.getServer().toString();
if (server != null && !hosts.contains(server)) { if (server != null && !hosts.contains(server)) {
@ -2045,10 +2035,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public List<String> getKnownConferenceHosts() { public List<String> getKnownConferenceHosts() {
ArrayList<String> mucServers = new ArrayList<>(); final ArrayList<String> mucServers = new ArrayList<>();
for (Account account : accounts) { for (final Account account : accounts) {
if (account.getXmppConnection() != null) { if (account.getXmppConnection() != null) {
String server = account.getXmppConnection().getMucServer(); final String server = account.getXmppConnection().getMucServer();
if (server != null && !mucServers.contains(server)) { if (server != null && !mucServers.contains(server)) {
mucServers.add(server); mucServers.add(server);
} }

View File

@ -31,6 +31,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Blockable; import eu.siacs.conversations.entities.Blockable;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
@ -316,7 +317,8 @@ public class ConversationActivity extends XmppActivity
} else { } else {
menuUnblock.setVisible(false); menuUnblock.setVisible(false);
} }
if (!this.getSelectedConversation().getAccount().getXmppConnection().getFeatures().blocking()) { final Account account = this.getSelectedConversation().getAccount();
if (account.getStatus() != Account.State.ONLINE || !account.getXmppConnection().getFeatures().blocking()) {
menuBlock.setVisible(false); menuBlock.setVisible(false);
menuUnblock.setVisible(false); menuUnblock.setVisible(false);
} }

View File

@ -327,7 +327,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
if (mAccount == null) { if (mAccount == null) {
showQrCode.setVisible(false); showQrCode.setVisible(false);
showBlocklist.setVisible(false); showBlocklist.setVisible(false);
} else if (!mAccount.getXmppConnection().getFeatures().blocking()) { } else if (mAccount.getStatus() != Account.State.ONLINE || !mAccount.getXmppConnection().getFeatures().blocking()) {
showBlocklist.setVisible(false); showBlocklist.setVisible(false);
} }
return true; return true;

View File

@ -2,4 +2,5 @@ package eu.siacs.conversations.utils;
public final class Xmlns { public final class Xmlns {
public static final String BLOCKING = "urn:xmpp:blocking"; public static final String BLOCKING = "urn:xmpp:blocking";
public static final String ROSTER = "jabber:iq:roster";
} }