In a project I'm developing, I need a script run from polkit to be able to display a notification to the user when the required event is caught.
The problem is that the user that polkit uses to execute its scripts, called polkitd
, is of type nologin , so it cannot change the user to launch the notification.
Therefore, I want to create a dbus server, to which a client executed with the user would be subscribed. polkitd
would send the message through the server, and the user would receive it through the client, to finally display it on the screen as a notification.
I have seen this example that might be useful to me, with a client and server written in Python ( https://stackoverflow.com/questions/22390064/use-dbus-to-just-send-a-message-in-python/ )
But, if I run the server as another user, the client is not able to access the bus to receive the message.
how could i solve it?
UPDATE:
After trying changing the user bus (SessionBus() ) to the system bus (SystemBus() ), it seems that the system is restricting my access to said bus, and not allowing me to use it.
The displayed error says:
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.AccessDenied: Connection ":1.69" is not allowed to own the service "org.preminder" due to security policies in the configuration file
org.preminder
It is the name that I have given to the bus.
Following this guide ( https://georgemuraruc.wordpress.com/2015/07/16/d-bus-tutorial-for-python/ ) I have found a way to grant permissions to users to access the bus. I've tried it, running the server as root, and it works, but I don't know if it's a good way to solve it.
Also, I see that this implementation does not allow the client to keep waiting for a new message, but if the client runs and there is no message, it simply fails.
The current code is here: https://github.com/AlmuHS/Pendrive_Reminder/tree/work-in-progress/dbus-server
The example you mention is valid, except that in that example they are using the session bus , which is used to communicate processes within the user session. Since you need to communicate a service with a user agent, the bus you should use is the system bus .
https://pythonhosted.org/txdbus/dbus_overview.html
The equivalent to the mentioned example but using the system bus would be the following (only changing the line
bus = dbus.SessionBus
tobus = dbus.SystemBus()
):In the same way, the dbus-send command should be passed the parameter
--system
instead of--session
: