summaryrefslogtreecommitdiff
path: root/src/mcd-account-manager-default.c
blob: ef39ba16b6b01e7bbf3aece924f4c9f32f6a39be (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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
 * The default account manager keyfile storage pseudo-plugin
 *
 * Copyright © 2010 Nokia Corporation
 * Copyright © 2010 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <errno.h>
#include <string.h>

#include <glib/gstdio.h>

#include <telepathy-glib/telepathy-glib.h>

#include "mcd-account-manager-default.h"
#include "mcd-debug.h"
#include "mcd-misc.h"

#define PLUGIN_NAME "default-gkeyfile"
#define PLUGIN_PRIORITY MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_DEFAULT
#define PLUGIN_DESCRIPTION "GKeyFile (default) account storage backend"
#define INITIAL_CONFIG "# Telepathy accounts\n"

#if ENABLE_GNOME_KEYRING
#include <gnome-keyring.h>

GnomeKeyringPasswordSchema keyring_schema =
  { GNOME_KEYRING_ITEM_GENERIC_SECRET,
    { { "account", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
      { "param",   GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
      { NULL,      0 } } };

typedef struct
{
  gchar *account;
  gchar *name;
  gboolean set;
} KeyringSetData;

static void
_keyring_set_cb (GnomeKeyringResult result,
    gpointer data)
{
  KeyringSetData *ksd = data;

  if (result != GNOME_KEYRING_RESULT_OK)
    g_warning ("failed to save %s.%s : %s", ksd->account, ksd->name,
        gnome_keyring_result_to_message (result));
  else
    DEBUG ("%s %s.%s in gnome keyring",
        ksd->set ? "saved" : "deleted",
        ksd->account,
        ksd->name);

  g_free (ksd->account);
  g_free (ksd->name);
  g_slice_free(KeyringSetData, ksd);
}

static void
_delete_from_keyring (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account,
    const gchar *key)
{
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);

  if (key == NULL)
    {
      /* flag the whole account as purged */
      gchar *removed = g_strdup (account);
      g_hash_table_replace (amd->removed_accounts, removed, removed);
      g_key_file_remove_group (amd->secrets, removed, NULL);
    }
  else
    {
      /* remember to forget this one param */
      g_key_file_set_value (amd->removed, account, key, "");
      g_key_file_remove_key (amd->secrets, account, key, NULL);
    }
}

static void
_keyring_remove_account (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account)
{
  GList *i;
  GList *items;
  GnomeKeyringAttributeList *match = gnome_keyring_attribute_list_new ();
  GnomeKeyringResult ok;

  gnome_keyring_attribute_list_append_string (match, "account", account);

  ok = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET,
      match, &items);

  if (ok != GNOME_KEYRING_RESULT_OK)
    goto finished;

  for (i = items; i != NULL; i = g_list_next (i))
    {
      GnomeKeyringFound *found = i->data;
      gnome_keyring_item_delete_sync (found->keyring, found->item_id);
    }

 finished:
  gnome_keyring_attribute_list_free (match);
}

static void
_keyring_commit_one (const McdAccountManagerDefault *amd,
    const McpAccountManager *am,
    const gchar *account_name)
{
  gsize j;
  gsize k;
  GStrv keys = g_key_file_get_keys (amd->secrets, account_name, &k, NULL);

  if (keys == NULL)
    k = 0;

  for (j = 0; j < k; j++)
    {
      gchar *name = g_strdup_printf ("account: %s; param: %s",
          account_name, keys[j]);
      gchar *val = g_key_file_get_value (amd->secrets,
          account_name, keys[j], NULL);
      gchar *key = keys[j];
      KeyringSetData *ksd = g_slice_new0 (KeyringSetData);

      /* for compatibility with old gnome keyring code we must strip  *
       * the param- prefix from the name before saving to the keyring */
      if (g_str_has_prefix (key, "param-"))
        key += strlen ("param-");

      ksd->account = g_strdup (account_name);
      ksd->name = g_strdup (keys[j]);
      ksd->set = TRUE;

      gnome_keyring_store_password (&keyring_schema, NULL,
          name, val, _keyring_set_cb, ksd, NULL,
          "account", account_name,
          "param", key,
          NULL);

      g_free (val);
      g_free (name);
    }

  g_strfreev (keys);
}

static void
_keyring_commit (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account_name)
{
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);
  gsize n;
  gsize i;
  GStrv accts;
  GHashTableIter iter = { 0 };
  gchar *account = NULL;

  if (!gnome_keyring_is_available ())
    return;

  /* purge any entirely removed accounts */
  g_hash_table_iter_init (&iter, amd->removed_accounts);

  while (g_hash_table_iter_next (&iter, (gpointer *) &account, NULL))
    _keyring_remove_account (self, am, (gchar *) account);

  g_hash_table_remove_all (amd->removed_accounts);

  /* purge deleted parameters for remaining accounts */
  accts = g_key_file_get_groups (amd->removed, &n);

  for (i = 0; i < n; i++)
    {
      gsize j;
      gsize k;
      GStrv keys = g_key_file_get_keys (amd->removed, accts[i], &k, NULL);

      if (keys == NULL)
        k = 0;

      for (j = 0; j < k; j++)
        {
          KeyringSetData *ksd = g_slice_new0 (KeyringSetData);
          const gchar *key = keys[j];

          /* for compatibility with old gnome keyring code we must strip  *
           * the param- prefix from the name before saving to the keyring */
          if (g_str_has_prefix (key, "param-"))
            key += strlen ("param-");

          ksd->account = g_strdup (accts[i]);
          ksd->name = g_strdup (key);
          ksd->set = FALSE;

          gnome_keyring_delete_password (&keyring_schema,
              _keyring_set_cb, ksd, NULL,
              "account", accts[i],
              "param", key,
              NULL);
        }

      g_strfreev (keys);
    }

  g_strfreev (accts);

  /* forget about all the purged params completely */
  g_key_file_load_from_data (amd->removed, "#\n", -1, 0, NULL);

  if (account_name == NULL)
    {
      /* ok, write out the values for all the accounts we have: */
      accts = g_key_file_get_groups (amd->secrets, &n);

      for (i = 0; i < n; i++)
        _keyring_commit_one (amd, am, accts[i]);

      g_strfreev (accts);
    }
  else
    {
      _keyring_commit_one (amd, am, account_name);
    }
}

