From d90f9f6a14d8c9cd7a86caebb9bd69879fad067e Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 13 Jul 2012 15:05:21 +0200 Subject: [PATCH] WIP: Migrate from libgnome-keyring to libsecret * See: https://live.gnome.org/GnomeGoals/LibsecretMigration --- configure.ac | 8 +-- src/Makefile.am | 4 +- src/mcd-account-manager-default.c | 127 +++++++++++++++---------------------- 3 files changed, 57 insertions(+), 82 deletions(-) diff --git a/configure.ac b/configure.ac index ab44945..235b598 100644 --- a/configure.ac +++ b/configure.ac @@ -191,7 +191,7 @@ PKG_PROG_PKG_CONFIG() if test "x$keyring_enabled" = xauto then - PKG_CHECK_EXISTS([gnome-keyring-1], [keyring_enabled=yes], + PKG_CHECK_EXISTS([libsecret-unstable], [keyring_enabled=yes], [keyring_enabled=no]) fi @@ -199,9 +199,9 @@ AM_CONDITIONAL(ENABLE_GNOME_KEYRING, [test x$keyring_enabled = xyes]) if test "x$keyring_enabled" = xyes then - PKG_CHECK_MODULES([GNOME_KEYRING], [gnome-keyring-1]) - AC_SUBST([GNOME_KEYRING_LIBS]) - AC_SUBST([GNOME_KEYRING_CFLAGS]) + PKG_CHECK_MODULES([LIBSECRET], [libsecret-unstable]) + AC_SUBST([LIBSECRET_LIBS]) + AC_SUBST([LIBSECRET_CFLAGS]) AC_DEFINE([ENABLE_GNOME_KEYRING], [1], [Define whether gnome-keyring support is enabled]) else AC_DEFINE([ENABLE_GNOME_KEYRING], [0], [Define whether gnome-keyring support is enabled]) diff --git a/src/Makefile.am b/src/Makefile.am index 0c78fc4..7250efd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,7 @@ INCLUDES = \ AM_CFLAGS = $(ERROR_CFLAGS) if ENABLE_GNOME_KEYRING -INCLUDES += $(GNOME_KEYRING_CFLAGS) +INCLUDES += $(LIBSECRET_CFLAGS) endif mc_headers = \ @@ -95,7 +95,7 @@ libmcd_convenience_la_LIBADD = \ $(NULL) if ENABLE_GNOME_KEYRING -libmcd_convenience_la_LIBADD += $(GNOME_KEYRING_LIBS) +libmcd_convenience_la_LIBADD += $(LIBSECRET_LIBS) endif if ENABLE_LIBACCOUNTS_SSO diff --git a/src/mcd-account-manager-default.c b/src/mcd-account-manager-default.c index deae163..158c2fc 100644 --- a/src/mcd-account-manager-default.c +++ b/src/mcd-account-manager-default.c @@ -30,13 +30,17 @@ #define INITIAL_CONFIG "# Telepathy accounts\n" #if ENABLE_GNOME_KEYRING -#include - -GnomeKeyringPasswordSchema keyring_schema = - { GNOME_KEYRING_ITEM_GENERIC_SECRET, - { { "account", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING }, - { "param", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING }, - { NULL, 0 } } }; +#include + +static const SecretSchema keyring_schema = { + "org.freedesktop.Telepathy.MissionControl", + SECRET_SCHEMA_DONT_MATCH_NAME, + { + { "account", SECRET_SCHEMA_ATTRIBUTE_STRING }, + { "param", SECRET_SCHEMA_ATTRIBUTE_STRING }, + { NULL, 0 } + } +}; typedef struct { @@ -46,14 +50,24 @@ typedef struct } KeyringSetData; static void -_keyring_set_cb (GnomeKeyringResult result, +_keyring_set_cb (GObject *source, + GAsyncResult *result, gpointer data) { KeyringSetData *ksd = data; + GError *error = NULL; - if (result != GNOME_KEYRING_RESULT_OK) - g_warning ("failed to save %s.%s : %s", ksd->account, ksd->name, - gnome_keyring_result_to_message (result)); + if (ksd->set) + secret_password_store_finish (result, &error); + else + secret_password_remove_finish (result, &error); + + if (error != NULL) + { + g_warning ("failed to save %s.%s : %s", ksd->account, ksd->name, + error->message); + g_error_free (error); + } else DEBUG ("%s %s.%s in gnome keyring", ksd->set ? "saved" : "deleted", @@ -91,27 +105,8 @@ _keyring_remove_account (const McpAccountStorage *self, const McpAccountManager *am, const gchar *account) { - GList *i; - GList *items; - GnomeKeyringAttributeList *match = gnome_keyring_attribute_list_new (); - GnomeKeyringResult ok; - - gnome_keyring_attribute_list_append_string (match, "account", account); - - ok = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET, - match, &items); - - if (ok != GNOME_KEYRING_RESULT_OK) - goto finished; - - for (i = items; i != NULL; i = g_list_next (i)) - { - GnomeKeyringFound *found = i->data; - gnome_keyring_item_delete_sync (found->keyring, found->item_id); - } - - finished: - gnome_keyring_attribute_list_free (match); + secret_password_remove_sync (&keyring_schema, NULL, NULL, + "account", account, NULL); } static void @@ -144,8 +139,8 @@ _keyring_commit_one (const McdAccountManagerDefault *amd, ksd->name = g_strdup (keys[j]); ksd->set = TRUE; - gnome_keyring_store_password (&keyring_schema, NULL, - name, val, _keyring_set_cb, ksd, NULL, + secret_password_store (&keyring_schema, NULL, name, val, NULL, + _keyring_set_cb, ksd, "account", account_name, "param", key, NULL); @@ -169,9 +164,6 @@ _keyring_commit (const McpAccountStorage *self, GHashTableIter iter = { 0 }; gchar *account = NULL; - if (!gnome_keyring_is_available ()) - return; - /* purge any entirely removed accounts */ g_hash_table_iter_init (&iter, amd->removed_accounts); @@ -200,8 +192,8 @@ _keyring_commit (const McpAccountStorage *self, ksd->name = g_strdup (keys[j]); ksd->set = FALSE; - gnome_keyring_delete_password (&keyring_schema, - _keyring_set_cb, ksd, NULL, + secret_password_remove (&keyring_schema, NULL, + _keyring_set_cb, ksd, "account", accts[i], "param", keys[j], NULL); @@ -237,63 +229,46 @@ _get_secrets_from_keyring (const McpAccountStorage *self, const gchar *account) { McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self); - GnomeKeyringResult ok = GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON; - GnomeKeyringAttributeList *match = gnome_keyring_attribute_list_new (); + GError *error = NULL; + GHashTable *match; GList *items = NULL; GList *i; - gnome_keyring_attribute_list_append_string (match, "account", account); - - ok = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET, - match, &items); + match = secret_attributes_build (&keyring_schema, "account", account, NULL); + items = secret_service_search_sync (NULL, &keyring_schema, match, + SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS, + NULL, &error); + g_hash_table_unref (match); - if (ok != GNOME_KEYRING_RESULT_OK) + if (error != NULL) goto finished; for (i = items; i != NULL; i = g_list_next (i)) { - gsize j; - GnomeKeyringFound *entry = i->data; - GnomeKeyringAttributeList *data = entry->attributes; + SecretItem *item = i->data; + const gchar *param = NULL; + SecretValue *secret; - for (j = 0; j < data->len; j++) + secret = secret_item_get_secret (item); + if (secret != NULL) { - GnomeKeyringAttribute *attr = - &(gnome_keyring_attribute_list_index (data, j)); - const gchar *name = attr->name; - const gchar *value = NULL; - const gchar *param = NULL; - - switch (attr->type) - { - case GNOME_KEYRING_ATTRIBUTE_TYPE_STRING: - if (g_strcmp0 ("param", name) == 0) - { - param = attr->value.string; - value = entry->secret; - } - break; - - default: - g_warning ("Unsupported value type for %s.%s", account, name); - } - - if (param != NULL && value != NULL) + param = g_hash_table_lookup (secret_item_get_attributes (item), "param"); + if (param != NULL) { gchar *key = g_strdup_printf ("param-%s", param); - - g_key_file_set_value (amd->secrets, account, key, value); + g_key_file_set_value (amd->secrets, account, key, secret_value_get (secret, NULL)); mcp_account_manager_parameter_make_secret (am, account, key); - g_free (key); } + secret_value_unref (secret); } } - gnome_keyring_found_list_free (items); + g_list_free_full (items, g_object_unref); finished: - gnome_keyring_attribute_list_free (match); + g_error_free (error); + g_hash_table_unref (match); } #else -- 1.7.10.4