]> git.0d.be Git - empathy.git/blob - libempathy/empathy-server-sasl-handler.c
Merge remote-tracking branch 'pochu/call-reuse-windows-580794'
[empathy.git] / libempathy / empathy-server-sasl-handler.c
1 /*
2  * empathy-server-sasl-handler.c - Source for EmpathyServerSASLHandler
3  * Copyright (C) 2010 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "empathy-server-sasl-handler.h"
21
22 #include <telepathy-glib/telepathy-glib.h>
23
24 #include <extensions/extensions.h>
25
26 #include <string.h>
27
28 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
29 #include "empathy-debug.h"
30 #include "empathy-keyring.h"
31
32 enum {
33   PROP_CHANNEL = 1,
34   PROP_ACCOUNT,
35   LAST_PROPERTY,
36 };
37
38 /* signal enum */
39 enum {
40   INVALIDATED,
41   LAST_SIGNAL,
42 };
43
44 static guint signals[LAST_SIGNAL] = {0};
45
46 typedef struct {
47   TpChannel *channel;
48   TpAccount *account;
49
50   GSimpleAsyncResult *result;
51
52   gchar *password;
53   gboolean save_password;
54
55   GSimpleAsyncResult *async_init_res;
56 } EmpathyServerSASLHandlerPriv;
57
58 static void async_initable_iface_init (GAsyncInitableIface *iface);
59
60 G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
61     G_TYPE_OBJECT,
62     G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
63
64 static const gchar *sasl_statuses[] = {
65   "not started",
66   "in progress",
67   "server succeeded",
68   "client accepted",
69   "succeeded",
70   "server failed",
71   "client failed",
72 };
73
74 static void
75 empathy_server_sasl_handler_set_password_cb (GObject *source,
76     GAsyncResult *result,
77     gpointer user_data)
78 {
79   GError *error = NULL;
80
81   if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
82           &error))
83     {
84       DEBUG ("Failed to set password: %s", error->message);
85       g_clear_error (&error);
86     }
87   else
88     {
89       DEBUG ("Password set successfully.");
90     }
91 }
92
93 static void
94 sasl_status_changed_cb (TpChannel *channel,
95     TpSASLStatus status,
96     const gchar *error,
97     GHashTable *details,
98     gpointer user_data,
99     GObject *weak_object)
100 {
101   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (weak_object)->priv;
102
103   /* buh boh */
104   if (status >= G_N_ELEMENTS (sasl_statuses))
105     {
106       DEBUG ("SASL status changed to unknown status");
107       return;
108     }
109
110   DEBUG ("SASL status changed to '%s'", sasl_statuses[status]);
111
112   if (status == TP_SASL_STATUS_SERVER_SUCCEEDED)
113     {
114       if (priv->save_password)
115         {
116           DEBUG ("Saving password in keyring");
117
118           empathy_keyring_set_account_password_async (priv->account,
119               priv->password, empathy_server_sasl_handler_set_password_cb,
120               NULL);
121         }
122
123       DEBUG ("Calling AcceptSASL");
124       tp_cli_channel_interface_sasl_authentication_call_accept_sasl (
125           priv->channel, -1, NULL, NULL, NULL, NULL);
126     }
127   else if (status == TP_SASL_STATUS_SUCCEEDED)
128     {
129       DEBUG ("SASL succeeded, calling Close");
130       tp_cli_channel_call_close (priv->channel, -1,
131           NULL, NULL, NULL, NULL);
132     }
133 }
134
135 static gboolean
136 empathy_server_sasl_handler_give_password (gpointer data)
137 {
138   EmpathyServerSASLHandler *self = data;
139   EmpathyServerSASLHandlerPriv *priv = self->priv;
140
141   empathy_server_sasl_handler_provide_password (self,
142       priv->password, FALSE);
143
144   return FALSE;
145 }
146
147 static void
148 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
149     GAsyncResult *result,
150     gpointer user_data)
151 {
152   EmpathyServerSASLHandlerPriv *priv;
153   const gchar *password;
154   GError *error = NULL;
155
156   priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
157
158   password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
159       result, &error);
160
161   if (password != NULL)
162     {
163       priv->password = g_strdup (password);
164
165       /* Do this in an idle so the async result will get there
166        * first. */
167       g_idle_add (empathy_server_sasl_handler_give_password, user_data);
168     }
169
170   g_simple_async_result_complete (priv->async_init_res);
171   tp_clear_object (&priv->async_init_res);
172 }
173
174 static void
175 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
176     gint io_priority,
177     GCancellable *cancellable,
178     GAsyncReadyCallback callback,
179     gpointer user_data)
180 {
181   EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
182   EmpathyServerSASLHandlerPriv *priv = self->priv;
183
184   g_assert (priv->account != NULL);
185
186   priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
187       callback, user_data, empathy_server_sasl_handler_new_async);
188
189   empathy_keyring_get_account_password_async (priv->account,
190       empathy_server_sasl_handler_get_password_async_cb, self);
191 }
192
193 static gboolean
194 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
195     GAsyncResult *res,
196     GError **error)
197 {
198   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
199           error))
200     return FALSE;
201
202   return TRUE;
203 }
204
205 static void
206 async_initable_iface_init (GAsyncInitableIface *iface)
207 {
208   iface->init_async = empathy_server_sasl_handler_init_async;
209   iface->init_finish = empathy_server_sasl_handler_init_finish;
210 }
211
212 static void
213 channel_invalidated_cb (TpProxy *proxy,
214     guint domain,
215     gint code,
216     gchar *message,
217     EmpathyServerSASLHandler *self)
218 {
219   g_signal_emit (self, signals[INVALIDATED], 0);
220 }
221
222 static void
223 empathy_server_sasl_handler_constructed (GObject *object)
224 {
225   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
226   GError *error = NULL;
227
228   tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed (
229       priv->channel, sasl_status_changed_cb, NULL, NULL, object, &error);
230
231   if (error != NULL)
232     {
233       DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
234       g_clear_error (&error);
235     }
236
237   tp_g_signal_connect_object (priv->channel, "invalidated",
238       G_CALLBACK (channel_invalidated_cb), object, 0);
239 }
240
241 static void
242 empathy_server_sasl_handler_get_property (GObject *object,
243     guint property_id,
244     GValue *value,
245     GParamSpec *pspec)
246 {
247   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
248
249   switch (property_id)
250     {
251     case PROP_CHANNEL:
252       g_value_set_object (value, priv->channel);
253       break;
254     case PROP_ACCOUNT:
255       g_value_set_object (value, priv->account);
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
259       break;
260     }
261 }
262
263 static void
264 empathy_server_sasl_handler_set_property (GObject *object,
265     guint property_id,
266     const GValue *value,
267     GParamSpec *pspec)
268 {
269   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
270
271   switch (property_id)
272     {
273     case PROP_CHANNEL:
274       priv->channel = g_value_dup_object (value);
275       break;
276     case PROP_ACCOUNT:
277       priv->account = g_value_dup_object (value);
278       break;
279     default:
280       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
281       break;
282     }
283 }
284
285 static void
286 empathy_server_sasl_handler_dispose (GObject *object)
287 {
288   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
289
290   DEBUG ("%p", object);
291
292   tp_clear_object (&priv->channel);
293   tp_clear_object (&priv->account);
294
295   G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
296 }
297
298 static void
299 empathy_server_sasl_handler_finalize (GObject *object)
300 {
301   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
302
303   DEBUG ("%p", object);
304
305   tp_clear_pointer (&priv->password, g_free);
306
307   G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
308 }
309
310 static void
311 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
312 {
313   GObjectClass *oclass = G_OBJECT_CLASS (klass);
314   GParamSpec *pspec;
315
316   oclass->constructed = empathy_server_sasl_handler_constructed;
317   oclass->get_property = empathy_server_sasl_handler_get_property;
318   oclass->set_property = empathy_server_sasl_handler_set_property;
319   oclass->dispose = empathy_server_sasl_handler_dispose;
320   oclass->finalize = empathy_server_sasl_handler_finalize;
321
322   g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
323
324   pspec = g_param_spec_object ("channel", "The TpChannel",
325       "The TpChannel this handler is supposed to handle.",
326       TP_TYPE_CHANNEL,
327       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
328   g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
329
330   pspec = g_param_spec_object ("account", "The TpAccount",
331       "The TpAccount this channel belongs to.",
332       TP_TYPE_ACCOUNT,
333       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
334   g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
335
336   signals[INVALIDATED] = g_signal_new ("invalidated",
337       G_TYPE_FROM_CLASS (klass),
338       G_SIGNAL_RUN_LAST, 0,
339       NULL, NULL,
340       g_cclosure_marshal_VOID__VOID,
341       G_TYPE_NONE, 0);
342 }
343
344 static void
345 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
346 {
347   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
348       EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
349 }
350
351 EmpathyServerSASLHandler *
352 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
353     GError **error)
354 {
355   GObject *object, *source_object;
356
357   source_object = g_async_result_get_source_object (result);
358
359   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
360       result, error);
361   g_object_unref (source_object);
362
363   if (object != NULL)
364     return EMPATHY_SERVER_SASL_HANDLER (object);
365   else
366     return NULL;
367 }
368
369 void
370 empathy_server_sasl_handler_new_async (TpAccount *account,
371     TpChannel *channel,
372     GAsyncReadyCallback callback,
373     gpointer user_data)
374 {
375   g_return_if_fail (TP_IS_ACCOUNT (account));
376   g_return_if_fail (TP_IS_CHANNEL (channel));
377   g_return_if_fail (callback != NULL);
378
379   g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
380       G_PRIORITY_DEFAULT, NULL, callback, user_data,
381       "account", account,
382       "channel", channel,
383       NULL);
384 }
385
386 static void
387 start_mechanism_with_data_cb (TpChannel *proxy,
388     const GError *error,
389     gpointer user_data,
390     GObject *weak_object)
391 {
392   if (error != NULL)
393     {
394       DEBUG ("Failed to start mechanism: %s", error->message);
395       return;
396     }
397
398   DEBUG ("Started mechanism successfully");
399 }
400
401 void
402 empathy_server_sasl_handler_provide_password (
403     EmpathyServerSASLHandler *handler,
404     const gchar *password,
405     gboolean remember)
406 {
407   EmpathyServerSASLHandlerPriv *priv;
408   GArray *array;
409   gboolean may_save_response, may_save_response_valid;
410
411   g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
412
413   priv = handler->priv;
414
415   array = g_array_sized_new (TRUE, FALSE,
416       sizeof (gchar), strlen (password));
417
418   g_array_append_vals (array, password, strlen (password));
419
420   DEBUG ("Calling StartMechanismWithData with our password");
421
422   tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
423       priv->channel, -1, "X-TELEPATHY-PASSWORD", array,
424       start_mechanism_with_data_cb, NULL, NULL, G_OBJECT (handler));
425
426   g_array_unref (array);
427
428   DEBUG ("%sremembering the password", remember ? "" : "not ");
429
430   /* determine if we are permitted to save the password locally */
431   may_save_response = tp_asv_get_boolean (
432       tp_channel_borrow_immutable_properties (priv->channel),
433       TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
434       &may_save_response_valid);
435
436   if (!may_save_response_valid)
437     {
438       DEBUG ("MaySaveResponse unknown, assuming TRUE");
439       may_save_response = TRUE;
440     }
441
442   if (remember)
443     {
444       if (may_save_response)
445         {
446           g_free (priv->password);
447
448           /* We'll save the password if we manage to connect */
449           priv->password = g_strdup (password);
450           priv->save_password = TRUE;
451         }
452       else if (tp_proxy_has_interface_by_id (priv->channel,
453             EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
454         {
455           DEBUG ("Channel implements Ch.I.CredentialsStorage");
456         }
457       else
458         {
459           DEBUG ("Asked to remember password, but doing so is not permitted");
460         }
461     }
462
463   if (!may_save_response)
464     {
465       /* delete any password present, it shouldn't be there */
466       empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
467     }
468
469   /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
470    * whether we want to remember the password */
471   if (tp_proxy_has_interface_by_id (priv->channel,
472         EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
473     {
474       emp_cli_channel_interface_credentials_storage_call_store_credentials (
475           TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
476     }
477 }
478
479 void
480 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
481 {
482   EmpathyServerSASLHandlerPriv *priv;
483
484   g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
485
486   priv = handler->priv;
487
488   DEBUG ("Cancelling SASL mechanism...");
489
490   tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
491       priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
492       "User cancelled the authentication",
493       NULL, NULL, NULL, NULL);
494 }
495
496 TpAccount *
497 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
498 {
499   EmpathyServerSASLHandlerPriv *priv;
500
501   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
502
503   priv = handler->priv;
504
505   return priv->account;
506 }
507
508 TpChannel *
509 empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
510 {
511   EmpathyServerSASLHandlerPriv *priv;
512
513   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
514
515   priv = handler->priv;
516
517   return priv->channel;
518 }
519
520 gboolean
521 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
522 {
523   EmpathyServerSASLHandlerPriv *priv;
524
525   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
526
527   priv = handler->priv;
528
529   return (priv->password != NULL);
530 }
531
532 /**
533  * empathy_server_sasl_handler_can_save_response_somewhere:
534  * @self:
535  *
536  * Returns: %TRUE if the response can be saved somewhere, either the keyring
537  *   or via Ch.I.CredentialsStorage
538  */
539 gboolean
540 empathy_server_sasl_handler_can_save_response_somewhere (
541     EmpathyServerSASLHandler *self)
542 {
543   EmpathyServerSASLHandlerPriv *priv;
544   gboolean may_save_response, may_save_response_valid;
545   gboolean has_storage_iface;
546
547   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
548
549   priv = self->priv;
550
551   /* determine if we are permitted to save the password locally */
552   may_save_response = tp_asv_get_boolean (
553       tp_channel_borrow_immutable_properties (priv->channel),
554       TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
555       &may_save_response_valid);
556
557   if (!may_save_response_valid)
558     {
559       DEBUG ("MaySaveResponse unknown, assuming TRUE");
560       may_save_response = TRUE;
561     }
562
563   has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
564       EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
565
566   return may_save_response || has_storage_iface;
567 }