]> git.0d.be Git - empathy.git/blob - src/empathy-auth-client.c
Implement a timeout machinery for the auth client
[empathy.git] / src / empathy-auth-client.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
30 #include <libempathy/empathy-debug.h>
31 #include <libempathy/empathy-auth-factory.h>
32 #include <libempathy/empathy-server-tls-handler.h>
33 #include <libempathy/empathy-tls-verifier.h>
34
35 #include <libempathy-gtk/empathy-tls-dialog.h>
36 #include <libempathy-gtk/empathy-ui-utils.h>
37
38 #include <gnutls/gnutls.h>
39
40 #include <extensions/extensions.h>
41
42 #define TIMEOUT 60
43
44 static gboolean use_timer = TRUE;
45 static guint timeout_id = 0;
46 static guint num_windows = 0;
47
48 static gboolean
49 timeout_cb (gpointer p)
50 {
51   DEBUG ("Timeout reached; exiting...");
52
53   gtk_main_quit ();
54   return FALSE;
55 }
56
57 static void
58 start_timer (void)
59 {
60   if (!use_timer)
61     return;
62
63   if (timeout_id != 0)
64     return;
65
66   DEBUG ("Start timer");
67
68   timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
69 }
70
71 static void
72 stop_timer (void)
73 {
74   if (timeout_id == 0)
75     return;
76
77   DEBUG ("Stop timer");
78
79   g_source_remove (timeout_id);
80   timeout_id = 0;
81 }
82
83 static void
84 tls_dialog_response_cb (GtkDialog *dialog,
85     gint response_id,
86     gpointer user_data)
87 {
88   EmpathyTLSCertificate *certificate = NULL;
89   EmpTLSCertificateRejectReason reason = 0;
90   EmpathyTLSDialog *tls_dialog = EMPATHY_TLS_DIALOG (dialog);
91   gboolean remember = FALSE;
92
93   DEBUG ("Response %d", response_id);
94
95   g_object_get (tls_dialog,
96       "certificate", &certificate,
97       "reason", &reason,
98       "remember", &remember,
99       NULL);
100
101   gtk_widget_destroy (GTK_WIDGET (dialog));
102
103   if (response_id == GTK_RESPONSE_YES)
104     empathy_tls_certificate_accept_async (certificate, NULL, NULL);
105   else
106     empathy_tls_certificate_reject_async (certificate, reason, TRUE,
107         NULL, NULL);
108
109   if (remember)
110     empathy_tls_certificate_store_ca (certificate);
111
112   g_object_unref (certificate);
113
114   /* restart the timeout */
115   num_windows--;
116
117   if (num_windows > 0)
118     return;
119
120   start_timer ();
121 }
122
123 static void
124 display_interactive_dialog (EmpathyTLSCertificate *certificate,
125     EmpTLSCertificateRejectReason reason,
126     GHashTable *details)
127 {
128   GtkWidget *tls_dialog;
129
130   /* stop the timeout */
131   num_windows++;
132   stop_timer ();
133
134   tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
135   g_signal_connect (tls_dialog, "response",
136       G_CALLBACK (tls_dialog_response_cb), NULL);
137
138   gtk_widget_show (tls_dialog);
139 }
140
141 static void
142 verifier_verify_cb (GObject *source,
143     GAsyncResult *result,
144     gpointer user_data)
145 {
146   gboolean res;
147   EmpTLSCertificateRejectReason reason;
148   GError *error = NULL;
149   EmpathyTLSCertificate *certificate = NULL;
150   GHashTable *details = NULL;
151
152   g_object_get (source,
153       "certificate", &certificate,
154       NULL);
155
156   res = empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
157       result, &reason, &details, &error);
158
159   if (error != NULL)
160     {
161       DEBUG ("Error: %s", error->message);
162       display_interactive_dialog (certificate, reason, details);
163
164       g_error_free (error);
165     }
166   else
167     {
168       empathy_tls_certificate_accept_async (certificate, NULL, NULL);
169     }
170
171   g_object_unref (certificate);
172 }
173
174 static void
175 auth_factory_new_handler_cb (EmpathyAuthFactory *factory,
176     EmpathyServerTLSHandler *handler,
177     gpointer user_data)
178 {
179   EmpathyTLSCertificate *certificate = NULL;
180   gchar *hostname = NULL;
181   EmpathyTLSVerifier *verifier;
182
183   DEBUG ("New TLS server handler received from the factory");
184
185   g_object_get (handler,
186       "certificate", &certificate,
187       "hostname", &hostname,
188       NULL);
189
190   verifier = empathy_tls_verifier_new (certificate, hostname);
191   empathy_tls_verifier_verify_async (verifier,
192       verifier_verify_cb, NULL);
193
194   g_object_unref (verifier);
195   g_object_unref (certificate);
196   g_free (hostname);
197 }
198
199 int
200 main (int argc,
201     char **argv)
202 {
203   GOptionContext *context;
204   GError *error = NULL;
205   EmpathyAuthFactory *factory;
206
207   g_thread_init (NULL);
208
209   context = g_option_context_new (N_(" - Empathy authentication client"));
210   g_option_context_add_group (context, gtk_get_option_group (TRUE));
211   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
212
213   if (!g_option_context_parse (context, &argc, &argv, &error))
214     {
215       g_print ("%s\nRun '%s --help' to see a full list of available command "
216           "line options.\n", error->message, argv[0]);
217       g_warning ("Error in empathy-auth-client init: %s", error->message);
218       return EXIT_FAILURE;
219     }
220
221   g_option_context_free (context);
222
223   empathy_gtk_init ();
224   gnutls_global_init ();
225   g_set_application_name (_("Empathy authentication client"));
226
227   gtk_window_set_default_icon_name ("empathy");
228   textdomain (GETTEXT_PACKAGE);
229
230   factory = empathy_auth_factory_dup_singleton ();
231
232   g_signal_connect (factory, "new-server-tls-handler",
233       G_CALLBACK (auth_factory_new_handler_cb), NULL);
234
235   if (!empathy_auth_factory_register (factory, &error))
236     {
237       g_critical ("Failed to register the auth factory: %s\n", error->message);
238       g_error_free (error);
239       g_object_unref (factory);
240
241       return EXIT_FAILURE;
242     }
243
244   DEBUG ("Empathy auth client started.");
245
246   if (g_getenv ("EMPATHY_PERSIST") != NULL)
247     {
248       DEBUG ("Timed-exit disabled");
249
250       use_timer = FALSE;
251     }
252
253   start_timer ();
254
255   gtk_main ();
256
257   g_object_unref (factory);
258
259   return EXIT_SUCCESS;
260 }