2009-10-22 16:44:07 +00:00
|
|
|
|
/* engine-g13.c - G13 engine.
|
|
|
|
|
Copyright (C) 2000 Werner Koch (dd9jn)
|
|
|
|
|
Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
This file is part of GPGME.
|
|
|
|
|
|
|
|
|
|
GPGME is free software; you can redistribute it and/or modify it
|
|
|
|
|
under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of
|
|
|
|
|
the License, or (at your option) any later version.
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
GPGME 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
|
|
|
|
|
Lesser General Public License for more details.
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
|
02111-1307, USA. */
|
|
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2010-11-03 09:56:27 +00:00
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
|
# include <sys/types.h>
|
|
|
|
|
#endif
|
2009-10-22 16:44:07 +00:00
|
|
|
|
#include <assert.h>
|
2010-11-02 16:27:46 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
2010-05-07 01:32:54 +00:00
|
|
|
|
#ifdef HAVE_LOCALE_H
|
2009-10-22 16:44:07 +00:00
|
|
|
|
#include <locale.h>
|
2010-05-07 01:32:54 +00:00
|
|
|
|
#endif
|
2009-10-22 16:44:07 +00:00
|
|
|
|
#include <fcntl.h> /* FIXME */
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include "gpgme.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "ops.h"
|
|
|
|
|
#include "wait.h"
|
|
|
|
|
#include "priv-io.h"
|
|
|
|
|
#include "sema.h"
|
|
|
|
|
|
|
|
|
|
#include "assuan.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
#include "engine-backend.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
int fd; /* FD we talk about. */
|
|
|
|
|
int server_fd;/* Server FD for this connection. */
|
|
|
|
|
int dir; /* Inbound/Outbound, maybe given implicit? */
|
|
|
|
|
void *data; /* Handler-specific data. */
|
|
|
|
|
void *tag; /* ID from the user for gpgme_remove_io_callback. */
|
|
|
|
|
char server_fd_str[15]; /* Same as SERVER_FD but as a string. We
|
|
|
|
|
need this because _gpgme_io_fd2str can't
|
|
|
|
|
be used on a closed descriptor. */
|
|
|
|
|
} iocb_data_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct engine_g13
|
|
|
|
|
{
|
|
|
|
|
assuan_context_t assuan_ctx;
|
|
|
|
|
|
|
|
|
|
int lc_ctype_set;
|
|
|
|
|
int lc_messages_set;
|
|
|
|
|
|
|
|
|
|
iocb_data_t status_cb;
|
|
|
|
|
|
|
|
|
|
struct gpgme_io_cbs io_cbs;
|
|
|
|
|
|
|
|
|
|
/* User provided callbacks. */
|
|
|
|
|
struct {
|
|
|
|
|
gpgme_assuan_data_cb_t data_cb;
|
|
|
|
|
void *data_cb_value;
|
|
|
|
|
|
|
|
|
|
gpgme_assuan_inquire_cb_t inq_cb;
|
|
|
|
|
void *inq_cb_value;
|
|
|
|
|
|
|
|
|
|
gpgme_assuan_status_cb_t status_cb;
|
|
|
|
|
void *status_cb_value;
|
|
|
|
|
} user;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct engine_g13 *engine_g13_t;
|
|
|
|
|
|
|
|
|
|
|
2012-09-25 13:29:49 +00:00
|
|
|
|
static void g13_io_event (void *engine,
|
2009-10-22 16:44:07 +00:00
|
|
|
|
gpgme_event_io_t type, void *type_data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
g13_get_version (const char *file_name)
|
|
|
|
|
{
|
|
|
|
|
return _gpgme_get_program_version (file_name ? file_name
|
2013-08-02 13:25:23 +00:00
|
|
|
|
: _gpgme_get_default_g13_name ());
|
2009-10-22 16:44:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
|
g13_get_req_version (void)
|
|
|
|
|
{
|
2013-12-27 15:08:20 +00:00
|
|
|
|
return "2.1.0";
|
2009-10-22 16:44:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
close_notify_handler (int fd, void *opaque)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = opaque;
|
|
|
|
|
|
|
|
|
|
assert (fd != -1);
|
|
|
|
|
if (g13->status_cb.fd == fd)
|
|
|
|
|
{
|
|
|
|
|
if (g13->status_cb.tag)
|
|
|
|
|
(*g13->io_cbs.remove) (g13->status_cb.tag);
|
|
|
|
|
g13->status_cb.fd = -1;
|
|
|
|
|
g13->status_cb.tag = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the default inquiry callback. We use it to handle the
|
|
|
|
|
Pinentry notifications. */
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
default_inq_cb (engine_g13_t g13, const char *keyword, const char *args)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
|
|
|
|
|
if (!strcmp (keyword, "PINENTRY_LAUNCHED"))
|
|
|
|
|
{
|
|
|
|
|
_gpgme_allow_set_foreground_window ((pid_t)strtoul (args, NULL, 10));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (g13->user.inq_cb)
|
|
|
|
|
{
|
|
|
|
|
gpgme_data_t data = NULL;
|
|
|
|
|
|
|
|
|
|
err = g13->user.inq_cb (g13->user.inq_cb_value,
|
|
|
|
|
keyword, args, &data);
|
|
|
|
|
if (!err && data)
|
|
|
|
|
{
|
|
|
|
|
/* FIXME: Returning data is not yet implemented. However we
|
|
|
|
|
need to allow the caller to cleanup his data object.
|
|
|
|
|
Thus we run the callback in finish mode immediately. */
|
|
|
|
|
err = g13->user.inq_cb (g13->user.inq_cb_value,
|
|
|
|
|
NULL, NULL, &data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
g13_cancel (void *engine)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
|
|
|
|
|
if (!g13)
|
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
|
|
if (g13->status_cb.fd != -1)
|
|
|
|
|
_gpgme_io_close (g13->status_cb.fd);
|
|
|
|
|
|
|
|
|
|
if (g13->assuan_ctx)
|
|
|
|
|
{
|
|
|
|
|
assuan_release (g13->assuan_ctx);
|
|
|
|
|
g13->assuan_ctx = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-10-26 18:52:32 +00:00
|
|
|
|
static gpgme_error_t
|
|
|
|
|
g13_cancel_op (void *engine)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
|
|
|
|
|
if (!g13)
|
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
|
|
if (g13->status_cb.fd != -1)
|
|
|
|
|
_gpgme_io_close (g13->status_cb.fd);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
static void
|
|
|
|
|
g13_release (void *engine)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
|
|
|
|
|
if (!g13)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g13_cancel (engine);
|
|
|
|
|
|
|
|
|
|
free (g13);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
g13_new (void **engine, const char *file_name, const char *home_dir)
|
|
|
|
|
{
|
|
|
|
|
gpgme_error_t err = 0;
|
|
|
|
|
engine_g13_t g13;
|
2014-02-21 10:22:45 +00:00
|
|
|
|
const char *pgmname;
|
2009-10-22 16:44:07 +00:00
|
|
|
|
int argc;
|
2010-04-19 16:59:23 +00:00
|
|
|
|
const char *argv[5];
|
2009-10-22 16:44:07 +00:00
|
|
|
|
char *dft_display = NULL;
|
|
|
|
|
char dft_ttyname[64];
|
|
|
|
|
char *dft_ttytype = NULL;
|
|
|
|
|
char *optstr;
|
|
|
|
|
|
|
|
|
|
g13 = calloc (1, sizeof *g13);
|
|
|
|
|
if (!g13)
|
2013-02-06 16:35:40 +00:00
|
|
|
|
return gpg_error_from_syserror ();
|
2009-10-22 16:44:07 +00:00
|
|
|
|
|
|
|
|
|
g13->status_cb.fd = -1;
|
|
|
|
|
g13->status_cb.dir = 1;
|
|
|
|
|
g13->status_cb.tag = 0;
|
|
|
|
|
g13->status_cb.data = g13;
|
|
|
|
|
|
2014-02-21 10:22:45 +00:00
|
|
|
|
pgmname = file_name ? file_name : _gpgme_get_default_g13_name ();
|
2009-10-22 16:44:07 +00:00
|
|
|
|
argc = 0;
|
2014-02-21 10:22:45 +00:00
|
|
|
|
argv[argc++] = _gpgme_get_basename (pgmname);
|
2009-10-22 16:44:07 +00:00
|
|
|
|
if (home_dir)
|
|
|
|
|
{
|
|
|
|
|
argv[argc++] = "--homedir";
|
|
|
|
|
argv[argc++] = home_dir;
|
|
|
|
|
}
|
|
|
|
|
argv[argc++] = "--server";
|
|
|
|
|
argv[argc++] = NULL;
|
|
|
|
|
|
|
|
|
|
err = assuan_new_ext (&g13->assuan_ctx, GPG_ERR_SOURCE_GPGME,
|
|
|
|
|
&_gpgme_assuan_malloc_hooks, _gpgme_assuan_log_cb,
|
|
|
|
|
NULL);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
assuan_ctx_set_system_hooks (g13->assuan_ctx, &_gpgme_assuan_system_hooks);
|
|
|
|
|
|
|
|
|
|
#if USE_DESCRIPTOR_PASSING
|
2014-02-21 10:22:45 +00:00
|
|
|
|
err = assuan_pipe_connect (g13->assuan_ctx, pgmname, argv,
|
|
|
|
|
NULL, NULL, NULL, ASSUAN_PIPE_CONNECT_FDPASSING);
|
2009-10-22 16:44:07 +00:00
|
|
|
|
#else
|
2014-02-21 10:22:45 +00:00
|
|
|
|
err = assuan_pipe_connect (g13->assuan_ctx, pgmname, argv,
|
|
|
|
|
NULL, NULL, NULL, 0);
|
2009-10-22 16:44:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
err = _gpgme_getenv ("DISPLAY", &dft_display);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
if (dft_display)
|
|
|
|
|
{
|
|
|
|
|
if (asprintf (&optstr, "OPTION display=%s", dft_display) < 0)
|
|
|
|
|
{
|
|
|
|
|
free (dft_display);
|
2013-02-06 16:35:40 +00:00
|
|
|
|
err = gpg_error_from_syserror ();
|
2009-10-22 16:44:07 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
free (dft_display);
|
|
|
|
|
|
|
|
|
|
err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL,
|
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
free (optstr);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isatty (1))
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname));
|
2015-07-27 07:19:52 +00:00
|
|
|
|
|
|
|
|
|
/* Even though isatty() returns 1, ttyname_r() may fail in many
|
|
|
|
|
ways, e.g., when /dev/pts is not accessible under chroot. */
|
|
|
|
|
if (!rc)
|
2009-10-22 16:44:07 +00:00
|
|
|
|
{
|
|
|
|
|
if (asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0)
|
|
|
|
|
{
|
2013-02-06 16:35:40 +00:00
|
|
|
|
err = gpg_error_from_syserror ();
|
2009-10-22 16:44:07 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL,
|
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
free (optstr);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
err = _gpgme_getenv ("TERM", &dft_ttytype);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
if (dft_ttytype)
|
|
|
|
|
{
|
|
|
|
|
if (asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0)
|
|
|
|
|
{
|
|
|
|
|
free (dft_ttytype);
|
2013-02-06 16:35:40 +00:00
|
|
|
|
err = gpg_error_from_syserror ();
|
2009-10-22 16:44:07 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
free (dft_ttytype);
|
|
|
|
|
|
|
|
|
|
err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL,
|
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
|
free (optstr);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
/* Under Windows we need to use AllowSetForegroundWindow. Tell
|
|
|
|
|
g13 to tell us when it needs it. */
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
err = assuan_transact (g13->assuan_ctx, "OPTION allow-pinentry-notify",
|
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION)
|
|
|
|
|
err = 0; /* This is a new feature of g13. */
|
|
|
|
|
}
|
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
g13_release (g13);
|
|
|
|
|
else
|
|
|
|
|
*engine = g13;
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
g13_set_locale (void *engine, int category, const char *value)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
gpgme_error_t err;
|
|
|
|
|
char *optstr;
|
|
|
|
|
char *catstr;
|
|
|
|
|
|
|
|
|
|
/* FIXME: If value is NULL, we need to reset the option to default.
|
|
|
|
|
But we can't do this. So we error out here. G13 needs support
|
|
|
|
|
for this. */
|
2010-05-07 01:32:54 +00:00
|
|
|
|
if (0)
|
|
|
|
|
;
|
|
|
|
|
#ifdef LC_CTYPE
|
|
|
|
|
else if (category == LC_CTYPE)
|
2009-10-22 16:44:07 +00:00
|
|
|
|
{
|
|
|
|
|
catstr = "lc-ctype";
|
|
|
|
|
if (!value && g13->lc_ctype_set)
|
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
if (value)
|
|
|
|
|
g13->lc_ctype_set = 1;
|
|
|
|
|
}
|
2010-05-07 01:32:54 +00:00
|
|
|
|
#endif
|
2009-10-22 16:44:07 +00:00
|
|
|
|
#ifdef LC_MESSAGES
|
|
|
|
|
else if (category == LC_MESSAGES)
|
|
|
|
|
{
|
|
|
|
|
catstr = "lc-messages";
|
|
|
|
|
if (!value && g13->lc_messages_set)
|
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
if (value)
|
|
|
|
|
g13->lc_messages_set = 1;
|
|
|
|
|
}
|
|
|
|
|
#endif /* LC_MESSAGES */
|
|
|
|
|
else
|
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
|
|
/* FIXME: Reset value to default. */
|
2012-09-25 13:29:49 +00:00
|
|
|
|
if (!value)
|
2009-10-22 16:44:07 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0)
|
2013-02-06 16:35:40 +00:00
|
|
|
|
err = gpg_error_from_syserror ();
|
2009-10-22 16:44:07 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL,
|
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
|
free (optstr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-19 16:59:23 +00:00
|
|
|
|
#if USE_DESCRIPTOR_PASSING
|
2009-10-22 16:44:07 +00:00
|
|
|
|
static gpgme_error_t
|
|
|
|
|
g13_assuan_simple_command (assuan_context_t ctx, char *cmd,
|
2009-11-06 00:12:59 +00:00
|
|
|
|
engine_status_handler_t status_fnc,
|
|
|
|
|
void *status_fnc_value)
|
2009-10-22 16:44:07 +00:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
char *line;
|
|
|
|
|
size_t linelen;
|
|
|
|
|
|
|
|
|
|
err = assuan_write_line (ctx, cmd);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
err = assuan_read_line (ctx, &line, &linelen);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
if (*line == '#' || !linelen)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (linelen >= 2
|
|
|
|
|
&& line[0] == 'O' && line[1] == 'K'
|
|
|
|
|
&& (line[2] == '\0' || line[2] == ' '))
|
|
|
|
|
return 0;
|
|
|
|
|
else if (linelen >= 4
|
|
|
|
|
&& line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
|
|
|
|
|
&& line[3] == ' ')
|
|
|
|
|
err = atoi (&line[4]);
|
|
|
|
|
else if (linelen >= 2
|
|
|
|
|
&& line[0] == 'S' && line[1] == ' ')
|
|
|
|
|
{
|
|
|
|
|
char *rest;
|
|
|
|
|
|
|
|
|
|
rest = strchr (line + 2, ' ');
|
|
|
|
|
if (!rest)
|
|
|
|
|
rest = line + linelen; /* set to an empty string */
|
|
|
|
|
else
|
|
|
|
|
*(rest++) = 0;
|
|
|
|
|
|
2009-10-26 18:52:32 +00:00
|
|
|
|
/* Nothing to do with status lines. */
|
2009-10-22 16:44:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
}
|
|
|
|
|
while (!err);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2010-04-19 16:59:23 +00:00
|
|
|
|
#endif
|
2009-10-22 16:44:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
status_handler (void *opaque, int fd)
|
|
|
|
|
{
|
2009-10-26 18:52:32 +00:00
|
|
|
|
struct io_cb_data *data = (struct io_cb_data *) opaque;
|
|
|
|
|
engine_g13_t g13 = (engine_g13_t) data->handler_value;
|
2009-10-22 16:44:07 +00:00
|
|
|
|
gpgme_error_t err = 0;
|
|
|
|
|
char *line;
|
|
|
|
|
size_t linelen;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
err = assuan_read_line (g13->assuan_ctx, &line, &linelen);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
/* Try our best to terminate the connection friendly. */
|
|
|
|
|
/* assuan_write_line (g13->assuan_ctx, "BYE"); */
|
|
|
|
|
TRACE2 (DEBUG_CTX, "gpgme:status_handler", g13,
|
|
|
|
|
"fd 0x%x: error reading assuan line: %s",
|
|
|
|
|
fd, gpg_strerror (err));
|
|
|
|
|
}
|
|
|
|
|
else if (linelen >= 3
|
|
|
|
|
&& line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
|
|
|
|
|
&& (line[3] == '\0' || line[3] == ' '))
|
|
|
|
|
{
|
|
|
|
|
if (line[3] == ' ')
|
|
|
|
|
err = atoi (&line[4]);
|
|
|
|
|
if (! err)
|
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
TRACE2 (DEBUG_CTX, "gpgme:status_handler", g13,
|
|
|
|
|
"fd 0x%x: ERR line: %s",
|
|
|
|
|
fd, err ? gpg_strerror (err) : "ok");
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-11-06 00:12:59 +00:00
|
|
|
|
/* Command execution errors are not fatal, as we use
|
2009-10-22 16:44:07 +00:00
|
|
|
|
a session based protocol. */
|
2009-11-06 00:12:59 +00:00
|
|
|
|
data->op_err = err;
|
|
|
|
|
|
|
|
|
|
/* The caller will do the rest (namely, call cancel_op,
|
|
|
|
|
which closes status_fd). */
|
|
|
|
|
return 0;
|
2009-10-22 16:44:07 +00:00
|
|
|
|
}
|
|
|
|
|
else if (linelen >= 2
|
|
|
|
|
&& line[0] == 'O' && line[1] == 'K'
|
|
|
|
|
&& (line[2] == '\0' || line[2] == ' '))
|
|
|
|
|
{
|
|
|
|
|
TRACE1 (DEBUG_CTX, "gpgme:status_handler", g13,
|
|
|
|
|
"fd 0x%x: OK line", fd);
|
2009-11-06 00:12:59 +00:00
|
|
|
|
|
|
|
|
|
_gpgme_io_close (g13->status_cb.fd);
|
|
|
|
|
return 0;
|
2009-10-22 16:44:07 +00:00
|
|
|
|
}
|
|
|
|
|
else if (linelen > 2
|
|
|
|
|
&& line[0] == 'D' && line[1] == ' ')
|
|
|
|
|
{
|
|
|
|
|
/* We are using the colon handler even for plain inline data
|
|
|
|
|
- strange name for that function but for historic reasons
|
|
|
|
|
we keep it. */
|
|
|
|
|
/* FIXME We can't use this for binary data because we
|
|
|
|
|
assume this is a string. For the current usage of colon
|
|
|
|
|
output it is correct. */
|
|
|
|
|
char *src = line + 2;
|
|
|
|
|
char *end = line + linelen;
|
|
|
|
|
char *dst = src;
|
|
|
|
|
|
|
|
|
|
linelen = 0;
|
|
|
|
|
while (src < end)
|
|
|
|
|
{
|
|
|
|
|
if (*src == '%' && src + 2 < end)
|
|
|
|
|
{
|
|
|
|
|
/* Handle escaped characters. */
|
|
|
|
|
++src;
|
|
|
|
|
*dst++ = _gpgme_hextobyte (src);
|
|
|
|
|
src += 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
*dst++ = *src++;
|
|
|
|
|
|
|
|
|
|
linelen++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
src = line + 2;
|
|
|
|
|
if (linelen && g13->user.data_cb)
|
|
|
|
|
err = g13->user.data_cb (g13->user.data_cb_value,
|
|
|
|
|
src, linelen);
|
|
|
|
|
else
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
|
|
TRACE2 (DEBUG_CTX, "gpgme:g13_status_handler", g13,
|
|
|
|
|
"fd 0x%x: D inlinedata; status from cb: %s",
|
|
|
|
|
fd, (g13->user.data_cb ?
|
|
|
|
|
(err? gpg_strerror (err):"ok"):"no callback"));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (linelen > 2
|
|
|
|
|
&& line[0] == 'S' && line[1] == ' ')
|
|
|
|
|
{
|
|
|
|
|
char *src;
|
|
|
|
|
char *args;
|
|
|
|
|
|
|
|
|
|
src = line + 2;
|
|
|
|
|
while (*src == ' ')
|
|
|
|
|
src++;
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
args = strchr (line + 2, ' ');
|
|
|
|
|
if (!args)
|
|
|
|
|
args = line + linelen; /* set to an empty string */
|
|
|
|
|
else
|
|
|
|
|
*(args++) = 0;
|
|
|
|
|
|
|
|
|
|
while (*args == ' ')
|
|
|
|
|
args++;
|
|
|
|
|
|
|
|
|
|
if (g13->user.status_cb)
|
|
|
|
|
err = g13->user.status_cb (g13->user.status_cb_value,
|
|
|
|
|
src, args);
|
|
|
|
|
else
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
|
|
TRACE3 (DEBUG_CTX, "gpgme:g13_status_handler", g13,
|
|
|
|
|
"fd 0x%x: S line (%s) - status from cb: %s",
|
|
|
|
|
fd, line+2, (g13->user.status_cb ?
|
|
|
|
|
(err? gpg_strerror (err):"ok"):"no callback"));
|
|
|
|
|
}
|
|
|
|
|
else if (linelen >= 7
|
|
|
|
|
&& line[0] == 'I' && line[1] == 'N' && line[2] == 'Q'
|
|
|
|
|
&& line[3] == 'U' && line[4] == 'I' && line[5] == 'R'
|
2012-09-25 13:29:49 +00:00
|
|
|
|
&& line[6] == 'E'
|
2009-10-22 16:44:07 +00:00
|
|
|
|
&& (line[7] == '\0' || line[7] == ' '))
|
|
|
|
|
{
|
|
|
|
|
char *src;
|
|
|
|
|
char *args;
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
for (src=line+7; *src == ' '; src++)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
args = strchr (src, ' ');
|
|
|
|
|
if (!args)
|
|
|
|
|
args = line + linelen; /* Let it point to an empty string. */
|
|
|
|
|
else
|
|
|
|
|
*(args++) = 0;
|
|
|
|
|
|
|
|
|
|
while (*args == ' ')
|
|
|
|
|
args++;
|
|
|
|
|
|
|
|
|
|
err = default_inq_cb (g13, src, args);
|
2012-09-25 13:29:49 +00:00
|
|
|
|
if (!err)
|
2009-10-22 16:44:07 +00:00
|
|
|
|
{
|
|
|
|
|
/* Flush and send END. */
|
|
|
|
|
err = assuan_send_data (g13->assuan_ctx, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
else if (gpg_err_code (err) == GPG_ERR_ASS_CANCELED)
|
|
|
|
|
{
|
|
|
|
|
/* Flush and send CANcel. */
|
|
|
|
|
err = assuan_send_data (g13->assuan_ctx, NULL, 1);
|
|
|
|
|
}
|
|
|
|
|
assuan_write_line (g13->assuan_ctx, "END");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (!err && assuan_pending_line (g13->assuan_ctx));
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2009-10-22 16:44:07 +00:00
|
|
|
|
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
add_io_cb (engine_g13_t g13, iocb_data_t *iocbd, gpgme_io_cb_t handler)
|
|
|
|
|
{
|
|
|
|
|
gpgme_error_t err;
|
|
|
|
|
|
|
|
|
|
TRACE_BEG2 (DEBUG_ENGINE, "engine-g13:add_io_cb", g13,
|
|
|
|
|
"fd %d, dir %d", iocbd->fd, iocbd->dir);
|
|
|
|
|
err = (*g13->io_cbs.add) (g13->io_cbs.add_priv,
|
|
|
|
|
iocbd->fd, iocbd->dir,
|
|
|
|
|
handler, iocbd->data, &iocbd->tag);
|
|
|
|
|
if (err)
|
|
|
|
|
return TRACE_ERR (err);
|
|
|
|
|
if (!iocbd->dir)
|
|
|
|
|
/* FIXME Kludge around poll() problem. */
|
|
|
|
|
err = _gpgme_io_set_nonblocking (iocbd->fd);
|
|
|
|
|
return TRACE_ERR (err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
start (engine_g13_t g13, const char *command)
|
|
|
|
|
{
|
|
|
|
|
gpgme_error_t err;
|
2010-05-07 01:06:14 +00:00
|
|
|
|
assuan_fd_t afdlist[5];
|
2009-10-22 16:44:07 +00:00
|
|
|
|
int fdlist[5];
|
|
|
|
|
int nfds;
|
2010-05-07 01:06:14 +00:00
|
|
|
|
int i;
|
2009-10-22 16:44:07 +00:00
|
|
|
|
|
|
|
|
|
/* We need to know the fd used by assuan for reads. We do this by
|
|
|
|
|
using the assumption that the first returned fd from
|
|
|
|
|
assuan_get_active_fds() is always this one. */
|
|
|
|
|
nfds = assuan_get_active_fds (g13->assuan_ctx, 0 /* read fds */,
|
2010-05-07 01:06:14 +00:00
|
|
|
|
afdlist, DIM (afdlist));
|
2009-10-22 16:44:07 +00:00
|
|
|
|
if (nfds < 1)
|
|
|
|
|
return gpg_error (GPG_ERR_GENERAL); /* FIXME */
|
2010-05-07 01:06:14 +00:00
|
|
|
|
/* For now... */
|
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
|
fdlist[i] = (int) afdlist[i];
|
2009-10-22 16:44:07 +00:00
|
|
|
|
|
|
|
|
|
/* We "duplicate" the file descriptor, so we can close it here (we
|
|
|
|
|
can't close fdlist[0], as that is closed by libassuan, and
|
|
|
|
|
closing it here might cause libassuan to close some unrelated FD
|
|
|
|
|
later). Alternatively, we could special case status_fd and
|
|
|
|
|
register/unregister it manually as needed, but this increases
|
|
|
|
|
code duplication and is more complicated as we can not use the
|
|
|
|
|
close notifications etc. A third alternative would be to let
|
|
|
|
|
Assuan know that we closed the FD, but that complicates the
|
|
|
|
|
Assuan interface. */
|
|
|
|
|
|
|
|
|
|
g13->status_cb.fd = _gpgme_io_dup (fdlist[0]);
|
|
|
|
|
if (g13->status_cb.fd < 0)
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
|
|
|
|
|
if (_gpgme_io_set_close_notify (g13->status_cb.fd,
|
|
|
|
|
close_notify_handler, g13))
|
|
|
|
|
{
|
|
|
|
|
_gpgme_io_close (g13->status_cb.fd);
|
|
|
|
|
g13->status_cb.fd = -1;
|
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = add_io_cb (g13, &g13->status_cb, status_handler);
|
|
|
|
|
if (!err)
|
|
|
|
|
err = assuan_write_line (g13->assuan_ctx, command);
|
|
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
|
g13_io_event (g13, GPGME_EVENT_START, NULL);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if USE_DESCRIPTOR_PASSING
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
g13_reset (void *engine)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
|
|
|
|
|
/* We must send a reset because we need to reset the list of
|
|
|
|
|
signers. Note that RESET does not reset OPTION commands. */
|
|
|
|
|
return g13_assuan_simple_command (g13->assuan_ctx, "RESET", NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gpgme_error_t
|
|
|
|
|
g13_transact (void *engine,
|
|
|
|
|
const char *command,
|
|
|
|
|
gpgme_assuan_data_cb_t data_cb,
|
|
|
|
|
void *data_cb_value,
|
|
|
|
|
gpgme_assuan_inquire_cb_t inq_cb,
|
|
|
|
|
void *inq_cb_value,
|
|
|
|
|
gpgme_assuan_status_cb_t status_cb,
|
|
|
|
|
void *status_cb_value)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
gpgme_error_t err;
|
|
|
|
|
|
|
|
|
|
if (!g13 || !command || !*command)
|
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
|
|
g13->user.data_cb = data_cb;
|
|
|
|
|
g13->user.data_cb_value = data_cb_value;
|
|
|
|
|
g13->user.inq_cb = inq_cb;
|
|
|
|
|
g13->user.inq_cb_value = inq_cb_value;
|
|
|
|
|
g13->user.status_cb = status_cb;
|
|
|
|
|
g13->user.status_cb_value = status_cb_value;
|
|
|
|
|
|
|
|
|
|
err = start (g13, command);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g13_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
g13->io_cbs = *io_cbs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g13_io_event (void *engine, gpgme_event_io_t type, void *type_data)
|
|
|
|
|
{
|
|
|
|
|
engine_g13_t g13 = engine;
|
|
|
|
|
|
|
|
|
|
TRACE3 (DEBUG_ENGINE, "gpgme:g13_io_event", g13,
|
|
|
|
|
"event %p, type %d, type_data %p",
|
|
|
|
|
g13->io_cbs.event, type, type_data);
|
|
|
|
|
if (g13->io_cbs.event)
|
|
|
|
|
(*g13->io_cbs.event) (g13->io_cbs.event_priv, type, type_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct engine_ops _gpgme_engine_ops_g13 =
|
|
|
|
|
{
|
|
|
|
|
/* Static functions. */
|
2013-08-02 13:25:23 +00:00
|
|
|
|
_gpgme_get_default_g13_name,
|
2009-10-22 16:44:07 +00:00
|
|
|
|
NULL,
|
|
|
|
|
g13_get_version,
|
|
|
|
|
g13_get_req_version,
|
|
|
|
|
g13_new,
|
|
|
|
|
|
|
|
|
|
/* Member functions. */
|
|
|
|
|
g13_release,
|
|
|
|
|
#if USE_DESCRIPTOR_PASSING
|
|
|
|
|
g13_reset,
|
|
|
|
|
#else
|
|
|
|
|
NULL, /* reset */
|
|
|
|
|
#endif
|
2016-05-19 15:01:07 +00:00
|
|
|
|
NULL, /* set_status_cb */
|
2009-10-22 16:44:07 +00:00
|
|
|
|
NULL, /* set_status_handler */
|
|
|
|
|
NULL, /* set_command_handler */
|
|
|
|
|
NULL, /* set_colon_line_handler */
|
|
|
|
|
g13_set_locale,
|
2009-11-10 Marcus Brinkmann <marcus@g10code.de>
* configure.ac: Activate UIServer if FD passing is enabled and
Assuan is available.
m4/
2009-11-10 Marcus Brinkmann <marcus@g10code.de>
* libassuan.m4: Fix LIBASSUAN_VERSION.
src/
2009-11-10 Marcus Brinkmann <marcus@g10code.de>
* Makefile.am (uiserver_components): New variable.
(main_sources): Add it.
* ops.h, key.c (_gpgme_key_append_name): Take CONVERT argument,
implement it. Adjust callers.
(gpgme_key_from_uid): New function.
* gpgme.h.in (gpgme_protocol_t): Add GPGME_PROTOCOL_DEFAULT.
(gpgme_encrypt_flags_t): Add GPGME_ENCRYPT_PREPARE,
GPGME_ENCRYPT_EXPECT_SIGN.
(gpgme_set_sub_protocol, gpgme_key_from_uid): New functions.
* libgpgme.vers, gpgme.def: Add new functions.
* gpgme.c (gpgme_set_protocol): Add UIServer protocol.
(gpgme_set_sub_protocol): New function.
(gpgme_get_protocol_name): Add UIServer and default protocol.
* assuan-support.c: Return correct error values, implement
socketpair for POSIX.
* priv-io.h, posix-io.c, w32-io.c, w32-glib-io.c,
w32-qt-io.cpp (_gpgme_io_spawn): Add ATFORK and ATFORKVALUE
arguments. Implement it for POSIX. Adjust all callers.
* engine.h, engine-backend.h (_gpgme_engine_set_protocol)
(_gpgme_engine_op_decrypt_verify): New prototypes. Adjust all
users.
* engine.c (engine_ops, gpgme_get_engine_info): Add UIServer
engine.
(_gpgme_engine_set_protocol, _gpgme_engine_op_decrypt_verify): New
function.
* decrypt-verify.c (decrypt_verify_start): Call
_gpgme_engine_op_decrypt_verify.
* util.h, posix-util.c,
w32-util.c (_gpgme_get_uiserver_socket_path): New function.
* engine-gpgsm.c (gpgsm_set_fd): Fix _gpgme_io_pipe invocation.
* gpgme-tool.c: Some support for UIServer protocol.
* engine-uiserver.c: New file.
2009-11-10 09:07:19 +00:00
|
|
|
|
NULL, /* set_protocol */
|
2009-10-22 16:44:07 +00:00
|
|
|
|
NULL, /* decrypt */
|
2009-11-10 Marcus Brinkmann <marcus@g10code.de>
* configure.ac: Activate UIServer if FD passing is enabled and
Assuan is available.
m4/
2009-11-10 Marcus Brinkmann <marcus@g10code.de>
* libassuan.m4: Fix LIBASSUAN_VERSION.
src/
2009-11-10 Marcus Brinkmann <marcus@g10code.de>
* Makefile.am (uiserver_components): New variable.
(main_sources): Add it.
* ops.h, key.c (_gpgme_key_append_name): Take CONVERT argument,
implement it. Adjust callers.
(gpgme_key_from_uid): New function.
* gpgme.h.in (gpgme_protocol_t): Add GPGME_PROTOCOL_DEFAULT.
(gpgme_encrypt_flags_t): Add GPGME_ENCRYPT_PREPARE,
GPGME_ENCRYPT_EXPECT_SIGN.
(gpgme_set_sub_protocol, gpgme_key_from_uid): New functions.
* libgpgme.vers, gpgme.def: Add new functions.
* gpgme.c (gpgme_set_protocol): Add UIServer protocol.
(gpgme_set_sub_protocol): New function.
(gpgme_get_protocol_name): Add UIServer and default protocol.
* assuan-support.c: Return correct error values, implement
socketpair for POSIX.
* priv-io.h, posix-io.c, w32-io.c, w32-glib-io.c,
w32-qt-io.cpp (_gpgme_io_spawn): Add ATFORK and ATFORKVALUE
arguments. Implement it for POSIX. Adjust all callers.
* engine.h, engine-backend.h (_gpgme_engine_set_protocol)
(_gpgme_engine_op_decrypt_verify): New prototypes. Adjust all
users.
* engine.c (engine_ops, gpgme_get_engine_info): Add UIServer
engine.
(_gpgme_engine_set_protocol, _gpgme_engine_op_decrypt_verify): New
function.
* decrypt-verify.c (decrypt_verify_start): Call
_gpgme_engine_op_decrypt_verify.
* util.h, posix-util.c,
w32-util.c (_gpgme_get_uiserver_socket_path): New function.
* engine-gpgsm.c (gpgsm_set_fd): Fix _gpgme_io_pipe invocation.
* gpgme-tool.c: Some support for UIServer protocol.
* engine-uiserver.c: New file.
2009-11-10 09:07:19 +00:00
|
|
|
|
NULL, /* decrypt_verify */
|
2009-10-22 16:44:07 +00:00
|
|
|
|
NULL, /* delete */
|
|
|
|
|
NULL, /* edit */
|
|
|
|
|
NULL, /* encrypt */
|
|
|
|
|
NULL, /* encrypt_sign */
|
|
|
|
|
NULL, /* export */
|
|
|
|
|
NULL, /* export_ext */
|
|
|
|
|
NULL, /* genkey */
|
|
|
|
|
NULL, /* import */
|
|
|
|
|
NULL, /* keylist */
|
|
|
|
|
NULL, /* keylist_ext */
|
|
|
|
|
NULL, /* sign */
|
|
|
|
|
NULL, /* trustlist */
|
|
|
|
|
NULL, /* verify */
|
|
|
|
|
NULL, /* getauditlog */
|
|
|
|
|
g13_transact,
|
|
|
|
|
NULL, /* conf_load */
|
|
|
|
|
NULL, /* conf_save */
|
|
|
|
|
g13_set_io_cbs,
|
|
|
|
|
g13_io_event,
|
2009-10-26 18:52:32 +00:00
|
|
|
|
g13_cancel,
|
|
|
|
|
g13_cancel_op,
|
2013-02-07 19:59:16 +00:00
|
|
|
|
NULL, /* passwd */
|
2014-04-10 11:01:00 +00:00
|
|
|
|
NULL, /* set_pinentry_mode */
|
|
|
|
|
NULL /* opspawn */
|
2009-10-22 16:44:07 +00:00
|
|
|
|
};
|