]> git.0d.be Git - empathy.git/blob - src/empathy-auth-client.c
Authentification → Authentication
[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/debug-sender.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   DEBUG ("Response %d", response_id);
104
105   g_object_get (tls_dialog,
106       "certificate", &certificate,
107       "reason", &reason,
108       "remember", &remember,
109       "details", &details,
110       NULL);
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 int
278 main (int argc,
279     char **argv)
280 {
281   GOptionContext *context;
282   GError *error = NULL;
283   EmpathyAuthFactory *factory;
284   TpDebugSender *debug_sender;
285   TpSimpleClientFactory *tp_factory;
286   TpDBusDaemon *dbus;
287
288   context = g_option_context_new (N_(" - Empathy authentication client"));
289   g_option_context_add_group (context, gtk_get_option_group (TRUE));
290   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
291
292   if (!g_option_context_parse (context, &argc, &argv, &error))
293     {
294       g_print ("%s\nRun '%s --help' to see a full list of available command "
295           "line options.\n", error->message, argv[0]);
296       g_warning ("Error in empathy-auth-client init: %s", error->message);
297       return EXIT_FAILURE;
298     }
299
300   g_option_context_free (context);
301
302   empathy_gtk_init ();
303   gnutls_global_init ();
304   g_set_application_name (_("Empathy authentication client"));
305
306   /* Make empathy and empathy-auth-client appear as the same app in
307    * gnome-shell */
308   gdk_set_program_class ("Empathy");
309   gtk_window_set_default_icon_name ("empathy");
310   textdomain (GETTEXT_PACKAGE);
311
312 #ifdef ENABLE_DEBUG
313   /* Set up debug sender */
314   debug_sender = tp_debug_sender_dup ();
315   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
316 #endif
317
318   dbus = tp_dbus_daemon_dup (NULL);
319   tp_factory = tp_simple_client_factory_new (dbus);
320   tp_simple_client_factory_add_account_features_varargs (tp_factory,
321       TP_ACCOUNT_FEATURE_STORAGE,
322       0);
323
324   factory = empathy_auth_factory_new (tp_factory);
325   g_object_unref (tp_factory);
326   g_object_unref (dbus);
327
328   g_signal_connect (factory, "new-server-tls-handler",
329       G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
330
331   g_signal_connect (factory, "new-server-sasl-handler",
332       G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
333
334   g_signal_connect (factory, "auth-password-failed",
335       G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
336
337   if (!empathy_auth_factory_register (factory, &error))
338     {
339       g_critical ("Failed to register the auth factory: %s\n", error->message);
340       g_error_free (error);
341       g_object_unref (factory);
342
343       return EXIT_FAILURE;
344     }
345
346   DEBUG ("Empathy auth client started.");
347
348   if (g_getenv ("EMPATHY_PERSIST") != NULL)
349     {
350       DEBUG ("Timed-exit disabled");
351
352       use_timer = FALSE;
353     }
354
355   start_timer ();
356   empathy_sanity_checking_run_if_needed ();
357
358   gtk_main ();
359
360   g_object_unref (factory);
361   g_object_unref (debug_sender);
362
363   return EXIT_SUCCESS;
364 }