As continuation of this question (How can I send a notification with polkit 0.106?), I've discovered that I have to execute notify-send
as the user who I want to send notification.
But, with my current config, I can't do this, because polkit execute the script as polkitd
user, and I can't do su $user
without known user password.
By this reason, I need to create a new polkit action, to allow execute notify-send
as other user from polkitd.
My polkit rule is this:
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.consolekit.system.stop" ||
action.id == "org.freedesktop.login1.power-off" ||
action.id == "org.freedesktop.login1.power-off-multiple-sessions" ||
action.id == "org.xfce.session.xfsm-shutdown-helper")
{
try{
polkit.spawn(["/usr/bin/pendrive-reminder/check_pendrive.sh", subject.user]);
return polkit.Result.YES;
}catch(error){
polkit.spawn(["/usr/bin/pendrive-reminder/send_notify.sh", subject.user]);
return polkit.Result.NO;
}
}
});
This polkit rule must lock shutdown option in shutdown menu, and shows a notification with notify-send
, with send_notify.sh
script, which execute this:
#!/bin/bash
export DISPLAY=":0"
user=$1
pkexec --user $user notify-send "Pendrive Reminder" "Shutdown lock enabled. Disconnect pendrive to enable shutdown" -u critical
exit 0
I tried to add this polkit policy file:
<policyconfig>
<action id="org.freedesktop.notify-send">
<description>Launch notify-send command</description>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/notify-send</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
</policyconfig>
I put this file in /usr/share/polkit-1/actions/org.freedesktop.policykit.notify-send.policy
But, after put policy file in /usr/share/polkit-1/rules.d/
and press shutdown button, the shutdown menu took a long time to be showed, and notification didn't appeared. The shutdown option is locked correctly
How can I get that polkit can call notify-send from my script?
su $user -c
, try to allowpkexec --user $user notify-send
. – mviereck Feb 25 '18 at 20:08LOG_FILE=/tmp/script.log ; exec > >(tee -a ${LOG_FILE} ) ; exec 2> >(tee -a ${LOG_FILE} >&2)
. Check the log file afterwards to see possible error messages. Btw.: use @mviereck if you adress me, than I get a notification. – mviereck Feb 25 '18 at 20:56polkitd
to sudoers file, only with notify-send allowed, and execute command withsudo su $user notify-send
– AlmuHS Feb 25 '18 at 21:32sudo
is not preset everywhere, for example, debian uses onlysu
andpkexec
. Btw., instead ofsudo su $user
usesudo -u $user
. The most portable way would be a passwordlesspkexec --user $user notify-send
. I'm quite sure it is possible. – mviereck Feb 25 '18 at 21:50polkitd ALL=(ALL) NOPASSWD: /usr/bin/notify-send
only allows polkitd to executenotify-send
, which isn't the problem, so It isn't solve my problem – AlmuHS Feb 25 '18 at 21:54pkexec --user [myuser] notify-send "hello"
from tty, after doingexport DISPLAY=":0"
Executing this as root, the notification has been showed.
So, pkexec can shows the notification from another user
But, in my script not
– AlmuHS Feb 25 '18 at 22:09@mviereck
– AlmuHS Feb 25 '18 at 22:12pkexec --user almu notify-send "hola"
successfully – AlmuHS Feb 25 '18 at 22:39