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"
31 #include "gsimpleasyncresult.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 (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_TYPE_INSTANCE_GET_PRIVATE (interaction, G_TYPE_TLS_INTERACTION,
189 GTlsInteractionPrivate);
190 interaction->priv->context = g_main_context_get_thread_default ();
191 if (interaction->priv->context)
192 g_main_context_ref (interaction->priv->context);
196 g_tls_interaction_finalize (GObject *object)
198 GTlsInteraction *interaction = G_TLS_INTERACTION (object);
200 if (interaction->priv->context)
201 g_main_context_unref (interaction->priv->context);
203 G_OBJECT_CLASS (g_tls_interaction_parent_class)->finalize (object);
207 g_tls_interaction_class_init (GTlsInteractionClass *klass)
209 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
211 gobject_class->finalize = g_tls_interaction_finalize;
213 g_type_class_add_private (klass, sizeof (GTlsInteractionPrivate));
217 on_invoke_ask_password_sync (gpointer user_data)
219 InvokeClosure *closure = user_data;
220 GTlsInteractionClass *klass;
222 g_mutex_lock (&closure->mutex);
224 klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
225 g_assert (klass->ask_password);
227 closure->result = klass->ask_password (closure->interaction,
228 G_TLS_PASSWORD (closure->argument),
229 closure->cancellable,
232 closure->complete = TRUE;
233 g_cond_signal (&closure->cond);
234 g_mutex_unlock (&closure->mutex);
236 return FALSE; /* don't call again */
240 on_async_as_sync_complete (GObject *source,
241 GAsyncResult *result,
244 InvokeClosure *closure = user_data;
245 GTlsInteractionClass *klass;
247 g_mutex_lock (&closure->mutex);
249 klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
250 g_assert (klass->ask_password_finish);
252 closure->result = klass->ask_password_finish (closure->interaction,
256 closure->complete = TRUE;
257 g_cond_signal (&closure->cond);
258 g_mutex_unlock (&closure->mutex);
262 on_invoke_ask_password_async_as_sync (gpointer user_data)
264 InvokeClosure *closure = user_data;
265 GTlsInteractionClass *klass;
267 g_mutex_lock (&closure->mutex);
269 klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
270 g_assert (klass->ask_password_async);
272 klass->ask_password_async (closure->interaction,
273 G_TLS_PASSWORD (closure->argument),
274 closure->cancellable,
275 on_async_as_sync_complete,
278 /* Note that we've used these */
279 closure->callback = NULL;
280 closure->user_data = NULL;
282 g_mutex_unlock (&closure->mutex);
284 return FALSE; /* don't call again */
288 * g_tls_interaction_invoke_ask_password:
289 * @interaction: a #GTlsInteraction object
290 * @password: a #GTlsPassword object
291 * @cancellable: an optional #GCancellable cancellation object
292 * @error: an optional location to place an error on failure
294 * Invoke the interaction to ask the user for a password. It invokes this
295 * interaction in the main loop, specifically the #GMainContext returned by
296 * g_main_context_get_thread_default() when the interaction is created. This
297 * is called by called by #GTlsConnection or #GTlsDatabase to ask the user
300 * Derived subclasses usually implement a password prompt, although they may
301 * also choose to provide a password from elsewhere. The @password value will
302 * be filled in and then @callback will be called. Alternatively the user may
303 * abort this password request, which will usually abort the TLS connection.
305 * The implementation can either be a synchronous (eg: modal dialog) or an
306 * asynchronous one (eg: modeless dialog). This function will take care of
307 * calling which ever one correctly.
309 * If the interaction is cancelled by the cancellation object, or by the
310 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
311 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
312 * not support immediate cancellation.
314 * Returns: The status of the ask password interaction.
318 GTlsInteractionResult
319 g_tls_interaction_invoke_ask_password (GTlsInteraction *interaction,
320 GTlsPassword *password,
321 GCancellable *cancellable,
324 GTlsInteractionResult result;
325 InvokeClosure *closure;
326 GTlsInteractionClass *klass;
328 g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
329 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
330 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
332 closure = invoke_closure_new (interaction, G_OBJECT (password), cancellable);
334 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
335 if (klass->ask_password)
337 g_main_context_invoke (interaction->priv->context,
338 on_invoke_ask_password_sync, closure);
339 result = invoke_closure_wait_and_free (closure, error);
341 else if (klass->ask_password_async)
343 g_return_val_if_fail (klass->ask_password_finish, G_TLS_INTERACTION_UNHANDLED);
344 g_main_context_invoke (interaction->priv->context,
345 on_invoke_ask_password_async_as_sync, closure);
348 * Handle the case where we've been called from within the main context
349 * or in the case where the main context is not running. This approximates
350 * the behavior of a modal dialog.
352 if (g_main_context_acquire (interaction->priv->context))
354 while (!closure->complete)
356 g_mutex_unlock (&closure->mutex);
357 g_main_context_iteration (interaction->priv->context, TRUE);
358 g_mutex_lock (&closure->mutex);
360 g_main_context_release (interaction->priv->context);
364 g_propagate_error (error, closure->error);
365 closure->error = NULL;
368 result = closure->result;
369 invoke_closure_free (closure);
373 * Handle the case where we're in a different thread than the main
374 * context and a main loop is running.
378 result = invoke_closure_wait_and_free (closure, error);
383 result = G_TLS_INTERACTION_UNHANDLED;
384 invoke_closure_free (closure);
392 * g_tls_interaction_ask_password:
393 * @interaction: a #GTlsInteraction object
394 * @password: a #GTlsPassword object
395 * @cancellable: an optional #GCancellable cancellation object
396 * @error: an optional location to place an error on failure
398 * Run synchronous interaction to ask the user for a password. In general,
399 * g_tls_interaction_invoke_ask_password() should be used instead of this
402 * Derived subclasses usually implement a password prompt, although they may
403 * also choose to provide a password from elsewhere. The @password value will
404 * be filled in and then @callback will be called. Alternatively the user may
405 * abort this password request, which will usually abort the TLS connection.
407 * If the interaction is cancelled by the cancellation object, or by the
408 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
409 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
410 * not support immediate cancellation.
412 * Returns: The status of the ask password interaction.
416 GTlsInteractionResult
417 g_tls_interaction_ask_password (GTlsInteraction *interaction,
418 GTlsPassword *password,
419 GCancellable *cancellable,
422 GTlsInteractionClass *klass;
424 g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
425 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
426 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
428 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
429 if (klass->ask_password)
430 return (klass->ask_password) (interaction, password, cancellable, error);
432 return G_TLS_INTERACTION_UNHANDLED;
436 * g_tls_interaction_ask_password_async:
437 * @interaction: a #GTlsInteraction object
438 * @password: a #GTlsPassword object
439 * @cancellable: an optional #GCancellable cancellation object
440 * @callback: (allow-none): will be called when the interaction completes
441 * @user_data: (allow-none): data to pass to the @callback
443 * Run asynchronous interaction to ask the user for a password. In general,
444 * g_tls_interaction_invoke_ask_password() should be used instead of this
447 * Derived subclasses usually implement a password prompt, although they may
448 * also choose to provide a password from elsewhere. The @password value will
449 * be filled in and then @callback will be called. Alternatively the user may
450 * abort this password request, which will usually abort the TLS connection.
452 * If the interaction is cancelled by the cancellation object, or by the
453 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
454 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
455 * not support immediate cancellation.
457 * Certain implementations may not support immediate cancellation.
462 g_tls_interaction_ask_password_async (GTlsInteraction *interaction,
463 GTlsPassword *password,
464 GCancellable *cancellable,
465 GAsyncReadyCallback callback,
468 GTlsInteractionClass *klass;
469 GSimpleAsyncResult *res;
471 g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
472 g_return_if_fail (G_IS_TLS_PASSWORD (password));
473 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
475 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
476 if (klass->ask_password_async)
478 g_return_if_fail (klass->ask_password_finish);
479 (klass->ask_password_async) (interaction, password, cancellable,
480 callback, user_data);
484 res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
485 g_tls_interaction_ask_password_async);
486 g_simple_async_result_complete_in_idle (res);
487 g_object_unref (res);
492 * g_tls_interaction_ask_password_finish:
493 * @interaction: a #GTlsInteraction object
494 * @result: the result passed to the callback
495 * @error: an optional location to place an error on failure
497 * Complete an ask password user interaction request. This should be once
498 * the g_tls_interaction_ask_password_async() completion callback is called.
500 * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsPassword passed
501 * to g_tls_interaction_ask_password() will have its password filled in.
503 * If the interaction is cancelled by the cancellation object, or by the
504 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
505 * contains a %G_IO_ERROR_CANCELLED error code.
507 * Returns: The status of the ask password interaction.
511 GTlsInteractionResult
512 g_tls_interaction_ask_password_finish (GTlsInteraction *interaction,
513 GAsyncResult *result,
516 GTlsInteractionClass *klass;
518 g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
519 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED);
521 /* If it's one of our simple unhandled async results, handle here */
522 if (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
523 g_tls_interaction_ask_password_async))
525 return G_TLS_INTERACTION_UNHANDLED;
528 /* Invoke finish of derived class */
531 klass = G_TLS_INTERACTION_GET_CLASS (interaction);
532 g_return_val_if_fail (klass->ask_password_finish, G_TLS_INTERACTION_UNHANDLED);
533 return (klass->ask_password_finish) (interaction, result, error);