Debian Codegraph @

daemon/dbus/gkd-secret-change.c

raw symbols SCIP
1 /*
2 * gnome-keyring
3 *
4 * Copyright (C) 2008 Stefan Walter
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "gkd-secret-change.h"
24 #include "gkd-secret-prompt.h"
25 #include "gkd-secret-secret.h"
26 #include "gkd-secret-service.h"
27 #include "gkd-secret-session.h"
28 #include "gkd-secret-types.h"
29
30 #include "egg/egg-error.h"
31 #include "egg/egg-secure-memory.h"
32
33 #include "pkcs11/pkcs11i.h"
34
35 #include <glib/gi18n.h>
36
37 #include <gck/gck.h>
38 #include <gcr/gcr-base.h>
39
40 #include <string.h>
41
42 enum {
43 PROP_0,
44 PROP_COLLECTION_PATH
45 };
46
47 struct _GkdSecretChange {
48 GkdSecretPrompt parent;
49 gchar *collection_path;
50 GckSession *session;
51 GkdSecretSecret *master;
52 GckObject *ocred;
53 gboolean unlocked;
54 gboolean confirmed;
55 };
56
57 struct _GkdSecretChangeClass {
58 GkdSecretPromptClass parent_class;
59 };
60
61 static void perform_prompting (GkdSecretChange *self,
62 GckObject *collection);
63
64 G_DEFINE_TYPE (GkdSecretChange, gkd_secret_change, GKD_SECRET_TYPE_PROMPT);
65
66 static void
67 setup_original_prompt (GkdSecretChange *self,
68 GckObject *collection)
69 {
70 GcrPrompt *prompt = GCR_PROMPT (self);
71 GError *error = NULL;
72 gpointer data;
73 gsize n_data;
74 g_autofree gchar *label = NULL;
75 gchar *text;
76
77 data = gck_object_get_data (collection, CKA_LABEL, NULL, &n_data, &error);
78 if (!data) {
79 g_warning ("couldn't get label for collection: %s", egg_error_message (error));
80 g_clear_error (&error);
81 }
82
83 if (!data || !n_data)
84 label = g_strdup (_("Unnamed"));
85 else
86 label = g_strndup (data, n_data);
87 g_free (data);
88
89 text = g_strdup_printf (_("Enter the old password for the “%s” keyring"), label);
90 gcr_prompt_set_message (prompt, text);
91 g_free (text);
92
93 text = g_strdup_printf (_("An application wants to change the password for the “%s” keyring. "
94 "Enter the old password for it."), label);
95 gcr_prompt_set_description (prompt, text);
96 g_free (text);
97
98 gcr_prompt_set_password_new (prompt, FALSE);
99 gcr_prompt_set_continue_label (prompt, _("Continue"));
100 }
101
102 static void
103 setup_password_prompt (GkdSecretChange *self,
104 GckObject *collection)
105 {
106 GcrPrompt *prompt = GCR_PROMPT (self);
107 GError *error = NULL;
108 gpointer data;
109 gsize n_data;
110 g_autofree gchar *label = NULL;
111 gchar *text;
112
113 data = gck_object_get_data (collection, CKA_LABEL, NULL, &n_data, &error);
114 if (!data) {
115 g_warning ("couldn't get label for collection: %s", egg_error_message (error));
116 g_clear_error (&error);
117 }
118
119 if (!data || !n_data)
120 label = g_strdup (_("Unnamed"));
121 else
122 label = g_strndup (data, n_data);
123 g_free (data);
124
125 text = g_strdup_printf (_("Choose a new password for the “%s” keyring"), label);
126 gcr_prompt_set_message (prompt, text);
127 g_free (text);
128
129 text = g_strdup_printf (_("An application wants to change the password for the “%s” keyring. "
130 "Choose the new password you want to use for it."), label);
131 gcr_prompt_set_description (prompt, text);
132 g_free (text);
133
134 gcr_prompt_set_password_new (prompt, TRUE);
135 gcr_prompt_set_continue_label (prompt, _("Continue"));
136 gcr_prompt_set_warning (prompt, NULL);
137 }
138
139 static void
140 setup_confirmation_prompt (GkdSecretChange *self)
141 {
142 gcr_prompt_set_message (GCR_PROMPT (self), _("Store passwords unencrypted?"));
143 gcr_prompt_set_description (GCR_PROMPT (self),
144 _("By choosing to use a blank password, your stored passwords will not be safely encrypted. "
145 "They will be accessible by anyone with access to your files."));
146 gcr_prompt_set_continue_label (GCR_PROMPT (self), _("Continue"));
147 }
148
149 static void
150 set_warning_wrong (GkdSecretChange *self)
151 {
152 gcr_prompt_set_warning (GCR_PROMPT (self), _("The original password was incorrect"));
153 }
154
155 static void
156 on_prompt_original_complete (GObject *source,
157 GAsyncResult *result,
158 gpointer user_data)
159 {
160 GkdSecretChange *self = GKD_SECRET_CHANGE (source);
161 GkdSecretPrompt *prompt = GKD_SECRET_PROMPT (source);
162 GckBuilder builder = GCK_BUILDER_INIT;
163 gboolean continue_prompting = TRUE;
164 GkdSecretSecret *original;
165 GckAttributes *attrs;
166 GError *error = NULL;
167 GckObject *collection;
168
169 gcr_prompt_password_finish (GCR_PROMPT (source), result, &error);
170 if (error != NULL) {
171 gkd_secret_prompt_dismiss_with_error (prompt, error);
172 g_error_free (error);
173 return;
174 }
175
176 /* The prompt was cancelled */
177 original = gkd_secret_prompt_take_secret (prompt);
178 if (original == NULL) {
179 gkd_secret_prompt_dismiss (prompt);
180 return;
181 }
182
183 collection = gkd_secret_prompt_lookup_collection (prompt, self->collection_path);
184 if (collection != NULL) {
185 gck_builder_add_ulong (&builder, CKA_CLASS, CKO_G_CREDENTIAL);
186 gck_builder_add_boolean (&builder, CKA_TOKEN, FALSE);
187 gck_builder_add_ulong (&builder, CKA_G_OBJECT, gck_object_get_handle (collection));
188
189 attrs = gck_attributes_ref_sink (gck_builder_end (&builder));
190
191 /* Create the original credential, in order to make sure we can unlock the collection */
192 self->ocred = gkd_secret_session_create_credential (original->session,
193 self->session, attrs,
194 original, &error);
195
196 gck_attributes_unref (attrs);
197
198 /* The unlock failed because password was bad */
199 if (g_error_matches (error, GCK_ERROR, CKR_PIN_INCORRECT)) {
200 set_warning_wrong (self);
201 g_error_free (error);
202
203 /* The unlock failed for some other reason */
204 } else if (error != NULL) {
205 continue_prompting = FALSE;
206 gkd_secret_prompt_dismiss_with_error (prompt, error);
207 g_error_free (error);
208
209 /* The unlock succeeded */
210 } else {
211 if (self->session == NULL)
212 self->session = gck_object_get_session (self->ocred);
213 self->unlocked = TRUE;
214 }
215 }
216
217 if (continue_prompting)
218 perform_prompting (self, collection);
219
220 gkd_secret_secret_free (original);
221 g_clear_object (&collection);
222 }
223
224 static void
225 on_prompt_password_complete (GObject *source,
226 GAsyncResult *result,
227 gpointer user_data)
228 {
229 GkdSecretChange *self = GKD_SECRET_CHANGE (source);
230 GkdSecretPrompt *prompt = GKD_SECRET_PROMPT (source);
231 GError *error = NULL;
232 GckObject *collection;
233
234 gcr_prompt_password_finish (GCR_PROMPT (source), result, &error);
235 if (error != NULL) {
236 gkd_secret_prompt_dismiss_with_error (prompt, error);
237 g_error_free (error);
238 return;
239 }
240
241 self->master = gkd_secret_prompt_take_secret (prompt);
242 if (self->master == NULL) {
243 gkd_secret_prompt_dismiss (prompt);
244 return;
245 }
246
247 /* If the password strength is greater than zero, then don't confirm */
248 if (gcr_prompt_get_password_strength (GCR_PROMPT (source)) > 0)
249 self->confirmed = TRUE;
250
251 collection = gkd_secret_prompt_lookup_collection (prompt, self->collection_path);
252 perform_prompting (self, collection);
253 g_clear_object (&collection);
254 }
255
256 static void
257 on_prompt_confirmation_complete (GObject *source,
258 GAsyncResult *result,
259 gpointer user_data)
260 {
261 GkdSecretChange *self = GKD_SECRET_CHANGE (source);
262 GkdSecretPrompt *prompt = GKD_SECRET_PROMPT (source);
263 GError *error = NULL;
264 GckObject *collection;
265
266 self->confirmed = gcr_prompt_confirm_finish (GCR_PROMPT (source), result, &error);
267 if (error != NULL) {
268 gkd_secret_prompt_dismiss_with_error (prompt, error);
269 g_error_free (error);
270 return;
271 }
272
273 /* If not confirmed, then prompt again */
274 if (!self->confirmed) {
275 gkd_secret_secret_free (self->master);
276 self->master = NULL;
277 }
278
279 collection = gkd_secret_prompt_lookup_collection (prompt, self->collection_path);
280 perform_prompting (self, collection);
281 g_clear_object (&collection);
282 }
283
284 static void
285 perform_prompting (GkdSecretChange *self,
286 GckObject *collection)
287 {
288 GkdSecretPrompt *prompt = GKD_SECRET_PROMPT (self);
289 GError *error = NULL;
290
291 /* Collection doesn't exist, just go away */
292 if (collection == NULL) {
293 gkd_secret_prompt_dismiss (prompt);
294
295 /* Get the original password and unlock */
296 } else if (!self->unlocked) {
297 setup_original_prompt (self, collection);
298 gcr_prompt_password_async (GCR_PROMPT (self),
299 gkd_secret_prompt_get_cancellable (prompt),
300 on_prompt_original_complete, NULL);
301
302 /* Get the new password */
303 } else if (self->master == NULL) {
304 setup_password_prompt (self, collection);
305 gcr_prompt_password_async (GCR_PROMPT (self),
306 gkd_secret_prompt_get_cancellable (prompt),
307 on_prompt_password_complete, NULL);
308
309 /* Check that the password is not empty */
310 } else if (!self->confirmed) {
311 setup_confirmation_prompt (self);
312 gcr_prompt_confirm_async (GCR_PROMPT (self),
313 gkd_secret_prompt_get_cancellable (prompt),
314 on_prompt_confirmation_complete, NULL);
315
316 /* Actually create the keyring */
317 } else if (gkd_secret_change_with_secrets (collection, self->session,
318 NULL, self->master, &error)) {
319 gkd_secret_prompt_complete (prompt);
320
321 /* Failed */
322 } else {
323 gkd_secret_prompt_dismiss_with_error (prompt, error);
324 g_error_free (error);
325 }
326 }
327
328 static void
329 gkd_secret_change_prompt_ready (GkdSecretPrompt *prompt)
330 {
331 GkdSecretChange *self = GKD_SECRET_CHANGE (prompt);
332 GckObject *collection;
333
334 collection = gkd_secret_prompt_lookup_collection (prompt, self->collection_path);
335 perform_prompting (self, collection);
336 g_clear_object (&collection);
337 }
338
339 static GVariant *
340 gkd_secret_change_encode_result (GkdSecretPrompt *base)
341 {
342 return g_variant_new_variant (g_variant_new_string (""));
343 }
344
345 static void
346 gkd_secret_change_init (GkdSecretChange *self)
347 {
348 gcr_prompt_set_title (GCR_PROMPT (self), _("Change Keyring Password"));
349 }
350
351 static void
352 gkd_secret_change_dispose (GObject *obj)
353 {
354 GkdSecretChange *self = GKD_SECRET_CHANGE (obj);
355
356 if (self->ocred) {
357 gck_object_destroy (self->ocred, NULL, NULL);
358 g_object_unref (self->ocred);
359 self->ocred = NULL;
360 }
361
362 G_OBJECT_CLASS (gkd_secret_change_parent_class)->dispose (obj);
363 }
364
365 static void
366 gkd_secret_change_finalize (GObject *obj)
367 {
368 GkdSecretChange *self = GKD_SECRET_CHANGE (obj);
369
370 g_free (self->collection_path);
371 if (self->master)
372 gkd_secret_secret_free (self->master);
373 if (self->session)
374 g_object_unref (self->session);
375
376 G_OBJECT_CLASS (gkd_secret_change_parent_class)->finalize (obj);
377 }
378
379 static void
380 gkd_secret_change_set_property (GObject *obj, guint prop_id, const GValue *value,
381 GParamSpec *pspec)
382 {
383 GkdSecretChange *self = GKD_SECRET_CHANGE (obj);
384
385 switch (prop_id) {
386 case PROP_COLLECTION_PATH:
387 g_return_if_fail (!self->collection_path);
388 self->collection_path = g_value_dup_string (value);
389 g_return_if_fail (self->collection_path);
390 break;
391 default:
392 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
393 break;
394 }
395 }
396
397 static void
398 gkd_secret_change_get_property (GObject *obj, guint prop_id, GValue *value,
399 GParamSpec *pspec)
400 {
401 GkdSecretChange *self = GKD_SECRET_CHANGE (obj);
402
403 switch (prop_id) {
404 case PROP_COLLECTION_PATH:
405 g_value_set_string (value, self->collection_path);
406 break;
407 default:
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
409 break;
410 }
411 }
412
413 static void
414 gkd_secret_change_class_init (GkdSecretChangeClass *klass)
415 {
416 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
417 GkdSecretPromptClass *prompt_class = GKD_SECRET_PROMPT_CLASS (klass);
418
419 gobject_class->dispose = gkd_secret_change_dispose;
420 gobject_class->finalize = gkd_secret_change_finalize;
421 gobject_class->get_property = gkd_secret_change_get_property;
422 gobject_class->set_property = gkd_secret_change_set_property;
423
424 prompt_class->prompt_ready = gkd_secret_change_prompt_ready;
425 prompt_class->encode_result = gkd_secret_change_encode_result;
426
427 g_object_class_install_property (gobject_class, PROP_COLLECTION_PATH,
428 g_param_spec_string ("collection-path", "Collection Path", "Collection Path",
429 "/", G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
430 }
431
432 /* -----------------------------------------------------------------------------
433 * PUBLIC
434 */
435
436 GkdSecretChange*
437 gkd_secret_change_new (GkdSecretService *service, const gchar *caller,
438 const gchar *path)
439 {
440 const gchar *prompter_name;
441
442 g_return_val_if_fail (GKD_SECRET_IS_SERVICE (service), NULL);
443 g_return_val_if_fail (caller, NULL);
444 g_return_val_if_fail (path, NULL);
445
446 prompter_name = g_getenv ("GNOME_KEYRING_TEST_PROMPTER");
447 return g_object_new (GKD_SECRET_TYPE_CHANGE,
448 "service", service,
449 "caller", caller,
450 "collection-path", path,
451 "bus-name", prompter_name,
452 NULL);
453 }
454
455 gboolean
456 gkd_secret_change_with_secrets (GckObject *collection,
457 GckSession *session,
458 GkdSecretSecret *original,
459 GkdSecretSecret *master,
460 GError **error)
461 {
462 GckBuilder builder = GCK_BUILDER_INIT;
463 GckAttributes *attrs = NULL;
464 gboolean result = FALSE;
465 GckObject *ocred = NULL;
466 GckObject *mcred = NULL;
467
468 g_assert (GCK_IS_OBJECT (collection));
469 g_assert (session == NULL || GCK_IS_SESSION (session));
470 g_assert (master != NULL);
471 g_assert (error == NULL || *error == NULL);
472
473 /* Create the new credential */
474 gck_builder_add_ulong (&builder, CKA_CLASS, CKO_G_CREDENTIAL);
475 gck_builder_add_boolean (&builder, CKA_TOKEN, FALSE);
476 attrs = gck_attributes_ref_sink (gck_builder_end (&builder));
477 mcred = gkd_secret_session_create_credential (master->session, session, attrs, master, error);
478 gck_builder_add_all (&builder, attrs);
479 gck_attributes_unref (attrs);
480
481 if (mcred == NULL)
482 goto cleanup;
483
484 /* Create the original credential, in order to make sure we can the collection */
485 if (original) {
486 gck_builder_add_ulong (&builder, CKA_G_OBJECT, gck_object_get_handle (collection));
487 attrs = gck_attributes_ref_sink (gck_builder_end (&builder));
488 ocred = gkd_secret_session_create_credential (original->session, session, attrs, original, error);
489 gck_attributes_unref (attrs);
490
491 if (ocred == NULL)
492 goto cleanup;
493 }
494
495 gck_builder_clear (&builder);
496 gck_builder_add_ulong (&builder, CKA_G_CREDENTIAL, gck_object_get_handle (mcred));
497
498 /* Now set the collection credentials to the first one */
499 result = gck_object_set (collection, gck_builder_end (&builder), NULL, error);
500
501 cleanup:
502 if (ocred) {
503 /* Always destroy the original credential */
504 gck_object_destroy (ocred, NULL, NULL);
505 g_object_unref (ocred);
506 }
507 if (mcred) {
508 /* Always destroy the master credential */
509 gck_object_destroy (mcred, NULL, NULL);
510 g_object_unref (mcred);
511 }
512
513 gck_builder_clear (&builder);
514 return result;
515 }