diff --git a/g13/be-encfs.c b/g13/be-encfs.c index 0e2c68bf3..25725daa2 100644 --- a/g13/be-encfs.c +++ b/g13/be-encfs.c @@ -1,471 +1,456 @@ /* be-encfs.c - The EncFS based backend * 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 . */ #include #include #include #include #include #include #include #include "g13.h" #include "../common/i18n.h" #include "keyblob.h" -#include "be-encfs.h" -#include "runner.h" #include "../common/sysutils.h" #include "../common/exechelp.h" +#include "runner.h" +#include "be-encfs.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[] = 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, unsigned int *r_id) { 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; + gnupg_process_t proc; + int inbound, outbound; (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; } err = runner_new (&runner, "encfs"); if (err) goto leave; - err = gnupg_create_inbound_pipe (inbound, NULL, 0); - if (!err) - err = gnupg_create_outbound_pipe (outbound, NULL, 0); - if (err) - { - log_error (_("error creating a pipe: %s\n"), gpg_strerror (err)); - goto leave; - } - pgmname = ENCFS; idx = 0; argv[idx++] = "-f"; if (opt.verbose) 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); + err = gnupg_process_spawn (pgmname, argv, + (GNUPG_PROCESS_STDIN_PIPE | GNUPG_PROCESS_STDOUT_NULL + | GNUPG_PROCESS_STDERR_PIPE), + NULL, NULL, &proc); 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. */ + err = gnupg_process_get_fds (proc, 0, &outbound, NULL, &inbound); + if (err) + { + log_error ("error get fds '%s': %s\n", pgmname, gpg_strerror (err)); + gnupg_process_release (proc); + goto leave; + } + + runner_set_fds (runner, inbound, outbound); 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. */ + runner_set_proc (runner, proc); err = runner_spawn (runner); if (err) - goto leave; + { + gnupg_process_release (proc); + goto leave; + } *r_id = runner_get_rid (runner); 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, 1, NULL); - gnupg_release_process (pid); - } 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. */ gpg_error_t be_encfs_get_detached_name (const char *fname, char **r_name, int *r_isdir) { char *result; if (!fname || !*fname) return gpg_error (GPG_ERR_INV_ARG); result = strconcat (fname, ".d", NULL); if (!result) return gpg_error_from_syserror (); *r_name = result; *r_isdir = 1; return 0; } /* 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, unsigned int *r_id) { 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 (!gnupg_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, r_id); /* 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. On success the runner id is stored at R_ID. */ gpg_error_t be_encfs_mount_container (ctrl_t ctrl, const char *fname, const char *mountpoint, tupledesc_t tuples, unsigned int *r_id) { 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, r_id); leave: xfree (containername); return err; } diff --git a/g13/g13.c b/g13/g13.c index 2bbb453eb..785c91950 100644 --- a/g13/g13.c +++ b/g13/g13.c @@ -1,1056 +1,1057 @@ /* g13.c - Disk Key management with GnuPG * 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 . * SPDX-License-Identifier: GPL-3.0-or-later */ #include #include #include #include #include #include #include #include #include #define INCLUDED_BY_MAIN_MODULE 1 #include "g13.h" #include #include #include "../common/i18n.h" #include "../common/sysutils.h" #include "../common/gc-opt-flags.h" #include "../common/asshelp.h" #include "../common/init.h" +#include "../common/exechelp.h" #include "keyblob.h" #include "server.h" #include "runner.h" #include "create.h" #include "mount.h" #include "suspend.h" #include "mountinfo.h" #include "backend.h" #include "call-syshelp.h" enum cmd_and_opt_values { aNull = 0, oQuiet = 'q', oVerbose = 'v', oRecipient = 'r', aGPGConfList = 500, aGPGConfTest, aCreate, aMount, aUmount, aSuspend, aResume, aServer, aFindDevice, oOptions, oDebug, oDebugLevel, oDebugAll, oDebugNone, oDebugWait, oDebugAllowCoreDump, oLogFile, oNoLogFile, oAuditLog, oOutput, oAgentProgram, oGpgProgram, oType, oDisplay, oTTYname, oTTYtype, oLCctype, oLCmessages, oXauthority, oStatusFD, oLoggerFD, oNoVerbose, oNoSecmemWarn, oNoGreeting, oNoTTY, oNoOptions, oHomedir, oWithColons, oDryRun, oNoDetach, oNoRandomSeedFile, oFakedSystemTime }; static gpgrt_opt_t opts[] = { ARGPARSE_group (300, N_("@Commands:\n ")), ARGPARSE_c (aCreate, "create", N_("Create a new file system container")), ARGPARSE_c (aMount, "mount", N_("Mount a file system container") ), ARGPARSE_c (aUmount, "umount", N_("Unmount a file system container") ), ARGPARSE_c (aSuspend, "suspend", N_("Suspend a file system container") ), ARGPARSE_c (aResume, "resume", N_("Resume a file system container") ), ARGPARSE_c (aServer, "server", N_("Run in server mode")), ARGPARSE_c (aFindDevice, "find-device", "@"), ARGPARSE_c (aGPGConfList, "gpgconf-list", "@"), ARGPARSE_c (aGPGConfTest, "gpgconf-test", "@"), ARGPARSE_group (301, N_("@\nOptions:\n ")), ARGPARSE_s_s (oRecipient, "recipient", N_("|USER-ID|encrypt for USER-ID")), ARGPARSE_s_s (oType, "type", N_("|NAME|use container format NAME")), ARGPARSE_s_s (oOutput, "output", N_("|FILE|write output to FILE")), 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", "@"), ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")), ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_s (oDebugLevel, "debug-level", N_("|LEVEL|set the debugging level to LEVEL")), ARGPARSE_s_n (oDebugAll, "debug-all", "@"), ARGPARSE_s_n (oDebugNone, "debug-none", "@"), ARGPARSE_s_i (oDebugWait, "debug-wait", "@"), ARGPARSE_s_n (oDebugAllowCoreDump, "debug-allow-core-dump", "@"), ARGPARSE_s_i (oStatusFD, "status-fd", N_("|FD|write status info to this FD")), ARGPARSE_group (302, N_( "@\n(See the man page for a complete listing of all commands and options)\n" )), ARGPARSE_group (303, N_("@\nExamples:\n\n" " blurb\n" " blurb\n")), /* Hidden options. */ ARGPARSE_s_n (oNoVerbose, "no-verbose", "@"), ARGPARSE_s_n (oNoSecmemWarn, "no-secmem-warning", "@"), ARGPARSE_s_n (oNoGreeting, "no-greeting", "@"), ARGPARSE_noconffile (oNoOptions, "no-options", "@"), ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_s (oAgentProgram, "agent-program", "@"), ARGPARSE_s_s (oGpgProgram, "gpg-program", "@"), ARGPARSE_s_s (oDisplay, "display", "@"), ARGPARSE_s_s (oTTYname, "ttyname", "@"), ARGPARSE_s_s (oTTYtype, "ttytype", "@"), ARGPARSE_s_s (oLCctype, "lc-ctype", "@"), ARGPARSE_s_s (oLCmessages, "lc-messages", "@"), ARGPARSE_s_s (oXauthority, "xauthority", "@"), ARGPARSE_s_s (oFakedSystemTime, "faked-system-time", "@"), ARGPARSE_s_n (oWithColons, "with-colons", "@"), ARGPARSE_s_n (oNoRandomSeedFile, "no-random-seed-file", "@"), /* Command aliases. */ ARGPARSE_end () }; /* The list of supported debug flags. */ static struct debug_flags_s debug_flags [] = { { DBG_MOUNT_VALUE , "mount" }, { DBG_CRYPTO_VALUE , "crypto" }, { DBG_MEMORY_VALUE , "memory" }, { DBG_MEMSTAT_VALUE, "memstat" }, { DBG_IPC_VALUE , "ipc" }, { 0, NULL } }; /* The timer tick interval used by the idle task. */ #define TIMERTICK_INTERVAL_SEC (1) /* It is possible that we are currently running under setuid permissions. */ static int maybe_setuid = 1; /* Helper to implement --debug-level and --debug. */ static const char *debug_level; static unsigned int debug_value; /* Flag to indicate that a shutdown was requested. */ static int shutdown_pending; /* The thread id of the idle task. */ static npth_t idle_task_thread; /* The container type as specified on the command line. */ static int cmdline_conttype; static void set_cmd (enum cmd_and_opt_values *ret_cmd, enum cmd_and_opt_values new_cmd ); static void start_idle_task (void); static void join_idle_task (void); /* Begin NPth wrapper functions. */ ASSUAN_SYSTEM_NPTH_IMPL; static const char * my_strusage( int level ) { const char *p; switch (level) { case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@G13@ (@GNUPG@)"; break; case 13: p = VERSION; break; case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n"); break; case 1: case 40: p = _("Usage: @G13@ [options] [files] (-h for help)"); break; case 41: p = _("Syntax: @G13@ [options] [files]\n" "Create, mount or unmount an encrypted file system container\n"); break; case 31: p = "\nHome: "; break; case 32: p = gnupg_homedir (); break; default: p = NULL; break; } return p; } static void wrong_args (const char *text) { fprintf (stderr, _("usage: %s [options] "), G13_NAME); fputs (text, stderr); putc ('\n', stderr); g13_exit (2); } /* Setup the debugging. With a DEBUG_LEVEL of NULL only the active debug flags are propagated to the subsystems. With DEBUG_LEVEL set, a specific set of debug flags is set; and individual debugging flags will be added on top. */ static void set_debug (void) { int numok = (debug_level && digitp (debug_level)); int numlvl = numok? atoi (debug_level) : 0; if (!debug_level) ; else if (!strcmp (debug_level, "none") || (numok && numlvl < 1)) opt.debug = 0; else if (!strcmp (debug_level, "basic") || (numok && numlvl <= 2)) opt.debug = DBG_IPC_VALUE|DBG_MOUNT_VALUE; else if (!strcmp (debug_level, "advanced") || (numok && numlvl <= 5)) opt.debug = DBG_IPC_VALUE|DBG_MOUNT_VALUE; else if (!strcmp (debug_level, "expert") || (numok && numlvl <= 8)) opt.debug = (DBG_IPC_VALUE|DBG_MOUNT_VALUE|DBG_CRYPTO_VALUE); else if (!strcmp (debug_level, "guru") || numok) { opt.debug = ~0; /* if (numok) */ /* opt.debug &= ~(DBG_HASHING_VALUE); */ } else { log_error (_("invalid debug-level '%s' given\n"), debug_level); g13_exit(2); } opt.debug |= debug_value; if (opt.debug && !opt.verbose) opt.verbose = 1; if (opt.debug) opt.quiet = 0; if (opt.debug & DBG_CRYPTO_VALUE ) gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1); gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose); if (opt.debug) parse_debug_flag (NULL, &opt.debug, debug_flags); } static void set_cmd (enum cmd_and_opt_values *ret_cmd, enum cmd_and_opt_values new_cmd) { enum cmd_and_opt_values cmd = *ret_cmd; if (!cmd || cmd == new_cmd) cmd = new_cmd; else { log_error (_("conflicting commands\n")); g13_exit (2); } *ret_cmd = cmd; } int main (int argc, char **argv) { gpgrt_argparse_t pargs; int orig_argc; char **orig_argv; gpg_error_t err = 0; /* const char *fname; */ int may_coredump; char *last_configname = NULL; const char *configname = NULL; int debug_argparser = 0; int no_more_options = 0; char *logfile = NULL; int greeting = 0; 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; strlist_t recipients = NULL; /*mtrace();*/ early_system_init (); gnupg_reopen_std (G13_NAME); gpgrt_set_strusage (my_strusage); gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN); log_set_prefix (G13_NAME, GPGRT_LOG_WITH_PREFIX); /* Make sure that our subsystems are ready. */ i18n_init (); init_common_subsystems (&argc, &argv); npth_init (); /* Take extra care of the random pool. */ gcry_control (GCRYCTL_USE_SECURE_RNDPOOL); may_coredump = disable_core_dumps (); g13_init_signals (); dotlock_create (NULL, 0); /* Register locking cleanup. */ opt.session_env = session_env_new (); if (!opt.session_env) log_fatal ("error allocating session environment block: %s\n", strerror (errno)); /* First check whether we have a config file on the commandline. */ orig_argc = argc; orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); while (gpgrt_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { case oDebug: case oDebugAll: debug_argparser++; break; case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; } } /* Reset the flags. */ pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); /* Initialize the secure memory. */ gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); maybe_setuid = 0; /* * Now we are now working under our real uid */ /* Setup malloc hooks. */ { struct assuan_malloc_hooks malloc_hooks; malloc_hooks.malloc = gcry_malloc; malloc_hooks.realloc = gcry_realloc; malloc_hooks.free = gcry_free; assuan_set_malloc_hooks (&malloc_hooks); } /* Prepare libassuan. */ assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT); assuan_set_system_hooks (ASSUAN_SYSTEM_NPTH); setup_libassuan_logging (&opt.debug, NULL); /* Setup a default control structure for command line mode. */ memset (&ctrl, 0, sizeof ctrl); g13_init_default_ctrl (&ctrl); ctrl.no_server = 1; ctrl.status_fd = -1; /* No status output. */ /* The configuraton directories for use by gpgrt_argparser. */ gpgrt_set_confdir (GPGRT_CONFDIR_SYS, gnupg_sysconfdir ()); gpgrt_set_confdir (GPGRT_CONFDIR_USER, gnupg_homedir ()); /* We are re-using the struct, thus the reset flag. We OR the * flags so that the internal intialized flag won't be cleared. */ argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; pargs.flags |= (ARGPARSE_FLAG_RESET | ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_SYS | ARGPARSE_FLAG_USER); while (!no_more_options && gpgrt_argparser (&pargs, opts, G13_NAME EXTSEP_S "conf")) { switch (pargs.r_opt) { case ARGPARSE_CONFFILE: { if (debug_argparser) log_info (_("reading options from '%s'\n"), pargs.r_type? pargs.r.ret_str: "[cmdline]"); if (pargs.r_type) { xfree (last_configname); last_configname = xstrdup (pargs.r.ret_str); configname = last_configname; } else configname = NULL; } break; case aGPGConfList: case aGPGConfTest: set_cmd (&cmd, pargs.r_opt); nogreeting = 1; /* nokeysetup = 1; */ break; case aServer: case aMount: case aUmount: case aSuspend: case aResume: case aCreate: case aFindDevice: set_cmd (&cmd, pargs.r_opt); break; case oOutput: opt.outfile = pargs.r.ret_str; break; case oQuiet: opt.quiet = 1; break; case oNoGreeting: nogreeting = 1; break; case oNoTTY: break; case oDryRun: opt.dry_run = 1; break; case oVerbose: opt.verbose++; gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose); break; case oNoVerbose: opt.verbose = 0; gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose); break; case oLogFile: logfile = pargs.r.ret_str; break; case oNoLogFile: logfile = NULL; break; case oNoDetach: /*nodetach = 1; */break; case oDebug: if (parse_debug_flag (pargs.r.ret_str, &opt.debug, debug_flags)) { pargs.r_opt = ARGPARSE_INVALID_ARG; pargs.err = ARGPARSE_PRINT_ERROR; } break; case oDebugAll: debug_value = ~0; break; case oDebugNone: debug_value = 0; break; case oDebugLevel: debug_level = pargs.r.ret_str; break; case oDebugWait: /*debug_wait = pargs.r.ret_int; */break; case oDebugAllowCoreDump: may_coredump = enable_core_dumps (); break; case oStatusFD: ctrl.status_fd = pargs.r.ret_int; break; case oLoggerFD: log_set_fd (pargs.r.ret_int ); break; case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oAgentProgram: opt.agent_program = pargs.r.ret_str; break; case oGpgProgram: opt.gpg_program = pargs.r.ret_str; break; case oDisplay: opt.display = xstrdup (pargs.r.ret_str); break; case oTTYname: opt.ttyname = xstrdup (pargs.r.ret_str); break; case oTTYtype: opt.ttytype = xstrdup (pargs.r.ret_str); break; case oLCctype: opt.lc_ctype = xstrdup (pargs.r.ret_str); break; case oLCmessages: opt.lc_messages = xstrdup (pargs.r.ret_str); break; case oXauthority: opt.xauthority = xstrdup (pargs.r.ret_str); break; case oFakedSystemTime: { time_t faked_time = isotime2epoch (pargs.r.ret_str); if (faked_time == (time_t)(-1)) faked_time = (time_t)strtoul (pargs.r.ret_str, NULL, 10); gnupg_set_time (faked_time, 0); } break; case oNoSecmemWarn: gcry_control (GCRYCTL_DISABLE_SECMEM_WARN); break; case oNoRandomSeedFile: use_random_seed = 0; break; case oRecipient: /* Store the encryption key. */ add_to_strlist (&recipients, pargs.r.ret_str); break; case oType: if (!strcmp (pargs.r.ret_str, "help")) { be_parse_conttype_name (NULL); g13_exit (0); } cmdline_conttype = be_parse_conttype_name (pargs.r.ret_str); if (!cmdline_conttype) { pargs.r_opt = ARGPARSE_INVALID_ARG; pargs.err = ARGPARSE_PRINT_ERROR; } break; default: if (configname) pargs.err = ARGPARSE_PRINT_WARNING; else pargs.err = ARGPARSE_PRINT_ERROR; break; } } gpgrt_argparse (NULL, &pargs, NULL); /* Construct GPG arguments. */ { strlist_t last; last = append_to_strlist (&opt.gpg_arguments, "-z"); last = append_to_strlist (&last, "0"); last = append_to_strlist (&last, "--trust-model"); last = append_to_strlist (&last, "always"); (void) last; } if (!last_configname) opt.config_filename = gpgrt_fnameconcat (gnupg_homedir (), G13_NAME EXTSEP_S "conf", NULL); else { opt.config_filename = last_configname; last_configname = NULL; } if (log_get_errorcount(0)) g13_exit(2); /* Now that we have the options parsed we need to update the default control structure. */ g13_init_default_ctrl (&ctrl); ctrl.recipients = recipients; recipients = NULL; if (nogreeting) greeting = 0; if (greeting) { fprintf (stderr, "%s %s; %s\n", gpgrt_strusage(11), gpgrt_strusage(13), gpgrt_strusage(14) ); fprintf (stderr, "%s\n", gpgrt_strusage(15)); } if (may_coredump && !opt.quiet) log_info (_("WARNING: program may create a core file!\n")); /* Print a warning if an argument looks like an option. */ if (!opt.quiet && !(pargs.flags & ARGPARSE_FLAG_STOP_SEEN)) { int i; for (i=0; i < argc; i++) if (argv[i][0] == '-' && argv[i][1] == '-') log_info (_("Note: '%s' is not considered an option\n"), argv[i]); } if (logfile) { log_set_file (logfile); log_set_prefix (NULL, (GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_TIME | GPGRT_LOG_WITH_PID)); } if (gnupg_faked_time_p ()) { gnupg_isotime_t tbuf; log_info (_("WARNING: running with faked system time: ")); gnupg_get_isotime (tbuf); dump_isotime (tbuf); log_printf ("\n"); } /* Print any pending secure memory warnings. */ gcry_control (GCRYCTL_RESUME_SECMEM_WARN); /* Setup the debug flags for all subsystems. */ set_debug (); /* Install emergency cleanup handler. */ g13_install_emergency_cleanup (); /* Terminate if we found any error until now. */ if (log_get_errorcount(0)) g13_exit (2); /* Set the standard GnuPG random seed file. */ if (use_random_seed) { char *p = make_filename (gnupg_homedir (), "random_seed", NULL); gcry_control (GCRYCTL_SET_RANDOM_SEED_FILE, p); xfree(p); } /* Store given filename into FNAME. */ /* fname = argc? *argv : NULL; */ /* Parse all given encryption keys. This does a lookup of the keys and stops if any of the given keys was not found. */ #if 0 /* Currently not implemented. */ if (!nokeysetup) { strlist_t sl; int failed = 0; for (sl = ctrl->recipients; sl; sl = sl->next) if (check_encryption_key ()) failed = 1; if (failed) g13_exit (1); } #endif /*0*/ /* Dispatch command. */ err = 0; switch (cmd) { case aGPGConfList: { /* List options and default values in the GPG Conf format. */ char *config_filename_esc = percent_escape (opt.config_filename, NULL); printf ("gpgconf-g13.conf:%lu:\"%s\n", GC_OPT_FLAG_DEFAULT, config_filename_esc); xfree (config_filename_esc); printf ("verbose:%lu:\n", GC_OPT_FLAG_NONE); printf ("quiet:%lu:\n", GC_OPT_FLAG_NONE); printf ("debug-level:%lu:\"none:\n", GC_OPT_FLAG_DEFAULT); printf ("log-file:%lu:\n", GC_OPT_FLAG_NONE); } break; case aGPGConfTest: /* This is merely a dummy command to test whether the configuration file is valid. */ break; case aServer: { start_idle_task (); ctrl.no_server = 0; err = g13_server (&ctrl); if (err) log_error ("server exited with error: %s <%s>\n", gpg_strerror (err), gpg_strsource (err)); else g13_request_shutdown (); } break; case aFindDevice: { char *blockdev; if (argc != 1) wrong_args ("--find-device name"); err = call_syshelp_find_device (&ctrl, argv[0], &blockdev); if (err) log_error ("error finding device '%s': %s <%s>\n", argv[0], gpg_strerror (err), gpg_strsource (err)); else puts (blockdev); } break; case aCreate: /* Create a new container. */ { if (argc != 1) wrong_args ("--create filename"); start_idle_task (); 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 g13_request_shutdown (); } break; case aMount: /* Mount a container. */ { if (argc != 1 && argc != 2 ) wrong_args ("--mount filename [mountpoint]"); start_idle_task (); 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)); } break; case aUmount: /* Unmount a mounted container. */ { if (argc != 1) wrong_args ("--umount filename"); err = g13_umount_container (&ctrl, argv[0], NULL); if (err) log_error ("error unmounting container '%s': %s <%s>\n", *argv, gpg_strerror (err), gpg_strsource (err)); } break; case aSuspend: /* Suspend a container. */ { /* Fixme: Should we add a suspend all container option? */ if (argc != 1) wrong_args ("--suspend filename"); err = g13_suspend_container (&ctrl, argv[0]); if (err) log_error ("error suspending container '%s': %s <%s>\n", *argv, gpg_strerror (err), gpg_strsource (err)); } break; case aResume: /* Resume a suspended container. */ { /* Fixme: Should we add a resume all container option? */ if (argc != 1) wrong_args ("--resume filename"); err = g13_resume_container (&ctrl, argv[0]); if (err) log_error ("error resuming container '%s': %s <%s>\n", *argv, gpg_strerror (err), gpg_strsource (err)); } break; default: log_error (_("invalid command (there is no implicit command)\n")); break; } g13_deinit_default_ctrl (&ctrl); if (!err) join_idle_task (); /* Cleanup. */ g13_exit (0); return 8; /*NOTREACHED*/ } /* Store defaults into the per-connection CTRL object. */ void g13_init_default_ctrl (ctrl_t ctrl) { ctrl->conttype = cmdline_conttype? cmdline_conttype : CONTTYPE_ENCFS; } /* Release remaining resources allocated in the CTRL object. */ void g13_deinit_default_ctrl (ctrl_t ctrl) { call_syshelp_release (ctrl); FREE_STRLIST (ctrl->recipients); } /* Request a shutdown. This can be used when the process should * finish instead of running the idle task. */ void g13_request_shutdown (void) { shutdown_pending++; } /* This function is called for each signal we catch. It is run in the main context or the one of a NPth thread and thus it is not restricted in what it may do. */ static void handle_signal (int signo) { switch (signo) { #ifndef HAVE_W32_SYSTEM case SIGHUP: log_info ("SIGHUP received - re-reading configuration\n"); /* Fixme: Not yet implemented. */ break; case SIGUSR1: log_info ("SIGUSR1 received - printing internal information:\n"); /* Fixme: We need to see how to integrate pth dumping into our logging system. */ /* pth_ctrl (PTH_CTRL_DUMPSTATE, log_get_stream ()); */ mountinfo_dump_all (); break; case SIGUSR2: log_info ("SIGUSR2 received - no action defined\n"); break; case SIGTERM: if (!shutdown_pending) log_info ("SIGTERM received - shutting down ...\n"); else log_info ("SIGTERM received - still %u runners active\n", runner_get_threads ()); shutdown_pending++; if (shutdown_pending > 2) { log_info ("shutdown forced\n"); log_info ("%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13) ); g13_exit (0); } break; case SIGINT: log_info ("SIGINT received - immediate shutdown\n"); log_info( "%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13)); g13_exit (0); break; #endif /*!HAVE_W32_SYSTEM*/ default: log_info ("signal %d received - no action defined\n", signo); } } /* This ticker function is called about every TIMERTICK_INTERVAL_SEC seconds. */ static void handle_tick (void) { /* log_debug ("TICK\n"); */ } /* The idle task. We use a separate thread to do idle stuff and to catch signals. */ static void * idle_task (void *dummy_arg) { int signo; /* The number of a raised signal is stored here. */ int saved_errno; struct timespec abstime; struct timespec curtime; struct timespec timeout; int ret; (void)dummy_arg; /* Create the event to catch the signals. */ #ifndef HAVE_W32_SYSTEM npth_sigev_init (); npth_sigev_add (SIGHUP); npth_sigev_add (SIGUSR1); npth_sigev_add (SIGUSR2); npth_sigev_add (SIGINT); npth_sigev_add (SIGTERM); npth_sigev_fini (); #endif npth_clock_gettime (&abstime); abstime.tv_sec += TIMERTICK_INTERVAL_SEC; for (;;) { /* The shutdown flag allows us to terminate the idle task. */ if (shutdown_pending) { runner_cancel_all (); if (!runner_get_threads ()) break; /* ready */ } npth_clock_gettime (&curtime); if (!(npth_timercmp (&curtime, &abstime, <))) { /* Timeout. */ handle_tick (); npth_clock_gettime (&abstime); abstime.tv_sec += TIMERTICK_INTERVAL_SEC; } npth_timersub (&abstime, &curtime, &timeout); #ifndef HAVE_W32_SYSTEM ret = npth_pselect (0, NULL, NULL, NULL, &timeout, npth_sigev_sigmask()); saved_errno = errno; while (npth_sigev_get_pending(&signo)) handle_signal (signo); #else ret = npth_eselect (0, NULL, NULL, NULL, &timeout, NULL, NULL); saved_errno = errno; #endif if (ret == -1 && saved_errno != EINTR) { log_error (_("npth_pselect failed: %s - waiting 1s\n"), strerror (saved_errno)); npth_sleep (1); continue; } if (ret <= 0) { /* Interrupt or timeout. Will be handled when calculating the next timeout. */ continue; } /* Here one would add processing of file descriptors. */ } log_info (_("%s %s stopped\n"), gpgrt_strusage(11), gpgrt_strusage(13)); return NULL; } /* Start the idle task. */ static void start_idle_task (void) { npth_attr_t tattr; npth_t thread; sigset_t sigs; /* The set of signals we want to catch. */ int err; #ifndef HAVE_W32_SYSTEM /* These signals should always go to the idle task, so they need to be blocked everywhere else. We assume start_idle_task is called from the main thread before any other threads are created. */ sigemptyset (&sigs); sigaddset (&sigs, SIGHUP); sigaddset (&sigs, SIGUSR1); sigaddset (&sigs, SIGUSR2); sigaddset (&sigs, SIGINT); sigaddset (&sigs, SIGTERM); npth_sigmask (SIG_BLOCK, &sigs, NULL); #endif npth_attr_init (&tattr); npth_attr_setdetachstate (&tattr, NPTH_CREATE_JOINABLE); err = npth_create (&thread, &tattr, idle_task, NULL); if (err) { log_fatal ("error starting idle task: %s\n", strerror (err)); return; /*NOTREACHED*/ } npth_setname_np (thread, "idle-task"); idle_task_thread = thread; npth_attr_destroy (&tattr); } /* Wait for the idle task to finish. */ static void join_idle_task (void) { int err; /* FIXME: This assumes that a valid pthread_t is non-null. That is not guaranteed. */ if (idle_task_thread) { err = npth_join (idle_task_thread, NULL); if (err) log_error ("waiting for idle task thread failed: %s\n", strerror (err)); } } diff --git a/g13/mount.c b/g13/mount.c index 45b60806c..071b76b67 100644 --- a/g13/mount.c +++ b/g13/mount.c @@ -1,265 +1,266 @@ /* 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 . */ #include #include #include #include #include #include #include #include #include "g13.h" #include "../common/i18n.h" #include "mount.h" #include "keyblob.h" #include "backend.h" #include "g13tuple.h" #include "mountinfo.h" -#include "runner.h" #include "../common/host2net.h" #include "server.h" /*(g13_keyblob_decrypt)*/ #include "../common/sysutils.h" +#include "../common/exechelp.h" +#include "runner.h" #include "call-syshelp.h" /* 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; int needs_syshelp = 0; 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; unsigned int rid; char *mountpoint_buffer = NULL; char *blockdev_buffer = NULL; /* Decide whether we need to use the g13-syshelp. */ err = call_syshelp_find_device (ctrl, filename, &blockdev_buffer); if (!err) { needs_syshelp = 1; filename = blockdev_buffer; } else if (gpg_err_code (err) != GPG_ERR_NOT_FOUND) { log_error ("error finding device '%s': %s <%s>\n", filename, gpg_strerror (err), gpg_strsource (err)); return err; } else { /* A quick check to see whether we can the container exists. */ if (gnupg_access (filename, R_OK)) return gpg_error_from_syserror (); } if (!mountpoint) { mountpoint_buffer = xtrystrdup ("/tmp/g13-XXXXXX"); if (!mountpoint_buffer) return gpg_error_from_syserror (); if (!gnupg_mkdtemp (mountpoint_buffer)) { err = gpg_error_from_syserror (); log_error (_("can't create directory '%s': %s\n"), "/tmp/g13-XXXXXX", gpg_strerror (err)); xfree (mountpoint_buffer); return err; } mountpoint = mountpoint_buffer; } err = 0; if (needs_syshelp) lock = NULL; else { /* Try to take a lock. */ lock = dotlock_create (filename, 0); if (!lock) { xfree (mountpoint_buffer); return gpg_error_from_syserror (); } if (dotlock_take (lock, 0)) { err = gpg_error_from_syserror (); goto leave; } } /* Check again that the file exists. */ if (!needs_syshelp) { struct stat sb; if (gnupg_stat (filename, &sb)) { err = gpg_error_from_syserror (); goto leave; } } /* Read the encrypted keyblob. */ if (needs_syshelp) { err = call_syshelp_set_device (ctrl, filename); if (err) goto leave; err = call_syshelp_get_keyblob (ctrl, &enckeyblob, &enckeybloblen); } else err = g13_keyblob_read (filename, &enckeyblob, &enckeybloblen); if (err) goto leave; /* Decrypt that keyblob and store it in a tuple descriptor. */ err = g13_keyblob_decrypt (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_tupledesc (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, &rid); if (err) ; else if (conttype == CONTTYPE_DM_CRYPT) g13_request_shutdown (); else { /* Unless this is a DM-CRYPT mount we put it into our mounttable so that we can manage the mounts ourselves. For dm-crypt we do not keep a process to monitor he mounts (for now). */ err = mountinfo_add_mount (filename, mountpoint, conttype, rid, !!mountpoint_buffer); /* Fixme: What shall we do if this fails? Add a provisional mountinfo entry first and remove it on error? */ if (!err) { char *tmp = percent_plus_escape (mountpoint); if (!tmp) err = gpg_error_from_syserror (); else { g13_status (ctrl, STATUS_MOUNTPOINT, tmp, NULL); xfree (tmp); } } } leave: destroy_tupledesc (tuples); xfree (keyblob); xfree (enckeyblob); dotlock_destroy (lock); xfree (mountpoint_buffer); xfree (blockdev_buffer); return err; } /* Unmount the container with name FILENAME or the one mounted at MOUNTPOINT. If both are given the FILENAME takes precedence. */ gpg_error_t g13_umount_container (ctrl_t ctrl, const char *filename, const char *mountpoint) { gpg_error_t err; char *blockdev; if (!filename && !mountpoint) return gpg_error (GPG_ERR_ENOENT); /* Decide whether we need to use the g13-syshelp. */ err = call_syshelp_find_device (ctrl, filename, &blockdev); if (!err) { /* Need to employ the syshelper to umount the file system. */ /* FIXME: We should get the CONTTYPE from the blockdev. */ err = be_umount_container (ctrl, CONTTYPE_DM_CRYPT, blockdev); if (!err) { /* if (conttype == CONTTYPE_DM_CRYPT) */ g13_request_shutdown (); } } else if (gpg_err_code (err) != GPG_ERR_NOT_FOUND) { log_error ("error finding device '%s': %s <%s>\n", filename, gpg_strerror (err), gpg_strsource (err)); } else { /* Not in g13tab - kill the runner process for this mount. */ unsigned int rid; runner_t runner; err = mountinfo_find_mount (filename, mountpoint, &rid); if (err) return err; runner = runner_find_by_rid (rid); if (!runner) { log_error ("runner %u not found\n", rid); return gpg_error (GPG_ERR_NOT_FOUND); } runner_cancel (runner); runner_release (runner); } xfree (blockdev); return err; } diff --git a/g13/runner.c b/g13/runner.c index b08d99030..c0534fe5d 100644 --- a/g13/runner.c +++ b/g13/runner.c @@ -1,539 +1,541 @@ /* runner.c - Run and watch the backend engines * 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 . */ #include #include #include #include #include #include #include #include #include "g13.h" #include "../common/i18n.h" #include "keyblob.h" -#include "runner.h" #include "../common/exechelp.h" +#include "runner.h" #include "mountinfo.h" /* The runner object. */ struct runner_s { char *name; /* The name of this runner. */ unsigned int identifier; /* The runner identifier. */ int spawned; /* True if runner_spawn has been called. */ npth_t thread; /* The TID of the runner thread. */ runner_t next_running; /* Builds a list of all running threads. */ int canceled; /* Set if a cancel has already been send once. */ int cancel_flag; /* If set the thread should terminate itself. */ /* We use a reference counter to know when it is safe to remove the object. Lacking an explicit ref function this counter will take only these two values: 1 = Thread not running or only the thread is still running. 2 = Thread is running and someone is holding a reference. */ int refcount; - pid_t pid; /* PID of the backend's process (the engine). */ + gnupg_process_t proc; /* Process of the backend's process (the engine). */ int in_fd; /* File descriptors to read from the engine. */ int out_fd; /* File descriptors to write to the engine. */ engine_handler_fnc_t handler; /* The handler functions. */ engine_handler_cleanup_fnc_t handler_cleanup; void *handler_data; /* Private data of HANDLER and HANDLER_CLEANUP. */ /* Instead of IN_FD we use an estream. Note that the runner thread may close the stream and set status_fp to NULL at any time. Thus it won't be a good idea to use it while the runner thread is running. */ estream_t status_fp; }; /* The head of the list of all running threads. */ static runner_t running_threads; /* Write NBYTES of BUF to file descriptor FD. */ static int writen (int fd, const void *buf, size_t nbytes) { size_t nleft = nbytes; int nwritten; while (nleft > 0) { nwritten = npth_write (fd, buf, nleft); if (nwritten < 0) { if (errno == EINTR) nwritten = 0; else return -1; } nleft -= nwritten; buf = (const char*)buf + nwritten; } return 0; } static int check_already_spawned (runner_t runner, const char *funcname) { if (runner->spawned) { log_error ("BUG: runner already spawned - ignoring call to %s\n", funcname); return 1; } else return 0; } /* Return the number of active threads. */ unsigned int runner_get_threads (void) { unsigned int n = 0; runner_t r; for (r = running_threads; r; r = r->next_running) n++; return n; } /* The public release function. */ void runner_release (runner_t runner) { gpg_error_t err; if (!runner) return; if (!--runner->refcount) return; err = mountinfo_del_mount (NULL, NULL, runner->identifier); if (err) log_error ("failed to remove mount with rid %u from mtab: %s\n", runner->identifier, gpg_strerror (err)); es_fclose (runner->status_fp); if (runner->in_fd != -1) close (runner->in_fd); if (runner->out_fd != -1) close (runner->out_fd); /* Fixme: close the process. */ /* Tell the engine to release its data. */ if (runner->handler_cleanup) runner->handler_cleanup (runner->handler_data); - if (runner->pid != (pid_t)(-1)) + if (runner->proc) { /* The process has not been cleaned up - do it now. */ - gnupg_kill_process (runner->pid); + gnupg_process_terminate (runner->proc); /* (Actually we should use the program name and not the arbitrary NAME of the runner object. However it does not matter because that information is only used for diagnostics.) */ - gnupg_wait_process (runner->name, runner->pid, 1, NULL); - gnupg_release_process (runner->pid); + gnupg_process_wait (runner->proc, 1); + gnupg_process_release (runner->proc); } xfree (runner->name); xfree (runner); } /* Create a new runner context. On success a new runner object is stored at R_RUNNER. On failure NULL is stored at this address and an error code returned. */ gpg_error_t runner_new (runner_t *r_runner, const char *name) { static unsigned int namecounter; /* Global name counter. */ char *namebuffer; runner_t runner, r; *r_runner = NULL; runner = xtrycalloc (1, sizeof *runner); if (!runner) return gpg_error_from_syserror (); /* Bump up the namecounter. In case we ever had an overflow we check that this number is currently not in use. The algorithm is a bit lame but should be sufficient because such an wrap is not very likely: Assuming that we do a mount 10 times a second, then we would overwrap on a 32 bit system after 13 years. */ do { namecounter++; for (r = running_threads; r; r = r->next_running) if (r->identifier == namecounter) break; } while (r); runner->identifier = namecounter; runner->name = namebuffer = xtryasprintf ("%s-%d", name, namecounter); if (!runner->name) { xfree (runner); return gpg_error_from_syserror (); } runner->refcount = 1; - runner->pid = (pid_t)(-1); + runner->proc = NULL; runner->in_fd = -1; runner->out_fd = -1; *r_runner = runner; return 0; } /* Return the identifier of RUNNER. */ unsigned int runner_get_rid (runner_t runner) { return runner->identifier; } /* Find a runner by its rid. Returns the runner object. The caller must release the runner object. */ runner_t runner_find_by_rid (unsigned int rid) { runner_t r; for (r = running_threads; r; r = r->next_running) if (r->identifier == rid) { r->refcount++; return r; } return NULL; } /* A runner usually maintains two file descriptors to control the backend engine. This function is used to set these file descriptors. The function takes ownership of these file descriptors. IN_FD will be used to read from engine and OUT_FD to send data to the engine. */ void runner_set_fds (runner_t runner, int in_fd, int out_fd) { if (check_already_spawned (runner, "runner_set_fds")) return; if (runner->in_fd != -1) close (runner->in_fd); if (runner->out_fd != -1) close (runner->out_fd); runner->in_fd = in_fd; runner->out_fd = out_fd; } -/* Set the PID of the backend engine. After this call the engine is +/* Set the PROC of the backend engine. After this call the engine is owned by the runner object. */ void -runner_set_pid (runner_t runner, pid_t pid) +runner_set_proc (runner_t runner, gnupg_process_t proc) { - if (check_already_spawned (runner, "runner_set_fds")) + if (check_already_spawned (runner, "runner_set_proc")) return; - runner->pid = pid; + runner->proc = proc; } /* Register the engine handler functions HANDLER and HANDLER_CLEANUP and its private HANDLER_DATA with RUNNER. */ void runner_set_handler (runner_t runner, engine_handler_fnc_t handler, engine_handler_cleanup_fnc_t handler_cleanup, void *handler_data) { if (check_already_spawned (runner, "runner_set_handler")) return; runner->handler = handler; runner->handler_cleanup = handler_cleanup; runner->handler_data = handler_data; } /* The thread spawned by runner_spawn. */ static void * runner_thread (void *arg) { runner_t runner = arg; gpg_error_t err = 0; log_debug ("starting runner thread\n"); /* If a status_fp is available, the thread's main task is to read from that stream and invoke the backend's handler function. This is done on a line by line base and the line length is limited to a reasonable value (about 1000 characters). Other work will continue either due to an EOF of the stream or by demand of the engine. */ if (runner->status_fp) { int c, cont_line; unsigned int pos; char buffer[1024]; estream_t fp = runner->status_fp; pos = 0; cont_line = 0; while (!err && !runner->cancel_flag && (c=es_getc (fp)) != EOF) { buffer[pos++] = c; if (pos >= sizeof buffer - 5 || c == '\n') { buffer[pos - (c == '\n')] = 0; if (opt.verbose) log_info ("%s%s: %s\n", runner->name, cont_line? "(cont)":"", buffer); /* We handle only complete lines and ignore any stuff we possibly had to truncate. That is - at least for the encfs engine - not an issue because our changes to the tool make sure that only relatively short prompt lines are of interest. */ if (!cont_line && runner->handler) err = runner->handler (runner->handler_data, runner, buffer); pos = 0; cont_line = (c != '\n'); } } if (!err && runner->cancel_flag) log_debug ("runner thread noticed cancel flag\n"); else log_debug ("runner thread saw EOF\n"); if (pos) { buffer[pos] = 0; if (opt.verbose) log_info ("%s%s: %s\n", runner->name, cont_line? "(cont)":"", buffer); if (!cont_line && !err && runner->handler) err = runner->handler (runner->handler_data, runner, buffer); } if (!err && es_ferror (fp)) { err = gpg_error_from_syserror (); log_error ("error reading from %s: %s\n", runner->name, gpg_strerror (err)); } runner->status_fp = NULL; es_fclose (fp); log_debug ("runner thread closed status fp\n"); } /* Now wait for the process to finish. */ - if (!err && runner->pid != (pid_t)(-1)) + if (!err && runner->proc) { int exitcode; log_debug ("runner thread waiting ...\n"); - err = gnupg_wait_process (runner->name, runner->pid, 1, &exitcode); - gnupg_release_process (runner->pid); - runner->pid = (pid_t)(-1); - if (err) + err = gnupg_process_wait (runner->proc, 1); + if (!err) + gnupg_process_ctl (runner->proc, GNUPG_PROCESS_GET_EXIT_ID, &exitcode); + gnupg_process_release (runner->proc); + runner->proc = NULL; + if (exitcode) log_error ("running '%s' failed (exitcode=%d): %s\n", runner->name, exitcode, gpg_strerror (err)); log_debug ("runner thread waiting finished\n"); } /* Get rid of the runner object (note: it is refcounted). */ log_debug ("runner thread releasing runner ...\n"); { runner_t r, rprev; for (r = running_threads, rprev = NULL; r; rprev = r, r = r->next_running) if (r == runner) { if (!rprev) running_threads = r->next_running; else rprev->next_running = r->next_running; r->next_running = NULL; break; } } runner_release (runner); log_debug ("runner thread runner released\n"); return NULL; } /* Spawn a new thread to let RUNNER work as a coprocess. */ gpg_error_t runner_spawn (runner_t runner) { gpg_error_t err; npth_attr_t tattr; npth_t thread; int ret; if (check_already_spawned (runner, "runner_spawn")) return gpg_error (GPG_ERR_BUG); /* In case we have an input fd, open it as an estream so that the Pth scheduling will work. The stdio functions don't work with Pth because they don't call the pth counterparts of read and write unless linker tricks are used. */ if (runner->in_fd != -1) { estream_t fp; fp = es_fdopen (runner->in_fd, "r"); if (!fp) { err = gpg_error_from_syserror (); log_error ("can't fdopen pipe for reading: %s\n", gpg_strerror (err)); return err; } runner->status_fp = fp; runner->in_fd = -1; /* Now owned by status_fp. */ } npth_attr_init (&tattr); npth_attr_setdetachstate (&tattr, NPTH_CREATE_DETACHED); ret = npth_create (&thread, &tattr, runner_thread, runner); if (ret) { err = gpg_error_from_errno (ret); log_error ("error spawning runner thread: %s\n", gpg_strerror (err)); return err; } npth_setname_np (thread, runner->name); /* The scheduler has not yet kicked in, thus we can safely set the spawned flag and the tid. */ runner->spawned = 1; runner->thread = thread; runner->next_running = running_threads; running_threads = runner; npth_attr_destroy (&tattr); /* The runner thread is now runnable. */ return 0; } /* Cancel a running thread. */ void runner_cancel (runner_t runner) { /* Warning: runner_cancel_all has knowledge of this code. */ if (runner->spawned) { runner->canceled = 1; /* Mark that we canceled this one already. */ /* FIXME: This does only work if the thread emits status lines. We need to change the thread to wait on an event. */ runner->cancel_flag = 1; /* For now we use the brutal way and kill the process. */ - gnupg_kill_process (runner->pid); + gnupg_process_terminate (runner->proc); } } /* Cancel all runner threads. */ void runner_cancel_all (void) { runner_t r; do { for (r = running_threads; r; r = r->next_running) if (r->spawned && !r->canceled) { runner_cancel (r); break; } } while (r); } /* Send a line of data down to the engine. This line may not contain a binary Nul or a LF character. This function is used by the engine's handler. */ gpg_error_t runner_send_line (runner_t runner, const void *data, size_t datalen) { gpg_error_t err = 0; if (!runner->spawned) { log_error ("BUG: runner for %s not spawned\n", runner->name); err = gpg_error (GPG_ERR_INTERNAL); } else if (runner->out_fd == -1) { log_error ("no output file descriptor for runner %s\n", runner->name); err = gpg_error (GPG_ERR_EBADF); } else if (data && datalen) { if (memchr (data, '\n', datalen)) { log_error ("LF detected in response data\n"); err = gpg_error (GPG_ERR_BUG); } else if (memchr (data, 0, datalen)) { log_error ("Nul detected in response data\n"); err = gpg_error (GPG_ERR_BUG); } else if (writen (runner->out_fd, data, datalen)) err = gpg_error_from_syserror (); } if (!err) if (writen (runner->out_fd, "\n", 1)) err = gpg_error_from_syserror (); return err; } diff --git a/g13/runner.h b/g13/runner.h index 36181adf9..01c395e02 100644 --- a/g13/runner.h +++ b/g13/runner.h @@ -1,76 +1,76 @@ /* runner.h - Run and watch the backend engines * 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 . */ #ifndef G13_RUNNER_H #define G13_RUNNER_H /* The runner object. */ struct runner_s; typedef struct runner_s *runner_t; /* Prototypes for the handler functions provided by the engine. */ typedef gpg_error_t (*engine_handler_fnc_t) (void *opaque, runner_t runner, const char *statusline); typedef void (*engine_handler_cleanup_fnc_t) (void *opaque); /* Return the number of active threads. */ unsigned int runner_get_threads (void); /* Create a new runner object. */ gpg_error_t runner_new (runner_t *r_runner, const char *name); /* Free a runner object. */ void runner_release (runner_t runner); /* Return the identifier of RUNNER. */ unsigned int runner_get_rid (runner_t runner); /* Find a runner by its rid. */ runner_t runner_find_by_rid (unsigned int rid); /* Functions to set properties of the runner. */ void runner_set_fds (runner_t runner, int in_fd, int out_fd); -void runner_set_pid (runner_t runner, pid_t pid); +void runner_set_proc (runner_t runner, gnupg_process_t proc); /* Register the handler functions with a runner. */ void runner_set_handler (runner_t runner, engine_handler_fnc_t handler, engine_handler_cleanup_fnc_t handler_cleanup, void *handler_data); /* Start the runner. */ gpg_error_t runner_spawn (runner_t runner); /* Cancel a runner. */ void runner_cancel (runner_t runner); /* Cancel all runner. */ void runner_cancel_all (void); /* Send data back to the engine. This function is used by the engine's handler. */ gpg_error_t runner_send_line (runner_t runner, const void *data, size_t datalen); #endif /*G13_RUNNER_H*/