From c013636ab6b9d7156367e3970fee99208ace8d0d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 12 Oct 2009 22:31:49 -0400 Subject: [PATCH 17/21] 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 | 231 ++++++++++++++++++++++++----------------------------- 1 files changed, 105 insertions(+), 126 deletions(-) diff --git a/src/ck-manager.c b/src/ck-manager.c index da2522b..d7afc04 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 @@ -1043,20 +997,18 @@ need_elevated_privileges (CkManager *manager) static void check_rbac_permissions (CkManager *manager, DBusGMethodInvocation *context, - 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); - res = get_caller_info (manager, - sender, - &uid, - &pid); + res = get_caller_info (manager, sender, &uid, &pid); if (!res) { goto out; } @@ -1079,9 +1031,14 @@ out: g_free (username); - if (res) { - callback (manager, context); - } + ready = g_new (AuthReady, 1); + ready->manager = manager; + ready->context = context; + ready->callback = callback; + + callback (ready, res ? 2 : 0, NULL); + + g_free (ready); } #endif @@ -1102,18 +1059,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); @@ -1125,15 +1095,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 @@ -1207,38 +1178,46 @@ ck_manager_stop (CkManager *manager, return restart_or_stop (manager, context); } -gboolean -ck_manager_can_restart (CkManager *manager, - DBusGMethodInvocation *context) - +static void +permission_ready (AuthReady *ready, + gint result, + GError *error) { - const char *action; + if (result < 0) { + dbus_g_method_return_error (ready->context, error); + } else { + dbus_g_method_return (ready->context, result != 0); + } +} - action = "org.freedesktop.consolekit.system.restart"; +gboolean +ck_manager_can_restart (CkManager *manager, + DBusGMethodInvocation *context) +{ #if defined HAVE_POLKIT - get_polkit_permissions (manager, action, context); + get_polkit_permissions (manager, + context, + "org.freedesktop.consolekit.system.restart", + permission_ready); #else dbus_g_method_return (context, TRUE); #endif - return TRUE; } gboolean -ck_manager_can_stop (CkManager *manager, - DBusGMethodInvocation *context) +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); #else dbus_g_method_return (context, TRUE); #endif - return TRUE; } -- 1.6.5.rc2