zephyr: Remove unused defines and headers.
[pidgin-git.git] / libpurple / keyring.c
blob46a23b811ad28ab1b355cc2dfbc653bded5d5df0
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program ; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include <glib.h>
23 #include <string.h>
24 #include "account.h"
25 #include "keyring.h"
26 #include "signals.h"
27 #include "core.h"
28 #include "debug.h"
29 #include "internal.h"
30 #include "plugins.h"
32 struct _PurpleKeyring
34 gchar *name;
35 gchar *id;
36 PurpleKeyringRead read_password;
37 PurpleKeyringSave save_password;
38 PurpleKeyringCancelRequests cancel_requests;
39 PurpleKeyringClose close_keyring;
40 PurpleKeyringImportPassword import_password;
41 PurpleKeyringExportPassword export_password;
42 PurpleKeyringReadSettings read_settings;
43 PurpleKeyringApplySettings apply_settings;
45 gboolean is_closing;
46 gboolean is_cancelling;
47 gboolean close_after_cancel;
50 typedef struct
52 GError *error;
53 PurpleKeyringSetInUseCallback cb;
54 gpointer cb_data;
55 PurpleKeyring *new_kr;
56 PurpleKeyring *old_kr;
59 * We are done when finished is positive and read_outstanding is zero.
61 gboolean finished;
62 int read_outstanding;
64 gboolean abort;
65 gboolean force;
66 gboolean succeeded;
67 } PurpleKeyringChangeTracker;
69 typedef void (*PurpleKeyringDropCallback)(gpointer data);
71 typedef struct
73 PurpleKeyringDropCallback cb;
74 gpointer cb_data;
76 gboolean finished;
77 int drop_outstanding;
78 } PurpleKeyringDropTracker;
80 typedef struct
82 PurpleKeyringSaveCallback cb;
83 gpointer cb_data;
84 } PurpleKeyringSetPasswordData;
86 typedef struct
88 gchar *keyring_id;
89 gchar *mode;
90 gchar *data;
91 } PurpleKeyringFailedImport;
93 static void
94 purple_keyring_change_tracker_free(PurpleKeyringChangeTracker *tracker)
96 if (tracker->error)
97 g_error_free(tracker->error);
98 g_free(tracker);
101 static void
102 purple_keyring_failed_import_free(PurpleKeyringFailedImport *import)
104 g_return_if_fail(import != NULL);
106 g_free(import->keyring_id);
107 g_free(import->mode);
108 purple_str_wipe(import->data);
109 g_free(import);
112 static void
113 purple_keyring_close(PurpleKeyring *keyring);
115 static void
116 purple_keyring_drop_passwords(PurpleKeyring *keyring,
117 PurpleKeyringDropCallback cb, gpointer data);
119 /* A list of available keyrings */
120 static GList *purple_keyring_keyrings = NULL;
122 /* Keyring being used. */
123 static PurpleKeyring *purple_keyring_inuse = NULL;
125 /* Keyring id marked to use (may not be loadable). */
126 static gchar *purple_keyring_to_use = NULL;
128 static guint purple_keyring_pref_cbid = 0;
129 static GList *purple_keyring_loaded_plugins = NULL;
130 static PurpleKeyringChangeTracker *current_change_tracker = NULL;
131 static gboolean purple_keyring_is_quitting = FALSE;
132 static GHashTable *purple_keyring_failed_imports = NULL;
134 static const gchar *
135 purple_keyring_print_account(PurpleAccount *account)
137 static gchar print_buff[100];
139 if (account == NULL) {
140 g_snprintf(print_buff, 100, "(null)");
141 return print_buff;
144 g_snprintf(print_buff, 100, "%s:%s",
145 purple_account_get_protocol_id(account),
146 purple_account_get_username(account));
147 return print_buff;
150 /**************************************************************************/
151 /* Setting used keyrings */
152 /**************************************************************************/
154 PurpleKeyring *
155 purple_keyring_find_keyring_by_id(const gchar *id)
157 GList *it;
159 for (it = purple_keyring_keyrings; it != NULL; it = it->next) {
160 PurpleKeyring *keyring = it->data;
161 const gchar *curr_id = purple_keyring_get_id(keyring);
163 if (g_strcmp0(id, curr_id) == 0)
164 return keyring;
167 return NULL;
170 static void
171 purple_keyring_pref_callback(const gchar *pref, PurplePrefType type,
172 gconstpointer id, gpointer data)
174 PurpleKeyring *new_keyring;
176 g_return_if_fail(g_strcmp0(pref, "/purple/keyring/active") == 0);
177 g_return_if_fail(type == PURPLE_PREF_STRING);
178 g_return_if_fail(id != NULL);
180 new_keyring = purple_keyring_find_keyring_by_id(id);
181 g_return_if_fail(new_keyring != NULL);
183 purple_keyring_set_inuse(new_keyring, FALSE, NULL, NULL);
186 static void
187 purple_keyring_pref_connect(void)
189 g_return_if_fail(purple_keyring_pref_cbid == 0);
191 purple_keyring_pref_cbid = purple_prefs_connect_callback(NULL,
192 "/purple/keyring/active", purple_keyring_pref_callback, NULL);
195 static void
196 purple_keyring_pref_disconnect(void)
198 g_return_if_fail(purple_keyring_pref_cbid != 0);
200 purple_prefs_disconnect_callback(purple_keyring_pref_cbid);
201 purple_keyring_pref_cbid = 0;
204 PurpleKeyring *
205 purple_keyring_get_inuse(void)
207 return purple_keyring_inuse;
210 static void
211 purple_keyring_set_inuse_drop_cb(gpointer _tracker)
213 PurpleKeyringChangeTracker *tracker = _tracker;
215 g_return_if_fail(tracker != NULL);
217 if (tracker->succeeded) {
218 purple_keyring_close(tracker->old_kr);
220 purple_debug_info("keyring", "Successfully changed keyring.\n");
222 purple_keyring_inuse = tracker->new_kr;
223 current_change_tracker = NULL;
225 if (tracker->cb != NULL)
226 tracker->cb(NULL, tracker->cb_data);
227 purple_keyring_change_tracker_free(tracker);
228 return;
231 purple_debug_error("keyring", "Failed to change keyring, aborting.\n");
233 purple_keyring_close(tracker->new_kr);
235 purple_keyring_pref_disconnect();
236 purple_prefs_set_string("/purple/keyring/active",
237 purple_keyring_get_id(tracker->old_kr));
238 purple_keyring_pref_connect();
240 current_change_tracker = NULL;
242 if (tracker->error == NULL) {
243 tracker->error = g_error_new(PURPLE_KEYRING_ERROR,
244 PURPLE_KEYRING_ERROR_UNKNOWN,
245 _("An unknown error has occured."));
248 if (tracker->cb != NULL)
249 tracker->cb(tracker->error, tracker->cb_data);
251 purple_keyring_change_tracker_free(tracker);
254 static void
255 purple_keyring_set_inuse_save_cb(PurpleAccount *account, GError *error,
256 gpointer _tracker)
258 PurpleKeyringChangeTracker *tracker = _tracker;
260 g_return_if_fail(account != NULL);
261 g_return_if_fail(tracker != NULL);
263 tracker->read_outstanding--;
265 if (error == NULL) {
266 /* no error */
267 } else if (g_error_matches(error, PURPLE_KEYRING_ERROR,
268 PURPLE_KEYRING_ERROR_NOPASSWORD))
270 if (purple_debug_is_verbose()) {
271 purple_debug_misc("keyring", "No password found while "
272 "changing keyring for account %s: %s.\n",
273 purple_keyring_print_account(account),
274 error->message);
276 } else if (g_error_matches(error, PURPLE_KEYRING_ERROR,
277 PURPLE_KEYRING_ERROR_ACCESSDENIED))
279 purple_debug_info("keyring", "Access denied while changing "
280 "keyring for account %s: %s.\n",
281 purple_keyring_print_account(account), error->message);
282 tracker->abort = TRUE;
283 if (tracker->error != NULL)
284 g_error_free(tracker->error);
285 tracker->error = g_error_copy(error);
286 } else if (g_error_matches(error, PURPLE_KEYRING_ERROR,
287 PURPLE_KEYRING_ERROR_CANCELLED))
289 purple_debug_info("keyring", "Operation cancelled while "
290 "changing keyring for account %s: %s.\n",
291 purple_keyring_print_account(account), error->message);
292 tracker->abort = TRUE;
293 if (tracker->error == NULL)
294 tracker->error = g_error_copy(error);
295 } else if (g_error_matches(error, PURPLE_KEYRING_ERROR,
296 PURPLE_KEYRING_ERROR_BACKENDFAIL))
298 purple_debug_error("keyring", "Failed to communicate with "
299 "backend while changing keyring for account %s: %s. "
300 "Aborting changes.\n",
301 purple_keyring_print_account(account), error->message);
302 tracker->abort = TRUE;
303 if (tracker->error != NULL)
304 g_error_free(tracker->error);
305 tracker->error = g_error_copy(error);
306 } else {
307 purple_debug_error("keyring", "Unknown error while changing "
308 "keyring for account %s: %s. Aborting changes.\n",
309 purple_keyring_print_account(account), error->message);
310 tracker->abort = TRUE;
311 if (tracker->error == NULL)
312 tracker->error = g_error_copy(error);
315 purple_signal_emit(purple_keyring_get_handle(), "password-migration",
316 account);
318 if (!tracker->finished || tracker->read_outstanding > 0)
319 return;
321 /* This was the last one. */
322 if (tracker->abort && !tracker->force) {
323 tracker->succeeded = FALSE;
324 purple_keyring_drop_passwords(tracker->new_kr,
325 purple_keyring_set_inuse_drop_cb, tracker);
326 } else {
327 tracker->succeeded = TRUE;
328 purple_keyring_drop_passwords(tracker->old_kr,
329 purple_keyring_set_inuse_drop_cb, tracker);
333 static void
334 purple_keyring_set_inuse_read_cb(PurpleAccount *account, const gchar *password,
335 GError *error, gpointer _tracker)
337 PurpleKeyringChangeTracker *tracker = _tracker;
338 PurpleKeyringSave save_cb;
340 g_return_if_fail(account != NULL);
341 g_return_if_fail(tracker != NULL);
343 if (tracker->abort) {
344 purple_keyring_set_inuse_save_cb(account, NULL, tracker);
345 return;
348 if (error != NULL) {
349 if (tracker->force == TRUE || g_error_matches(error,
350 PURPLE_KEYRING_ERROR,
351 PURPLE_KEYRING_ERROR_NOPASSWORD))
353 /* Don't save password, and ignore it. */
354 } else {
355 tracker->abort = TRUE;
357 purple_keyring_set_inuse_save_cb(account, error, tracker);
358 return;
361 save_cb = purple_keyring_get_save_password(tracker->new_kr);
362 g_assert(save_cb != NULL);
364 save_cb(account, password, purple_keyring_set_inuse_save_cb, tracker);
367 void
368 purple_keyring_set_inuse(PurpleKeyring *newkeyring, gboolean force,
369 PurpleKeyringSetInUseCallback cb, gpointer data)
371 PurpleKeyring *oldkeyring;
372 PurpleKeyringChangeTracker *tracker;
373 GList *it;
374 PurpleKeyringRead read_cb;
376 if (current_change_tracker != NULL) {
377 GError *error;
378 purple_debug_error("keyring", "There is password migration "
379 "session already running.\n");
380 if (cb == NULL)
381 return;
382 error = g_error_new(PURPLE_KEYRING_ERROR,
383 PURPLE_KEYRING_ERROR_INTERNAL,
384 _("There is a password migration session already "
385 "running."));
386 cb(error, data);
387 g_error_free(error);
388 return;
391 oldkeyring = purple_keyring_get_inuse();
393 if (oldkeyring == newkeyring) {
394 if (purple_debug_is_verbose()) {
395 purple_debug_misc("keyring",
396 "Old and new keyring are the same: %s.\n",
397 (newkeyring != NULL) ?
398 newkeyring->id : "(null)");
400 if (cb != NULL)
401 cb(NULL, data);
402 return;
405 purple_debug_info("keyring", "Attempting to set new keyring: %s.\n",
406 (newkeyring != NULL) ? newkeyring->id : "(null)");
408 if (oldkeyring == NULL) { /* No keyring was set before. */
409 if (purple_debug_is_verbose()) {
410 purple_debug_misc("keyring", "Setting keyring for the "
411 "first time: %s.\n", newkeyring->id);
414 purple_keyring_inuse = newkeyring;
415 g_assert(current_change_tracker == NULL);
416 if (cb != NULL)
417 cb(NULL, data);
418 return;
421 /* Starting a migration. */
423 read_cb = purple_keyring_get_read_password(oldkeyring);
424 g_assert(read_cb != NULL);
426 purple_debug_misc("keyring", "Starting migration from: %s.\n",
427 oldkeyring->id);
429 tracker = g_new0(PurpleKeyringChangeTracker, 1);
430 current_change_tracker = tracker;
432 tracker->cb = cb;
433 tracker->cb_data = data;
434 tracker->new_kr = newkeyring;
435 tracker->old_kr = oldkeyring;
436 tracker->force = force;
438 for (it = purple_accounts_get_all(); it != NULL; it = it->next) {
439 if (tracker->abort) {
440 tracker->finished = TRUE;
441 break;
443 tracker->read_outstanding++;
445 if (it->next == NULL)
446 tracker->finished = TRUE;
448 read_cb(it->data, purple_keyring_set_inuse_read_cb, tracker);
452 void
453 purple_keyring_register(PurpleKeyring *keyring)
455 const gchar *keyring_id;
457 g_return_if_fail(keyring != NULL);
459 keyring_id = purple_keyring_get_id(keyring);
461 purple_debug_info("keyring", "Registering keyring: %s\n",
462 keyring_id ? keyring_id : "(null)");
464 if (purple_keyring_get_id(keyring) == NULL ||
465 purple_keyring_get_name(keyring) == NULL ||
466 purple_keyring_get_read_password(keyring) == NULL ||
467 purple_keyring_get_save_password(keyring) == NULL)
469 purple_debug_error("keyring", "Cannot register %s, some "
470 "required fields are missing.\n",
471 keyring_id ? keyring_id : "(null)");
472 return;
475 if (purple_keyring_find_keyring_by_id(keyring_id) != NULL) {
476 purple_debug_error("keyring",
477 "Keyring is already registered.\n");
478 return;
481 /* If this is the configured keyring, use it. */
482 if (purple_keyring_inuse == NULL &&
483 g_strcmp0(keyring_id, purple_keyring_to_use) == 0)
485 purple_debug_misc("keyring", "Keyring %s matches keyring to "
486 "use, using it.", keyring_id);
487 purple_keyring_set_inuse(keyring, TRUE, NULL, NULL);
490 purple_signal_emit(purple_keyring_get_handle(), "keyring-register",
491 keyring_id, keyring);
492 if (purple_debug_is_verbose()) {
493 purple_debug_info("keyring", "Registered keyring: %s.\n",
494 keyring_id);
497 purple_keyring_keyrings = g_list_prepend(purple_keyring_keyrings,
498 keyring);
501 void
502 purple_keyring_unregister(PurpleKeyring *keyring)
504 PurpleKeyring *inuse;
505 PurpleKeyring *fallback;
506 const gchar *keyring_id;
508 g_return_if_fail(keyring != NULL);
510 keyring_id = purple_keyring_get_id(keyring);
512 purple_debug_info("keyring", "Unregistering keyring: %s.\n",
513 keyring_id);
515 purple_signal_emit(purple_keyring_get_handle(), "keyring-unregister",
516 keyring_id, keyring);
518 inuse = purple_keyring_get_inuse();
519 fallback = purple_keyring_find_keyring_by_id(PURPLE_DEFAULT_KEYRING);
521 if (inuse == keyring) {
522 if (inuse != fallback) {
523 purple_keyring_set_inuse(fallback, TRUE, NULL, NULL);
524 } else {
525 fallback = NULL;
526 purple_keyring_set_inuse(NULL, TRUE, NULL, NULL);
530 purple_keyring_keyrings = g_list_remove(purple_keyring_keyrings,
531 keyring);
534 GList *
535 purple_keyring_get_options(void)
537 GList *options = NULL;
538 GList *it;
539 static gchar currentDisabledName[40];
541 if (purple_keyring_get_inuse() == NULL && purple_keyring_to_use != NULL
542 && purple_keyring_to_use[0] != '\0')
544 g_snprintf(currentDisabledName, sizeof(currentDisabledName),
545 _("%s (disabled)"), purple_keyring_to_use);
547 options = g_list_append(options, currentDisabledName);
548 options = g_list_append(options, purple_keyring_to_use);
551 for (it = purple_keyring_keyrings; it != NULL; it = it->next) {
552 PurpleKeyring *keyring = it->data;
554 options = g_list_append(options,
555 (gpointer)purple_keyring_get_name(keyring));
556 options = g_list_append(options,
557 (gpointer)purple_keyring_get_id(keyring));
560 return options;
564 /**************************************************************************/
565 /* Keyring plugin wrappers */
566 /**************************************************************************/
568 static void
569 purple_keyring_close(PurpleKeyring *keyring)
571 PurpleKeyringClose close_cb;
573 g_return_if_fail(keyring != NULL);
575 if (keyring->is_cancelling) {
576 keyring->close_after_cancel = TRUE;
577 return;
579 if (keyring->is_closing)
580 return;
581 keyring->is_closing = TRUE;
583 close_cb = purple_keyring_get_close_keyring(keyring);
585 if (close_cb != NULL)
586 close_cb();
588 keyring->is_closing = FALSE;
591 static void
592 purple_keyring_cancel_requests(PurpleKeyring *keyring)
594 PurpleKeyringCancelRequests cancel_cb;
596 g_return_if_fail(keyring != NULL);
598 if (keyring->is_cancelling)
599 return;
600 keyring->is_cancelling = TRUE;
602 cancel_cb = purple_keyring_get_cancel_requests(keyring);
604 if (cancel_cb != NULL)
605 cancel_cb();
607 keyring->is_cancelling = FALSE;
609 if (keyring->close_after_cancel) {
610 keyring->close_after_cancel = FALSE;
611 purple_keyring_close(keyring);
615 static void
616 purple_keyring_drop_passwords_save_cb(PurpleAccount *account, GError *error,
617 gpointer _tracker)
619 PurpleKeyringDropTracker *tracker = _tracker;
621 tracker->drop_outstanding--;
623 if (!tracker->finished || tracker->drop_outstanding > 0)
624 return;
626 if (tracker->cb)
627 tracker->cb(tracker->cb_data);
628 g_free(tracker);
631 static void
632 purple_keyring_drop_passwords(PurpleKeyring *keyring,
633 PurpleKeyringDropCallback cb, gpointer data)
635 GList *it;
636 PurpleKeyringSave save_cb;
637 PurpleKeyringDropTracker *tracker;
639 g_return_if_fail(keyring != NULL);
641 save_cb = purple_keyring_get_save_password(keyring);
642 g_assert(save_cb != NULL);
644 it = purple_accounts_get_all();
645 if (it == NULL)
646 return;
648 tracker = g_new0(PurpleKeyringDropTracker, 1);
649 tracker->cb = cb;
650 tracker->cb_data = data;
652 for (; it != NULL; it = it->next) {
653 PurpleAccount *account = it->data;
655 tracker->drop_outstanding++;
656 if (it->next == NULL)
657 tracker->finished = TRUE;
659 save_cb(account, NULL, purple_keyring_drop_passwords_save_cb,
660 tracker);
664 gboolean
665 purple_keyring_import_password(PurpleAccount *account, const gchar *keyring_id,
666 const gchar *mode, const gchar *data, GError **error)
668 PurpleKeyring *keyring;
669 PurpleKeyring *inuse;
670 PurpleKeyringImportPassword import;
672 g_return_val_if_fail(account != NULL, FALSE);
674 if (keyring_id == NULL)
675 keyring_id = PURPLE_DEFAULT_KEYRING;
677 purple_debug_misc("keyring", "Importing password for account %s to "
678 "keyring %s.\n", purple_keyring_print_account(account),
679 keyring_id);
681 keyring = purple_keyring_find_keyring_by_id(keyring_id);
682 if (keyring == NULL) {
683 if (error != NULL) {
684 *error = g_error_new(PURPLE_KEYRING_ERROR,
685 PURPLE_KEYRING_ERROR_BACKENDFAIL,
686 _("Specified keyring is not registered."));
688 purple_debug_warning("Keyring", "Specified keyring is not "
689 "registered, cannot import password info for account "
690 "%s.\n", purple_keyring_print_account(account));
691 return FALSE;
694 inuse = purple_keyring_get_inuse();
695 if (inuse == NULL) {
696 PurpleKeyringFailedImport *import;
697 if (error != NULL) {
698 *error = g_error_new(PURPLE_KEYRING_ERROR,
699 PURPLE_KEYRING_ERROR_NOKEYRING,
700 _("No keyring loaded, cannot import password "
701 "info."));
703 purple_debug_warning("Keyring",
704 "No keyring loaded, cannot import password info for "
705 "account %s.\n", purple_keyring_print_account(account));
707 import = g_new0(PurpleKeyringFailedImport, 1);
708 import->keyring_id = g_strdup(keyring_id);
709 import->mode = g_strdup(mode);
710 import->data = g_strdup(data);
711 g_hash_table_insert(purple_keyring_failed_imports, account,
712 import);
713 return FALSE;
716 if (inuse != keyring) {
717 if (error != NULL) {
718 *error = g_error_new(PURPLE_KEYRING_ERROR,
719 PURPLE_KEYRING_ERROR_INTERNAL,
720 _("Specified keyring ID does not match the "
721 "loaded one."));
723 purple_debug_error("keyring",
724 "Specified keyring %s is not currently used (%s). "
725 "Data will be lost.\n", keyring_id,
726 purple_keyring_get_id(inuse));
727 return FALSE;
730 import = purple_keyring_get_import_password(inuse);
731 if (import == NULL) {
732 if (purple_debug_is_verbose()) {
733 purple_debug_misc("Keyring", "Configured keyring "
734 "cannot import password info. This might be "
735 "normal.\n");
737 return TRUE;
740 return import(account, mode, data, error);
743 gboolean
744 purple_keyring_export_password(PurpleAccount *account, const gchar **keyring_id,
745 const gchar **mode, gchar **data, GError **error,
746 GDestroyNotify *destroy)
748 PurpleKeyring *inuse;
749 PurpleKeyringExportPassword export;
751 g_return_val_if_fail(account != NULL, FALSE);
752 g_return_val_if_fail(keyring_id != NULL, FALSE);
753 g_return_val_if_fail(mode != NULL, FALSE);
754 g_return_val_if_fail(data != NULL, FALSE);
755 g_return_val_if_fail(error != NULL, FALSE);
757 inuse = purple_keyring_get_inuse();
759 if (inuse == NULL) {
760 PurpleKeyringFailedImport *import = g_hash_table_lookup(
761 purple_keyring_failed_imports, account);
763 if (import == NULL) {
764 *error = g_error_new(PURPLE_KEYRING_ERROR,
765 PURPLE_KEYRING_ERROR_NOKEYRING,
766 _("No keyring configured, cannot export "
767 "password info."));
768 purple_debug_warning("keyring",
769 "No keyring configured, cannot export password "
770 "info.\n");
771 return FALSE;
772 } else {
773 purple_debug_info("keyring", "No keyring configured, "
774 "getting fallback export data for %s.\n",
775 purple_keyring_print_account(account));
777 *keyring_id = import->keyring_id;
778 *mode = import->mode;
779 *data = g_strdup(import->data);
780 *destroy = (GDestroyNotify)purple_str_wipe;
781 return TRUE;
785 if (purple_debug_is_verbose()) {
786 purple_debug_misc("keyring",
787 "Exporting password for account %s from keyring %s\n",
788 purple_keyring_print_account(account),
789 purple_keyring_get_id(inuse));
792 *keyring_id = purple_keyring_get_id(inuse);
794 export = purple_keyring_get_export_password(inuse);
795 if (export == NULL) {
796 if (purple_debug_is_verbose()) {
797 purple_debug_misc("Keyring", "Configured keyring "
798 "cannot export password info. This might be "
799 "normal.\n");
801 *mode = NULL;
802 *data = NULL;
803 *destroy = NULL;
804 return TRUE;
807 return export(account, mode, data, error, destroy);
810 void
811 purple_keyring_get_password(PurpleAccount *account,
812 PurpleKeyringReadCallback cb, gpointer data)
814 GError *error;
815 PurpleKeyring *inuse;
816 PurpleKeyringRead read_cb;
818 g_return_if_fail(account != NULL);
820 if (purple_keyring_is_quitting) {
821 purple_debug_error("keyring", "Cannot request a password while "
822 "quitting.\n");
823 if (cb == NULL)
824 return;
825 error = g_error_new(PURPLE_KEYRING_ERROR,
826 PURPLE_KEYRING_ERROR_INTERNAL,
827 _("Cannot request a password while quitting."));
828 cb(account, NULL, error, data);
829 g_error_free(error);
830 return;
833 inuse = purple_keyring_get_inuse();
835 if (inuse == NULL) {
836 purple_debug_error("keyring", "No keyring configured.\n");
837 if (cb == NULL)
838 return;
840 error = g_error_new(PURPLE_KEYRING_ERROR,
841 PURPLE_KEYRING_ERROR_NOKEYRING,
842 _("No keyring configured."));
843 cb(account, NULL, error, data);
844 g_error_free(error);
845 return;
848 read_cb = purple_keyring_get_read_password(inuse);
849 g_assert(read_cb != NULL);
851 purple_debug_info("keyring", "Reading password for account %s...\n",
852 purple_keyring_print_account(account));
853 read_cb(account, cb, data);
856 static void
857 purple_keyring_set_password_save_cb(PurpleAccount *account, GError *error,
858 gpointer _set_data)
860 PurpleKeyringSetPasswordData *set_data = _set_data;
862 g_return_if_fail(account != NULL);
863 g_return_if_fail(set_data != NULL);
865 if (error == NULL && purple_debug_is_verbose()) {
866 purple_debug_misc("keyring", "Password for account %s "
867 "saved successfully.\n",
868 purple_keyring_print_account(account));
869 } else if (purple_debug_is_verbose()) {
870 purple_debug_warning("keyring", "Password for account %s "
871 "not saved successfully.\n",
872 purple_keyring_print_account(account));
875 if (error != NULL) {
876 purple_notify_error(NULL, _("Keyrings"),
877 _("Failed to save a password in keyring."),
878 error->message, NULL);
881 if (set_data->cb != NULL)
882 set_data->cb(account, error, set_data->cb_data);
883 g_free(set_data);
886 void
887 purple_keyring_set_password(PurpleAccount *account, const gchar *password,
888 PurpleKeyringSaveCallback cb, gpointer data)
890 GError *error;
891 PurpleKeyring *inuse;
892 PurpleKeyringSave save_cb;
893 PurpleKeyringSetPasswordData *set_data;
895 g_return_if_fail(account != NULL);
897 if (purple_keyring_is_quitting) {
898 purple_debug_error("keyring", "Cannot save a password while "
899 "quitting.\n");
900 if (cb == NULL)
901 return;
902 error = g_error_new(PURPLE_KEYRING_ERROR,
903 PURPLE_KEYRING_ERROR_INTERNAL,
904 _("Cannot save a password while quitting."));
905 cb(account, error, data);
906 g_error_free(error);
907 return;
910 if (current_change_tracker != NULL) {
911 purple_debug_error("keyring", "Cannot save a password during "
912 "password migration.\n");
913 if (cb == NULL)
914 return;
915 error = g_error_new(PURPLE_KEYRING_ERROR,
916 PURPLE_KEYRING_ERROR_INTERNAL,
917 _("Cannot save a password during password migration."));
918 cb(account, error, data);
919 g_error_free(error);
920 return;
923 inuse = purple_keyring_get_inuse();
924 if (inuse == NULL) {
925 if (cb == NULL)
926 return;
927 error = g_error_new(PURPLE_KEYRING_ERROR,
928 PURPLE_KEYRING_ERROR_NOKEYRING,
929 _("No keyring configured."));
930 cb(account, error, data);
931 g_error_free(error);
932 return;
935 save_cb = purple_keyring_get_save_password(inuse);
936 g_assert(save_cb != NULL);
938 set_data = g_new(PurpleKeyringSetPasswordData, 1);
939 set_data->cb = cb;
940 set_data->cb_data = data;
941 purple_debug_info("keyring", "%s password for account %s...\n",
942 (password ? "Saving" : "Removing"),
943 purple_keyring_print_account(account));
944 save_cb(account, password, purple_keyring_set_password_save_cb, set_data);
947 PurpleRequestFields *
948 purple_keyring_read_settings(void)
950 PurpleKeyring *inuse;
951 PurpleKeyringReadSettings read_settings;
953 if (purple_keyring_is_quitting || current_change_tracker != NULL) {
954 purple_debug_error("keyring", "Cannot read settngs at the "
955 "moment.\n");
956 return NULL;
959 inuse = purple_keyring_get_inuse();
960 if (inuse == NULL) {
961 purple_debug_error("keyring", "No keyring in use.\n");
962 return NULL;
965 read_settings = purple_keyring_get_read_settings(inuse);
966 if (read_settings == NULL)
967 return NULL;
968 return read_settings();
971 gboolean
972 purple_keyring_apply_settings(void *notify_handle, PurpleRequestFields *fields)
974 PurpleKeyring *inuse;
975 PurpleKeyringApplySettings apply_settings;
977 g_return_val_if_fail(fields != NULL, FALSE);
979 if (purple_keyring_is_quitting || current_change_tracker != NULL) {
980 purple_debug_error("keyring", "Cannot apply settngs at the "
981 "moment.\n");
982 return FALSE;
985 inuse = purple_keyring_get_inuse();
986 if (inuse == NULL) {
987 purple_debug_error("keyring", "No keyring in use.\n");
988 return FALSE;
991 apply_settings = purple_keyring_get_apply_settings(inuse);
992 if (apply_settings == NULL) {
993 purple_debug_warning("keyring", "Applying settings not "
994 "supported.\n");
995 return FALSE;
997 return apply_settings(notify_handle, fields);
1000 /**************************************************************************/
1001 /* PurpleKeyring accessors */
1002 /**************************************************************************/
1004 PurpleKeyring *
1005 purple_keyring_new(void)
1007 return g_new0(PurpleKeyring, 1);
1010 void
1011 purple_keyring_free(PurpleKeyring *keyring)
1013 g_return_if_fail(keyring != NULL);
1015 g_free(keyring->name);
1016 g_free(keyring->id);
1017 g_free(keyring);
1020 const gchar *
1021 purple_keyring_get_name(const PurpleKeyring *keyring)
1023 g_return_val_if_fail(keyring != NULL, NULL);
1025 return keyring->name;
1028 const gchar *
1029 purple_keyring_get_id(const PurpleKeyring *keyring)
1031 g_return_val_if_fail(keyring != NULL, NULL);
1033 return keyring->id;
1036 PurpleKeyringRead
1037 purple_keyring_get_read_password(const PurpleKeyring *keyring)
1039 g_return_val_if_fail(keyring != NULL, NULL);
1041 return keyring->read_password;
1044 PurpleKeyringSave
1045 purple_keyring_get_save_password(const PurpleKeyring *keyring)
1047 g_return_val_if_fail(keyring != NULL, NULL);
1049 return keyring->save_password;
1052 PurpleKeyringCancelRequests
1053 purple_keyring_get_cancel_requests(const PurpleKeyring *keyring)
1055 g_return_val_if_fail(keyring != NULL, NULL);
1057 return keyring->cancel_requests;
1060 PurpleKeyringClose
1061 purple_keyring_get_close_keyring(const PurpleKeyring *keyring)
1063 g_return_val_if_fail(keyring != NULL, NULL);
1065 return keyring->close_keyring;
1068 PurpleKeyringImportPassword
1069 purple_keyring_get_import_password(const PurpleKeyring *keyring)
1071 g_return_val_if_fail(keyring != NULL, NULL);
1073 return keyring->import_password;
1076 PurpleKeyringExportPassword
1077 purple_keyring_get_export_password(const PurpleKeyring *keyring)
1079 g_return_val_if_fail(keyring != NULL, NULL);
1081 return keyring->export_password;
1084 PurpleKeyringReadSettings
1085 purple_keyring_get_read_settings(const PurpleKeyring *keyring)
1087 g_return_val_if_fail(keyring != NULL, NULL);
1089 return keyring->read_settings;
1092 PurpleKeyringApplySettings
1093 purple_keyring_get_apply_settings(const PurpleKeyring *keyring)
1095 g_return_val_if_fail(keyring != NULL, NULL);
1097 return keyring->apply_settings;
1100 void
1101 purple_keyring_set_name(PurpleKeyring *keyring, const gchar *name)
1103 g_return_if_fail(keyring != NULL);
1104 g_return_if_fail(name != NULL);
1106 g_free(keyring->name);
1107 keyring->name = g_strdup(name);
1110 void
1111 purple_keyring_set_id(PurpleKeyring *keyring, const gchar *id)
1113 g_return_if_fail(keyring != NULL);
1114 g_return_if_fail(id != NULL);
1116 g_free(keyring->id);
1117 keyring->id = g_strdup(id);
1120 void
1121 purple_keyring_set_read_password(PurpleKeyring *keyring,
1122 PurpleKeyringRead read_cb)
1124 g_return_if_fail(keyring != NULL);
1125 g_return_if_fail(read_cb != NULL);
1127 keyring->read_password = read_cb;
1130 void
1131 purple_keyring_set_save_password(PurpleKeyring *keyring,
1132 PurpleKeyringSave save_cb)
1134 g_return_if_fail(keyring != NULL);
1135 g_return_if_fail(save_cb != NULL);
1137 keyring->save_password = save_cb;
1140 void
1141 purple_keyring_set_cancel_requests(PurpleKeyring *keyring,
1142 PurpleKeyringCancelRequests cancel_requests)
1144 g_return_if_fail(keyring != NULL);
1146 keyring->cancel_requests = cancel_requests;
1149 void
1150 purple_keyring_set_close_keyring(PurpleKeyring *keyring,
1151 PurpleKeyringClose close_cb)
1153 g_return_if_fail(keyring != NULL);
1155 keyring->close_keyring = close_cb;
1158 void
1159 purple_keyring_set_import_password(PurpleKeyring *keyring,
1160 PurpleKeyringImportPassword import_password)
1162 g_return_if_fail(keyring != NULL);
1164 keyring->import_password = import_password;
1167 void
1168 purple_keyring_set_export_password(PurpleKeyring *keyring,
1169 PurpleKeyringExportPassword export_password)
1171 g_return_if_fail(keyring != NULL);
1173 keyring->export_password = export_password;
1176 void
1177 purple_keyring_set_read_settings(PurpleKeyring *keyring,
1178 PurpleKeyringReadSettings read_settings)
1180 g_return_if_fail(keyring != NULL);
1182 keyring->read_settings = read_settings;
1185 void
1186 purple_keyring_set_apply_settings(PurpleKeyring *keyring,
1187 PurpleKeyringApplySettings apply_settings)
1189 g_return_if_fail(keyring != NULL);
1191 keyring->apply_settings = apply_settings;
1195 /**************************************************************************/
1196 /* Error Codes */
1197 /**************************************************************************/
1199 GQuark purple_keyring_error_domain(void)
1201 return g_quark_from_static_string("libpurple keyring");
1204 /**************************************************************************/
1205 /* Keyring Subsystem */
1206 /**************************************************************************/
1208 static void purple_keyring_core_initialized_cb(void)
1210 if (purple_keyring_inuse == NULL) {
1211 purple_notify_error(NULL, _("Keyrings"),
1212 _("Failed to load selected keyring."),
1213 _("Check your system configuration or select another "
1214 "one in Preferences dialog."), NULL);
1218 static void purple_keyring_core_quitting_cb()
1220 if (current_change_tracker != NULL) {
1221 PurpleKeyringChangeTracker *tracker = current_change_tracker;
1222 tracker->abort = TRUE;
1223 if (tracker->old_kr)
1224 purple_keyring_cancel_requests(tracker->old_kr);
1225 if (current_change_tracker == tracker && tracker->new_kr)
1226 purple_keyring_cancel_requests(tracker->new_kr);
1229 purple_keyring_is_quitting = TRUE;
1230 if (purple_keyring_inuse != NULL)
1231 purple_keyring_cancel_requests(purple_keyring_inuse);
1234 void
1235 purple_keyring_init(void)
1237 const gchar *touse;
1238 GList *plugins, *it;
1240 purple_keyring_keyrings = NULL;
1241 purple_keyring_inuse = NULL;
1243 purple_keyring_failed_imports = g_hash_table_new_full(g_direct_hash,
1244 g_direct_equal, NULL,
1245 (GDestroyNotify)purple_keyring_failed_import_free);
1247 /* void keyring_register(const char *keyring_id,
1248 * PurpleKeyring * keyring);
1250 * A signal called when keyring is registered.
1252 * @param keyring_id The keyring ID.
1253 * @param keyring The keyring.
1255 purple_signal_register(purple_keyring_get_handle(), "keyring-register",
1256 purple_marshal_VOID__POINTER_POINTER, G_TYPE_NONE, 2, G_TYPE_STRING,
1257 PURPLE_TYPE_KEYRING);
1259 /* void keyring_unregister(const char *keyring_id,
1260 * PurpleKeyring * keyring);
1262 * A signal called when keyring is unregistered.
1264 * @param keyring_id The keyring ID.
1265 * @param keyring The keyring.
1267 purple_signal_register(purple_keyring_get_handle(), "keyring-unregister",
1268 purple_marshal_VOID__POINTER_POINTER, G_TYPE_NONE, 2, G_TYPE_STRING,
1269 PURPLE_TYPE_KEYRING);
1271 /* void password_migration(PurpleAccount* account);
1273 * A signal called, when a password for the account was moved to another
1274 * keyring.
1276 * @param account The account.
1278 purple_signal_register(purple_keyring_get_handle(), "password-migration",
1279 purple_marshal_VOID__POINTER, G_TYPE_NONE, 1, PURPLE_TYPE_ACCOUNT);
1281 touse = purple_prefs_get_string("/purple/keyring/active");
1282 if (touse == NULL) {
1283 purple_prefs_add_none("/purple/keyring");
1284 purple_prefs_add_string("/purple/keyring/active",
1285 PURPLE_DEFAULT_KEYRING);
1286 purple_keyring_to_use = g_strdup(PURPLE_DEFAULT_KEYRING);
1287 } else
1288 purple_keyring_to_use = g_strdup(touse);
1290 purple_keyring_pref_connect();
1292 plugins = purple_plugins_find_all();
1293 for (it = plugins; it != NULL; it = it->next) {
1294 PurplePlugin *plugin = PURPLE_PLUGIN(it->data);
1295 PurplePluginInfo *info = purple_plugin_get_info(plugin);
1297 if (strncmp(purple_plugin_info_get_id(info), "keyring-", 8) != 0)
1298 continue;
1300 if (purple_plugin_is_loaded(plugin))
1301 continue;
1303 if (purple_plugin_load(plugin, NULL)) {
1304 purple_keyring_loaded_plugins = g_list_append(
1305 purple_keyring_loaded_plugins, plugin);
1308 g_list_free(plugins);
1310 if (purple_keyring_inuse == NULL)
1311 purple_debug_error("keyring", "Selected keyring failed to load\n");
1313 purple_signal_connect(purple_get_core(), "core-initialized",
1314 purple_keyring_get_handle(),
1315 PURPLE_CALLBACK(purple_keyring_core_initialized_cb), NULL);
1316 purple_signal_connect(purple_get_core(), "quitting",
1317 purple_keyring_get_handle(),
1318 PURPLE_CALLBACK(purple_keyring_core_quitting_cb), NULL);
1321 void
1322 purple_keyring_uninit(void)
1324 GList *it;
1326 purple_keyring_pref_disconnect();
1328 g_free(purple_keyring_to_use);
1329 purple_keyring_inuse = NULL;
1331 g_hash_table_destroy(purple_keyring_failed_imports);
1332 purple_keyring_failed_imports = NULL;
1334 for (it = g_list_first(purple_keyring_loaded_plugins); it != NULL;
1335 it = g_list_next(it))
1337 PurplePlugin *plugin = PURPLE_PLUGIN(it->data);
1338 if (g_list_find(purple_plugins_get_loaded(), plugin) == NULL)
1339 continue;
1340 purple_plugin_unload(plugin, NULL);
1342 g_list_free(purple_keyring_loaded_plugins);
1343 purple_keyring_loaded_plugins = NULL;
1345 purple_signals_unregister_by_instance(purple_keyring_get_handle());
1346 purple_signals_disconnect_by_handle(purple_keyring_get_handle());
1349 void *
1350 purple_keyring_get_handle(void)
1352 static int handle;
1354 return &handle;
1357 static PurpleKeyring *
1358 purple_keyring_copy(PurpleKeyring *keyring)
1360 PurpleKeyring *keyring_copy;
1362 g_return_val_if_fail(keyring != NULL, NULL);
1364 keyring_copy = purple_keyring_new();
1365 *keyring_copy = *keyring;
1367 keyring_copy->name = g_strdup(keyring->name);
1368 keyring_copy->id = g_strdup(keyring->id);
1370 return keyring_copy;
1373 GType
1374 purple_keyring_get_type(void)
1376 static GType type = 0;
1378 if (type == 0) {
1379 type = g_boxed_type_register_static("PurpleKeyring",
1380 (GBoxedCopyFunc)purple_keyring_copy,
1381 (GBoxedFreeFunc)purple_keyring_free);
1384 return type;