summaryrefslogtreecommitdiff
path: root/gcr/gcr-system-prompter.c
blob: fb03ec89156612b2468ae9086dae3e68b81b8c9e (plain)
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
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
872
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/*
 * gnome-keyring
 *
 * Copyright (C) 2011 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.
 *
 * Author: Stef Walter <stef@thewalter.net>
 */

#include "config.h"

#include "gcr-dbus-constants.h"
#include "gcr-dbus-generated.h"
#define DEBUG_FLAG GCR_DEBUG_PROMPT
#include "gcr-debug.h"
#include "gcr-enum-types-base.h"
#include "gcr-internal.h"
#include "gcr-library.h"
#include "gcr-marshal.h"
#include "gcr-prompt.h"
#include "gcr-secret-exchange.h"
#include "gcr-system-prompter.h"
#include "gcr-system-prompt.h"

#include "egg/egg-error.h"

#include <string.h>

/**
 * SECTION:gcr-system-prompter
 * @title: GcrSystemPrompter
 * @short_description: a prompter which displays system prompts
 *
 * This is a DBus service which is rarely implemented. Use #GcrSystemPrompt
 * to display system prompts.
 *
 * The GcrSystemPrompter service responds to dbus requests to create system
 * prompts and creates #GcrPrompt type objects to display those prompts.
 *
 * Pass the GType of the implementation of #GcrPrompt to gcr_system_prompter_new().
 */

/**
 * GcrSystemPrompter:
 *
 * A prompter used by implementations of system prompts.
 */

/**
 * GcrSystemPrompterClass:
 * @parent_class: parent class
 * @new_prompt: default handler for the #GcrSystemPrompter::new-prompt signal
 *
 * The class for #GcrSystemPrompter.
 */

/**
 * GcrSystemPrompterMode:
 * @GCR_SYSTEM_PROMPTER_SINGLE: only one prompt shown at a time
 * @GCR_SYSTEM_PROMPTER_MULTIPLE: more than one prompt shown at a time
 *
 * The mode for the system prompter. Most system prompters can only show
 * one prompt at a time and would use the %GCR_SYSTEM_PROMPTER_SINGLE mode.
 */

enum {
	PROP_0,
	PROP_MODE,
	PROP_PROMPT_TYPE,
	PROP_PROMPTING
};

enum {
	NEW_PROMPT,
	LAST_SIGNAL,
};

struct _GcrSystemPrompterPrivate {
	GcrSystemPrompterMode mode;
	GType prompt_type;

	guint prompter_registered;
	GDBusConnection *connection;

	GHashTable *callbacks;       /* Callback -> guint watch_id */
	GHashTable *active;          /* Callback -> active (ActivePrompt) */
	GQueue waiting;
};

static guint signals[LAST_SIGNAL] = { 0, };

G_DEFINE_TYPE (GcrSystemPrompter, gcr_system_prompter, G_TYPE_OBJECT);

typedef struct {
	const gchar *path;
	const gchar *name;
} Callback;

typedef struct {
	gint refs;
	Callback *callback;
	GcrSystemPrompter *prompter;
	GCancellable *cancellable;
	GcrPrompt *prompt;
	gboolean ready;
	guint notify_sig;
	GHashTable *changed;
	GcrSecretExchange *exchange;
	gboolean received;
} ActivePrompt;

static void    prompt_send_ready               (ActivePrompt *active,
                                                const gchar *response,
                                                const gchar *secret);

static void    prompt_next_ready               (GcrSystemPrompter *self);

static void    prompt_stop_prompting           (GcrSystemPrompter *self,
                                                Callback *callback,
                                                gboolean send_done_message,
                                                gboolean wait_for_reply);

static ActivePrompt *
active_prompt_ref (ActivePrompt *active)
{
	g_atomic_int_inc (&active->refs);
	return active;
}

static void
on_prompt_notify (GObject *object,
                  GParamSpec *param,
                  gpointer user_data)
{
	ActivePrompt *active = user_data;
	gpointer key = (gpointer)g_intern_string (param->name);
	g_hash_table_replace (active->changed, key, key);
}

static Callback *
callback_dup (Callback *original)
{
	Callback *callback = g_slice_new0 (Callback);
	g_assert (original != NULL);
	g_assert (original->path != NULL);
	g_assert (original->name != NULL);
	callback->path = g_strdup (original->path);
	callback->name = g_strdup (original->name);
	return callback;
}

