我有一个 DBus 激活的小守护进程,它在系统总线中注册自己,但以 GDM 用户身份运行(其想法是允许普通用户设置 dconf 设置和其他内容),并且它工作得很好。重点是我想限制对特定UID的访问,要求用户在更改允许更改的UID时输入密码。我尝试使用 polkit,使用“auth_admin”定义一个文件,并使用“POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION”标志调用“polkit_authority_check_authorization”,但我收到此错误:
Error: GDBus.Error:org.freedesktop.PolicyKit1.Error.NotAuthorized: Only trusted callers (e.g. uid 0 or an action owner) can use CheckAuthorization() for subjects belonging to other identities
如何进行此身份验证?
以 GDM 用户身份运行的守护进程中的一段代码:
g_autoptr (PolkitAuthority) authority = NULL;
g_autoptr (PolkitAuthorizationResult) result = NULL;
g_autoptr (PolkitSubject) sender = NULL;
GError *error = NULL;
sender = polkit_system_bus_name_new (g_dbus_method_invocation_get_sender (invocation));
authority = polkit_authority_get_sync (NULL, NULL);
result = polkit_authority_check_authorization_sync (authority,
sender,
"org.gnome.GdmSettings.",
NULL,
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
NULL,
&error);
set_timeout ();
if (error != NULL) {
g_print("Error: %s\n", error->message);
}
(我知道我不应该使用带有该标志的 _sync 调用,但这仍然是一个概念验证;当它起作用时,我将使用异步版本)。
这是 org.gnome.GdmSettings.SetAllowedUID.policy 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
<policyconfig>
<vendor>The GNOME Project</vendor>
<vendor_url>http://www.gnome.org/</vendor_url>
<action id="org.gnome.GdmSettings.SetAllowedUID">
<description>Manage Gdm Settings</description>
<message>Authentication is required to change GDM data</message>
<defaults>
<allow_any>no</allow_any>
<allow_inactive>no</allow_inactive>
<allow_active>auth_admin_keep</allow_active>
</defaults>
</action>
</policyconfig>
安装在
/usr/share/polkit-1/actions
。我尝试使用 auth_admin_keep 和 auth_self_keep,但它总是返回相同的结果。
这是
org.gnome.GdmSettings.service
文件:
[Unit]
Description=GNOME Display Manager Settings
[D-BUS Service]
Name=org.gnome.GdmSettings
Exec=/usr/bin/dbus-launch @daemon@
User=@gdm_user@
(我用
dbus-launch
启动它,因为 dconf 需要会话 dbus 允许设置密钥)。
好的,我找到了解决方案:我必须添加
<annotate key="org.freedesktop.policykit.owner">unix-user:@gdm_user@</annotate>
到 .policy 文件。
您能否澄清一下您是如何成功实现它的。我正在尝试在基于 yocto 的 Linux 映像中执行此操作,您能否指定步骤以成功正确实现它?守护程序文件,是什么类型的文件?以及您应该拥有哪些文件才能正确实施它
谢谢你