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