secret-password.c 46 KB
Newer Older
Stef Walter's avatar
Stef Walter committed
1
/* libsecret - GLib wrapper for Secret Service
Stef Walter's avatar
Stef Walter committed
2 3 4 5 6
 *
 * Copyright 2011 Collabora Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
Stef Walter's avatar
Stef Walter committed
7
 * by the Free Software Foundation; either version 2.1 of the licence or (at
Stef Walter's avatar
Stef Walter committed
8 9 10
 * your option) any later version.
 *
 * See the included COPYING file for more information.
Stef Walter's avatar
Stef Walter committed
11 12
 *
 * Author: Stef Walter <stefw@gnome.org>
Stef Walter's avatar
Stef Walter committed
13 14 15 16
 */

#include "config.h"

17
#include "secret-attributes.h"
Stef Walter's avatar
Stef Walter committed
18 19
#include "secret-password.h"
#include "secret-private.h"
20
#include "secret-retrievable.h"
Stef Walter's avatar
Stef Walter committed
21
#include "secret-value.h"
Stef Walter's avatar
Stef Walter committed
22 23 24

#include <egg/egg-secure-memory.h>

Stef Walter's avatar
Stef Walter committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * SECTION:secret-password
 * @title: Password storage
 * @short_description: Simple password storage and lookup
 *
 * This is a simple API for storing passwords and retrieving passwords in the
 * Secret Service.
 *
 * Each password is associated with a set of attributes. Attribute values can
 * be either strings, integers or booleans.
 *
 * The names and types of allowed attributes for a given password are defined
 * with a schema. Certain schemas are predefined. Additional schemas can be
 * defined via the %SecretSchema structure.
 *
 * Each of the functions accept a variable list of attributes names and their
 * values. Include a %NULL to terminate the list of attributes.
42
 *
43
 * Stability: Stable
Stef Walter's avatar
Stef Walter committed
44 45
 */

46
/**
47
 * secret_password_store: (skip)
48
 * @schema: the schema for attributes
49
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 * @label: label for the secret
 * @password: the null-terminated password to store
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Store a password in the secret service.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the @schema.
 * The list of attribtues should be terminated with a %NULL.
 *
 * If the attributes match a secret item already stored in the collection, then
 * the item will be updated with these new values.
 *
67
 * If @collection is %NULL, then the default collection will be
68 69 70
 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
 * collection, which doesn't get stored across login sessions.
 *
71 72
 * This method will return immediately and complete asynchronously.
 */
Stef Walter's avatar
Stef Walter committed
73
void
Stef Walter's avatar
Stef Walter committed
74
secret_password_store (const SecretSchema *schema,
75
                       const gchar *collection,
76 77 78 79 80 81
                       const gchar *label,
                       const gchar *password,
                       GCancellable *cancellable,
                       GAsyncReadyCallback callback,
                       gpointer user_data,
                       ...)
