Commit 3fee2254 authored by David Zeuthen's avatar David Zeuthen

Port core bits to gdbus

Signed-off-by: default avatarDavid Zeuthen <davidz@redhat.com>
parent 779c0153
......@@ -34,13 +34,23 @@
#include <polkit/polkit.h>
static gboolean
on_tensec_timeout (gpointer user_data)
{
GMainLoop *loop = user_data;
g_print ("Ten seconds has passed. Now exiting.\n");
g_main_loop_quit (loop);
return FALSE;
}
static void
check_authorization_cb (PolkitAuthority *authority,
GAsyncResult *res,
GMainLoop *loop)
gpointer user_data)
{
GError *error;
GMainLoop *loop = user_data;
PolkitAuthorizationResult *result;
GError *error;
error = NULL;
result = polkit_authority_check_authorization_finish (authority, res, &error);
......@@ -68,7 +78,9 @@ check_authorization_cb (PolkitAuthority *authority,
g_print ("Authorization result: %s\n", result_str);
}
g_main_loop_quit (loop);
g_print ("Authorization check has been cancelled and the dialog should now be hidden.\n"
"This process will exit in ten seconds.\n");
g_timeout_add (10000, on_tensec_timeout, loop);
}
static gboolean
......
......@@ -71,7 +71,8 @@ polkit_action_description_finalize (GObject *object)
action_description = POLKIT_ACTION_DESCRIPTION (object);
g_object_unref (action_description->real);
if (action_description->real != NULL)
g_object_unref (action_description->real);
g_strfreev (action_description->annotation_keys);
......@@ -302,3 +303,88 @@ polkit_action_description_get_annotation_keys (PolkitActionDescription *action_d
out:
return (const gchar * const *) action_description->annotation_keys;
}
PolkitActionDescription *
polkit_action_description_new_for_gvariant (GVariant *value)
{
PolkitActionDescription *action_description;
GVariantIter iter;
const gchar *action_id;
const gchar *description;
const gchar *message;
const gchar *vendor_name;
const gchar *vendor_url;
const gchar *icon_name;
PolkitImplicitAuthorization implicit_any;
PolkitImplicitAuthorization implicit_inactive;
PolkitImplicitAuthorization implicit_active;
GVariant *annotations_dict;
gchar *a_key;
gchar *a_value;
EggDBusHashMap *hm;
action_description = POLKIT_ACTION_DESCRIPTION (g_object_new (POLKIT_TYPE_ACTION_DESCRIPTION, NULL));
g_variant_get (value,
"(&s&s&s&s&s&suuu@a{ss})",
&action_id,
&description,
&message,
&vendor_name,
&vendor_url,
&icon_name,
&implicit_any,
&implicit_inactive,
&implicit_active,
&annotations_dict);
hm = egg_dbus_hash_map_new (G_TYPE_STRING, g_free, G_TYPE_STRING, g_free);
g_variant_iter_init (&iter, annotations_dict);
while (g_variant_iter_next (&iter, "{ss}", &a_key, &a_value))
egg_dbus_hash_map_insert (hm, a_key, a_value);
g_variant_unref (annotations_dict);
action_description->real = _polkit_action_description_new (action_id, description, message, vendor_name, vendor_url, icon_name, implicit_any, implicit_inactive, implicit_active, hm);
g_object_unref (hm);
return action_description;
}
static gboolean
add_annotation (EggDBusHashMap *hash_map,
gpointer key,
gpointer value,
gpointer user_data)
{
GVariantBuilder *builder = user_data;
g_variant_builder_add (builder, "{ss}", key, value);
return FALSE;
}
GVariant *
polkit_action_description_to_gvariant (PolkitActionDescription *action_description)
{
GVariant *value;
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
egg_dbus_hash_map_foreach (_polkit_action_description_get_annotations (action_description->real),
add_annotation,
&builder);
/* TODO: note 'foo ? : ""' is a gcc specific extension (it's a short-hand for 'foo ? foo : ""') */
value = g_variant_new ("(ssssssuuua{ss})",
_polkit_action_description_get_action_id (action_description->real) ? : "",
_polkit_action_description_get_description (action_description->real) ? : "",
_polkit_action_description_get_message (action_description->real) ? : "",
_polkit_action_description_get_vendor_name (action_description->real) ? : "",
_polkit_action_description_get_vendor_url (action_description->real) ? : "",
_polkit_action_description_get_icon_name (action_description->real) ? : "",
_polkit_action_description_get_implicit_any (action_description->real),
_polkit_action_description_get_implicit_inactive (action_description->real),
_polkit_action_description_get_implicit_active (action_description->real),
&builder);
return value;
}
This diff is collapsed.
......@@ -302,3 +302,43 @@ polkit_authorization_result_get_locked_down (PolkitAuthorizationResult *result)
return ret;
}
PolkitAuthorizationResult *
polkit_authorization_result_new_for_gvariant (GVariant *value)
{
gboolean is_authorized;
gboolean is_challenge;
GVariant *dict;
PolkitDetails *details;
PolkitAuthorizationResult *ret;
g_variant_get (value,
"(bb@a{ss})",
&is_authorized,
&is_challenge,
&dict);
details = polkit_details_new_for_gvariant (dict);
g_variant_unref (dict);
ret = polkit_authorization_result_new (is_authorized, is_challenge, details);
g_object_unref (details);
return ret;
}
GVariant *
polkit_authorization_result_to_gvariant (PolkitAuthorizationResult *authorization_result)
{
GVariant *ret;
GVariant *details_gvariant;
details_gvariant = polkit_details_to_gvariant (polkit_authorization_result_get_details (authorization_result));
g_variant_ref_sink (details_gvariant);
ret = g_variant_new ("(bb@a{ss})",
polkit_authorization_result_get_is_authorized (authorization_result),
polkit_authorization_result_get_is_challenge (authorization_result),
details_gvariant);
g_variant_unref (details_gvariant);
return ret;
}
......@@ -188,3 +188,43 @@ polkit_details_get_keys (PolkitDetails *details)
return ret;
}
GVariant *
polkit_details_to_gvariant (PolkitDetails *details)
{
GVariant *ret;
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
if (details != NULL && details->hash != NULL)
{
GHashTableIter hash_iter;
const gchar *key;
const gchar *value;
g_hash_table_iter_init (&hash_iter, details->hash);
while (g_hash_table_iter_next (&hash_iter, (gpointer) &key, (gpointer) &value))
g_variant_builder_add (&builder, "{ss}", key, value);
}
ret = g_variant_builder_end (&builder);
return ret;
}
PolkitDetails *
polkit_details_new_for_gvariant (GVariant *value)
{
PolkitDetails *ret;
GHashTable *hash;
GVariantIter iter;
gchar *hash_key;
gchar *hash_value;
hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_variant_iter_init (&iter, value);
while (g_variant_iter_next (&iter, "{ss}", &hash_key, &hash_value))
g_hash_table_insert (hash, hash_key, hash_value);
ret = polkit_details_new_for_hash (hash);
g_hash_table_unref (hash);
return ret;
}
......@@ -34,9 +34,22 @@
* Error codes.
*/
static const GDBusErrorEntry polkit_error_entries[] =
{
{POLKIT_ERROR_FAILED, "org.freedesktop.PolicyKit1.Error.Failed"},
{POLKIT_ERROR_CANCELLED, "org.freedesktop.PolicyKit1.Error.Cancelled"},
{POLKIT_ERROR_NOT_SUPPORTED, "org.freedesktop.PolicyKit1.Error.NotSupported"},
{POLKIT_ERROR_NOT_AUTHORIZED, "org.freedesktop.PolicyKit1.Error.NotAuthorized"},
};
GQuark
polkit_error_quark (void)
{
return _polkit_error_quark ();
static volatile gsize quark_volatile = 0;
g_dbus_error_register_error_domain ("polkit-error-quark",
&quark_volatile,
polkit_error_entries,
G_N_ELEMENTS (polkit_error_entries));
G_STATIC_ASSERT (G_N_ELEMENTS (polkit_error_entries) - 1 == POLKIT_ERROR_NOT_AUTHORIZED);
return (GQuark) quark_volatile;
}
......@@ -259,3 +259,125 @@ polkit_identity_get_real (PolkitIdentity *identity)
return real;
}
GVariant *
polkit_identity_to_gvariant (PolkitIdentity *identity)
{
g_assert_not_reached ();
return NULL;
}
static GVariant *
lookup_asv (GVariant *dict,
const gchar *given_key,
const GVariantType *given_type,
GError **error)
{
GVariantIter iter;
const gchar *key;
GVariant *value;
GVariant *ret;
ret = NULL;
g_variant_iter_init (&iter, dict);
while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
{
if (g_strcmp0 (key, given_key) == 0)
{
if (!g_variant_is_of_type (value, given_type))
{
gchar *type_string;
type_string = g_variant_type_dup_string (given_type);
g_set_error (error,
POLKIT_ERROR,
POLKIT_ERROR_FAILED,
"Value for key `%s' found but is of type %s and type %s was expected",
given_key,
g_variant_get_type_string (value),
type_string);
g_free (type_string);
goto out;
}
ret = value;
goto out;
}
g_variant_unref (value);
}
out:
if (ret == NULL)
{
gchar *type_string;
type_string = g_variant_type_dup_string (given_type);
g_set_error (error,
POLKIT_ERROR,
POLKIT_ERROR_FAILED,
"Didn't find value for key `%s' of type %s",
given_key,
type_string);
g_free (type_string);
}
return ret;
}
PolkitIdentity *
polkit_identity_new_for_gvariant (GVariant *variant,
GError **error)
{
PolkitIdentity *ret;
const gchar *kind;
GVariant *details_gvariant;
ret = NULL;
g_variant_get (variant,
"(&s@a{sv})",
&kind,
&details_gvariant);
if (g_strcmp0 (kind, "unix-user") == 0)
{
GVariant *v;
guint32 uid;
v = lookup_asv (details_gvariant, "uid", G_VARIANT_TYPE_UINT32, error);
if (v == NULL)
{
g_prefix_error (error, "Error parsing unix-user identity: ");
goto out;
}
uid = g_variant_get_uint32 (v);
g_variant_unref (v);
ret = polkit_unix_user_new (uid);
}
else if (g_strcmp0 (kind, "unix-group") == 0)
{
GVariant *v;
guint32 gid;
v = lookup_asv (details_gvariant, "gid", G_VARIANT_TYPE_UINT32, error);
if (v == NULL)
{
g_prefix_error (error, "Error parsing unix-user identity: ");
goto out;
}
gid = g_variant_get_uint32 (v);
g_variant_unref (v);
ret = polkit_unix_group_new (gid);
}
else
{
g_set_error (error,
POLKIT_ERROR,
POLKIT_ERROR_FAILED,
"Unknown identity of kind `%s'",
kind);
}
out:
g_variant_unref (details_gvariant);
return ret;
}
......@@ -38,6 +38,14 @@
PolkitActionDescription *polkit_action_description_new_for_real (_PolkitActionDescription *real);
_PolkitActionDescription *polkit_action_description_get_real (PolkitActionDescription *action_description);
PolkitActionDescription *polkit_action_description_new_for_gvariant (GVariant *value);
GVariant *polkit_action_description_to_gvariant (PolkitActionDescription *action_description);
GVariant *polkit_subject_to_gvariant (PolkitSubject *subject);
GVariant *polkit_identity_to_gvariant (PolkitIdentity *identity);
PolkitSubject *polkit_subject_new_for_gvariant (GVariant *variant, GError **error);
PolkitIdentity *polkit_identity_new_for_gvariant (GVariant *variant, GError **error);
PolkitSubject *polkit_subject_new_for_real (_PolkitSubject *real);
_PolkitSubject *polkit_subject_get_real (PolkitSubject *subject);
......@@ -47,11 +55,17 @@ _PolkitIdentity *polkit_identity_get_real (PolkitIdentity *identity);
PolkitAuthorizationResult *polkit_authorization_result_new_for_real (_PolkitAuthorizationResult *real);
_PolkitAuthorizationResult *polkit_authorization_result_get_real (PolkitAuthorizationResult *authorization_result);
PolkitAuthorizationResult *polkit_authorization_result_new_for_gvariant (GVariant *value);
GVariant *polkit_authorization_result_to_gvariant (PolkitAuthorizationResult *authorization_result);
_PolkitTemporaryAuthorization *polkit_temporary_authorization_get_real (PolkitTemporaryAuthorization *authorization);
PolkitTemporaryAuthorization *polkit_temporary_authorization_new_for_real (_PolkitTemporaryAuthorization *real);
PolkitTemporaryAuthorization *polkit_temporary_authorization_new_for_gvariant (GVariant *value);
GVariant *polkit_temporary_authorization_to_gvariant (PolkitTemporaryAuthorization *authorization);
PolkitDetails *polkit_details_new_for_hash (GHashTable *hash);
GHashTable *polkit_details_get_hash (PolkitDetails *details);
GVariant *polkit_details_to_gvariant (PolkitDetails *details);
PolkitDetails *polkit_details_new_for_gvariant (GVariant *value);
#endif /* __POLKIT_PRIVATE_H */
......@@ -360,3 +360,193 @@ polkit_subject_get_real (PolkitSubject *subject)
return real;
}
GVariant *
polkit_subject_to_gvariant (PolkitSubject *subject)
{
GVariantBuilder builder;
GVariant *dict;
GVariant *ret;
const gchar *kind;
kind = "";
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
if (POLKIT_IS_UNIX_PROCESS (subject))
{
kind = "unix-process";
g_variant_builder_add (&builder, "{sv}", "pid",
g_variant_new_uint32 (polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))));
g_variant_builder_add (&builder, "{sv}", "start-time",
g_variant_new_uint64 (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject))));
}
else if (POLKIT_IS_UNIX_SESSION (subject))
{
kind = "unix-process";
g_variant_builder_add (&builder, "{sv}", "session-id",
g_variant_new_string (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject))));
}
else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
{
kind = "system-bus-name";
g_variant_builder_add (&builder, "{sv}", "name",
g_variant_new_string (polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))));
}
else
{
g_warning ("Unknown class %s implementing PolkitSubject", g_type_name (G_TYPE_FROM_INSTANCE (subject)));
}
dict = g_variant_builder_end (&builder);
ret = g_variant_new ("(s@a{sv})", kind, dict);
return ret;
}
static GVariant *
lookup_asv (GVariant *dict,
const gchar *given_key,
const GVariantType *given_type,
GError **error)
{
GVariantIter iter;
const gchar *key;
GVariant *value;
GVariant *ret;
ret = NULL;
g_variant_iter_init (&iter, dict);
while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
{
if (g_strcmp0 (key, given_key) == 0)
{
if (!g_variant_is_of_type (value, given_type))
{
gchar *type_string;
type_string = g_variant_type_dup_string (given_type);
g_set_error (error,
POLKIT_ERROR,
POLKIT_ERROR_FAILED,
"Value for key `%s' found but is of type %s and type %s was expected",
given_key,
g_variant_get_type_string (value),
type_string);
g_free (type_string);
goto out;
}
ret = value;
goto out;
}
g_variant_unref (value);
}
out:
if (ret == NULL)
{
gchar *type_string;
type_string = g_variant_type_dup_string (given_type);
g_set_error (error,
POLKIT_ERROR,
POLKIT_ERROR_FAILED,
"Didn't find value for key `%s' of type %s",
given_key,
type_string);
g_free (type_string);
}
return ret;
}
PolkitSubject *
polkit_subject_new_for_gvariant (GVariant *variant,
GError **error)
{
PolkitSubject *ret;
const gchar *kind;
GVariant *details_gvariant;
ret = NULL;
g_variant_get (variant,
"(&s@a{sv})",
&kind,
&details_gvariant);
if (g_strcmp0 (kind, "unix-process") == 0)
{
GVariant *v;
guint32 pid;
guint64 start_time;
v = lookup_asv (details_gvariant, "pid", G_VARIANT_TYPE_UINT32, error);
if (v == NULL)
{
g_prefix_error (error, "Error parsing unix-process subject: ");
goto out;
}
pid = g_variant_get_uint32 (v);
g_variant_unref (v);
v = lookup_asv (details_gvariant, "start-time", G_VARIANT_TYPE_UINT64, error);
if (v == NULL)
{
g_prefix_error (error, "Error parsing unix-process subject: ");
goto out;
}
start_time = g_variant_get_uint64 (v);
g_variant_unref (v);
ret = polkit_unix_process_new_full (pid, start_time);
}
else if (g_strcmp0 (kind, "unix-session") == 0)
{
GVariant *v;
const gchar *session_id;
v = lookup_asv (details_gvariant, "session-id", G_VARIANT_TYPE_STRING, error);
if (v == NULL)
{
g_prefix_error (error, "Error parsing unix-session subject: ");
goto out;
}
session_id = g_variant_get_string (v, NULL);
ret = polkit_unix_session_new (session_id);
g_variant_unref (v);
}
else if (g_strcmp0 (kind, "system-bus-name") == 0)
{
GVariant *v;
const gchar *name;
v = lookup_asv (details_gvariant, "name", G_VARIANT_TYPE_STRING, error);
if (v == NULL)
{
g_prefix_error (error, "Error parsing system-bus-name subject: ");
goto out;
}
name = g_variant_get_string (v, NULL);
if (!g_dbus_is_unique_name (name))
{
g_set_error (error,
POLKIT_ERROR,
POLKIT_ERROR_FAILED,
"Error parsing system-bus-name subject: `%s' is not a valid unique name",
name);
goto out;
}
ret = polkit_system_bus_name_new (name);
g_variant_unref (v);
}
else
{
g_set_error (error,
POLKIT_ERROR,
POLKIT_ERROR_FAILED,
"Unknown subject of kind `%s'",
kind);
}
out:
g_variant_unref (details_gvariant);
return ret;
}
......@@ -209,3 +209,43 @@ polkit_temporary_authorization_get_time_expires (PolkitTemporaryAuthorization *a
{
return _polkit_temporary_authorization_get_time_expires (authorization->real);
}
PolkitTemporaryAuthorization *
polkit_temporary_authorization_new_for_gvariant (GVariant *value)
{
g_assert_not_reached ();
return NULL;
}
GVariant *
polkit_temporary_authorization_to_gvariant (PolkitTemporaryAuthorization *authorization)
{
const gchar *id;
const gchar *action_id;
PolkitSubject *subject;
guint64 time_obtained;
guint64 time_expires;
GVariant *ret;
GVariant *subject_gvariant;
id = polkit_temporary_authorization_get_id (authorization);
action_id = polkit_temporary_authorization_get_action_id (authorization);
subject = polkit_temporary_authorization_get_subject (authorization);
time_obtained = polkit_temporary_authorization_get_time_obtained (authorization);
time_expires = polkit_temporary_authorization_get_time_expires (authorization);
subject_gvariant = polkit_subject_to_gvariant (subject);
g_variant_ref_sink (subject_gvariant);
ret = g_variant_new ("(ss@(sa{sv})tt)",
id,
action_id,
subject_gvariant,
time_obtained,
time_expires);
g_variant_unref (subject_gvariant);
g_object_unref (subject);
return ret;
}
This diff is collapsed.
......@@ -51,7 +51,7 @@ main (int argc, char **argv)
"/org/freedesktop/PolicyKit1/Authority",
&error))
{
g_printerr ("Error registering authority: %s", error->message);
g_printerr ("Error registering authority: %s\n", error->message);
g_error_free (error);
goto out;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment