aboutsummaryrefslogtreecommitdiff
path: root/src/backend-dbus/users.c
blob: 1db5ac17649888052ed70318bebc74fa3740c588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
 * Copyright 2013 Canonical Ltd.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "dbus-user.h"

#include "users.h"

struct _IndicatorSessionUsersDbusPriv
{
  Login1Manager * login1_manager;
  Login1Seat * login1_seat;
  DisplayManagerSeat * dm_seat;
  Accounts * accounts;

  /* hash table of int uids to AccountsUser* */
  GHashTable * uid_to_account;

  /* a hashset of int uids of users who are logged in */
  GHashTable * logins;

  /* the user-id of the owner of the active session */
  guint active_uid;

  /* true if this is a live session */
  gboolean is_live;

  GCancellable * cancellable;
};

typedef IndicatorSessionUsersDbusPriv priv_t;

G_DEFINE_TYPE (IndicatorSessionUsersDbus,
               indicator_session_users_dbus,
               INDICATOR_TYPE_SESSION_USERS)

/***
****
***/

/* returns true if the fields indicate this is the 'live cd' user */
static gboolean
is_live_user (guint uid, const char * username)
{
  return uid==999 && !g_strcmp0 (username, "ubuntu");
}

/* returns true if this is a user who should be listed in the session indicator */
static gboolean
is_public_user (IndicatorSessionUsersDbus * self G_GNUC_UNUSED,
                AccountsUser              * au)
{
  gboolean is_public;

  /* the 'live session' user is the only system account we'll show */
  if (is_live_user (accounts_user_get_uid(au), accounts_user_get_user_name(au)))
    is_public = TRUE;
  else
    is_public = !accounts_user_get_system_account (au);

  return is_public;
}

/* get our private org.freedesktop.Accounts.User proxy for the given uid */
static AccountsUser *
get_user_for_uid (IndicatorSessionUsersDbus * self, guint uid)
{
  priv_t * p = self->priv;

  return g_hash_table_lookup (p->uid_to_account, GUINT_TO_POINTER(uid));
}

/* returns true if this is a uid that should be listed in the session indicator */
static gboolean
is_public_uid (IndicatorSessionUsersDbus * self, guint uid)
{
  AccountsUser * au;
  gboolean is_public;

  if ((au = get_user_for_uid (self, uid)))
    {
      is_public = is_public_user (self, au);
    }
  else
    {
      /* making a guess to serve until the user's proxy object is ready.
         assuming that 999 is a 'live session' user, <999 is system account */
      is_public = uid >= 999;
    }

  return is_public;
}

/***
****
***/

static void
emit_user_added (IndicatorSessionUsersDbus * self, guint uid)
{
  if (is_public_uid (self, uid))
    indicator_session_users_added (INDICATOR_SESSION_USERS(self), uid);
}

static void
emit_user_changed (IndicatorSessionUsersDbus * self, guint uid)
{
  if (is_public_uid (self, uid))
    indicator_session_users_changed (INDICATOR_SESSION_USERS(self), uid);
}

static void
emit_user_removed (IndicatorSessionUsersDbus * self, guint uid)
{
  if (is_public_uid (self, uid))
    indicator_session_users_removed (INDICATOR_SESSION_USERS(self), uid);
}

/***
****
***/

static void
set_is_live_session_flag (IndicatorSessionUsersDbus * self, gboolean b)
{
  priv_t * p = self->priv;

  if (p->is_live != b)
    {
      p->is_live = b;

      indicator_session_users_notify_is_live_session (INDICATOR_SESSION_USERS (self));
    }
}

static void
set_active_uid (IndicatorSessionUsersDbus * self, guint uid)
{
  priv_t * p = self->priv;

  if (p->active_uid != uid)
    {
      const guint old_uid = p->active_uid;

      p->active_uid = uid;

      emit_user_changed (self, old_uid);
      emit_user_changed (self, uid);
    }
}

