]> git.0d.be Git - empathy.git/blob - src/empathy-auth-client.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[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 <glib/gi18n.h>
25
26 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
27 #include <libempathy/empathy-debug.h>
28 #include <libempathy/empathy-auth-factory.h>
29 #include <libempathy/empathy-server-tls-handler.h>
30 #include <libempathy/empathy-tls-verifier.h>
31
32 #include <libempathy-gtk/empathy-bad-password-dialog.h>
33 #include <libempathy-gtk/empathy-password-dialog.h>
34 #include <libempathy-gtk/empathy-tls-dialog.h>
35 #include <libempathy-gtk/empathy-ui-utils.h>
36
37 #include "empathy-sanity-cleaning.h"
38
39 #include <gnutls/gnutls.h>
40
41 #define TIMEOUT 60
42
43 static gboolean use_timer = TRUE;
44 static guint timeout_id = 0;
45 static guint num_windows = 0;
46
47 static gboolean
48 timeout_cb (gpointer p)
49 {
50   DEBUG ("Timeout reached; exiting...");
51
52   gtk_main_quit ();
53   return FALSE;
54 }
55
56 static void
57 start_timer (void)
58 {
59   if (!use_timer)
60     return;
61
62   if (timeout_id != 0)
63     return;
64
65   DEBUG ("Start timer");
66
67   timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
68 }
69
70 static void
71 stop_timer (void)
72 {
73   if (timeout_id == 0)
74     return;
75
76   DEBUG ("Stop timer");
77
78   g_source_remove (timeout_id);
79   timeout_id = 0;
80 }
81
82 static void
83 tls_dialog_response_cb (GtkDialog *dialog,
84     gint response_id,
85     gpointer user_data)
86 {
87   TpTLSCertificate *certificate = NULL;
88   TpTLSCertificateRejectReason reason = 0;
89   GHashTable *details = NULL;
90   EmpathyTLSDialog *tls_dialog = EMPATHY_TLS_DIALOG (dialog);
91   gboolean remember = FALSE;
92   EmpathyTLSVerifier *verifier = EMPATHY_TLS_VERIFIER (user_data);
93
94   g_object_get (tls_dialog,
95       "certificate", &certificate,
96       "reason", &reason,
97       "remember", &remember,
98       "details", &details,
99       NULL);
100
101   DEBUG ("Response %d (remember: %d)", response_id, remember);
102
103   gtk_widget_destroy (GTK_WIDGET (dialog));
104
105   if (response_id == GTK_RESPONSE_YES)
106     {
107       tp_tls_certificate_accept_async (certificate, NULL, NULL);
108     }
109   else
110     {
111       tp_asv_set_boolean (details, "user-requested", TRUE);
112       tp_tls_certificate_add_rejection (certificate, reason, NULL,
113           g_variant_new_parsed ("{ 'user-requested': <%b> }", TRUE));
114
115       tp_tls_certificate_reject_async (certificate, NULL, NULL);
116     }
117
118   if (remember)
119     empathy_tls_verifier_store_exception (verifier);
120
121   g_object_unref (certificate);
122   g_hash_table_unref (details);
123
124   /* restart the timeout */
125   num_windows--;
126
127   if (num_windows > 0)
128     return;
129
130   start_timer ();
131 }
132
133 static void
134 display_interactive_dialog (TpTLSCertificate *certificate,
135     EmpathyTLSVerifier *verifier,
136     TpTLSCertificateRejectReason reason,
137     GHashTable *details)
138 {
139   GtkWidget *tls_dialog;
140
141   /* stop the timeout */
142   num_windows++;
143   stop_timer ();
144
145   tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
146   g_signal_connect_data (tls_dialog, "response",
147       G_CALLBACK (tls_dialog_response_cb), g_object_ref (verifier),
148       (GClosureNotify)g_object_unref, 0);
149
150   gtk_widget_show (tls_dialog);
151 }
152
153 static void
154 verifier_verify_cb (GObject *source,
155     GAsyncResult *result,
156     gpointer user_data)
157 {
158   TpTLSCertificateRejectReason reason;
159   GError *error = NULL;
160   TpTLSCertificate *certificate = NULL;
161   GHashTable *details = NULL;
162   gchar *hostname = NULL;
163
164   g_object_get (source,
165       "certificate", &certificate,
166       NULL);
167
168   empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
169       result, &reason, &details, &error);
170
171   if (error != NULL)
172     {
173       DEBUG ("Error: %s", error->message);
174       display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
175               reason, details);
176
177       g_error_free (error);
178     }
179   else
180     {
181       tp_tls_certificate_accept_async (certificate, NULL, NULL);
182     }
183
184   g_free (hostname);
185   g_object_unref (certificate);
186 }
187
188 static void
189 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
190     EmpathyServerTLSHandler *handler,
191     gpointer user_data)
192 {
193   TpTLSCertificate *certificate = NULL;
194   gchar *hostname = NULL;
195   gchar **reference_identities = NULL;
196   EmpathyTLSVerifier *verifier;
197
198   DEBUG ("New TLS server handler received from the factory");
199
200   g_object_get (handler,
201       "certificate", &certificate,
202       "hostname", &hostname,
203       "reference-identities", &reference_identities,
204       NULL);
205
206   verifier = empathy_tls_verifier_new (certificate, hostname,
207       (const gchar **) reference_identities);
208   empathy_tls_verifier_verify_async (verifier,
209       verifier_verify_cb, NULL);
210
211   g_object_unref (verifier);
212   g_object_unref (certificate);
213   g_free (hostname);
214   g_strfreev (reference_identities);
215 }
216
217 static void
218 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
219     EmpathyServerSASLHandler *handler,
220     gpointer user_data)
221 {
222   GtkWidget *dialog;
223
224   DEBUG ("New SASL server handler received from the factory");
225
226   /* If the handler has the password it will deal with it itself. */
227   if (!empathy_server_sasl_handler_has_password (handler))
228     {
229       DEBUG ("SASL handler doesn't have a password, prompt for one");
230
231       dialog = empathy_password_dialog_new (handler);
232       gtk_widget_show (dialog);
233     }
234 }
235
236 static void
237 retry_account_cb (GtkWidget *dialog,
238     TpAccount *account,
239     const gchar *password,
240     EmpathyAuthFactory *factory)
241 {
242   DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account));
243
244   empathy_auth_factory_save_retry_password (factory, account, password);
245
246   tp_account_reconnect_async (account, NULL, NULL);
247 }
248
249 static void
250 auth_factory_auth_passsword_failed (EmpathyAuthFactory *factory,
251     TpAccount *account,
252     const gchar *password,
253     gpointer user_data)
254 {
255   GtkWidget *dialog;
256
257   DEBUG ("Authentication on %s failed, popup password dialog",
258       tp_account_get_path_suffix (account));
259
260   dialog = empathy_bad_password_dialog_new (account, password);
261
262   tp_g_signal_connect_object (dialog, "retry",
263       G_CALLBACK (retry_account_cb), factory, 0);
264
265   gtk_widget_show (dialog);
266 }
267
268 static void
269 sanity_cb (GObject *source,
270     GAsyncResult *result,
271     gpointer user_data)
272 {
273   start_timer ();
274 }
275
276 int
277 main (int argc,
278     char **argv)
279 {
280   GOptionContext *context;
281   GError *error = NULL;
282   EmpathyAuthFactory *factory;
283   TpDebugSender *debug_sender;
284   TpSimpleClientFactory *tp_factory;
285   TpDBusDaemon *dbus;
286
287   context = g_option_context_new (N_(" - Empathy authentication client"));
288   g_option_context_add_group (context, gtk_get_option_group (TRUE));
289   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
290
291   if (!g_option_context_parse (context, &argc, &argv, &error))
292     {
293       g_print ("%s\nRun '%s --help' to see a full list of available command "
294           "line options.\n", error->message, argv[0]);
295       g_warning ("Error in empathy-auth-client init: %s", error->message);
296       return EXIT_FAILURE;
297     }
298
299   g_option_context_free (context);
300
301   empathy_gtk_init ();
302   gnutls_global_init ();
303   g_set_application_name (_("Empathy authentication client"));
304
305   /* Make empathy and empathy-auth-client appear as the same app in
306    * gnome-shell */
307   gdk_set_program_class ("Empathy");
308   gtk_window_set_default_icon_name ("empathy");
309   textdomain (GETTEXT_PACKAGE);
310
311   /* There is no 'main' UI window so just use the default GdkScreen */
312   empathy_set_css_provider (NULL);
313
314 #ifdef ENABLE_DEBUG
315   /* Set up debug sender */
316   debug_sender = tp_debug_sender_dup ();
317   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
318 #endif
319
320   dbus = tp_dbus_daemon_dup (NULL);
321   tp_factory = tp_simple_client_factory_new (dbus);
322   tp_simple_client_factory_add_account_features_varargs (tp_factory,
323       TP_ACCOUNT_FEATURE_STORAGE,
324       0);
325
326   factory = empathy_auth_factory_new (tp_factory);
327   g_object_unref (tp_factory);
328   g_object_unref (dbus);
329
330   g_signal_connect (factory, "new-server-tls-handler",
331       G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
332
333   g_signal_connect (factory, "new-server-sasl-handler",
334       G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
335
336   g_signal_connect (factory, "auth-password-failed",
337       G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
338
339   if (!empathy_auth_factory_register (factory, &error))
340     {
341       g_critical ("Failed to register the auth factory: %s\n", error->message);
342       g_error_free (error);
343       g_object_unref (factory);
344
345       return EXIT_FAILURE;
346     }
347
348   DEBUG ("Empathy auth client started.");
349
350   if (g_getenv ("EMPATHY_PERSIST") != NULL)
351     {
352       DEBUG ("Timed-exit disabled");
353
354       use_timer = FALSE;
355     }
356
357   /* Wait for the migration code to be done before starting the timer */
358   empathy_sanity_checking_run_async (sanity_cb, NULL);
359
360   gtk_main ();
361
362   g_object_unref (factory);
363   g_object_unref (debug_sender);
364
365   return EXIT_SUCCESS;
366 }