-
Notifications
You must be signed in to change notification settings - Fork 7
/
agent.cpp
84 lines (68 loc) · 2.93 KB
/
agent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Copyright (C) 2020 Martin Sandsmark
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "agent.h"
#include <QApplication>
#include <QDBusMetaType>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QFileInfo>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
// Details
qRegisterMetaType<QMap<QString, QString>>("QMap<QString, QString>");
qDBusRegisterMetaType<QMap<QString, QString>>();
// Identity
qRegisterMetaType<QPair<QString, QVariantMap>>("QPair<QString, QVariantMap>>");
qDBusRegisterMetaType<QPair<QString, QVariantMap>>();
// Identity list
qRegisterMetaType<QList<QPair<QString, QVariantMap>>>("QList<QPair<QString, QVariantMap>>>");
qDBusRegisterMetaType<QList<QPair<QString, QVariantMap>>>();
if (!QDBusConnection::systemBus().isConnected()) {
qDebug() << "failed to connect to system dbus bus";
return 1;
}
QFileInfo responderInfo(LIBEXEC_DIR "polkit-dumb-agent-responder");
if (!responderInfo.exists() || !responderInfo.isExecutable()) {
qWarning() << "responder" << responderInfo.filePath() << "not installed properly, exists:" << responderInfo.exists() << "exec:" << responderInfo.isExecutable();
return 1;
}
Agent agent;
agent.responderPath = responderInfo.filePath();
if (!QDBusConnection::systemBus().registerObject("/com/iskrembilen/polkitAuthAgent", &agent, QDBusConnection::ExportAllSlots)) {
qWarning() << "Failed to register agent";
return 1;
}
QString sessionId = qgetenv("XDG_SESSION_ID");
bool isInt;
sessionId.toInt(&isInt);
if (!isInt) {
qDebug() << "XDG_SESSION_ID " << sessionId << "is not a valid integer, defaulting to 1";
sessionId = "1";
}
QDBusMessage regMessage = QDBusMessage::createMethodCall(
"org.freedesktop.PolicyKit1",
"/org/freedesktop/PolicyKit1/Authority",
"org.freedesktop.PolicyKit1.Authority",
"RegisterAuthenticationAgent"
);
regMessage.setArguments({
QVariant::fromValue(QPair<QString, QVariantMap>("unix-session", {{"session-id", sessionId}})),
"en_US.UTF-8",
"/com/iskrembilen/polkitAuthAgent"
});
QDBusConnection::systemBus().send(regMessage);
return app.exec();
}