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