Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access another user's D-Bus session

Tags:

unix

dbus

Let's assume this kind of situation: we have one user logged in, executing some application through sudo or su. This user has got a dbus-daemon running.
However, when an application running with root privileges tries to access D-Bus, it just spawns another dbus-daemon, owned by root user. That's not a desired situation.

Is there a way to gain access to D-Bus session of user who ran the application through sudo or su?

like image 347
Dr McKay Avatar asked Jun 27 '11 17:06

Dr McKay


People also ask

What is D-bus session?

dbus-run-session is used to start a session bus instance of dbus-daemon from a shell script, and start a specified program in that session. The dbus-daemon will run for as long as the program does, after which it will terminate.

What is dbus session bus address?

dbus/session-bus for the session bus address based on the machine id and DISPLAY environment variable. This contains the location of the session bus that we need and might be sufficient to accomplishing the goal of not requiring the DBUS_SESSION_BUS_ADDRESS to be set in the environment.

What is D-bus connection in Linux?

D-Bus is an inter-process communication (IPC) mechanism initially designed to replace the software component communications systems used by the GNOME and KDE Linux desktop environments (CORBA and DCOP respectively).

What is D-bus daemon?

D-Bus is first a library that provides one-to-one communication between any two applications; dbus-daemon is an application that uses this library to implement a message bus daemon. Multiple programs connect to the message bus daemon and can exchange messages with one another.


1 Answers

If you're on a systemd distro, the relatively new machinectl shell command can do the work of su/sudo, and it will also set session variables like XDG_RUNTIME_DIR and DBUS_SESSION_BUS_ADDRESS. So for example, if I want to run systemctl --user as user test, the normal approach will fail:

$ sudo --user=test systemctl --user
Failed to connect to bus: No such file or directory

But this way works:

$ sudo machinectl shell --uid=test .host -- /usr/bin/systemctl --user

If you need to "reach back" into the user session that invoked a sudo script, you could use the SUDO_USER/SUDO_UID to hack something together.

like image 87
Jack O'Connor Avatar answered Oct 17 '22 19:10

Jack O'Connor