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>
24 #include <glib/gi18n.h>
25 #include <gnutls/gnutls.h>
27 #include "empathy-auth-factory.h"
28 #include "empathy-bad-password-dialog.h"
29 #include "empathy-password-dialog.h"
30 #include "empathy-sanity-cleaning.h"
31 #include "empathy-server-tls-handler.h"
32 #include "empathy-tls-dialog.h"
33 #include "empathy-tls-verifier.h"
34 #include "empathy-ui-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
37 #include "empathy-debug.h"
41 static gboolean use_timer
= TRUE
;
42 static guint timeout_id
= 0;
43 static guint num_windows
= 0;
46 timeout_cb (gpointer p
)
48 DEBUG ("Timeout reached; exiting...");
63 DEBUG ("Start timer");
65 timeout_id
= g_timeout_add_seconds (TIMEOUT
, timeout_cb
, NULL
);
76 g_source_remove (timeout_id
);
81 tls_dialog_response_cb (GtkDialog
*dialog
,
85 TpTLSCertificate
*certificate
= NULL
;
86 TpTLSCertificateRejectReason reason
= 0;
87 GHashTable
*details
= NULL
;
88 EmpathyTLSDialog
*tls_dialog
= EMPATHY_TLS_DIALOG (dialog
);
89 gboolean remember
= FALSE
;
90 EmpathyTLSVerifier
*verifier
= EMPATHY_TLS_VERIFIER (user_data
);
92 g_object_get (tls_dialog
,
93 "certificate", &certificate
,
95 "remember", &remember
,
99 DEBUG ("Response %d (remember: %d)", response_id
, remember
);
101 gtk_widget_destroy (GTK_WIDGET (dialog
));
103 if (response_id
== GTK_RESPONSE_YES
)
105 tp_tls_certificate_accept_async (certificate
, NULL
, NULL
);
109 tp_asv_set_boolean (details
, "user-requested", TRUE
);
110 tp_tls_certificate_add_rejection (certificate
, reason
, NULL
,
111 g_variant_new_parsed ("{ 'user-requested': <%b> }", TRUE
));
113 tp_tls_certificate_reject_async (certificate
, NULL
, NULL
);
117 empathy_tls_verifier_store_exception (verifier
);
119 g_object_unref (certificate
);
120 g_hash_table_unref (details
);
122 /* restart the timeout */
132 display_interactive_dialog (TpTLSCertificate
*certificate
,
133 EmpathyTLSVerifier
*verifier
,
134 TpTLSCertificateRejectReason reason
,
137 GtkWidget
*tls_dialog
;
139 /* stop the timeout */
143 tls_dialog
= empathy_tls_dialog_new (certificate
, reason
, details
);
144 g_signal_connect_data (tls_dialog
, "response",
145 G_CALLBACK (tls_dialog_response_cb
), g_object_ref (verifier
),
146 (GClosureNotify
)g_object_unref
, 0);
148 gtk_widget_show (tls_dialog
);
152 verifier_verify_cb (GObject
*source
,
153 GAsyncResult
*result
,
156 TpTLSCertificateRejectReason reason
;
157 GError
*error
= NULL
;
158 TpTLSCertificate
*certificate
= NULL
;
159 GHashTable
*details
= NULL
;
160 gchar
*hostname
= NULL
;
162 g_object_get (source
,
163 "certificate", &certificate
,
166 empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source
),
167 result
, &reason
, &details
, &error
);
171 DEBUG ("Error: %s", error
->message
);
172 display_interactive_dialog (certificate
, EMPATHY_TLS_VERIFIER (source
),
175 g_error_free (error
);
179 tp_tls_certificate_accept_async (certificate
, NULL
, NULL
);
183 g_object_unref (certificate
);
187 auth_factory_new_tls_handler_cb (EmpathyAuthFactory
*factory
,
188 EmpathyServerTLSHandler
*handler
,
191 TpTLSCertificate
*certificate
= NULL
;
192 gchar
*hostname
= NULL
;
193 gchar
**reference_identities
= NULL
;
194 EmpathyTLSVerifier
*verifier
;
196 DEBUG ("New TLS server handler received from the factory");
198 g_object_get (handler
,
199 "certificate", &certificate
,
200 "hostname", &hostname
,
201 "reference-identities", &reference_identities
,
204 verifier
= empathy_tls_verifier_new (certificate
, hostname
,
205 (const gchar
**) reference_identities
);
206 empathy_tls_verifier_verify_async (verifier
,
207 verifier_verify_cb
, NULL
);
209 g_object_unref (verifier
);
210 g_object_unref (certificate
);
212 g_strfreev (reference_identities
);
216 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory
*factory
,
217 EmpathyServerSASLHandler
*handler
,
222 DEBUG ("New SASL server handler received from the factory");
224 /* If the handler has the password it will deal with it itself. */
225 if (!empathy_server_sasl_handler_has_password (handler
))
227 DEBUG ("SASL handler doesn't have a password, prompt for one");
229 dialog
= empathy_password_dialog_new (handler
);
230 gtk_widget_show (dialog
);
235 retry_account_cb (GtkWidget
*dialog
,
237 const gchar
*password
,
238 EmpathyAuthFactory
*factory
)
240 DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account
));
242 empathy_auth_factory_save_retry_password (factory
, account
, password
);
244 tp_account_reconnect_async (account
, NULL
, NULL
);
248 auth_factory_auth_passsword_failed (EmpathyAuthFactory
*factory
,
250 const gchar
*password
,
255 DEBUG ("Authentication on %s failed, popup password dialog",
256 tp_account_get_path_suffix (account
));
258 dialog
= empathy_bad_password_dialog_new (account
, password
);
260 tp_g_signal_connect_object (dialog
, "retry",
261 G_CALLBACK (retry_account_cb
), factory
, 0);
263 gtk_widget_show (dialog
);
267 sanity_cb (GObject
*source
,
268 GAsyncResult
*result
,
278 GOptionContext
*context
;
279 GError
*error
= NULL
;
280 EmpathyAuthFactory
*factory
;
281 TpDebugSender
*debug_sender
;
282 TpSimpleClientFactory
*tp_factory
;
285 context
= g_option_context_new (N_(" - Empathy authentication client"));
286 g_option_context_add_group (context
, gtk_get_option_group (TRUE
));
287 g_option_context_set_translation_domain (context
, GETTEXT_PACKAGE
);
289 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
291 g_print ("%s\nRun '%s --help' to see a full list of available command "
292 "line options.\n", error
->message
, argv
[0]);
293 g_warning ("Error in empathy-auth-client init: %s", error
->message
);
297 g_option_context_free (context
);
300 gnutls_global_init ();
301 g_set_application_name (_("Empathy authentication client"));
303 /* Make empathy and empathy-auth-client appear as the same app in
305 g_set_prgname ("empathy");
306 gtk_window_set_default_icon_name ("empathy");
307 textdomain (GETTEXT_PACKAGE
);
309 /* There is no 'main' UI window so just use the default GdkScreen */
310 empathy_set_css_provider (NULL
);
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");
355 /* Wait for the migration code to be done before starting the timer */
356 empathy_sanity_checking_run_async (sanity_cb
, NULL
);
360 g_object_unref (factory
);
361 g_object_unref (debug_sender
);