static void
_get_secrets_from_keyring (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account)
{
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);
  GnomeKeyringResult ok = GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON;
  GnomeKeyringAttributeList *match = gnome_keyring_attribute_list_new ();
  GList *items = NULL;
  GList *i;

  gnome_keyring_attribute_list_append_string (match, "account", account);

  ok = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET,
      match, &items);

  if (ok != GNOME_KEYRING_RESULT_OK)
    goto finished;

  for (i = items; i != NULL; i = g_list_next (i))
    {
      gsize j;
      GnomeKeyringFound *entry = i->data;
      GnomeKeyringAttributeList *data = entry->attributes;

      for (j = 0; j < data->len; j++)
        {
          GnomeKeyringAttribute *attr =
            &(gnome_keyring_attribute_list_index (data, j));
          const gchar *name = attr->name;
          const gchar *value = NULL;
          const gchar *param = NULL;

          switch (attr->type)
            {
              case GNOME_KEYRING_ATTRIBUTE_TYPE_STRING:
                if (g_strcmp0 ("param", name) == 0)
                  {
                    param = attr->value.string;
                    value = entry->secret;
                  }
                break;

              default:
                g_warning ("Unsupported value type for %s.%s", account, name);
            }

          if (!tp_strdiff (param, "password"))
            {
              /* Empathy 3.0 was meant to migrate passwords from MC to
               * itself, but it couldn't complete the migration by
               * deleting the password from MC, because MC had several
               * bugs that meant deleting passwords didn't work. To atone
               * for our past sins, detect an incomplete migration and
               * complete it. */
              GnomeKeyringResult empathy_ok =
                GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON;
              GnomeKeyringAttributeList *empathy_match =
                gnome_keyring_attribute_list_new ();
              GList *empathy_items = NULL;

              gnome_keyring_attribute_list_append_string (empathy_match,
                  "account-id", account);
              gnome_keyring_attribute_list_append_string (empathy_match,
                  "param-name", "password");

              empathy_ok = gnome_keyring_find_items_sync (
                  GNOME_KEYRING_ITEM_GENERIC_SECRET, empathy_match,
                  &empathy_items);

              if (empathy_ok == GNOME_KEYRING_RESULT_OK &&
                  empathy_items != NULL)
                {
                  KeyringSetData *ksd = g_slice_new0 (KeyringSetData);

                  DEBUG ("An Empathy 3.0 password migration wasn't finished "
                      "due to fd.o #42088. Finishing it now by deleting the "
                      "password for %s", account);

                  ksd->account = g_strdup (account);
                  ksd->name = g_strdup ("password");
                  ksd->set = FALSE;

                  gnome_keyring_delete_password (&keyring_schema,
                      _keyring_set_cb, ksd, NULL,
                      "account", account,
                      "param", "password",
                      NULL);

                  /* behave as if it had already been deleted, i.e. we never
                   * actually found it... */
                  param = NULL;
                  value = NULL;
                }

              gnome_keyring_found_list_free (empathy_items);
            }

          if (param != NULL && value != NULL)
            {
              gchar *key = g_strdup_printf ("param-%s", param);

              g_key_file_set_value (amd->secrets, account, key, value);
              mcp_account_manager_parameter_make_secret (am, account, key);

              g_free (key);
            }
        }
    }

  gnome_keyring_found_list_free (items);

 finished:
  gnome_keyring_attribute_list_free (match);
}

