1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* list-keyrings.c - test app to list keyrings |
---|
3 | |
---|
4 | Copyright (C) 2003 Red Hat, Inc |
---|
5 | |
---|
6 | The Gnome Library is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU Library General Public License as |
---|
8 | published by the Free Software Foundation; either version 2 of the |
---|
9 | License, or (at your option) any later version. |
---|
10 | |
---|
11 | The Gnome Library is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | Library General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU Library General Public |
---|
17 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | |
---|
21 | Author: Alexander Larsson <alexl@redhat.com> |
---|
22 | */ |
---|
23 | #include "gnome-keyring.h" |
---|
24 | |
---|
25 | static GMainLoop *loop = NULL; |
---|
26 | |
---|
27 | |
---|
28 | static void |
---|
29 | string_callback (GnomeKeyringResult result, |
---|
30 | const char *str, |
---|
31 | gpointer data) |
---|
32 | { |
---|
33 | char **out; |
---|
34 | |
---|
35 | out = data; |
---|
36 | |
---|
37 | if (result != GNOME_KEYRING_RESULT_OK) { |
---|
38 | g_print ("string op failed: %d\n", result); |
---|
39 | *out = NULL; |
---|
40 | } else { |
---|
41 | *out = g_strdup (str); |
---|
42 | } |
---|
43 | g_main_loop_quit (loop); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | static void |
---|
48 | print_keyring_info (GnomeKeyringResult result, |
---|
49 | GnomeKeyringInfo *info, |
---|
50 | gpointer data) |
---|
51 | { |
---|
52 | gboolean *locked; |
---|
53 | locked = data; |
---|
54 | |
---|
55 | *locked = TRUE; |
---|
56 | if (result != GNOME_KEYRING_RESULT_OK) { |
---|
57 | g_print ("error getting keyring info: %d\n", result); |
---|
58 | } else { |
---|
59 | g_print ("lock_on_idle: %d\n", gnome_keyring_info_get_lock_on_idle (info)); |
---|
60 | g_print ("lock timeout: %d\n", gnome_keyring_info_get_lock_timeout (info)); |
---|
61 | g_print ("mtime: %lu\n", (unsigned long)gnome_keyring_info_get_mtime (info)); |
---|
62 | g_print ("ctime: %lu\n", (unsigned long)gnome_keyring_info_get_ctime (info)); |
---|
63 | g_print ("locked: %d\n", gnome_keyring_info_get_is_locked (info)); |
---|
64 | *locked = gnome_keyring_info_get_is_locked (info); |
---|
65 | } |
---|
66 | |
---|
67 | g_main_loop_quit (loop); |
---|
68 | } |
---|
69 | |
---|
70 | static void |
---|
71 | print_item_info (GnomeKeyringResult result, |
---|
72 | GnomeKeyringItemInfo *info, |
---|
73 | gpointer data) |
---|
74 | { |
---|
75 | char *secret; |
---|
76 | char *name; |
---|
77 | if (result != GNOME_KEYRING_RESULT_OK) { |
---|
78 | g_print ("error getting item info: %d\n", result); |
---|
79 | } else { |
---|
80 | name = gnome_keyring_item_info_get_display_name (info); |
---|
81 | secret = gnome_keyring_item_info_get_secret (info); |
---|
82 | g_print (" type: %u\n", gnome_keyring_item_info_get_type (info)); |
---|
83 | g_print (" name: %s\n", name); |
---|
84 | g_print (" secret: %s\n", secret); |
---|
85 | g_print (" mtime: %lu\n", (unsigned long)gnome_keyring_item_info_get_mtime (info)); |
---|
86 | g_print (" ctime: %lu\n", (unsigned long)gnome_keyring_item_info_get_ctime (info)); |
---|
87 | gnome_keyring_free_password (secret); |
---|
88 | g_free (name); |
---|
89 | } |
---|
90 | |
---|
91 | g_main_loop_quit (loop); |
---|
92 | } |
---|
93 | |
---|
94 | static void |
---|
95 | print_attributes (GnomeKeyringResult result, |
---|
96 | GnomeKeyringAttributeList *attributes, |
---|
97 | gpointer data) |
---|
98 | { |
---|
99 | GnomeKeyringAttribute *array; |
---|
100 | int i; |
---|
101 | |
---|
102 | if (result != GNOME_KEYRING_RESULT_OK) { |
---|
103 | g_print ("error getting item attributes: %d\n", result); |
---|
104 | } else { |
---|
105 | array = (GnomeKeyringAttribute *)attributes->data; |
---|
106 | g_print (" Attributes:\n"); |
---|
107 | for (i = 0; i < attributes->len; i++) { |
---|
108 | if (array[i].type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING) { |
---|
109 | g_print (" %s = '%s'\n", array[i].name, array[i].value.string); |
---|
110 | } else if (array[i].type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32) { |
---|
111 | g_print (" %s = %u\n", array[i].name, array[i].value.integer); |
---|
112 | } else { |
---|
113 | g_print (" %s = ** unsupported attribute type **\n", array[i].name); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | g_main_loop_quit (loop); |
---|
119 | } |
---|
120 | |
---|
121 | static void |
---|
122 | get_items_callback (GnomeKeyringResult result, |
---|
123 | GList *list, |
---|
124 | gpointer data) |
---|
125 | { |
---|
126 | GList **out; |
---|
127 | |
---|
128 | out = data; |
---|
129 | *out = NULL; |
---|
130 | |
---|
131 | if (result != GNOME_KEYRING_RESULT_OK) { |
---|
132 | g_print ("error getting item list: %d\n", result); |
---|
133 | } else { |
---|
134 | *out = g_list_copy (list); |
---|
135 | } |
---|
136 | |
---|
137 | g_main_loop_quit (loop); |
---|
138 | } |
---|
139 | |
---|
140 | static void |
---|
141 | string_list_callback (GnomeKeyringResult result, |
---|
142 | GList *list, |
---|
143 | gpointer data) |
---|
144 | { |
---|
145 | GList *l; |
---|
146 | char *name; |
---|
147 | GList **out; |
---|
148 | |
---|
149 | out = data; |
---|
150 | |
---|
151 | *out = NULL; |
---|
152 | |
---|
153 | if (result != GNOME_KEYRING_RESULT_OK) { |
---|
154 | g_print ("error getting keyring list: %d\n", result); |
---|
155 | } else { |
---|
156 | for (l = list; l != NULL; l = l->next) { |
---|
157 | name = l->data; |
---|
158 | *out = g_list_append (*out, g_strdup (name)); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | g_main_loop_quit (loop); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | int |
---|
167 | main (int argc, char *argv[]) |
---|
168 | { |
---|
169 | GList *keyrings, *l, *items, *ll; |
---|
170 | char *keyring; |
---|
171 | gboolean locked; |
---|
172 | guint32 item_id; |
---|
173 | |
---|
174 | loop = g_main_loop_new (NULL, FALSE); |
---|
175 | |
---|
176 | g_print ("Keyrings:\n"); |
---|
177 | gnome_keyring_list_keyring_names (string_list_callback, &keyrings, NULL); |
---|
178 | g_main_loop_run (loop); |
---|
179 | for (l = keyrings; l != NULL; l = l->next) { |
---|
180 | keyring = l->data; |
---|
181 | g_print ("\nkeyring: %s\n", keyring); |
---|
182 | |
---|
183 | gnome_keyring_get_info (keyring, print_keyring_info, &locked, NULL); |
---|
184 | g_main_loop_run (loop); |
---|
185 | |
---|
186 | if (1 || !locked) { |
---|
187 | gnome_keyring_list_item_ids (keyring, get_items_callback, &items, NULL); |
---|
188 | g_main_loop_run (loop); |
---|
189 | |
---|
190 | if (items != NULL) { |
---|
191 | g_print ("Items: \n"); |
---|
192 | } |
---|
193 | for (ll = items; ll != NULL; ll = ll->next) { |
---|
194 | item_id = GPOINTER_TO_UINT(ll->data); |
---|
195 | |
---|
196 | g_print ("\n"); |
---|
197 | g_print (" id: %u\n", item_id); |
---|
198 | gnome_keyring_item_get_info (keyring, |
---|
199 | item_id, |
---|
200 | print_item_info, NULL, NULL); |
---|
201 | g_main_loop_run (loop); |
---|
202 | gnome_keyring_item_get_attributes (keyring, |
---|
203 | item_id, |
---|
204 | print_attributes, NULL, NULL); |
---|
205 | g_main_loop_run (loop); |
---|
206 | } |
---|
207 | g_list_free (items); |
---|
208 | } |
---|
209 | |
---|
210 | g_free (keyring); |
---|
211 | } |
---|
212 | g_list_free (keyrings); |
---|
213 | |
---|
214 | gnome_keyring_get_default_keyring (string_callback, &keyring, NULL); |
---|
215 | g_main_loop_run (loop); |
---|
216 | g_print ("\n"); |
---|
217 | if (keyring != NULL) { |
---|
218 | g_print ("The default keyring for storage is '%s'\n", keyring); |
---|
219 | } else { |
---|
220 | g_print ("No default keyring defined\n"); |
---|
221 | } |
---|
222 | |
---|
223 | return 0; |
---|
224 | } |
---|