]> git.0d.be Git - empathy.git/blob - src/empathy-auth-client.c
Updated Czech 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 <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   EmpathyTLSVerifier *verifier = EMPATHY_TLS_VERIFIER (user_data);
97
98   DEBUG ("Response %d", response_id);
99
100   g_object_get (tls_dialog,
101       "certificate", &certificate,
102       "reason", &reason,
103       "remember", &remember,
104       "details", &details,
105       NULL);
106
107   gtk_widget_destroy (GTK_WIDGET (dialog));
108
109   if (response_id == GTK_RESPONSE_YES)
110     {
111       empathy_tls_certificate_accept_async (certificate, NULL, NULL);
112     }
113   else
114     {
115       tp_asv_set_boolean (details, "user-requested", TRUE);
116       empathy_tls_certificate_reject_async (certificate, reason, details,
117           NULL, NULL);
118     }
119
120   if (remember)
121     empathy_tls_verifier_store_exception (verifier);
122
123   g_object_unref (certificate);
124   g_hash_table_unref (details);
125
126   /* restart the timeout */
127   num_windows--;
128
129   if (num_windows > 0)
130     return;
131
132   start_timer ();
133 }
134
135 static void
136 display_interactive_dialog (EmpathyTLSCertificate *certificate,
137     EmpathyTLSVerifier *verifier,
138     EmpTLSCertificateRejectReason reason,
139     GHashTable *details)
140 {
141   GtkWidget *tls_dialog;
142
143   /* stop the timeout */
144   num_windows++;
145   stop_timer ();
146
147   tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
148   g_signal_connect_data (tls_dialog, "response",
149       G_CALLBACK (tls_dialog_response_cb), g_object_ref (verifier),
150       (GClosureNotify)g_object_unref, 0);
151
152   gtk_widget_show (tls_dialog);
153 }
154
155 static void
156 verifier_verify_cb (GObject *source,
157     GAsyncResult *result,
158     gpointer user_data)
159 {
160   gboolean res;
161   EmpTLSCertificateRejectReason reason;
162   GError *error = NULL;
163   EmpathyTLSCertificate *certificate = NULL;
164   GHashTable *details = NULL;
165   gchar *hostname = NULL;
166
167   g_object_get (source,
168       "certificate", &certificate,
169       NULL);
170
171   res = empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
172       result, &reason, &details, &error);
173
174   if (error != NULL)
175     {
176       DEBUG ("Error: %s", error->message);
177       display_interactive_dialog (certificate, EMPATHY_TLS_VERIFIER (source),
178               reason, details);
179
180       g_error_free (error);
181     }
182   else
183     {
184       empathy_tls_certificate_accept_async (certificate, NULL, NULL);
185     }
186
187   g_free (hostname);
188   g_object_unref (certificate);
189 }
190
191 static void
192 auth_factory_new_tls_handler_cb (EmpathyAuthFactory *factory,
193     EmpathyServerTLSHandler *handler,
194     gpointer user_data)
195 {
196   EmpathyTLSCertificate *certificate = NULL;
197   gchar *hostname = NULL;
198   EmpathyTLSVerifier *verifier;
199
200   DEBUG ("New TLS server handler received from the factory");
201
202   g_object_get (handler,
203       "certificate", &certificate,
204       "hostname", &hostname,
205       NULL);
206
207   verifier = empathy_tls_verifier_new (certificate, hostname);
208   empathy_tls_verifier_verify_async (verifier,
209       verifier_verify_cb, NULL);
210
211   g_object_unref (verifier);
212   g_object_unref (certificate);
213   g_free (hostname);
214 }
215
216 static void
217 auth_factory_new_sasl_handler_cb (EmpathyAuthFactory *factory,
218     EmpathyServerSASLHandler *handler,
219     gpointer user_data)
220 {
221   GtkWidget *dialog;
222
223   DEBUG ("New SASL server handler received from the factory");
224
225   /* If the handler has the password it will deal with it itself. */
226   if (!empathy_server_sasl_handler_has_password (handler))
227     {
228       dialog = empathy_password_dialog_new (handler);
229       gtk_widget_show (dialog);
230     }
231 }
232
233 int
234 main (int argc,
235     char **argv)
236 {
237   GOptionContext *context;
238   GError *error = NULL;
239   EmpathyAuthFactory *factory;
240
241   g_thread_init (NULL);
242
243   context = g_option_context_new (N_(" - Empathy authentication client"));
244   g_option_context_add_group (context, gtk_get_option_group (TRUE));
245   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
246
247   if (!g_option_context_parse (context, &argc, &argv, &error))
248     {
249       g_print ("%s\nRun '%s --help' to see a full list of available command "
250           "line options.\n", error->message, argv[0]);
251       g_warning ("Error in empathy-auth-client init: %s", error->message);
252       return EXIT_FAILURE;
253     }
254
255   g_option_context_free (context);
256
257   empathy_gtk_init ();
258   gnutls_global_init ();
259   g_set_application_name (_("Empathy authentication client"));
260
261   /* Make empathy and empathy-auth-client appear as the same app in
262    * gnome-shell */
263   gdk_set_program_class ("Empathy");
264   gtk_window_set_default_icon_name ("empathy");
265   textdomain (GETTEXT_PACKAGE);
266
267   factory = empathy_auth_factory_dup_singleton ();
268
269   g_signal_connect (factory, "new-server-tls-handler",
270       G_CALLBACK (auth_factory_new_tls_handler_cb), NULL);
271
272   g_signal_connect (factory, "new-server-sasl-handler",
273       G_CALLBACK (auth_factory_new_sasl_handler_cb), NULL);
274
275   if (!empathy_auth_factory_register (factory, &error))
276     {
277       g_critical ("Failed to register the auth factory: %s\n", error->message);
278       g_error_free (error);
279       g_object_unref (factory);
280
281       return EXIT_FAILURE;
282     }
283
284   DEBUG ("Empathy auth client started.");
285
286   if (g_getenv ("EMPATHY_PERSIST") != NULL)
287     {
288       DEBUG ("Timed-exit disabled");
289
290       use_timer = FALSE;
291     }
292
293   start_timer ();
294
295   gtk_main ();
296
297   g_object_unref (factory);
298
299   return EXIT_SUCCESS;
300 }