#else

static void
_delete_from_keyring (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account,
    const gchar *key)
{
  return;
}

static void
_keyring_commit (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account_name)
{
  return;
}

static void
_get_secrets_from_keyring (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account)
{
  return;
}

#endif

static void account_storage_iface_init (McpAccountStorageIface *,
    gpointer);

G_DEFINE_TYPE_WITH_CODE (McdAccountManagerDefault, mcd_account_manager_default,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
        account_storage_iface_init));

static gchar *
get_old_filename (void)
{
  const gchar *base;

  base = g_getenv ("MC_ACCOUNT_DIR");

  if (!base)
    base = ACCOUNTS_DIR;

  if (!base)
    return NULL;

  if (base[0] == '~')
    return g_build_filename (g_get_home_dir(), base + 1, "accounts.cfg", NULL);
  else
    return g_build_filename (base, "accounts.cfg", NULL);
}

static gchar *
account_filename_in (const gchar *dir)
{
  return g_build_filename (dir, "telepathy", "mission-control", "accounts.cfg",
      NULL);
}

static void
mcd_account_manager_default_init (McdAccountManagerDefault *self)
{
  DEBUG ("mcd_account_manager_default_init");
  self->filename = account_filename_in (g_get_user_data_dir ());
  self->keyfile = g_key_file_new ();
  self->secrets = g_key_file_new ();
  self->removed = g_key_file_new ();
  self->removed_accounts =
    g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
  self->save = FALSE;
  self->loaded = FALSE;
}

static void
mcd_account_manager_default_class_init (McdAccountManagerDefaultClass *cls)
{
  DEBUG ("mcd_account_manager_default_class_init");
}

/* We happen to know that the string MC gave us is "sufficiently escaped" to
 * put it in the keyfile as-is. */
static gboolean
_set (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account,
    const gchar *key,
    const gchar *val)
{
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);
#if ENABLE_GNOME_KEYRING
  gboolean secret;
#endif

  amd->save = TRUE;

#if ENABLE_GNOME_KEYRING
  /* if we have a keyring, secrets are segregated */
  secret = mcp_account_manager_parameter_is_secret (am, account, key);

  /* remove it from both sets, then re-add it to the right one if non-null */
  g_key_file_remove_key (amd->secrets, account, key, NULL);
  g_key_file_remove_key (amd->keyfile, account, key, NULL);

  if (val != NULL)
    {
      if (secret)
        g_key_file_set_value (amd->secrets, account, key, val);
      else
        g_key_file_set_value (amd->keyfile, account, key, val);
    }

  /* if we removed the account before, it now exists again, so... */
  g_hash_table_remove (amd->removed_accounts, account);

  /* likewise the param should no longer be on the deleted list */
  g_key_file_remove_key (amd->removed, account, key, NULL);
#else

  if (val != NULL)
    g_key_file_set_value (amd->keyfile, account, key, val);
  else
    g_key_file_remove_key (amd->keyfile, account, key, NULL);

#endif

  return TRUE;
}

