]> git.0d.be Git - empathy.git/blob - libempathy/empathy-server-sasl-handler.c
include telepathy-glib.h
[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 "config.h"
21
22 #include "empathy-server-sasl-handler.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 #include "empathy-sasl-mechanisms.h"
32
33 enum {
34   PROP_CHANNEL = 1,
35   PROP_ACCOUNT,
36   LAST_PROPERTY,
37 };
38
39 /* signal enum */
40 enum {
41   AUTH_PASSWORD_FAILED,
42   INVALIDATED,
43   LAST_SIGNAL,
44 };
45
46 static guint signals[LAST_SIGNAL] = {0};
47
48 typedef struct {
49   TpChannel *channel;
50   TpAccount *account;
51
52   GSimpleAsyncResult *result;
53
54   gchar *password;
55   gboolean save_password;
56
57   GSimpleAsyncResult *async_init_res;
58 } EmpathyServerSASLHandlerPriv;
59
60 static void async_initable_iface_init (GAsyncInitableIface *iface);
61
62 G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
63     G_TYPE_OBJECT,
64     G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
65
66 static void
67 empathy_server_sasl_handler_set_password_cb (GObject *source,
68     GAsyncResult *result,
69     gpointer user_data)
70 {
71   GError *error = NULL;
72
73   if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
74           &error))
75     {
76       DEBUG ("Failed to set password: %s", error->message);
77       g_clear_error (&error);
78     }
79   else
80     {
81       DEBUG ("Password set successfully.");
82     }
83 }
84
85 static gboolean
86 empathy_server_sasl_handler_give_password (gpointer data)
87 {
88   EmpathyServerSASLHandler *self = data;
89   EmpathyServerSASLHandlerPriv *priv = self->priv;
90
91   empathy_server_sasl_handler_provide_password (self,
92       priv->password, FALSE);
93
94   return FALSE;
95 }
96
97 static void
98 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
99     GAsyncResult *result,
100     gpointer user_data)
101 {
102   EmpathyServerSASLHandlerPriv *priv;
103   const gchar *password;
104   GError *error = NULL;
105
106   priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
107
108   password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
109       result, &error);
110
111   if (password != NULL)
112     {
113       priv->password = g_strdup (password);
114
115       /* Do this in an idle so the async result will get there
116        * first. */
117       g_idle_add (empathy_server_sasl_handler_give_password, user_data);
118     }
119
120   g_simple_async_result_complete (priv->async_init_res);
121   tp_clear_object (&priv->async_init_res);
122 }
123
124 static void
125 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
126     gint io_priority,
127     GCancellable *cancellable,
128     GAsyncReadyCallback callback,
129     gpointer user_data)
130 {
131   EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
132   EmpathyServerSASLHandlerPriv *priv = self->priv;
133
134   g_assert (priv->account != NULL);
135
136   priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
137       callback, user_data, empathy_server_sasl_handler_new_async);
138
139   empathy_keyring_get_account_password_async (priv->account,
140       empathy_server_sasl_handler_get_password_async_cb, self);
141 }
142
143 static gboolean
144 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
145     GAsyncResult *res,
146     GError **error)
147 {
148   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
149           error))
150     return FALSE;
151
152   return TRUE;
153 }
154
155 static void
156 async_initable_iface_init (GAsyncInitableIface *iface)
157 {
158   iface->init_async = empathy_server_sasl_handler_init_async;
159   iface->init_finish = empathy_server_sasl_handler_init_finish;
160 }
161
162 static void
163 channel_invalidated_cb (TpProxy *proxy,
164     guint domain,
165     gint code,
166     gchar *message,
167     EmpathyServerSASLHandler *self)
168 {
169   g_signal_emit (self, signals[INVALIDATED], 0);
170 }
171
172 static void
173 empathy_server_sasl_handler_constructed (GObject *object)
174 {
175   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
176   GError *error = NULL;
177
178   if (error != NULL)
179     {
180       DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
181       g_clear_error (&error);
182     }
183
184   tp_g_signal_connect_object (priv->channel, "invalidated",
185       G_CALLBACK (channel_invalidated_cb), object, 0);
186 }
187
188 static void
189 empathy_server_sasl_handler_get_property (GObject *object,
190     guint property_id,
191     GValue *value,
192     GParamSpec *pspec)
193 {
194   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
195
196   switch (property_id)
197     {
198     case PROP_CHANNEL:
199       g_value_set_object (value, priv->channel);
200       break;
201     case PROP_ACCOUNT:
202       g_value_set_object (value, priv->account);
203       break;
204     default:
205       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
206       break;
207     }
208 }
209
210 static void
211 empathy_server_sasl_handler_set_property (GObject *object,
212     guint property_id,
213     const GValue *value,
214     GParamSpec *pspec)
215 {
216   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
217
218   switch (property_id)
219     {
220     case PROP_CHANNEL:
221       priv->channel = g_value_dup_object (value);
222       break;
223     case PROP_ACCOUNT:
224       priv->account = g_value_dup_object (value);
225       break;
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
228       break;
229     }
230 }
231
232 static void
233 empathy_server_sasl_handler_dispose (GObject *object)
234 {
235   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
236
237   DEBUG ("%p", object);
238
239   tp_clear_object (&priv->channel);
240   tp_clear_object (&priv->account);
241
242   G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
243 }
244
245 static void
246 empathy_server_sasl_handler_finalize (GObject *object)
247 {
248   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
249
250   DEBUG ("%p", object);
251
252   tp_clear_pointer (&priv->password, g_free);
253
254   G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
255 }
256
257 static void
258 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
259 {
260   GObjectClass *oclass = G_OBJECT_CLASS (klass);
261   GParamSpec *pspec;
262
263   oclass->constructed = empathy_server_sasl_handler_constructed;
264   oclass->get_property = empathy_server_sasl_handler_get_property;
265   oclass->set_property = empathy_server_sasl_handler_set_property;
266   oclass->dispose = empathy_server_sasl_handler_dispose;
267   oclass->finalize = empathy_server_sasl_handler_finalize;
268
269   g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
270
271   pspec = g_param_spec_object ("channel", "The TpChannel",
272       "The TpChannel this handler is supposed to handle.",
273       TP_TYPE_CHANNEL,
274       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
275   g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
276
277   pspec = g_param_spec_object ("account", "The TpAccount",
278       "The TpAccount this channel belongs to.",
279       TP_TYPE_ACCOUNT,
280       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
281   g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
282
283   signals[AUTH_PASSWORD_FAILED] = g_signal_new ("auth-password-failed",
284       G_TYPE_FROM_CLASS (klass),
285       G_SIGNAL_RUN_LAST, 0,
286       NULL, NULL,
287       g_cclosure_marshal_generic,
288       G_TYPE_NONE, 1, G_TYPE_STRING);
289
290   signals[INVALIDATED] = g_signal_new ("invalidated",
291       G_TYPE_FROM_CLASS (klass),
292       G_SIGNAL_RUN_LAST, 0,
293       NULL, NULL,
294       g_cclosure_marshal_generic,
295       G_TYPE_NONE, 0);
296 }
297
298 static void
299 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
300 {
301   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
302       EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
303 }
304
305 EmpathyServerSASLHandler *
306 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
307     GError **error)
308 {
309   GObject *object, *source_object;
310
311   source_object = g_async_result_get_source_object (result);
312
313   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
314       result, error);
315   g_object_unref (source_object);
316
317   if (object != NULL)
318     return EMPATHY_SERVER_SASL_HANDLER (object);
319   else
320     return NULL;
321 }
322
323 void
324 empathy_server_sasl_handler_new_async (TpAccount *account,
325     TpChannel *channel,
326     GAsyncReadyCallback callback,
327     gpointer user_data)
328 {
329   g_return_if_fail (TP_IS_ACCOUNT (account));
330   g_return_if_fail (TP_IS_CHANNEL (channel));
331   g_return_if_fail (callback != NULL);
332
333   g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
334       G_PRIORITY_DEFAULT, NULL, callback, user_data,
335       "account", account,
336       "channel", channel,
337       NULL);
338 }
339
340 static void
341 auth_cb (GObject *source,
342     GAsyncResult *result,
343     gpointer user_data)
344 {
345   EmpathyServerSASLHandler *self = user_data;
346   EmpathyServerSASLHandlerPriv *priv = self->priv;
347   GError *error = NULL;
348
349   if (!empathy_sasl_auth_finish (priv->channel, result, &error))
350     {
351       if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED))
352         {
353           g_signal_emit (self, signals[AUTH_PASSWORD_FAILED], 0, priv->password);
354         }
355       g_clear_error (&error);
356     }
357   else
358     {
359       DEBUG ("Saving password in keyring");
360       empathy_keyring_set_account_password_async (priv->account,
361           priv->password, priv->save_password,
362           empathy_server_sasl_handler_set_password_cb,
363           NULL);
364     }
365
366   tp_channel_close_async (priv->channel, NULL, NULL);
367   g_object_unref (self);
368 }
369
370 static gboolean
371 channel_has_may_save_response (TpChannel *channel)
372 {
373   /* determine if we are permitted to save the password locally */
374   GVariant *props;
375   gboolean may_save_response;
376
377   props = tp_channel_dup_immutable_properties (channel);
378
379   if (!g_variant_lookup (props,
380         TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
381         "b", &may_save_response))
382     {
383       DEBUG ("MaySaveResponse unknown, assuming TRUE");
384       may_save_response = TRUE;
385     }
386
387   g_variant_unref (props);
388   return may_save_response;
389 }
390
391 void
392 empathy_server_sasl_handler_provide_password (
393     EmpathyServerSASLHandler *handler,
394     const gchar *password,
395     gboolean remember)
396 {
397   EmpathyServerSASLHandlerPriv *priv;
398   gboolean may_save_response;
399
400   g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
401
402   priv = handler->priv;
403
404   empathy_sasl_auth_password_async (priv->channel, password,
405       auth_cb, g_object_ref (handler));
406
407   DEBUG ("%sremembering the password", remember ? "" : "not ");
408
409   may_save_response = channel_has_may_save_response (priv->channel);
410
411   if (remember)
412     {
413       if (may_save_response)
414         {
415           g_free (priv->password);
416
417           /* We'll save the password if we manage to connect */
418           priv->password = g_strdup (password);
419           priv->save_password = TRUE;
420         }
421       else if (tp_proxy_has_interface_by_id (priv->channel,
422             EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
423         {
424           DEBUG ("Channel implements Ch.I.CredentialsStorage");
425         }
426       else
427         {
428           DEBUG ("Asked to remember password, but doing so is not permitted");
429         }
430     }
431
432   if (!may_save_response)
433     {
434       /* delete any password present, it shouldn't be there */
435       empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
436     }
437
438   /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
439    * whether we want to remember the password */
440   if (tp_proxy_has_interface_by_id (priv->channel,
441         EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
442     {
443       emp_cli_channel_interface_credentials_storage_call_store_credentials (
444           TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
445     }
446 }
447
448 void
449 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
450 {
451   EmpathyServerSASLHandlerPriv *priv;
452
453   g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
454
455   priv = handler->priv;
456
457   DEBUG ("Cancelling SASL mechanism...");
458
459   tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
460       priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
461       "User cancelled the authentication",
462       NULL, NULL, NULL, NULL);
463 }
464
465 TpAccount *
466 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
467 {
468   EmpathyServerSASLHandlerPriv *priv;
469
470   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
471
472   priv = handler->priv;
473
474   return priv->account;
475 }
476
477 TpChannel *
478 empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
479 {
480   EmpathyServerSASLHandlerPriv *priv;
481
482   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
483
484   priv = handler->priv;
485
486   return priv->channel;
487 }
488
489 gboolean
490 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
491 {
492   EmpathyServerSASLHandlerPriv *priv;
493
494   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
495
496   priv = handler->priv;
497
498   return (priv->password != NULL);
499 }
500
501 /**
502  * empathy_server_sasl_handler_can_save_response_somewhere:
503  * @self:
504  *
505  * Returns: %TRUE if the response can be saved somewhere, either the keyring
506  *   or via Ch.I.CredentialsStorage
507  */
508 gboolean
509 empathy_server_sasl_handler_can_save_response_somewhere (
510     EmpathyServerSASLHandler *self)
511 {
512   EmpathyServerSASLHandlerPriv *priv;
513   gboolean may_save_response;
514   gboolean has_storage_iface;
515
516   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
517
518   priv = self->priv;
519
520   may_save_response = channel_has_may_save_response (priv->channel);
521
522   has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
523       EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
524
525   return may_save_response || has_storage_iface;
526 }