gnome-keyring r1229 - in trunk: . tool



Author: nnielsen
Date: Sun Aug 10 04:18:00 2008
New Revision: 1229
URL: http://svn.gnome.org/viewvc/gnome-keyring?rev=1229&view=rev

Log:
	* conifgure.in:
	* Makefile.am:
	* tool/gkr-tool.c: (added)
	* tool/gkr-tool.h: (added)
	* tool/gkr-tool-import.c: (added)
	* tool/Makefile.am: (added) Add basics of gnome-keyring 
	command line tool.


Added:
   trunk/tool/   (props changed)
   trunk/tool/Makefile.am
   trunk/tool/gkr-tool-import.c
   trunk/tool/gkr-tool.c
   trunk/tool/gkr-tool.h
Modified:
   trunk/ChangeLog
   trunk/Makefile.am
   trunk/configure.in

Modified: trunk/Makefile.am
==============================================================================
--- trunk/Makefile.am	(original)
+++ trunk/Makefile.am	Sun Aug 10 04:18:00 2008
@@ -17,6 +17,7 @@
 	library \
 	pkcs11 \
 	daemon \
+	tool \
 	po \
 	reference \
 	$(TESTS_DIR) \

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Sun Aug 10 04:18:00 2008
@@ -514,6 +514,7 @@
 po/Makefile.in
 reference/Makefile
 tests/Makefile
+tool/Makefile
 library/gnome-keyring-1.pc
 library/gnome-keyring-1-uninstalled.pc
 ])

Added: trunk/tool/Makefile.am
==============================================================================
--- (empty file)
+++ trunk/tool/Makefile.am	Sun Aug 10 04:18:00 2008
@@ -0,0 +1,18 @@
+bin_PROGRAMS= \
+	gnome-keyring
+
+INCLUDES=	\
+	-DPKCS11_MODULE_PATH=\""$(libdir)/gnome-keyring/gnome-keyring-pkcs11.so"\" 	\
+	-DGNOMELOCALEDIR=\""$(datadir)/locale"\"					\
+	-I$(top_srcdir) 								\
+	-I$(top_builddir) 								\
+	$(GOBJECT_CFLAGS) 								\
+	$(GLIB_CFLAGS)
+	
+gnome_keyring_SOURCES = \
+	gkr-tool.c gkr-tool.h \
+	gkr-tool-import.c
+
+gnome_keyring_LDADD = \
+	$(top_builddir)/gp11/libgp11.la \
+	$(GLIB_LIBS)

