]> git.0d.be Git - empathy.git/blob - src/empathy-auth-client.c
Updated Oriya translation
[empathy.git] / src / empathy-auth-client.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
18  *
19  * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20  */
21
22 #include "config.h"
23
24 #include <glib/gi18n.h>
25 #include <gnutls/gnutls.h>
26
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"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
37 #include "empathy-debug.h"
38
39 #define TIMEOUT 60
40
41 static gboolean use_timer = TRUE;
42 static guint timeout_id = 0;
43 static guint num_windows = 0;
44
45 static gboolean
46 timeout_cb (gpointer p)
47 {
48   DEBUG ("Timeout reached; exiting...");
49
50   gtk_main_quit ();
51   return FALSE;
52 }
53
54 static void
55 start_timer (void)
56 {
57   if (!use_timer)
58     return;
59
60   if (timeout_id != 0)
61     return;
62
63   DEBUG ("Start timer");
64
65   timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
66 }
67
68 static void
69 stop_timer (void)
70 {
71   if (timeout_id == 0)
72     return;
73
74   DEBUG ("Stop timer");
75
76   g_source_remove (timeout_id);
77   timeout_id = 0;
78 }
79
80 static void
81 tls_dialog_response_cb (GtkDialog *dialog,
82     gint response_id,
83     gpointer user_data)
84 {
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);
91
92   g_object_get (tls_dialog,
93       "certificate", &certificate,
94       "reason", &reason,
95       "remember", &remember,
96       "details", &details,
97       NULL);
98
99   DEBUG ("Response %d (remember: %d)", response_id, remember);
100
101   gtk_widget_destroy (GTK_WIDGET (dialog));
102
103   if (response_id == GTK_RESPONSE_YES)
104     {
105       tp_tls_certificate_accept_async (certificate, NULL, NULL);
106     }
107   else
108     {
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));
112
113       tp_tls_certificate_reject_async (certificate, NULL, NULL);
114     }
115
116   if (remember)
117     empathy_tls_verifier_store_exception (verifier);
118
119   g_object_unref (certificate);
120   g_hash_table_unref (details);
121
122   /* restart the timeout */
123   num_windows--;
124
125   if (num_windows > 0)
126     return;
127
128   start_timer ();
129 }
130
131 static void
132 display_interactive_dialog (TpTLSCertificate *certificate,
133     EmpathyTLSVerifier *verifier,
134     TpTLSCertificateRejectReason reason,
135     GHashTable *details)
136 {
137   GtkWidget *tls_dialog;
138
139   /* stop the timeout */
140   num_windows++;
141   stop_timer ();
142
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);
147
148   gtk_widget_show (tls_dialog);
149 }
150
151 static void
152 verifier_verify_cb (GObject *source,
153     GAsyncResult *result,
154     gpointer user_data)
155 {
156   TpTLSCertificateRejectReason reason;
157   GError *error = NULL;
158   TpTLSCertificate *certificate = NULL;
159   GHashTable *details = NULL;
160   gchar *hostname = NULL;
161
162   g_object_get (source,
163       "certificate", &certificate,
164       NULL);
165
166   empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
167       result, &reason, &details, &error);
168
169   if (error != NULL)
170     {
171       DEBUG ("Error: %s", error->message);
172       display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
173               reason, details);
174
175       g_error_free (error);
176     }
177   else
178     {
179       tp_tls_certificate_accept_async (certificate, NULL, NULL);
180     }
181
182   g_free (hostname);
183   g_object_unref (certificate);
184 }
185
186 static void
187 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
188     EmpathyServerTLSHandler *handler,
189     gpointer user_data)
190 {
191   TpTLSCertificate *certificate = NULL;
192   gchar *hostname = NULL;
193   gchar **reference_identities = NULL;
194   EmpathyTLSVerifier *verifier;
195
196   DEBUG ("New TLS server handler received from the factory");
197
198   g_object_get (handler,
199       "certificate", &certificate,
200       "hostname", &hostname,
201       "reference-identities", &reference_identities,
202       NULL);
203
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);
208
209   g_object_unref (verifier);
210   g_object_unref (certificate);
211   g_free (hostname);
212   g_strfreev (reference_identities);
213 }
214
215 static void
216 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
217     EmpathyServerSASLHandler *handler,
218     gpointer user_data)
219 {
220   GtkWidget *dialog;
221
222   DEBUG ("New SASL server handler received from the factory");
223
224   /* If the handler has the password it will deal with it itself. */
225   if (!empathy_server_sasl_handler_has_password (handler))
226     {
227       DEBUG ("SASL handler doesn't have a password, prompt for one");
228
229       dialog = empathy_password_dialog_new (handler);
230       gtk_widget_show (dialog);
231     }
232 }
233
234 static void
235 retry_account_cb (GtkWidget *dialog,
236     TpAccount *account,
237     const gchar *password,
238     EmpathyAuthFactory *factory)
239 {
240   DEBUG ("Try reconnecting to %s", tp_account_get_path_suffix (account));
241
242   empathy_auth_factory_save_retry_password (factory, account, password);
243
244   tp_account_reconnect_async (account, NULL, NULL);
245 }
246
247 static void
248 auth_factory_auth_passsword_failed (EmpathyAuthFactory *factory,
249     TpAccount *account,
250     const gchar *password,
251     gpointer user_data)
252 {
253   GtkWidget *dialog;
254
255   DEBUG ("Authentication on %s failed, popup password dialog",
256       tp_account_get_path_suffix (account));
257
258   dialog = empathy_bad_password_dialog_new (account, password);
259
260   tp_g_signal_connect_object (dialog, "retry",
261       G_CALLBACK (retry_account_cb), factory, 0);
262
263   gtk_widget_show (dialog);
264 }
265
266 static void
267 sanity_cb (GObject *source,
268     GAsyncResult *result,
269     gpointer user_data)
270 {
271   start_timer ();
272 }
273
274 int
275 main (int argc,
276     char **argv)
277 {
278   GOptionContext *context;
279   GError *error = NULL;
280   EmpathyAuthFactory *factory;
281   TpDebugSender *debug_sender;
282   TpSimpleClientFactory *tp_factory;
283   TpDBusDaemon *dbus;
284
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);
288
289   if (!g_option_context_parse (context, &argc, &argv, &error))
290     {
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);
294       return EXIT_FAILURE;
295     }
296
297   g_option_context_free (context);
298
299   empathy_gtk_init ();
300   gnutls_global_init ();
301   g_set_application_name (_("Empathy authentication client"));
302
303   /* Make empathy and empathy-auth-client appear as the same app in
304    * gnome-shell */
305   gdk_set_program_class ("Empathy");
306   gtk_window_set_default_icon_name ("empathy");
307   textdomain (GETTEXT_PACKAGE);
308
309   /* There is no 'main' UI window so just use the default GdkScreen */
310   empathy_set_css_provider (NULL);
311
312 #ifdef ENABLE_DEBUG
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);
316 #endif
317
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,
322       0);
323
324   factory = empathy_auth_factory_new (tp_factory);
325   g_object_unref (tp_factory);
326   g_object_unref (dbus);
327
328   g_signal_connect (factory, "new-server-tls-handler",
329       G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
330
331   g_signal_connect (factory, "new-server-sasl-handler",
332       G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
333
334   g_signal_connect (factory, "auth-password-failed",
335       G_CALLBACK (auth_factory_auth_passsword_failed), NULL);
336
337   if (!empathy_auth_factory_register (factory, &error))
338     {
339       g_critical ("Failed to register the auth factory: %s\n", error->message);
340       g_error_free (error);
341       g_object_unref (factory);
342
343       return EXIT_FAILURE;
344     }
345
346   DEBUG ("Empathy auth client started.");
347
348   if (g_getenv ("EMPATHY_PERSIST") != NULL)
349     {
350       DEBUG ("Timed-exit disabled");
351
352       use_timer = FALSE;
353     }
354
355   /* Wait for the migration code to be done before starting the timer */
356   empathy_sanity_checking_run_async (sanity_cb, NULL);
357
358   gtk_main ();
359
360   g_object_unref (factory);
361   g_object_unref (debug_sender);
362
363   return EXIT_SUCCESS;
364 }