static void
set_logins (IndicatorSessionUsersDbus * self, GHashTable * logins)
{
  GHashTable * old_logins = self->priv->logins;
  gpointer uid;
  GHashTableIter iter;

  self->priv->logins = logins;

  /* fire 'user changed' event for users who logged out */
  g_hash_table_iter_init (&iter, old_logins);
  while ((g_hash_table_iter_next (&iter, &uid, NULL)))
    if (!g_hash_table_contains (logins, uid))
      emit_user_changed (self, GPOINTER_TO_UINT(uid));

  /* fire 'user changed' event for users who logged in */
  g_hash_table_iter_init (&iter, logins);
  while ((g_hash_table_iter_next (&iter, &uid, NULL)))
    if (!g_hash_table_contains (old_logins, uid))
      emit_user_changed (self, GPOINTER_TO_UINT(uid));

  g_hash_table_destroy (old_logins);
}

/***
****
***/

static GQuark
get_connection_list_quark (void)
{
  static GQuark q = 0;

  if (G_UNLIKELY (q == 0))
    q = g_quark_from_static_string ("connection-ids");

  return q;
}

static void
object_unref_and_disconnect (gpointer instance)
{
  GSList * l;
  GSList * ids;
  const GQuark q = get_connection_list_quark ();

  ids = g_object_steal_qdata (G_OBJECT(instance), q);
  for (l=ids; l!=NULL; l=l->next)
    {
      gulong * handler_id = l->data;
      g_signal_handler_disconnect (instance, *handler_id);
      g_free (handler_id);
      g_object_unref (instance);
    }

  g_slist_free (ids);
}

static void
object_add_connection (GObject * o, gulong connection_id)
{
  const GQuark q = get_connection_list_quark ();
  GSList * ids;
  gulong * ptr;

  ptr = g_new (gulong, 1);
  *ptr = connection_id;

  ids = g_object_steal_qdata (o, q);
  ids = g_slist_prepend (ids, ptr);
  g_object_set_qdata (o, q, ids);
}

/***
****  User Account Tracking
***/

static void create_user_proxy_for_path (IndicatorSessionUsersDbus *, const char *);

/* called when a user proxy gets the 'Changed' signal */
static void
on_user_changed (AccountsUser * user, gpointer gself)
{
  /* Accounts.User doesn't update properties in the standard way,
   * so create a new proxy to pull in the new properties.
   * The older proxy is freed when it's replaced in our accounts hash */
  const char * path = g_dbus_proxy_get_object_path (G_DBUS_PROXY(user));
  create_user_proxy_for_path (gself, path);
}

static void
track_user (IndicatorSessionUsersDbus * self,
            AccountsUser              * user)
{
  const guint32 uid = accounts_user_get_uid (user);
  const gpointer uid_key = GUINT_TO_POINTER (uid);
  priv_t * p = self->priv;
  gulong id;
  gboolean already_had_user;

  g_return_if_fail (is_public_user (self, user));

  already_had_user = g_hash_table_contains (p->uid_to_account, uid_key);

  id  = g_signal_connect (user, "changed", G_CALLBACK(on_user_changed), self);
  object_add_connection (G_OBJECT(user), id);
  g_hash_table_insert (p->uid_to_account, uid_key, user);

  if (already_had_user)
    emit_user_changed (self, uid);
  else
    emit_user_added (self, uid);
}

static void
untrack_user (IndicatorSessionUsersDbus * self,
              const gchar               * path)
{
  guint uid;
  gpointer key;
  gpointer val;
  GHashTableIter iter;
  priv_t * p = self->priv;

  uid = 0;
  g_hash_table_iter_init (&iter, p->uid_to_account);
  while (!uid && g_hash_table_iter_next (&iter, &key, &val))
    if (!g_strcmp0 (path, g_dbus_proxy_get_object_path (val)))
      uid = GPOINTER_TO_UINT (key);

  if (uid)
    {
      g_hash_table_remove (p->uid_to_account, GUINT_TO_POINTER(uid)); 

      emit_user_removed (self, uid);
    }
}

