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
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
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
;
46 gboolean is_cancelling
;
47 gboolean close_after_cancel
;
53 PurpleKeyringSetInUseCallback cb
;
55 PurpleKeyring
*new_kr
;
56 PurpleKeyring
*old_kr
;
59 * We are done when finished is positive and read_outstanding is zero.
67 } PurpleKeyringChangeTracker
;
69 typedef void (*PurpleKeyringDropCallback
)(gpointer data
);
73 PurpleKeyringDropCallback cb
;
78 } PurpleKeyringDropTracker
;
82 PurpleKeyringSaveCallback cb
;
84 } PurpleKeyringSetPasswordData
;
91 } PurpleKeyringFailedImport
;
94 purple_keyring_change_tracker_free(PurpleKeyringChangeTracker
*tracker
)
97 g_error_free(tracker
->error
);
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
);
113 purple_keyring_close(PurpleKeyring
*keyring
);
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
;
135 purple_keyring_print_account(PurpleAccount
*account
)
137 static gchar print_buff
[100];
139 if (account
== NULL
) {
140 g_snprintf(print_buff
, 100, "(null)");
144 g_snprintf(print_buff
, 100, "%s:%s",
145 purple_account_get_protocol_id(account
),
146 purple_account_get_username(account
));
150 /**************************************************************************/
151 /* Setting used keyrings */
152 /**************************************************************************/
155 purple_keyring_find_keyring_by_id(const gchar
*id
)
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)
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
);
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
);
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;
205 purple_keyring_get_inuse(void)
207 return purple_keyring_inuse
;
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
);
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
);
255 purple_keyring_set_inuse_save_cb(PurpleAccount
*account
, GError
*error
,
258 PurpleKeyringChangeTracker
*tracker
= _tracker
;
260 g_return_if_fail(account
!= NULL
);
261 g_return_if_fail(tracker
!= NULL
);
263 tracker
->read_outstanding
--;
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
),
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
);
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",
318 if (!tracker
->finished
|| tracker
->read_outstanding
> 0)
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
);
327 tracker
->succeeded
= TRUE
;
328 purple_keyring_drop_passwords(tracker
->old_kr
,
329 purple_keyring_set_inuse_drop_cb
, tracker
);
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
);
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. */
355 tracker
->abort
= TRUE
;
357 purple_keyring_set_inuse_save_cb(account
, error
, tracker
);
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
);
368 purple_keyring_set_inuse(PurpleKeyring
*newkeyring
, gboolean force
,
369 PurpleKeyringSetInUseCallback cb
, gpointer data
)
371 PurpleKeyring
*oldkeyring
;
372 PurpleKeyringChangeTracker
*tracker
;
374 PurpleKeyringRead read_cb
;
376 if (current_change_tracker
!= NULL
) {
378 purple_debug_error("keyring", "There is password migration "
379 "session already running.\n");
382 error
= g_error_new(PURPLE_KEYRING_ERROR
,
383 PURPLE_KEYRING_ERROR_INTERNAL
,
384 _("There is a password migration session already "
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)");
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
);
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",
429 tracker
= g_new0(PurpleKeyringChangeTracker
, 1);
430 current_change_tracker
= tracker
;
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
;
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
);
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)");
475 if (purple_keyring_find_keyring_by_id(keyring_id
) != NULL
) {
476 purple_debug_error("keyring",
477 "Keyring is already registered.\n");
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",
497 purple_keyring_keyrings
= g_list_prepend(purple_keyring_keyrings
,
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",
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
);
526 purple_keyring_set_inuse(NULL
, TRUE
, NULL
, NULL
);
530 purple_keyring_keyrings
= g_list_remove(purple_keyring_keyrings
,
535 purple_keyring_get_options(void)
537 GList
*options
= NULL
;
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
));
564 /**************************************************************************/
565 /* Keyring plugin wrappers */
566 /**************************************************************************/
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
;
579 if (keyring
->is_closing
)
581 keyring
->is_closing
= TRUE
;
583 close_cb
= purple_keyring_get_close_keyring(keyring
);
585 if (close_cb
!= NULL
)
588 keyring
->is_closing
= FALSE
;
592 purple_keyring_cancel_requests(PurpleKeyring
*keyring
)
594 PurpleKeyringCancelRequests cancel_cb
;
596 g_return_if_fail(keyring
!= NULL
);
598 if (keyring
->is_cancelling
)
600 keyring
->is_cancelling
= TRUE
;
602 cancel_cb
= purple_keyring_get_cancel_requests(keyring
);
604 if (cancel_cb
!= NULL
)
607 keyring
->is_cancelling
= FALSE
;
609 if (keyring
->close_after_cancel
) {
610 keyring
->close_after_cancel
= FALSE
;
611 purple_keyring_close(keyring
);
616 purple_keyring_drop_passwords_save_cb(PurpleAccount
*account
, GError
*error
,
619 PurpleKeyringDropTracker
*tracker
= _tracker
;
621 tracker
->drop_outstanding
--;
623 if (!tracker
->finished
|| tracker
->drop_outstanding
> 0)
627 tracker
->cb(tracker
->cb_data
);
632 purple_keyring_drop_passwords(PurpleKeyring
*keyring
,
633 PurpleKeyringDropCallback cb
, gpointer data
)
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();
648 tracker
= g_new0(PurpleKeyringDropTracker
, 1);
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
,
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
),
681 keyring
= purple_keyring_find_keyring_by_id(keyring_id
);
682 if (keyring
== 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
));
694 inuse
= purple_keyring_get_inuse();
696 PurpleKeyringFailedImport
*import
;
698 *error
= g_error_new(PURPLE_KEYRING_ERROR
,
699 PURPLE_KEYRING_ERROR_NOKEYRING
,
700 _("No keyring loaded, cannot import password "
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
,
716 if (inuse
!= keyring
) {
718 *error
= g_error_new(PURPLE_KEYRING_ERROR
,
719 PURPLE_KEYRING_ERROR_INTERNAL
,
720 _("Specified keyring ID does not match the "
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
));
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 "
740 return import(account
, mode
, data
, error
);
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();
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 "
768 purple_debug_warning("keyring",
769 "No keyring configured, cannot export password "
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
;
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 "
807 return export(account
, mode
, data
, error
, destroy
);
811 purple_keyring_get_password(PurpleAccount
*account
,
812 PurpleKeyringReadCallback cb
, gpointer data
)
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 "
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
);
833 inuse
= purple_keyring_get_inuse();
836 purple_debug_error("keyring", "No keyring configured.\n");
840 error
= g_error_new(PURPLE_KEYRING_ERROR
,
841 PURPLE_KEYRING_ERROR_NOKEYRING
,
842 _("No keyring configured."));
843 cb(account
, NULL
, error
, data
);
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
);
857 purple_keyring_set_password_save_cb(PurpleAccount
*account
, GError
*error
,
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
));
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
);
887 purple_keyring_set_password(PurpleAccount
*account
, const gchar
*password
,
888 PurpleKeyringSaveCallback cb
, gpointer data
)
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 "
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
);
910 if (current_change_tracker
!= NULL
) {
911 purple_debug_error("keyring", "Cannot save a password during "
912 "password migration.\n");
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
);
923 inuse
= purple_keyring_get_inuse();
927 error
= g_error_new(PURPLE_KEYRING_ERROR
,
928 PURPLE_KEYRING_ERROR_NOKEYRING
,
929 _("No keyring configured."));
930 cb(account
, error
, data
);
935 save_cb
= purple_keyring_get_save_password(inuse
);
936 g_assert(save_cb
!= NULL
);
938 set_data
= g_new(PurpleKeyringSetPasswordData
, 1);
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 "
959 inuse
= purple_keyring_get_inuse();
961 purple_debug_error("keyring", "No keyring in use.\n");
965 read_settings
= purple_keyring_get_read_settings(inuse
);
966 if (read_settings
== NULL
)
968 return read_settings();
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 "
985 inuse
= purple_keyring_get_inuse();
987 purple_debug_error("keyring", "No keyring in use.\n");
991 apply_settings
= purple_keyring_get_apply_settings(inuse
);
992 if (apply_settings
== NULL
) {
993 purple_debug_warning("keyring", "Applying settings not "
997 return apply_settings(notify_handle
, fields
);
1000 /**************************************************************************/
1001 /* PurpleKeyring accessors */
1002 /**************************************************************************/
1005 purple_keyring_new(void)
1007 return g_new0(PurpleKeyring
, 1);
1011 purple_keyring_free(PurpleKeyring
*keyring
)
1013 g_return_if_fail(keyring
!= NULL
);
1015 g_free(keyring
->name
);
1016 g_free(keyring
->id
);
1021 purple_keyring_get_name(const PurpleKeyring
*keyring
)
1023 g_return_val_if_fail(keyring
!= NULL
, NULL
);
1025 return keyring
->name
;
1029 purple_keyring_get_id(const PurpleKeyring
*keyring
)
1031 g_return_val_if_fail(keyring
!= NULL
, NULL
);
1037 purple_keyring_get_read_password(const PurpleKeyring
*keyring
)
1039 g_return_val_if_fail(keyring
!= NULL
, NULL
);
1041 return keyring
->read_password
;
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
;
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
;
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
);
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
);
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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 /**************************************************************************/
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
);
1235 purple_keyring_init(void)
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
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
);
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)
1300 if (purple_plugin_is_loaded(plugin
))
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
);
1322 purple_keyring_uninit(void)
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
)
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());
1350 purple_keyring_get_handle(void)
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
;
1374 purple_keyring_get_type(void)
1376 static GType type
= 0;
1379 type
= g_boxed_type_register_static("PurpleKeyring",
1380 (GBoxedCopyFunc
)purple_keyring_copy
,
1381 (GBoxedFreeFunc
)purple_keyring_free
);