2 * Copyright (C) 2010 Collabora Ltd.
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.
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.
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
19 * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
26 #include <glib/gi18n.h>
29 #include <telepathy-glib/debug-sender.h>
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>
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>
44 #include "empathy-sanity-cleaning.h"
46 #include <gnutls/gnutls.h>
48 #include <extensions/extensions.h>
52 static gboolean use_timer = TRUE;
53 static guint timeout_id = 0;
54 static guint num_windows = 0;
57 timeout_cb (gpointer p)
59 DEBUG ("Timeout reached; exiting...");
74 DEBUG ("Start timer");
76 timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
87 g_source_remove (timeout_id);
92 tls_dialog_response_cb (GtkDialog *dialog,
96 EmpathyTLSCertificate *certificate = NULL;
97 EmpTLSCertificateRejectReason 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);
103 DEBUG ("Response %d", response_id);
105 g_object_get (tls_dialog,
106 "certificate", &certificate,
108 "remember", &remember,
112 gtk_widget_destroy (GTK_WIDGET (dialog));
114 if (response_id == GTK_RESPONSE_YES)
116 empathy_tls_certificate_accept_async (certificate, NULL, NULL);
120 tp_asv_set_boolean (details, "user-requested", TRUE);
121 empathy_tls_certificate_reject_async (certificate, reason, details,
126 empathy_tls_verifier_store_exception (verifier);
128 g_object_unref (certificate);
129 g_hash_table_unref (details);
131 /* restart the timeout */
141 display_interactive_dialog (EmpathyTLSCertificate *certificate,
142 EmpathyTLSVerifier *verifier,
143 EmpTLSCertificateRejectReason reason,
146 GtkWidget *tls_dialog;
148 /* stop the timeout */
152 tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
153 g_signal_connect_data (tls_dialog, "response",
154 G_CALLBACK (tls_dialog_response_cb), g_object_ref (verifier),
155 (GClosureNotify)g_object_unref, 0);
157 gtk_widget_show (tls_dialog);
161 verifier_verify_cb (GObject *source,
162 GAsyncResult *result,
165 EmpTLSCertificateRejectReason reason;
166 GError *error = NULL;
167 EmpathyTLSCertificate *certificate = NULL;
168 GHashTable *details = NULL;
169 gchar *hostname = NULL;
171 g_object_get (source,
172 "certificate", &certificate,
175 empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
176 result, &reason, &details, &error);
180 DEBUG ("Error: %s", error->message);
181 display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
184 g_error_free (error);
188 empathy_tls_certificate_accept_async (certificate, NULL, NULL);
192 g_object_unref (certificate);
196 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
197 EmpathyServerTLSHandler *handler,
200 EmpathyTLSCertificate *certificate = NULL;
201 gchar *hostname = NULL;
202 gchar **reference_identities = NULL;
203 EmpathyTLSVerifier *verifier;
205 DEBUG ("New TLS server handler received from the factory");
207 g_object_get (handler,
208 "certificate", &certificate,
209 "hostname", &hostname,
210 "reference-identities", &reference_identities,
213 verifier = empathy_tls_verifier_new (certificate, hostname,
214 (const gchar **) reference_identities);
215 empathy_tls_verifier_verify_async (verifier,
216 verifier_verify_cb, NULL);
218 g_object_unref (verifier);
219 g_object_unref (certificate);
221 g_strfreev (reference_identities);
225 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
226 EmpathyServerSASLHandler *handler,
231 DEBUG ("New SASL server handler received from the factory");
233 /* If the handler has the password it will deal with it itself. */
234 if (!empathy_server_sasl_handler_has_password (handler))
236 DEBUG ("SASL handler doesn't have a password, prompt for one");
238 dialog = empathy_password_dialog_new (handler);
239 gtk_widget_show (dialog);
244 retry_account_cb (GtkWidget *dialog,
246 const gchar *password,
247 EmpathyAuthFactory *factory)
249 DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account));
251 empathy_auth_factory_save_retry_password (factory, account, password);
253 tp_account_reconnect_async (account, NULL, NULL);
257 auth_factory_auth_passsword_failed (EmpathyAuthFactory *factory,
259 const gchar *password,
264 DEBUG ("Authentification on %s failed, popup password dialog",
265 tp_account_get_path_suffix (account));
267 dialog = empathy_bad_password_dialog_new (account, password);
269 tp_g_signal_connect_object (dialog, "retry",
270 G_CALLBACK (retry_account_cb), factory, 0);
272 gtk_widget_show (dialog);
279 GOptionContext *context;
280 GError *error = NULL;
281 EmpathyAuthFactory *factory;
282 TpDebugSender *debug_sender;
283 TpSimpleClientFactory *tp_factory;
286 g_thread_init (NULL);
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);
292 if (!g_option_context_parse (context, &argc, &argv, &error))
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);
300 g_option_context_free (context);
303 gnutls_global_init ();
304 g_set_application_name (_("Empathy authentication client"));
306 /* Make empathy and empathy-auth-client appear as the same app in
308 gdk_set_program_class ("Empathy");
309 gtk_window_set_default_icon_name ("empathy");
310 textdomain (GETTEXT_PACKAGE);
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);
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,
324 factory = empathy_auth_factory_new (tp_factory);
325 g_object_unref (tp_factory);
326 g_object_unref (dbus);
328 g_signal_connect (factory, "new-server-tls-handler",
329 G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
331 g_signal_connect (factory, "new-server-sasl-handler",
332 G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
334 g_signal_connect (factory, "auth-password-failed",
335 G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
337 if (!empathy_auth_factory_register (factory, &error))
339 g_critical ("Failed to register the auth factory: %s\n", error->message);
340 g_error_free (error);
341 g_object_unref (factory);
346 DEBUG ("Empathy auth client started.");
348 if (g_getenv ("EMPATHY_PERSIST") != NULL)
350 DEBUG ("Timed-exit disabled");
356 empathy_sanity_checking_run_if_needed ();
360 g_object_unref (factory);
361 g_object_unref (debug_sender);