Access another user's D-Bus session

落爺英雄遲暮 提交于 2019-12-10 14:44:23

问题


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?


回答1:


First, you need DBUS_SESSION_BUS_ADDRESS environment variable to be preserved when invoking application with su or sudo. 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 call seteuid from this application before connecting to the session bus. You can regain your privileges then with seteuid(0).




回答2:


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.



来源:https://stackoverflow.com/questions/6496847/access-another-users-d-bus-session

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!