| 1 |
/* |
| 2 |
* Copyright (C) 2014 Canonical Ltd. |
| 3 |
* |
| 4 |
* Contact: Alberto Mardegan <[email protected]> |
| 5 |
* |
| 6 |
* This file is part of lomiri-online-accounts-service |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify it |
| 9 |
* under the terms of the GNU General Public License version 3, as published |
| 10 |
* 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 General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <online-accounts-common/debug.h> |
| 22 |
#include "utils.h" |
| 23 |
|
| 24 |
#include <QDBusConnection> |
| 25 |
#include <QDBusError> |
| 26 |
#include <QDBusMessage> |
| 27 |
#include <QDBusReply> |
| 28 |
#include <sys/apparmor.h> |
| 29 |
|
| 30 |
namespace OnlineAccountsUi { |
| 31 |
|
| 32 |
QString apparmorProfileOfPeer(const QDBusMessage &message) |
| 33 |
{ |
| 34 |
static QString ourProfile; |
| 35 |
|
| 36 |
QString uniqueConnectionId = message.service(); |
| 37 |
/* This is mainly for unit tests: real messages on the session bus always |
| 38 |
* have a service name. */ |
| 39 |
if (uniqueConnectionId.isEmpty()) return QString(); |
| 40 |
|
| 41 |
if (ourProfile.isEmpty()) { |
| 42 |
char *label = NULL; |
| 43 |
char *mode = NULL; |
| 44 |
aa_getcon(&label, &mode); |
| 45 |
ourProfile = QString::fromUtf8(label); |
| 46 |
free(label); |
| 47 |
} |
| 48 |
|
| 49 |
QString appId; |
| 50 |
|
| 51 |
QDBusMessage msg = |
| 52 |
QDBusMessage::createMethodCall("org.freedesktop.DBus", |
| 53 |
"/org/freedesktop/DBus", |
| 54 |
"org.freedesktop.DBus", |
| 55 |
"GetConnectionCredentials"); |
| 56 |
QVariantList args; |
| 57 |
args << uniqueConnectionId; |
| 58 |
msg.setArguments(args); |
| 59 |
QDBusReply<QVariantMap> reply = |
| 60 |
QDBusConnection::sessionBus().call(msg, QDBus::Block); |
| 61 |
if (reply.isValid()) { |
| 62 |
QVariantMap map = reply.value(); |
| 63 |
QByteArray context = map.value("LinuxSecurityLabel").toByteArray(); |
| 64 |
if (!context.isEmpty()) { |
| 65 |
aa_splitcon(context.data(), NULL); |
| 66 |
appId = QString::fromUtf8(context); |
| 67 |
if (appId == ourProfile) { |
| 68 |
qDebug() << "Same profile as ourselves, assuming unconfined"; |
| 69 |
appId = "unconfined"; |
| 70 |
} |
| 71 |
} |
| 72 |
qDebug() << "App ID:" << appId; |
| 73 |
} else { |
| 74 |
QDBusError error = reply.error(); |
| 75 |
qWarning() << "Error getting app ID:" << error.name() << |
| 76 |
error.message(); |
| 77 |
} |
| 78 |
return appId; |
| 79 |
} |
| 80 |
|
| 81 |
} // namespace |