summaryrefslogtreecommitdiff
path: root/tests/test-keyrings.c
blob: 4f3366e49f02c52947fa9f2847fa49ca8a6c31b0 (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
#include <stdlib.h>

#include "library/gnome-keyring.h"

static GMainLoop *loop = NULL;

static void
print_attributes (GnomeKeyringAttributeList *attributes)
{
	GnomeKeyringAttribute *array;
	int i;
	
	array = (GnomeKeyringAttribute *)attributes->data;
	g_print (" Attributes:\n");
	for (i = 0; i < attributes->len; i++) {
		if (array[i].type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING) {
			g_print ("  %s = '%s'\n", array[i].name, array[i].value.string);
		} else if (array[i].type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32) {
			g_print ("  %s = %u\n", array[i].name, array[i].value.integer);
		} else {
			g_print ("  %s = ** unsupported attribute type **\n", array[i].name);
		}
	}
}

static const gchar* result_msg[] = {
	"GNOME_KEYRING_RESULT_OK",
	"GNOME_KEYRING_RESULT_DENIED",
	"GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON",
	"GNOME_KEYRING_RESULT_ALREADY_UNLOCKED",
	"GNOME_KEYRING_RESULT_NO_SUCH_KEYRING",
	"GNOME_KEYRING_RESULT_BAD_ARGUMENTS",
	"GNOME_KEYRING_RESULT_IO_ERROR",
	"GNOME_KEYRING_RESULT_CANCELLED",
	"GNOME_KEYRING_RESULT_ALREADY_EXISTS"
};

static const gchar*
get_msg_for_keyring_result (GnomeKeyringResult result)
{
	if (result<=GNOME_KEYRING_RESULT_ALREADY_EXISTS) {
		return result_msg[result];
	} else {
		return "Unknown GnomeKeyringResult";
	}
}

static void
ok_cb  (GnomeKeyringResult result,
	gpointer           data)
{
	g_print ("%s: %d (%s)\n", (char *)data, result, get_msg_for_keyring_result (result));
	g_main_loop_quit (loop);
}

static void
lock_all (void)
{
	gnome_keyring_lock_all (ok_cb, "lock all", NULL);
	g_main_loop_run (loop);
}

static void
lock (char *keyring)
{
	gnome_keyring_lock (keyring,
 			    ok_cb, "lock", NULL);
	g_main_loop_run (loop);
}

static void
unlock (char *keyring, char *password)
{
	gnome_keyring_unlock (keyring, password,
			      ok_cb, "unlock", NULL);
	g_main_loop_run (loop);
}

static void
find_items_cb (GnomeKeyringResult result,
	       GList *found_items,
	       gpointer data)
{
	g_print ("found items: res: %d (%s) nr items: %d\n", result, get_msg_for_keyring_result (result), g_list_length (found_items));

	if (found_items != NULL) {
		GnomeKeyringFound *found = found_items->data;
		
		g_print ("Found item: keyring=%s, id=%d, secret='%s'\n", found->keyring, found->item_id, found->secret); 
		print_attributes (found->attributes);
	}
	
	g_main_loop_quit (loop); 
}

static void
find_items (char *attr_val)
{
	gnome_keyring_find_itemsv (GNOME_KEYRING_ITEM_NOTE,
				   find_items_cb, NULL, NULL,
				   "testattribute", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING, attr_val,
				   NULL);
	g_main_loop_run (loop);
}

static void
creat_item_cb  (GnomeKeyringResult result,
		guint32            id,
		gpointer           data)
{
	g_print ("created item: res: %d (%s) id: %d\n", result, get_msg_for_keyring_result (result), id);
	g_main_loop_quit (loop);
}

static void
create_item (char *name, char *attr_name, gboolean update_if_exists)
{
	GnomeKeyringAttributeList *attributes;
	GnomeKeyringAttribute attribute;

	attribute.name = g_strdup ("testattribute");
	attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
	attribute.value.string = g_strdup (attr_name);
	
	attributes = gnome_keyring_attribute_list_new ();
	g_array_append_val (attributes, attribute);
	
	gnome_keyring_item_create (NULL,
				   GNOME_KEYRING_ITEM_NOTE,
				   name,
				   attributes,
				   "secret text",
				   update_if_exists,
				   creat_item_cb, NULL, NULL);
	gnome_keyring_attribute_list_free (attributes);
	g_main_loop_run (loop);
}

static void
creat_application_item_cb  (GnomeKeyringResult result,
			    guint32            id,
			    gpointer           data)
{
	g_print ("created application item: res: %d (%s) id: %d\n", result, get_msg_for_keyring_result (result), id);
	g_main_loop_quit (loop);
}

static void
create_application_item (char *name, char *attr_name, gboolean update_if_exists)
{
	GnomeKeyringAttributeList *attributes;
	GnomeKeyringAttribute attribute;

	attribute.name = g_strdup ("testattribute");
	attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
	attribute.value.string = g_strdup (attr_name);
	
	attributes = gnome_keyring_attribute_list_new ();
	g_array_append_val (attributes, attribute);
	
	gnome_keyring_item_create (NULL,
				   GNOME_KEYRING_ITEM_NOTE | GNOME_KEYRING_ITEM_APPLICATION_SECRET,
				   name,
				   attributes,
				   "application secret text",
				   update_if_exists,
				   creat_application_item_cb, NULL, NULL);
	gnome_keyring_attribute_list_free (attributes);
	g_main_loop_run (loop);
}

static void
show_item_cb (GnomeKeyringResult result,
	      GnomeKeyringItemInfo  *info,
	      gpointer           data)
{
	char *secret;
	char *name;
	if (result != GNOME_KEYRING_RESULT_OK) {
		g_print ("error getting item info: %d (%s)\n", result, get_msg_for_keyring_result (result));
	} else {
		name = gnome_keyring_item_info_get_display_name (info);
		secret = gnome_keyring_item_info_get_secret (info);
		g_print (" type: %u\n", gnome_keyring_item_info_get_type (info));
		g_print (" name: %s\n", name);
		g_print (" secret: %s\n", secret);
		g_print (" mtime: %lu\n", (unsigned long)gnome_keyring_item_info_get_mtime (info));
		g_print (" ctime: %lu\n", (unsigned long)gnome_keyring_item_info_get_ctime (info));
		gnome_keyring_free_password (secret);
		g_free (name);
	}
	
	g_main_loop_quit (loop);
}

static void
print_attributes_cb (GnomeKeyringResult result,
		  GnomeKeyringAttributeList *attributes,
		  gpointer           data)
{
	if (result != GNOME_KEYRING_RESULT_OK) {
		g_print ("error getting item attributes: %d (%s)\n", result, get_msg_for_keyring_result (result));
	} else {
		print_attributes (attributes);
	}

	g_main_loop_quit (loop);
}

static void
show_item (char *keyring, guint32 item_id, guint32 parts)
{
	gnome_keyring_item_get_info_full (keyring, item_id, parts,
				     	  show_item_cb, NULL, NULL);
	g_main_loop_run (loop);
	gnome_keyring_item_get_attributes (keyring, item_id,
					   print_attributes_cb, NULL, NULL);
	g_main_loop_run (loop);
}

static void
delete_item (char *keyring, guint32 item_id)
{
	gnome_keyring_item_delete (keyring, item_id,
				   ok_cb, "delete item", NULL);
	g_main_loop_run (loop);
}

static void
set_item_secret (char *keyring, guint32 item_id, char *secret)
{
	GnomeKeyringItemInfo *info;

	info = gnome_keyring_item_info_new ();
	gnome_keyring_item_info_set_secret (info, secret);
	gnome_keyring_item_set_info (keyring, item_id, info, 
				     ok_cb, "set item", NULL);
	gnome_keyring_item_info_free (info);
	g_main_loop_run (loop);
}

static void
set_item_attribute (char *keyring, guint32 item_id, char *value)
{
	GnomeKeyringAttributeList *attributes;
	GnomeKeyringAttribute attribute;

	attribute.name = g_strdup ("testattribute");
	attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
	attribute.value.string = g_strdup (value);
	
	attributes = gnome_keyring_attribute_list_new ();
	g_array_append_val (attributes, attribute);
	
	gnome_keyring_item_set_attributes (keyring, item_id, attributes, 
					   ok_cb, "set attributes", NULL);
	gnome_keyring_attribute_list_free (attributes);
	g_main_loop_run (loop);
}

static void
create_keyring (char *name, char *password)
{
	gnome_keyring_create (name,  password, 
			      ok_cb, "create keyring", NULL);
	g_main_loop_run (loop);
}

static void
set_default (char *name)
{
	gnome_keyring_set_default_keyring (name,
					   ok_cb, "set default", NULL);
	g_main_loop_run (loop);
}

static void
set_network_cb  (GnomeKeyringResult result,
		 guint32            id,
		 gpointer           data)
{
	g_print ("set network password: res: %d id: %d\n", result, id);
	g_main_loop_quit (loop);
}


static void
set_network (char *server, char *password)
{
	gnome_keyring_set_network_password (NULL /* default keyring */,
					    NULL,
					    NULL,
					    server,
					    NULL,
					    "smb",
					    NULL,
					    0,
					    password,
					    set_network_cb, NULL, NULL);
	g_main_loop_run (loop);
}

static void
set_network_sync (char *server, char *password)
{
	guint32 id;
	GnomeKeyringResult res;
	res = gnome_keyring_set_network_password_sync (NULL /* default keyring */,
						       NULL,
						       NULL,
						       server,
						       NULL,
						       "smb",
						       NULL,
						       0,
						       password,
						       &id);
	g_print ("set network password: res: %d id: %d\n", res, id);
}

static void
find_network (char *server)
{
	GnomeKeyringResult res;
	GList *list, *l;

	list = NULL;
	res = gnome_keyring_find_network_password_sync (NULL, NULL,
							server, NULL,
							"smb",
							NULL, 
							0,
							&list);
	g_print ("find network password, res: %d, len: %d\n", res, g_list_length (list));
	for (l = list; l != NULL; l = l->next) {
		GnomeKeyringNetworkPasswordData *data;
		data = l->data;

		g_print ("%s:%d - proto: %s, server: %s, object: %s, authtype: %s, port: %d, user: %s, domain: %s, password: %s\n",
			 data->keyring,
			 data->item_id,
			 data->protocol,
			 data->server,
			 data->object,
			 data->authtype,
			 data->port,
			 data->user,
			 data->domain,
			 data->password);
	}
}

static void 
list_items_cb (GnomeKeyringResult result, GList *list, gpointer data)
{
	g_print ("list items: res: %d (%s)\n", result, get_msg_for_keyring_result (result));
	for ( ; list; list = list->next)
		g_print ("   id: %d\n", GPOINTER_TO_UINT (list->data));
	g_main_loop_quit (loop);
}

static void
list_items (const char *keyring)
{
	gnome_keyring_list_item_ids (keyring, list_items_cb, NULL, NULL);
	g_main_loop_run (loop);
}

int
main (int argc, char *argv[])
{
	char arg;

	g_set_application_name("test-keyring");
	loop = g_main_loop_new (NULL, FALSE);
	
	arg = 0;
	if (argc >= 2) {
		arg = argv[1][0];
	}

	if (arg == 'L') {
		lock_all ();
	} else if (arg == 'l') {
		if (argc >= 3) {
			lock (argv[2]);
		} else {
			lock (NULL);
		}
	} else if (arg == 'u') {
		if (argc >= 4) {
			unlock (argv[2], argv[3]);
		} else {
			g_print ("unlock requires keyring and password\n");
		}
	} else if (arg == 'c') {
		if (argc >= 4) {
			create_item (argv[2], argv[3], FALSE);
		} else {
			g_print ("create item requires item name and attr value\n");
		}
	} else if (arg == 'C') {
		if (argc >= 4) {
			create_item (argv[2], argv[3], TRUE);
		} else {
			g_print ("create item requires item name and attr value\n");
		}

 	} else if (arg == 'A') {
 		if (argc >= 4) {
 			create_application_item (argv[2], argv[3], FALSE);
 		} else {
 			g_print ("create application item requires item name and attr value\n");
 		}

	/* Show complete item information */
	} else if (arg == 'i') {
		if (argc >= 4) {
			show_item (argv[2], atoi(argv[3]), GNOME_KEYRING_ITEM_INFO_SECRET);
		} else {
			g_print ("must give keyring & item id to show\n");
		}

	/* Show basic item information */
	} else if (arg == 'b') {
		if (argc >= 4) {
			show_item (argv[2], atoi(argv[3]), GNOME_KEYRING_ITEM_INFO_BASICS);
		} else {
			g_print ("must give keyring & item id to show\n");
		}
	} else if (arg == 'd') {
		if (argc >= 4) {
			delete_item (argv[2] ,atoi (argv[3]));
		} else {
			g_print ("must give keyring & item id to delete\n");
		}
	} else if (arg == 's') {
		if (argc >= 5) {
			set_item_secret (argv[2] ,atoi (argv[3]), argv[4]);
		} else {
			g_print ("must give keyring & item id & secret\n");
		}
	} else if (arg == 'a') {
		if (argc >= 5) {
			set_item_attribute (argv[2] ,atoi (argv[3]), argv[4]);
		} else {
			g_print ("must give keyring & item id & attribute value\n");
		}
	} else if (arg == 'f') {
		if (argc >= 3) {
			find_items (argv[2]);
		} else {
			g_print ("must give testattribute value\n");
		}
	} else if (arg == 'k') {
		if (argc >= 4) {
			create_keyring (argv[2], argv[3]);
		} else if (argc >= 3) {
			create_keyring (argv[2], NULL);
		} else {
			g_print ("create keyring requires keyring name\n");
		}
	} else if (arg == 'D') {
		if (argc >= 3) {
			set_default (argv[2]);
		} else {
			set_default (NULL);
		}
	} else if (arg == 'n') {
		if (argc >= 4) {
			set_network (argv[2], argv[3]);
		} else {
			g_print ("need server & password\n");
		}
	} else if (arg == 'N') {
		if (argc >= 4) {
			set_network_sync (argv[2], argv[3]);
		} else {
			g_print ("need server & password\n");
		}
	} else if (arg == 'p') {
		if (argc >= 3) {
			find_network (argv[2]);
		} else {
			g_print ("need server\n");
		}
	} else if (arg == 't') {
		g_print ("gnome keyring is: %s\n",
			 gnome_keyring_is_available ()?"available":"not available");
	} else if (arg == 'I') {
		if (argc >= 3) {
			list_items(argv[2]);
		} else {
			g_print ("need keyring\n");
		}
	} else {
		g_print ("unsupported test\n");
	}
	
	
	return 0;
}