1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2011 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Stef Walter <stefw@collabora.co.uk>
27 #include "gtlsinteraction.h"
28 #include "gtlspassword.h"
29 #include "gasyncresult.h"
30 #include "gcancellable.h"
32 #include "gioenumtypes.h"
37 * SECTION:gtlsinteraction
38 * @short_description: Interaction with the user during TLS operations.
41 * #GTlsInteraction provides a mechanism for the TLS connection and database
42 * code to interact with the user. It can be used to ask the user for passwords.
44 * To use a #GTlsInteraction with a TLS connection use
45 * g_tls_connection_set_interaction().
47 * Callers should instantiate a derived class that implements the various
48 * interaction methods to show the required dialogs.
50 * Callers should use the 'invoke' functions like
51 * g_tls_interaction_invoke_ask_password() to run interaction methods. These
52 * functions make sure that the interaction is invoked in the main loop
53 * and not in the current thread, if the current thread is not running the
56 * Derived classes can choose to implement whichever interactions methods they'd
57 * like to support by overriding those virtual methods in their class
58 * initialization function. Any interactions not implemented will return
59 * %G_TLS_INTERACTION_UNHANDLED. If a derived class implements an async method,
60 * it must also implement the corresponding finish method.
66 * An object representing interaction that the TLS connection and database
67 * might have with the user.
73 * GTlsInteractionClass:
74 * @ask_password: ask for a password synchronously. If the implementation
75 * returns %G_TLS_INTERACTION_HANDLED, then the password argument should
76 * have been filled in by using g_tls_password_set_value() or a similar
78 * @ask_password_async: ask for a password asynchronously.
79 * @ask_password_finish: complete operation to ask for a password asynchronously.
80 * If the implementation returns %G_TLS_INTERACTION_HANDLED, then the
81 * password argument of the async method should have been filled in by using
82 * g_tls_password_set_value() or a similar function.
84 * The class for #GTlsInteraction. Derived classes implement the various
85 * virtual interaction methods to handle TLS interactions.
87 * Derived classes can choose to implement whichever interactions methods they'd
88 * like to support by overriding those virtual methods in their class
89 * initialization function. If a derived class implements an async method,
90 * it must also implement the corresponding finish method.
92 * The synchronous interaction methods should implement to display modal dialogs,
93 * and the asynchronous methods to display modeless dialogs.
95 * If the user cancels an interaction, then the result should be
96 * %G_TLS_INTERACTION_FAILED and the error should be set with a domain of
97 * %G_IO_ERROR and code of %G_IO_ERROR_CANCELLED.
102 struct _GTlsInteractionPrivate {
103 GMainContext *context;
106 G_DEFINE_TYPE_WITH_PRIVATE (GTlsInteraction, g_tls_interaction, G_TYPE_OBJECT)
111 /* Input arguments */
112 GTlsInteraction *interaction;
114 GCancellable *cancellable;
116 /* Used when we're invoking async interactions */
117 GAsyncReadyCallback callback;
120 /* Used when we expect results */
121 GTlsInteractionResult result;
128 invoke_closure_free (gpointer data)
130 InvokeClosure *closure = data;
132 g_object_unref (closure->interaction);
133 g_clear_object (&closure->argument);
134 g_clear_object (&closure->cancellable);
135 g_cond_clear (&closure->cond);
136 g_mutex_clear (&closure->mutex);
137 g_clear_error (&closure->error);
139 /* Insurance that we've actually used these before freeing */
140 g_assert (closure->callback == NULL);
141 g_assert (closure->user_data == NULL);
146 static InvokeClosure *
147 invoke_closure_new (GTlsInteraction *interaction,
149 GCancellable *cancellable)
151 InvokeClosure *closure = g_new0 (InvokeClosure, 1);
152 closure->interaction = g_object_ref (interaction);
153 closure->argument = argument ? g_object_ref (argument) : NULL;
154 closure->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
155 g_mutex_init (&closure->mutex);
156 g_cond_init (&closure->cond);
157 closure->result = G_TLS_INTERACTION_UNHANDLED;
161 static GTlsInteractionResult
162 invoke_closure_wait_and_free (InvokeClosure *closure,
165 GTlsInteractionResult result;
167 g_mutex_lock (&closure->mutex);
169 while (!closure->complete)
170 g_cond_wait (&closure->cond, &closure->mutex);
172 g_mutex_unlock (&closure->mutex);
176 g_propagate_error (error, closure->error);
177 closure->error = NULL;
179 result = closure->result;
181 invoke_closure_free (closure);
186 g_tls_interaction_init (GTlsInteraction *interaction)
188 interaction->priv = g_tls_interaction_get_instance_private (interaction);
189 interaction->priv->context = g_main_context_ref_thread_default ();
193 g_tls_interaction_finalize (GObject *object)
195 GTlsInteraction *interaction = G_TLS_INTERACTION (object);
197 g_main_context_unref (interaction->priv->context);
199 G_OBJECT_CLASS (g_tls_interaction_parent_class)->finalize (object);
203 g_tls_interaction_class_init (GTlsInteractionClass *klass)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
207 gobject_class->finalize = g_tls_interaction_finalize;
211 on_invoke_ask_password_sync (gpointer user_data)
213 InvokeClosure *closure = user_data;
214 GTlsInteractionClass *klass;
216 g_mutex_lock (&closure->mutex);
218 klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
219 g_assert (klass->ask_password);
221 closure->result = klass->ask_password (closure->interaction,
222 G_TLS_PASSWORD (closure->argument),
223 closure->cancellable,
226 closure->complete = TRUE;
227 g_cond_signal (&closure->cond);
228 g_mutex_unlock (&closure->mutex);
230 return FALSE; /* don't call again */
234 on_async_as_sync_complete (GObject *source,
235 GAsyncResult *result,
238 InvokeClosure *closure = user_data;
239 GTlsInteractionClass *klass;
241 g_mutex_lock (&closure->mutex);
243 klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
244 g_assert (klass->ask_password_finish);
246 closure->result = klass->ask_password_finish (closure->interaction,
250 closure->complete = TRUE;
251 g_cond_signal (&closure->cond);
252 g_mutex_unlock (&closure->mutex);
256 on_invoke_ask_password_async_as_sync (gpointer user_data)
258 InvokeClosure *closure = user_data;
259 GTlsInteractionClass *klass;
261 g_mutex_lock (&closure->mutex);
263 klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
264 g_assert (klass->ask_password_async);
266 klass->ask_password_async (closure->interaction,
267 G_TLS_PASSWORD (closure->argument),
268 closure->cancellable,
269 on_async_as_sync_complete,
272 /* Note that we've used these */
273 closure->callback = NULL;
274 closure->user_data = NULL;
276 g_mutex_unlock (&closure->mutex);
278 return FALSE; /* don't call again */
282 * g_tls_interaction_invoke_ask_password:
283 * @interaction: a #GTlsInteraction object
284 * @password: a #GTlsPassword object
285 * @cancellable: an optional #GCancellable cancellation object
286 * @error: an optional location to place an error on failure
288 * Invoke the interaction to ask the user for a password. It invokes this
289 * interaction in the main loop, specifically the #GMainContext returned by
290 * g_main_context_get_thread_default() when the interaction is created. This
291 * is called by called by #GTlsConnection or #GTlsDatabase to ask the user
294 * Derived subclasses usually implement a password prompt, although they may
295 * also choose to provide a password from elsewhere. The @password value will
296 * be filled in and then @callback will be called. Alternatively the user may
297 * abort this password request, which will usually abort the TLS connection.
299 * The implementation can either be a synchronous (eg: modal dialog) or an
300 * asynchronous one (eg: modeless dialog). This function will take care of
301 * calling which ever one correctly.
303 * If the interaction is cancelled by the cancellation object, or by the
304 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
305 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
306 * not support immediate cancellation.
308 * Returns: The status of the ask password interaction.
312 GTlsInteractionResult
313 g_tls_interaction_invoke_ask_password (GTlsInteraction *interaction,
314 GTlsPassword *password,
315 GCancellable *cancellable,
318 GTlsInteractionResult result;
319 InvokeClosure *closure;
320 GTlsInteractionClass *klass;
323 g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
324 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
325 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
327 closure = invoke_closure_new (interaction, G_OBJECT (password), cancellable);
329 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
330 if (klass->ask_password)
332 g_main_context_invoke (interaction->priv->context,
333 on_invoke_ask_password_sync, closure);
334 result = invoke_closure_wait_and_free (closure, error);
336 else if (klass->ask_password_async)
338 g_return_val_if_fail (klass->ask_password_finish, G_TLS_INTERACTION_UNHANDLED);
339 g_main_context_invoke (interaction->priv->context,
340 on_invoke_ask_password_async_as_sync, closure);
343 * Handle the case where we've been called from within the main context
344 * or in the case where the main context is not running. This approximates
345 * the behavior of a modal dialog.
347 if (g_main_context_acquire (interaction->priv->context))
351 g_mutex_lock (&closure->mutex);
352 complete = closure->complete;
353 g_mutex_unlock (&closure->mutex);
356 g_main_context_iteration (interaction->priv->context, TRUE);
359 g_main_context_release (interaction->priv->context);
363 g_propagate_error (error, closure->error);
364 closure->error = NULL;
367 result = closure->result;
368 invoke_closure_free (closure);
372 * Handle the case where we're in a different thread than the main
373 * context and a main loop is running.
377 result = invoke_closure_wait_and_free (closure, error);
382 result = G_TLS_INTERACTION_UNHANDLED;
383 invoke_closure_free (closure);
391 * g_tls_interaction_ask_password:
392 * @interaction: a #GTlsInteraction object
393 * @password: a #GTlsPassword object
394 * @cancellable: an optional #GCancellable cancellation object
395 * @error: an optional location to place an error on failure
397 * Run synchronous interaction to ask the user for a password. In general,
398 * g_tls_interaction_invoke_ask_password() should be used instead of this
401 * Derived subclasses usually implement a password prompt, although they may
402 * also choose to provide a password from elsewhere. The @password value will
403 * be filled in and then @callback will be called. Alternatively the user may
404 * abort this password request, which will usually abort the TLS connection.
406 * If the interaction is cancelled by the cancellation object, or by the
407 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
408 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
409 * not support immediate cancellation.
411 * Returns: The status of the ask password interaction.
415 GTlsInteractionResult
416 g_tls_interaction_ask_password (GTlsInteraction *interaction,
417 GTlsPassword *password,
418 GCancellable *cancellable,
421 GTlsInteractionClass *klass;
423 g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
424 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
425 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
427 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
428 if (klass->ask_password)
429 return (klass->ask_password) (interaction, password, cancellable, error);
431 return G_TLS_INTERACTION_UNHANDLED;
435 * g_tls_interaction_ask_password_async:
436 * @interaction: a #GTlsInteraction object
437 * @password: a #GTlsPassword object
438 * @cancellable: an optional #GCancellable cancellation object
439 * @callback: (allow-none): will be called when the interaction completes
440 * @user_data: (allow-none): data to pass to the @callback
442 * Run asynchronous interaction to ask the user for a password. In general,
443 * g_tls_interaction_invoke_ask_password() should be used instead of this
446 * Derived subclasses usually implement a password prompt, although they may
447 * also choose to provide a password from elsewhere. The @password value will
448 * be filled in and then @callback will be called. Alternatively the user may
449 * abort this password request, which will usually abort the TLS connection.
451 * If the interaction is cancelled by the cancellation object, or by the
452 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
453 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
454 * not support immediate cancellation.
456 * Certain implementations may not support immediate cancellation.
461 g_tls_interaction_ask_password_async (GTlsInteraction *interaction,
462 GTlsPassword *password,
463 GCancellable *cancellable,
464 GAsyncReadyCallback callback,
467 GTlsInteractionClass *klass;
470 g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
471 g_return_if_fail (G_IS_TLS_PASSWORD (password));
472 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
474 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
475 if (klass->ask_password_async)
477 g_return_if_fail (klass->ask_password_finish);
478 (klass->ask_password_async) (interaction, password, cancellable,
479 callback, user_data);
483 task = g_task_new (interaction, cancellable, callback, user_data);
484 g_task_set_source_tag (task, g_tls_interaction_ask_password_async);
485 g_task_return_int (task, G_TLS_INTERACTION_UNHANDLED);
486 g_object_unref (task);
491 * g_tls_interaction_ask_password_finish:
492 * @interaction: a #GTlsInteraction object
493 * @result: the result passed to the callback
494 * @error: an optional location to place an error on failure
496 * Complete an ask password user interaction request. This should be once
497 * the g_tls_interaction_ask_password_async() completion callback is called.
499 * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsPassword passed
500 * to g_tls_interaction_ask_password() will have its password filled in.
502 * If the interaction is cancelled by the cancellation object, or by the
503 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
504 * contains a %G_IO_ERROR_CANCELLED error code.
506 * Returns: The status of the ask password interaction.
510 GTlsInteractionResult
511 g_tls_interaction_ask_password_finish (GTlsInteraction *interaction,
512 GAsyncResult *result,
515 GTlsInteractionClass *klass;
517 g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
518 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED);
520 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
521 if (klass->ask_password_finish)
523 g_return_val_if_fail (klass->ask_password_async != NULL, G_TLS_INTERACTION_UNHANDLED);
525 return (klass->ask_password_finish) (interaction, result, error);
529 g_return_val_if_fail (g_async_result_is_tagged (result, g_tls_interaction_ask_password_async), G_TLS_INTERACTION_UNHANDLED);
531 return g_task_propagate_int (G_TASK (result), error);