diff options
| author | Linus Jahn <lnj@kaidan.im> | 2022-08-13 22:12:50 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2022-08-14 17:32:53 +0200 |
| commit | cb9a9db0649ce4eda1078f7904504fdfc21043bc (patch) | |
| tree | ba1dea520a732fc287c773587fdae8f55e0a6d02 /src/client | |
| parent | e577f28b630628a068b4c60c1bbd5537fc63153d (diff) | |
| download | qxmpp-cb9a9db0649ce4eda1078f7904504fdfc21043bc.tar.gz | |
Add new message carbons manager
The new manager automatically enabled carbons and the user doesn't need
to do anything. Messages are injected into the message pipeline of the
client instead of using a signal. This makes it possible to decrypt the
messages in the next step or do any other possible post-processing on
the message.
Adapting the old manager was not possible because that would mean major
behaviour change that could potentially lead to clients processing
message twice.
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/QXmppCarbonManagerV2.cpp | 165 | ||||
| -rw-r--r-- | src/client/QXmppCarbonManagerV2.h | 26 |
2 files changed, 191 insertions, 0 deletions
diff --git a/src/client/QXmppCarbonManagerV2.cpp b/src/client/QXmppCarbonManagerV2.cpp new file mode 100644 index 00000000..25b76ec7 --- /dev/null +++ b/src/client/QXmppCarbonManagerV2.cpp @@ -0,0 +1,165 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppCarbonManagerV2.h" + +#include "QXmppClient.h" +#include "QXmppConstants_p.h" +#include "QXmppFutureUtils_p.h" +#include "QXmppMessage.h" + +#include <QDomElement> +#include <QStringBuilder> + +using namespace QXmpp; +using namespace QXmpp::Private; +using Manager = QXmppCarbonManagerV2; + +class CarbonEnableIq : public QXmppIq +{ +public: + CarbonEnableIq() + : QXmppIq() + { + setType(QXmppIq::Set); + } + + // parsing not implemented + void parseElementFromChild(const QDomElement &) override + { + } + void toXmlElementFromChild(QXmlStreamWriter *writer) const override + { + writer->writeStartElement(ns_carbons, "enable"); + writer->writeEndElement(); + } +}; + +auto firstXmlnsElement(const QDomElement &el, const char *xmlns) +{ + for (auto child = el.firstChildElement(); + !child.isNull(); + child = child.nextSiblingElement()) { + if (child.namespaceURI() == xmlns) { + return child; + } + } + return QDomElement(); +} + +auto firstChildElement(const QDomElement &el, const char *tagName, const char *xmlns) +{ + for (auto child = el.firstChild(); + !child.isNull(); + child = child.nextSibling()) { + if (child.isElement() && child.namespaceURI() == xmlns) { + auto childEl = child.toElement(); + if (childEl.tagName() == tagName) { + return childEl; + } + } + } + return QDomElement(); +} + +auto parseIq(std::variant<QDomElement, SendError> &&sendResult) -> std::optional<QXmppStanza::Error> +{ + if (auto el = std::get_if<QDomElement>(&sendResult)) { + auto iqType = el->attribute(QStringLiteral("type")); + if (iqType == "result") { + return {}; + } + QXmppIq iq; + iq.parse(*el); + return iq.error(); + } else if (auto err = std::get_if<SendError>(&sendResult)) { + using Error = QXmppStanza::Error; + return Error(Error::Wait, Error::UndefinedCondition, + QStringLiteral("Couldn't send request: ") + err->text); + } + return {}; +} + +/// +/// \class QXmppCarbonManagerV2 +/// +/// \brief The QXmppCarbonManagerV2 class handles message carbons as described in \xep{0280, +/// Message Carbons}. +/// +/// The manager automatically enables carbons when a connection is established. If the connection +/// could be resumed, no new request is done. Carbon copied messages from other devices of the same +/// account and carbon copied messages from other accounts are injected into the QXmppClient. This +/// way you can handle them like any other incoming message by implementing QXmppMessageHandler. +/// +/// Checks are done to ensure that the entity sending the carbon copy is allowed to send the +/// forwarded message. +/// +/// You don't need to do anything other than adding the extension to the client to use it. +/// \code +/// QXmppClient client; +/// client.addNewExtension<QXmppCarbonManagerV2>(); +/// \endcode +/// +/// \ingroup Managers +/// +/// \since QXmpp 1.5 +/// + +bool Manager::handleStanza(const QDomElement &element, const std::optional<QXmppE2eeMetadata> &) +{ + if (element.tagName() != "message") { + return false; + } + + auto carbon = firstXmlnsElement(element, ns_carbons); + if (carbon.isNull() || (carbon.tagName() != "sent" && carbon.tagName() != "received")) { + return false; + } + + // carbon copies must always come from our bare JID + auto from = element.attribute(QStringLiteral("from")); + if (from != client()->configuration().jidBare()) { + info("Received carbon copy from attacker or buggy client '" % from % + "' trying to use CVE-2017-5603."); + return false; + } + + auto forwarded = firstChildElement(carbon, "forwarded", ns_forwarding); + auto messageElement = firstChildElement(forwarded, "message", ns_client); + if (messageElement.isNull()) { + return false; + } + + QXmppMessage message; + message.parse(messageElement); + + injectMessage(std::move(message)); + return true; +} + +void Manager::setClient(QXmppClient *newClient) +{ + if (client()) { + disconnect(client(), &QXmppClient::connected, this, &Manager::enableCarbons); + } + + QXmppClientExtension::setClient(newClient); + connect(newClient, &QXmppClient::connected, this, &Manager::enableCarbons); +} + +void Manager::enableCarbons() +{ + if (client()->streamManagementState() == QXmppClient::ResumedStream) { + // skip re-enabling for resumed streams + return; + } + + await(client()->sendIq(CarbonEnableIq()), this, [this](QXmppClient::IqResult domResult) { + if (auto err = parseIq(std::move(domResult))) { + warning("Could not enable message carbons: " % err->text()); + } else { + info("Message Carbons enabled."); + } + }); +} diff --git a/src/client/QXmppCarbonManagerV2.h b/src/client/QXmppCarbonManagerV2.h new file mode 100644 index 00000000..405792d0 --- /dev/null +++ b/src/client/QXmppCarbonManagerV2.h @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPCARBONMANAGERV2_H +#define QXMPPCARBONMANAGERV2_H + +#include "QXmppClientExtension.h" + +class QXMPP_EXPORT QXmppCarbonManagerV2 : public QXmppClientExtension +{ + Q_OBJECT +public: + bool handleStanza(const QDomElement &, const std::optional<QXmppE2eeMetadata> &) override; + +protected: + void setClient(QXmppClient *client) override; + +private: + void enableCarbons(); + + // placeholder (we may need a d-ptr in the future) + void *d; +}; + +#endif // QXMPPCARBONMANAGERV2_H |