Added: trunk/tool/gkr-tool-import.c
==============================================================================
--- (empty file)
+++ trunk/tool/gkr-tool-import.c	Sun Aug 10 04:18:00 2008
@@ -0,0 +1,210 @@
+
+#include "config.h"
+
+#include "gkr-tool.h"
+
+#include "gp11/gp11.h"
+#include "gp11/pkcs11g.h"
+
+static gchar **import_files = NULL;
+
+static GOptionEntry import_entries[] = {
+	GKR_TOOL_BASIC_OPTIONS
+	{ G_OPTION_REMAINING, 0, G_OPTION_FLAG_FILENAME, G_OPTION_ARG_FILENAME_ARRAY, &import_files, "Filename", NULL },
+	{ NULL }
+};
+
+static const guint ATTR_TYPES[] = {
+	CKA_LABEL,
+	CKA_CLASS,
+	CKA_ID
+};
+
+static const char HEXC[] = "0123456789ABCDEF";
+
+static void
+print_object_information (GP11Object *object)
+{
+	GP11Attributes *attrs;
+	GP11Attribute *id;
+	CK_OBJECT_CLASS klass;
+	const gchar *message;
+	GError *err = NULL;
+	gchar *label;
+	
+	attrs = gp11_object_get_full (object, ATTR_TYPES, G_N_ELEMENTS(ATTR_TYPES), NULL, &err);
+	if(!attrs) {
+		gkr_tool_handle_error (&err, "couldn't get imported object info");
+		return;
+	}
+
+	if (!gp11_attributes_find_string (attrs, CKA_LABEL, &label))
+		label = g_strdup ("unknown");
+	if (!gp11_attributes_find_ulong (attrs, CKA_CLASS, &klass))
+		klass = CKO_DATA;
+	id = gp11_attributes_find (attrs, CKA_ID);
+	
+	switch (klass) {
+	case CKO_CERTIFICATE:
+		message = "Imported certificate: %s\n";
+		break;
+	case CKO_DATA:
+		message = "Imported data: %s\n";
+		break;
+	case CKO_PRIVATE_KEY:
+		message = "Imported private key: %s\n";
+		break;
+	case CKO_PUBLIC_KEY:
+		message = "Imported public key: %s\n";
+		break;
+	case CKO_SECRET_KEY:
+		message = "Imported secret key: %s\n";
+		break;
+	default:
+		message = "Imported object: %s\n";
+		break;
+	};
+	
+	g_print (message, label);
+
+	if (id) {
+		guchar *data = id->value;
+		gsize n_data = id->length;
+		gchar pair[3];
+		
+		g_print ("\tID: ");
+		
+		while(n_data > 0) {
+			pair[0] = HEXC[*(data) >> 4 & 0xf];
+			pair[1] = HEXC[*(data++) & 0xf];
+			pair[2] = 0;
+			n_data--;
+			g_print ("%s", pair);
+		}
+
+		g_print ("\n");
+	}
+	
+	gp11_attributes_unref (attrs);
+	g_free (label);
+}
+
+static void
+print_import_information (GP11Session *session, GP11Object *import)
+{
+	GP11Attribute *attr;
+	GList *objects, *l;
+	GError *err;
+	
+	attr = gp11_object_get_one (import, CKA_GNOME_IMPORT_OBJECTS, &err);
+	if (!attr) {
+		gkr_tool_handle_error (&err, "couldn't find imported objects");
+		return;
+	}
+
+	objects = gp11_objects_from_handle_array (session, attr);
+	gp11_attribute_free (attr);
+
+	for (l = objects; l; l = g_list_next (l))
+		print_object_information (GP11_OBJECT (l->data));
+	
+	gp11_list_unref_free (objects);
+}
+
+static int
+import_from_file (GP11Session *session, const gchar *filename)
+{
+	GError *err = NULL;
+	GP11Object *import;
+	GP11Attributes *attrs;
+	gchar *data;
+	gsize n_data;
+	
+	/* Read in the file data */
+	if (!g_file_get_contents (filename, &data, &n_data, &err)) {
+		gkr_tool_handle_error (&err, NULL);
+		return 1;
+	}
+	
+	/* Setup the attributes on the object */
+	attrs = gp11_attributes_new ();
+	gp11_attributes_add_data (attrs, CKA_VALUE, data, n_data);
+	gp11_attributes_add_boolean (attrs, CKA_TOKEN, FALSE);
+	gp11_attributes_add_boolean (attrs, CKA_GNOME_IMPORT_TOKEN, TRUE);
+	gp11_attributes_add_string (attrs, CKA_GNOME_IMPORT_LABEL, g_basename (filename));
+	
+	import = gp11_session_create_object_full (session, attrs, NULL, &err);
+	gp11_attributes_unref (attrs);
+	g_free (data);
+	
+	if (!import) {
+		gkr_tool_handle_error (&err, "couldn't import file: %s", filename);
+		return 1;
+	}
+	
+	if (!gkr_tool_mode_quiet)
+		print_import_information (session, import);
+	
+	g_object_unref (import);
+	return 0;
+}
+
+static GP11Session*
+open_import_session (void)
+{
+	GP11Module *module;
+	GP11Session *session;
+	GList *slots;
+	GError *err = NULL;
+	
+	module = gp11_module_initialize (PKCS11_MODULE_PATH, NULL, &err);
+	if (!module) {
+		gkr_tool_handle_error (&err, NULL);
+		return NULL;
+	}
+	
+	slots = gp11_module_get_slots (module, FALSE);
+	g_return_val_if_fail (slots && slots->data, NULL);
+	
+	session = gp11_slot_open_session(slots->data, CKF_RW_SESSION, &err);
+	gp11_list_unref_free (slots);
+	g_object_unref (module);
+	
+	if (!session) {
+		gkr_tool_handle_error (&err, "couldn't connect to gnome-keyring");
+		return NULL;
+	}
+	
+	return session;
+}
+
+int
+gkr_tool_import (int argc, char *argv[])
+{
+	GP11Session *session;
+	gchar **imp;
+	int ret = 0;
+	
+	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;
+	}
+	
+	/* Open a session */
+	session = open_import_session ();
+	if (!session)
+		return 1;
+	
+	for (imp = import_files; *imp; ++imp) {
+		ret = import_from_file (session, *imp);
+		if (ret != 0)
+			break;
+	}
+	
+	g_object_unref (session);
+	return ret;
+}