static void
callback_free (gpointer data)
{
	Callback *callback = data;
	g_free ((gchar *)callback->path);
	g_free ((gchar *)callback->name);
	g_slice_free (Callback, callback);
}

static guint
callback_hash (gconstpointer data)
{
	const Callback *callback = data;
	return g_str_hash (callback->name) ^ g_str_hash (callback->path);
}

static gboolean
callback_equal (gconstpointer one,
                gconstpointer two)
{
	const Callback *cone = one;
	const Callback *ctwo = two;
	return g_str_equal (cone->name, ctwo->name) &&
	       g_str_equal (cone->path, ctwo->path);
}

static void
active_prompt_unref (gpointer data)
{
	ActivePrompt *active = data;

	if (g_atomic_int_dec_and_test (&active->refs)) {
		g_object_unref (active->prompter);
		g_object_unref (active->cancellable);
		g_signal_handlers_disconnect_by_func (active->prompt, on_prompt_notify, active);
		g_object_unref (active->prompt);
		g_hash_table_destroy (active->changed);
		if (active->exchange)
			g_object_unref (active->exchange);
		g_slice_free (ActivePrompt, active);
	}
}

static GcrSecretExchange *
active_prompt_get_secret_exchange (ActivePrompt *active)
{
	if (active->exchange == NULL)
		active->exchange = gcr_secret_exchange_new (NULL);
	return active->exchange;
}

static ActivePrompt *
active_prompt_create (GcrSystemPrompter *self,
                      Callback *lookup)
{
	ActivePrompt *active;

	active = g_slice_new0 (ActivePrompt);
	if (!g_hash_table_lookup_extended (self->pv->callbacks, lookup,
	                                   (gpointer *)&active->callback, NULL))
		g_return_val_if_reached (NULL);

	active->refs = 1;
	active->prompter = g_object_ref (self);
	active->cancellable = g_cancellable_new ();
	g_signal_emit (self, signals[NEW_PROMPT], 0, &active->prompt);
	g_return_val_if_fail (active->prompt != NULL, NULL);

	active->notify_sig = g_signal_connect (active->prompt, "notify", G_CALLBACK (on_prompt_notify), active);
	active->changed = g_hash_table_new (g_direct_hash, g_direct_equal);

	/* Insert us into the active hash table */
	g_hash_table_replace (self->pv->active, active->callback, active);
	return active;
}

static void
unwatch_name (gpointer data)
{
	g_bus_unwatch_name (GPOINTER_TO_UINT (data));
}

static void
gcr_system_prompter_init (GcrSystemPrompter *self)
{
	self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_SYSTEM_PROMPTER,
	                                        GcrSystemPrompterPrivate);
	self->pv->callbacks = g_hash_table_new_full (callback_hash, callback_equal, callback_free, unwatch_name);
	self->pv->active = g_hash_table_new_full (callback_hash, callback_equal, NULL, active_prompt_unref);
}