static gboolean
_get (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account,
    const gchar *key)
{
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);

  if (key != NULL)
    {
      gchar *v = NULL;

#if ENABLE_GNOME_KEYRING
      if (mcp_account_manager_parameter_is_secret (am, account, key))
        v = g_key_file_get_value (amd->secrets, account, key, NULL);

      /* fall back to public source if secret was not in keyring */
      if (v == NULL)
        v = g_key_file_get_value (amd->keyfile, account, key, NULL);
#else
      v = g_key_file_get_value (amd->keyfile, account, key, NULL);
#endif

      if (v == NULL)
        return FALSE;

      mcp_account_manager_set_value (am, account, key, v);
      g_free (v);
    }
  else
    {
      gsize i;
      gsize n;
      GStrv keys = g_key_file_get_keys (amd->keyfile, account, &n, NULL);

      if (keys == NULL)
        n = 0;

      for (i = 0; i < n; i++)
        {
          gchar *v = g_key_file_get_value (amd->keyfile, account, keys[i], NULL);

          if (v != NULL)
            mcp_account_manager_set_value (am, account, keys[i], v);

          g_free (v);
        }

      g_strfreev (keys);

#if ENABLE_GNOME_KEYRING
      keys = g_key_file_get_keys (amd->secrets, account, &n, NULL);

      if (keys == NULL)
        n = 0;

      for (i = 0; i < n; i++)
        {
          gchar *v = g_key_file_get_value (amd->secrets, account, keys[i], NULL);

          if (v != NULL)
            {
              mcp_account_manager_set_value (am, account, keys[i], v);
              mcp_account_manager_parameter_make_secret (am, account, keys[i]);
            }

          g_free (v);
        }

      g_strfreev (keys);
#endif
    }

  return TRUE;
}

static gchar *
_create (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *manager,
    const gchar *protocol,
    GHashTable *params,
    GError **error)
{
  gchar *unique_name;

  /* See comment in plugin-account.c::_storage_create_account() before changing
   * this implementation, it's more subtle than it looks */
  unique_name = mcp_account_manager_get_unique_name (MCP_ACCOUNT_MANAGER (am),
                                                     manager, protocol, params);
  g_return_val_if_fail (unique_name != NULL, NULL);

  return unique_name;
}

static gboolean
_delete (const McpAccountStorage *self,
      const McpAccountManager *am,
      const gchar *account,
      const gchar *key)
{
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);

  if (key == NULL)
    {
      if (g_key_file_remove_group (amd->keyfile, account, NULL))
        amd->save = TRUE;
      _delete_from_keyring (self, am, account, NULL);
    }
  else
    {
      gsize n;
      GStrv keys;
      gboolean save = FALSE;

#if ENABLE_GNOME_KEYRING
      save = g_key_file_remove_key (amd->secrets, account, key, NULL);
      if (g_key_file_remove_key (amd->keyfile, account, key, NULL))
        save = TRUE;
#else
      save = g_key_file_remove_key (amd->keyfile, account, key, NULL);
#endif

      if (save)
        amd->save = TRUE;

      keys = g_key_file_get_keys (amd->keyfile, account, &n, NULL);

      /* if that was the last parameter, the account is gone too:  *
       * note that secret parameters don't keep an account alive - *
       * when the last public param dies, the account dies with it */
      if (keys == NULL || n == 0)
        {
          g_key_file_remove_group (amd->secrets, account, NULL);
          g_key_file_remove_group (amd->keyfile, account, NULL);
          _delete_from_keyring (self, am, account, NULL);
        }
      else
        {
          /* always delete from keyring, even if we didn't previously
           * think it was secret - we might have been wrong */
          _delete_from_keyring (self, am, account, key);
        }

      g_strfreev (keys);
    }

  return TRUE;
}


static gboolean
_commit (const McpAccountStorage *self,
    const McpAccountManager *am,
    const gchar *account)
{
  gsize n;
  gchar *data;
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);
  gboolean rval = FALSE;
  gchar *dir;
  GError *error = NULL;

  if (!amd->save)
    return TRUE;

  dir = g_path_get_dirname (amd->filename);

  DEBUG ("Saving accounts to %s", amd->filename);

  if (!mcd_ensure_directory (dir, &error))
    {
      g_warning ("%s", error->message);
      g_error_free (error);
      /* fall through anyway: writing to the file will fail, but it does
       * give us a chance to commit to the keyring too */
    }

  g_free (dir);

  data = g_key_file_to_data (amd->keyfile, &n, NULL);
  rval = g_file_set_contents (amd->filename, data, n, &error);

  if (rval)
    {
      amd->save = FALSE;
    }
  else
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }

  g_free (data);

  _keyring_commit (self, am, account);

  return rval;
}

