]> git.0d.be Git - empathy.git/blob - src/empathy-auth-client.c
Merge branch 'change-audio'
[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 <stdlib.h>
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include <telepathy-glib/debug-sender.h>
30
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>
38
39 #include <libempathy-gtk/empathy-password-dialog.h>
40 #include <libempathy-gtk/empathy-tls-dialog.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
42
43 #include <gnutls/gnutls.h>
44
45 #include <extensions/extensions.h>
46
47 #define TIMEOUT 60
48
49 static gboolean use_timer = TRUE;
50 static guint timeout_id = 0;
51 static guint num_windows = 0;
52
53 static gboolean
54 timeout_cb (gpointer p)
55 {
56   DEBUG ("Timeout reached; exiting...");
57
58   gtk_main_quit ();
59   return FALSE;
60 }
61
62 static void
63 start_timer (void)
64 {
65   if (!use_timer)
66     return;
67
68   if (timeout_id != 0)
69     return;
70
71   DEBUG ("Start timer");
72
73   timeout_id = g_timeout_add_seconds (TIMEOUT, timeout_cb, NULL);
74 }
75
76 static void
77 stop_timer (void)
78 {
79   if (timeout_id == 0)
80     return;
81
82   DEBUG ("Stop timer");
83
84   g_source_remove (timeout_id);
85   timeout_id = 0;
86 }
87
88 static void
89 tls_dialog_response_cb (GtkDialog *dialog,
90     gint response_id,
91     gpointer user_data)
92 {
93   EmpathyTLSCertificate *certificate = NULL;
94   EmpTLSCertificateRejectReason reason = 0;
95   GHashTable *details = NULL;
96   EmpathyTLSDialog *tls_dialog = EMPATHY_TLS_DIALOG (dialog);
97   gboolean remember = FALSE;
98   EmpathyTLSVerifier *verifier = EMPATHY_TLS_VERIFIER (user_data);
99
100   DEBUG ("Response %d", response_id);
101
102   g_object_get (tls_dialog,
103       "certificate", &certificate,
104       "reason", &reason,
105       "remember", &remember,
106       "details", &details,
107       NULL);
108
109   gtk_widget_destroy (GTK_WIDGET (dialog));
110
111   if (response_id == GTK_RESPONSE_YES)
112     {
113       empathy_tls_certificate_accept_async (certificate, NULL, NULL);
114     }
115   else
116     {
117       tp_asv_set_boolean (details, "user-requested", TRUE);
118       empathy_tls_certificate_reject_async (certificate, reason, details,
119           NULL, NULL);
120     }
121
122   if (remember)
123     empathy_tls_verifier_store_exception (verifier);
124
125   g_object_unref (certificate);
126   g_hash_table_unref (details);
127
128   /* restart the timeout */
129   num_windows--;
130
131   if (num_windows > 0)
132     return;
133
134   start_timer ();
135 }
136
137 static void
138 display_interactive_dialog (EmpathyTLSCertificate *certificate,
139     EmpathyTLSVerifier *verifier,
140     EmpTLSCertificateRejectReason reason,
141     GHashTable *details)
142 {
143   GtkWidget *tls_dialog;
144
145   /* stop the timeout */
146   num_windows++;
147   stop_timer ();
148
149   tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
150   g_signal_connect_data (tls_dialog, "response",
151       G_CALLBACK (tls_dialog_response_cb), g_object_ref (verifier),
152       (GClosureNotify)g_object_unref, 0);
153
154   gtk_widget_show (tls_dialog);
155 }
156
157 static void
158 verifier_verify_cb (GObject *source,
159     GAsyncResult *result,
160     gpointer user_data)
161 {
162   EmpTLSCertificateRejectReason reason;
163   GError *error = NULL;
164   EmpathyTLSCertificate *certificate = NULL;
165   GHashTable *details = NULL;
166   gchar *hostname = NULL;
167
168   g_object_get (source,
169       "certificate", &certificate,
170       NULL);
171
172   empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
173       result, &reason, &details, &error);
174
175   if (error != NULL)
176     {
177       DEBUG ("Error: %s", error->message);
178       display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
179               reason, details);
180
181       g_error_free (error);
182     }
183   else
184     {
185       empathy_tls_certificate_accept_async (certificate, NULL, NULL);
186     }
187
188   g_free (hostname);
189   g_object_unref (certificate);
190 }
191
192 static void
193 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
194     EmpathyServerTLSHandler *handler,
195     gpointer user_data)
196 {
197   EmpathyTLSCertificate *certificate = NULL;
198   gchar *hostname = NULL;
199   gchar **reference_identities = NULL;
200   EmpathyTLSVerifier *verifier;
201
202   DEBUG ("New TLS server handler received from the factory");
203
204   g_object_get (handler,
205       "certificate", &certificate,
206       "hostname", &hostname,
207       "reference-identities", &reference_identities,
208       NULL);
209
210   verifier = empathy_tls_verifier_new (certificate, hostname,
211       (const gchar **) reference_identities);
212   empathy_tls_verifier_verify_async (verifier,
213       verifier_verify_cb, NULL);
214
215   g_object_unref (verifier);
216   g_object_unref (certificate);
217   g_free (hostname);
218   g_strfreev (reference_identities);
219 }
220
221 static void
222 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
223     EmpathyServerSASLHandler *handler,
224     gpointer user_data)
225 {
226   GtkWidget *dialog;
227
228   DEBUG ("New SASL server handler received from the factory");
229
230   /* If the handler has the password it will deal with it itself. */
231   if (!empathy_server_sasl_handler_has_password (handler))
232     {
233       dialog = empathy_password_dialog_new (handler);
234       gtk_widget_show (dialog);
235     }
236 }
237
238 int
239 main (int argc,
240     char **argv)
241 {
242   GOptionContext *context;
243   GError *error = NULL;
244   EmpathyAuthFactory *factory;
245   TpDebugSender *debug_sender;
246
247   g_thread_init (NULL);
248
249   context = g_option_context_new (N_(" - Empathy authentication client"));
250   g_option_context_add_group (context, gtk_get_option_group (TRUE));
251   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
252
253   if (!g_option_context_parse (context, &argc, &argv, &error))
254     {
255       g_print ("%s\nRun '%s --help' to see a full list of available command "
256           "line options.\n", error->message, argv[0]);
257       g_warning ("Error in empathy-auth-client init: %s", error->message);
258       return EXIT_FAILURE;
259     }
260
261   g_option_context_free (context);
262
263   empathy_gtk_init ();
264   gnutls_global_init ();
265   g_set_application_name (_("Empathy authentication client"));
266
267   /* Make empathy and empathy-auth-client appear as the same app in
268    * gnome-shell */
269   gdk_set_program_class ("Empathy");
270   gtk_window_set_default_icon_name ("empathy");
271   textdomain (GETTEXT_PACKAGE);
272
273 #ifdef ENABLE_DEBUG
274   /* Set up debug sender */
275   debug_sender = tp_debug_sender_dup ();
276   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
277 #endif
278
279   factory = empathy_auth_factory_dup_singleton ();
280
281   g_signal_connect (factory, "new-server-tls-handler",
282       G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
283
284   g_signal_connect (factory, "new-server-sasl-handler",
285       G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
286
287   if (!empathy_auth_factory_register (factory, &error))
288     {
289       g_critical ("Failed to register the auth factory: %s\n", error->message);
290       g_error_free (error);
291       g_object_unref (factory);
292
293       return EXIT_FAILURE;
294     }
295
296   DEBUG ("Empathy auth client started.");
297
298   if (g_getenv ("EMPATHY_PERSIST") != NULL)
299     {
300       DEBUG ("Timed-exit disabled");
301
302       use_timer = FALSE;
303     }
304
305   start_timer ();
306
307   gtk_main ();
308
309   g_object_unref (factory);
310   g_object_unref (debug_sender);
311
312   return EXIT_SUCCESS;
313 }