[svn] GnuPG - r5175 - in trunk: . common g13

svn author wk cvs at cvs.gnupg.org
Tue Oct 13 21:17:24 CEST 2009


Author: wk
Date: 2009-10-13 21:17:24 +0200 (Tue, 13 Oct 2009)
New Revision: 5175

Added:
   trunk/g13/mount.c
   trunk/g13/mount.h
   trunk/g13/runner.c
   trunk/g13/runner.h
Modified:
   trunk/ChangeLog
   trunk/NEWS
   trunk/common/ChangeLog
   trunk/common/exechelp.c
   trunk/common/exechelp.h
   trunk/common/iobuf.c
   trunk/configure.ac
   trunk/g13/Makefile.am
   trunk/g13/backend.c
   trunk/g13/backend.h
   trunk/g13/be-encfs.c
   trunk/g13/be-encfs.h
   trunk/g13/call-gpg.c
   trunk/g13/call-gpg.h
   trunk/g13/create.c
   trunk/g13/create.h
   trunk/g13/g13.c
   trunk/g13/keyblob.h
   trunk/g13/utils.c
   trunk/g13/utils.h
Log:
Keep on hacking on g13.  A simple --create and --mount does now work.
A hacked up encfs is required.


[The diff below has been truncated]

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/ChangeLog	2009-10-13 19:17:24 UTC (rev 5175)
@@ -1,3 +1,8 @@
+2009-10-12  Werner Koch  <wk at g10code.com>
+
+	* configure.ac: Use -O3 because newer gcc versions require that
+	for uninitialized variable warnings.
+
 2009-09-23  Werner Koch  <wk at g10code.com>
 
 	* configure.ac (HAVE_ASSUAN_SET_IO_MONITOR): Remove test.

Modified: trunk/common/ChangeLog
===================================================================
--- trunk/common/ChangeLog	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/common/ChangeLog	2009-10-13 19:17:24 UTC (rev 5175)
@@ -1,3 +1,7 @@
+2009-10-13  Werner Koch  <wk at g10code.com>
+
+	* exechelp.c (gnupg_kill_process): New.
+
 2009-09-29  Werner Koch  <wk at g10code.com>
 
 	* exechelp.c (create_inheritable_pipe): Rename to

Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/NEWS	2009-10-13 19:17:24 UTC (rev 5175)
@@ -1,7 +1,10 @@
 Noteworthy changes in version 2.1 (under development)
 -------------------------------------------------
 
+ * Encrypted OpenPGP messages with trailing data (e.g. other OpenPGP
+   packets) are now correctly parsed.
 
+
 Noteworthy changes in version 2.0.13 (2009-09-04)
 -------------------------------------------------
 

Modified: trunk/common/exechelp.c
===================================================================
--- trunk/common/exechelp.c	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/common/exechelp.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -1102,3 +1102,28 @@
   return 0;
 #endif /* !HAVE_W32_SYSTEM*/
 }
+
+
+/* Kill a process; that is send an appropriate signal to the process.
+   gnupg_wait_process must be called to actually remove the process
+   from the system.  An invalid PID is ignored.  */
+void
+gnupg_kill_process (pid_t pid)
+{
+#ifdef HAVE_W32_SYSTEM
+  /* Older versions of libassuan set PID to 0 on Windows to indicate
+     an invalid value.  */
+  if (pid != (pid_t) INVALID_HANDLE_VALUE && pid != 0)
+    {
+      HANDLE process = (HANDLE) pid;
+      
+      /* Arbitrary error code.  */
+      TerminateProcess (process, 1);
+    }
+#else
+  if (pid != (pid_t)(-1))
+    {
+      kill (pid, SIGTERM); 
+    }
+#endif
+}

Modified: trunk/common/exechelp.h
===================================================================
--- trunk/common/exechelp.h	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/common/exechelp.h	2009-10-13 19:17:24 UTC (rev 5175)
@@ -86,6 +86,12 @@
 gpg_error_t gnupg_wait_process (const char *pgmname, pid_t pid, int *exitcode);
 
 
+/* Kill a process; that is send an appropriate signal to the process.
+   gnupg_wait_process must be called to actually remove the process
+   from the system.  An invalid PID is ignored.  */
+void gnupg_kill_process (pid_t pid);
+
+
 /* Spawn a new process and immediatley detach from it.  The name of
    the program to exec is PGMNAME and its arguments are in ARGV (the
    programname is automatically passed as first argument).

Modified: trunk/common/iobuf.c
===================================================================
--- trunk/common/iobuf.c	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/common/iobuf.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -1262,22 +1262,22 @@
 
 
 /* Either open the file specified by the file descriptor FD or - if FD
-   is GNUPG_INVALID_FD - the file with name FNAME.  As of now MODE is
-   assumed to be "rb" if FNAME is used.  In contrast to iobuf_fdopen
-   the fiel descriptor FD will not be closed during an iobuf_close. */
+   is -1, the file with name FNAME.  As of now MODE is assumed to be
+   "rb" if FNAME is used.  In contrast to iobuf_fdopen the file
+   descriptor FD will not be closed during an iobuf_close.  */
 iobuf_t
