2
* MC account storage backend inspector
4
* Copyright © 2010 Nokia Corporation
5
* Copyright © 2010 Collabora Ltd.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
#include <glib-object.h>
30
#include "account-store-default.h"
33
"%s OP BACKEND ACCOUNT [KEY [VALUE]]\n\n" \
34
" OP := <get | set | del | has | list>\n" \
39
" ACCOUNT := <MANAGER>/<PROTOCOL>/<ACCOUNT-UID>\n" \
40
" KEY := <manager | protocol | DisplayName | param-<PARAMETER>>\n" \
41
" VALUE := <STRING>\n\n"
43
#if ENABLE_LIBACCOUNTS_SSO
44
#include "account-store-libaccounts.h"
49
gchar * (*get) (const gchar *account, const gchar *key);
50
gboolean (*set) (const gchar *account, const gchar *key, const gchar *value);
51
gboolean (*delete) (const gchar *account);
52
gboolean (*exists) (const gchar *account);
54
guint (*count_passwords) (void);
67
const Backend backends[] = {
74
default_count_passwords },
76
#if ENABLE_LIBACCOUNTS_SSO
89
static void usage (const gchar *name, const gchar *fmt,
92
#if ENABLE_GNOME_KEYRING
93
#include <gnome-keyring.h>
96
setup_default_keyring (void)
98
GnomeKeyringResult result;
100
g_debug ("Setting default keyring to: %s", g_getenv ("MC_KEYRING_NAME"));
102
if (g_getenv ("MC_KEYRING_NAME") != NULL)
104
const gchar *keyring_name = g_getenv ("MC_KEYRING_NAME");
106
g_debug ("MC Keyring name: %s", keyring_name);
108
if ((result = gnome_keyring_set_default_keyring_sync (keyring_name)) ==
109
GNOME_KEYRING_RESULT_OK)
111
g_debug ("Successfully set up temporary keyring %s for tests",
116
g_warning ("Failed to set %s as the default keyring: %s",
117
keyring_name, gnome_keyring_result_to_message (result));
123
int main (int argc, char **argv)
126
const gchar *op_name = NULL;
127
const gchar *backend = NULL;
128
const gchar *account = NULL;
129
const gchar *setting = NULL;
130
const gchar *value = NULL;
131
const Backend *store = NULL;
132
Operation op = OP_UNKNOWN;
133
gchar *output = NULL;
134
gboolean success = FALSE;
137
g_set_application_name (argv[0]);
139
#if ENABLE_GNOME_KEYRING
140
setup_default_keyring ();
149
for (i = 0; backends[i].name != NULL; i++)
151
if (g_str_equal (backends[i].name, backend))
153
store = &backends[i];
159
usage (argv[0], "No such backend %s", backend);
161
if (g_str_equal (op_name, "get"))
163
else if (g_str_equal (op_name, "set"))
165
else if (g_str_equal (op_name, "del"))
167
else if (g_str_equal (op_name, "has"))
169
else if (g_str_equal (op_name, "list"))
171
else if (g_str_equal (op_name, "count-passwords"))
172
op = OP_COUNT_PASSWORDS;
184
usage (argv[0], "op '%s' requires an account and key", op_name);
189
if (account == NULL || *account == '\0')
190
usage (argv[0], "op '%s' requires an account", op_name);
192
if (setting == NULL || *setting == '\0')
193
usage (argv[0], "op '%s' requires a key", op_name);
201
usage (argv[0], "op '%s' requires an account", op_name);
208
usage (argv[0], "op '%s' requires an backend", op_name);
211
case OP_COUNT_PASSWORDS:
213
usage (argv[0], "op '%s' requires an backend", op_name);
217
usage (argv[0], "Unknown operation: %s", op_name);
220
/* if we got this far, we have all the args we need: */
226
output = store->get (account, setting);
227
success = output != NULL;
231
success = store->set (account, setting, value);
232
output = g_strdup_printf ("%s.%s set to '%s' in %s",
233
account, setting, value, store->name);
237
success = store->delete (account);
238
output = g_strdup_printf ("%s deleted from %s", account, store->name);
242
success = store->exists (account);
244
output = g_strdup_printf ("Exists in %s", store->name);
248
list = store->list ();
249
output = g_strjoinv ("\n", list);
253
case OP_COUNT_PASSWORDS:
254
if (store->count_passwords == NULL)
256
g_printerr ("Password-counting is unimplemented\n");
260
guint n = store->count_passwords ();
261
output = g_strdup_printf ("%u", n);
267
output = g_strdup ("Unknown operation");
271
printf ("%s\n", output);
275
return success ? 0 : 1;
279
usage (const gchar *name, const gchar *fmt, ...)
284
fprintf (stderr, DOCSTRING_A, name);
286
fprintf (stderr, "%s", backends[0].name);
288
for (i = 1; backends[i].name != NULL; i++)
289
fprintf (stderr, " | %s", backends[i].name);
291
fprintf (stderr, DOCSTRING_B);
294
vfprintf (stderr, fmt, ap);