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