From d6b0e741e15d2f936d9bd21645982c60ff7bb89e Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Mon, 8 Feb 2010 17:46:11 +0100 Subject: [PATCH 2/3] Add {Suspend,Hibernate}Allowed D-Bus methods Add two D-Bus server methods to check whether the caller has the privilege to suspend or hibernate. This enables us to check for PK privileges in UpClient's can_{suspend,hibernate} properties, so that clients like gnome-session or gnome-power-manager hide the suspend/hibernate related actions if the admin or OEM disabled suspend/hibernate through a PolicyKit .pkla file. https://bugs.freedesktop.org/show_bug.cgi?id=26473 --- src/org.freedesktop.UPower.xml | 33 ++++++++++++++++++++ src/up-daemon.c | 64 ++++++++++++++++++++++++++++++++++++++++ src/up-daemon.h | 4 ++ 3 files changed, 101 insertions(+), 0 deletions(-) diff --git a/src/org.freedesktop.UPower.xml b/src/org.freedesktop.UPower.xml index cf32edc..a4066ff 100644 --- a/src/org.freedesktop.UPower.xml +++ b/src/org.freedesktop.UPower.xml @@ -195,6 +195,23 @@ method return sender=:1.386 -> dest=:1.451 reply_serial=2 + + + + TRUE if allowed, otherwise FALSE + + + + + Check if the caller has (or can get) the PolicyKit privilege to call + Suspend. + + + + + + + @@ -216,6 +233,22 @@ method return sender=:1.386 -> dest=:1.451 reply_serial=2 + + + + + + TRUE if allowed, otherwise FALSE + + + + + Check if the caller has (or can get) the PolicyKit privilege to call + Hibernate. + + + + diff --git a/src/up-daemon.c b/src/up-daemon.c index ac6e937..3f58ae9 100644 --- a/src/up-daemon.c +++ b/src/up-daemon.c @@ -659,6 +659,38 @@ out: } /** + * up_daemon_suspend_allowed: + **/ +gboolean +up_daemon_suspend_allowed (UpDaemon *daemon, DBusGMethodInvocation *context) +{ + gboolean ret; + GError *error; + PolkitSubject *subject = NULL; + + /* no kernel support */ + if (!daemon->priv->kernel_can_suspend) { + error = g_error_new (UP_DAEMON_ERROR, + UP_DAEMON_ERROR_GENERAL, + "No kernel support"); + dbus_g_method_return_error (context, error); + goto out; + } + + subject = up_polkit_get_subject (daemon->priv->polkit, context); + if (subject == NULL) + goto out; + + ret = up_polkit_is_allowed (daemon->priv->polkit, subject, "org.freedesktop.upower.suspend", context); + dbus_g_method_return (context, ret); + +out: + if (subject != NULL) + g_object_unref (subject); + return TRUE; +} + +/** * up_daemon_hibernate: **/ gboolean @@ -719,6 +751,38 @@ out: } /** + * up_daemon_hibernate_allowed: + **/ +gboolean +up_daemon_hibernate_allowed (UpDaemon *daemon, DBusGMethodInvocation *context) +{ + gboolean ret; + GError *error; + PolkitSubject *subject = NULL; + + /* no kernel support */ + if (!daemon->priv->kernel_can_hibernate) { + error = g_error_new (UP_DAEMON_ERROR, + UP_DAEMON_ERROR_GENERAL, + "No kernel support"); + dbus_g_method_return_error (context, error); + goto out; + } + + subject = up_polkit_get_subject (daemon->priv->polkit, context); + if (subject == NULL) + goto out; + + ret = up_polkit_is_allowed (daemon->priv->polkit, subject, "org.freedesktop.upower.hibernate", context); + dbus_g_method_return (context, ret); + +out: + if (subject != NULL) + g_object_unref (subject); + return TRUE; +} + +/** * up_daemon_register_power_daemon: **/ static gboolean diff --git a/src/up-daemon.h b/src/up-daemon.h index c919a91..d2bcf91 100644 --- a/src/up-daemon.h +++ b/src/up-daemon.h @@ -85,8 +85,12 @@ gboolean up_daemon_suspend (UpDaemon *daemon, DBusGMethodInvocation *context); gboolean up_daemon_about_to_sleep (UpDaemon *daemon, DBusGMethodInvocation *context); +gboolean up_daemon_suspend_allowed (UpDaemon *daemon, + DBusGMethodInvocation *context); gboolean up_daemon_hibernate (UpDaemon *daemon, DBusGMethodInvocation *context); +gboolean up_daemon_hibernate_allowed (UpDaemon *daemon, + DBusGMethodInvocation *context); gboolean up_daemon_can_suspend (UpDaemon *daemon, gboolean interactive, DBusGMethodInvocation *context); -- 1.6.5