Added: trunk/tool/gkr-tool.c
==============================================================================
--- (empty file)
+++ trunk/tool/gkr-tool.c	Sun Aug 10 04:18:00 2008
@@ -0,0 +1,131 @@
+
+#include "config.h"
+
+#include "gkr-tool.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <glib-object.h>
+
+#include <locale.h>
+#include <string.h>
+
+/* -----------------------------------------------------------------------------
+ * GENERAL HELPERS
+ */
+
+gboolean gkr_tool_mode_quiet = FALSE;
+
+int
+gkr_tool_parse_options (int *argc, char** argv[], GOptionEntry *options)
+{
+	GError *err = NULL;
+	GOptionContext *context;
+	int ret = 0;
+	
+	context = g_option_context_new ("- Gnome 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;
+}
+
+void gkr_tool_handle_error (GError **error, const gchar *format, ...)
+{
+	gchar *message = NULL;
+	GError *err = error ? *error : NULL;
+	va_list va;
+	
+	if (format) {
+		va_start(va, format);
+		message = g_strdup_vprintf(format, va);
+		va_end(va);
+	}
+	
+	g_printerr ("gnome-keyring: %s%s%s",
+	            message ? message : "",
+	            message && err && err->message ? ": " : "",
+	            err && err->message ? err->message : "");
+	
+	g_free (message);
+	g_clear_error (error);
+}
+
+/* -----------------------------------------------------------------------------
+ * COMMAND LINE
+ */
+
+typedef struct _CommandInfo {
+	const char *name;
+	int (*handler) (int argc, char *argv[]);
+} CommandInfo;
+
+static CommandInfo command_info[] = {
+	{ "import", gkr_tool_import },
+	{ NULL, NULL }
+};
+
+static void
+print_general_usage (void)
+{
+	CommandInfo *cmd;
+	const gchar *prefix;
+	
+	g_printerr ("usage: gnome-keyring command [options]\n");
+	
+	prefix = "commands: ";
+	for (cmd = command_info; cmd->name; ++cmd) {
+		g_printerr ("%s%s\n", prefix, cmd->name);
+		prefix = "          ";
+	}
+}
+
+int
+main (int argc, char *argv[])
+{
+	CommandInfo *cmd;
+	int ret = -1;
+	
+	g_type_init ();
+	g_thread_init (NULL);
+	
+#ifdef HAVE_LOCALE_H
+	/* internationalisation */
+	setlocale (LC_ALL, "");
+#endif
+
+#ifdef HAVE_GETTEXT
+	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
+	textdomain (GETTEXT_PACKAGE);
+	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+#endif
+
+	/* The first argument is the command */
+	if (argc < 2) {
+		print_general_usage ();
+		return 2;
+	}
+	
+	/* Find the command to run */
+	for (cmd = command_info; cmd->name; ++cmd) {
+		g_return_val_if_fail (argv[1], 1);
+		if (strcmp (cmd->name, argv[1]) == 0) {
+			/* Remove the command and replace with executable command */
+			argv[1] = argv[0];
+			ret = (cmd->handler) (argc - 1, argv + 1);
+			break;
+		}
+	}
+	
+	if (ret == -1) {
+		print_general_usage ();
+		ret = 2;
+	}
+	
+	return ret;
+}

Added: trunk/tool/gkr-tool.h
==============================================================================
--- (empty file)
+++ trunk/tool/gkr-tool.h	Sun Aug 10 04:18:00 2008
@@ -0,0 +1,24 @@
+#ifndef GKRTOOL_H_
+#define GKRTOOL_H_
+
+#include <glib.h>
+
+/* -------------------------------------------------------------------------------
+ * GENERAL HELPERS
+ */
+
+extern gboolean gkr_tool_mode_quiet;
+
+#define GKR_TOOL_BASIC_OPTIONS \
+	{ "quiet", 'q', 0, G_OPTION_ARG_NONE, &gkr_tool_mode_quiet, "Don't print unnecessary output", NULL }, 
+
+void gkr_tool_handle_error (GError **error, const gchar *message, ...);
+
+int gkr_tool_parse_options (int *argc, char** argv[], GOptionEntry *options);
+
+/* -------------------------------------------------------------------------------
+ * VARIOUS COMMAND HANDLERS 
+ */
+int gkr_tool_import (int argc, char *argv[]);
+
+#endif /* GKRTOOL_H_ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]