1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2011 Collabora, Ltd.
4 *
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.1 of the License, or (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Stef Walter <stefw@collabora.co.uk>
19 */
20
21#include "config.h"
22
23#include <glib.h>
24#include <glib/gprintf.h>
25#include <string.h>
26
27#ifdef G_OS_WIN32
28#include <conio.h>
29#endif
30
31#include "gtlsconsoleinteraction.h"
32
33/*
34 * WARNING: This is not the example you're looking for [slow hand wave]. This
35 * is not industrial strength, it's just for testing. It uses embarrassing
36 * functions like getpass() and does lazy things with threads.
37 */
38
39G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INTERACTION)
40
41#if defined(G_OS_WIN32) || defined(__BIONIC__)
42/* win32 doesn't have getpass() */
43#include <stdio.h>
44#ifndef BUFSIZ
45#define BUFSIZ 8192
46#endif
47static gchar *
48getpass (const gchar *prompt)
49{
50 static gchar buf[BUFSIZ];
51 gint i;
52
53 g_printf ("%s", prompt);
54 fflush (stdout);
55
56 for (i = 0; i < BUFSIZ - 1; ++i)
57 {
58#ifdef __BIONIC__
59 buf[i] = getc (stdin);
60#else
61 buf[i] = _getch ();
62#endif
63 if (buf[i] == '\r')
64 break;
65 }
66 buf[i] = '\0';
67
68 g_printf ("\n");
69
70 return &buf[0];
71}
72#endif
73
74static GTlsInteractionResult
75g_tls_console_interaction_ask_password (GTlsInteraction *interaction,
76 GTlsPassword *password,
77 GCancellable *cancellable,
78 GError **error)
79{
80 const gchar *value;
81 gchar *prompt;
82
83 prompt = g_strdup_printf (format: "Password \"%s\"': ", g_tls_password_get_description (password));
84 value = getpass (prompt: prompt);
85 g_free (mem: prompt);
86
87 if (g_cancellable_set_error_if_cancelled (cancellable, error))
88 return G_TLS_INTERACTION_FAILED;
89
90 g_tls_password_set_value (password, value: (guchar *)value, length: -1);
91 return G_TLS_INTERACTION_HANDLED;
92}
93
94static void
95ask_password_with_getpass (GTask *task,
96 gpointer object,
97 gpointer task_data,
98 GCancellable *cancellable)
99{
100 GTlsPassword *password = task_data;
101 GError *error = NULL;
102
103 g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password,
104 cancellable, error: &error);
105 if (error != NULL)
106 g_task_return_error (task, error);
107 else
108 g_task_return_int (task, result: G_TLS_INTERACTION_HANDLED);
109}
110
111static void
112g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction,
113 GTlsPassword *password,
114 GCancellable *cancellable,
115 GAsyncReadyCallback callback,
116 gpointer user_data)
117{
118 GTask *task;
119
120 task = g_task_new (source_object: interaction, cancellable, callback, callback_data: user_data);
121 g_task_set_task_data (task, g_object_ref (password), task_data_destroy: g_object_unref);
122 g_task_run_in_thread (task, task_func: ask_password_with_getpass);
123 g_object_unref (object: task);
124}
125
126static GTlsInteractionResult
127g_tls_console_interaction_ask_password_finish (GTlsInteraction *interaction,
128 GAsyncResult *result,
129 GError **error)
130{
131 GTlsInteractionResult ret;
132
133 g_return_val_if_fail (g_task_is_valid (result, interaction),
134 G_TLS_INTERACTION_FAILED);
135
136 ret = g_task_propagate_int (G_TASK (result), error);
137 if (ret == (GTlsInteractionResult)-1)
138 return G_TLS_INTERACTION_FAILED;
139 else
140 return ret;
141}
142
143static void
144g_tls_console_interaction_init (GTlsConsoleInteraction *interaction)
145{
146
147}
148
149static void
150g_tls_console_interaction_class_init (GTlsConsoleInteractionClass *klass)
151{
152 GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
153 interaction_class->ask_password = g_tls_console_interaction_ask_password;
154 interaction_class->ask_password_async = g_tls_console_interaction_ask_password_async;
155 interaction_class->ask_password_finish = g_tls_console_interaction_ask_password_finish;
156}
157
158GTlsInteraction *
159g_tls_console_interaction_new (void)
160{
161 return g_object_new (G_TYPE_TLS_CONSOLE_INTERACTION, NULL);
162}
163

source code of gtk/subprojects/glib/gio/tests/gtlsconsoleinteraction.c