]> git.0d.be Git - empathy.git/blob - libempathy/empathy-auth-factory.c
Don't ignore the CA certificate if it's the only one in the chain
[empathy.git] / libempathy / empathy-auth-factory.c
1 /*
2  * empathy-auth-factory.c - Source for EmpathyAuthFactory
3  * Copyright (C) 2010 Collabora Ltd.
4  * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "empathy-auth-factory.h"
22
23 #include <telepathy-glib/interfaces.h>
24 #include <telepathy-glib/simple-handler.h>
25 #include <telepathy-glib/util.h>
26
27 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
28 #include "empathy-debug.h"
29 #include "empathy-server-tls-handler.h"
30 #include "empathy-utils.h"
31
32 #include "extensions/extensions.h"
33
34 G_DEFINE_TYPE (EmpathyAuthFactory, empathy_auth_factory, G_TYPE_OBJECT);
35
36 typedef struct {
37   TpBaseClient *handler;
38
39   gboolean dispose_run;
40 } EmpathyAuthFactoryPriv;
41
42 enum {
43   NEW_SERVER_TLS_HANDLER,
44   LAST_SIGNAL,
45 };
46
47 static guint signals[LAST_SIGNAL] = { 0, };
48
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAuthFactory)
50
51 static EmpathyAuthFactory *auth_factory_singleton = NULL;
52
53 typedef struct {
54   TpHandleChannelsContext *context;
55   EmpathyAuthFactory *self;
56 } HandlerContextData;
57
58 static void
59 handler_context_data_free (HandlerContextData *data)
60 {
61   tp_clear_object (&data->self);
62   tp_clear_object (&data->context);
63
64   g_slice_free (HandlerContextData, data);
65 }
66
67 static HandlerContextData *
68 handler_context_data_new (EmpathyAuthFactory *self,
69     TpHandleChannelsContext *context)
70 {
71   HandlerContextData *data;
72
73   data = g_slice_new0 (HandlerContextData);
74   data->self = g_object_ref (self);
75   data->context = g_object_ref (context);
76
77   return data;
78 }
79
80 static void
81 server_tls_handler_ready_cb (GObject *source,
82     GAsyncResult *res,
83     gpointer user_data)
84 {
85   EmpathyServerTLSHandler *handler;
86   GError *error = NULL;
87   EmpathyAuthFactoryPriv *priv;
88   HandlerContextData *data = user_data;
89
90   priv = GET_PRIV (data->self);
91   handler = empathy_server_tls_handler_new_finish (res, &error);
92
93   if (error != NULL)
94     {
95       DEBUG ("Failed to create a server TLS handler; error %s",
96           error->message);
97       tp_handle_channels_context_fail (data->context, error);
98
99       g_error_free (error);
100     }
101   else
102     {
103       tp_handle_channels_context_accept (data->context);
104       g_signal_emit (data->self, signals[NEW_SERVER_TLS_HANDLER], 0,
105           handler);
106
107       g_object_unref (handler);
108     }
109
110   handler_context_data_free (data);
111 }
112
113 static void
114 handle_channels_cb (TpSimpleHandler *handler,
115     TpAccount *account,
116     TpConnection *connection,
117     GList *channels,
118     GList *requests_satisfied,
119     gint64 user_action_time,
120     TpHandleChannelsContext *context,
121     gpointer user_data)
122 {
123   TpChannel *channel;
124   const GError *dbus_error;
125   GError *error = NULL;
126   EmpathyAuthFactory *self = user_data;
127   HandlerContextData *data;
128
129   DEBUG ("Handle TLS carrier channels.");
130
131   /* there can't be more than one ServerTLSConnection channels
132    * at the same time, for the same connection/account.
133    */
134   if (g_list_length (channels) != 1)
135     {
136       g_set_error_literal (&error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
137           "Can't handle more than one ServerTLSConnection channel "
138           "for the same connection.");
139
140       goto error;
141     }
142
143   channel = channels->data;
144
145   if (tp_channel_get_channel_type_id (channel) !=
146       EMP_IFACE_QUARK_CHANNEL_TYPE_SERVER_TLS_CONNECTION)
147     {
148       g_set_error (&error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
149           "Can only handle ServerTLSConnection channels, this was a %s "
150           "channel", tp_channel_get_channel_type (channel));
151
152       goto error;
153     }
154
155   dbus_error = tp_proxy_get_invalidated (channel);
156
157   if (dbus_error != NULL)
158     {
159       error = g_error_copy (dbus_error);
160       goto error;
161     }
162
163   /* create a handler */
164   data = handler_context_data_new (self, context);
165   tp_handle_channels_context_delay (context);
166   empathy_server_tls_handler_new_async (channel, server_tls_handler_ready_cb,
167       data);
168
169   return;
170
171  error:
172   tp_handle_channels_context_fail (context, error);
173   g_clear_error (&error);
174 }
175
176 static GObject *
177 empathy_auth_factory_constructor (GType type,
178     guint n_params,
179     GObjectConstructParam *params)
180 {
181   GObject *retval;
182
183   if (auth_factory_singleton != NULL)
184     {
185       retval = g_object_ref (auth_factory_singleton);
186     }
187   else
188     {
189       retval = G_OBJECT_CLASS (empathy_auth_factory_parent_class)->constructor
190         (type, n_params, params);
191
192       auth_factory_singleton = EMPATHY_AUTH_FACTORY (retval);
193       g_object_add_weak_pointer (retval, (gpointer *) &auth_factory_singleton);
194     }
195
196   return retval;
197 }
198
199 static void
200 empathy_auth_factory_init (EmpathyAuthFactory *self)
201 {
202   EmpathyAuthFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
203       EMPATHY_TYPE_AUTH_FACTORY, EmpathyAuthFactoryPriv);
204   TpDBusDaemon *bus;
205   GError *error = NULL;
206
207   self->priv = priv;
208
209   bus = tp_dbus_daemon_dup (&error);
210   if (error != NULL)
211     {
212       g_critical ("Failed to get TpDBusDaemon: %s", error->message);
213       g_error_free (error);
214       return;
215     }
216
217   priv->handler = tp_simple_handler_new (bus, FALSE, FALSE, "Empathy.Auth",
218       FALSE, handle_channels_cb, self, NULL);
219
220   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
221           TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
222           EMP_IFACE_CHANNEL_TYPE_SERVER_TLS_CONNECTION,
223           TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
224           TP_HANDLE_TYPE_NONE, NULL));
225
226   g_object_unref (bus);
227 }
228
229 static void
230 empathy_auth_factory_dispose (GObject *object)
231 {
232   EmpathyAuthFactoryPriv *priv = GET_PRIV (object);
233
234   if (priv->dispose_run)
235     return;
236
237   priv->dispose_run = TRUE;
238
239   tp_clear_object (&priv->handler);
240
241   G_OBJECT_CLASS (empathy_auth_factory_parent_class)->dispose (object);
242 }
243
244 static void
245 empathy_auth_factory_class_init (EmpathyAuthFactoryClass *klass)
246 {
247   GObjectClass *oclass = G_OBJECT_CLASS (klass);
248
249   oclass->constructor = empathy_auth_factory_constructor;
250   oclass->dispose = empathy_auth_factory_dispose;
251
252   g_type_class_add_private (klass, sizeof (EmpathyAuthFactoryPriv));
253
254   signals[NEW_SERVER_TLS_HANDLER] =
255     g_signal_new ("new-server-tls-handler",
256       G_TYPE_FROM_CLASS (klass),
257       G_SIGNAL_RUN_LAST, 0,
258       NULL, NULL,
259       g_cclosure_marshal_VOID__OBJECT,
260       G_TYPE_NONE,
261       1, EMPATHY_TYPE_SERVER_TLS_HANDLER);
262 }
263
264 EmpathyAuthFactory *
265 empathy_auth_factory_dup_singleton (void)
266 {
267   return g_object_new (EMPATHY_TYPE_AUTH_FACTORY, NULL);
268 }
269
270 gboolean
271 empathy_auth_factory_register (EmpathyAuthFactory *self,
272     GError **error)
273 {
274   EmpathyAuthFactoryPriv *priv = GET_PRIV (self);
275
276   return tp_base_client_register (priv->handler, error);
277 }