static void
am_default_load_keyfile (McdAccountManagerDefault *self,
    const gchar *filename)
{
  GError *error = NULL;

  if (g_key_file_load_from_file (self->keyfile, filename,
        G_KEY_FILE_KEEP_COMMENTS, &error))
    {
      DEBUG ("Loaded accounts from %s", filename);
    }
  else
    {
      DEBUG ("Failed to load accounts from %s: %s", filename, error->message);
      g_error_free (error);

      /* Start with a blank configuration, but do not save straight away;
       * we don't want to overwrite a corrupt-but-maybe-recoverable
       * configuration file with an empty one until given a reason to
       * do so. */
      g_key_file_load_from_data (self->keyfile, INITIAL_CONFIG, -1,
          G_KEY_FILE_KEEP_COMMENTS, NULL);
    }
}

static GList *
_list (const McpAccountStorage *self,
    const McpAccountManager *am)
{
  gsize i;
  gsize n;
  GStrv accounts;
  GList *rval = NULL;
  McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self);

  if (!amd->loaded && g_file_test (amd->filename, G_FILE_TEST_EXISTS))
    {
      /* If the file exists, but loading it fails, we deliberately
       * do not fall through to the "initial configuration" case,
       * because we don't want to overwrite a corrupted file
       * with an empty one until an actual write takes place. */
      am_default_load_keyfile (amd, amd->filename);
      amd->loaded = TRUE;
    }

  if (!amd->loaded)
    {
      const gchar * const *iter;

      for (iter = g_get_system_data_dirs ();
          iter != NULL && *iter != NULL;
          iter++)
        {
          gchar *filename = account_filename_in (*iter);

          if (g_file_test (filename, G_FILE_TEST_EXISTS))
            {
              am_default_load_keyfile (amd, filename);
              amd->loaded = TRUE;
              /* Do not set amd->save: we don't need to write it to a
               * higher-priority directory until it actually changes. */
            }

          g_free (filename);

          if (amd->loaded)
            break;
        }
    }

  if (!amd->loaded)
    {
      gchar *old_filename = get_old_filename ();

      if (g_file_test (old_filename, G_FILE_TEST_EXISTS))
        {
          am_default_load_keyfile (amd, old_filename);
          amd->loaded = TRUE;
          amd->save = TRUE;

          if (_commit (self, am, NULL))
            {
              DEBUG ("Migrated %s to new location: deleting old copy");
              if (g_unlink (old_filename) != 0)
                g_warning ("Unable to delete %s: %s", old_filename,
                    g_strerror (errno));
            }
        }

      g_free (old_filename);
    }

  if (!amd->loaded)
    {
      DEBUG ("Creating initial account data");
      g_key_file_load_from_data (amd->keyfile, INITIAL_CONFIG, -1,
          G_KEY_FILE_KEEP_COMMENTS, NULL);
      amd->loaded = TRUE;
      amd->save = TRUE;
      _commit (self, am, NULL);
    }

  accounts = g_key_file_get_groups (amd->keyfile, &n);

  for (i = 0; i < n; i++)
    {
      _get_secrets_from_keyring (self, am, accounts[i]);
      rval = g_list_prepend (rval, g_strdup (accounts[i]));
    }

  g_strfreev (accounts);

  return rval;
}

static void
account_storage_iface_init (McpAccountStorageIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
  mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
  mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);

  mcp_account_storage_iface_implement_get (iface, _get);
  mcp_account_storage_iface_implement_set (iface, _set);
  mcp_account_storage_iface_implement_create (iface, _create);
  mcp_account_storage_iface_implement_delete (iface, _delete);
  mcp_account_storage_iface_implement_commit_one (iface, _commit);
  mcp_account_storage_iface_implement_list (iface, _list);

}

McdAccountManagerDefault *
mcd_account_manager_default_new (void)
{
  return g_object_new (MCD_TYPE_ACCOUNT_MANAGER_DEFAULT, NULL);
}