/* We got a new org.freedesktop.Accounts.User proxy.
   If it's one we want to remember, pass it to track_user() */
static void
on_user_proxy_ready (GObject       * o G_GNUC_UNUSED,
                     GAsyncResult  * res,
                     gpointer        self)
{
  GError * err;
  AccountsUser * user;

  err = NULL;
  user = accounts_user_proxy_new_for_bus_finish (res, &err);
  if (err != NULL)
    {
      if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s: %s", G_STRFUNC, err->message);

      g_error_free (err);
    }
  else if (is_public_user (self, user))
    {
      track_user (self, user);
    }
  else
    {
      g_object_unref (self);
    }
}

static void
create_user_proxy_for_path (IndicatorSessionUsersDbus * self,
                            const char                * path)
{
  accounts_user_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                                   G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
                                   "org.freedesktop.Accounts",
                                   path,
                                   self->priv->cancellable,
                                   on_user_proxy_ready, self);
}

/* create proxy objects for everything in Account's user-list */
static void
on_user_list_ready (GObject * o, GAsyncResult * res, gpointer gself)
{
  GError * err;
  gchar ** paths;

  err = NULL;
  paths = NULL;
  accounts_call_list_cached_users_finish (ACCOUNTS(o), &paths, res, &err);
  if (err != NULL)
    {
      if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, err->message);

      g_error_free (err);
    }
  else
    {
      int i;

      for (i=0; paths && paths[i]; ++i)
        create_user_proxy_for_path (gself, paths[i]);

      g_strfreev (paths);
    }
}

static void
set_account_manager (IndicatorSessionUsersDbus * self, Accounts * a)
{
  priv_t * p = self->priv;

  if (p->accounts != NULL)
    {
      g_signal_handlers_disconnect_by_data (p->accounts, self);
      g_clear_object (&p->accounts);
    }

  if (a != NULL)
    {
      p->accounts = g_object_ref (a);

      accounts_call_list_cached_users (a,
                                       self->priv->cancellable,
                                       on_user_list_ready,
                                       self);

      g_signal_connect_swapped (a, "user-added",
                                G_CALLBACK(create_user_proxy_for_path), self);

      g_signal_connect_swapped (a, "user-deleted",
                                G_CALLBACK(untrack_user), self);
    }
}

/***
****
***/

/* Based on the login1 manager's list of current sessions,
   update our 'logins', 'is_live', and 'active_uid' fields */
static void
on_login1_manager_session_list_ready (GObject      * o,
                                      GAsyncResult * res,
                                      gpointer       gself)
{
  GVariant * sessions;
  GError * err;

  sessions = NULL;
  err = NULL;
  login1_manager_call_list_sessions_finish (LOGIN1_MANAGER(o),
                                            &sessions,
                                            res,
                                            &err);

  if (err != NULL)
    {
      if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s: %s", G_STRFUNC, err->message);

      g_error_free (err);
    }
  else
    {
      const gchar * const current_seat_id = g_getenv ("XDG_SEAT");
      const gchar * const current_session_id = g_getenv ("XDG_SESSION_ID");
      IndicatorSessionUsersDbus * self = INDICATOR_SESSION_USERS_DBUS (gself);
      const gchar * session_id = NULL;
      guint32 uid = 0;
      const gchar * user_name = NULL;
      const gchar * seat_id = NULL;
      const gchar * path = NULL;
      gboolean is_live_session = FALSE;
      GHashTable * logins = g_hash_table_new (g_direct_hash, g_direct_equal);
      GVariantIter iter;

      g_variant_iter_init (&iter, sessions);
      while (g_variant_iter_loop (&iter, "(&su&s&s&o)", &session_id,
                                                        &uid,
                                                        &user_name,
                                                        &seat_id,
                                                        &path))
        {
          /* only track sessions on our seat */
          if (g_strcmp0 (seat_id, current_seat_id))
            continue;

          if (!g_strcmp0 (session_id, current_session_id))
            {
              set_active_uid (self, uid);

              if (is_live_user (uid, user_name))
                is_live_session = TRUE;
            }

          if (is_public_uid (self, uid))
            g_hash_table_add (logins, GINT_TO_POINTER(uid));
        }

      set_is_live_session_flag (self, is_live_session);
      set_logins (self, logins);

      g_variant_unref (sessions);
    }
}

