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 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
30 #include <libempathy/empathy-debug.h>
31 #include <libempathy/empathy-auth-factory.h>
32 #include <libempathy/empathy-server-sasl-handler.h>
33 #include <libempathy/empathy-server-tls-handler.h>
34 #include <libempathy/empathy-tls-verifier.h>
35 #include <libempathy/empathy-utils.h>
37 #include <libempathy-gtk/empathy-password-dialog.h>
38 #include <libempathy-gtk/empathy-tls-dialog.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include <gnutls/gnutls.h>
43 #include <extensions/extensions.h>
47 static gboolean use_timer = TRUE;
48 static guint timeout_id = 0;
49 static guint num_windows = 0;
52 timeout_cb (gpointer p)
54 DEBUG ("Timeout reached; exiting...");
69 DEBUG ("Start timer");
71 timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
82 g_source_remove (timeout_id);
87 tls_dialog_response_cb (GtkDialog *dialog,
91 EmpathyTLSCertificate *certificate = NULL;
92 EmpTLSCertificateRejectReason reason = 0;
93 GHashTable *details = NULL;
94 EmpathyTLSDialog *tls_dialog = EMPATHY_TLS_DIALOG (dialog);
95 gboolean remember = FALSE;
96 EmpathyTLSVerifier *verifier = EMPATHY_TLS_VERIFIER (user_data);
98 DEBUG ("Response %d", response_id);
100 g_object_get (tls_dialog,
101 "certificate", &certificate,
103 "remember", &remember,
107 gtk_widget_destroy (GTK_WIDGET (dialog));
109 if (response_id == GTK_RESPONSE_YES)
111 empathy_tls_certificate_accept_async (certificate, NULL, NULL);
115 tp_asv_set_boolean (details, "user-requested", TRUE);
116 empathy_tls_certificate_reject_async (certificate, reason, details,
121 empathy_tls_verifier_store_exception (verifier);
123 g_object_unref (certificate);
124 g_hash_table_unref (details);
126 /* restart the timeout */
136 display_interactive_dialog (EmpathyTLSCertificate *certificate,
137 EmpathyTLSVerifier *verifier,
138 EmpTLSCertificateRejectReason reason,
141 GtkWidget *tls_dialog;
143 /* stop the timeout */
147 tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
148 g_signal_connect_data (tls_dialog, "response",
149 G_CALLBACK (tls_dialog_response_cb), g_object_ref (verifier),
150 (GClosureNotify)g_object_unref, 0);
152 gtk_widget_show (tls_dialog);
156 verifier_verify_cb (GObject *source,
157 GAsyncResult *result,
161 EmpTLSCertificateRejectReason reason;
162 GError *error = NULL;
163 EmpathyTLSCertificate *certificate = NULL;
164 GHashTable *details = NULL;
165 gchar *hostname = NULL;
167 g_object_get (source,
168 "certificate", &certificate,
171 res = empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
172 result, &reason, &details, &error);
176 DEBUG ("Error: %s", error->message);
177 display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
180 g_error_free (error);
184 empathy_tls_certificate_accept_async (certificate, NULL, NULL);
188 g_object_unref (certificate);
192 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
193 EmpathyServerTLSHandler *handler,
196 EmpathyTLSCertificate *certificate = NULL;
197 gchar *hostname = NULL;
198 EmpathyTLSVerifier *verifier;
200 DEBUG ("New TLS server handler received from the factory");
202 g_object_get (handler,
203 "certificate", &certificate,
204 "hostname", &hostname,
207 verifier = empathy_tls_verifier_new (certificate, hostname);
208 empathy_tls_verifier_verify_async (verifier,
209 verifier_verify_cb, NULL);
211 g_object_unref (verifier);
212 g_object_unref (certificate);
217 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
218 EmpathyServerSASLHandler *handler,
223 DEBUG ("New SASL server handler received from the factory");
225 /* If the handler has the password it will deal with it itself. */
226 if (!empathy_server_sasl_handler_has_password (handler))
228 dialog = empathy_password_dialog_new (handler);
229 gtk_widget_show (dialog);
237 GOptionContext *context;
238 GError *error = NULL;
239 EmpathyAuthFactory *factory;
241 g_thread_init (NULL);
243 context = g_option_context_new (N_(" - Empathy authentication client"));
244 g_option_context_add_group (context, gtk_get_option_group (TRUE));
245 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
247 if (!g_option_context_parse (context, &argc, &argv, &error))
249 g_print ("%s\nRun '%s --help' to see a full list of available command "
250 "line options.\n", error->message, argv[0]);
251 g_warning ("Error in empathy-auth-client init: %s", error->message);
255 g_option_context_free (context);
258 gnutls_global_init ();
259 g_set_application_name (_("Empathy authentication client"));
261 /* Make empathy and empathy-auth-client appear as the same app in
263 gdk_set_program_class ("Empathy");
264 gtk_window_set_default_icon_name ("empathy");
265 textdomain (GETTEXT_PACKAGE);
267 factory = empathy_auth_factory_dup_singleton ();
269 g_signal_connect (factory, "new-server-tls-handler",
270 G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
272 g_signal_connect (factory, "new-server-sasl-handler",
273 G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
275 if (!empathy_auth_factory_register (factory, &error))
277 g_critical ("Failed to register the auth factory: %s\n", error->message);
278 g_error_free (error);
279 g_object_unref (factory);
284 DEBUG ("Empathy auth client started.");
286 if (g_getenv ("EMPATHY_PERSIST") != NULL)
288 DEBUG ("Timed-exit disabled");
297 g_object_unref (factory);