/** * polkit_backend_session_monitor_get_user: * @monitor: A #PolkitBackendSessionMonitor. * @subject: A #PolkitSubject. * @error: Return location for error. * * Gets the user corresponding to @subject or %NULL if no user exists. * * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref(). */ PolkitIdentity * polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor, PolkitSubject *subject, GError **error) { PolkitIdentity *ret; guint32 uid; ret = NULL; if (POLKIT_IS_UNIX_PROCESS (subject)) { uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject)); if ((gint) uid == -1) { g_set_error (error, POLKIT_ERROR, POLKIT_ERROR_FAILED, "Unix process subject does not have uid set"); goto out; } ret = polkit_unix_user_new (uid); } else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) { ret = (PolkitIdentity*)polkit_system_bus_name_get_user_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error); } else if (POLKIT_IS_UNIX_SESSION (subject)) { if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0) { g_set_error (error, POLKIT_ERROR, POLKIT_ERROR_FAILED, "Error getting uid for session"); goto out; } ret = polkit_unix_user_new (uid); } out: return ret; }
/** * polkit_system_bus_name_get_user_sync: * @system_bus_name: A #PolkitSystemBusName. * @cancellable: (allow-none): A #GCancellable or %NULL. * @error: (allow-none): Return location for error or %NULL. * * Synchronously gets a #PolkitUnixUser object for @system_bus_name; * the calling thread is blocked until a reply is received. * * Returns: (allow-none) (transfer full): A #PolkitUnixUser object or %NULL if @error is set. **/ PolkitUnixUser * polkit_system_bus_name_get_user_sync (PolkitSystemBusName *system_bus_name, GCancellable *cancellable, GError **error) { PolkitUnixUser *ret = NULL; guint32 uid; g_return_val_if_fail (POLKIT_IS_SYSTEM_BUS_NAME (system_bus_name), NULL); g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); if (!polkit_system_bus_name_get_creds_sync (system_bus_name, &uid, NULL, cancellable, error)) goto out; ret = (PolkitUnixUser*)polkit_unix_user_new (uid); out: return ret; }
static gboolean send_dbus_message (const char *cookie, uid_t uid) { PolkitAuthority *authority = NULL; PolkitIdentity *identity = NULL; GError *error = NULL; gboolean ret; ret = FALSE; g_type_init (); authority = polkit_authority_get_sync (NULL, &error); if (!authority) { warnx ("couldn't contact polkit authority: %s", error->message); goto out; } identity = polkit_unix_user_new (uid); if (!polkit_authority_authentication_agent_response_sync (authority, cookie, identity, NULL, &error)) { warnx ("couldn't respond to polkit daemon: %s", error->message); goto out; } ret = TRUE; out: g_clear_error (&error); if (authority) g_object_unref (authority); if (identity) g_object_unref (identity); return ret; }
/** * polkit_backend_session_monitor_get_user: * @monitor: A #PolkitBackendSessionMonitor. * @subject: A #PolkitSubject. * @error: Return location for error. * * Gets the user corresponding to @subject or %NULL if no user exists. * * Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref(). */ PolkitIdentity * polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor, PolkitSubject *subject, GError **error) { PolkitIdentity *ret; guint32 uid; ret = NULL; if (POLKIT_IS_UNIX_PROCESS (subject)) { uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject)); if ((gint) uid == -1) { g_set_error (error, POLKIT_ERROR, POLKIT_ERROR_FAILED, "Unix process subject does not have uid set"); goto out; } ret = polkit_unix_user_new (uid); } else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) { GVariant *result; result = g_dbus_connection_call_sync (monitor->system_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetConnectionUnixUser", g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))), G_VARIANT_TYPE ("(u)"), G_DBUS_CALL_FLAGS_NONE, -1, /* timeout_msec */ NULL, /* GCancellable */ error); if (result == NULL) goto out; g_variant_get (result, "(u)", &uid); g_variant_unref (result); ret = polkit_unix_user_new (uid); } else if (POLKIT_IS_UNIX_SESSION (subject)) { if (sd_session_get_uid (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject)), &uid) < 0) { g_set_error (error, POLKIT_ERROR, POLKIT_ERROR_FAILED, "Error getting uid for session"); goto out; } ret = polkit_unix_user_new (uid); } out: return ret; }