| 1 | /* |
| 2 | * This file is part of OnlineAccountsDaemon |
| 3 | * |
| 4 | * Copyright (C) 2015 Canonical Ltd. |
| 5 | * |
| 6 | * Contact: Alberto Mardegan <[email protected]> |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU Lesser General Public License version 3, as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "client_registry.h" |
| 22 | |
| 23 | #include <QByteArray> |
| 24 | #include <QDBusConnection> |
| 25 | #include <QDBusConnectionInterface> |
| 26 | #include <QDBusMessage> |
| 27 | #include <QDBusReply> |
| 28 | #include <QDBusServiceWatcher> |
| 29 | #include <QDebug> |
| 30 | #include <QHash> |
| 31 | #include <QVariantMap> |
| 32 | #include <sys/apparmor.h> |
| 33 | |
| 34 | using namespace OnlineAccountsDaemon; |
| 35 | |
| 36 | namespace OnlineAccountsDaemon { |
| 37 | |
| 38 | class ClientRegistryPrivate: public QObject |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | Q_DECLARE_PUBLIC(ClientRegistry) |
| 42 | |
| 43 | public: |
| 44 | ClientRegistryPrivate(ClientRegistry *q); |
| 45 | |
| 46 | void registerClient(const QString &client); |
| 47 | QString getSecurityContext(const QString &client) const; |
| 48 | pid_t getPid(const QString &client) const; |
| 49 | |
| 50 | private Q_SLOTS: |
| 51 | void onServiceUnregistered(const QString &client); |
| 52 | |
| 53 | private: |
| 54 | static ClientRegistry *m_instance; |
| 55 | QDBusConnection m_connection; |
| 56 | QDBusServiceWatcher m_watcher; |
| 57 | QHash<QString,QString> m_clientContexts; |
| 58 | ClientRegistry *q_ptr; |
| 59 | }; |
| 60 | |
| 61 | ClientRegistry *ClientRegistryPrivate::m_instance = 0; |
| 62 | |
| 63 | } // namespace |
| 64 | |
| 65 | ClientRegistryPrivate::ClientRegistryPrivate(ClientRegistry *q): |
| 66 | QObject(q), |
| 67 | m_connection(QDBusConnection::sessionBus()), |
| 68 | q_ptr(q) |
| 69 | { |
| 70 | m_watcher.setConnection(m_connection); |
| 71 | QObject::connect(&m_watcher, SIGNAL(serviceUnregistered(const QString&)), |
| 72 | this, SLOT(onServiceUnregistered(const QString&))); |
| 73 | } |
| 74 | |
| 75 | void ClientRegistryPrivate::registerClient(const QString &client) |
| 76 | { |
| 77 | Q_Q(ClientRegistry); |
| 78 | |
| 79 | if (m_clientContexts.contains(client)) return; |
| 80 | |
| 81 | bool wasEmpty = m_clientContexts.isEmpty(); |
| 82 | m_clientContexts.insert(client, getSecurityContext(client)); |
| 83 | m_watcher.addWatchedService(client); |
| 84 | if (wasEmpty) { |
| 85 | Q_EMIT q->hasClientsChanged(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | QString ClientRegistryPrivate::getSecurityContext(const QString &client) const |
| 90 | { |
| 91 | QString dbusService = qEnvironmentVariableIsEmpty("OAD_TESTING") ? |
| 92 | "org.freedesktop.DBus" : "mocked.org.freedesktop.dbus"; |
| 93 | QDBusMessage msg = |
| 94 | QDBusMessage::createMethodCall(dbusService, |
| 95 | "/org/freedesktop/DBus", |
| 96 | "org.freedesktop.DBus", |
| 97 | "GetConnectionCredentials"); |
| 98 | msg << client; |
| 99 | QDBusReply<QVariantMap> reply = m_connection.call(msg, QDBus::Block); |
| 100 | |
| 101 | QString context; |
| 102 | if (reply.isValid()) { |
| 103 | QVariantMap map = reply.value(); |
| 104 | QByteArray label = map.value("LinuxSecurityLabel").toByteArray(); |
| 105 | if (!label.isEmpty()) { |
| 106 | aa_splitcon(label.data(), NULL); |
| 107 | context = QString::fromUtf8(label); |
| 108 | } |
| 109 | } else { |
| 110 | QDBusError error = reply.error(); |
| 111 | qWarning() << "Error getting app ID:" << error.name() << |
| 112 | error.message(); |
| 113 | context = QStringLiteral("unconfined"); |
| 114 | } |
| 115 | qDebug() << "Client security context:" << context; |
| 116 | |
| 117 | return context; |
| 118 | } |
| 119 | |
| 120 | pid_t ClientRegistryPrivate::getPid(const QString &client) const |
| 121 | { |
| 122 | QDBusReply<uint> reply = m_connection.interface()->servicePid(client); |
| 123 | return pid_t(reply.value()); |
| 124 | } |
| 125 | |
| 126 | void ClientRegistryPrivate::onServiceUnregistered(const QString &client) |
| 127 | { |
| 128 | Q_Q(ClientRegistry); |
| 129 | |
| 130 | qDebug() << "Client disappeared:" << client; |
| 131 | m_clientContexts.remove(client); |
| 132 | if (m_clientContexts.isEmpty()) { |
| 133 | Q_EMIT q->hasClientsChanged(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | ClientRegistry::ClientRegistry(): |
| 138 | QObject(), |
| 139 | d_ptr(new ClientRegistryPrivate(this)) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | ClientRegistry::~ClientRegistry() |
| 144 | { |
| 145 | delete d_ptr; |
| 146 | } |
| 147 | |
| 148 | ClientRegistry *ClientRegistry::instance() |
| 149 | { |
| 150 | if (!ClientRegistryPrivate::m_instance) { |
| 151 | ClientRegistryPrivate::m_instance = new ClientRegistry(); |
| 152 | } |
| 153 | return ClientRegistryPrivate::m_instance; |
| 154 | } |
| 155 | |
| 156 | QString ClientRegistry::registerClient(const QDBusConnection &connection, |
| 157 | const QDBusMessage &message) |
| 158 | { |
| 159 | Q_UNUSED(connection); // only needed for p2p clients |
| 160 | |
| 161 | QString client = message.service(); |
| 162 | registerClient(client); |
| 163 | return client; |
| 164 | } |
| 165 | |
| 166 | void ClientRegistry::registerClient(const QString &client) |
| 167 | { |
| 168 | Q_D(ClientRegistry); |
| 169 | d->registerClient(client); |
| 170 | } |
| 171 | |
| 172 | QStringList ClientRegistry::clients() const |
| 173 | { |
| 174 | Q_D(const ClientRegistry); |
| 175 | return d->m_clientContexts.keys(); |
| 176 | } |
| 177 | |
| 178 | void ClientRegistry::registerActiveClients(const QStringList &clients) |
| 179 | { |
| 180 | Q_D(const ClientRegistry); |
| 181 | QStringList activeServices = |
| 182 | d->m_connection.interface()->registeredServiceNames().value(); |
| 183 | Q_FOREACH(const QString &client, clients) { |
| 184 | if (activeServices.contains(client)) { |
| 185 | registerClient(client); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | QString ClientRegistry::clientSecurityContext(const QString &client) const |
| 191 | { |
| 192 | Q_D(const ClientRegistry); |
| 193 | QHash<QString,QString>::const_iterator i = |
| 194 | d->m_clientContexts.find(client); |
| 195 | if (i != d->m_clientContexts.constEnd()) { |
| 196 | return i.value(); |
| 197 | } |
| 198 | |
| 199 | return d->getSecurityContext(client); |
| 200 | } |
| 201 | |
| 202 | pid_t ClientRegistry::clientPid(const QString &client) const |
| 203 | { |
| 204 | Q_D(const ClientRegistry); |
| 205 | return d->getPid(client); |
| 206 | } |
| 207 | |
| 208 | #include "client_registry.moc" |