Skip to content

Commit 9609529

Browse files
committed
dbus: add support for setgid binaries
1 parent dd9e2a4 commit 9609529

5 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ set(libinputactions_SRCS
4747
libinputactions/handlers/TouchscreenTriggerHandler.cpp
4848
libinputactions/handlers/TriggerHandler.cpp
4949
libinputactions/helpers/Math.cpp
50+
libinputactions/helpers/QDBusConnection.cpp
5051
libinputactions/helpers/QString.cpp
5152
libinputactions/helpers/QThread.cpp
5253
libinputactions/helpers/QVariant.cpp
53-
libinputactions/helpers/Session.cpp
5454
libinputactions/input/backends/InputBackend.cpp
5555
libinputactions/input/backends/LibevdevComplementaryInputBackend.cpp
5656
libinputactions/input/backends/LibinputInputBackend.cpp

src/libinputactions/dbus/IntegratedDBusInterface.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <libinputactions/InputActionsMain.h>
2222
#include <libinputactions/config/ConfigIssueManager.h>
2323
#include <libinputactions/config/ConfigLoader.h>
24+
#include <libinputactions/helpers/QDBusConnection.h>
2425
#include <libinputactions/input/StrokeRecorder.h>
2526
#include <libinputactions/input/backends/InputBackend.h>
2627
#include <libinputactions/interfaces/OnScreenMessageManager.h>
@@ -31,7 +32,7 @@ namespace InputActions
3132
{
3233

3334
IntegratedDBusInterface::IntegratedDBusInterface()
34-
: m_bus(QDBusConnection::sessionBus())
35+
: m_bus(QDBusConnectionHelpers::sessionBus())
3536
{
3637
m_bus.registerService(INPUTACTIONS_DBUS_SERVICE);
3738
m_bus.registerObject(INPUTACTIONS_DBUS_PATH, this, QDBusConnection::ExportAllSlots);

src/libinputactions/helpers/Session.cpp renamed to src/libinputactions/helpers/QDBusConnection.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,31 @@
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19-
#include "Session.h"
20-
#include <QFile>
19+
#include "QDBusConnection.h"
20+
#include <sys/types.h>
2121

22-
namespace InputActions::SessionHelpers
22+
namespace InputActions::QDBusConnectionHelpers
2323
{
2424

25-
QString currentTty()
25+
const QDBusConnection &sessionBus()
2626
{
27-
QFile f("/sys/class/tty/tty0/active");
28-
if (f.open(QIODeviceBase::ReadOnly)) {
29-
return QString::fromUtf8(f.readAll()).trimmed();
27+
static std::optional<QDBusConnection> cached;
28+
if (cached) {
29+
return cached.value();
3030
}
31-
return "unknown";
31+
32+
gid_t rgid{};
33+
gid_t egid{};
34+
gid_t sgid{};
35+
if (!getresgid(&rgid, &egid, &sgid) && (rgid != egid || egid != sgid)) {
36+
if (const auto address = qEnvironmentVariable("DBUS_SESSION_BUS_ADDRESS"); !address.isEmpty()) {
37+
cached = QDBusConnection::connectToBus(address, "sessionBus");
38+
return cached.value();
39+
}
40+
}
41+
42+
cached = QDBusConnection::sessionBus();
43+
return cached.value();
3244
}
3345

3446
}

src/libinputactions/helpers/Session.h renamed to src/libinputactions/helpers/QDBusConnection.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818

1919
#pragma once
2020

21-
#include <QString>
21+
#include <QDBusConnection>
2222

23-
namespace InputActions::SessionHelpers
23+
namespace InputActions::QDBusConnectionHelpers
2424
{
2525

26-
QString currentTty();
26+
/**
27+
* Same as QDBusConnection::sessionBus, but uses QDBusConnection::connectToBus with the address specified in the DBUS_SESSION_BUS_ADDRESS environment variable
28+
* for setgid binaries.
29+
*
30+
* Connection is cached.
31+
*/
32+
const QDBusConnection &sessionBus();
2733

2834
}

src/libinputactions/interfaces/implementations/DBusNotificationManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "DBusNotificationManager.h"
2020
#include <QDBusInterface>
2121
#include <QThreadPool>
22+
#include <libinputactions/helpers/QDBusConnection.h>
2223

2324
namespace InputActions
2425
{
@@ -31,7 +32,7 @@ void DBusNotificationManager::sendNotification(const QString &title, const QStri
3132
QDBusInterface notificationsInterface("org.freedesktop.Notifications",
3233
"/org/freedesktop/Notifications",
3334
"org.freedesktop.Notifications",
34-
QDBusConnection::sessionBus());
35+
QDBusConnectionHelpers::sessionBus());
3536
if (notificationsInterface.isValid()) {
3637
notificationsInterface.asyncCall("Notify", "InputActions", 0U, "", title, content, QStringList(), QVariantMap(), 5000);
3738
}

0 commit comments

Comments
 (0)