作为该问题的继续(如何使用polkit 0.106发送通知?),我发现我必须以notify-send
要发送通知的用户身份执行。
但是,使用我当前的配置,我无法执行此操作,因为polkit以polkitd
用户身份执行脚本,并且su $user
没有已知的用户密码也无法执行。
因此,我需要创建一个新的polkit动作,以允许notify-send
其他用户从polkitd中执行。
我的polkit规则是这样的:
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;
}
}
});
此polkit规则必须锁定“关闭”菜单中的“关闭”选项,并notify-send
使用send_notify.sh
脚本显示带有的通知,该脚本将执行以下操作:
#!/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
我试图添加此polkit策略文件:
<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>
我把这个文件放在 /usr/share/polkit-1/actions/org.freedesktop.policykit.notify-send.policy
但是,在放入策略文件/usr/share/polkit-1/rules.d/
并按“关闭”按钮后,关闭菜单显示了很长时间,并且没有出现通知。关机选项已正确锁定
我如何获得polkit可以从我的脚本中调用通知发送?
经过几次测试后,我得到了以下结果:
如果执行此命令,以polkitd用户执行脚本,将显示错误:
sudo su polkitd -s /bin/bash -c aux_scripts/send_notify.sh almu
Error executing command as another user: Not authorized
This incident has been reported.
因此,我认为polkitd用户是一个受限帐户,它无法像其他用户一样执行命令
结论是,我确定如果不修改系统内部就无法执行此操作。我不允许在我的应用程序中使用它,因此无法以其他用户的身份从polkit启动命令
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句