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?
First, you need
DBUS_SESSION_BUS_ADDRESSenvironment variable to be preserved when invoking application withsuorsudo. Unfortunately, this is not enough, because DBus always checks (as a security measure) whether UIDs of the calling process and the session daemon are the same. The only workaround is to callseteuidfrom this application before connecting to the session bus. You can regain your privileges then withseteuid(0).