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.
42 * These functions have an unstable API and may change across versions. Use
43 * <literal>libsecret-unstable</literal> package to access them.
49 * secret_password_store: (skip)
50 * @schema: the schema for attributes
51 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
52 * @label: label for the secret
53 * @password: the null-terminated password to store
54 * @cancellable: optional cancellation object
55 * @callback: called when the operation completes
56 * @user_data: data to be passed to the callback
57 * @...: the attribute keys and values, terminated with %NULL
59 * Store a password in the secret service.
61 * The variable argument list should contain pairs of a) The attribute name as
62 * a null-terminated string, followed by b) attribute value, either a character
63 * string, an int number, or a gboolean value, as defined in the @schema.
64 * The list of attribtues should be terminated with a %NULL.
66 * If the attributes match a secret item already stored in the collection, then
67 * the item will be updated with these new values.
69 * If @collection is %NULL, then the default collection will be
70 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
71 * collection, which doesn't get stored across login sessions.
73 * This method will return immediately and complete asynchronously.
76 secret_password_store (const SecretSchema *schema,
77 const gchar *collection,
79 const gchar *password,
80 GCancellable *cancellable,
81 GAsyncReadyCallback callback,
85 GHashTable *attributes;
88 g_return_if_fail (schema != NULL);
89 g_return_if_fail (label != NULL);
90 g_return_if_fail (password != NULL);
91 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
93 va_start (va, user_data);
94 attributes = secret_attributes_buildv (schema, va);
97 /* Precondition failed, already warned */
101 secret_password_storev (schema, attributes, collection, label, password,
102 cancellable, callback, user_data);
104 g_hash_table_unref (attributes);
108 * secret_password_storev:
109 * @schema: the schema for attributes
110 * @attributes: (element-type utf8 utf8): the attribute keys and values
111 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
112 * @label: label for the secret
113 * @password: the null-terminated password to store
114 * @cancellable: optional cancellation object
115 * @callback: called when the operation completes
116 * @user_data: data to be passed to the callback
118 * Store a password in the secret service.
120 * The @attributes should be a set of key and value string pairs.
122 * If the attributes match a secret item already stored in the collection, then
123 * the item will be updated with these new values.
125 * If @collection is %NULL, then the default collection will be
126 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
127 * collection, which doesn't get stored across login sessions.
129 * This method will return immediately and complete asynchronously.
131 * Rename to: secret_password_store
134 secret_password_storev (const SecretSchema *schema,
135 GHashTable *attributes,
136 const gchar *collection,
138 const gchar *password,
139 GCancellable *cancellable,
140 GAsyncReadyCallback callback,
145 g_return_if_fail (schema != NULL);
146 g_return_if_fail (label != NULL);
147 g_return_if_fail (password != NULL);
148 g_return_if_fail (attributes != NULL);
149 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
151 /* Warnings raised already */
152 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
155 value = secret_value_new (password, -1, "text/plain");
157 secret_service_store (NULL, schema, attributes, collection,
158 label, value, cancellable, callback, user_data);
160 secret_value_unref (value);
164 * secret_password_store_finish:
165 * @result: the asynchronous result passed to the callback
166 * @error: location to place an error on failure
168 * Finish asynchronous operation to store a password in the secret service.
170 * Returns: whether the storage was successful or not
173 secret_password_store_finish (GAsyncResult *result,
176 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
177 return secret_service_store_finish (NULL, result, error);
181 * secret_password_store_sync:
182 * @schema: the schema for attributes
183 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
184 * @label: label for the secret
185 * @password: the null-terminated password to store
186 * @cancellable: optional cancellation object
187 * @error: location to place an error on failure
188 * @...: the attribute keys and values, terminated with %NULL
190 * Store a password in the secret service.
192 * The variable argument list should contain pairs of a) The attribute name as
193 * a null-terminated string, followed by b) attribute value, either a character
194 * string, an int number, or a gboolean value, as defined in the @schema.
195 * The list of attribtues should be terminated with a %NULL.
197 * If the attributes match a secret item already stored in the collection, then
198 * the item will be updated with these new values.
200 * If @collection is %NULL, then the default collection will be
201 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
202 * collection, which doesn't get stored across login sessions.
204 * This method may block indefinitely and should not be used in user interface
207 * Returns: whether the storage was successful or not
210 secret_password_store_sync (const SecretSchema *schema,
211 const gchar *collection,
213 const gchar *password,
214 GCancellable *cancellable,
218 GHashTable *attributes;
222 g_return_val_if_fail (schema != NULL, FALSE);
223 g_return_val_if_fail (label != NULL, FALSE);
224 g_return_val_if_fail (password != NULL, FALSE);
225 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
226 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
228 va_start (va, error);
229 attributes = secret_attributes_buildv (schema, va);
232 /* Precondition failed, already warned */
236 ret = secret_password_storev_sync (schema, attributes, collection,
237 label, password, cancellable, error);
239 g_hash_table_unref (attributes);
244 * secret_password_storev_sync:
245 * @schema: the schema for attributes
246 * @attributes: (element-type utf8 utf8): the attribute keys and values
247 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
248 * @label: label for the secret
249 * @password: the null-terminated password to store
250 * @cancellable: optional cancellation object
251 * @error: location to place an error on failure
253 * Store a password in the secret service.
255 * The @attributes should be a set of key and value string pairs.
257 * If the attributes match a secret item already stored in the collection, then
258 * the item will be updated with these new values.
260 * If @collection is %NULL, then the default collection will be
261 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
262 * collection, which doesn't get stored across login sessions.
264 * This method may block indefinitely and should not be used in user interface
267 * Returns: whether the storage was successful or not
269 * Rename to: secret_password_store_sync
272 secret_password_storev_sync (const SecretSchema *schema,
273 GHashTable *attributes,
274 const gchar *collection,
276 const gchar *password,
277 GCancellable *cancellable,
283 g_return_val_if_fail (schema != NULL, FALSE);
284 g_return_val_if_fail (label != NULL, FALSE);
285 g_return_val_if_fail (password != NULL, FALSE);
286 g_return_val_if_fail (attributes != NULL, FALSE);
287 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
288 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
290 /* Warnings raised already */
291 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
294 sync = _secret_sync_new ();
295 g_main_context_push_thread_default (sync->context);
297 secret_password_storev (schema, attributes, collection, label, password,
298 cancellable, _secret_sync_on_result, sync);
300 g_main_loop_run (sync->loop);
302 ret = secret_password_store_finish (sync->result, error);
304 g_main_context_pop_thread_default (sync->context);
305 _secret_sync_free (sync);
311 * secret_password_lookup: (skip)
312 * @schema: the schema for the attributes
313 * @cancellable: optional cancellation object
314 * @callback: called when the operation completes
315 * @user_data: data to be passed to the callback
316 * @...: the attribute keys and values, terminated with %NULL
318 * Lookup a password in the secret service.
320 * The variable argument list should contain pairs of a) The attribute name as
321 * a null-terminated string, followed by b) attribute value, either a character
322 * string, an int number, or a gboolean value, as defined in the password
323 * @schema. The list of attribtues should be terminated with a %NULL.
325 * If no secret is found then %NULL is returned.
327 * This method will return immediately and complete asynchronously.
330 secret_password_lookup (const SecretSchema *schema,
331 GCancellable *cancellable,
332 GAsyncReadyCallback callback,
336 GHashTable *attributes;
339 g_return_if_fail (schema != NULL);
340 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
342 va_start (va, user_data);
343 attributes = secret_attributes_buildv (schema, va);
346 /* Precondition failed, already warned */
350 secret_password_lookupv (schema, attributes, cancellable,
351 callback, user_data);
353 g_hash_table_unref (attributes);
357 * secret_password_lookupv:
358 * @schema: the schema for attributes
359 * @attributes: (element-type utf8 utf8): the attribute keys and values
360 * @cancellable: optional cancellation object
361 * @callback: called when the operation completes
362 * @user_data: data to be passed to the callback
364 * Lookup a password in the secret service.
366 * The @attributes should be a set of key and value string pairs.
368 * If no secret is found then %NULL is returned.
370 * This method will return immediately and complete asynchronously.
372 * Rename to: secret_password_lookup
375 secret_password_lookupv (const SecretSchema *schema,
376 GHashTable *attributes,
377 GCancellable *cancellable,
378 GAsyncReadyCallback callback,
381 g_return_if_fail (schema != NULL);
382 g_return_if_fail (attributes != NULL);
383 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
385 /* Warnings raised already */
386 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
389 secret_service_lookup (NULL, schema, attributes,
390 cancellable, callback, user_data);
394 * secret_password_lookup_nonpageable_finish: (skip)
395 * @result: the asynchronous result passed to the callback
396 * @error: location to place an error on failure
398 * Finish an asynchronous operation to lookup a password in the secret service.
400 * Returns: (transfer full): a new password string stored in nonpageable memory
401 * which must be freed with secret_password_free() when done
404 secret_password_lookup_nonpageable_finish (GAsyncResult *result,
409 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
411 value = secret_service_lookup_finish (NULL, result, error);
415 return _secret_value_unref_to_password (value);
419 * secret_password_lookup_finish:
420 * @result: the asynchronous result passed to the callback
421 * @error: location to place an error on failure
423 * Finish an asynchronous operation to lookup a password in the secret service.
425 * Returns: (transfer full): a new password string which should be freed with
426 * secret_password_free() or may be freed with g_free() when done
429 secret_password_lookup_finish (GAsyncResult *result,
434 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
436 value = secret_service_lookup_finish (NULL, result, error);
440 return _secret_value_unref_to_string (value);
444 * secret_password_lookup_sync: (skip)
445 * @schema: the schema for the attributes
446 * @cancellable: optional cancellation object
447 * @error: location to place an error on failure
448 * @...: the attribute keys and values, terminated with %NULL
450 * Lookup a password in the secret service.
452 * The variable argument list should contain pairs of a) The attribute name as
453 * a null-terminated string, followed by b) attribute value, either a character
454 * string, an int number, or a gboolean value, as defined in the password
455 * @schema. The list of attribtues should be terminated with a %NULL.
457 * If no secret is found then %NULL is returned.
459 * This method may block indefinitely and should not be used in user interface
462 * Returns: (transfer full): a new password string which should be freed with
463 * secret_password_free() or may be freed with g_free() when done
466 secret_password_lookup_sync (const SecretSchema *schema,
467 GCancellable *cancellable,
471 GHashTable *attributes;
475 g_return_val_if_fail (schema != NULL, NULL);
476 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
477 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
479 va_start (va, error);
480 attributes = secret_attributes_buildv (schema, va);
483 /* Precondition failed, already warned */
487 password = secret_password_lookupv_sync (schema, attributes,
490 g_hash_table_unref (attributes);
496 * secret_password_lookup_nonpageable_sync: (skip)
497 * @schema: the schema for the attributes
498 * @cancellable: optional cancellation object
499 * @error: location to place an error on failure
500 * @...: the attribute keys and values, terminated with %NULL
502 * Lookup a password in the secret service.
504 * The variable argument list should contain pairs of a) The attribute name as
505 * a null-terminated string, followed by b) attribute value, either a character
506 * string, an int number, or a gboolean value, as defined in the password
507 * @schema. The list of attribtues should be terminated with a %NULL.
509 * If no secret is found then %NULL is returned.
511 * This method may block indefinitely and should not be used in user interface
514 * Returns: (transfer full): a new password string stored in nonpageable memory
515 * which must be freed with secret_password_free() when done
518 secret_password_lookup_nonpageable_sync (const SecretSchema *schema,
519 GCancellable *cancellable,
523 GHashTable *attributes;
527 g_return_val_if_fail (schema != NULL, NULL);
528 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
529 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
531 va_start (va, error);
532 attributes = secret_attributes_buildv (schema, va);
535 /* Precondition failed, already warned */
539 password = secret_password_lookupv_nonpageable_sync (schema, attributes,
542 g_hash_table_unref (attributes);
548 * secret_password_lookupv_nonpageable_sync: (skip)
549 * @schema: the schema for attributes
550 * @attributes: (element-type utf8 utf8): the attribute keys and values
551 * @cancellable: optional cancellation object
552 * @error: location to place an error on failure
554 * Lookup a password in the secret service.
556 * The @attributes should be a set of key and value string pairs.
558 * If no secret is found then %NULL is returned.
560 * This method may block indefinitely and should not be used in user interface
563 * Returns: (transfer full): a new password string stored in non pageable memory
564 * which should be freed with secret_password_free() when done
567 secret_password_lookupv_nonpageable_sync (const SecretSchema *schema,
568 GHashTable *attributes,
569 GCancellable *cancellable,
575 g_return_val_if_fail (schema != NULL, NULL);
576 g_return_val_if_fail (attributes != NULL, NULL);
577 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
578 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
580 /* Warnings raised already */
581 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
584 sync = _secret_sync_new ();
585 g_main_context_push_thread_default (sync->context);
587 secret_password_lookupv (schema, attributes, cancellable,
588 _secret_sync_on_result, sync);
590 g_main_loop_run (sync->loop);
592 password = secret_password_lookup_nonpageable_finish (sync->result, error);
594 g_main_context_pop_thread_default (sync->context);
595 _secret_sync_free (sync);
601 * secret_password_lookupv_sync:
602 * @schema: the schema for attributes
603 * @attributes: (element-type utf8 utf8): the attribute keys and values
604 * @cancellable: optional cancellation object
605 * @error: location to place an error on failure
607 * Lookup a password in the secret service.
609 * The @attributes should be a set of key and value string pairs.
611 * If no secret is found then %NULL is returned.
613 * This method may block indefinitely and should not be used in user interface
616 * Returns: (transfer full): a new password string which should be freed with
617 * secret_password_free() or may be freed with g_free() when done
619 * Rename to: secret_password_lookup_sync
622 secret_password_lookupv_sync (const SecretSchema *schema,
623 GHashTable *attributes,
624 GCancellable *cancellable,
630 g_return_val_if_fail (schema != NULL, NULL);
631 g_return_val_if_fail (attributes != NULL, NULL);
632 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
633 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
635 /* Warnings raised already */
636 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
639 sync = _secret_sync_new ();
640 g_main_context_push_thread_default (sync->context);
642 secret_password_lookupv (schema, attributes, cancellable,
643 _secret_sync_on_result, sync);
645 g_main_loop_run (sync->loop);
647 string = secret_password_lookup_finish (sync->result, error);
649 g_main_context_pop_thread_default (sync->context);
650 _secret_sync_free (sync);
656 * secret_password_clear:
657 * @schema: the schema for the attributes
658 * @cancellable: optional cancellation object
659 * @callback: called when the operation completes
660 * @user_data: data to be passed to the callback
661 * @...: the attribute keys and values, terminated with %NULL
663 * Clear unlocked matching passwords from the secret service.
665 * The variable argument list should contain pairs of a) The attribute name as
666 * a null-terminated string, followed by b) attribute value, either a character
667 * string, an int number, or a gboolean value, as defined in the password
668 * @schema. The list of attribtues should be terminated with a %NULL.
670 * All unlocked items that match the attributes will be deleted.
672 * This method will return immediately and complete asynchronously.
675 secret_password_clear (const SecretSchema *schema,
676 GCancellable *cancellable,
677 GAsyncReadyCallback callback,
681 GHashTable *attributes;
684 g_return_if_fail (schema != NULL);
685 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
687 va_start (va, user_data);
688 attributes = secret_attributes_buildv (schema, va);
691 /* Precondition failed, already warned */
695 secret_password_clearv (schema, attributes, cancellable,
696 callback, user_data);
698 g_hash_table_unref (attributes);
703 * secret_password_clearv:
704 * @schema: the schema for the attributes
705 * @attributes: (element-type utf8 utf8): the attribute keys and values
706 * @cancellable: optional cancellation object
707 * @callback: called when the operation completes
708 * @user_data: data to be passed to the callback
710 * Remove unlocked matching passwords from the secret service.
712 * The @attributes should be a set of key and value string pairs.
714 * All unlocked items that match the attributes will be deleted.
716 * This method will return immediately and complete asynchronously.
718 * Rename to: secret_password_clear
721 secret_password_clearv (const SecretSchema *schema,
722 GHashTable *attributes,
723 GCancellable *cancellable,
724 GAsyncReadyCallback callback,
727 g_return_if_fail (schema != NULL);
728 g_return_if_fail (attributes != NULL);
729 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
731 /* Warnings raised already */
732 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
735 secret_service_clear (NULL, schema, attributes,
736 cancellable, callback, user_data);
740 * secret_password_clear_finish:
741 * @result: the asynchronous result passed to the callback
742 * @error: location to place an error on failure
744 * Finish an asynchronous operation to remove passwords from the secret
747 * Returns: whether any passwords were removed
750 secret_password_clear_finish (GAsyncResult *result,
753 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
754 return secret_service_clear_finish (NULL, result, error);
758 * secret_password_clear_sync:
759 * @schema: the schema for the attributes
760 * @cancellable: optional cancellation object
761 * @error: location to place an error on failure
762 * @...: the attribute keys and values, terminated with %NULL
764 * Remove unlocked matching passwords from the secret service.
766 * The variable argument list should contain pairs of a) The attribute name as
767 * a null-terminated string, followed by b) attribute value, either a character
768 * string, an int number, or a gboolean value, as defined in the password
769 * @schema. The list of attribtues should be terminated with a %NULL.
771 * All unlocked items that match the attributes will be deleted.
773 * This method may block indefinitely and should not be used in user interface
776 * Returns: whether the any passwords were removed
779 secret_password_clear_sync (const SecretSchema* schema,
780 GCancellable *cancellable,
784 GHashTable *attributes;
788 g_return_val_if_fail (schema != NULL, FALSE);
789 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
790 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
792 va_start (va, error);
793 attributes = secret_attributes_buildv (schema, va);
796 /* Precondition failed, already warned */
800 result = secret_password_clearv_sync (schema, attributes,
803 g_hash_table_unref (attributes);
809 * secret_password_clearv_sync:
810 * @schema: the schema for the attributes
811 * @attributes: (element-type utf8 utf8): the attribute keys and values
812 * @cancellable: optional cancellation object
813 * @error: location to place an error on failure
815 * Remove unlocked matching passwords from the secret service.
817 * The @attributes should be a set of key and value string pairs.
819 * All unlocked items that match the attributes will be deleted.
821 * This method may block indefinitely and should not be used in user interface
824 * Returns: whether any passwords were removed
826 * Rename to: secret_password_clear_sync
829 secret_password_clearv_sync (const SecretSchema *schema,
830 GHashTable *attributes,
831 GCancellable *cancellable,
837 g_return_val_if_fail (schema != NULL, FALSE);
838 g_return_val_if_fail (attributes != NULL, FALSE);
839 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
840 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
842 /* Warnings raised already */
843 if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
846 sync = _secret_sync_new ();
847 g_main_context_push_thread_default (sync->context);
849 secret_password_clearv (schema, attributes, cancellable,
850 _secret_sync_on_result, sync);
852 g_main_loop_run (sync->loop);
854 result = secret_password_clear_finish (sync->result, error);
856 g_main_context_pop_thread_default (sync->context);
857 _secret_sync_free (sync);
863 * secret_password_free: (skip)
864 * @password: (allow-none): password to free
866 * Clear the memory used by a password, and then free it.
868 * This function must be used to free nonpageable memory returned by
869 * secret_password_lookup_nonpageable_finish(),
870 * secret_password_lookup_nonpageable_sync() or
871 * secret_password_lookupv_nonpageable_sync().
874 secret_password_free (gchar *password)
876 if (password == NULL)
879 egg_secure_strfree (password);
883 * secret_password_wipe:
884 * @password: (allow-none): password to clear
886 * Clear the memory used by a password.
889 secret_password_wipe (gchar *password)
891 if (password == NULL)
894 egg_secure_strclear (password);