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 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);
103 g_object_get (tls_dialog,
104 "certificate", &certificate,
106 "remember", &remember,
110 DEBUG ("Response %d (remember: %d)", response_id, remember);
112 gtk_widget_destroy (GTK_WIDGET (dialog));
114 if (response_id == GTK_RESPONSE_YES)
116 tp_tls_certificate_accept_async (certificate, NULL, NULL);
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));
124 tp_tls_certificate_reject_async (certificate, NULL, NULL);
128 empathy_tls_verifier_store_exception (verifier);
130 g_object_unref (certificate);
131 g_hash_table_unref (details);
133 /* restart the timeout */
143 display_interactive_dialog (TpTLSCertificate *certificate,
144 EmpathyTLSVerifier *verifier,
145 TpTLSCertificateRejectReason reason,
148 GtkWidget *tls_dialog;
150 /* stop the timeout */
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);
159 gtk_widget_show (tls_dialog);
163 verifier_verify_cb (GObject *source,
164 GAsyncResult *result,
167 TpTLSCertificateRejectReason reason;
168 GError *error = NULL;
169 TpTLSCertificate *certificate = NULL;
170 GHashTable *details = NULL;
171 gchar *hostname = NULL;
173 g_object_get (source,
174 "certificate", &certificate,
177 empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
178 result, &reason, &details, &error);
182 DEBUG ("Error: %s", error->message);
183 display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
186 g_error_free (error);
190 tp_tls_certificate_accept_async (certificate, NULL, NULL);
194 g_object_unref (certificate);
198 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
199 EmpathyServerTLSHandler *handler,
202 TpTLSCertificate *certificate = NULL;
203 gchar *hostname = NULL;
204 gchar **reference_identities = NULL;
205 EmpathyTLSVerifier *verifier;
207 DEBUG ("New TLS server handler received from the factory");
209 g_object_get (handler,
210 "certificate", &certificate,
211 "hostname", &hostname,
212 "reference-identities", &reference_identities,
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);
220 g_object_unref (verifier);
221 g_object_unref (certificate);
223 g_strfreev (reference_identities);
227 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
228 EmpathyServerSASLHandler *handler,
233 DEBUG ("New SASL server handler received from the factory");
235 /* If the handler has the password it will deal with it itself. */
236 if (!empathy_server_sasl_handler_has_password (handler))
238 DEBUG ("SASL handler doesn't have a password, prompt for one");
240 dialog = empathy_password_dialog_new (handler);
241 gtk_widget_show (dialog);
246 retry_account_cb (GtkWidget *dialog,
248 const gchar *password,
249 EmpathyAuthFactory *factory)
251 DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account));
253 empathy_auth_factory_save_retry_password (factory, account, password);
255 tp_account_reconnect_async (account, NULL, NULL);
259 auth_factory_auth_passsword_failed (EmpathyAuthFactory *factory,
261 const gchar *password,
266 DEBUG ("Authentication on %s failed, popup password dialog",
267 tp_account_get_path_suffix (account));
269 dialog = empathy_bad_password_dialog_new (account, password);
271 tp_g_signal_connect_object (dialog, "retry",
272 G_CALLBACK (retry_account_cb), factory, 0);
274 gtk_widget_show (dialog);
281 GOptionContext *context;
282 GError *error = NULL;
283 EmpathyAuthFactory *factory;
284 TpDebugSender *debug_sender;
285 TpSimpleClientFactory *tp_factory;
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);
312 /* There is no 'main' UI window so just use the default GdkScreen */
313 empathy_set_css_provider (NULL);
316 /* Set up debug sender */
317 debug_sender = tp_debug_sender_dup ();
318 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
321 dbus = tp_dbus_daemon_dup (NULL);
322 tp_factory = tp_simple_client_factory_new (dbus);
323 tp_simple_client_factory_add_account_features_varargs (tp_factory,
324 TP_ACCOUNT_FEATURE_STORAGE,
327 factory = empathy_auth_factory_new (tp_factory);
328 g_object_unref (tp_factory);
329 g_object_unref (dbus);
331 g_signal_connect (factory, "new-server-tls-handler",
332 G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
334 g_signal_connect (factory, "new-server-sasl-handler",
335 G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
337 g_signal_connect (factory, "auth-password-failed",
338 G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
340 if (!empathy_auth_factory_register (factory, &error))
342 g_critical ("Failed to register the auth factory: %s\n", error->message);
343 g_error_free (error);
344 g_object_unref (factory);
349 DEBUG ("Empathy auth client started.");
351 if (g_getenv ("EMPATHY_PERSIST") != NULL)
353 DEBUG ("Timed-exit disabled");
359 empathy_sanity_checking_run_if_needed ();
363 g_object_unref (factory);
364 g_object_unref (debug_sender);