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>
26 #include "empathy-auth-factory.h"
27 #include "empathy-bad-password-dialog.h"
28 #include "empathy-password-dialog.h"
29 #include "empathy-sanity-cleaning.h"
30 #include "empathy-server-tls-handler.h"
31 #include "empathy-tls-dialog.h"
32 #include "empathy-tls-verifier.h"
33 #include "empathy-ui-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
36 #include "empathy-debug.h"
40 static gboolean use_timer
= TRUE
;
41 static guint timeout_id
= 0;
42 static guint num_windows
= 0;
45 timeout_cb (gpointer p
)
47 DEBUG ("Timeout reached; exiting...");
62 DEBUG ("Start timer");
64 timeout_id
= g_timeout_add_seconds (TIMEOUT
, timeout_cb
, NULL
);
75 g_source_remove (timeout_id
);
80 tls_dialog_response_cb (GtkDialog
*dialog
,
84 TpTLSCertificate
*certificate
= NULL
;
85 TpTLSCertificateRejectReason reason
= 0;
86 GHashTable
*details
= NULL
;
87 EmpathyTLSDialog
*tls_dialog
= EMPATHY_TLS_DIALOG (dialog
);
88 gboolean remember
= FALSE
;
89 EmpathyTLSVerifier
*verifier
= EMPATHY_TLS_VERIFIER (user_data
);
91 g_object_get (tls_dialog
,
92 "certificate", &certificate
,
94 "remember", &remember
,
98 DEBUG ("Response %d (remember: %d)", response_id
, remember
);
100 gtk_widget_destroy (GTK_WIDGET (dialog
));
102 if (response_id
== GTK_RESPONSE_YES
)
104 tp_tls_certificate_accept_async (certificate
, NULL
, NULL
);
108 tp_asv_set_boolean (details
, "user-requested", TRUE
);
109 tp_tls_certificate_add_rejection (certificate
, reason
, NULL
,
110 g_variant_new_parsed ("{ 'user-requested': <%b> }", TRUE
));
112 tp_tls_certificate_reject_async (certificate
, NULL
, NULL
);
116 empathy_tls_verifier_store_exception (verifier
);
118 g_object_unref (certificate
);
119 g_hash_table_unref (details
);
121 /* restart the timeout */
131 display_interactive_dialog (TpTLSCertificate
*certificate
,
132 EmpathyTLSVerifier
*verifier
,
133 TpTLSCertificateRejectReason reason
,
136 GtkWidget
*tls_dialog
;
138 /* stop the timeout */
142 tls_dialog
= empathy_tls_dialog_new (certificate
, reason
, details
);
143 g_signal_connect_data (tls_dialog
, "response",
144 G_CALLBACK (tls_dialog_response_cb
), g_object_ref (verifier
),
145 (GClosureNotify
)g_object_unref
, 0);
147 gtk_widget_show (tls_dialog
);
151 verifier_verify_cb (GObject
*source
,
152 GAsyncResult
*result
,
155 TpTLSCertificateRejectReason reason
;
156 GError
*error
= NULL
;
157 TpTLSCertificate
*certificate
= NULL
;
158 GHashTable
*details
= NULL
;
159 gchar
*hostname
= NULL
;
161 g_object_get (source
,
162 "certificate", &certificate
,
165 empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source
),
166 result
, &reason
, &details
, &error
);
170 DEBUG ("Error: %s", error
->message
);
171 display_interactive_dialog (certificate
, EMPATHY_TLS_VERIFIER (source
),
174 g_error_free (error
);
178 tp_tls_certificate_accept_async (certificate
, NULL
, NULL
);
182 g_object_unref (certificate
);
186 auth_factory_new_tls_handler_cb (EmpathyAuthFactory
*factory
,
187 EmpathyServerTLSHandler
*handler
,
190 TpTLSCertificate
*certificate
= NULL
;
191 gchar
*hostname
= NULL
;
192 gchar
**reference_identities
= NULL
;
193 EmpathyTLSVerifier
*verifier
;
195 DEBUG ("New TLS server handler received from the factory");
197 g_object_get (handler
,
198 "certificate", &certificate
,
199 "hostname", &hostname
,
200 "reference-identities", &reference_identities
,
203 verifier
= empathy_tls_verifier_new (certificate
, hostname
,
204 (const gchar
**) reference_identities
);
205 empathy_tls_verifier_verify_async (verifier
,
206 verifier_verify_cb
, NULL
);
208 g_object_unref (verifier
);
209 g_object_unref (certificate
);
211 g_strfreev (reference_identities
);
215 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory
*factory
,
216 EmpathyServerSASLHandler
*handler
,
221 DEBUG ("New SASL server handler received from the factory");
223 /* If the handler has the password it will deal with it itself. */
224 if (!empathy_server_sasl_handler_has_password (handler
))
226 DEBUG ("SASL handler doesn't have a password, prompt for one");
228 dialog
= empathy_password_dialog_new (handler
);
229 gtk_widget_show (dialog
);
234 retry_account_cb (GtkWidget
*dialog
,
236 const gchar
*password
,
237 EmpathyAuthFactory
*factory
)
239 DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account
));
241 empathy_auth_factory_save_retry_password (factory
, account
, password
);
243 tp_account_reconnect_async (account
, NULL
, NULL
);
247 auth_factory_auth_passsword_failed (EmpathyAuthFactory
*factory
,
249 const gchar
*password
,
254 DEBUG ("Authentication on %s failed, popup password dialog",
255 tp_account_get_path_suffix (account
));
257 dialog
= empathy_bad_password_dialog_new (account
, password
);
259 tp_g_signal_connect_object (dialog
, "retry",
260 G_CALLBACK (retry_account_cb
), factory
, 0);
262 gtk_widget_show (dialog
);
266 sanity_cb (GObject
*source
,
267 GAsyncResult
*result
,
277 GOptionContext
*context
;
278 GError
*error
= NULL
;
279 EmpathyAuthFactory
*factory
;
280 TpDebugSender
*debug_sender
;
281 TpSimpleClientFactory
*tp_factory
;
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 g_set_application_name (_("Empathy authentication client"));
301 /* Make empathy and empathy-auth-client appear as the same app in
303 g_set_prgname ("empathy");
304 gtk_window_set_default_icon_name ("empathy");
305 textdomain (GETTEXT_PACKAGE
);
307 /* There is no 'main' UI window so just use the default GdkScreen */
308 empathy_set_css_provider (NULL
);
311 /* Set up debug sender */
312 debug_sender
= tp_debug_sender_dup ();
313 g_log_set_default_handler (tp_debug_sender_log_handler
, G_LOG_DOMAIN
);
316 dbus
= tp_dbus_daemon_dup (NULL
);
317 tp_factory
= tp_simple_client_factory_new (dbus
);
318 tp_simple_client_factory_add_account_features_varargs (tp_factory
,
319 TP_ACCOUNT_FEATURE_STORAGE
,
322 factory
= empathy_auth_factory_new (tp_factory
);
323 g_object_unref (tp_factory
);
324 g_object_unref (dbus
);
326 g_signal_connect (factory
, "new-server-tls-handler",
327 G_CALLBACK (auth_factory_new_tls_handler_cb
), NULL
);
329 g_signal_connect (factory
, "new-server-sasl-handler",
330 G_CALLBACK (auth_factory_new_sasl_handler_cb
), NULL
);
332 g_signal_connect (factory
, "auth-password-failed",
333 G_CALLBACK (auth_factory_auth_passsword_failed
), NULL
);
335 if (!empathy_auth_factory_register (factory
, &error
))
337 g_critical ("Failed to register the auth factory: %s\n", error
->message
);
338 g_error_free (error
);
339 g_object_unref (factory
);
344 DEBUG ("Empathy auth client started.");
346 if (g_getenv ("EMPATHY_PERSIST") != NULL
)
348 DEBUG ("Timed-exit disabled");
353 /* Wait for the migration code to be done before starting the timer */
354 empathy_sanity_checking_run_async (sanity_cb
, NULL
);
358 g_object_unref (factory
);
359 g_object_unref (debug_sender
);