-iobuf_open_fd_or_name (gnupg_fd_t fd, const char *fname, const char *mode)
+iobuf_open_fd_or_name (int fd, const char *fname, const char *mode)
 {
   iobuf_t a;
 
-  if (fd == GNUPG_INVALID_FD)
+  if (fd == -1)
     a = iobuf_open (fname);
   else
     {
-      gnupg_fd_t fd2;
+      int fd2;
 
       fd2 = dup (fd);
-      if (fd2 == GNUPG_INVALID_FD)
+      if (fd2 == -1)
         a = NULL;
       else
         a = iobuf_fdopen (fd2, mode);

Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/configure.ac	2009-10-13 19:17:24 UTC (rev 5175)
@@ -1230,7 +1230,7 @@
     # warning options and the user should have a chance of overriding
     # them.
     if test "$USE_MAINTAINER_MODE" = "yes"; then
-        CFLAGS="$CFLAGS -Wall -Wcast-align -Wshadow -Wstrict-prototypes"
+        CFLAGS="$CFLAGS -O3 -Wall -Wcast-align -Wshadow -Wstrict-prototypes"
         CFLAGS="$CFLAGS -Wformat -Wno-format-y2k -Wformat-security"
         AC_MSG_CHECKING([if gcc supports -Wno-missing-field-initializers])
         _gcc_cflags_save=$CFLAGS

Modified: trunk/g13/Makefile.am
===================================================================
--- trunk/g13/Makefile.am	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/Makefile.am	2009-10-13 19:17:24 UTC (rev 5175)
@@ -31,7 +31,9 @@
 	keyblob.h \
 	utils.c utils.h \
 	create.c create.h \
+	mount.c mount.h \
 	call-gpg.c call-gpg.h \
+	runner.c runner.h \
 	backend.c backend.h \
 	be-encfs.c be-encfs.h \
 	be-truecrypt.c be-truecrypt.h

Modified: trunk/g13/backend.c
===================================================================
--- trunk/g13/backend.c	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/backend.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -41,6 +41,22 @@
 }
 
 
+/* Return true if CONTTYPE is supported by us.  */
+int 
+be_is_supported_conttype (int conttype)
+{
+  switch (conttype)
+    {
+    case CONTTYPE_ENCFS:
+      return 1;
+
+    default: 
+      return 0;
+    }
+}
+
+
+
 /* If the backend requires a separate file or directory for the
    container, return its name by computing it from FNAME which gives
    the g13 filename.  The new file name is allocated and stored at
@@ -81,3 +97,37 @@
     }
 }
 
+
+/*  Dispatcher to the backend's create function.  */
+gpg_error_t
+be_create_container (ctrl_t ctrl, int conttype, 
+                     const char *fname, int fd, tupledesc_t tuples)
+{
+  (void)fd;  /* Not yet used.  */
+
+  switch (conttype)
+    {
+    case CONTTYPE_ENCFS: 
+      return be_encfs_create_container (ctrl, fname, tuples);
+
+    default:
+      return no_such_backend (conttype);
+    }
+}
+
+
+/*  Dispatcher to the backend's mount function.  */
+gpg_error_t
+be_mount_container (ctrl_t ctrl, int conttype, 
+                    const char *fname,  const char *mountpoint,
+                    tupledesc_t tuples)
+{
+  switch (conttype)
+    {
+    case CONTTYPE_ENCFS: 
+      return be_encfs_mount_container (ctrl, fname, mountpoint, tuples);
+
+    default:
+      return no_such_backend (conttype);
+    }
+}

Modified: trunk/g13/backend.h
===================================================================
--- trunk/g13/backend.h	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/backend.h	2009-10-13 19:17:24 UTC (rev 5175)
@@ -21,12 +21,20 @@
 #define G13_BACKEND_H
 
 #include "../common/membuf.h"
+#include "utils.h"  /* For tupledesc_t */
 
-
+int         be_is_supported_conttype (int conttype);
 gpg_error_t be_get_detached_name (int conttype, const char *fname, 
                                   char **r_name, int *r_isdir);
 gpg_error_t be_create_new_keys (int conttype, membuf_t *mb);
 
+gpg_error_t be_create_container (ctrl_t ctrl, int conttype, 
+                                 const char *fname, int fd,
+                                 tupledesc_t tuples);
+gpg_error_t be_mount_container (ctrl_t ctrl, int conttype, 
+                                const char *fname, const char *mountpoint,
+                                tupledesc_t tuples);
 
+
 #endif /*G13_BACKEND_H*/
 

Modified: trunk/g13/be-encfs.c
===================================================================
--- trunk/g13/be-encfs.c	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/be-encfs.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -23,12 +23,301 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#include <assert.h>
 
 #include "g13.h"
 #include "i18n.h"
 #include "keyblob.h"
 #include "be-encfs.h"
+#include "runner.h"
+#include "../common/exechelp.h"
 
+
+/* Command values used to run the encfs tool.  */
+enum encfs_cmds
+  {
+    ENCFS_CMD_CREATE,
+    ENCFS_CMD_MOUNT,
+    ENCFS_CMD_UMOUNT
+  };
+
+
+/* An object to keep the private state of the encfs tool.  It is
+   released by encfs_handler_cleanup.  */
+struct encfs_parm_s
+{
+  enum encfs_cmds cmd;  /* The current command. */
+  tupledesc_t tuples;   /* NULL or the tuples object.  */
+  char *mountpoint;     /* The mountpoint.  */
+};
+typedef struct encfs_parm_s *encfs_parm_t;
+
+
+static gpg_error_t 
+send_cmd_bin (runner_t runner, const void *data, size_t datalen)
+{
+  return runner_send_line (runner, data, datalen);
+}
+
+
+static gpg_error_t 
+send_cmd (runner_t runner, const char *string)
+{
+  log_debug ("sending command  -->%s<--\n", string);
+  return send_cmd_bin (runner, string, strlen (string));
+}
+
+
+
+static void
+run_umount_helper (const char *mountpoint)
+{
+  gpg_error_t err;
+  const char pgmname[] = "/usr/bin/fusermount";
+  const char *args[3];
+  
+  args[0] = "-u";
+  args[1] = mountpoint;
+  args[2] = NULL;
+
+  err = gnupg_spawn_process_detached (pgmname, args, NULL);
+  if (err)
+    log_error ("failed to run `%s': %s\n",
+               pgmname, gpg_strerror (err));
+}
+
+
+/* Handle one line of the encfs tool's output.  This function is
+   allowed to modify the content of BUFFER.  */
+static gpg_error_t
+handle_status_line (runner_t runner, const char *line,
+                    enum encfs_cmds cmd, tupledesc_t tuples)
+{
+  gpg_error_t err;
+
+  /* Check that encfs understands our new options.  */
+  if (!strncmp (line, "$STATUS$", 8))
+    {
+      for (line +=8; *line && spacep (line); line++)
+        ;
+      log_info ("got status `%s'\n", line);
+      if (!strcmp (line, "fuse_main_start"))
+        {
+          /* Send a special error code back to let the caller know
+             that everything has been setup by encfs.  */
+          err = gpg_error (GPG_ERR_UNFINISHED);
+        }
+      else
+        err = 0;
+    }
+  else if (!strncmp (line, "$PROMPT$", 8))
+    {
+      for (line +=8; *line && spacep (line); line++)
+        ;
+      log_info ("got prompt `%s'\n", line);
+      if (!strcmp (line, "create_root_dir"))
+        err = send_cmd (runner, cmd == ENCFS_CMD_CREATE? "y":"n");
+      else if (!strcmp (line, "create_mount_point"))
+        err = send_cmd (runner, "y");
+      else if (!strcmp (line, "passwd")
+               || !strcmp (line, "new_passwd"))
+        {
+          if (tuples)
+            {
+              size_t n;
+              const void *value;
+          
+              value = find_tuple (tuples, KEYBLOB_TAG_ENCKEY, &n);
+              if (!value)
+                err = gpg_error (GPG_ERR_INV_SESSION_KEY);
+              else if ((err = send_cmd_bin (runner, value, n)))
+                {
+                  if (gpg_err_code (err) == GPG_ERR_BUG 
+                      && gpg_err_source (err) == GPG_ERR_SOURCE_DEFAULT)
+                    err = gpg_error (GPG_ERR_INV_SESSION_KEY);
+                }
+            }
+          else
+            err = gpg_error (GPG_ERR_NO_DATA);
+        }
+      else
+        err = send_cmd (runner, ""); /* Default to send an empty line.  */
+    }
+  else if (strstr (line, "encfs: unrecognized option '"))
+    err = gpg_error (GPG_ERR_INV_ENGINE);
+  else
+    err = 0;
+
+  return err;
+}
+
+
+/* The main processing function as used by the runner.  */
+static gpg_error_t
+encfs_handler (void *opaque, runner_t runner, const char *status_line)
+{
+  encfs_parm_t parm = opaque;
+  gpg_error_t err;
+
+  if (!parm || !runner)
+    return gpg_error (GPG_ERR_BUG);
+  if (!status_line)
+    {
+      /* Runner requested internal flushing - nothing to do here. */
+      return 0;
+    }
+
+  err = handle_status_line (runner, status_line, parm->cmd, parm->tuples);
+  if (gpg_err_code (err) == GPG_ERR_UNFINISHED
+      && gpg_err_source (err) == GPG_ERR_SOURCE_DEFAULT)
+    {
+      err = 0;
+      /* No more need for the tuples.  */
+      destroy_tupledesc (parm->tuples);
+      parm->tuples = NULL;
+
+      if (parm->cmd == ENCFS_CMD_CREATE)
+        {
+          /* The encfs tool keeps on running after creation of the
+             container.  We don't want that and thus need to stop the
+             encfs process. */
+          run_umount_helper (parm->mountpoint);
+          /* In case the umount helper does not work we try to kill
+             the engine.  FIXME: We should figure out how to make
+             fusermount work.  */
+          runner_cancel (runner);
+        }
+    }
+
+  return err;
+}
+
+
+/* Called by the runner to cleanup the private data. */
+static void
+encfs_handler_cleanup (void *opaque)
+{
+  encfs_parm_t parm = opaque;
+
+  if (!parm)
+    return;
+
+  destroy_tupledesc (parm->tuples);
+  xfree (parm->mountpoint);
+  xfree (parm);
+}
+
+
+/* Run the encfs tool.  */
+static gpg_error_t
+run_encfs_tool (ctrl_t ctrl, enum encfs_cmds cmd,
+                const char *rawdir, const char *mountpoint, tupledesc_t tuples)
+{
+  gpg_error_t err;
+  encfs_parm_t parm;
+  runner_t runner = NULL;
+  int outbound[2] = { -1, -1 };
+  int inbound[2]  = { -1, -1 };
+  const char *pgmname;
+  const char *argv[10];
+  pid_t pid = (pid_t)(-1);
+  int idx;
+
+  (void)ctrl;
+
+  parm = xtrycalloc (1, sizeof *parm);
+  if (!parm)
+    {
+      err = gpg_error_from_syserror ();
+      goto leave;
+    }
+  parm->cmd = cmd;
+  parm->tuples = ref_tupledesc (tuples);
+  parm->mountpoint = xtrystrdup (mountpoint);
+  if (!parm->mountpoint)
+    {
+      err = gpg_error_from_syserror ();
+      goto leave;
+    }
+
+  {
+    static int namecounter;
+    char buffer[50];
+
+    snprintf (buffer, sizeof buffer, "encfs-%d", ++namecounter);
+    err = runner_new (&runner, buffer);
+    if (err)
+      goto leave;
+  }
+
+  err = gnupg_create_inbound_pipe (inbound);
+  if (!err)
+    err = gnupg_create_outbound_pipe (outbound);
+  if (err)
+    {
+      log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
+      goto leave;
+    }
+
+  pgmname = "/usr/bin/encfs";
+  idx = 0;
+  argv[idx++] = "-f";
+  argv[idx++] = "-v";
+  argv[idx++] = "--stdinpass";
+  argv[idx++] = "--annotate";
+  argv[idx++] = rawdir;
+  argv[idx++] = mountpoint;
+  argv[idx++] = NULL;
+  assert (idx <= DIM (argv));
+
+  err = gnupg_spawn_process_fd (pgmname, argv,
+                                outbound[0], -1, inbound[1], &pid);
+  if (err)
+    {
+      log_error ("error spawning `%s': %s\n", pgmname, gpg_strerror (err));
+      goto leave;
+    }
+  close (outbound[0]); outbound[0] = -1;
+  close ( inbound[1]);  inbound[1] = -1;
+
+  runner_set_fds (runner, inbound[0], outbound[1]);
+  inbound[0] = -1;  /* Now owned by RUNNER.  */
+  outbound[1] = -1; /* Now owned by RUNNER.  */
+
+  runner_set_handler (runner, encfs_handler, encfs_handler_cleanup, parm);
+  parm = NULL; /* Now owned by RUNNER.  */
+
+  runner_set_pid (runner, pid);
+  pid = (pid_t)(-1); /* The process is now owned by RUNNER.  */
+
+  err = runner_spawn (runner);
+  if (err)
+    goto leave;
+
+  log_info ("running `%s' in the background\n", pgmname);
+
+ leave:
+  if (inbound[0] != -1)
+    close (inbound[0]);
+  if (inbound[1] != -1)
+    close (inbound[1]);
+  if (outbound[0] != -1)
+    close (outbound[0]);
+  if (outbound[1] != -1)
+    close (outbound[1]);
+  if (pid != (pid_t)(-1))
+    {
+      gnupg_wait_process (pgmname, pid, NULL);
+    }
+  runner_release (runner);
+  encfs_handler_cleanup (parm);
+  return err;
+}
+
+
+
+
+
 /* See be_get_detached_name for a description.  Note that the
    dispatcher code makes sure that NULL is stored at R_NAME before
    calling us. */
@@ -49,10 +338,134 @@
 }
 
 