static void
update_session_list (IndicatorSessionUsersDbus * self)
{
  priv_t * p = self->priv;

  if (p->login1_manager != NULL)
    {
      login1_manager_call_list_sessions (p->login1_manager,
                                         p->cancellable,
                                         on_login1_manager_session_list_ready,
                                         self);
    }
}

static void
set_login1_manager (IndicatorSessionUsersDbus * self, Login1Manager * login1_manager)
{
  priv_t * p = self->priv;

  if (p->login1_manager != NULL)
    {
      g_signal_handlers_disconnect_by_data (p->login1_manager, self);

      g_clear_object (&p->login1_manager);
    }

  if (login1_manager != NULL)
    {
      p->login1_manager = g_object_ref (login1_manager);

      g_signal_connect_swapped (login1_manager, "session-new",
                                G_CALLBACK(update_session_list), self);
      g_signal_connect_swapped (login1_manager, "session-removed",
                                G_CALLBACK(update_session_list), self);
      update_session_list (self);
    }
}

static void
set_login1_seat (IndicatorSessionUsersDbus * self,
                 Login1Seat                * login1_seat)
{
  priv_t * p = self->priv;

  if (p->login1_seat != NULL)
    {
      g_signal_handlers_disconnect_by_data (p->login1_seat, self);

      g_clear_object (&p->login1_seat);
    }

  if (login1_seat != NULL)
    {
      p->login1_seat = g_object_ref (login1_seat);

      g_signal_connect_swapped (login1_seat, "notify::active-session",
                                G_CALLBACK(update_session_list), self);
      update_session_list (self);
    }
}

static void
set_display_manager_seat (IndicatorSessionUsersDbus * self,
                          DisplayManagerSeat        * dm_seat)
{
  priv_t * p = self->priv;

  g_clear_object (&p->dm_seat);

  if (dm_seat != NULL)
    p->dm_seat = g_object_ref (dm_seat);
}

/***
****  IndicatorSessionUsers virtual functions
***/

/* switch to (or create) a session for the specified user */
static void
my_activate_user (IndicatorSessionUsers * users, guint uid)
{
  IndicatorSessionUsersDbus * self = INDICATOR_SESSION_USERS_DBUS(users);
  priv_t * p = self->priv;
  AccountsUser * au;
  const char * username;

  au = get_user_for_uid (self, uid);
  username = au ? accounts_user_get_user_name (au) : NULL;

  if (!username)
    {
      g_warning ("%s %s can't find user '%u'", G_STRLOC, G_STRFUNC, uid);
    }
  else
    {
      g_return_if_fail (p->dm_seat != NULL);

      display_manager_seat_call_switch_to_user (p->dm_seat,
                                                username,
                                                "",
                                                p->cancellable,
                                                NULL,
                                                NULL);
    }
}

/* returns true if this is a live session */
static gboolean
my_is_live_session (IndicatorSessionUsers * users)
{
  g_return_val_if_fail (INDICATOR_IS_SESSION_USERS_DBUS(users), FALSE);

  return INDICATOR_SESSION_USERS_DBUS(users)->priv->is_live;
}

/* get a list of our user ids */
static GList *
my_get_uids (IndicatorSessionUsers * users)
{
  IndicatorSessionUsersDbus * self = INDICATOR_SESSION_USERS_DBUS (users);
  return g_hash_table_get_keys (self->priv->uid_to_account);
}

