1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
212
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
455
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
/*
* gnome-keyring
*
* Copyright (C) 2008 Stefan Walter
*
* 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 by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "config.h"
#include "gkd-dbus-util.h"
#include "gkd-secret-dispatch.h"
#include "gkd-secret-introspect.h"
#include "gkd-secret-objects.h"
#include "gkd-secret-secret.h"
#include "gkd-secret-session.h"
#include "gkd-secret-service.h"
#include "gkd-secret-types.h"
#include "gkd-secret-unlock.h"
#include "gkd-secret-util.h"
#include "egg/egg-error.h"
#include "egg/egg-secure-memory.h"
#include "login/gkd-login.h"
#include "pkcs11/pkcs11i.h"
#include <glib/gi18n.h>
#include <gck/gck.h>
#include <string.h>
/*
* We try to serialize unlock requests, so the user doesn't get prompted
* multiple times for the same thing. There are two queues:
* - self->queued: A queue of object paths per unlock requests.
* - unlock_prompt_queue: A queue of unlock requests ready to prompt.
*/
enum {
PROP_0,
PROP_CALLER,
PROP_OBJECT_PATH,
PROP_SERVICE
};
struct _GkdSecretUnlock {
GObject parent;
gchar *object_path;
GkdSecretService *service;
gchar *caller;
gchar *window_id;
GQueue *queued;
gchar *current;
GArray *results;
gboolean prompted;
gboolean completed;
GCancellable *cancellable;
};
/* Forward declarations */
static void gkd_secret_dispatch_iface (GkdSecretDispatchIface *iface);
static void perform_next_unlock (GkdSecretUnlock *self);
G_DEFINE_TYPE_WITH_CODE (GkdSecretUnlock, gkd_secret_unlock, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GKD_SECRET_TYPE_DISPATCH, gkd_secret_dispatch_iface));
static guint unique_prompt_number = 0;
static GQueue unlock_prompt_queue = G_QUEUE_INIT;
/* -----------------------------------------------------------------------------
* INTERNAL
*/
static GckObject*
lookup_collection (GkdSecretUnlock *self, const gchar *path)
{
GkdSecretObjects *objects = gkd_secret_service_get_objects (self->service);
return gkd_secret_objects_lookup_collection (objects, self->caller, path);
}
static gboolean
check_locked_collection (GckObject *collection, gboolean *locked)
{
GError *error = NULL;
gpointer value;
gsize n_value;
value = gck_object_get_data (collection, CKA_G_LOCKED, NULL, &n_value, &error);
if (value == NULL) {
if (!g_error_matches (error, GCK_ERROR, CKR_OBJECT_HANDLE_INVALID))
g_warning ("couldn't check locked status of collection: %s",
egg_error_message (error));
return FALSE;
}
*locked = (value && n_value == sizeof (CK_BBOOL) && *(CK_BBOOL*)value);
g_free (value);
return TRUE;
}
static void
common_unlock_attributes (GckAttributes *attrs, GckObject *collection)
{
g_assert (attrs);
g_assert (GCK_IS_OBJECT (collection));
gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_G_CREDENTIAL);
gck_attributes_add_ulong (attrs, CKA_G_OBJECT, gck_object_get_handle (collection));
}
static gboolean
mark_as_complete (GkdSecretUnlock *self, gboolean dismissed)
{
GkdSecretUnlock *other;
DBusMessage *signal;
DBusMessageIter iter;
dbus_bool_t bval;
DBusMessageIter variant;
DBusMessageIter array;
const char *value;
gint i;
if (self->completed)
return FALSE;
self->completed = TRUE;
signal = dbus_message_new_signal (self->object_path, SECRET_PROMPT_INTERFACE,
"Completed");
dbus_message_set_destination (signal, self->caller);
dbus_message_iter_init_append (signal, &iter);
bval = dismissed;
dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &bval);
dbus_message_iter_open_container (&iter, DBUS_TYPE_VARIANT, "ao", &variant);
dbus_message_iter_open_container (&variant, DBUS_TYPE_ARRAY, "o", &array);
for (i = 0; i < self->results->len; ++i) {
value = g_array_index (self->results, gchar*, i);
dbus_message_iter_append_basic (&array, DBUS_TYPE_OBJECT_PATH, &value);
}
dbus_message_iter_close_container (&variant, &array);
dbus_message_iter_close_container (&iter, &variant);
gkd_secret_service_send (self->service, signal);
dbus_message_unref (signal);
/* Fire off the next item in the unlock prompt queue */
other = g_queue_pop_head (&unlock_prompt_queue);
if (other != NULL) {
perform_next_unlock (other);
g_object_unref (other);
}
return TRUE;
}
static void
on_unlock_complete (GObject *object, GAsyncResult *res, gpointer user_data)
{
GkdSecretUnlock *self = GKD_SECRET_UNLOCK (user_data);
GkdSecretUnlock *other;
GckObject *cred;
GError *error = NULL;
/* We should be at the front of the unlock queue, pop ourselves */
other = g_queue_pop_head (&unlock_prompt_queue);
if (other == self)
g_object_unref (other);
else
g_warning ("unlock prompt queue is out of sync with prompts");
/* Now process the results */
cred = gck_session_create_object_finish (GCK_SESSION (object), res, &error);
/* Successfully authentication */
if (cred) {
g_object_unref (cred);
g_array_append_val (self->results, self->current);
self->current = NULL;
perform_next_unlock (self);
/* The user cancelled the protected auth prompt */
} else if (g_error_matches (error, GCK_ERROR, CKR_PIN_INCORRECT)) {
g_free (self->current);
self->current = NULL;
mark_as_complete (self, TRUE);
/* The operation was cancelled via Dismiss call */
} else if (g_error_matches (error, GCK_ERROR, CKR_CANCEL)) {
/* Should have been the result of a dismiss */
g_return_if_fail (self->completed);
/* Another error, something's broken */
} else {
g_warning ("couldn't create credential for collection: %s",
egg_error_message (error));
}
g_clear_error (&error);
/* refed for async call */
g_object_unref (self);
}
static void
perform_next_unlock (GkdSecretUnlock *self)
{
GckObject *collection;
GckAttributes *template;
GckSession *session;
gboolean locked;
gboolean proceed;
gchar *objpath;
for (;;) {
g_assert (!self->current);
objpath = g_queue_pop_head (self->queued);
/* Nothing more to prompt for? */
if (!objpath) {
mark_as_complete (self, FALSE);
break;
}
/* Find the collection, make sure it's still around */
collection = lookup_collection (self, objpath);
if (collection == NULL) {
g_free (objpath);
continue;
}
if (!check_locked_collection (collection, &locked)) {
g_object_unref (collection);
g_free (objpath);
continue;
} else if (!locked) {
g_array_append_val (self->results, objpath);
g_object_unref (collection);
continue;
}
/* Add ourselves to the unlock prompt queue */
proceed = g_queue_is_empty (&unlock_prompt_queue);
g_queue_push_tail (&unlock_prompt_queue, g_object_ref (self));
/*
* Proceed with this unlock request. The on_unlock_complete callback
* pops us back off the unlock prompt queue
*/
if (proceed) {
template = gck_attributes_new ();
common_unlock_attributes (template, collection);
gck_attributes_add_data (template, CKA_VALUE, NULL, 0);
session = gkd_secret_service_get_pkcs11_session (self->service, self->caller);
gck_session_create_object_async (session, template, self->cancellable, on_unlock_complete,
g_object_ref (self));
gck_attributes_unref (template);
self->current = objpath;
break;
}
g_object_unref (collection);
/*
* Already have one unlock request going on. Just wait around
* and this function will be called again later.
*/
if (!proceed) {
g_queue_push_head (self->queued, objpath);
break;
}
}
}
/* -----------------------------------------------------------------------------
* DBUS
*/
static DBusMessage*
prompt_method_prompt (GkdSecretUnlock *self, DBusMessage *message)
{
DBusMessage *reply;
const char *window_id;
/* Act as if this object no longer exists */
if (self->completed)
return NULL;
if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING,
&window_id, DBUS_TYPE_INVALID))
return NULL;
/* Prompt can only be called once */
if (self->prompted)
return dbus_message_new_error (message, SECRET_ERROR_ALREADY_EXISTS,
"This prompt has already been shown.");
gkd_secret_unlock_call_prompt (self, window_id);
reply = dbus_message_new_method_return (message);
dbus_message_append_args (reply, DBUS_TYPE_INVALID);
return reply;
}
static DBusMessage*
prompt_method_dismiss (GkdSecretUnlock *self, DBusMessage *message)
{
DBusMessage *reply;
/* Act as if this object no longer exists */
if (self->completed)
return NULL;
if (!dbus_message_get_args (message, NULL, DBUS_TYPE_INVALID))
return NULL;
g_cancellable_cancel (self->cancellable);
mark_as_complete (self, TRUE);
reply = dbus_message_new_method_return (message);
dbus_message_append_args (reply, DBUS_TYPE_INVALID);
return reply;
}
/* -----------------------------------------------------------------------------
* OBJECT
*/
static DBusMessage*
gkd_secret_unlock_real_dispatch_message (GkdSecretDispatch *base, DBusMessage *message)
{
DBusMessage *reply = NULL;
GkdSecretUnlock *self;
const gchar *caller;
g_return_val_if_fail (message, NULL);
g_return_val_if_fail (GKD_SECRET_IS_UNLOCK (base), NULL);
self = GKD_SECRET_UNLOCK (base);
/* This should already have been caught elsewhere */
caller = dbus_message_get_sender (message);
if (!caller || !g_str_equal (caller, self->caller))
g_return_val_if_reached (NULL);
/* org.freedesktop.Secret.Prompt.Prompt() */
else if (dbus_message_is_method_call (message, SECRET_PROMPT_INTERFACE, "Prompt"))
reply = prompt_method_prompt (self, message);
/* org.freedesktop.Secret.Prompt.Negotiate() */
else if (dbus_message_is_method_call (message, SECRET_PROMPT_INTERFACE, "Dismiss"))
reply = prompt_method_dismiss (self, message);
else if (dbus_message_has_interface (message, DBUS_INTERFACE_INTROSPECTABLE))
return gkd_dbus_introspect_handle (message, gkd_secret_introspect_prompt);
return reply;
}
static void
gkd_secret_unlock_init (GkdSecretUnlock *self)
{
self->queued = g_queue_new ();
self->results = g_array_new (TRUE, TRUE, sizeof (gchar*));
self->cancellable = g_cancellable_new ();
}
static GObject*
gkd_secret_unlock_constructor (GType type, guint n_props, GObjectConstructParam *props)
{
GkdSecretUnlock *self = GKD_SECRET_UNLOCK (G_OBJECT_CLASS (gkd_secret_unlock_parent_class)->constructor(type, n_props, props));
g_return_val_if_fail (self, NULL);
g_return_val_if_fail (self->caller, NULL);
g_return_val_if_fail (self->service, NULL);
/* Setup the path for the object */
if (!self->object_path)
self->object_path = g_strdup_printf (SECRET_PROMPT_PREFIX "/u%d", ++unique_prompt_number);
return G_OBJECT (self);
}
static void
gkd_secret_unlock_dispose (GObject *obj)
{
GkdSecretUnlock *self = GKD_SECRET_UNLOCK (obj);
if (self->service) {
g_object_remove_weak_pointer (G_OBJECT (self->service),
(gpointer*)&(self->service));
self->service = NULL;
}
G_OBJECT_CLASS (gkd_secret_unlock_parent_class)->dispose (obj);
}
static void
gkd_secret_unlock_finalize (GObject *obj)
{
GkdSecretUnlock *self = GKD_SECRET_UNLOCK (obj);
g_free (self->object_path);
self->object_path = NULL;
if (g_queue_find (&unlock_prompt_queue, self))
g_warning ("unlock queue is not in sync with prompting");
if (self->queued) {
while (!g_queue_is_empty (self->queued))
g_free (g_queue_pop_head (self->queued));
g_queue_free (self->queued);
self->queued = NULL;
}
if (self->results) {
gkd_secret_unlock_reset_results (self);
g_array_free (self->results, TRUE);
self->results = NULL;
}
g_free (self->current);
self->current = NULL;
g_object_unref (self->cancellable);
self->cancellable = NULL;
g_assert (!self->object_path);
g_assert (!self->service);
g_free (self->caller);
self->caller = NULL;
g_free (self->window_id);
self->window_id = NULL;
G_OBJECT_CLASS (gkd_secret_unlock_parent_class)->finalize (obj);
}
static void
gkd_secret_unlock_set_property (GObject *obj, guint prop_id, const GValue *value,
GParamSpec *pspec)
{
GkdSecretUnlock *self = GKD_SECRET_UNLOCK (obj);
switch (prop_id) {
case PROP_CALLER:
g_return_if_fail (!self->caller);
self->caller = g_value_dup_string (value);
break;
case PROP_SERVICE:
g_return_if_fail (!self->service);
self->service = g_value_get_object (value);
g_return_if_fail (self->service);
g_object_add_weak_pointer (G_OBJECT (self->service),
(gpointer*)&(self->service));
break;
case PROP_OBJECT_PATH:
g_return_if_fail (!self->object_path);
self->object_path = g_strdup (g_value_get_pointer (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
gkd_secret_unlock_get_property (GObject *obj, guint prop_id, GValue *value,
GParamSpec *pspec)
{
GkdSecretUnlock *self = GKD_SECRET_UNLOCK (obj);
switch (prop_id) {
case PROP_CALLER:
g_value_set_string (value, self->caller);
break;
case PROP_OBJECT_PATH:
g_value_set_pointer (value, self->object_path);
break;
case PROP_SERVICE:
g_value_set_object (value, self->service);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
gkd_secret_unlock_class_init (GkdSecretUnlockClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->constructor = gkd_secret_unlock_constructor;
gobject_class->get_property = gkd_secret_unlock_get_property;
gobject_class->set_property = gkd_secret_unlock_set_property;
gobject_class->dispose = gkd_secret_unlock_dispose;
gobject_class->finalize = gkd_secret_unlock_finalize;
g_object_class_install_property (gobject_class, PROP_CALLER,
g_param_spec_string ("caller", "Caller", "DBus caller name",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY ));
g_object_class_install_property (gobject_class, PROP_OBJECT_PATH,
g_param_spec_pointer ("object-path", "Object Path", "DBus Object Path",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (gobject_class, PROP_SERVICE,
g_param_spec_object ("service", "Service", "Service which owns this prompt",
GKD_SECRET_TYPE_SERVICE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
gkd_secret_dispatch_iface (GkdSecretDispatchIface *iface)
{
iface->dispatch_message = gkd_secret_unlock_real_dispatch_message;
}
/* -----------------------------------------------------------------------------
* PUBLIC
*/
GkdSecretUnlock*
gkd_secret_unlock_new (GkdSecretService *service, const gchar *caller,
const gchar *object_path)
{
return g_object_new (GKD_SECRET_TYPE_UNLOCK,
"service", service,
"caller", caller,
"object-path", object_path,
NULL);
}
void
gkd_secret_unlock_queue (GkdSecretUnlock *self, const gchar *unlock_path)
{
gboolean locked = TRUE;
GckObject *coll;
gchar *path;
g_return_if_fail (GKD_SECRET_IS_UNLOCK (self));
g_return_if_fail (unlock_path);
coll = lookup_collection (self, unlock_path);
if (coll == NULL)
return;
/* Try to unlock with an empty password, which produces no prompt */
if (gkd_secret_unlock_with_password (coll, (const guchar*)"", 0, NULL)) {
locked = FALSE;
}
path = g_strdup (unlock_path);
if (locked)
g_queue_push_tail (self->queued, path);
else
g_array_append_val (self->results, path);
g_object_unref (coll);
}
gboolean
gkd_secret_unlock_have_queued (GkdSecretUnlock *self)
{
g_return_val_if_fail (GKD_SECRET_IS_UNLOCK (self), FALSE);
return !g_queue_is_empty (self->queued) || self->current;
}
gchar**
gkd_secret_unlock_get_results (GkdSecretUnlock *self, gint *n_results)
{
g_return_val_if_fail (GKD_SECRET_IS_UNLOCK (self), NULL);
g_return_val_if_fail (n_results, NULL);
*n_results = self->results->len;
return (gchar**)self->results->data;
}
void
gkd_secret_unlock_reset_results (GkdSecretUnlock *self)
{
gint i;
g_return_if_fail (GKD_SECRET_IS_UNLOCK (self));
for (i = 0; i < self->results->len; ++i)
g_free (g_array_index (self->results, gchar*, i));
g_array_set_size (self->results, 0);
}
void
gkd_secret_unlock_call_prompt (GkdSecretUnlock *self, const gchar *window_id)
{
g_return_if_fail (GKD_SECRET_IS_UNLOCK (self));
g_return_if_fail (!self->prompted);
g_assert (!self->window_id);
self->window_id = g_strdup (window_id);
self->prompted = TRUE;
perform_next_unlock (self);
}
gboolean
gkd_secret_unlock_with_secret (GckObject *collection, GkdSecretSecret *master,
DBusError *derr)
{
GckAttributes *attrs;
GckObject *cred;
gboolean locked;
g_return_val_if_fail (GCK_IS_OBJECT (collection), FALSE);
g_return_val_if_fail (master, FALSE);
/* Shortcut if already unlocked */
if (check_locked_collection (collection, &locked) && !locked)
return TRUE;
attrs = gck_attributes_new ();
common_unlock_attributes (attrs, collection);
gck_attributes_add_boolean (attrs, CKA_GNOME_TRANSIENT, TRUE);
gck_attributes_add_boolean (attrs, CKA_TOKEN, TRUE);
cred = gkd_secret_session_create_credential (master->session, NULL, attrs, master, derr);
gck_attributes_unref (attrs);
if (cred != NULL)
g_object_unref (cred);
return (cred != NULL);
}
gboolean
gkd_secret_unlock_with_password (GckObject *collection, const guchar *password,
gsize n_password, DBusError *derr)
{
GckAttributes *attrs;
GError *error = NULL;
GckSession *session;
GckObject *cred;
gboolean locked;
g_return_val_if_fail (GCK_IS_OBJECT (collection), FALSE);
/* Shortcut if already unlocked */
if (check_locked_collection (collection, &locked) && !locked)
return TRUE;
session = gck_object_get_session (collection);
g_return_val_if_fail (session, FALSE);
attrs = gck_attributes_new_full (egg_secure_realloc);
common_unlock_attributes (attrs, collection);
gck_attributes_add_boolean (attrs, CKA_GNOME_TRANSIENT, TRUE);
gck_attributes_add_boolean (attrs, CKA_TOKEN, TRUE);
gck_attributes_add_data (attrs, CKA_VALUE, password, n_password);
cred = gck_session_create_object (session, attrs, NULL, &error);
if (cred == NULL) {
if (g_error_matches (error, GCK_ERROR, CKR_PIN_INCORRECT)) {
dbus_set_error_const (derr, INTERNAL_ERROR_DENIED, "The password was incorrect.");
} else {
g_message ("couldn't create credential: %s", egg_error_message (error));
dbus_set_error_const (derr, DBUS_ERROR_FAILED, "Couldn't use credentials");
}
g_clear_error (&error);
return FALSE;
}
g_object_unref (cred);
return TRUE;
}
|