+/* Create a new session key and append it as a tuple to the memory
+   buffer MB.  
+
+   The EncFS daemon takes a passphrase from stdin and internally
+   mangles it by means of some KDF from OpenSSL.  We want to store a
+   binary key but we need to make sure that certain characters are not
+   used because the EncFS utility reads it from stdin and obviously
+   acts on some of the characters.  This we replace CR (in case of an
+   MSDOS version of EncFS), LF (the delimiter used by EncFS) and Nul
+   (because it is unlikely to work).  We use 32 bytes (256 bit)
+   because that is sufficient for the largest cipher (AES-256) and in
+   addition gives enough margin for a possible entropy degradation by
+   the KDF.  */
 gpg_error_t
 be_encfs_create_new_keys (membuf_t *mb)
 {
+  char *buffer;
+  int i, j;
+
+  /* Allocate a buffer of 32 bytes plus 8 spare bytes we may need to
+     replace the unwanted values.  */
+  buffer = xtrymalloc_secure (32+8);
+  if (!buffer)
+    return gpg_error_from_syserror ();
+
+  /* Randomize the buffer.  STRONG random should be enough as it is a
+     good compromise between security and performance.  The
+     anticipated usage of this tool is the quite often creation of new
+     containers and thus this should not deplete the system's entropy
+     tool too much.  */ 
+  gcry_randomize (buffer, 32+8, GCRY_STRONG_RANDOM);
+  for (i=j=0; i < 32; i++)
+    {
+      if (buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == 0 )
+        {
+          /* Replace.  */
+          if (j == 8)
+            {
+              /* Need to get more random.  */
+              gcry_randomize (buffer+32, 8, GCRY_STRONG_RANDOM);
+              j = 0;
+            }
+          buffer[i] = buffer[32+j];
+          j++;
+        }
+    }
+
+  /* Store the key.  */
+  append_tuple (mb, KEYBLOB_TAG_ENCKEY, buffer, 32);
+
+  /* Free the temporary buffer.  */
+  wipememory (buffer, 32+8);  /*  A failsafe extra wiping.  */
+  xfree (buffer);
+
   return 0;
 }
 
 
