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