static void
imported_object (GckObject *object,
                 const gchar *destination)
{
	gulong attr_types[3];
	GckAttributes *attrs;
	const GckAttribute *id;
	CK_OBJECT_CLASS klass;
	const gchar *message;
	GError *err = NULL;
	gchar *label, *hex;

	attr_types[0] = CKA_LABEL;
	attr_types[1] = CKA_CLASS;
	attr_types[2] = CKA_ID;

	attrs = gck_object_get_full (object, attr_types, G_N_ELEMENTS (attr_types), NULL, &err);
	if (attrs == NULL) {
		gkr_tool_handle_error (&err, "couldn't get imported object info");
		return;
	}

	if (!gck_attributes_find_string (attrs, CKA_LABEL, &label))
		label = g_strdup ("unknown");
	if (!gck_attributes_find_ulong (attrs, CKA_CLASS, &klass))
		klass = CKO_DATA;
	id = gck_attributes_find (attrs, CKA_ID);
	
	switch (klass) {
	case CKO_CERTIFICATE:
		message = "%s: imported certificate: %s\n";
		break;
	case CKO_DATA:
		message = "%s: imported data: %s\n";
		break;
	case CKO_PRIVATE_KEY:
		message = "%s: imported private key: %s\n";
		break;
	case CKO_PUBLIC_KEY:
		message = "%s: imported public key: %s\n";
		break;
	case CKO_SECRET_KEY:
		message = "%s: imported secret key: %s\n";
		break;
	default:
		message = "%s: imported object: %s\n";
		break;
	};
	
	g_print (message, destination, label);

	if (id) {
		hex = egg_hex_encode (id->value, id->length);
		g_print ("\tidentifier: %s\n", hex);
		g_free (hex);
	}

	gck_attributes_unref (attrs);
	g_free (label);
}
Exemple #2
0
int
gkr_tool_parse_options (int *argc, char** argv[], GOptionEntry *options)
{
	GError *err = NULL;
	GOptionContext *context;
	int ret = 0;
	
	context = g_option_context_new ("- Mate Keyring Tool");
	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
	
	if (!g_option_context_parse (context, argc, argv, &err)) {
		gkr_tool_handle_error (&err, NULL);
		ret = 2;
	}
	
	g_option_context_free (context);
	return ret;
}
int
gkr_tool_import (int argc, char *argv[])
{
	GcrParser *parser;
	GError *error = NULL;
	GInputStream *input;
	ImportClosure *closure;
	GFile *file;
	gchar **imp;
	int ret = 0;
	GList *l;

	ret = gkr_tool_parse_options (&argc, &argv, import_entries);
	if (ret != 0)
		return ret;
	
	if(!import_files || !*import_files) {
		gkr_tool_handle_error (NULL, "specify files to import");
		return 2;
	}

	if (!gcr_pkcs11_initialize (NULL, &error)) {
		gkr_tool_handle_error (&error, "couldn't initialize pkcs11 modules");
		return 1;
	}

	parser = gcr_parser_new ();
	closure = g_new0 (ImportClosure, 1);
	g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), closure);

	for (imp = import_files; *imp; ++imp) {
		file = g_file_new_for_commandline_arg (*imp);
		
		input = G_INPUT_STREAM (g_file_read (file, NULL, &error));
		g_object_unref (file);
		if (input == NULL) {
			gkr_tool_handle_error (&error, "couldn't read file: %s", *imp);
			ret = 1;

		} else {
			if (!gcr_parser_parse_stream (parser, input, NULL, &error)) {
				if (error->code != GCR_ERROR_CANCELLED)
					gkr_tool_handle_error (&error, "couldn't parse: %s", *imp);
				ret = 1;
			}

			g_object_unref (input);
		}
	}

	if (closure->importers == NULL) {
		gkr_tool_handle_error (NULL, "couldn't find any place to import files");
		ret = 1;
	}

	for (l = closure->importers; l != NULL; l = g_list_next (l)) {
		if (gcr_importer_import (l->data, NULL, &error)) {
			if (!gkr_tool_mode_quiet)
				imported_display (l->data);
		} else {
			if (error->code != GCR_ERROR_CANCELLED)
				gkr_tool_handle_error (&error, "couldn't import");
			ret = 1;
		}
	}

	gck_list_unref_free (closure->importers);
	g_free (closure);

	g_object_unref (parser);
	return ret;
}