+/* Create the container described by the filename FNAME and the keyblob
+   information in TUPLES. */
+gpg_error_t
+be_encfs_create_container (ctrl_t ctrl, const char *fname, tupledesc_t tuples)
+{
+  gpg_error_t err;
+  int dummy;
+  char *containername = NULL;
+  char *mountpoint = NULL;
+
+  err = be_encfs_get_detached_name (fname, &containername, &dummy);
+  if (err)
+    goto leave;
+
+  mountpoint = xtrystrdup ("/tmp/.#g13_XXXXXX");
+  if (!mountpoint)
+    {
+      err = gpg_error_from_syserror ();
+      goto leave;
+    }
+  if (!mkdtemp (mountpoint))
+    {
+      err = gpg_error_from_syserror ();
+      log_error (_("can't create directory `%s': %s\n"),
+                 "/tmp/g13-XXXXXX", gpg_strerror (err));
+      goto leave;
+    }
+
+  err = run_encfs_tool (ctrl, ENCFS_CMD_CREATE, containername, mountpoint,
+                        tuples);
+  
+  /* In any case remove the temporary mount point.  */
+  if (rmdir (mountpoint))
+    log_error ("error removing temporary mount point `%s': %s\n",
+               mountpoint, gpg_strerror (gpg_error_from_syserror ()));
+
+
+ leave:
+  xfree (containername);
+  xfree (mountpoint);
+  return err;
+}
+
+
+/* Mount the container described by the filename FNAME and the keyblob
+   information in TUPLES. */
+gpg_error_t
+be_encfs_mount_container (ctrl_t ctrl, 
+                          const char *fname, const char *mountpoint,
+                          tupledesc_t tuples)
+{
+  gpg_error_t err;
+  int dummy;
+  char *containername = NULL;
+
+  if (!mountpoint)
+    {
+      log_error ("the encfs backend requires an explicit mountpoint\n");
+      err = gpg_error (GPG_ERR_NOT_SUPPORTED);
+      goto leave;
+    }
+
+  err = be_encfs_get_detached_name (fname, &containername, &dummy);
+  if (err)
+    goto leave;
+
+  err = run_encfs_tool (ctrl, ENCFS_CMD_MOUNT, containername, mountpoint,
+                        tuples);
+  
+ leave:
+  xfree (containername);
+  return err;
+}

Modified: trunk/g13/be-encfs.h
===================================================================
--- trunk/g13/be-encfs.h	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/be-encfs.h	2009-10-13 19:17:24 UTC (rev 5175)
@@ -26,6 +26,15 @@
                                         char **r_name, int *r_isdir);
 gpg_error_t be_encfs_create_new_keys (membuf_t *mb);
 
+gpg_error_t be_encfs_create_container (ctrl_t ctrl, 
+                                       const char *fname,
+                                       tupledesc_t tuples);
 
+gpg_error_t be_encfs_mount_container (ctrl_t ctrl,
+                                      const char *fname, 
+                                      const char *mountpoint,
+                                      tupledesc_t tuples);
+
+
 #endif /*G13_BE_ENCFS_H*/
 

Modified: trunk/g13/call-gpg.c
===================================================================
--- trunk/g13/call-gpg.c	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/call-gpg.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -43,7 +43,7 @@
   gpg_error_t err;
   assuan_context_t ctx = NULL;
   const char *pgmname;
-  const char *argv[6];
+  const char *argv[7];
   int no_close_list[5];
   int i;
   char line[ASSUAN_LINELENGTH];
@@ -464,3 +464,133 @@
 }
 
 
