1 /* libsecret - GLib wrapper for Secret Service
3 * Copyright 2011 Collabora Ltd.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Author: Stef Walter <stefw@gnome.org>
17 #include "secret-attributes.h"
18 #include "secret-password.h"
19 #include "secret-private.h"
20 #include "secret-value.h"
22 #include <egg/egg-secure-memory.h>
25 * SECTION:secret-password
26 * @title: Password storage
27 * @short_description: Simple password storage and lookup
29 * This is a simple API for storing passwords and retrieving passwords in the
32 * Each password is associated with a set of attributes. Attribute values can
33 * be either strings, integers or booleans.
35 * The names and types of allowed attributes for a given password are defined
36 * with a schema. Certain schemas are predefined. Additional schemas can be
37 * defined via the %SecretSchema structure.
39 * Each of the functions accept a variable list of attributes names and their
40 * values. Include a %NULL to terminate the list of attributes.
46 * secret_password_store: (skip)
47 * @schema: the schema for attributes
48 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
49 * @label: label for the secret
50 * @password: the null-terminated password to store
51 * @cancellable: optional cancellation object
52 * @callback: called when the operation completes
53 * @user_data: data to be passed to the callback
54 * @...: the attribute keys and values, terminated with %NULL
56 * Store a password in the secret service.
58 * The variable argument list should contain pairs of a) The attribute name as
59 * a null-terminated string, followed by b) attribute value, either a character
60 * string, an int number, or a gboolean value, as defined in the @schema.
61 * The list of attribtues should be terminated with a %NULL.
63 * If the attributes match a secret item already stored in the collection, then
64 * the item will be updated with these new values.
66 * If @collection is %NULL, then the default collection will be
67 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
68 * collection, which doesn't get stored across login sessions.
70 * This method will return immediately and complete asynchronously.
73 secret_password_store (const SecretSchema *schema,
74 const gchar *collection,
76 const gchar *password,
77 GCancellable *cancellable,
78 GAsyncReadyCallback callback,
82 GHashTable *attributes;
85 g_return_if_fail (schema != NULL);
86 g_return_if_fail (label != NULL);
87 g_return_if_fail (password != NULL);
88 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
90 va_start (va, user_data);
91 attributes = secret_attributes_buildv (schema, va);
94 secret_password_storev (schema, attributes, collection, label, password,
95 cancellable, callback, user_data);
97 g_hash_table_unref (attributes);
101 * secret_password_storev:
102 * @schema: the schema for attributes
103 * @attributes: (element-type utf8 utf8): the attribute keys and values
104 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
105 * @label: label for the secret
106 * @password: the null-terminated password to store
107 * @cancellable: optional cancellation object
108 * @callback: called when the operation completes
109 * @user_data: data to be passed to the callback
111 * Store a password in the secret service.
113 * The @attributes should be a set of key and value string pairs.
115 * If the attributes match a secret item already stored in the collection, then
116 * the item will be updated with these new values.
118 * If @collection is %NULL, then the default collection will be
119 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
120 * collection, which doesn't get stored across login sessions.
122 * This method will return immediately and complete asynchronously.
124 * Rename to: secret_password_store
127 secret_password_storev (const SecretSchema *schema,
128 GHashTable *attributes,
129 const gchar *collection,
131 const gchar *password,
132 GCancellable *cancellable,
133 GAsyncReadyCallback callback,
138 g_return_if_fail (schema != NULL);
139 g_return_if_fail (label != NULL);
140 g_return_if_fail (password != NULL);
141 g_return_if_fail (attributes != NULL);
142 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
144 /* Warnings raised already */
145 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
148 value = secret_value_new (password, -1, "text/plain");
150 secret_service_store (NULL, schema, attributes, collection,
151 label, value, cancellable, callback, user_data);
153 secret_value_unref (value);
157 * secret_password_store_finish:
158 * @result: the asynchronous result passed to the callback
159 * @error: location to place an error on failure
161 * Finish asynchronous operation to store a password in the secret service.
163 * Returns: whether the storage was successful or not
166 secret_password_store_finish (GAsyncResult *result,
169 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
170 return secret_service_store_finish (NULL, result, error);
174 * secret_password_store_sync:
175 * @schema: the schema for attributes
176 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
177 * @label: label for the secret
178 * @password: the null-terminated password to store
179 * @cancellable: optional cancellation object
180 * @error: location to place an error on failure
181 * @...: the attribute keys and values, terminated with %NULL
183 * Store a password in the secret service.
185 * The variable argument list should contain pairs of a) The attribute name as
186 * a null-terminated string, followed by b) attribute value, either a character
187 * string, an int number, or a gboolean value, as defined in the @schema.
188 * The list of attribtues should be terminated with a %NULL.
190 * If the attributes match a secret item already stored in the collection, then
191 * the item will be updated with these new values.
193 * If @collection is %NULL, then the default collection will be
194 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
195 * collection, which doesn't get stored across login sessions.
197 * This method may block indefinitely and should not be used in user interface
200 * Returns: whether the storage was successful or not
203 secret_password_store_sync (const SecretSchema *schema,
204 const gchar *collection,
206 const gchar *password,
207 GCancellable *cancellable,
211 GHashTable *attributes;
215 g_return_val_if_fail (schema != NULL, FALSE);
216 g_return_val_if_fail (label != NULL, FALSE);
217 g_return_val_if_fail (password != NULL, FALSE);
218 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
219 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
221 va_start (va, error);
222 attributes = secret_attributes_buildv (schema, va);
225 ret = secret_password_storev_sync (schema, attributes, collection,
226 label, password, cancellable, error);
228 g_hash_table_unref (attributes);
233 * secret_password_storev_sync:
234 * @schema: the schema for attributes
235 * @attributes: (element-type utf8 utf8): the attribute keys and values
236 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
237 * @label: label for the secret
238 * @password: the null-terminated password to store
239 * @cancellable: optional cancellation object
240 * @error: location to place an error on failure
242 * Store a password in the secret service.
244 * The @attributes should be a set of key and value string pairs.
246 * If the attributes match a secret item already stored in the collection, then
247 * the item will be updated with these new values.
249 * If @collection is %NULL, then the default collection will be
250 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
251 * collection, which doesn't get stored across login sessions.
253 * This method may block indefinitely and should not be used in user interface
256 * Returns: whether the storage was successful or not
258 * Rename to: secret_password_store_sync
261 secret_password_storev_sync (const SecretSchema *schema,
262 GHashTable *attributes,
263 const gchar *collection,
265 const gchar *password,
266 GCancellable *cancellable,
272 g_return_val_if_fail (schema != NULL, FALSE);
273 g_return_val_if_fail (label != NULL, FALSE);
274 g_return_val_if_fail (password != NULL, FALSE);
275 g_return_val_if_fail (attributes != NULL, FALSE);
276 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
277 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
279 /* Warnings raised already */
280 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
283 sync = _secret_sync_new ();
284 g_main_context_push_thread_default (sync->context);
286 secret_password_storev (schema, attributes, collection, label, password,
287 cancellable, _secret_sync_on_result, sync);
289 g_main_loop_run (sync->loop);
291 ret = secret_password_store_finish (sync->result, error);
293 g_main_context_pop_thread_default (sync->context);
294 _secret_sync_free (sync);
300 * secret_password_lookup: (skip)
301 * @schema: the schema for the attributes
302 * @cancellable: optional cancellation object
303 * @callback: called when the operation completes
304 * @user_data: data to be passed to the callback
305 * @...: the attribute keys and values, terminated with %NULL
307 * Lookup a password in the secret service.
309 * The variable argument list should contain pairs of a) The attribute name as
310 * a null-terminated string, followed by b) attribute value, either a character
311 * string, an int number, or a gboolean value, as defined in the password
312 * @schema. The list of attribtues should be terminated with a %NULL.
314 * If no secret is found then %NULL is returned.
316 * This method will return immediately and complete asynchronously.
319 secret_password_lookup (const SecretSchema *schema,
320 GCancellable *cancellable,
321 GAsyncReadyCallback callback,
325 GHashTable *attributes;
328 g_return_if_fail (schema != NULL);
329 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
331 va_start (va, user_data);
332 attributes = secret_attributes_buildv (schema, va);
335 secret_password_lookupv (schema, attributes, cancellable,
336 callback, user_data);
338 g_hash_table_unref (attributes);
342 * secret_password_lookupv:
343 * @schema: the schema for attributes
344 * @attributes: (element-type utf8 utf8): the attribute keys and values
345 * @cancellable: optional cancellation object
346 * @callback: called when the operation completes
347 * @user_data: data to be passed to the callback
349 * Lookup a password in the secret service.
351 * The @attributes should be a set of key and value string pairs.
353 * If no secret is found then %NULL is returned.
355 * This method will return immediately and complete asynchronously.
357 * Rename to: secret_password_lookup
360 secret_password_lookupv (const SecretSchema *schema,
361 GHashTable *attributes,
362 GCancellable *cancellable,
363 GAsyncReadyCallback callback,
366 g_return_if_fail (schema != NULL);
367 g_return_if_fail (attributes != NULL);
368 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
370 /* Warnings raised already */
371 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
374 secret_service_lookup (NULL, schema, attributes,
375 cancellable, callback, user_data);
379 * secret_password_lookup_nonpageable_finish: (skip)
380 * @result: the asynchronous result passed to the callback
381 * @error: location to place an error on failure
383 * Finish an asynchronous operation to lookup a password in the secret service.
385 * Returns: (transfer full): a new password string stored in nonpageable memory
386 * which must be freed with secret_password_free() when done
389 secret_password_lookup_nonpageable_finish (GAsyncResult *result,
394 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
396 value = secret_service_lookup_finish (NULL, result, error);
400 return _secret_value_unref_to_password (value);
404 * secret_password_lookup_finish:
405 * @result: the asynchronous result passed to the callback
406 * @error: location to place an error on failure
408 * Finish an asynchronous operation to lookup a password in the secret service.
410 * Returns: (transfer full): a new password string which should be freed with
411 * secret_password_free() or may be freed with g_free() when done
414 secret_password_lookup_finish (GAsyncResult *result,
419 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
421 value = secret_service_lookup_finish (NULL, result, error);
425 return _secret_value_unref_to_string (value);
429 * secret_password_lookup_sync: (skip)
430 * @schema: the schema for the attributes
431 * @cancellable: optional cancellation object
432 * @error: location to place an error on failure
433 * @...: the attribute keys and values, terminated with %NULL
435 * Lookup a password in the secret service.
437 * The variable argument list should contain pairs of a) The attribute name as
438 * a null-terminated string, followed by b) attribute value, either a character
439 * string, an int number, or a gboolean value, as defined in the password
440 * @schema. The list of attribtues should be terminated with a %NULL.
442 * If no secret is found then %NULL is returned.
444 * This method may block indefinitely and should not be used in user interface
447 * Returns: (transfer full): a new password string which should be freed with
448 * secret_password_free() or may be freed with g_free() when done
451 secret_password_lookup_sync (const SecretSchema *schema,
452 GCancellable *cancellable,
456 GHashTable *attributes;
460 g_return_val_if_fail (schema != NULL, NULL);
461 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
462 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
464 va_start (va, error);
465 attributes = secret_attributes_buildv (schema, va);
468 password = secret_password_lookupv_sync (schema, attributes,
471 g_hash_table_unref (attributes);
477 * secret_password_lookup_nonpageable_sync: (skip)
478 * @schema: the schema for the attributes
479 * @cancellable: optional cancellation object
480 * @error: location to place an error on failure
481 * @...: the attribute keys and values, terminated with %NULL
483 * Lookup a password in the secret service.
485 * The variable argument list should contain pairs of a) The attribute name as
486 * a null-terminated string, followed by b) attribute value, either a character
487 * string, an int number, or a gboolean value, as defined in the password
488 * @schema. The list of attribtues should be terminated with a %NULL.
490 * If no secret is found then %NULL is returned.
492 * This method may block indefinitely and should not be used in user interface
495 * Returns: (transfer full): a new password string stored in nonpageable memory
496 * which must be freed with secret_password_free() when done
499 secret_password_lookup_nonpageable_sync (const SecretSchema *schema,
500 GCancellable *cancellable,
504 GHashTable *attributes;
508 g_return_val_if_fail (schema != NULL, NULL);
509 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
510 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
512 va_start (va, error);
513 attributes = secret_attributes_buildv (schema, va);
516 password = secret_password_lookupv_nonpageable_sync (schema, attributes,
519 g_hash_table_unref (attributes);
525 * secret_password_lookupv_nonpageable_sync: (skip)
526 * @schema: the schema for attributes
527 * @attributes: (element-type utf8 utf8): the attribute keys and values
528 * @cancellable: optional cancellation object
529 * @error: location to place an error on failure
531 * Lookup a password in the secret service.
533 * The @attributes should be a set of key and value string pairs.
535 * If no secret is found then %NULL is returned.
537 * This method may block indefinitely and should not be used in user interface
540 * Returns: (transfer full): a new password string stored in non pageable memory
541 * which should be freed with secret_password_free() when done
544 secret_password_lookupv_nonpageable_sync (const SecretSchema *schema,
545 GHashTable *attributes,
546 GCancellable *cancellable,
552 g_return_val_if_fail (schema != NULL, NULL);
553 g_return_val_if_fail (attributes != NULL, NULL);
554 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
555 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
557 /* Warnings raised already */
558 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
561 sync = _secret_sync_new ();
562 g_main_context_push_thread_default (sync->context);
564 secret_password_lookupv (schema, attributes, cancellable,
565 _secret_sync_on_result, sync);
567 g_main_loop_run (sync->loop);
569 password = secret_password_lookup_nonpageable_finish (sync->result, error);
571 g_main_context_pop_thread_default (sync->context);
572 _secret_sync_free (sync);
578 * secret_password_lookupv_sync:
579 * @schema: the schema for attributes
580 * @attributes: (element-type utf8 utf8): the attribute keys and values
581 * @cancellable: optional cancellation object
582 * @error: location to place an error on failure
584 * Lookup a password in the secret service.
586 * The @attributes should be a set of key and value string pairs.
588 * If no secret is found then %NULL is returned.
590 * This method may block indefinitely and should not be used in user interface
593 * Returns: (transfer full): a new password string which should be freed with
594 * secret_password_free() or may be freed with g_free() when done
596 * Rename to: secret_password_lookup_sync
599 secret_password_lookupv_sync (const SecretSchema *schema,
600 GHashTable *attributes,
601 GCancellable *cancellable,
607 g_return_val_if_fail (schema != NULL, NULL);
608 g_return_val_if_fail (attributes != NULL, NULL);
609 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
610 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
612 /* Warnings raised already */
613 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
616 sync = _secret_sync_new ();
617 g_main_context_push_thread_default (sync->context);
619 secret_password_lookupv (schema, attributes, cancellable,
620 _secret_sync_on_result, sync);
622 g_main_loop_run (sync->loop);
624 string = secret_password_lookup_finish (sync->result, error);
626 g_main_context_pop_thread_default (sync->context);
627 _secret_sync_free (sync);
633 * secret_password_remove:
634 * @schema: the schema for the attributes
635 * @cancellable: optional cancellation object
636 * @callback: called when the operation completes
637 * @user_data: data to be passed to the callback
638 * @...: the attribute keys and values, terminated with %NULL
640 * Remove a password from the secret service.
642 * The variable argument list should contain pairs of a) The attribute name as
643 * a null-terminated string, followed by b) attribute value, either a character
644 * string, an int number, or a gboolean value, as defined in the password
645 * @schema. The list of attribtues should be terminated with a %NULL.
647 * If multiple items match the attributes, then only one will be deleted.
649 * This method will return immediately and complete asynchronously.
652 secret_password_remove (const SecretSchema *schema,
653 GCancellable *cancellable,
654 GAsyncReadyCallback callback,
658 GHashTable *attributes;
661 g_return_if_fail (schema != NULL);
662 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
664 va_start (va, user_data);
665 attributes = secret_attributes_buildv (schema, va);
668 secret_password_removev (schema, attributes, cancellable,
669 callback, user_data);
671 g_hash_table_unref (attributes);
676 * secret_password_removev:
677 * @schema: the schema for the attributes
678 * @attributes: (element-type utf8 utf8): the attribute keys and values
679 * @cancellable: optional cancellation object
680 * @callback: called when the operation completes
681 * @user_data: data to be passed to the callback
683 * Remove passwords from the secret service.
685 * The @attributes should be a set of key and value string pairs.
687 * All unlocked items that match the attributes will be deleted.
689 * This method will return immediately and complete asynchronously.
691 * Rename to: secret_password_remove
694 secret_password_removev (const SecretSchema *schema,
695 GHashTable *attributes,
696 GCancellable *cancellable,
697 GAsyncReadyCallback callback,
700 g_return_if_fail (schema != NULL);
701 g_return_if_fail (attributes != NULL);
702 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
704 /* Warnings raised already */
705 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
708 secret_service_remove (NULL, schema, attributes,
709 cancellable, callback, user_data);
713 * secret_password_remove_finish:
714 * @result: the asynchronous result passed to the callback
715 * @error: location to place an error on failure
717 * Finish an asynchronous operation to remove passwords from the secret
720 * Returns: whether any passwords were removed
723 secret_password_remove_finish (GAsyncResult *result,
726 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
727 return secret_service_remove_finish (NULL, result, error);
731 * secret_password_remove_sync:
732 * @schema: the schema for the attributes
733 * @cancellable: optional cancellation object
734 * @error: location to place an error on failure
735 * @...: the attribute keys and values, terminated with %NULL
737 * Remove passwords from the secret service.
739 * The variable argument list should contain pairs of a) The attribute name as
740 * a null-terminated string, followed by b) attribute value, either a character
741 * string, an int number, or a gboolean value, as defined in the password
742 * @schema. The list of attribtues should be terminated with a %NULL.
744 * All unlocked items that match the attributes will be deleted.
746 * This method may block indefinitely and should not be used in user interface
749 * Returns: whether the any passwords were removed
752 secret_password_remove_sync (const SecretSchema* schema,
753 GCancellable *cancellable,
757 GHashTable *attributes;
761 g_return_val_if_fail (schema != NULL, FALSE);
762 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
763 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
765 va_start (va, error);
766 attributes = secret_attributes_buildv (schema, va);
769 result = secret_password_removev_sync (schema, attributes,
772 g_hash_table_unref (attributes);
778 * secret_password_removev_sync:
779 * @schema: the schema for the attributes
780 * @attributes: (element-type utf8 utf8): the attribute keys and values
781 * @cancellable: optional cancellation object
782 * @error: location to place an error on failure
784 * Remove a password from the secret service.
786 * The @attributes should be a set of key and value string pairs.
788 * All unlocked items that match the attributes will be deleted.
790 * This method may block indefinitely and should not be used in user interface
793 * Returns: whether any passwords were removed
795 * Rename to: secret_password_remove_sync
798 secret_password_removev_sync (const SecretSchema *schema,
799 GHashTable *attributes,
800 GCancellable *cancellable,
806 g_return_val_if_fail (schema != NULL, FALSE);
807 g_return_val_if_fail (attributes != NULL, FALSE);
808 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
809 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
811 /* Warnings raised already */
812 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
815 sync = _secret_sync_new ();
816 g_main_context_push_thread_default (sync->context);
818 secret_password_removev (schema, attributes, cancellable,
819 _secret_sync_on_result, sync);
821 g_main_loop_run (sync->loop);
823 result = secret_password_remove_finish (sync->result, error);
825 g_main_context_pop_thread_default (sync->context);
826 _secret_sync_free (sync);
832 * secret_password_free: (skip)
833 * @password: (allow-none): password to free
835 * Clear the memory used by a password, and then free it.
837 * This function must be used to free nonpageable memory returned by
838 * secret_password_lookup_nonpageable_finish(),
839 * secret_password_lookup_nonpageable_sync() or
840 * secret_password_lookupv_nonpageable_sync().
843 secret_password_free (gchar *password)
845 if (password == NULL)
848 egg_secure_strfree (password);
852 * secret_password_clear:
853 * @password: (allow-none): password to clear
855 * Clear the memory used by a password.
858 secret_password_clear (gchar *password)
860 if (password == NULL)
863 egg_secure_strclear (password);