2003-08-05 19:11:04 +02:00
|
|
|
|
/* scdaemon.c - The GnuPG Smartcard Daemon
|
2005-05-18 12:48:06 +02:00
|
|
|
|
* Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
|
2003-08-05 19:11:04 +02:00
|
|
|
|
*
|
|
|
|
|
* 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 2 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, write to the Free Software
|
2006-06-20 19:21:37 +02:00
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
|
* USA.
|
2003-08-05 19:11:04 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <fcntl.h>
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <signal.h>
|
2005-05-18 12:48:06 +02:00
|
|
|
|
#include <pth.h>
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
#define JNLIB_NEED_LOG_LOGV
|
|
|
|
|
#include "scdaemon.h"
|
|
|
|
|
#include <ksba.h>
|
|
|
|
|
#include <gcrypt.h>
|
|
|
|
|
|
|
|
|
|
#include <assuan.h> /* malloc hooks */
|
|
|
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
#include "sysutils.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#include "app-common.h"
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
#include "../jnlib/w32-afunix.h"
|
|
|
|
|
#endif
|
2005-05-20 22:39:36 +02:00
|
|
|
|
#include "ccid-driver.h"
|
* configure.ac (gl_INIT): Add gnulib stuff.
(fseeko, ftello, ttyname, isascii): Replaced the AC_REPLACE_FUNCS
by a simple check.
(putc_unlocked): Removed check. Not used.
(strsep, mkdtemp, asprintf): Replaced checks by gnulib checks.
(xsize): Added will probably come handy soon.
(CFLAGS): Use -Wformat-security instead of
-Wformat-nonliteral. Add --Wno-format-y2k.
* gl/, gl/m4/: New.
* gpg-agent.c: Include setenv.h.
* Makefile.am (AM_CPPFLAGS): Added.
* util.h: Add some includes for gnulib.
(ttyname, isascii): Define them inline.
* fseeko.c, ftello.c: Removed.
* strsep.c, mkdtemp.c: Removed.
* ttyname.c, isascii.c: Removed.
* mkdtemp.c: Removed.
* exec.c: Include mkdtemp.h
* keybox-file.c (ftello) [!HAVE_FSEEKO]: New replacement
function. Copied from ../common/ftello.c.
* keybox-update.c (fseeko) [!HAVE_FSEEKO]: New replacement
function. Copied from ../common/iobuf.c.
* scdaemon.c: Include mkdtemp.h.
* misc.c: Include setenv.h.
* symcryptrun.c: Include mkdtemp.h.
2005-06-01 17:46:01 +02:00
|
|
|
|
#include "mkdtemp.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
enum cmd_and_opt_values
|
|
|
|
|
{ aNull = 0,
|
|
|
|
|
oCsh = 'c',
|
|
|
|
|
oQuiet = 'q',
|
|
|
|
|
oSh = 's',
|
|
|
|
|
oVerbose = 'v',
|
|
|
|
|
|
|
|
|
|
oNoVerbose = 500,
|
2004-02-18 18:00:11 +01:00
|
|
|
|
aGPGConfList,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
oOptions,
|
|
|
|
|
oDebug,
|
|
|
|
|
oDebugAll,
|
2004-02-18 18:00:11 +01:00
|
|
|
|
oDebugLevel,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
oDebugWait,
|
2005-06-07 21:09:18 +02:00
|
|
|
|
oDebugAllowCoreDump,
|
2005-05-20 22:39:36 +02:00
|
|
|
|
oDebugCCIDDriver,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
oNoGreeting,
|
|
|
|
|
oNoOptions,
|
|
|
|
|
oHomedir,
|
|
|
|
|
oNoDetach,
|
|
|
|
|
oNoGrab,
|
|
|
|
|
oLogFile,
|
|
|
|
|
oServer,
|
2005-05-18 12:48:06 +02:00
|
|
|
|
oMultiServer,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
oDaemon,
|
|
|
|
|
oBatch,
|
|
|
|
|
oReaderPort,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
octapiDriver,
|
2003-08-19 11:36:48 +02:00
|
|
|
|
opcscDriver,
|
2003-09-02 21:06:34 +02:00
|
|
|
|
oDisableCCID,
|
2003-08-18 19:34:28 +02:00
|
|
|
|
oDisableOpenSC,
|
2005-11-28 12:52:25 +01:00
|
|
|
|
oDisableKeypad,
|
2003-12-01 11:54:09 +01:00
|
|
|
|
oAllowAdmin,
|
|
|
|
|
oDenyAdmin,
|
2004-08-05 11:24:36 +02:00
|
|
|
|
oDisableApplication,
|
2005-05-20 22:39:36 +02:00
|
|
|
|
oDebugDisableTicker
|
|
|
|
|
};
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static ARGPARSE_OPTS opts[] = {
|
2004-02-18 18:00:11 +01:00
|
|
|
|
|
|
|
|
|
{ aGPGConfList, "gpgconf-list", 256, "@" },
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
{ 301, NULL, 0, N_("@Options:\n ") },
|
|
|
|
|
|
|
|
|
|
{ oServer, "server", 0, N_("run in server mode (foreground)") },
|
2005-05-20 22:39:36 +02:00
|
|
|
|
{ oMultiServer, "multi-server", 0,
|
|
|
|
|
N_("run in multi server mode (foreground)") },
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{ oDaemon, "daemon", 0, N_("run in daemon mode (background)") },
|
|
|
|
|
{ oVerbose, "verbose", 0, N_("verbose") },
|
|
|
|
|
{ oQuiet, "quiet", 0, N_("be somewhat more quiet") },
|
|
|
|
|
{ oSh, "sh", 0, N_("sh-style command output") },
|
|
|
|
|
{ oCsh, "csh", 0, N_("csh-style command output") },
|
|
|
|
|
{ oOptions, "options" , 2, N_("read options from file")},
|
2004-02-18 18:00:11 +01:00
|
|
|
|
{ oDebug, "debug" ,4|16, "@"},
|
|
|
|
|
{ oDebugAll, "debug-all" ,0, "@"},
|
|
|
|
|
{ oDebugLevel, "debug-level" ,2, "@"},
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{ oDebugWait,"debug-wait",1, "@"},
|
2005-06-07 21:09:18 +02:00
|
|
|
|
{ oDebugAllowCoreDump, "debug-allow-core-dump", 0, "@" },
|
2005-05-20 22:39:36 +02:00
|
|
|
|
{ oDebugCCIDDriver, "debug-ccid-driver", 0, "@"},
|
|
|
|
|
{ oDebugDisableTicker, "debug-disable-ticker", 0, "@"},
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{ oNoDetach, "no-detach" ,0, N_("do not detach from the console")},
|
|
|
|
|
{ oLogFile, "log-file" ,2, N_("use a log file for the server")},
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{ oReaderPort, "reader-port", 2, N_("|N|connect to reader at port N")},
|
2004-03-16 19:59:21 +01:00
|
|
|
|
{ octapiDriver, "ctapi-driver", 2, N_("|NAME|use NAME as ct-API driver")},
|
|
|
|
|
{ opcscDriver, "pcsc-driver", 2, N_("|NAME|use NAME as PC/SC driver")},
|
2004-01-27 17:40:42 +01:00
|
|
|
|
{ oDisableCCID, "disable-ccid", 0,
|
2003-09-02 21:06:34 +02:00
|
|
|
|
#ifdef HAVE_LIBUSB
|
|
|
|
|
N_("do not use the internal CCID driver")
|
|
|
|
|
#else
|
|
|
|
|
"@"
|
|
|
|
|
#endif
|
|
|
|
|
/* end --disable-ccid */},
|
2005-11-28 12:52:25 +01:00
|
|
|
|
{ oDisableKeypad, "disable-keypad", 0, N_("do not use a reader's keypad")},
|
2003-12-01 11:54:09 +01:00
|
|
|
|
{ oAllowAdmin, "allow-admin", 0, N_("allow the use of admin card commands")},
|
|
|
|
|
{ oDenyAdmin, "deny-admin", 0, "@" },
|
2004-08-05 11:24:36 +02:00
|
|
|
|
{ oDisableApplication, "disable-application", 2, "@"},
|
2003-08-18 19:34:28 +02:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{0}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
|
/* The card dirver we use by default for PC/SC. */
|
2005-11-28 12:52:25 +01:00
|
|
|
|
#if defined(HAVE_W32_SYSTEM) || defined(__CYGWIN__)
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#define DEFAULT_PCSC_DRIVER "winscard.dll"
|
2006-04-11 15:53:21 +02:00
|
|
|
|
#elif defined(__GLIBC__)
|
|
|
|
|
#define DEFAULT_PCSC_DRIVER "libpcsclite.so.1"
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#else
|
2004-03-16 19:59:21 +01:00
|
|
|
|
#define DEFAULT_PCSC_DRIVER "libpcsclite.so"
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#endif
|
2004-03-16 19:59:21 +01:00
|
|
|
|
|
|
|
|
|
|
2004-04-20 16:17:10 +02:00
|
|
|
|
/* Flag to indicate that a shutdown was requested. */
|
|
|
|
|
static int shutdown_pending;
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* It is possible that we are currently running under setuid permissions */
|
|
|
|
|
static int maybe_setuid = 1;
|
|
|
|
|
|
|
|
|
|
/* Name of the communication socket */
|
2005-05-18 12:48:06 +02:00
|
|
|
|
static char *socket_name;
|
|
|
|
|
|
2005-05-20 22:39:36 +02:00
|
|
|
|
|
|
|
|
|
/* Debug flag to disable the ticker. The ticker is in fact not
|
|
|
|
|
disabled but it won't perform any ticker specific actions. */
|
|
|
|
|
static int ticker_disabled;
|
|
|
|
|
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
|
|
|
|
|
static char *create_socket_name (int use_standard_socket,
|
|
|
|
|
char *standard_name, char *template);
|
|
|
|
|
static int create_server_socket (int is_standard_name, const char *name);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
static void *start_connection_thread (void *arg);
|
|
|
|
|
static void handle_connections (int listen_fd);
|
2004-04-20 16:17:10 +02:00
|
|
|
|
|
|
|
|
|
/* Pth wrapper function definitions. */
|
|
|
|
|
GCRY_THREAD_OPTION_PTH_IMPL;
|
|
|
|
|
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static const char *
|
|
|
|
|
my_strusage (int level)
|
|
|
|
|
{
|
|
|
|
|
const char *p;
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
|
|
|
|
case 11: p = "scdaemon (GnuPG)";
|
|
|
|
|
break;
|
|
|
|
|
case 13: p = VERSION; 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: scdaemon [options] (-h for help)");
|
|
|
|
|
break;
|
|
|
|
|
case 41: p = _("Syntax: scdaemon [options] [command [args]]\n"
|
|
|
|
|
"Smartcard daemon for GnuPG\n");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: p = NULL;
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
i18n_init (void)
|
|
|
|
|
{
|
|
|
|
|
#ifdef USE_SIMPLE_GETTEXT
|
2004-04-06 12:01:04 +02:00
|
|
|
|
set_gettext_file( PACKAGE_GT );
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#else
|
|
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
|
setlocale (LC_ALL, "");
|
2004-04-06 12:01:04 +02:00
|
|
|
|
bindtextdomain (PACKAGE_GT, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE_GT);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Used by gcry for logging */
|
|
|
|
|
static void
|
|
|
|
|
my_gcry_logger (void *dummy, int level, const char *fmt, va_list arg_ptr)
|
|
|
|
|
{
|
|
|
|
|
/* translate the log levels */
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
|
|
|
|
case GCRY_LOG_CONT: level = JNLIB_LOG_CONT; break;
|
|
|
|
|
case GCRY_LOG_INFO: level = JNLIB_LOG_INFO; break;
|
|
|
|
|
case GCRY_LOG_WARN: level = JNLIB_LOG_WARN; break;
|
|
|
|
|
case GCRY_LOG_ERROR:level = JNLIB_LOG_ERROR; break;
|
|
|
|
|
case GCRY_LOG_FATAL:level = JNLIB_LOG_FATAL; break;
|
|
|
|
|
case GCRY_LOG_BUG: level = JNLIB_LOG_BUG; break;
|
|
|
|
|
case GCRY_LOG_DEBUG:level = JNLIB_LOG_DEBUG; break;
|
|
|
|
|
default: level = JNLIB_LOG_ERROR; break;
|
|
|
|
|
}
|
|
|
|
|
log_logv (level, fmt, arg_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-18 18:00:11 +01:00
|
|
|
|
/* Setup the debugging. With a LEVEL of NULL only the active debug
|
|
|
|
|
flags are propagated to the subsystems. With LEVEL set, a specific
|
|
|
|
|
set of debug flags is set; thus overriding all flags already
|
|
|
|
|
set. */
|
|
|
|
|
static void
|
|
|
|
|
set_debug (const char *level)
|
|
|
|
|
{
|
|
|
|
|
if (!level)
|
|
|
|
|
;
|
|
|
|
|
else if (!strcmp (level, "none"))
|
|
|
|
|
opt.debug = 0;
|
|
|
|
|
else if (!strcmp (level, "basic"))
|
|
|
|
|
opt.debug = DBG_ASSUAN_VALUE;
|
|
|
|
|
else if (!strcmp (level, "advanced"))
|
|
|
|
|
opt.debug = DBG_ASSUAN_VALUE|DBG_COMMAND_VALUE;
|
|
|
|
|
else if (!strcmp (level, "expert"))
|
|
|
|
|
opt.debug = (DBG_ASSUAN_VALUE|DBG_COMMAND_VALUE
|
|
|
|
|
|DBG_CACHE_VALUE|DBG_CARD_IO_VALUE);
|
|
|
|
|
else if (!strcmp (level, "guru"))
|
|
|
|
|
opt.debug = ~0;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log_error (_("invalid debug-level `%s' given\n"), level);
|
|
|
|
|
scd_exit(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (opt.debug && !opt.verbose)
|
|
|
|
|
opt.verbose = 1;
|
|
|
|
|
if (opt.debug && opt.quiet)
|
|
|
|
|
opt.quiet = 0;
|
|
|
|
|
|
|
|
|
|
if (opt.debug & DBG_MPI_VALUE)
|
|
|
|
|
gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 2);
|
|
|
|
|
if (opt.debug & DBG_CRYPTO_VALUE )
|
|
|
|
|
gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1);
|
|
|
|
|
gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static void
|
|
|
|
|
cleanup (void)
|
|
|
|
|
{
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if (socket_name && *socket_name)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
remove (socket_name);
|
|
|
|
|
p = strrchr (socket_name, '/');
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*p = 0;
|
|
|
|
|
rmdir (socket_name);
|
|
|
|
|
*p = '/';
|
|
|
|
|
}
|
|
|
|
|
*socket_name = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv )
|
|
|
|
|
{
|
|
|
|
|
ARGPARSE_ARGS pargs;
|
|
|
|
|
int orig_argc;
|
2004-04-20 16:17:10 +02:00
|
|
|
|
gpg_error_t err;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int may_coredump;
|
|
|
|
|
char **orig_argv;
|
|
|
|
|
FILE *configfp = NULL;
|
|
|
|
|
char *configname = NULL;
|
|
|
|
|
const char *shell;
|
|
|
|
|
unsigned configlineno;
|
|
|
|
|
int parse_debug = 0;
|
2004-02-18 18:00:11 +01:00
|
|
|
|
const char *debug_level = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int default_config =1;
|
|
|
|
|
int greeting = 0;
|
|
|
|
|
int nogreeting = 0;
|
|
|
|
|
int pipe_server = 0;
|
2005-05-18 12:48:06 +02:00
|
|
|
|
int multi_server = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int is_daemon = 0;
|
|
|
|
|
int nodetach = 0;
|
|
|
|
|
int csh_style = 0;
|
|
|
|
|
char *logfile = NULL;
|
|
|
|
|
int debug_wait = 0;
|
2004-02-18 18:00:11 +01:00
|
|
|
|
int gpgconf_list = 0;
|
2004-03-16 19:59:21 +01:00
|
|
|
|
const char *config_filename = NULL;
|
2005-06-07 21:09:18 +02:00
|
|
|
|
int allow_coredump = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
set_strusage (my_strusage);
|
|
|
|
|
gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
|
|
|
|
|
/* Please note that we may running SUID(ROOT), so be very CAREFUL
|
|
|
|
|
when adding any stuff between here and the call to INIT_SECMEM()
|
|
|
|
|
somewhere after the option parsing */
|
|
|
|
|
log_set_prefix ("scdaemon", 1|4);
|
2004-02-18 18:00:11 +01:00
|
|
|
|
/* Try to auto set the character set. */
|
|
|
|
|
set_native_charset (NULL);
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
i18n_init ();
|
|
|
|
|
|
2004-04-20 16:17:10 +02:00
|
|
|
|
/* Libgcrypt requires us to register the threading model first.
|
|
|
|
|
Note that this will also do the pth_init. */
|
|
|
|
|
err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_fatal ("can't register GNU Pth with Libgcrypt: %s\n",
|
|
|
|
|
gpg_strerror (err));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check that the libraries are suitable. Do it here because
|
2003-08-05 19:11:04 +02:00
|
|
|
|
the option parsing may need services of the library */
|
|
|
|
|
if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
|
|
|
|
|
{
|
|
|
|
|
log_fatal( _("libgcrypt is too old (need %s, have %s)\n"),
|
|
|
|
|
NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ksba_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free);
|
2003-12-16 17:30:55 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
assuan_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free);
|
2003-12-16 17:30:55 +01:00
|
|
|
|
assuan_set_assuan_log_stream (log_get_stream ());
|
|
|
|
|
assuan_set_assuan_log_prefix (log_get_prefix (NULL));
|
2006-09-06 18:35:52 +02:00
|
|
|
|
assuan_set_assuan_err_source (GPG_ERR_SOURCE_DEFAULT);
|
|
|
|
|
|
2003-12-16 17:30:55 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
gcry_set_log_handler (my_gcry_logger, NULL);
|
|
|
|
|
gcry_control (GCRYCTL_USE_SECURE_RNDPOOL);
|
|
|
|
|
|
|
|
|
|
may_coredump = disable_core_dumps ();
|
|
|
|
|
|
2003-08-19 11:36:48 +02:00
|
|
|
|
/* Set default options. */
|
2004-03-16 19:59:21 +01:00
|
|
|
|
opt.pcsc_driver = DEFAULT_PCSC_DRIVER;
|
2003-08-19 11:36:48 +02:00
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
shell = getenv ("SHELL");
|
|
|
|
|
if (shell && strlen (shell) >= 3 && !strcmp (shell+strlen (shell)-3, "csh") )
|
|
|
|
|
csh_style = 1;
|
|
|
|
|
|
2004-12-21 11:03:00 +01:00
|
|
|
|
opt.homedir = default_homedir ();
|
|
|
|
|
|
|
|
|
|
/* Check whether we have a config file on the commandline */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
orig_argc = argc;
|
|
|
|
|
orig_argv = argv;
|
|
|
|
|
pargs.argc = &argc;
|
|
|
|
|
pargs.argv = &argv;
|
|
|
|
|
pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */
|
|
|
|
|
while (arg_parse( &pargs, opts))
|
|
|
|
|
{
|
|
|
|
|
if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll)
|
|
|
|
|
parse_debug++;
|
|
|
|
|
else if (pargs.r_opt == oOptions)
|
|
|
|
|
{ /* yes there is one, so we do not try the default one, but
|
|
|
|
|
read the option file when it is encountered at the
|
|
|
|
|
commandline */
|
|
|
|
|
default_config = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (pargs.r_opt == oNoOptions)
|
|
|
|
|
default_config = 0; /* --no-options */
|
|
|
|
|
else if (pargs.r_opt == oHomedir)
|
|
|
|
|
opt.homedir = pargs.r.ret_str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* initialize the secure memory. */
|
|
|
|
|
gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
|
|
|
|
|
maybe_setuid = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Now we are working under our real uid
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (default_config)
|
|
|
|
|
configname = make_filename (opt.homedir, "scdaemon.conf", NULL );
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
argc = orig_argc;
|
|
|
|
|
argv = orig_argv;
|
|
|
|
|
pargs.argc = &argc;
|
|
|
|
|
pargs.argv = &argv;
|
|
|
|
|
pargs.flags= 1; /* do not remove the args */
|
|
|
|
|
next_pass:
|
|
|
|
|
if (configname)
|
|
|
|
|
{
|
|
|
|
|
configlineno = 0;
|
|
|
|
|
configfp = fopen (configname, "r");
|
|
|
|
|
if (!configfp)
|
|
|
|
|
{
|
|
|
|
|
if (default_config)
|
|
|
|
|
{
|
|
|
|
|
if( parse_debug )
|
|
|
|
|
log_info (_("NOTE: no default option file `%s'\n"),
|
|
|
|
|
configname );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log_error (_("option file `%s': %s\n"),
|
|
|
|
|
configname, strerror(errno) );
|
|
|
|
|
exit(2);
|
|
|
|
|
}
|
|
|
|
|
xfree (configname);
|
|
|
|
|
configname = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (parse_debug && configname )
|
|
|
|
|
log_info (_("reading options from `%s'\n"), configname );
|
|
|
|
|
default_config = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (optfile_parse( configfp, configname, &configlineno, &pargs, opts) )
|
|
|
|
|
{
|
|
|
|
|
switch (pargs.r_opt)
|
|
|
|
|
{
|
2004-02-18 18:00:11 +01:00
|
|
|
|
case aGPGConfList: gpgconf_list = 1; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
case oQuiet: opt.quiet = 1; break;
|
|
|
|
|
case oVerbose: opt.verbose++; break;
|
|
|
|
|
case oBatch: opt.batch=1; break;
|
|
|
|
|
|
|
|
|
|
case oDebug: opt.debug |= pargs.r.ret_ulong; break;
|
|
|
|
|
case oDebugAll: opt.debug = ~0; break;
|
2004-02-18 18:00:11 +01:00
|
|
|
|
case oDebugLevel: debug_level = pargs.r.ret_str; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
case oDebugWait: debug_wait = pargs.r.ret_int; break;
|
2005-06-07 21:09:18 +02:00
|
|
|
|
case oDebugAllowCoreDump:
|
|
|
|
|
enable_core_dumps ();
|
|
|
|
|
allow_coredump = 1;
|
|
|
|
|
break;
|
2005-05-20 22:39:36 +02:00
|
|
|
|
case oDebugCCIDDriver:
|
2005-07-20 17:05:05 +02:00
|
|
|
|
#ifdef HAVE_LIBUSB
|
2005-05-20 22:39:36 +02:00
|
|
|
|
ccid_set_debug_level (ccid_set_debug_level (-1)+1);
|
2005-07-20 17:05:05 +02:00
|
|
|
|
#endif /*HAVE_LIBUSB*/
|
2005-05-20 22:39:36 +02:00
|
|
|
|
break;
|
|
|
|
|
case oDebugDisableTicker: ticker_disabled = 1; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
case oOptions:
|
|
|
|
|
/* config files may not be nested (silently ignore them) */
|
|
|
|
|
if (!configfp)
|
|
|
|
|
{
|
|
|
|
|
xfree(configname);
|
|
|
|
|
configname = xstrdup(pargs.r.ret_str);
|
|
|
|
|
goto next_pass;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case oNoGreeting: nogreeting = 1; break;
|
|
|
|
|
case oNoVerbose: opt.verbose = 0; break;
|
|
|
|
|
case oNoOptions: break; /* no-options */
|
|
|
|
|
case oHomedir: opt.homedir = pargs.r.ret_str; break;
|
|
|
|
|
case oNoDetach: nodetach = 1; break;
|
|
|
|
|
case oLogFile: logfile = pargs.r.ret_str; break;
|
|
|
|
|
case oCsh: csh_style = 1; break;
|
|
|
|
|
case oSh: csh_style = 0; break;
|
|
|
|
|
case oServer: pipe_server = 1; break;
|
2005-05-20 22:39:36 +02:00
|
|
|
|
case oMultiServer: pipe_server = 1; multi_server = 1; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
case oDaemon: is_daemon = 1; break;
|
|
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
|
case oReaderPort: opt.reader_port = pargs.r.ret_str; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
case octapiDriver: opt.ctapi_driver = pargs.r.ret_str; break;
|
2003-08-19 11:36:48 +02:00
|
|
|
|
case opcscDriver: opt.pcsc_driver = pargs.r.ret_str; break;
|
2003-09-02 21:06:34 +02:00
|
|
|
|
case oDisableCCID: opt.disable_ccid = 1; break;
|
2005-04-27 14:09:21 +02:00
|
|
|
|
case oDisableOpenSC: break;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
|
case oDisableKeypad: opt.disable_keypad = 1; break;
|
|
|
|
|
|
2003-12-01 11:54:09 +01:00
|
|
|
|
case oAllowAdmin: opt.allow_admin = 1; break;
|
|
|
|
|
case oDenyAdmin: opt.allow_admin = 0; break;
|
|
|
|
|
|
2004-08-05 11:24:36 +02:00
|
|
|
|
case oDisableApplication:
|
|
|
|
|
add_to_strlist (&opt.disabled_applications, pargs.r.ret_str);
|
|
|
|
|
break;
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
default : pargs.err = configfp? 1:2; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (configfp)
|
|
|
|
|
{
|
|
|
|
|
fclose( configfp );
|
|
|
|
|
configfp = NULL;
|
2004-03-16 19:59:21 +01:00
|
|
|
|
/* Keep a copy of the config name for use by --gpgconf-list. */
|
|
|
|
|
config_filename = configname;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
configname = NULL;
|
|
|
|
|
goto next_pass;
|
|
|
|
|
}
|
|
|
|
|
xfree (configname);
|
|
|
|
|
configname = NULL;
|
|
|
|
|
if (log_get_errorcount(0))
|
|
|
|
|
exit(2);
|
|
|
|
|
if (nogreeting )
|
|
|
|
|
greeting = 0;
|
|
|
|
|
|
|
|
|
|
if (greeting)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s %s; %s\n",
|
|
|
|
|
strusage(11), strusage(13), strusage(14) );
|
|
|
|
|
fprintf (stderr, "%s\n", strusage(15) );
|
|
|
|
|
}
|
|
|
|
|
#ifdef IS_DEVELOPMENT_VERSION
|
|
|
|
|
log_info ("NOTE: this is a development version!\n");
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-02-18 18:00:11 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (atexit (cleanup))
|
|
|
|
|
{
|
|
|
|
|
log_error ("atexit failed\n");
|
|
|
|
|
cleanup ();
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-18 18:00:11 +01:00
|
|
|
|
set_debug (debug_level);
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (debug_wait && pipe_server)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("waiting for debugger - my pid is %u .....\n",
|
|
|
|
|
(unsigned int)getpid());
|
|
|
|
|
sleep (debug_wait);
|
|
|
|
|
log_debug ("... okay\n");
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-18 18:00:11 +01:00
|
|
|
|
if (gpgconf_list)
|
2004-04-26 15:07:08 +02:00
|
|
|
|
{
|
|
|
|
|
/* List options and default values in the GPG Conf format. */
|
2004-03-16 19:59:21 +01:00
|
|
|
|
|
|
|
|
|
/* The following list is taken from gnupg/tools/gpgconf-comp.c. */
|
|
|
|
|
/* Option flags. YOU MUST NOT CHANGE THE NUMBERS OF THE EXISTING
|
|
|
|
|
FLAGS, AS THEY ARE PART OF THE EXTERNAL INTERFACE. */
|
|
|
|
|
#define GC_OPT_FLAG_NONE 0UL
|
|
|
|
|
/* The RUNTIME flag for an option indicates that the option can be
|
|
|
|
|
changed at runtime. */
|
|
|
|
|
#define GC_OPT_FLAG_RUNTIME (1UL << 3)
|
|
|
|
|
/* The DEFAULT flag for an option indicates that the option has a
|
|
|
|
|
default value. */
|
|
|
|
|
#define GC_OPT_FLAG_DEFAULT (1UL << 4)
|
|
|
|
|
/* The DEF_DESC flag for an option indicates that the option has a
|
|
|
|
|
default, which is described by the value of the default field. */
|
|
|
|
|
#define GC_OPT_FLAG_DEF_DESC (1UL << 5)
|
|
|
|
|
/* The NO_ARG_DESC flag for an option indicates that the argument has
|
|
|
|
|
a default, which is described by the value of the ARGDEF field. */
|
|
|
|
|
#define GC_OPT_FLAG_NO_ARG_DESC (1UL << 6)
|
2004-04-26 15:46:36 +02:00
|
|
|
|
if (!config_filename)
|
|
|
|
|
config_filename = make_filename (opt.homedir, "scdaemon.conf", NULL );
|
2004-03-16 19:59:21 +01:00
|
|
|
|
|
2004-03-17 14:01:16 +01:00
|
|
|
|
printf ("gpgconf-scdaemon.conf:%lu:\"%s\n",
|
2004-04-26 15:46:36 +02:00
|
|
|
|
GC_OPT_FLAG_DEFAULT, config_filename);
|
2004-03-16 19:59:21 +01:00
|
|
|
|
|
|
|
|
|
printf ("verbose:%lu:\n"
|
|
|
|
|
"quiet:%lu:\n"
|
2004-03-17 14:01:16 +01:00
|
|
|
|
"debug-level:%lu:\"none:\n"
|
2004-03-16 19:59:21 +01:00
|
|
|
|
"log-file:%lu:\n",
|
|
|
|
|
GC_OPT_FLAG_NONE,
|
|
|
|
|
GC_OPT_FLAG_NONE,
|
|
|
|
|
GC_OPT_FLAG_DEFAULT,
|
|
|
|
|
GC_OPT_FLAG_NONE );
|
|
|
|
|
|
|
|
|
|
printf ("reader-port:%lu:\n", GC_OPT_FLAG_NONE );
|
|
|
|
|
printf ("ctapi-driver:%lu:\n", GC_OPT_FLAG_NONE );
|
2004-03-23 13:33:20 +01:00
|
|
|
|
printf ("pcsc-driver:%lu:\"%s:\n",
|
2004-03-16 19:59:21 +01:00
|
|
|
|
GC_OPT_FLAG_DEFAULT, DEFAULT_PCSC_DRIVER );
|
|
|
|
|
#ifdef HAVE_LIBUSB
|
|
|
|
|
printf ("disable-ccid:%lu:\n", GC_OPT_FLAG_NONE );
|
|
|
|
|
#endif
|
|
|
|
|
printf ("allow-admin:%lu:\n", GC_OPT_FLAG_NONE );
|
|
|
|
|
|
2004-02-18 18:00:11 +01:00
|
|
|
|
|
|
|
|
|
scd_exit (0);
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* now start with logging to a file if this is desired */
|
|
|
|
|
if (logfile)
|
|
|
|
|
{
|
|
|
|
|
log_set_file (logfile);
|
|
|
|
|
log_set_prefix (NULL, 1|2|4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pipe_server)
|
2005-05-18 12:48:06 +02:00
|
|
|
|
{
|
|
|
|
|
/* This is the simple pipe based server */
|
2004-04-20 16:17:10 +02:00
|
|
|
|
pth_attr_t tattr;
|
2005-05-18 12:48:06 +02:00
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
|
sigemptyset (&sa.sa_mask);
|
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
|
sigaction (SIGPIPE, &sa, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-07 21:09:18 +02:00
|
|
|
|
/* If --debug-allow-core-dump has been given we also need to
|
|
|
|
|
switch the working directory to a place where we can actually
|
|
|
|
|
write. */
|
|
|
|
|
if (allow_coredump)
|
|
|
|
|
{
|
|
|
|
|
if (chdir("/tmp"))
|
|
|
|
|
log_debug ("chdir to `/tmp' failed: %s\n", strerror (errno));
|
|
|
|
|
else
|
|
|
|
|
log_debug ("changed working directory to `/tmp'\n");
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
/* In multi server mode we need to listen on an additional
|
|
|
|
|
socket. Create that socket now before starting the handler
|
|
|
|
|
for the pipe connection. This allows that handler to send
|
|
|
|
|
back the name of that socket. */
|
|
|
|
|
if (multi_server)
|
|
|
|
|
{
|
|
|
|
|
socket_name = create_socket_name (0,
|
|
|
|
|
"S.scdaemon",
|
|
|
|
|
"/tmp/gpg-XXXXXX/S.scdaemon");
|
|
|
|
|
|
|
|
|
|
fd = create_server_socket (0, socket_name);
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-20 16:17:10 +02:00
|
|
|
|
tattr = pth_attr_new();
|
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_JOINABLE, 0);
|
2004-07-22 11:37:36 +02:00
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_STACK_SIZE, 512*1024);
|
2005-05-18 12:48:06 +02:00
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_NAME, "pipe-connection");
|
2004-04-20 16:17:10 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if (!pth_spawn (tattr, start_connection_thread, (void*)(-1)))
|
2004-04-20 16:17:10 +02:00
|
|
|
|
{
|
2005-05-18 12:48:06 +02:00
|
|
|
|
log_error ("error spawning pipe connection handler: %s\n",
|
|
|
|
|
strerror (errno) );
|
2004-04-20 16:17:10 +02:00
|
|
|
|
scd_exit (2);
|
|
|
|
|
}
|
2005-05-18 12:48:06 +02:00
|
|
|
|
|
|
|
|
|
handle_connections (fd);
|
|
|
|
|
if (fd != -1)
|
|
|
|
|
close (fd);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
else if (!is_daemon)
|
|
|
|
|
{
|
|
|
|
|
log_info (_("please use the option `--daemon'"
|
|
|
|
|
" to run the program in the background\n"));
|
|
|
|
|
}
|
|
|
|
|
else
|
2005-05-18 12:48:06 +02:00
|
|
|
|
{ /* Regular server mode */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int fd;
|
|
|
|
|
pid_t pid;
|
|
|
|
|
int i;
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
/* Create the socket. */
|
|
|
|
|
socket_name = create_socket_name (0,
|
|
|
|
|
"S.scdaemon",
|
|
|
|
|
"/tmp/gpg-XXXXXX/S.scdaemon");
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
fd = create_server_socket (0, socket_name);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fflush (NULL);
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2003-08-05 19:11:04 +02:00
|
|
|
|
pid = fork ();
|
|
|
|
|
if (pid == (pid_t)-1)
|
|
|
|
|
{
|
|
|
|
|
log_fatal ("fork failed: %s\n", strerror (errno) );
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
else if (pid)
|
|
|
|
|
{ /* we are the parent */
|
|
|
|
|
char *infostr;
|
|
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
|
|
/* create the info string: <name>:<pid>:<protocol_version> */
|
|
|
|
|
if (asprintf (&infostr, "SCDAEMON_INFO=%s:%lu:1",
|
|
|
|
|
socket_name, (ulong)pid ) < 0)
|
|
|
|
|
{
|
|
|
|
|
log_error ("out of core\n");
|
|
|
|
|
kill (pid, SIGTERM);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
*socket_name = 0; /* don't let cleanup() remove the socket -
|
|
|
|
|
the child should do this from now on */
|
|
|
|
|
if (argc)
|
|
|
|
|
{ /* run the program given on the commandline */
|
|
|
|
|
if (putenv (infostr))
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to set environment: %s\n",
|
|
|
|
|
strerror (errno) );
|
|
|
|
|
kill (pid, SIGTERM );
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
execvp (argv[0], argv);
|
|
|
|
|
log_error ("failed to run the command: %s\n", strerror (errno));
|
|
|
|
|
kill (pid, SIGTERM);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-05-18 12:48:06 +02:00
|
|
|
|
/* Print the environment string, so that the caller can use
|
2003-08-05 19:11:04 +02:00
|
|
|
|
shell's eval to set it */
|
|
|
|
|
if (csh_style)
|
|
|
|
|
{
|
|
|
|
|
*strchr (infostr, '=') = ' ';
|
|
|
|
|
printf ( "setenv %s\n", infostr);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf ( "%s; export SCDAEMON_INFO;\n", infostr);
|
|
|
|
|
}
|
|
|
|
|
free (infostr);
|
|
|
|
|
exit (0);
|
|
|
|
|
}
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
} /* end parent */
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
/* This is the child. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
/* Detach from tty and put process into a new session. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (!nodetach )
|
2005-05-18 12:48:06 +02:00
|
|
|
|
{
|
|
|
|
|
/* Close stdin, stdout and stderr unless it is the log stream. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
for (i=0; i <= 2; i++)
|
|
|
|
|
{
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if ( log_test_fd (i) && i != fd)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
close (i);
|
|
|
|
|
}
|
|
|
|
|
if (setsid() == -1)
|
|
|
|
|
{
|
|
|
|
|
log_error ("setsid() failed: %s\n", strerror(errno) );
|
|
|
|
|
cleanup ();
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2005-05-18 12:48:06 +02:00
|
|
|
|
struct sigaction sa;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
|
sigemptyset (&sa.sa_mask);
|
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
|
sigaction (SIGPIPE, &sa, NULL);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chdir("/"))
|
|
|
|
|
{
|
|
|
|
|
log_error ("chdir to / failed: %s\n", strerror (errno));
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#endif /*!HAVE_W32_SYSTEM*/
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
handle_connections (fd);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
scd_exit (int rc)
|
|
|
|
|
{
|
2004-02-18 18:00:11 +01:00
|
|
|
|
#if 0
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#warning no update_random_seed_file
|
|
|
|
|
update_random_seed_file();
|
2004-02-18 18:00:11 +01:00
|
|
|
|
#endif
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#if 0
|
|
|
|
|
/* at this time a bit annoying */
|
|
|
|
|
if (opt.debug & DBG_MEMSTAT_VALUE)
|
|
|
|
|
{
|
|
|
|
|
gcry_control( GCRYCTL_DUMP_MEMORY_STATS );
|
|
|
|
|
gcry_control( GCRYCTL_DUMP_RANDOM_STATS );
|
|
|
|
|
}
|
|
|
|
|
if (opt.debug)
|
|
|
|
|
gcry_control (GCRYCTL_DUMP_SECMEM_STATS );
|
|
|
|
|
#endif
|
|
|
|
|
gcry_control (GCRYCTL_TERM_SECMEM );
|
|
|
|
|
rc = rc? rc : log_get_errorcount(0)? 2 : 0;
|
|
|
|
|
exit (rc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-05-18 12:48:06 +02:00
|
|
|
|
scd_init_default_ctrl (ctrl_t ctrl)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
2004-04-20 16:17:10 +02:00
|
|
|
|
ctrl->reader_slot = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
/* Return the name of the socket to be used to connect to this
|
|
|
|
|
process. If no socket is available, return NULL. */
|
|
|
|
|
const char *
|
|
|
|
|
scd_get_socket_name ()
|
|
|
|
|
{
|
|
|
|
|
if (socket_name && *socket_name)
|
|
|
|
|
return socket_name;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-20 16:17:10 +02:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_signal (int signo)
|
|
|
|
|
{
|
|
|
|
|
switch (signo)
|
|
|
|
|
{
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2004-04-20 16:17:10 +02:00
|
|
|
|
case SIGHUP:
|
|
|
|
|
log_info ("SIGHUP received - "
|
|
|
|
|
"re-reading configuration and resetting cards\n");
|
|
|
|
|
/* reread_configuration (); */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SIGUSR1:
|
2005-05-21 20:49:00 +02:00
|
|
|
|
log_info ("SIGUSR1 received - printing internal information:\n");
|
|
|
|
|
pth_ctrl (PTH_CTRL_DUMPSTATE, log_get_stream ());
|
2005-06-03 15:57:24 +02:00
|
|
|
|
app_dump_state ();
|
2004-04-20 16:17:10 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SIGUSR2:
|
2004-04-21 16:44:09 +02:00
|
|
|
|
log_info ("SIGUSR2 received - no action defined\n");
|
2004-04-20 16:17:10 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SIGTERM:
|
|
|
|
|
if (!shutdown_pending)
|
|
|
|
|
log_info ("SIGTERM received - shutting down ...\n");
|
|
|
|
|
else
|
|
|
|
|
log_info ("SIGTERM received - still %ld running threads\n",
|
|
|
|
|
pth_ctrl( PTH_CTRL_GETTHREADS ));
|
|
|
|
|
shutdown_pending++;
|
|
|
|
|
if (shutdown_pending > 2)
|
|
|
|
|
{
|
|
|
|
|
log_info ("shutdown forced\n");
|
|
|
|
|
log_info ("%s %s stopped\n", strusage(11), strusage(13) );
|
|
|
|
|
cleanup ();
|
|
|
|
|
scd_exit (0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
|
log_info ("SIGINT received - immediate shutdown\n");
|
|
|
|
|
log_info( "%s %s stopped\n", strusage(11), strusage(13));
|
|
|
|
|
cleanup ();
|
|
|
|
|
scd_exit (0);
|
|
|
|
|
break;
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#endif /*!HAVE_W32_SYSTEM*/
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2004-04-20 16:17:10 +02:00
|
|
|
|
default:
|
|
|
|
|
log_info ("signal %d received - no action defined\n", signo);
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
|
2004-04-20 16:17:10 +02:00
|
|
|
|
static void
|
|
|
|
|
handle_tick (void)
|
|
|
|
|
{
|
2005-05-20 22:39:36 +02:00
|
|
|
|
if (!ticker_disabled)
|
|
|
|
|
scd_update_reader_status_file ();
|
2004-04-20 16:17:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
|
|
|
|
|
/* Create a name for the socket. With USE_STANDARD_SOCKET given as
|
|
|
|
|
true using STANDARD_NAME in the home directory or if given has
|
|
|
|
|
false from the mkdir type name TEMPLATE. In the latter case a
|
|
|
|
|
unique name in a unique new directory will be created. In both
|
|
|
|
|
cases check for valid characters as well as against a maximum
|
|
|
|
|
allowed length for a unix domain socket is done. The function
|
|
|
|
|
terminates the process in case of an error. Retunrs: Pointer to an
|
|
|
|
|
allcoated string with the absolute name of the socket used. */
|
|
|
|
|
static char *
|
|
|
|
|
create_socket_name (int use_standard_socket,
|
|
|
|
|
char *standard_name, char *template)
|
|
|
|
|
{
|
|
|
|
|
char *name, *p;
|
|
|
|
|
|
|
|
|
|
if (use_standard_socket)
|
|
|
|
|
name = make_filename (opt.homedir, standard_name, NULL);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
name = xstrdup (template);
|
|
|
|
|
p = strrchr (name, '/');
|
|
|
|
|
if (!p)
|
|
|
|
|
BUG ();
|
|
|
|
|
*p = 0;
|
|
|
|
|
if (!mkdtemp (name))
|
|
|
|
|
{
|
|
|
|
|
log_error (_("can't create directory `%s': %s\n"),
|
|
|
|
|
name, strerror (errno));
|
|
|
|
|
scd_exit (2);
|
|
|
|
|
}
|
|
|
|
|
*p = '/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strchr (name, PATHSEP_C))
|
|
|
|
|
{
|
|
|
|
|
log_error (("`%s' are not allowed in the socket name\n"), PATHSEP_S);
|
|
|
|
|
scd_exit (2);
|
|
|
|
|
}
|
|
|
|
|
if (strlen (name) + 1 >= DIMof (struct sockaddr_un, sun_path) )
|
|
|
|
|
{
|
|
|
|
|
log_error (_("name of socket too long\n"));
|
|
|
|
|
scd_exit (2);
|
|
|
|
|
}
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a Unix domain socket with NAME. IS_STANDARD_NAME indicates
|
|
|
|
|
whether a non-random socket is used. Returns the file descriptor
|
|
|
|
|
or terminates the process in case of an error. */
|
|
|
|
|
static int
|
|
|
|
|
create_server_socket (int is_standard_name, const char *name)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_un *serv_addr;
|
|
|
|
|
socklen_t len;
|
|
|
|
|
int fd;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
fd = _w32_sock_new (AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
#else
|
|
|
|
|
fd = socket (AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
#endif
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("can't create socket: %s\n"), strerror (errno));
|
|
|
|
|
scd_exit (2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serv_addr = xmalloc (sizeof (*serv_addr));
|
|
|
|
|
memset (serv_addr, 0, sizeof *serv_addr);
|
|
|
|
|
serv_addr->sun_family = AF_UNIX;
|
|
|
|
|
assert (strlen (name) + 1 < sizeof (serv_addr->sun_path));
|
|
|
|
|
strcpy (serv_addr->sun_path, name);
|
|
|
|
|
len = (offsetof (struct sockaddr_un, sun_path)
|
|
|
|
|
+ strlen (serv_addr->sun_path) + 1);
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
rc = _w32_sock_bind (fd, (struct sockaddr*) serv_addr, len);
|
|
|
|
|
if (is_standard_name && rc == -1 )
|
|
|
|
|
{
|
|
|
|
|
remove (name);
|
|
|
|
|
rc = bind (fd, (struct sockaddr*) serv_addr, len);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
rc = bind (fd, (struct sockaddr*) serv_addr, len);
|
|
|
|
|
if (is_standard_name && rc == -1 && errno == EADDRINUSE)
|
|
|
|
|
{
|
|
|
|
|
remove (name);
|
|
|
|
|
rc = bind (fd, (struct sockaddr*) serv_addr, len);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (rc == -1)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("error binding socket to `%s': %s\n"),
|
|
|
|
|
serv_addr->sun_path, strerror (errno));
|
|
|
|
|
close (fd);
|
|
|
|
|
scd_exit (2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (listen (fd, 5 ) == -1)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("listen() failed: %s\n"), strerror (errno));
|
|
|
|
|
close (fd);
|
|
|
|
|
scd_exit (2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info (_("listening on socket `%s'\n"), serv_addr->sun_path);
|
|
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the standard connection thread's main function. */
|
2004-04-20 16:17:10 +02:00
|
|
|
|
static void *
|
2005-05-18 12:48:06 +02:00
|
|
|
|
start_connection_thread (void *arg)
|
2004-04-20 16:17:10 +02:00
|
|
|
|
{
|
2005-05-18 12:48:06 +02:00
|
|
|
|
int fd = (int)arg;
|
|
|
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info (_("handler for fd %d started\n"), fd);
|
|
|
|
|
|
|
|
|
|
scd_command_handler (fd);
|
|
|
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info (_("handler for fd %d terminated\n"), fd);
|
|
|
|
|
|
|
|
|
|
/* If this thread is the pipe connection thread, flag that a
|
|
|
|
|
shutdown is required. With the next ticker event and given that
|
|
|
|
|
no other connections are running the shutdown will then
|
|
|
|
|
happen. */
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
shutdown_pending = 1;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Connection handler loop. Wait for connection requests and spawn a
|
|
|
|
|
thread after accepting a connection. LISTEN_FD is allowed to be -1
|
|
|
|
|
in which case this code will only do regular timeouts and handle
|
|
|
|
|
signals. */
|
|
|
|
|
static void
|
|
|
|
|
handle_connections (int listen_fd)
|
|
|
|
|
{
|
|
|
|
|
pth_attr_t tattr;
|
|
|
|
|
pth_event_t ev, time_ev;
|
2004-04-20 16:17:10 +02:00
|
|
|
|
sigset_t sigs;
|
|
|
|
|
int signo;
|
2005-05-18 12:48:06 +02:00
|
|
|
|
struct sockaddr_un paddr;
|
|
|
|
|
socklen_t plen;
|
|
|
|
|
fd_set fdset, read_fdset;
|
|
|
|
|
int ret;
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
tattr = pth_attr_new();
|
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_JOINABLE, 0);
|
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_STACK_SIZE, 512*1024);
|
2004-04-20 16:17:10 +02:00
|
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#ifndef HAVE_W32_SYSTEM /* fixme */
|
2004-04-20 16:17:10 +02:00
|
|
|
|
sigemptyset (&sigs );
|
|
|
|
|
sigaddset (&sigs, SIGHUP);
|
|
|
|
|
sigaddset (&sigs, SIGUSR1);
|
|
|
|
|
sigaddset (&sigs, SIGUSR2);
|
|
|
|
|
sigaddset (&sigs, SIGINT);
|
|
|
|
|
sigaddset (&sigs, SIGTERM);
|
2005-06-03 15:57:24 +02:00
|
|
|
|
pth_sigmask (SIG_UNBLOCK, &sigs, NULL);
|
2005-05-18 12:48:06 +02:00
|
|
|
|
ev = pth_event (PTH_EVENT_SIGS, &sigs, &signo);
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#else
|
2005-05-18 12:48:06 +02:00
|
|
|
|
ev = NULL;
|
2004-12-15 15:15:54 +01:00
|
|
|
|
#endif
|
2005-05-18 12:48:06 +02:00
|
|
|
|
time_ev = NULL;
|
|
|
|
|
|
|
|
|
|
FD_ZERO (&fdset);
|
|
|
|
|
if (listen_fd != -1)
|
|
|
|
|
FD_SET (listen_fd, &fdset);
|
|
|
|
|
|
|
|
|
|
for (;;)
|
2004-04-20 16:17:10 +02:00
|
|
|
|
{
|
2005-06-03 15:57:24 +02:00
|
|
|
|
sigset_t oldsigs;
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if (shutdown_pending)
|
2004-04-20 16:17:10 +02:00
|
|
|
|
{
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if (pth_ctrl (PTH_CTRL_GETTHREADS) == 1)
|
|
|
|
|
break; /* ready */
|
|
|
|
|
|
|
|
|
|
/* Do not accept anymore connections but wait for existing
|
|
|
|
|
connections to terminate. We do this by clearing out all
|
|
|
|
|
file descriptors to wait for, so that the select will be
|
|
|
|
|
used to just wait on a signal or timeout event. */
|
|
|
|
|
FD_ZERO (&fdset);
|
|
|
|
|
}
|
2004-04-20 16:17:10 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
/* Create a timeout event if needed. */
|
|
|
|
|
if (!time_ev)
|
|
|
|
|
time_ev = pth_event (PTH_EVENT_TIME, pth_timeout (2, 0));
|
|
|
|
|
|
|
|
|
|
/* POSIX says that fd_set should be implemented as a structure,
|
|
|
|
|
thus a simple assignment is fine to copy the entire set. */
|
|
|
|
|
read_fdset = fdset;
|
|
|
|
|
|
|
|
|
|
if (time_ev)
|
|
|
|
|
pth_event_concat (ev, time_ev, NULL);
|
|
|
|
|
ret = pth_select_ev (FD_SETSIZE, &read_fdset, NULL, NULL, NULL, ev);
|
|
|
|
|
if (time_ev)
|
|
|
|
|
pth_event_isolate (time_ev);
|
|
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
|
{
|
|
|
|
|
if (pth_event_occurred (ev)
|
|
|
|
|
|| (time_ev && pth_event_occurred (time_ev)))
|
|
|
|
|
{
|
|
|
|
|
if (pth_event_occurred (ev))
|
|
|
|
|
handle_signal (signo);
|
|
|
|
|
if (time_ev && pth_event_occurred (time_ev))
|
|
|
|
|
{
|
|
|
|
|
pth_event_free (time_ev, PTH_FREE_ALL);
|
|
|
|
|
time_ev = NULL;
|
|
|
|
|
handle_tick ();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
log_error (_("pth_select failed: %s - waiting 1s\n"),
|
|
|
|
|
strerror (errno));
|
|
|
|
|
pth_sleep (1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-04-20 16:17:10 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if (pth_event_occurred (ev))
|
|
|
|
|
{
|
|
|
|
|
handle_signal (signo);
|
|
|
|
|
}
|
2004-04-20 16:17:10 +02:00
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if (time_ev && pth_event_occurred (time_ev))
|
2004-04-20 16:17:10 +02:00
|
|
|
|
{
|
|
|
|
|
pth_event_free (time_ev, PTH_FREE_ALL);
|
|
|
|
|
time_ev = NULL;
|
|
|
|
|
handle_tick ();
|
|
|
|
|
}
|
2005-05-18 12:48:06 +02:00
|
|
|
|
|
2005-06-03 15:57:24 +02:00
|
|
|
|
/* We now might create new threads and because we don't want any
|
|
|
|
|
signals - we are handling here - to be delivered to a new
|
|
|
|
|
thread. Thus we need to block those signals. */
|
|
|
|
|
pth_sigmask (SIG_BLOCK, &sigs, &oldsigs);
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
if (listen_fd != -1 && FD_ISSET (listen_fd, &read_fdset))
|
|
|
|
|
{
|
|
|
|
|
plen = sizeof paddr;
|
|
|
|
|
fd = pth_accept (listen_fd, (struct sockaddr *)&paddr, &plen);
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
{
|
|
|
|
|
log_error ("accept failed: %s\n", strerror (errno));
|
|
|
|
|
}
|
2005-06-03 15:57:24 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char threadname[50];
|
|
|
|
|
snprintf (threadname, sizeof threadname-1, "conn fd=%d", fd);
|
|
|
|
|
threadname[sizeof threadname -1] = 0;
|
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_NAME, threadname);
|
|
|
|
|
|
|
|
|
|
if (!pth_spawn (tattr, start_connection_thread, (void*)fd))
|
|
|
|
|
{
|
|
|
|
|
log_error ("error spawning connection handler: %s\n",
|
|
|
|
|
strerror (errno) );
|
|
|
|
|
close (fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-05-18 12:48:06 +02:00
|
|
|
|
fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-03 15:57:24 +02:00
|
|
|
|
/* Restore the signal mask. */
|
|
|
|
|
pth_sigmask (SIG_SETMASK, &oldsigs, NULL);
|
|
|
|
|
|
2004-04-20 16:17:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-18 12:48:06 +02:00
|
|
|
|
pth_event_free (ev, PTH_FREE_ALL);
|
|
|
|
|
if (time_ev)
|
|
|
|
|
pth_event_free (time_ev, PTH_FREE_ALL);
|
|
|
|
|
cleanup ();
|
|
|
|
|
log_info (_("%s %s stopped\n"), strusage(11), strusage(13));
|
2004-04-20 16:17:10 +02:00
|
|
|
|
}
|
2005-05-18 12:48:06 +02:00
|
|
|
|
|
|
|
|
|
|