+
+/* Call GPG to decrypt a block of data. 
+
+
+ */
+gpg_error_t
+gpg_decrypt_blob (ctrl_t ctrl, const void *ciph, size_t ciphlen,
+                  void **r_plain, size_t *r_plainlen)
+{
+  gpg_error_t err;
+  assuan_context_t ctx;
+  int outbound_fds[2] = { -1, -1 };
+  int inbound_fds[2]  = { -1, -1 };
+  pth_t writer_tid = NULL;
+  pth_t reader_tid = NULL;
+  gpg_error_t writer_err, reader_err;
+  membuf_t reader_mb;
+
+  *r_plain = NULL;
+  *r_plainlen = 0;
+
+  /* Init the memory buffer to receive the encrypted stuff.  */
+  init_membuf_secure (&reader_mb, 1024);
+
+  /* Create two pipes.  */
+  err = gnupg_create_outbound_pipe (outbound_fds);
+  if (!err)
+    err = gnupg_create_inbound_pipe (inbound_fds);
+  if (err)
+    {
+      log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
+      goto leave;
+    }
+
+  /* Start GPG and send the INPUT and OUTPUT commands.  */
+  err = start_gpg (ctrl, outbound_fds[0], inbound_fds[1], &ctx);
+  if (err)
+    goto leave;
+  close (outbound_fds[0]); outbound_fds[0] = -1;
+  close (inbound_fds[1]); inbound_fds[1] = -1;
+  
+  /* Start a writer thread to feed the INPUT command of the server.  */
+  err = start_writer (outbound_fds[1], ciph, ciphlen, 
+                      &writer_tid, &writer_err);
+  if (err)
+    return err;
+  outbound_fds[1] = -1;  /* The thread owns the FD now.  */
+
+  /* Start a reader thread to eat from the OUTPUT command of the
+     server.  */
+  err = start_reader (inbound_fds[0], &reader_mb, 
+                      &reader_tid, &reader_err);
+  if (err)
+    return err;
+  outbound_fds[0] = -1;  /* The thread owns the FD now.  */
+
+  /* Run the decryption.  */
+  err = assuan_transact (ctx, "DECRYPT", NULL, NULL, NULL, NULL, NULL, NULL);
+  if (err)
+    {
+      log_error ("the engine's DECRYPT command failed: %s <%s>\n",
+                 gpg_strerror (err), gpg_strsource (err));
+      goto leave;
+    }
+
+  /* Wait for reader and return the data.  */
+  if (!pth_join (reader_tid, NULL))
+    {
+      err = gpg_error_from_syserror ();
+      log_error ("waiting for reader thread failed: %s\n", gpg_strerror (err));
+      goto leave;
+    }
+  reader_tid = NULL;
+  if (reader_err)
+    {
+      err = reader_err;
+      log_error ("read error in reader thread: %s\n", gpg_strerror (err));
+      goto leave;
+    }
+
+  /* Wait for the writer to catch a writer error.  */
+  if (!pth_join (writer_tid, NULL))
+    {
+      err = gpg_error_from_syserror ();
+      log_error ("waiting for writer thread failed: %s\n", gpg_strerror (err));
+      goto leave;
+    }
+  writer_tid = NULL;
+  if (writer_err)
+    {
+      err = writer_err;
+      log_error ("write error in writer thread: %s\n", gpg_strerror (err));
+      goto leave;
+    }
+
+  /* Return the data.  */
+  *r_plain = get_membuf (&reader_mb, r_plainlen);
+  if (!*r_plain)
+    {
+      err = gpg_error_from_syserror ();
+      log_error ("error while storing the data in the reader thread: %s\n",
+                 gpg_strerror (err));
+      goto leave;
+    }
+
+ leave:
+  if (reader_tid)
+    {
+      pth_cancel (reader_tid);
+      pth_join (reader_tid, NULL);
+    }
+  if (writer_tid)
+    {
+      pth_cancel (writer_tid);
+      pth_join (writer_tid, NULL);
+    }
+  if (outbound_fds[0] != -1)
+    close (outbound_fds[0]);
+  if (outbound_fds[1] != -1)
+    close (outbound_fds[1]);
+  if (inbound_fds[0] != -1)
+    close (inbound_fds[0]);
+  if (inbound_fds[1] != -1)
+    close (inbound_fds[1]);
+  release_gpg (ctx);
+  xfree (get_membuf (&reader_mb, NULL));
+  return err;
+}
+
+

Modified: trunk/g13/call-gpg.h
===================================================================
--- trunk/g13/call-gpg.h	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/call-gpg.h	2009-10-13 19:17:24 UTC (rev 5175)
@@ -23,6 +23,8 @@
 gpg_error_t gpg_encrypt_blob (ctrl_t ctrl,
                               const void *plain, size_t plainlen,
                               void **r_ciph, size_t *r_ciphlen);
+gpg_error_t gpg_decrypt_blob (ctrl_t ctrl, const void *ciph, size_t ciphlen,
+                              void **r_plain, size_t *r_plainlen);
 
 
 

Modified: trunk/g13/create.c
===================================================================
--- trunk/g13/create.c	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/create.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -79,9 +79,9 @@
   if (err)
     goto leave;
 
+  /* Just for testing.  */
   append_tuple (&mb, KEYBLOB_TAG_FILLER, "filler", 6);
 
