From 04be0a63129e77d05b6e140218829412e967a8a4 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 4 Feb 2010 23:28:52 -0500 Subject: [PATCH 17/22] Refactor get_polkit_permissions Refactor get_polkit_permissions to take a callback, and make check_polkit_permissions take the same callback. --- src/ck-manager.c | 218 +++++++++++++++++++++++++----------------------------- 1 files changed, 101 insertions(+), 117 deletions(-) diff --git a/src/ck-manager.c b/src/ck-manager.c index 3db2f6a..58dccaa 100644 --- a/src/ck-manager.c +++ b/src/ck-manager.c @@ -741,91 +741,65 @@ get_cookie_for_pid (CkManager *manager, return cookie; } -typedef void (*AuthorizedCallback) (CkManager *manager, - DBusGMethodInvocation *context); +typedef struct _AuthReady AuthReady; -typedef struct -{ - CkManager *manager; - DBusGMethodInvocation *context; - AuthorizedCallback callback; -} AuthorizedCallbackData; +typedef void (*AuthReadyCallback) (AuthReady *ready, + gint result, + GError *error); -static void -data_free (AuthorizedCallbackData *data) -{ - g_object_unref (data->manager); - g_free (data); -} +struct _AuthReady { + CkManager *manager; + DBusGMethodInvocation *context; + AuthReadyCallback callback; +}; #ifdef HAVE_POLKIT static void -auth_ready_callback (PolkitAuthority *authority, - GAsyncResult *res, - AuthorizedCallbackData *data) +ready_cb (PolkitAuthority *authority, + GAsyncResult *res, + AuthReady *ready) { + PolkitAuthorizationResult *ret; GError *error; - GError *error2; - PolkitAuthorizationResult *result; error = NULL; - - result = polkit_authority_check_authorization_finish (authority, - res, - &error); + ret = polkit_authority_check_authorization_finish (authority, res, &error); if (error != NULL) { - error2 = g_error_new (CK_MANAGER_ERROR, - CK_MANAGER_ERROR_NOT_PRIVILEGED, - "Not Authorized: %s", error->message); - dbus_g_method_return_error (data->context, error2); - g_error_free (error2); + ready->callback (ready, -1, error); g_error_free (error); } - else if (polkit_authorization_result_get_is_authorized (result)) { - data->callback (data->manager, data->context); + else if (polkit_authorization_result_get_is_authorized (ret)) { + ready->callback (ready, 2, NULL); } - else if (polkit_authorization_result_get_is_challenge (result)) { - error = g_error_new (CK_MANAGER_ERROR, - CK_MANAGER_ERROR_NOT_PRIVILEGED, - "Authorization is required"); - dbus_g_method_return_error (data->context, error); - g_error_free (error); + else if (polkit_authorization_result_get_is_challenge (ret)) { + ready->callback (ready, 1, NULL); } else { - error = g_error_new (CK_MANAGER_ERROR, - CK_MANAGER_ERROR_NOT_PRIVILEGED, - "Not Authorized"); - dbus_g_method_return_error (data->context, error); - g_error_free (error); + ready->callback (ready, 0, NULL); } - - g_object_unref (result); - - data_free (data); + g_free (ready); + g_object_unref (ret); } static void check_polkit_permissions (CkManager *manager, DBusGMethodInvocation *context, const char *action, - AuthorizedCallback callback) + AuthReadyCallback callback) { const char *sender; PolkitSubject *subject; - AuthorizedCallbackData *data; + AuthReady *ready; - g_debug ("constructing polkit data"); - - /* Check that caller is privileged */ + g_debug ("checking if caller %s is authorized", sender); + sender = dbus_g_method_get_sender (context); subject = polkit_system_bus_name_new (sender); - g_debug ("checking if caller %s is authorized", sender); - - data = g_new0 (AuthorizedCallbackData, 1); - data->manager = g_object_ref (manager); - data->context = context; - data->callback = callback; + ready = g_new0 (AuthReady, 1); + ready->manager = manager; + ready->context = context; + ready->callback = callback; polkit_authority_check_authorization (manager->priv->pol_ctx, subject, @@ -833,51 +807,31 @@ check_polkit_permissions (CkManager *manager, NULL, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, NULL, - (GAsyncReadyCallback)auth_ready_callback, - data); + (GAsyncReadyCallback) ready_cb, + ready); g_object_unref (subject); } static void -ready_cb (PolkitAuthority *authority, - GAsyncResult *res, - DBusGMethodInvocation *context) -{ - PolkitAuthorizationResult *ret; - GError *error; - - error = NULL; - ret = polkit_authority_check_authorization_finish (authority, res, &error); - if (error != NULL) { - dbus_g_method_return_error (context, error); - g_error_free (error); - } - else if (polkit_authorization_result_get_is_authorized (ret)) { - dbus_g_method_return (context, TRUE); - } - else if (polkit_authorization_result_get_is_challenge (ret)) { - dbus_g_method_return (context, TRUE); - } - else { - dbus_g_method_return (context, FALSE); - } - - g_object_unref (ret); -} - -static void -get_polkit_permissions (CkManager *manager, - const char *action, - DBusGMethodInvocation *context) +get_polkit_permissions (CkManager *manager, + DBusGMethodInvocation *context, + const char *action, + AuthReadyCallback callback) { const char *sender; PolkitSubject *subject; + AuthReady *ready; g_debug ("get permissions for action %s", action); - sender = dbus_g_method_get_sender (context); + sender = dbus_g_method_get_sender (ready->context); subject = polkit_system_bus_name_new (sender); + ready = g_new0 (AuthReady, 1); + ready->manager = manager; + ready->context = context; + ready->callback = callback; + polkit_authority_check_authorization (manager->priv->pol_ctx, subject, action, @@ -885,7 +839,7 @@ get_polkit_permissions (CkManager *manager, 0, NULL, (GAsyncReadyCallback) ready_cb, - context); + ready); g_object_unref (subject); } #endif @@ -1044,13 +998,14 @@ static gboolean check_rbac_permissions (CkManager *manager, DBusGMethodInvocation *context, const char *action, - AuthorizedCallback callback) + AuthReadyCallback callback) { const char *sender; char *username; gboolean res; uid_t uid; pid_t pid; + AuthReady *ready; username = NULL; sender = dbus_g_method_get_sender (context); @@ -1071,7 +1026,6 @@ check_rbac_permissions (CkManager *manager, } out: - if (res == TRUE) { g_debug ("User %s has RBAC permission to stop/restart", username); } else { @@ -1080,9 +1034,15 @@ out: g_free (username); - if (res && callback) { - callback (manager, context); - } + ready = g_new (AuthReady, 1); + ready->manager = manager; + ready->context = context; + ready->callback = callback; + + if (callback) + callback (ready, res ? 2 : 0, NULL); + + g_free (ready); return res; } @@ -1105,18 +1065,31 @@ cleanup_after_stop (CkManager *manager) } static void -do_stop (CkManager *manager, - DBusGMethodInvocation *context) +do_stop (AuthReady *ready, + gint result, + GError *error) { - GError *error; - gboolean res; + gboolean res; - g_debug ("ConsoleKit preforming %s", manager->priv->restart ? "Restart" : "Stop"); + if (result < 0) { + GError *new_error; - log_system_stop_event (manager); + new_error = g_error_new (CK_MANAGER_ERROR, + CK_MANAGER_ERROR_NOT_PRIVILEGED, + "Not Authorized: %s", new_error->message); + dbus_g_method_return_error (ready->context, new_error); + g_error_free (new_error); + cleanup_after_stop (ready->manager); + + return; + } + + g_debug ("ConsoleKit preforming %s", ready->manager->priv->restart ? "Restart" : "Stop"); + + log_system_stop_event (ready->manager); error = NULL; - res = g_spawn_command_line_async (manager->priv->restart + res = g_spawn_command_line_async (ready->manager->priv->restart ? PREFIX "/lib/ConsoleKit/scripts/ck-system-restart" : PREFIX "/lib/ConsoleKit/scripts/ck-system-stop", &error); @@ -1128,15 +1101,16 @@ do_stop (CkManager *manager, new_error = g_error_new (CK_MANAGER_ERROR, CK_MANAGER_ERROR_GENERAL, "Unable to restart/stop system: %s", error->message); - dbus_g_method_return_error (context, new_error); + dbus_g_method_return_error (ready->context, new_error); g_error_free (new_error); - g_error_free (error); + cleanup_after_stop (ready->manager); + + return; - cleanup_after_stop (manager); - } else { - dbus_g_method_return (context); } + + dbus_g_method_return (ready->context); } static gboolean @@ -1202,16 +1176,27 @@ ck_manager_restart (CkManager *manager, return restart_or_stop (manager, context); } +static void +permission_ready (AuthReady *ready, + gint result, + GError *error) +{ + if (result < 0) { + dbus_g_method_return_error (ready->context, error); + } else { + dbus_g_method_return (ready->context, result != 0); + } +} + gboolean ck_manager_can_restart (CkManager *manager, DBusGMethodInvocation *context) { - const char *action; - - action = "org.freedesktop.consolekit.system.restart"; - #if defined HAVE_POLKIT - get_polkit_permissions (manager, action, context); + get_polkit_permissions (manager, + context, + "org.freedesktop.consolekit.system.restart", + permission_ready); #elif defined ENABLE_RBAC_SHUTDOWN if (check_rbac_permissions (manager, context, RBAC_SHUTDOWN_KEY, NULL)) { @@ -1238,12 +1223,11 @@ gboolean ck_manager_can_stop (CkManager *manager, DBusGMethodInvocation *context) { - const char *action; - - action = "org.freedesktop.consolekit.system.stop"; - #if defined HAVE_POLKIT - get_polkit_permissions (manager, action, context); + get_polkit_permissions (manager, + context, + "org.freedesktop.consolekit.system.stop", + permission_ready); #elif defined ENABLE_RBAC_SHUTDOWN if (check_rbac_permissions (manager, context, RBAC_SHUTDOWN_KEY, NULL)) { -- 1.6.6