]> git.0d.be Git - empathy.git/blob - libempathy/empathy-auth-factory.c
Merge branch 'sasl'
[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-sasl-handler.h"
30 #include "empathy-server-tls-handler.h"
31 #include "empathy-utils.h"
32
33 #include "extensions/extensions.h"
34
35 G_DEFINE_TYPE (EmpathyAuthFactory, empathy_auth_factory, G_TYPE_OBJECT);
36
37 typedef struct {
38   TpBaseClient *handler;
39
40   /* Keep a ref here so the auth client doesn't have to mess with
41    * refs. It will be cleared when the channel (and so the handler)
42    * gets invalidated. */
43   EmpathyServerSASLHandler *sasl_handler;
44
45   gboolean dispose_run;
46 } EmpathyAuthFactoryPriv;
47
48 enum {
49   NEW_SERVER_TLS_HANDLER,
50   NEW_SERVER_SASL_HANDLER,
51   LAST_SIGNAL,
52 };
53
54 static guint signals[LAST_SIGNAL] = { 0, };
55
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAuthFactory)
57
58 static EmpathyAuthFactory *auth_factory_singleton = NULL;
59
60 typedef struct {
61   TpHandleChannelsContext *context;
62   EmpathyAuthFactory *self;
63 } HandlerContextData;
64
65 static void
66 handler_context_data_free (HandlerContextData *data)
67 {
68   tp_clear_object (&data->self);
69   tp_clear_object (&data->context);
70
71   g_slice_free (HandlerContextData, data);
72 }
73
74 static HandlerContextData *
75 handler_context_data_new (EmpathyAuthFactory *self,
76     TpHandleChannelsContext *context)
77 {
78   HandlerContextData *data;
79
80   data = g_slice_new0 (HandlerContextData);
81   data->self = g_object_ref (self);
82   data->context = g_object_ref (context);
83
84   return data;
85 }
86
87 static void
88 server_tls_handler_ready_cb (GObject *source,
89     GAsyncResult *res,
90     gpointer user_data)
91 {
92   EmpathyServerTLSHandler *handler;
93   GError *error = NULL;
94   EmpathyAuthFactoryPriv *priv;
95   HandlerContextData *data = user_data;
96
97   priv = GET_PRIV (data->self);
98   handler = empathy_server_tls_handler_new_finish (res, &error);
99
100   if (error != NULL)
101     {
102       DEBUG ("Failed to create a server TLS handler; error %s",
103           error->message);
104       tp_handle_channels_context_fail (data->context, error);
105
106       g_error_free (error);
107     }
108   else
109     {
110       tp_handle_channels_context_accept (data->context);
111       g_signal_emit (data->self, signals[NEW_SERVER_TLS_HANDLER], 0,
112           handler);
113
114       g_object_unref (handler);
115     }
116
117   handler_context_data_free (data);
118 }
119
120 static void
121 sasl_handler_invalidated_cb (EmpathyServerSASLHandler *handler,
122     gpointer user_data)
123 {
124   EmpathyAuthFactory *self = user_data;
125   EmpathyAuthFactoryPriv *priv = GET_PRIV (self);
126
127   DEBUG ("SASL handler is invalidated, unref it");
128
129   tp_clear_object (&priv->sasl_handler);
130 }
131
132 static void
133 server_sasl_handler_ready_cb (GObject *source,
134     GAsyncResult *res,
135     gpointer user_data)
136 {
137   EmpathyAuthFactoryPriv *priv;
138   GError *error = NULL;
139   HandlerContextData *data = user_data;
140
141   priv = GET_PRIV (data->self);
142   priv->sasl_handler = empathy_server_sasl_handler_new_finish (res, &error);
143
144   if (error != NULL)
145     {
146       DEBUG ("Failed to create a server SASL handler; error %s",
147           error->message);
148       tp_handle_channels_context_fail (data->context, error);
149
150       g_error_free (error);
151     }
152   else
153     {
154       tp_handle_channels_context_accept (data->context);
155
156       g_signal_connect (priv->sasl_handler, "invalidated",
157           G_CALLBACK (sasl_handler_invalidated_cb), data->self);
158
159       g_signal_emit (data->self, signals[NEW_SERVER_SASL_HANDLER], 0,
160           priv->sasl_handler);
161     }
162
163   handler_context_data_free (data);
164 }
165
166 static void
167 handle_channels_cb (TpSimpleHandler *handler,
168     TpAccount *account,
169     TpConnection *connection,
170     GList *channels,
171     GList *requests_satisfied,
172     gint64 user_action_time,
173     TpHandleChannelsContext *context,
174     gpointer user_data)
175 {
176   TpChannel *channel;
177   const GError *dbus_error;
178   GError *error = NULL;
179   EmpathyAuthFactory *self = user_data;
180   EmpathyAuthFactoryPriv *priv = GET_PRIV (self);
181   HandlerContextData *data;
182   GHashTable *props;
183   const gchar * const *available_mechanisms;
184
185   DEBUG ("Handle TLS or SASL carrier channels.");
186
187   /* there can't be more than one ServerTLSConnection or
188    * ServerAuthentication channels at the same time, for the same
189    * connection/account.
190    */
191   if (g_list_length (channels) != 1)
192     {
193       g_set_error_literal (&error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
194           "Can't handle more than one ServerTLSConnection or ServerAuthentication "
195           "channel for the same connection.");
196
197       goto error;
198     }
199
200   channel = channels->data;
201
202   if (tp_channel_get_channel_type_id (channel) !=
203       EMP_IFACE_QUARK_CHANNEL_TYPE_SERVER_TLS_CONNECTION
204       && tp_channel_get_channel_type_id (channel) !=
205       TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
206     {
207       g_set_error (&error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
208           "Can only handle ServerTLSConnection or ServerAuthentication channels, "
209           "this was a %s channel", tp_channel_get_channel_type (channel));
210
211       goto error;
212     }
213
214   if (tp_channel_get_channel_type_id (channel) ==
215       TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION
216       && priv->sasl_handler != NULL)
217     {
218       g_set_error_literal (&error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
219           "Can't handle more than one ServerAuthentication channel at one time");
220
221       goto error;
222     }
223
224   props = tp_channel_borrow_immutable_properties (channel);
225   available_mechanisms = tp_asv_get_boxed (props,
226       TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
227       G_TYPE_STRV);
228
229   if (tp_channel_get_channel_type_id (channel) ==
230       TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION
231       && !tp_strv_contains (available_mechanisms, "X-TELEPATHY-PASSWORD"))
232     {
233       g_set_error_literal (&error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
234           "Only the X-TELEPATHY-PASSWORD SASL mechanism is supported");
235
236       goto error;
237     }
238
239   dbus_error = tp_proxy_get_invalidated (channel);
240
241   if (dbus_error != NULL)
242     {
243       error = g_error_copy (dbus_error);
244       goto error;
245     }
246
247   data = handler_context_data_new (self, context);
248   tp_handle_channels_context_delay (context);
249
250   /* create a handler */
251   if (tp_channel_get_channel_type_id (channel) ==
252       EMP_IFACE_QUARK_CHANNEL_TYPE_SERVER_TLS_CONNECTION)
253     {
254       empathy_server_tls_handler_new_async (channel, server_tls_handler_ready_cb,
255           data);
256     }
257   else if (tp_channel_get_channel_type_id (channel) ==
258       TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
259     {
260       empathy_server_sasl_handler_new_async (account, channel,
261           server_sasl_handler_ready_cb, data);
262     }
263   return;
264
265  error:
266   tp_handle_channels_context_fail (context, error);
267   g_clear_error (&error);
268 }
269
270 static GObject *
271 empathy_auth_factory_constructor (GType type,
272     guint n_params,
273     GObjectConstructParam *params)
274 {
275   GObject *retval;
276
277   if (auth_factory_singleton != NULL)
278     {
279       retval = g_object_ref (auth_factory_singleton);
280     }
281   else
282     {
283       retval = G_OBJECT_CLASS (empathy_auth_factory_parent_class)->constructor
284         (type, n_params, params);
285
286       auth_factory_singleton = EMPATHY_AUTH_FACTORY (retval);
287       g_object_add_weak_pointer (retval, (gpointer *) &auth_factory_singleton);
288     }
289
290   return retval;
291 }
292
293 static void
294 empathy_auth_factory_init (EmpathyAuthFactory *self)
295 {
296   EmpathyAuthFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
297       EMPATHY_TYPE_AUTH_FACTORY, EmpathyAuthFactoryPriv);
298   TpDBusDaemon *bus;
299   GError *error = NULL;
300
301   self->priv = priv;
302
303   bus = tp_dbus_daemon_dup (&error);
304   if (error != NULL)
305     {
306       g_critical ("Failed to get TpDBusDaemon: %s", error->message);
307       g_error_free (error);
308       return;
309     }
310
311   priv->handler = tp_simple_handler_new (bus, FALSE, FALSE, "Empathy.Auth",
312       FALSE, handle_channels_cb, self, NULL);
313
314   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
315           /* ChannelType */
316           TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
317           EMP_IFACE_CHANNEL_TYPE_SERVER_TLS_CONNECTION,
318           /* AuthenticationMethod */
319           TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
320           TP_HANDLE_TYPE_NONE, NULL));
321
322   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
323           /* ChannelType */
324           TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
325           TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
326           /* AuthenticationMethod */
327           TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
328           G_TYPE_STRING, TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
329           NULL));
330
331  g_object_unref (bus);
332 }
333
334 static void
335 empathy_auth_factory_dispose (GObject *object)
336 {
337   EmpathyAuthFactoryPriv *priv = GET_PRIV (object);
338
339   if (priv->dispose_run)
340     return;
341
342   priv->dispose_run = TRUE;
343
344   tp_clear_object (&priv->handler);
345   tp_clear_object (&priv->sasl_handler);
346
347   G_OBJECT_CLASS (empathy_auth_factory_parent_class)->dispose (object);
348 }
349
350 static void
351 empathy_auth_factory_class_init (EmpathyAuthFactoryClass *klass)
352 {
353   GObjectClass *oclass = G_OBJECT_CLASS (klass);
354
355   oclass->constructor = empathy_auth_factory_constructor;
356   oclass->dispose = empathy_auth_factory_dispose;
357
358   g_type_class_add_private (klass, sizeof (EmpathyAuthFactoryPriv));
359
360   signals[NEW_SERVER_TLS_HANDLER] =
361     g_signal_new ("new-server-tls-handler",
362       G_TYPE_FROM_CLASS (klass),
363       G_SIGNAL_RUN_LAST, 0,
364       NULL, NULL,
365       g_cclosure_marshal_VOID__OBJECT,
366       G_TYPE_NONE,
367       1, EMPATHY_TYPE_SERVER_TLS_HANDLER);
368
369   signals[NEW_SERVER_SASL_HANDLER] =
370     g_signal_new ("new-server-sasl-handler",
371       G_TYPE_FROM_CLASS (klass),
372       G_SIGNAL_RUN_LAST, 0,
373       NULL, NULL,
374       g_cclosure_marshal_VOID__OBJECT,
375       G_TYPE_NONE,
376       1, EMPATHY_TYPE_SERVER_SASL_HANDLER);
377 }
378
379 EmpathyAuthFactory *
380 empathy_auth_factory_dup_singleton (void)
381 {
382   return g_object_new (EMPATHY_TYPE_AUTH_FACTORY, NULL);
383 }
384
385 gboolean
386 empathy_auth_factory_register (EmpathyAuthFactory *self,
387     GError **error)
388 {
389   EmpathyAuthFactoryPriv *priv = GET_PRIV (self);
390
391   return tp_base_client_register (priv->handler, error);
392 }