/* build a new struct populated with info on the specified user */
static IndicatorSessionUser *
my_get_user (IndicatorSessionUsers * users, guint uid)
{
  IndicatorSessionUsersDbus * self = INDICATOR_SESSION_USERS_DBUS (users);
  priv_t * p = self->priv;
  IndicatorSessionUser * ret;
  AccountsUser * au;

  ret = NULL;
  au = get_user_for_uid (self, uid);
  if (au && !accounts_user_get_system_account(au))
    {
      g_assert (uid == accounts_user_get_uid (au));

      ret = g_new0 (IndicatorSessionUser, 1);
      ret->uid = uid;
      ret->user_name = g_strdup (accounts_user_get_user_name (au));
      ret->real_name = g_strdup (accounts_user_get_real_name (au));
      ret->icon_file = g_strdup (accounts_user_get_icon_file (au));
      ret->login_frequency = accounts_user_get_login_frequency (au);
      ret->is_logged_in = g_hash_table_contains (p->logins, GINT_TO_POINTER(uid));
      ret->is_current_user = uid == p->active_uid;
    }

  return ret;
}

/***
****  GObject virtual functions
***/

static void
my_dispose (GObject * o)
{
  IndicatorSessionUsersDbus * self = INDICATOR_SESSION_USERS_DBUS (o);
  priv_t * p = self->priv;

  if (p->cancellable)
    {
      g_cancellable_cancel (p->cancellable);
      g_clear_object (&p->cancellable);
    }

  set_account_manager (self, NULL);
  set_display_manager_seat (self, NULL);
  set_login1_seat (self, NULL);
  set_login1_manager (self, NULL);

  g_hash_table_remove_all (p->uid_to_account);

  G_OBJECT_CLASS (indicator_session_users_dbus_parent_class)->dispose (o);
}

static void
my_finalize (GObject * o)
{
  IndicatorSessionUsersDbus * self = INDICATOR_SESSION_USERS_DBUS (o);
  priv_t * p = self->priv;

  g_hash_table_destroy (p->logins);
  g_hash_table_destroy (p->uid_to_account);

  G_OBJECT_CLASS (indicator_session_users_dbus_parent_class)->finalize (o);
}

static void
indicator_session_users_dbus_class_init (IndicatorSessionUsersDbusClass * klass)
{
  GObjectClass * object_class;
  IndicatorSessionUsersClass * users_class;

  object_class = G_OBJECT_CLASS (klass);
  object_class->dispose = my_dispose;
  object_class->finalize = my_finalize;

  users_class = INDICATOR_SESSION_USERS_CLASS (klass);
  users_class->is_live_session = my_is_live_session;
  users_class->get_uids = my_get_uids;
  users_class->get_user = my_get_user;
  users_class->activate_user = my_activate_user;

  g_type_class_add_private (klass, sizeof (IndicatorSessionUsersDbusPriv));
}

static void
indicator_session_users_dbus_init (IndicatorSessionUsersDbus * self)
{
  priv_t * p;

  p = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                   INDICATOR_TYPE_SESSION_USERS_DBUS,
                                   IndicatorSessionUsersDbusPriv);
  self->priv = p;
  p->cancellable = g_cancellable_new ();

  p->uid_to_account = g_hash_table_new_full (g_direct_hash,
                                             g_direct_equal,
                                             NULL,
                                             object_unref_and_disconnect);

  p->logins = g_hash_table_new (g_direct_hash, g_direct_equal);
}

/***
****  Public
***/

IndicatorSessionUsers *
indicator_session_users_dbus_new (void)
{
  gpointer o = g_object_new (INDICATOR_TYPE_SESSION_USERS_DBUS, NULL);

  return INDICATOR_SESSION_USERS (o);
}

void
indicator_session_users_dbus_set_proxies (IndicatorSessionUsersDbus * self,
                                          Login1Manager             * login1_manager,
                                          Login1Seat                * login1_seat,
                                          DisplayManagerSeat        * dm_seat,
                                          Accounts                  * accounts)
{
  g_return_if_fail (INDICATOR_IS_SESSION_USERS_DBUS (self));

  set_login1_manager (self, login1_manager);
  set_login1_seat (self, login1_seat);
  set_display_manager_seat (self, dm_seat);
  set_account_manager (self, accounts);
}