-
   *r_blob = get_membuf (&mb, r_bloblen);
   if (!*r_blob)
     {
@@ -122,7 +122,7 @@
    appropriate header.  This fucntion is called with a lock file in
    place and after checking that the filename does not exists.  */
 static gpg_error_t
-write_keyblob (ctrl_t ctrl, const char *filename, 
+write_keyblob (const char *filename, 
                const void *keyblob, size_t keybloblen)
 {
   gpg_error_t err;
@@ -152,7 +152,7 @@
   packet[4] = 0;
   packet[5] = 26;
   memcpy (packet+6, "GnuPG/G13", 10); /* Packet subtype.  */
-  packet[16] = 1;   /* G13 packet format.  */
+  packet[16] = 1;   /* G13 packet format version.  */
   packet[17] = 0;   /* Reserved.  */
   packet[18] = 0;   /* Reserved.  */
   packet[19] = 0;   /* OS Flag.  */
@@ -202,7 +202,7 @@
       return err;
     }
 
-  return err;
+  return 0;
   
 
  writeerr:
@@ -220,7 +220,7 @@
    using the current settings.  If the file already exists an error is
    returned. */
 gpg_error_t
-create_new_container (ctrl_t ctrl, const char *filename)
+g13_create_container (ctrl_t ctrl, const char *filename)
 {
   gpg_error_t err;
   dotlock_t lock;
@@ -230,6 +230,7 @@
   size_t enckeybloblen;
   char *detachedname = NULL;
   int detachedisdir;
+  tupledesc_t tuples = NULL;
 
   /* A quick check to see that no container with that name already
      exists.  */
@@ -286,17 +287,28 @@
                          &enckeyblob, &enckeybloblen);
   if (err)
     goto leave;
+
+  /* Put a copy of the keyblob into a tuple structure.  */
+  err = create_tupledesc (&tuples, keyblob, keybloblen);
+  if (err)
+    goto leave;
+  keyblob = NULL;
+  /* if (opt.verbose) */
+  /*   dump_keyblob (tuples); */
   
   /* Write out the header, the encrypted keyblob and some padding. */
-  err = write_keyblob (ctrl, filename, enckeyblob, enckeybloblen);
+  err = write_keyblob (filename, enckeyblob, enckeybloblen);
   if (err)
     goto leave;
 
-  /* Create and append the container.  */
+  /* Create and append the container.  FIXME: We should pass the
+     estream object in addition to the filename, so that the backend
+     can append the container to the g13 file.  */
+  err = be_create_container (ctrl, ctrl->conttype, filename, -1, tuples);
 
 
-
  leave:
+  destroy_tupledesc (tuples);
   xfree (detachedname);
   xfree (enckeyblob);
   xfree (keyblob);

Modified: trunk/g13/create.h
===================================================================
--- trunk/g13/create.h	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/create.h	2009-10-13 19:17:24 UTC (rev 5175)
@@ -20,7 +20,7 @@
 #ifndef G13_CREATE_H
 #define G13_CREATE_H
 
-gpg_error_t create_new_container (ctrl_t ctrl, const char *filename);
+gpg_error_t g13_create_container (ctrl_t ctrl, const char *filename);
 
 
 #endif /*G13_CREATE_H*/

Modified: trunk/g13/g13.c
===================================================================
--- trunk/g13/g13.c	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/g13.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -35,8 +35,10 @@
 #include "i18n.h"
 #include "sysutils.h"
 #include "gc-opt-flags.h"
-#include "create.h"
 #include "keyblob.h"
+#include "./runner.h"
+#include "./create.h"
+#include "./mount.h"
 
 
 enum cmd_and_opt_values {
@@ -84,6 +86,7 @@
   oHomedir,
   oWithColons,
   oDryRun,
+  oNoDetach,
 
   oRecipient,
 
@@ -111,6 +114,7 @@
   ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
   ARGPARSE_s_n (oQuiet,	"quiet",  N_("be somewhat more quiet")),
   ARGPARSE_s_n (oNoTTY, "no-tty", N_("don't use the terminal at all")),
+  ARGPARSE_s_n (oNoDetach, "no-detach", N_("do not detach from the console")),
   ARGPARSE_s_s (oLogFile, "log-file",  N_("|FILE|write log output to FILE")),
   ARGPARSE_s_n (oNoLogFile, "no-log-file", "@"),
   ARGPARSE_s_i (oLoggerFD, "logger-fd", "@"),
@@ -329,6 +333,7 @@
   int nogreeting = 0;
   int debug_wait = 0;
   int use_random_seed = 1;
+  int nodetach = 0;
   int nokeysetup = 0;
   enum cmd_and_opt_values cmd = 0;
   struct server_control_s ctrl;
@@ -499,6 +504,8 @@
 
         case oAuditLog: auditlog = pargs.r.ret_str; break;
 
+        case oNoDetach: nodetach = 1; break;
+
         case oDebug: debug_value |= pargs.r.ret_ulong; break;
         case oDebugAll: debug_value = ~0; break;
         case oDebugNone: debug_value = 0; break;
@@ -677,16 +684,47 @@
       {
         if (argc != 1) 
           wrong_args ("--create filename");
-        err = create_new_container (&ctrl, argv[0]);
+        err = g13_create_container (&ctrl, argv[0]);
         if (err)
           log_error ("error creating a new container: %s <%s>\n",
                      gpg_strerror (err), gpg_strsource (err));
+        else
+          {
+            unsigned int n;
+
+            while ((n = runner_get_threads ()))
+              {
+                log_info ("number of running threads: %u\n", n);
+                pth_sleep (5);
+              }
+          }
       }
       break;
 
+    case aMount: /* Mount a container. */
+      {
+        if (argc != 1 && argc != 2 ) 
+          wrong_args ("--mount filename [mountpoint]");
+        err = g13_mount_container (&ctrl, argv[0], argc == 2?argv[1]:NULL);
+        if (err)
+          log_error ("error mounting container `%s': %s <%s>\n",
+                     *argv, gpg_strerror (err), gpg_strsource (err));
+        else
+          {
+            unsigned int n;
+
+            while ((n = runner_get_threads ()))
+              {
+                log_info ("number of running threads: %u\n", n);
+                pth_sleep (5);
+              }
+          }
+      }
+      break;
+
     default:
-        log_error (_("invalid command (there is no implicit command)\n"));
-	break;
+      log_error (_("invalid command (there is no implicit command)\n"));
+      break;
     }
 
   /* Print the audit result if needed.  */
@@ -735,3 +773,84 @@
 }
 
 
+/* static void */
+/* daemonize (int nodetach) */
+/* { */
+/*   gnupg_fd_t fd; */
+/*   gnupg_fd_t fd_ssh; */
+/*   pid_t pid; */
+
+/*   fflush (NULL); */
+/* #ifdef HAVE_W32_SYSTEM */
+/*   pid = getpid (); */
+/* #else /\*!HAVE_W32_SYSTEM*\/ */
+/*   pid = fork (); */
+/*   if (pid == (pid_t)-1)  */
+/*     { */
+/*       log_fatal ("fork failed: %s\n", strerror (errno) ); */
+/*       g13_exit (1); */
+/*     } */
+/*   else if (pid) /\* We are the parent *\/ */
+/*     {  */
+/*       /\* We need to clwanup our resources.  An gcry_atfork might be */
+/*          needed. *\/ */
+/*       exit (0);  */
+/*       /\*NOTREACHED*\/ */
+/*     } /\* End parent *\/ */
+
+/*   /\*  */
+/*      This is the child */
+/*   *\/ */
+
+/*   /\* Detach from tty and put process into a new session *\/ */
+/*   if (!nodetach ) */
+/*     {  */
+/*       int i; */
+/*       unsigned int oldflags; */
+      
+/*       /\* Close stdin, stdout and stderr unless it is the log stream *\/ */
+/*       for (i=0; i <= 2; i++)  */
+/*         { */
+/*           if (!log_test_fd (i) && i != fd ) */
+/*             { */
+/*               if ( ! close (i) */
+/*                    && open ("/dev/null", i? O_WRONLY : O_RDONLY) == -1) */
+/*                 { */
+/*                   log_error ("failed to open `%s': %s\n", */
+/*                              "/dev/null", strerror (errno)); */
+/*                   cleanup (); */
+/*                   exit (1); */
+/*                 } */
+/*             } */
+/*         } */
+/*       if (setsid() == -1) */
+/*         { */
+/*           log_error ("setsid() failed: %s\n", strerror(errno) ); */
+/*           cleanup (); */
+/*           exit (1); */
+/*         } */
+
+/*       log_get_prefix (&oldflags); */
+/*       log_set_prefix (NULL, oldflags | JNLIB_LOG_RUN_DETACHED); */
+/*       opt.running_detached = 1; */
+/*     } */
+  
+/*   if (chdir("/")) */
+/*     { */
+/*       log_error ("chdir to / failed: %s\n", strerror (errno)); */
+/*       exit (1); */
+/*     } */
+
+/*   { */
+/*     struct sigaction sa; */
+    
+/*     sa.sa_handler = SIG_IGN; */
+/*     sigemptyset (&sa.sa_mask); */
+/*     sa.sa_flags = 0; */
+/*     sigaction (SIGPIPE, &sa, NULL); */
+/*   } */
+/* #endif /\*!HAVE_W32_SYSTEM*\/ */
+
+/*   log_info ("%s %s started\n", strusage(11), strusage(13) ); */
+/*   handle_something (fd, opt.ssh_support ? fd_ssh : GNUPG_INVALID_FD); */
+/* } */

Modified: trunk/g13/keyblob.h
===================================================================
--- trunk/g13/keyblob.h	2009-10-13 09:13:19 UTC (rev 5174)
+++ trunk/g13/keyblob.h	2009-10-13 19:17:24 UTC (rev 5175)
@@ -63,8 +63,9 @@
 
 #define KEYBLOB_TAG_BLOBVERSION 0
 /* This tag is used to describe the version of the keyblob.  It must
-   be the first tag in a keyblob.  Its value is a single byte giving
-   the blob version.  The current version is 1.  */
+   be the first tag in a keyblob and may only occur once.  Its value
+   is a single byte giving the blob version.  The only defined version
+   is 1.  */
 
 #define KEYBLOB_TAG_CONTTYPE 1
 /* This tag gives the type of the container.  The value is a two byte

Added: trunk/g13/mount.c
===================================================================
--- trunk/g13/mount.c	                        (rev 0)
+++ trunk/g13/mount.c	2009-10-13 19:17:24 UTC (rev 5175)
@@ -0,0 +1,303 @@
+/* mount.c - Mount a crypto container
+ * Copyright (C) 2009 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <assert.h>
+
+#include "g13.h"
+#include "i18n.h"
+#include "mount.h"
+
+#include "keyblob.h"
+#include "backend.h"
+#include "utils.h"
+#include "call-gpg.h"
+#include "estream.h"
+
+
+/* Parse the header prefix and return the length of the entire header.  */
+static gpg_error_t
+parse_header (const char *filename, 
+              const unsigned char *packet, size_t packetlen,
+              size_t *r_headerlen)
+{
+  unsigned int len;
+
+  if (packetlen != 32)
+    return gpg_error (GPG_ERR_BUG);
+
+  len = ((packet[2] << 24) | (packet[3] << 16)
+         | (packet[4] << 8) | packet[5]);
+  if (packet[0] != (0xc0|61) || len < 26
+      || memcmp (packet+6, "GnuPG/G13", 10))
+    {
+      log_error ("file `%s' is not valid container\n", filename);
+      return gpg_error (GPG_ERR_INV_OBJ);
+    }
+  if (packet[16] != 1)
+    {
+      log_error ("unknown version %u of container `%s'\n",
+                 (unsigned int)packet[16], filename);
+      return gpg_error (GPG_ERR_INV_OBJ);
+    }
+  if (packet[17] || packet[18]
+      || packet[26] || packet[27] || packet[28] || packet[29] 
+      || packet[30] || packet[31])
+    log_info ("WARNING: unknown meta information in `%s'\n", filename);
+  if (packet[19])
+    log_info ("WARNING: OS flag is not supported in `%s'\n", filename);
+  if (packet[24] != 1 || packet[25] != 0)
+    {
+      log_error ("meta data copies in `%s' are not supported\n", filename);
+      return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
+    }
+
+  len = ((packet[20] << 24) | (packet[21] << 16)
+         | (packet[22] << 8) | packet[23]);
+
+  /* Do a basic sanity check on the length.  */
+  if (len < 32 || len > 1024*1024)
+    {
+      log_error ("bad length given in container `%s'\n", filename);
+      return gpg_error (GPG_ERR_INV_OBJ);
+    }
+     
+  *r_headerlen = len;
+  return 0;
+}
+
+
+
+/* Read the keyblob at FILENAME.  The caller should have acquired a
+   lockfile and checked that the file exists.  */
+static gpg_error_t
+read_keyblob (const char *filename, 
+              void **r_enckeyblob, size_t *r_enckeybloblen)
+{
+  gpg_error_t err;
+  estream_t fp;
+  unsigned char packet[32];
+  size_t headerlen, msglen;
+  void *msg = NULL;
+  
+  *r_enckeyblob = NULL;
+  *r_enckeybloblen = 0;
+
+  fp = es_fopen (filename, "rb");
+  if (!fp)
+    {
+      err = gpg_error_from_syserror ();
+      log_error ("error reading `%s': %s\n", 
+                 filename, gpg_strerror (err));
+      return err;
+    }
+  
+  /* Read the header.  It is defined as 32 bytes thus we read it in one go.  */
+  if (es_fread (packet, 32, 1, fp) != 1)
+    {
+      err = gpg_error_from_syserror ();
+      log_error ("error reading the header of `%s': %s\n",
+                 filename, gpg_strerror (err));
+      goto leave;
+    }
+  
+  err = parse_header (filename, packet, 32, &headerlen);
+  if (err)
+    goto leave;
+  
+  if (opt.verbose)
+    log_info ("header length of `%s' is %zu\n", filename, headerlen);
+
+  /* Read everything including the padding.  We should eventually do a
+     regular OpenPGP parsing to detect the padding packet and pass
+     only the actual used OpenPGP data to the engine.  This is in
+     particular required when supporting CMS which will be
+     encapsulated in an OpenPGP packet.  */
+  assert (headerlen >= 32);
+  msglen = headerlen - 32;
+  if (!msglen)
+    {
+      err = gpg_error (GPG_ERR_NO_DATA);
+      goto leave;
+    }
+  msg = xtrymalloc (msglen);
+  if (!msglen)
+    {
+      err = gpg_error_from_syserror ();
+      goto leave;
+    }
+  if (es_fread (msg, msglen, 1, fp) != 1)
+    {
+      err = gpg_error_from_syserror ();
+      log_error ("error reading keyblob of `%s': %s\n",
+                 filename, gpg_strerror (err));
+      goto leave;
+    }
+
+  *r_enckeyblob = msg;
+  msg = NULL;
+  *r_enckeybloblen = msglen;
+
+ leave:
+  xfree (msg);
+  es_fclose (fp);
+
+  return err;
+}
+
+
+
+
+/* Decrypt the keyblob (ENCKEYBLOB,ENCKEYBLOBLEN) and store the result at
+   (R_KEYBLOB, R_KEYBLOBLEN).  Returns 0 on success or an error code.
+   On error R_KEYBLOB is set to NULL.  */
+static gpg_error_t
+decrypt_keyblob (ctrl_t ctrl, const void *enckeyblob, size_t enckeybloblen,
+                 void **r_keyblob, size_t *r_keybloblen)
+{
+  gpg_error_t err;
+
+  /* FIXME:  For now we only implement OpenPGP.  */
+  err = gpg_decrypt_blob (ctrl, enckeyblob, enckeybloblen,
+                          r_keyblob, r_keybloblen);
+
+  return err;
+}
+
+
+static void
+dump_keyblob (tupledesc_t tuples)
+{
+  size_t n;
+  unsigned int tag;
+  const void *value;
+
+  log_info ("keyblob dump:\n");
+  tag = KEYBLOB_TAG_BLOBVERSION;
+  value = find_tuple (tuples, tag, &n);
+  while (value)
+    {
+      log_info ("   tag: %-5u len: %-2u value: ", tag, (unsigned int)n);
+      if (tag == KEYBLOB_TAG_ENCKEY
+          ||  tag == KEYBLOB_TAG_MACKEY)
+        log_printf ("[confidential]\n");
+      else if (!n)
+        log_printf ("[none]\n");
+      else
+        log_printhex ("", value, n);
+      value = next_tuple (tuples, &tag, &n);
+    }
+}
+
+
+
+/* Mount the container with name FILENAME at MOUNTPOINT.  */
+gpg_error_t
+g13_mount_container (ctrl_t ctrl, const char *filename, const char *mountpoint)
+{
+  gpg_error_t err;
+  dotlock_t lock;
+  void *enckeyblob = NULL;
+  size_t enckeybloblen;
+  void *keyblob = NULL;
+  size_t keybloblen;
+  tupledesc_t tuples = NULL;
+  size_t n;
+  const unsigned char *value;
+  int conttype;
+
+  /* A quick check to see whether the container exists.  */
+  if (access (filename, R_OK))
+    return gpg_error_from_syserror ();
+
+  /* Try to take a lock.  */
+  lock = create_dotlock (filename);
+  if (!lock)
+    return gpg_error_from_syserror ();
+
+  if (make_dotlock (lock, 0))
+    {
+      err = gpg_error_from_syserror ();
+      goto leave;
+    }
+  else
+    err = 0;
+
+  /* Check again that the file exists.  */
+  {
+    struct stat sb;
+    
+    if (stat (filename, &sb))
+      {
+        err = gpg_error_from_syserror ();
+        goto leave;
+      }
+  }
+
+  /* Read the encrypted keyblob.  */
+  err = read_keyblob (filename, &enckeyblob, &enckeybloblen);
+  if (err)
+    goto leave;
+
+  /* Decrypt that keyblob and store it in a tuple descriptor.  */
+  err = decrypt_keyblob (ctrl, enckeyblob, enckeybloblen,
+                         &keyblob, &keybloblen);
+  if (err)
+    goto leave;
+  xfree (enckeyblob);
+  enckeyblob = NULL;
+
+  err = create_tupledesc (&tuples, keyblob, keybloblen);
+  if (!err)
+    keyblob = NULL;
+  else
+    {
+      if (gpg_err_code (err) == GPG_ERR_NOT_SUPPORTED)
+        log_error ("unknown keyblob version\n");
+      goto leave;
+    }
+  if (opt.verbose)
+    dump_keyblob (tuples);
+
+  value = find_tuple (tuples, KEYBLOB_TAG_CONTTYPE, &n);
+  if (!value || n != 2)
+    conttype = 0;
+  else
+    conttype = (value[0] << 8 | value[1]);
+  if (!be_is_supported_conttype (conttype))
+    {
+      log_error ("content type %d is not supported\n", conttype);
+      err = gpg_error (GPG_ERR_NOT_SUPPORTED);
+      goto leave;
+    }
+  err = be_mount_container (ctrl, conttype, filename, mountpoint, tuples);
+
+ leave:
+  destroy_tupledesc (tuples);
+  xfree (keyblob);
+  xfree (enckeyblob);
+  destroy_dotlock (lock);
+  return err;
+}

Added: trunk/g13/mount.h
===================================================================
--- trunk/g13/mount.h	                        (rev 0)
+++ trunk/g13/mount.h	2009-10-13 19:17:24 UTC (rev 5175)
@@ -0,0 +1,29 @@
+/* mmount.h - Defs to mount a crypto container
+ * Copyright (C) 2009 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef G13_MOUNT_H
+#define G13_MOUNT_H
+
+gpg_error_t g13_mount_container (ctrl_t ctrl, 
+                                 const char *filename,
+                                 const char *mountpoint);
+
+




More information about the Gnupg-commits mailing list