Stef Walter's avatar
Stef Walter committed
82
{
83
	GHashTable *attributes;
Stef Walter's avatar
Stef Walter committed
84 85 86 87 88 89 90 91
	va_list va;

	g_return_if_fail (schema != NULL);
	g_return_if_fail (label != NULL);
	g_return_if_fail (password != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

	va_start (va, user_data);
92
	attributes = secret_attributes_buildv (schema, va);
Stef Walter's avatar
Stef Walter committed
93 94
	va_end (va);

95 96 97 98
	/* Precondition failed, already warned */
	if (!attributes)
		return;

99
	secret_password_storev (schema, attributes, collection, label, password,
100
	                        cancellable, callback, user_data);
101 102 103 104

	g_hash_table_unref (attributes);
}

105
/**
106
 * secret_password_storev: (rename-to secret_password_store)
107
 * @schema: (nullable): the schema for attributes
108
 * @attributes: (element-type utf8 utf8): the attribute keys and values
109
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
110 111 112 113 114 115 116 117 118 119 120 121 122
 * @label: label for the secret
 * @password: the null-terminated password to store
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 *
 * Store a password in the secret service.
 *
 * The @attributes should be a set of key and value string pairs.
 *
 * If the attributes match a secret item already stored in the collection, then
 * the item will be updated with these new values.
 *
123
 * If @collection is %NULL, then the default collection will be
124 125 126
 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
 * collection, which doesn't get stored across login sessions.
 *
127 128
 * This method will return immediately and complete asynchronously.
 */
129
void
Stef Walter's avatar
Stef Walter committed
130
secret_password_storev (const SecretSchema *schema,
131
                        GHashTable *attributes,
132
                        const gchar *collection,
133 134 135 136 137
                        const gchar *label,
                        const gchar *password,
                        GCancellable *cancellable,
                        GAsyncReadyCallback callback,
                        gpointer user_data)
138
{
139
	SecretValue *value;
140 141 142 143 144

	g_return_if_fail (label != NULL);
	g_return_if_fail (password != NULL);
	g_return_if_fail (attributes != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Stef Walter's avatar
Stef Walter committed
145

146
	/* Warnings raised already */
147
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
148 149
		return;

150 151
	value = secret_value_new (password, -1, "text/plain");

152
	secret_service_store (NULL, schema, attributes, collection,
153 154 155
	                      label, value, cancellable, callback, user_data);

	secret_value_unref (value);
Stef Walter's avatar
Stef Walter committed
156 157
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/**
 * secret_password_store_binary: (skip)
 * @schema: the schema for attributes
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
 * @label: label for the secret
 * @value: a #SecretValue
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Store a password in the secret service.
 *
 * This is similar to secret_password_store(), but takes a
 * #SecretValue as the argument instead of a null-terminated password.
 *
 * This method will return immediately and complete asynchronously.
 *
 * Since: 0.19.0
 */
void
secret_password_store_binary (const SecretSchema *schema,
			      const gchar *collection,
			      const gchar *label,
			      SecretValue *value,
			      GCancellable *cancellable,
			      GAsyncReadyCallback callback,
			      gpointer user_data,
			      ...)
{
	GHashTable *attributes;
	va_list va;

	g_return_if_fail (schema != NULL);
	g_return_if_fail (label != NULL);
	g_return_if_fail (value != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

	va_start (va, user_data);
	attributes = secret_attributes_buildv (schema, va);
	va_end (va);

	/* Precondition failed, already warned */
	if (!attributes)
		return;

	secret_password_storev_binary (schema, attributes, collection, label, value,
				       cancellable, callback, user_data);

	g_hash_table_unref (attributes);
}

/**
 * secret_password_storev_binary: (rename-to secret_password_store_binary)
212
 * @schema: (nullable): the schema for attributes
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
 * @attributes: (element-type utf8 utf8): the attribute keys and values
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
 * @label: label for the secret
 * @value: a #SecretValue
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 *
 * Store a password in the secret service.
 *
 * This is similar to secret_password_storev(), but takes a
 * #SecretValue as the argument instead of a null-terminated password.
 *
 * This method will return immediately and complete asynchronously.
 *
 * Since: 0.19.0
 */
void
secret_password_storev_binary (const SecretSchema *schema,
			       GHashTable *attributes,
			       const gchar *collection,
			       const gchar *label,
			       SecretValue *value,
			       GCancellable *cancellable,
			       GAsyncReadyCallback callback,
			       gpointer user_data)
{
	g_return_if_fail (label != NULL);
	g_return_if_fail (value != NULL);
	g_return_if_fail (attributes != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

	/* Warnings raised already */
246
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
247 248 249 250 251 252
		return;

	secret_service_store (NULL, schema, attributes, collection,
	                      label, value, cancellable, callback, user_data);
}

253
/**
Stef Walter's avatar
Stef Walter committed
254
 * secret_password_store_finish:
255 256 257 258 259 260 261
 * @result: the asynchronous result passed to the callback
 * @error: location to place an error on failure
 *
 * Finish asynchronous operation to store a password in the secret service.
 *
 * Returns: whether the storage was successful or not
 */
Stef Walter's avatar
Stef Walter committed
262
gboolean
Stef Walter's avatar
Stef Walter committed
263
secret_password_store_finish (GAsyncResult *result,
264
                              GError **error)
Stef Walter's avatar
Stef Walter committed
265 266
{
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
267
	return secret_service_store_finish (NULL, result, error);
Stef Walter's avatar
Stef Walter committed
268 269
}

270 271 272
/**
 * secret_password_store_sync:
 * @schema: the schema for attributes
273
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
 * @label: label for the secret
 * @password: the null-terminated password to store
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Store a password in the secret service.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the @schema.
 * The list of attribtues should be terminated with a %NULL.
 *
 * If the attributes match a secret item already stored in the collection, then
 * the item will be updated with these new values.
 *
290
 * If @collection is %NULL, then the default collection will be
291 292 293
 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
 * collection, which doesn't get stored across login sessions.
 *
294 295 296 297 298
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: whether the storage was successful or not
 */
Stef Walter's avatar
Stef Walter committed
299
gboolean
Stef Walter's avatar
Stef Walter committed
300
secret_password_store_sync (const SecretSchema *schema,
301
                            const gchar *collection,
302 303 304 305 306
                            const gchar *label,
                            const gchar *password,
                            GCancellable *cancellable,
                            GError **error,
                            ...)
Stef Walter's avatar
Stef Walter committed
307
{
308 309 310 311 312 313 314 315 316 317 318
	GHashTable *attributes;
	va_list va;
	gboolean ret;

	g_return_val_if_fail (schema != NULL, FALSE);
	g_return_val_if_fail (label != NULL, FALSE);
	g_return_val_if_fail (password != NULL, FALSE);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	va_start (va, error);
319
	attributes = secret_attributes_buildv (schema, va);
320 321
	va_end (va);

322 323 324 325
	/* Precondition failed, already warned */
	if (!attributes)
		return FALSE;

326
	ret = secret_password_storev_sync (schema, attributes, collection,
327
	                                   label, password, cancellable, error);
328 329 330 331 332

	g_hash_table_unref (attributes);
	return ret;
}

333
/**
334
 * secret_password_storev_sync: (rename-to secret_password_store_sync)
335
 * @schema: (nullable): the schema for attributes
336
 * @attributes: (element-type utf8 utf8): the attribute keys and values
337
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
338 339 340 341 342 343 344 345 346 347 348 349
 * @label: label for the secret
 * @password: the null-terminated password to store
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 *
 * Store a password in the secret service.
 *
 * The @attributes should be a set of key and value string pairs.
 *
 * If the attributes match a secret item already stored in the collection, then
 * the item will be updated with these new values.
 *
350
 * If @collection is %NULL, then the default collection will be
351 352 353
 * used. Use #SECRET_COLLECTION_SESSION to store the password in the session
 * collection, which doesn't get stored across login sessions.
 *
354 355 356 357 358
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: whether the storage was successful or not
 */
359
gboolean
Stef Walter's avatar
Stef Walter committed
360
secret_password_storev_sync (const SecretSchema *schema,
361
                             GHashTable *attributes,
362
                             const gchar *collection,
363 364 365 366
                             const gchar *label,
                             const gchar *password,
                             GCancellable *cancellable,
                             GError **error)
367
{
Stef Walter's avatar
Stef Walter committed
368
	SecretSync *sync;
369 370 371
	gboolean ret;

	g_return_val_if_fail (label != NULL, FALSE);
Stef Walter's avatar
Stef Walter committed
372
	g_return_val_if_fail (password != NULL, FALSE);
373
	g_return_val_if_fail (attributes != NULL, FALSE);
Stef Walter's avatar
Stef Walter committed
374 375 376
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

377
	/* Warnings raised already */
378
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
379 380
		return FALSE;

Stef Walter's avatar
Stef Walter committed
381
	sync = _secret_sync_new ();
382
	g_main_context_push_thread_default (sync->context);
383

384
	secret_password_storev (schema, attributes, collection, label, password,
385
	                        cancellable, _secret_sync_on_result, sync);
386

387
	g_main_loop_run (sync->loop);
388

Stef Walter's avatar
Stef Walter committed
389
	ret = secret_password_store_finish (sync->result, error);
390

391
	g_main_context_pop_thread_default (sync->context);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	_secret_sync_free (sync);

	return ret;
}

/**
 * secret_password_store_binary_sync:
 * @schema: the schema for attributes
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
 * @label: label for the secret
 * @value: a #SecretValue
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Store a password in the secret service.
 *
 * This is similar to secret_password_store_sync(), but takes a
 * #SecretValue as the argument instead of a null terminated password.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: whether the storage was successful or not
 *
 * Since: 0.19.0
 */
gboolean
secret_password_store_binary_sync (const SecretSchema *schema,
				   const gchar *collection,
				   const gchar *label,
				   SecretValue *value,
				   GCancellable *cancellable,
				   GError **error,
				   ...)
{
	GHashTable *attributes;
	va_list va;
	gboolean ret;

	g_return_val_if_fail (schema != NULL, FALSE);
	g_return_val_if_fail (label != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	va_start (va, error);
	attributes = secret_attributes_buildv (schema, va);
	va_end (va);

	/* Precondition failed, already warned */
	if (!attributes)
		return FALSE;

	ret = secret_password_storev_binary_sync (schema, attributes, collection,
						  label, value, cancellable, error);

	g_hash_table_unref (attributes);
	return ret;
}

/**
 * secret_password_storev_binary_sync: (rename-to secret_password_store_binary_sync)
455
 * @schema: (nullable): the schema for attributes
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
 * @attributes: (element-type utf8 utf8): the attribute keys and values
 * @collection: (allow-none): a collection alias, or D-Bus object path of the collection where to store the secret
 * @label: label for the secret
 * @value: a #SecretValue
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 *
 * Store a password in the secret service.
 *
 * This is similar to secret_password_storev_sync(), but takes a
 * #SecretValue as the argument instead of a null-terminated passwords.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: whether the storage was successful or not
 *
 * Since: 0.19.0
 */
gboolean
secret_password_storev_binary_sync (const SecretSchema *schema,
				    GHashTable *attributes,
				    const gchar *collection,
				    const gchar *label,
				    SecretValue *value,
				    GCancellable *cancellable,
				    GError **error)
{
	SecretSync *sync;
	gboolean ret;

	g_return_val_if_fail (label != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);
	g_return_val_if_fail (attributes != NULL, FALSE);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	/* Warnings raised already */
494
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
495 496 497 498 499 500 501 502 503 504 505 506 507
		return FALSE;

	sync = _secret_sync_new ();
	g_main_context_push_thread_default (sync->context);

	secret_password_storev_binary (schema, attributes, collection, label, value,
				       cancellable, _secret_sync_on_result, sync);

	g_main_loop_run (sync->loop);

	ret = secret_password_store_finish (sync->result, error);

	g_main_context_pop_thread_default (sync->context);
Stef Walter's avatar
Stef Walter committed
508
	_secret_sync_free (sync);
509 510 511 512

	return ret;
}

513
/**
514
 * secret_password_lookup: (skip)
515
 * @schema: the schema for the attributes
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Lookup a password in the secret service.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attribtues should be terminated with a %NULL.
 *
 * If no secret is found then %NULL is returned.
 *
 * This method will return immediately and complete asynchronously.
 */
Stef Walter's avatar
Stef Walter committed
532
void
Stef Walter's avatar
Stef Walter committed
533
secret_password_lookup (const SecretSchema *schema,
534 535 536 537
                        GCancellable *cancellable,
                        GAsyncReadyCallback callback,
                        gpointer user_data,
                        ...)
Stef Walter's avatar
Stef Walter committed
538
{
539 540 541
	GHashTable *attributes;
	va_list va;

Stef Walter's avatar
Stef Walter committed
542 543 544
	g_return_if_fail (schema != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

545
	va_start (va, user_data);
546
	attributes = secret_attributes_buildv (schema, va);
547 548
	va_end (va);

549 550 551 552
	/* Precondition failed, already warned */
	if (!attributes)
		return;

553 554
	secret_password_lookupv (schema, attributes, cancellable,
	                         callback, user_data);
555 556 557 558

	g_hash_table_unref (attributes);
}

559
/**
560
 * secret_password_lookupv: (rename-to secret_password_lookup)
561
 * @schema: (nullable): the schema for attributes
562
 * @attributes: (element-type utf8 utf8): the attribute keys and values
563 564 565 566 567 568 569 570 571 572 573 574
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 *
 * Lookup a password in the secret service.
 *
 * The @attributes should be a set of key and value string pairs.
 *
 * If no secret is found then %NULL is returned.
 *
 * This method will return immediately and complete asynchronously.
 */
575
void
576 577 578 579 580
secret_password_lookupv (const SecretSchema *schema,
                         GHashTable *attributes,
                         GCancellable *cancellable,
                         GAsyncReadyCallback callback,
                         gpointer user_data)
581 582 583 584
{
	g_return_if_fail (attributes != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

585
	/* Warnings raised already */
586
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
587 588
		return;

589 590
	secret_service_lookup (NULL, schema, attributes,
	                       cancellable, callback, user_data);
Stef Walter's avatar
Stef Walter committed
591 592
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606
/**
 * secret_password_lookup_nonpageable_finish: (skip)
 * @result: the asynchronous result passed to the callback
 * @error: location to place an error on failure
 *
 * Finish an asynchronous operation to lookup a password in the secret service.
 *
 * Returns: (transfer full): a new password string stored in nonpageable memory
 *          which must be freed with secret_password_free() when done
 */
gchar *
secret_password_lookup_nonpageable_finish (GAsyncResult *result,
                                           GError **error)
{
607
	SecretValue *value;
608 609 610

	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

611 612
	value = secret_service_lookup_finish (NULL, result, error);
	if (value == NULL)
613 614
		return NULL;

615
	return _secret_value_unref_to_password (value);
616 617
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
/**
 * secret_password_lookup_binary_finish: (skip)
 * @result: the asynchronous result passed to the callback
 * @error: location to place an error on failure
 *
 * Finish an asynchronous operation to lookup a password in the secret service.
 *
 * Returns: (transfer full): a newly allocated #SecretValue, which should be
 *          released with secret_value_unref(), or %NULL if no secret found
 *
 * Since: 0.19.0
 */
SecretValue *
secret_password_lookup_binary_finish (GAsyncResult *result,
				      GError **error)
{
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	return secret_service_lookup_finish (NULL, result, error);
}

639 640 641 642 643 644 645 646
/**
 * secret_password_lookup_finish:
 * @result: the asynchronous result passed to the callback
 * @error: location to place an error on failure
 *
 * Finish an asynchronous operation to lookup a password in the secret service.
 *
 * Returns: (transfer full): a new password string which should be freed with
647
 *          secret_password_free() or may be freed with g_free() when done
648
 */
Stef Walter's avatar
Stef Walter committed
649
gchar *
Stef Walter's avatar
Stef Walter committed
650
secret_password_lookup_finish (GAsyncResult *result,
651
                               GError **error)
Stef Walter's avatar
Stef Walter committed
652
{
653
	SecretValue *value;
654

Stef Walter's avatar
Stef Walter committed
655
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);
656

657 658
	value = secret_service_lookup_finish (NULL, result, error);
	if (value == NULL)
659 660
		return NULL;

661
	return _secret_value_unref_to_string (value);
Stef Walter's avatar
Stef Walter committed
662 663
}

664
/**
665
 * secret_password_lookup_sync: (skip)
666
 * @schema: the schema for the attributes
667 668 669 670 671 672 673 674 675
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Lookup a password in the secret service.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
676
 * @schema. The list of attributes should be terminated with a %NULL.
677 678 679 680 681 682 683
 *
 * If no secret is found then %NULL is returned.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full): a new password string which should be freed with
684
 *          secret_password_free() or may be freed with g_free() when done
685
 */
Stef Walter's avatar
Stef Walter committed
686
gchar *
Stef Walter's avatar
Stef Walter committed
687
secret_password_lookup_sync (const SecretSchema *schema,
688 689 690
                             GCancellable *cancellable,
                             GError **error,
                             ...)
Stef Walter's avatar
Stef Walter committed
691
{
692 693 694 695
	GHashTable *attributes;
	gchar *password;
	va_list va;

Stef Walter's avatar
Stef Walter committed
696 697 698 699
	g_return_val_if_fail (schema != NULL, NULL);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

700
	va_start (va, error);
701
	attributes = secret_attributes_buildv (schema, va);
702 703
	va_end (va);

704 705 706 707
	/* Precondition failed, already warned */
	if (!attributes)
		return NULL;

708 709
	password = secret_password_lookupv_sync (schema, attributes,
	                                         cancellable, error);
710 711 712 713

	g_hash_table_unref (attributes);

	return password;
Stef Walter's avatar
Stef Walter committed
714 715
}

716 717
/**
 * secret_password_lookup_nonpageable_sync: (skip)
718
 * @schema: the schema for the attributes
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Lookup a password in the secret service.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attribtues should be terminated with a %NULL.
 *
 * If no secret is found then %NULL is returned.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full): a new password string stored in nonpageable memory
 *          which must be freed with secret_password_free() when done
 */
gchar *
secret_password_lookup_nonpageable_sync (const SecretSchema *schema,
                                         GCancellable *cancellable,
                                         GError **error,
                                         ...)
{
	GHashTable *attributes;
	gchar *password;
	va_list va;

	g_return_val_if_fail (schema != NULL, NULL);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	va_start (va, error);
753
	attributes = secret_attributes_buildv (schema, va);
754 755
	va_end (va);

756 757 758 759
	/* Precondition failed, already warned */
	if (!attributes)
		return NULL;

760 761 762 763 764 765 766 767 768 769
	password = secret_password_lookupv_nonpageable_sync (schema, attributes,
	                                                     cancellable, error);

	g_hash_table_unref (attributes);

	return password;
}

/**
 * secret_password_lookupv_nonpageable_sync: (skip)
770
 * @schema: (nullable): the schema for attributes
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
 * @attributes: (element-type utf8 utf8): the attribute keys and values
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 *
 * Lookup a password in the secret service.
 *
 * The @attributes should be a set of key and value string pairs.
 *
 * If no secret is found then %NULL is returned.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full): a new password string stored in non pageable memory
 *          which should be freed with secret_password_free() when done
 */
gchar *
secret_password_lookupv_nonpageable_sync (const SecretSchema *schema,
                                          GHashTable *attributes,
                                          GCancellable *cancellable,
                                          GError **error)
{
	SecretSync *sync;
	gchar *password;

	g_return_val_if_fail (attributes != NULL, NULL);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	/* Warnings raised already */
801
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		return FALSE;

	sync = _secret_sync_new ();
	g_main_context_push_thread_default (sync->context);

	secret_password_lookupv (schema, attributes, cancellable,
	                         _secret_sync_on_result, sync);

	g_main_loop_run (sync->loop);

	password = secret_password_lookup_nonpageable_finish (sync->result, error);

	g_main_context_pop_thread_default (sync->context);
	_secret_sync_free (sync);

	return password;
}

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/**
 * secret_password_lookup_binary_sync: (skip)
 * @schema: the schema for the attributes
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Lookup a password in the secret service.
 *
 * This is similar to secret_password_lookup_sync(), but returns a
 * #SecretValue instead of a null-terminated password.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full): a newly allocated #SecretValue, which should be
 *          released with secret_value_unref(), or %NULL if no secret found
 *
 * Since: 0.19.0
 */
SecretValue *
secret_password_lookup_binary_sync (const SecretSchema *schema,
				    GCancellable *cancellable,
				    GError **error,
				    ...)
{
	GHashTable *attributes;
	SecretValue *value;
	va_list va;

	g_return_val_if_fail (schema != NULL, NULL);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	va_start (va, error);
	attributes = secret_attributes_buildv (schema, va);
	va_end (va);

	/* Precondition failed, already warned */
	if (!attributes)
		return NULL;

	value = secret_password_lookupv_binary_sync (schema, attributes,
						     cancellable, error);

	g_hash_table_unref (attributes);

	return value;
}

/**
 * secret_password_lookupv_binary_sync: (skip)
872
 * @schema: (nullable): the schema for attributes
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
 * @attributes: (element-type utf8 utf8): the attribute keys and values
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 *
 * Lookup a password in the secret service.
 *
 * This is similar to secret_password_lookupv_sync(), but returns a
 * #SecretValue instead of a null-terminated password.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full): a newly allocated #SecretValue, which should be
 *          released with secret_value_unref(), or %NULL if no secret found
 *
 * Since: 0.19.0
 */
SecretValue *
secret_password_lookupv_binary_sync (const SecretSchema *schema,
				     GHashTable *attributes,
				     GCancellable *cancellable,
				     GError **error)
{
	SecretSync *sync;
	SecretValue *value;

	g_return_val_if_fail (attributes != NULL, NULL);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	/* Warnings raised already */
904
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
		return FALSE;

	sync = _secret_sync_new ();
	g_main_context_push_thread_default (sync->context);

	secret_password_lookupv (schema, attributes, cancellable,
	                         _secret_sync_on_result, sync);

	g_main_loop_run (sync->loop);

	value = secret_password_lookup_binary_finish (sync->result, error);

	g_main_context_pop_thread_default (sync->context);
	_secret_sync_free (sync);

	return value;
}

923
/**
924
 * secret_password_lookupv_sync: (rename-to secret_password_lookup_sync)
925
 * @schema: (nullable): the schema for attributes
926
 * @attributes: (element-type utf8 utf8): the attribute keys and values
927 928 929 930 931 932 933 934 935 936 937 938 939
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 *
 * Lookup a password in the secret service.
 *
 * The @attributes should be a set of key and value string pairs.
 *
 * If no secret is found then %NULL is returned.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full): a new password string which should be freed with
940
 *          secret_password_free() or may be freed with g_free() when done
941
 */
942
gchar *
943 944 945 946
secret_password_lookupv_sync (const SecretSchema *schema,
                              GHashTable *attributes,
                              GCancellable *cancellable,
                              GError **error)
947
{
Stef Walter's avatar
Stef Walter committed
948
	SecretSync *sync;
949
	gchar *string;
950 951 952 953 954

	g_return_val_if_fail (attributes != NULL, NULL);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

955
	/* Warnings raised already */
956
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
957 958
		return FALSE;

Stef Walter's avatar
Stef Walter committed
959
	sync = _secret_sync_new ();
960
	g_main_context_push_thread_default (sync->context);
961

962 963
	secret_password_lookupv (schema, attributes, cancellable,
	                         _secret_sync_on_result, sync);
964

965
	g_main_loop_run (sync->loop);
966

967
	string = secret_password_lookup_finish (sync->result, error);
968

969
	g_main_context_pop_thread_default (sync->context);
Stef Walter's avatar
Stef Walter committed
970
	_secret_sync_free (sync);
971

972
	return string;
973
}
Stef Walter's avatar
Stef Walter committed
974

975
/**
976
 * secret_password_clear:
977
 * @schema: the schema for the attributes
978 979 980 981 982
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 * @...: the attribute keys and values, terminated with %NULL
 *
983
 * Clear unlocked matching passwords from the secret service.
984 985 986 987 988 989
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attribtues should be terminated with a %NULL.
 *
990
 * All unlocked items that match the attributes will be deleted.
991 992 993
 *
 * This method will return immediately and complete asynchronously.
 */
Stef Walter's avatar
Stef Walter committed
994
void
995 996 997 998 999
secret_password_clear (const SecretSchema *schema,
                       GCancellable *cancellable,
                       GAsyncReadyCallback callback,
                       gpointer user_data,
                       ...)
Stef Walter's avatar
Stef Walter committed
1000 1001 1002 1003 1004 1005 1006 1007
{
	GHashTable *attributes;
	va_list va;

	g_return_if_fail (schema != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

	va_start (va, user_data);
1008
	attributes = secret_attributes_buildv (schema, va);
Stef Walter's avatar
Stef Walter committed
1009 1010
	va_end (va);

1011 1012 1013 1014
	/* Precondition failed, already warned */
	if (!attributes)
		return;

1015 1016
	secret_password_clearv (schema, attributes, cancellable,
	                        callback, user_data);
Stef Walter's avatar
Stef Walter committed
1017 1018 1019 1020 1021

	g_hash_table_unref (attributes);
}


1022
/**
1023
 * secret_password_clearv: (rename-to secret_password_clear)
1024
 * @schema: (nullable): the schema for the attributes
1025
 * @attributes: (element-type utf8 utf8): the attribute keys and values
1026 1027 1028 1029
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 *
1030
 * Remove unlocked matching passwords from the secret service.
1031 1032 1033
 *
 * The @attributes should be a set of key and value string pairs.
 *
1034
 * All unlocked items that match the attributes will be deleted.
1035 1036 1037
 *
 * This method will return immediately and complete asynchronously.
 */
Stef Walter's avatar
Stef Walter committed
1038
void
1039 1040 1041 1042 1043
secret_password_clearv (const SecretSchema *schema,
                        GHashTable *attributes,
                        GCancellable *cancellable,
                        GAsyncReadyCallback callback,
                        gpointer user_data)
Stef Walter's avatar
Stef Walter committed
1044 1045 1046 1047
{
	g_return_if_fail (attributes != NULL);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

1048
	/* Warnings raised already */
1049
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
1050 1051
		return;

1052 1053
	secret_service_clear (NULL, schema, attributes,
	                      cancellable, callback, user_data);
Stef Walter's avatar
Stef Walter committed
1054 1055
}

1056
/**
1057
 * secret_password_clear_finish:
1058 1059 1060
 * @result: the asynchronous result passed to the callback
 * @error: location to place an error on failure
 *
1061
 * Finish an asynchronous operation to remove passwords from the secret
1062 1063
 * service.
 *
1064
 * Returns: whether any passwords were removed
1065
 */
Stef Walter's avatar
Stef Walter committed
1066
gboolean
1067 1068
secret_password_clear_finish (GAsyncResult *result,
                              GError **error)
Stef Walter's avatar
Stef Walter committed
1069 1070
{
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1071
	return secret_service_clear_finish (NULL, result, error);
Stef Walter's avatar
Stef Walter committed
1072 1073
}

1074
/**
1075
 * secret_password_clear_sync:
1076
 * @schema: the schema for the attributes
1077 1078 1079 1080
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 * @...: the attribute keys and values, terminated with %NULL
 *
1081
 * Remove unlocked matching passwords from the secret service.
1082 1083 1084 1085 1086 1087
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attribtues should be terminated with a %NULL.
 *
1088
 * All unlocked items that match the attributes will be deleted.
1089 1090 1091 1092
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
1093
 * Returns: whether the any passwords were removed
1094
 */
Stef Walter's avatar
Stef Walter committed
1095
gboolean
1096 1097 1098 1099
secret_password_clear_sync (const SecretSchema* schema,
                            GCancellable *cancellable,
                            GError **error,
                            ...)
Stef Walter's avatar
Stef Walter committed
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
{
	GHashTable *attributes;
	gboolean result;
	va_list va;

	g_return_val_if_fail (schema != NULL, FALSE);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	va_start (va, error);
1110
	attributes = secret_attributes_buildv (schema, va);
Stef Walter's avatar
Stef Walter committed
1111 1112
	va_end (va);

1113 1114 1115 1116
	/* Precondition failed, already warned */
	if (!attributes)
		return FALSE;

1117 1118
	result = secret_password_clearv_sync (schema, attributes,
	                                      cancellable, error);
Stef Walter's avatar
Stef Walter committed
1119 1120 1121 1122 1123 1124

	g_hash_table_unref (attributes);

	return result;
}

1125
/**
1126
 * secret_password_clearv_sync: (rename-to secret_password_clear_sync)
1127
 * @schema: (nullable): the schema for the attributes
1128
 * @attributes: (element-type utf8 utf8): the attribute keys and values
1129 1130 1131
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 *
1132
 * Remove unlocked matching passwords from the secret service.
1133 1134 1135
 *
 * The @attributes should be a set of key and value string pairs.
 *
1136
 * All unlocked items that match the attributes will be deleted.
1137 1138 1139 1140
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
1141
 * Returns: whether any passwords were removed
1142
 */
Stef Walter's avatar
Stef Walter committed
1143
gboolean
1144 1145 1146 1147
secret_password_clearv_sync (const SecretSchema *schema,
                             GHashTable *attributes,
                             GCancellable *cancellable,
                             GError **error)
Stef Walter's avatar
Stef Walter committed
1148
{
Stef Walter's avatar
Stef Walter committed
1149
	SecretSync *sync;
Stef Walter's avatar
Stef Walter committed
1150 1151 1152 1153 1154 1155
	gboolean result;

	g_return_val_if_fail (attributes != NULL, FALSE);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

1156
	/* Warnings raised already */
1157
	if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
1158 1159
		return FALSE;

Stef Walter's avatar
Stef Walter committed
1160
	sync = _secret_sync_new ();
1161
	g_main_context_push_thread_default (sync->context);
Stef Walter's avatar
Stef Walter committed
1162

1163 1164
	secret_password_clearv (schema, attributes, cancellable,
	                        _secret_sync_on_result, sync);
Stef Walter's avatar
Stef Walter committed
1165

1166
	g_main_loop_run (sync->loop);
Stef Walter's avatar
Stef Walter committed
1167

1168
	result = secret_password_clear_finish (sync->result, error);
Stef Walter's avatar
Stef Walter committed
1169

1170
	g_main_context_pop_thread_default (sync->context);
Stef Walter's avatar
Stef Walter committed
1171
	_secret_sync_free (sync);
Stef Walter's avatar
Stef Walter committed
1172 1173 1174 1175

	return result;
}

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/**
 * secret_password_search: (skip)
 * @schema: the schema for the attributes
 * @flags: search option flags
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Search for items in the secret service.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attribtues should be terminated with a %NULL.
 *
 * This method will return immediately and complete asynchronously.
 *
 * Since: 0.19.0
 */
void
secret_password_search (const SecretSchema *schema,
                        SecretSearchFlags flags,
                        GCancellable *cancellable,
                        GAsyncReadyCallback callback,
                        gpointer user_data,
                        ...)
{
        GHashTable *attributes;
        va_list va;

        g_return_if_fail (schema != NULL);
        g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

        va_start (va, user_data);
        attributes = secret_attributes_buildv (schema, va);
        va_end (va);

        /* Precondition failed, already warned */
        if (!attributes)
                return;

        secret_password_searchv (schema, attributes, flags, cancellable,
                                 callback, user_data);

        g_hash_table_unref (attributes);
}

/**
 * secret_password_searchv: (rename-to secret_password_search)
1226
 * @schema: (nullable): the schema for attributes
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
 * @attributes: (element-type utf8 utf8): the attribute keys and values
 * @flags: search option flags
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 *
 * Search for items in the secret service.
 *
 * The @attributes should be a set of key and value string pairs.
 *
 * This method will return immediately and complete asynchronously.
 *
 * Since: 0.19.0
 */
void
secret_password_searchv (const SecretSchema *schema,
                         GHashTable *attributes,
                         SecretSearchFlags flags,
                         GCancellable *cancellable,
                         GAsyncReadyCallback callback,
                         gpointer user_data)
{
        g_return_if_fail (attributes != NULL);
        g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

        /* Warnings raised already */
1253
        if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
                return;

        secret_service_search (NULL, schema, attributes, flags,
                               cancellable, callback, user_data);
}

/**
 * secret_password_search_finish:
 * @result: the asynchronous result passed to the callback
 * @error: location to place an error on failure
 *
 * Finish an asynchronous operation to search for items in the secret service.
 *
 * Returns: (transfer full) (element-type Secret.Retrievable): a list of
 *          #SecretRetrievable containing attributes of the matched items
 *
 * Since: 0.19.0
 */
GList *
secret_password_search_finish (GAsyncResult *result,
                               GError **error)
{
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);

        return secret_service_search_finish (NULL, result, error);
}

/**
 * secret_password_search_sync: (skip)
 * @schema: the schema for the attributes
 * @flags: search option flags
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 * @...: the attribute keys and values, terminated with %NULL
 *
 * Search for items in the secret service.
 *
 * The variable argument list should contain pairs of a) The attribute name as
 * a null-terminated string, followed by b) attribute value, either a character
 * string, an int number, or a gboolean value, as defined in the password
 * @schema. The list of attributes should be terminated with a %NULL.
 *
 * If no secret is found then %NULL is returned.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full) (element-type Secret.Retrievable): a list of
 *          #SecretRetrievable containing attributes of the matched items
 *
 * Since: 0.19.0
 */
GList *
secret_password_search_sync (const SecretSchema *schema,
                             SecretSearchFlags flags,
                             GCancellable *cancellable,
                             GError **error,
                             ...)
{
        GHashTable *attributes;
        GList *items;
        va_list va;

        g_return_val_if_fail (schema != NULL, NULL);
        g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);

        va_start (va, error);
        attributes = secret_attributes_buildv (schema, va);
        va_end (va);

        /* Precondition failed, already warned */
        if (!attributes)
                return NULL;

        items = secret_password_searchv_sync (schema, attributes, flags,
                                              cancellable, error);

        g_hash_table_unref (attributes);

        return items;
}

/**
 * secret_password_searchv_sync: (rename-to secret_password_search_sync)
1339
 * @schema: (nullable): the schema for attributes
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
 * @attributes: (element-type utf8 utf8): the attribute keys and values
 * @flags: search option flags
 * @cancellable: optional cancellation object
 * @error: location to place an error on failure
 *
 * Search for items in the secret service.
 *
 * The @attributes should be a set of key and value string pairs.
 *
 * If no secret is found then %NULL is returned.
 *
 * This method may block indefinitely and should not be used in user interface
 * threads.
 *
 * Returns: (transfer full) (element-type Secret.Retrievable): a list of
 *          #SecretRetrievable containing attributes of the matched items
 *
 * Since: 0.19.0
 */
GList *
secret_password_searchv_sync (const SecretSchema *schema,
                              GHashTable *attributes,
                              SecretSearchFlags flags,
                              GCancellable *cancellable,
                              GError **error)
{
        SecretSync *sync;
        GList *items;

        g_return_val_if_fail (attributes != NULL, NULL);
        g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);

        /* Warnings raised already */
1374
        if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
                return NULL;

        sync = _secret_sync_new ();
        g_main_context_push_thread_default (sync->context);

        secret_password_searchv (schema, attributes, flags, cancellable,
                                 _secret_sync_on_result, sync);

        g_main_loop_run (sync->loop);

        items = secret_password_search_finish (sync->result, error);

        g_main_context_pop_thread_default (sync->context);
        _secret_sync_free (sync);

        return items;
}

1393
/**
1394
 * secret_password_free: (skip)
1395
 * @password: (allow-none): password to free
1396
 *
1397 1398 1399 1400 1401 1402
 * Clear the memory used by a password, and then free it.
 *
 * This function must be used to free nonpageable memory returned by
 * secret_password_lookup_nonpageable_finish(),
 * secret_password_lookup_nonpageable_sync() or
 * secret_password_lookupv_nonpageable_sync().
1403
 */
Stef Walter's avatar
Stef Walter committed
1404
void
1405
secret_password_free (gchar *password)
Stef Walter's avatar
Stef Walter committed
1406 1407 1408 1409 1410 1411
{
	if (password == NULL)
		return;

	egg_secure_strfree (password);
}
1412 1413

/**
1414
 * secret_password_wipe:
1415 1416 1417 1418 1419
 * @password: (allow-none): password to clear
 *
 * Clear the memory used by a password.
 */
void
1420
secret_password_wipe (gchar *password)
1421 1422 1423 1424 1425 1426
{
	if (password == NULL)
		return;

	egg_secure_strclear (password);
}