summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Walter <stefw@src.gnome.org>2008-08-10 04:18:00 +0000
committerStefan Walter <stefw@src.gnome.org>2008-08-10 04:18:00 +0000
commit00f221b06ea513752c0205e169a22e0b056a4bbf (patch)
tree088aebbd37379262e1e6bc27d4e5e0303678042b
parentfa73b03cac788073c2abbcf778f0a14dc78131eb (diff)
downloadgnome-keyring-00f221b06ea513752c0205e169a22e0b056a4bbf.tar.gz
Add basics of gnome-keyring command line tool.
* 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. svn path=/trunk/; revision=1229
-rw-r--r--ChangeLog10
-rw-r--r--Makefile.am1
-rw-r--r--configure.in1
-rw-r--r--tool/Makefile.am18
-rw-r--r--tool/gkr-tool-import.c210
-rw-r--r--tool/gkr-tool.c131
-rw-r--r--tool/gkr-tool.h24
7 files changed, 395 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 7149390d..638b8307 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-08-09 Stef Walter <stef@memberwebs.com>
+
+ * 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.
+
2008-08-07 Stef Walter <stef@memberwebs.com>
* daemon/pk/gkr-pk-storage.c: Fix problem with unititialized
diff --git a/Makefile.am b/Makefile.am
index 38e3c50a..2509a7e0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,6 +17,7 @@ SUBDIRS = \
library \
pkcs11 \
daemon \
+ tool \
po \
reference \
$(TESTS_DIR) \
diff --git a/configure.in b/configure.in
index dc4f8220..62cc7aee 100644
--- a/configure.in
+++ b/configure.in
@@ -514,6 +514,7 @@ pkcs11/tests/Makefile
po/Makefile.in
reference/Makefile
tests/Makefile
+tool/Makefile
library/gnome-keyring-1.pc
library/gnome-keyring-1-uninstalled.pc
])
diff --git a/tool/Makefile.am b/tool/Makefile.am
new file mode 100644
index 00000000..605c4295
--- /dev/null
+++ b/tool/Makefile.am
@@ -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)
diff --git a/tool/gkr-tool-import.c b/tool/gkr-tool-import.c
new file mode 100644
index 00000000..123127ab
--- /dev/null
+++ b/tool/gkr-tool-import.c
@@ -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;
+}
diff --git a/tool/gkr-tool.c b/tool/gkr-tool.c
new file mode 100644
index 00000000..663fc65d
--- /dev/null
+++ b/tool/gkr-tool.c
@@ -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;
+}
diff --git a/tool/gkr-tool.h b/tool/gkr-tool.h
new file mode 100644
index 00000000..08469cf3
--- /dev/null
+++ b/tool/gkr-tool.h
@@ -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_ */