]> git.0d.be Git - empathy.git/blob - src/empathy-auth-helper.c
Push the details table to the dialog after verification
[empathy.git] / src / empathy-auth-helper.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 static void
43 tls_dialog_response_cb (GtkDialog *dialog,
44     gint response_id,
45     gpointer user_data)
46 {
47   EmpathyTLSCertificate *certificate = NULL;
48   EmpTLSCertificateRejectReason reason = 0;
49   EmpathyTLSDialog *tls_dialog = EMPATHY_TLS_DIALOG (dialog);
50   gboolean remember = FALSE;
51
52   DEBUG ("Response %d", response_id);
53
54   g_object_get (tls_dialog,
55       "certificate", &certificate,
56       "reason", &reason,
57       "remember", &remember,
58       NULL);
59
60   gtk_widget_destroy (GTK_WIDGET (dialog));
61
62   if (response_id == GTK_RESPONSE_YES)
63     empathy_tls_certificate_accept_async (certificate, NULL, NULL);
64   else
65     empathy_tls_certificate_reject_async (certificate, reason, TRUE,
66         NULL, NULL);
67
68   if (remember)
69     empathy_tls_certificate_store_ca (certificate);
70
71   g_object_unref (certificate);
72 }
73
74 static void
75 display_interactive_dialog (EmpathyTLSCertificate *certificate,
76     EmpTLSCertificateRejectReason reason,
77     GHashTable *details)
78 {
79   GtkWidget *tls_dialog;
80
81   tls_dialog = empathy_tls_dialog_new (certificate, reason, details);
82   g_signal_connect (tls_dialog, "response",
83       G_CALLBACK (tls_dialog_response_cb), NULL);
84
85   gtk_widget_show (tls_dialog);
86 }
87
88 static void
89 verifier_verify_cb (GObject *source,
90     GAsyncResult *result,
91     gpointer user_data)
92 {
93   gboolean res;
94   EmpTLSCertificateRejectReason reason;
95   GError *error = NULL;
96   EmpathyTLSCertificate *certificate = NULL;
97   GHashTable *details = NULL;
98
99   g_object_get (source,
100       "certificate", &certificate,
101       NULL);
102
103   res = empathy_tls_verifier_verify_finish (EMPATHY_TLS_VERIFIER (source),
104       result, &reason, &details, &error);
105
106   if (error != NULL)
107     {
108       DEBUG ("Error: %s", error->message);
109       display_interactive_dialog (certificate, reason, details);
110
111       g_error_free (error);
112     }
113   else
114     {
115       empathy_tls_certificate_accept_async (certificate, NULL, NULL);
116     }
117
118   g_object_unref (certificate);
119 }
120
121 static void
122 auth_factory_new_handler_cb (EmpathyAuthFactory *factory,
123     EmpathyServerTLSHandler *handler,
124     gpointer user_data)
125 {
126   EmpathyTLSCertificate *certificate = NULL;
127   gchar *hostname = NULL;
128   EmpathyTLSVerifier *verifier;
129
130   DEBUG ("New TLS server handler received from the factory");
131
132   g_object_get (handler,
133       "certificate", &certificate,
134       "hostname", &hostname,
135       NULL);
136
137   verifier = empathy_tls_verifier_new (certificate, hostname);
138   empathy_tls_verifier_verify_async (verifier,
139       verifier_verify_cb, NULL);
140
141   g_object_unref (verifier);
142   g_object_unref (certificate);
143   g_free (hostname);
144 }
145
146 int
147 main (int argc,
148     char **argv)
149 {
150   GOptionContext *context;
151   GError *error = NULL;
152   EmpathyAuthFactory *factory;
153
154   g_thread_init (NULL);
155
156   context = g_option_context_new (N_(" - Empathy authentication helper"));
157   g_option_context_add_group (context, gtk_get_option_group (TRUE));
158   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
159
160   if (!g_option_context_parse (context, &argc, &argv, &error))
161     {
162       g_print ("%s\nRun '%s --help' to see a full list of available command "
163           "line options.\n", error->message, argv[0]);
164       g_warning ("Error in empathy-auth-helper init: %s", error->message);
165       return EXIT_FAILURE;
166     }
167
168   g_option_context_free (context);
169
170   empathy_gtk_init ();
171   gnutls_global_init ();
172   g_set_application_name (_("Empathy authentication helper"));
173
174   gtk_window_set_default_icon_name ("empathy");
175   textdomain (GETTEXT_PACKAGE);
176
177   factory = empathy_auth_factory_dup_singleton ();
178
179   g_signal_connect (factory, "new-server-tls-handler",
180       G_CALLBACK (auth_factory_new_handler_cb), NULL);
181
182   if (!empathy_auth_factory_register (factory, &error))
183     {
184       g_critical ("Failed to register the auth factory: %s\n", error->message);
185       g_error_free (error);
186       g_object_unref (factory);
187
188       return EXIT_FAILURE;
189     }
190
191   DEBUG ("Empathy auth client started.");  
192
193   gtk_main ();
194
195   g_object_unref (factory);
196
197   return EXIT_SUCCESS;
198 }