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 <gnutls/gnutls.h>
46 #include <extensions/extensions.h>
50 static gboolean use_timer = TRUE;
51 static guint timeout_id = 0;
52 static guint num_windows = 0;
55 timeout_cb (gpointer p)
57 DEBUG ("Timeout reached; exiting...");
72 DEBUG ("Start timer");
74 timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
85 g_source_remove (timeout_id);
90 tls_dialog_response_cb (GtkDialog *dialog,
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);
101 DEBUG ("Response %d", response_id);
103 g_object_get (tls_dialog,
104 "certificate", &certificate,
106 "remember", &remember,
110 gtk_widget_destroy (GTK_WIDGET (dialog));
112 if (response_id == GTK_RESPONSE_YES)
114 empathy_tls_certificate_accept_async (certificate, NULL, NULL);
118 tp_asv_set_boolean (details, "user-requested", TRUE);
119 empathy_tls_certificate_reject_async (certificate, reason, details,
124 empathy_tls_verifier_store_exception (verifier);
126 g_object_unref (certificate);
127 g_hash_table_unref (details);
129 /* restart the timeout */
139 display_interactive_dialog (EmpathyTLSCertificate *certificate,
140 EmpathyTLSVerifier *verifier,
141 EmpTLSCertificateRejectReason reason,
144 GtkWidget *tls_dialog;
146 /* stop the timeout */
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);
155 gtk_widget_show (tls_dialog);
159 verifier_verify_cb (GObject *source,
160 GAsyncResult *result,
163 EmpTLSCertificateRejectReason reason;
164 GError *error = NULL;
165 EmpathyTLSCertificate *certificate = NULL;
166 GHashTable *details = NULL;
167 gchar *hostname = NULL;
169 g_object_get (source,
170 "certificate", &certificate,
173 empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
174 result, &reason, &details, &error);
178 DEBUG ("Error: %s", error->message);
179 display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
182 g_error_free (error);
186 empathy_tls_certificate_accept_async (certificate, NULL, NULL);
190 g_object_unref (certificate);
194 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
195 EmpathyServerTLSHandler *handler,
198 EmpathyTLSCertificate *certificate = NULL;
199 gchar *hostname = NULL;
200 gchar **reference_identities = NULL;
201 EmpathyTLSVerifier *verifier;
203 DEBUG ("New TLS server handler received from the factory");
205 g_object_get (handler,
206 "certificate", &certificate,
207 "hostname", &hostname,
208 "reference-identities", &reference_identities,
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);
216 g_object_unref (verifier);
217 g_object_unref (certificate);
219 g_strfreev (reference_identities);
223 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
224 EmpathyServerSASLHandler *handler,
229 DEBUG ("New SASL server handler received from the factory");
231 /* If the handler has the password it will deal with it itself. */
232 if (!empathy_server_sasl_handler_has_password (handler))
234 dialog = empathy_password_dialog_new (handler);
235 gtk_widget_show (dialog);
240 retry_account_cb (GtkWidget *dialog,
242 const gchar *password,
243 EmpathyAuthFactory *factory)
245 DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account));
247 empathy_auth_factory_save_retry_password (factory, account, password);
249 tp_account_reconnect_async (account, NULL, NULL);
253 auth_factory_auth_passsword_failed (EmpathyAuthFactory *factory,
255 const gchar *password,
260 DEBUG ("Authentification on %s failed, popup password dialog",
261 tp_account_get_path_suffix (account));
263 dialog = empathy_bad_password_dialog_new (account, password);
265 tp_g_signal_connect_object (dialog, "retry",
266 G_CALLBACK (retry_account_cb), factory, 0);
268 gtk_widget_show (dialog);
275 GOptionContext *context;
276 GError *error = NULL;
277 EmpathyAuthFactory *factory;
278 TpDebugSender *debug_sender;
279 TpSimpleClientFactory *tp_factory;
282 g_thread_init (NULL);
284 context = g_option_context_new (N_(" - Empathy authentication client"));
285 g_option_context_add_group (context, gtk_get_option_group (TRUE));
286 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
288 if (!g_option_context_parse (context, &argc, &argv, &error))
290 g_print ("%s\nRun '%s --help' to see a full list of available command "
291 "line options.\n", error->message, argv[0]);
292 g_warning ("Error in empathy-auth-client init: %s", error->message);
296 g_option_context_free (context);
299 gnutls_global_init ();
300 g_set_application_name (_("Empathy authentication client"));
302 /* Make empathy and empathy-auth-client appear as the same app in
304 gdk_set_program_class ("Empathy");
305 gtk_window_set_default_icon_name ("empathy");
306 textdomain (GETTEXT_PACKAGE);
309 /* Set up debug sender */
310 debug_sender = tp_debug_sender_dup ();
311 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
314 dbus = tp_dbus_daemon_dup (NULL);
315 tp_factory = tp_simple_client_factory_new (dbus);
316 tp_simple_client_factory_add_account_features_varargs (tp_factory,
317 TP_ACCOUNT_FEATURE_STORAGE,
320 factory = empathy_auth_factory_new (tp_factory);
321 g_object_unref (tp_factory);
322 g_object_unref (dbus);
324 g_signal_connect (factory, "new-server-tls-handler",
325 G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
327 g_signal_connect (factory, "new-server-sasl-handler",
328 G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
330 g_signal_connect (factory, "auth-password-failed",
331 G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
333 if (!empathy_auth_factory_register (factory, &error))
335 g_critical ("Failed to register the auth factory: %s\n", error->message);
336 g_error_free (error);
337 g_object_unref (factory);
342 DEBUG ("Empathy auth client started.");
344 if (g_getenv ("EMPATHY_PERSIST") != NULL)
346 DEBUG ("Timed-exit disabled");
355 g_object_unref (factory);
356 g_object_unref (debug_sender);