static void
gcr_system_prompter_set_property (GObject *obj,
                                  guint prop_id,
                                  const GValue *value,
                                  GParamSpec *pspec)
{
	GcrSystemPrompter *self = GCR_SYSTEM_PROMPTER (obj);

	switch (prop_id) {
	case PROP_MODE:
		self->pv->mode = g_value_get_enum (value);
		break;
	case PROP_PROMPT_TYPE:
		self->pv->prompt_type = g_value_get_gtype (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
gcr_system_prompter_get_property (GObject *obj,
                                  guint prop_id,
                                  GValue *value,
                                  GParamSpec *pspec)
{
	GcrSystemPrompter *self = GCR_SYSTEM_PROMPTER (obj);

	switch (prop_id) {
	case PROP_MODE:
		g_value_set_enum (value, gcr_system_prompter_get_mode (self));
		break;
	case PROP_PROMPT_TYPE:
		g_value_set_gtype (value, gcr_system_prompter_get_prompt_type (self));
		break;
	case PROP_PROMPTING:
		g_value_set_boolean (value, gcr_system_prompter_get_prompting (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
gcr_system_prompter_dispose (GObject *obj)
{
	GcrSystemPrompter *self = GCR_SYSTEM_PROMPTER (obj);

	_gcr_debug ("disposing prompter");

	if (self->pv->prompter_registered)
		gcr_system_prompter_unregister (self, FALSE);

	g_hash_table_remove_all (self->pv->callbacks);
	g_hash_table_remove_all (self->pv->active);
	g_object_notify (obj, "prompting");

	G_OBJECT_CLASS (gcr_system_prompter_parent_class)->dispose (obj);
}

static void
gcr_system_prompter_finalize (GObject *obj)
{
	GcrSystemPrompter *self = GCR_SYSTEM_PROMPTER (obj);

	_gcr_debug ("finalizing prompter");

	g_assert (self->pv->connection == NULL);
	g_assert (self->pv->prompter_registered == 0);

	g_hash_table_destroy (self->pv->callbacks);
	g_hash_table_destroy (self->pv->active);

	G_OBJECT_CLASS (gcr_system_prompter_parent_class)->finalize (obj);
}

static GcrPrompt *
gcr_system_prompter_new_prompt (GcrSystemPrompter *self)
{
	g_return_val_if_fail (self->pv->prompt_type != 0, NULL);

	_gcr_debug ("creating new %s prompt", g_type_name (self->pv->prompt_type));

	return g_object_new (self->pv->prompt_type, NULL);
}

static gboolean
gcr_system_prompter_new_prompt_acculmulator (GSignalInvocationHint *ihint,
                                             GValue *return_accu,
                                             const GValue *handler_return,
                                             gpointer user_data)
{
	if (g_value_get_object (handler_return) != NULL) {
		g_value_copy (handler_return, return_accu);
		return FALSE;
	}

	return TRUE;
}

static void
gcr_system_prompter_class_init (GcrSystemPrompterClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	gobject_class->get_property = gcr_system_prompter_get_property;
	gobject_class->set_property = gcr_system_prompter_set_property;
	gobject_class->dispose = gcr_system_prompter_dispose;
	gobject_class->finalize = gcr_system_prompter_finalize;

	klass->new_prompt = gcr_system_prompter_new_prompt;

	g_type_class_add_private (gobject_class, sizeof (GcrSystemPrompterPrivate));

	/**
	 * GcrSystemPrompter:mode:
	 *
	 * The mode for this prompter.
	 *
	 * Most system prompters only display one prompt at a time and therefore
	 * return %GCR_SYSTEM_PROMPTER_SINGLE.
	 */
	g_object_class_install_property (gobject_class, PROP_MODE,
	              g_param_spec_enum ("mode", "Mode", "Prompting mode",
	                                 GCR_TYPE_SYSTEM_PROMPTER_MODE, GCR_SYSTEM_PROMPTER_SINGLE,
	                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

	/**
	 * GcrSystemPrompter:prompt-type:
	 *
	 * The #GType for prompts created by this prompter. This must be a
	 * #GcrPrompt implementation.
	 */
	g_object_class_install_property (gobject_class, PROP_PROMPT_TYPE,
	             g_param_spec_gtype ("prompt-type", "Prompt GType", "GObject type of prompts",
	                                 GCR_TYPE_PROMPT,
	                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

	/**
	 * GcrSystemPrompter:prompting:
	 *
	 * Whether the prompter is prompting or not.
	 */
	g_object_class_install_property (gobject_class, PROP_PROMPTING,
	           g_param_spec_boolean ("prompting", "Prompting", "Whether prompting or not",
	                                 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	/**
	 * GcrSystemPrompter::new-prompt:
	 *
	 * Signal emitted to create a new prompt when needed.
	 *
	 * The default implementation of this signal creates a prompt of the type
	 * gcr_system_prompter_get_prompt_type().
	 *
	 * Returns: (transfer full): the new prompt
	 */
	signals[NEW_PROMPT] = g_signal_new ("new-prompt", GCR_TYPE_SYSTEM_PROMPTER, G_SIGNAL_RUN_LAST,
	                                    G_STRUCT_OFFSET (GcrSystemPrompterClass, new_prompt),
	                                    gcr_system_prompter_new_prompt_acculmulator, NULL,
	                                    _gcr_marshal_OBJECT__VOID,
	                                    GCR_TYPE_PROMPT, 0, G_TYPE_NONE);
}

static GVariantBuilder *
prompt_build_properties (GcrPrompt *prompt,
                         GHashTable *changed)
{
	GObject *obj = G_OBJECT (prompt);
	GVariantBuilder *builder;
	const gchar *property_name;
	GParamSpec *pspec;
	GHashTableIter iter;
	const GVariantType *type;
	GVariant *variant;
	GValue value;

	builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
	g_hash_table_iter_init (&iter, changed);
	while (g_hash_table_iter_next (&iter, (gpointer *)&property_name, NULL)) {

		/* Make sure this property is on the prompt interface */
		pspec = g_object_interface_find_property (GCR_PROMPT_GET_INTERFACE (obj),
		                                          property_name);
		if (pspec == NULL)
			continue;

		memset (&value, 0, sizeof (GValue));
		g_value_init (&value, pspec->value_type);
		g_object_get_property (obj, property_name, &value);

		switch (pspec->value_type) {
		case G_TYPE_STRING:
			type = G_VARIANT_TYPE ("s");
			break;
		case G_TYPE_INT:
			type = G_VARIANT_TYPE ("i");
			break;
		case G_TYPE_BOOLEAN:
			type = G_VARIANT_TYPE ("b");
			break;
		default:
			g_critical ("encountered unsupported property type on GcrPrompt: %s",
			            g_type_name (pspec->value_type));
			continue;
		}

		variant = g_dbus_gvalue_to_gvariant (&value, type);
		g_variant_builder_add (builder, "{sv}", property_name,
		                       g_variant_new_variant (variant));
		g_value_unset (&value);
		g_variant_unref (variant);
	}
	g_hash_table_remove_all (changed);
	return builder;
}

static void
prompt_stop_prompting (GcrSystemPrompter *self,
                       Callback *callback,
                       gboolean send_done_message,
                       gboolean wait_for_reply)
{
	ActivePrompt *active;
	GVariant *retval;

	/* Get a pointer to our actual callback */
	if (!g_hash_table_lookup_extended (self->pv->callbacks, callback,
	                                   (gpointer *)&callback, NULL))
		return;

	/* Removed from the waiting queue */
	g_queue_remove (&self->pv->waiting, callback);

	/* Close any active prompt */
	active = g_hash_table_lookup (self->pv->active, callback);
	if (active != NULL) {
		if (!active->ready) {
			_gcr_debug ("cancelling active prompting operation for %s@%s",
			            callback->path, callback->name);
			g_cancellable_cancel (active->cancellable);
		}

		_gcr_debug ("disposing the prompt");

		g_object_run_dispose (G_OBJECT (active->prompt));
		g_hash_table_remove (self->pv->active, callback);
	}

	/* Notify the caller */
	if (send_done_message && wait_for_reply) {
		_gcr_debug ("calling the %s on %s@%s, and waiting for reply",
		            GCR_DBUS_CALLBACK_METHOD_DONE, callback->path, callback->name);

		retval = g_dbus_connection_call_sync (self->pv->connection,
		                                      callback->name,
		                                      callback->path,
		                                      GCR_DBUS_CALLBACK_INTERFACE,
		                                      GCR_DBUS_CALLBACK_METHOD_DONE,
		                                      g_variant_new ("()"),
		                                      G_VARIANT_TYPE ("()"),
		                                      G_DBUS_CALL_FLAGS_NO_AUTO_START,
		                                      -1, NULL, NULL);
		if (retval)
			g_variant_unref (retval);

		_gcr_debug ("returned from %s on %s@%s",
		            GCR_DBUS_CALLBACK_METHOD_DONE, callback->path, callback->name);

	} else if (send_done_message) {
		_gcr_debug ("calling the %s on %s@%s, and ignoring reply",
		            GCR_DBUS_CALLBACK_METHOD_DONE, callback->path, callback->name);

		g_dbus_connection_call (self->pv->connection,
		                        callback->name,
		                        callback->path,
		                        GCR_DBUS_CALLBACK_INTERFACE,
		                        GCR_DBUS_CALLBACK_METHOD_DONE,
		                        g_variant_new ("()"),
		                        G_VARIANT_TYPE ("()"),
		                        G_DBUS_CALL_FLAGS_NO_AUTO_START,
		                        -1, NULL, NULL, NULL);
	}

	/* And all traces gone, including watch */
	g_hash_table_remove (self->pv->callbacks, callback);
	g_object_notify (G_OBJECT (self), "prompting");
}

static void
on_prompt_ready_complete (GObject *source,
                          GAsyncResult *result,
                          gpointer user_data)
{
	ActivePrompt *active = user_data;
	GcrSystemPrompter *self = g_object_ref (active->prompter);
	GError *error = NULL;
	GVariant *retval;

	g_assert (active->ready == FALSE);

	_gcr_debug ("returned from the %s method on %s@%s",
	            GCR_DBUS_CALLBACK_METHOD_READY, active->callback->path, active->callback->name);

	active->ready = TRUE;
	retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);

	/* Was cancelled, prompter probably unregistered */
	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
	    g_cancellable_is_cancelled (active->cancellable)) {
		g_error_free (error);

	/* The ready call failed,  */
	} else if (error != NULL) {
		if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD))
			_gcr_debug ("prompt %s@%s disappeared or does not exist",
			            active->callback->path, active->callback->name);
		else
			g_message ("received an error from the prompt callback: %s", error->message);
		g_error_free (error);

		prompt_stop_prompting (self, active->callback, FALSE, FALSE);

		/* Another new prompt may be ready to go active? */
		prompt_next_ready (self);
	}

	if (retval != NULL)
		g_variant_unref (retval);

	active_prompt_unref (active);
	g_object_unref (self);
}

static void
prompt_send_ready (ActivePrompt *active,
                   const gchar *response,
                   const gchar *secret)
{
	GcrSystemPrompter *self;
	GVariantBuilder *builder;
	GcrSecretExchange *exchange;
	gchar *sent;

	g_assert (active->ready == FALSE);

	exchange = active_prompt_get_secret_exchange (active);
	if (!active->received) {
		g_return_if_fail (secret == NULL);
		sent = gcr_secret_exchange_begin (exchange);
	} else {
		sent = gcr_secret_exchange_send (exchange, secret, -1);
	}

	self = active->prompter;
	builder = prompt_build_properties (active->prompt, active->changed);

	_gcr_debug ("calling the %s method on %s@%s",
	            GCR_DBUS_CALLBACK_METHOD_READY, active->callback->path, active->callback->name);

	g_dbus_connection_call (self->pv->connection,
	                        active->callback->name,
	                        active->callback->path,
	                        GCR_DBUS_CALLBACK_INTERFACE,
	                        GCR_DBUS_CALLBACK_METHOD_READY,
	                        g_variant_new ("(sa{sv}s)", response, builder, sent),
	                        G_VARIANT_TYPE ("()"),
	                        G_DBUS_CALL_FLAGS_NO_AUTO_START,
	                        -1, active->cancellable,
	                        on_prompt_ready_complete,
	                        active_prompt_ref (active));

	g_variant_builder_unref (builder);
	g_free (sent);
}

static void
prompt_next_ready (GcrSystemPrompter *self)
{
	ActivePrompt *active;
	Callback *callback;

	if (self->pv->mode == GCR_SYSTEM_PROMPTER_SINGLE &&
	    g_hash_table_size (self->pv->active) > 0)
		return;

	callback = g_queue_pop_head (&self->pv->waiting);
	if (callback == NULL)
		return;

	_gcr_debug ("preparing a prompt for callback %s@%s",
	            callback->path, callback->name);

	active = g_hash_table_lookup (self->pv->active, callback);
	g_assert (active == NULL);

	active = active_prompt_create (self, callback);
	g_return_if_fail (active != NULL);

	prompt_send_ready (active, GCR_DBUS_PROMPT_REPLY_NONE, NULL);
}

static void
prompt_update_properties (GcrPrompt *prompt,
                          GVariantIter *iter)
{
	GObject *obj = G_OBJECT (prompt);
	gchar *property_name;
	GVariant *variant;
	GValue value;

	g_object_freeze_notify (obj);
	while (g_variant_iter_loop (iter, "{&sv}", &property_name, &variant)) {
		memset (&value, 0, sizeof (GValue));
		g_dbus_gvariant_to_gvalue (variant, &value);
		g_object_set_property (obj, property_name, &value);
		g_value_unset (&value);
	}
	g_object_thaw_notify (obj);
}

static GVariant *
prompter_get_property (GDBusConnection *connection,
                       const gchar *sender,
                       const gchar *object_path,
                       const gchar *interface_name,
                       const gchar *property_name,
                       GError **error,
                       gpointer user_data)
{
	g_return_val_if_reached (NULL);
}

static gboolean
prompter_set_property (GDBusConnection *connection,
                       const gchar *sender,
                       const gchar *object_path,
                       const gchar *interface_name,
                       const gchar *property_name,
                       GVariant *value,
                       GError **error,
                       gpointer user_data)
{
	g_return_val_if_reached (FALSE);
}

static void
on_caller_vanished (GDBusConnection *connection,
                    const gchar *name,
                    gpointer user_data)
{
	GcrSystemPrompter *self = GCR_SYSTEM_PROMPTER (user_data);
	GQueue queue = G_QUEUE_INIT;
	Callback *callback;
	GHashTableIter iter;

	g_hash_table_iter_init (&iter, self->pv->callbacks);
	while (g_hash_table_iter_next (&iter, (gpointer *)&callback, NULL)) {
		if (g_strcmp0 (name, callback->name) == 0)
			g_queue_push_tail (&queue, callback);
	}

	while ((callback = g_queue_pop_head (&queue)) != NULL) {
		_gcr_debug ("caller vanished for callback %s@%s",
		            callback->path, callback->name);
		prompt_stop_prompting (self, callback, FALSE, FALSE);
	}
}

static void
prompter_method_begin_prompting (GcrSystemPrompter *self,
                                 GDBusMethodInvocation *invocation,
                                 GVariant *parameters)
{
	Callback lookup;
	Callback *callback;
	const gchar *caller;
	guint watch_id;

	lookup.name = caller = g_dbus_method_invocation_get_sender (invocation);
	g_variant_get (parameters, "(&o)", &lookup.path);

	_gcr_debug ("received %s call from callback %s@%s",
	            GCR_DBUS_PROMPTER_METHOD_BEGIN,
	            lookup.path, lookup.name);

	if (g_hash_table_lookup (self->pv->callbacks, &lookup)) {
		_gcr_debug ("already begun prompting for callback %s@%s",
		            lookup.path, lookup.name);
		g_dbus_method_invocation_return_error_literal (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
		                                               "Already begun prompting for this prompt callback");
		return;
	}

	callback = callback_dup (&lookup);
	watch_id = g_bus_watch_name_on_connection (self->pv->connection, caller,
	                                           G_BUS_NAME_WATCHER_FLAGS_NONE,
	                                           NULL, on_caller_vanished,
	                                           self, NULL);
	g_hash_table_insert (self->pv->callbacks, callback, GUINT_TO_POINTER (watch_id));

	g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));

	g_queue_push_tail (&self->pv->waiting, callback);
	g_object_notify (G_OBJECT (self), "prompting");

	prompt_next_ready (self);
}

static void
on_prompt_password (GObject *source,
                    GAsyncResult *result,
                    gpointer user_data)
{
	ActivePrompt *active = user_data;
	const gchar *reply;
	GError *error = NULL;
	const gchar *response;

	g_assert (active->ready == FALSE);
	g_assert (active->callback != NULL);

	_gcr_debug ("completed password prompt for callback %s@%s",
	            active->callback->name, active->callback->path);

	reply = gcr_prompt_password_finish (GCR_PROMPT (source), result, &error);
	if (error != NULL) {
		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
			g_warning ("prompting failed: %s", error->message);
		g_clear_error (&error);
	}

	if (reply == NULL)
		response = "no";
	else
		response = "yes";

	prompt_send_ready (active, response, reply);
	active_prompt_unref (active);
}

static void
on_prompt_confirm (GObject *source,
                   GAsyncResult *result,
                   gpointer user_data)
{
	ActivePrompt *active = user_data;
	GcrPromptReply reply;
	GError *error = NULL;
	const gchar *response;

	g_assert (active->ready == FALSE);
	g_assert (active->callback != NULL);

	_gcr_debug ("completed confirm prompt for callback %s@%s",
	            active->callback->name, active->callback->path);

	reply = gcr_prompt_confirm_finish (GCR_PROMPT (source), result, &error);
	if (error != NULL) {
		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
			g_warning ("prompting failed: %s", error->message);
		g_clear_error (&error);
	}

	switch (reply) {
	case GCR_PROMPT_REPLY_CONTINUE:
		response = GCR_DBUS_PROMPT_REPLY_YES;
		break;
	case GCR_PROMPT_REPLY_CANCEL:
		response = GCR_DBUS_PROMPT_REPLY_NO;
		break;
	default:
		response = GCR_DBUS_PROMPT_REPLY_NONE;
		g_warn_if_reached ();
		break;
	}

	prompt_send_ready (active, response, NULL);
	active_prompt_unref (active);
}

static void
prompter_method_perform_prompt (GcrSystemPrompter *self,
                                GDBusMethodInvocation *invocation,
                                GVariant *parameters)
{
	GcrSecretExchange *exchange;
	GError *error = NULL;
	ActivePrompt *active;
	Callback lookup;
	const gchar *type;
	GVariantIter *iter;
	const gchar *received;

	lookup.name = g_dbus_method_invocation_get_sender (invocation);
	g_variant_get (parameters, "(&o&sa{sv}&s)",
	               &lookup.path, &type, &iter, &received);

	_gcr_debug ("received %s call from callback %s@%s",
	            GCR_DBUS_PROMPTER_METHOD_PERFORM,
	            lookup.path, lookup.name);

	active = g_hash_table_lookup (self->pv->active, &lookup);
	if (active == NULL) {
		_gcr_debug ("not begun prompting for this callback %s@%s",
		            lookup.path, lookup.name);
		error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
		                     "Not begun prompting for this prompt callback");

	} else if (!active->ready) {
		_gcr_debug ("already performing prompt for this callback %s@%s",
		            lookup.path, lookup.name);
		error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
		                     "Already performing a prompt for this prompt callback");
	}

	if (error != NULL) {
		g_dbus_method_invocation_take_error (invocation, error);
		g_variant_iter_free (iter);
		return;
	}

	g_assert (active != NULL);
	prompt_update_properties (active->prompt, iter);
	g_variant_iter_free (iter);

	exchange = active_prompt_get_secret_exchange (active);
	if (!gcr_secret_exchange_receive (exchange, received)) {
		_gcr_debug ("received invalid secret exchange from callback %s@%s",
		            lookup.path, lookup.name);
		g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
		                                       "Invalid secret exchange received");
		return;
	}

	active->received = TRUE;

	if (g_strcmp0 (type, GCR_DBUS_PROMPT_TYPE_CONFIRM) == 0) {
		active->ready = FALSE;
		_gcr_debug ("starting confirm prompt for callback %s@%s",
		            lookup.path, lookup.name);
		gcr_prompt_confirm_async (active->prompt, active->cancellable,
		                          on_prompt_confirm, active_prompt_ref (active));

	} else if (g_strcmp0 (type, GCR_DBUS_PROMPT_TYPE_PASSWORD) == 0) {
		active->ready = FALSE;
		_gcr_debug ("starting password prompt for callback %s@%s",
		            lookup.path, lookup.name);
		gcr_prompt_password_async (active->prompt, active->cancellable,
		                           on_prompt_password, active_prompt_ref (active));

	} else {
		_gcr_debug ("invalid type of prompt from callback %s@%s",
		            lookup.path, lookup.name);
		g_dbus_method_invocation_return_error_literal (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
		                                               "Invalid type argument");
		return;
	}

	g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
}

static void
prompter_method_stop_prompting (GcrSystemPrompter *self,
                                GDBusMethodInvocation *invocation,
                                GVariant *parameters)
{
	Callback lookup;

	lookup.name = g_dbus_method_invocation_get_sender (invocation);
	g_variant_get (parameters, "(&o)", &lookup.path);

	_gcr_debug ("received %s call from callback %s@%s",
	            GCR_DBUS_PROMPTER_METHOD_PERFORM,
	            lookup.path, lookup.name);

	prompt_stop_prompting (self, &lookup, TRUE, FALSE);

	g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
	prompt_next_ready (self);
}

static void
prompter_method_call (GDBusConnection *connection,
                      const gchar *sender,
                      const gchar *object_path,
                      const gchar *interface_name,
                      const gchar *method_name,
                      GVariant *parameters,
                      GDBusMethodInvocation *invocation,
                      gpointer user_data)
{
	GcrSystemPrompter *self = GCR_SYSTEM_PROMPTER (user_data);

	g_return_if_fail (method_name != NULL);

	if (g_str_equal (method_name, GCR_DBUS_PROMPTER_METHOD_BEGIN)) {
		prompter_method_begin_prompting (self, invocation, parameters);

	} else if (g_str_equal (method_name, GCR_DBUS_PROMPTER_METHOD_PERFORM)) {
		prompter_method_perform_prompt (self, invocation, parameters);

	} else if (g_str_equal (method_name, GCR_DBUS_PROMPTER_METHOD_STOP)) {
		prompter_method_stop_prompting (self, invocation, parameters);

	} else {
		g_return_if_reached ();
	}
}

static GDBusInterfaceVTable prompter_dbus_vtable = {
	prompter_method_call,
	prompter_get_property,
	prompter_set_property,
};

/**
 * gcr_system_prompter_register:
 * @self: the system prompter
 * @connection: a DBus connection
 *
 * Register this system prompter on the DBus @connection.
 *
 * This makes the prompter available for clients to call. The prompter will
 * remain registered until gcr_system_prompter_unregister() is called, or the
 * prompter is unreferenced.
 */
void
gcr_system_prompter_register (GcrSystemPrompter *self,
                              GDBusConnection *connection)
{
	GError *error = NULL;

	g_return_if_fail (GCR_IS_SYSTEM_PROMPTER (self));
	g_return_if_fail (G_DBUS_CONNECTION (connection));
	g_return_if_fail (self->pv->prompter_registered == 0);
	g_return_if_fail (self->pv->connection == NULL);

	_gcr_debug ("registering prompter");

	self->pv->connection = g_object_ref (connection);

	self->pv->prompter_registered = g_dbus_connection_register_object (connection,
	                                                                   GCR_DBUS_PROMPTER_OBJECT_PATH,
	                                                                   _gcr_dbus_prompter_interface_info (),
	                                                                   &prompter_dbus_vtable,
	                                                                   self, NULL, &error);
	if (error != NULL) {
		g_warning ("error registering prompter %s", egg_error_message (error));
		g_clear_error (&error);
	}
}

/**
 * gcr_system_prompter_unregister:
 * @self: the system prompter
 * @wait: whether to wait for closing prompts
 *
 * Unregister this system prompter on the DBus @connection.
 *
 * The prompter must have previously been registered with gcr_system_prompter_register().
 *
 * If @wait is set then this function will wait until all prompts have been closed
 * or cancelled. This is usually only used by tests.
 */
void
gcr_system_prompter_unregister (GcrSystemPrompter *self,
                                gboolean wait)
{
	GList *callbacks;
	GList *l;

	g_return_if_fail (GCR_IS_SYSTEM_PROMPTER (self));
	g_return_if_fail (self->pv->prompter_registered != 0);

	_gcr_debug ("unregistering prompter");

	callbacks = g_hash_table_get_keys (self->pv->callbacks);
	for (l = callbacks; l != NULL; l = g_list_next (l))
		prompt_stop_prompting (self, l->data, TRUE, wait);
	g_list_free (callbacks);

	g_assert (g_hash_table_size (self->pv->active) == 0);
	g_assert (g_hash_table_size (self->pv->callbacks) == 0);
	g_assert (g_queue_is_empty (&self->pv->waiting));

	if (!g_dbus_connection_unregister_object (self->pv->connection, self->pv->prompter_registered))
		g_assert_not_reached ();
	self->pv->prompter_registered = 0;

	g_clear_object (&self->pv->connection);
}

/**
 * gcr_system_prompter_new:
 * @mode: the mode for the prompt
 * @prompt_type: the gobject type for prompts created by this prompter
 *
 * Create a new system prompter service. This prompter won't do anything unless
 * you connect to its signals and show appropriate prompts.
 *
 * If @prompt_type is zero, then the new-prompt signal must be handled and
 * return a valid prompt object implementing the #GcrPrompt interface.
 *
 * If @prompt_type is non-zero then the #GType must implement the #GcrPrompt
 * interface.
 *
 * Returns: (transfer full): a new prompter service
 */
GcrSystemPrompter *
gcr_system_prompter_new (GcrSystemPrompterMode mode,
                         GType prompt_type)
{
	if (prompt_type == 0) {
		return g_object_new (GCR_TYPE_SYSTEM_PROMPTER,
		                     "mode", mode,
		                     NULL);

	} else {
		return g_object_new (GCR_TYPE_SYSTEM_PROMPTER,
		                     "mode", mode,
		                     "prompt-type", prompt_type,
		                     NULL);
	}
}

/**
 * gcr_system_prompter_get_mode:
 * @self: the prompter
 *
 * Get the mode for this prompter.
 *
 * Most system prompters only display one prompt at a time and therefore
 * return %GCR_SYSTEM_PROMPTER_SINGLE.
 *
 * Returns: the prompter mode
 */
GcrSystemPrompterMode
gcr_system_prompter_get_mode (GcrSystemPrompter *self)
{
	g_return_val_if_fail (GCR_IS_SYSTEM_PROMPTER (self), GCR_SYSTEM_PROMPTER_SINGLE);
	return self->pv->mode;
}

/**
 * gcr_system_prompter_get_prompt_type:
 * @self: the prompter
 *
 * Get the #GType for prompts created by this prompter.
 *
 * The returned #GType will be a #GcrPrompt implementation.
 *
 * Returns: the prompt #GType
 */
GType
gcr_system_prompter_get_prompt_type (GcrSystemPrompter *self)
{
	g_return_val_if_fail (GCR_IS_SYSTEM_PROMPTER (self), 0);
	return self->pv->prompt_type;
}

/**
 * gcr_system_prompter_get_prompting:
 * @self: the prompter
 *
 * Get whether prompting or not.
 *
 * Returns: whether prompting or not
 */
gboolean
gcr_system_prompter_get_prompting (GcrSystemPrompter *self)
{
	g_return_val_if_fail (GCR_IS_SYSTEM_PROMPTER (self), FALSE);
	return g_hash_table_size (self->pv->callbacks);
}