diff --git a/src/assuan-handler.c b/src/assuan-handler.c
index 126eccb..8eae253 100644
--- a/src/assuan-handler.c
+++ b/src/assuan-handler.c
@@ -1,1047 +1,1091 @@
/* assuan-handler.c - dispatch commands
* Copyright (C) 2001, 2002, 2003, 2007, 2009,
* 2011 Free Software Foundation, Inc.
*
* This file is part of Assuan.
*
* Assuan 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.
*
* Assuan 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see .
* SPDX-License-Identifier: LGPL-2.1+
*/
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
#include
#include
+#if HAVE_W32_SYSTEM || HAVE_W64_SYSTEM
+#include
+#endif
#include "assuan-defs.h"
#include "debug.h"
#define spacep(p) (*(p) == ' ' || *(p) == '\t')
#define digitp(a) ((a) >= '0' && (a) <= '9')
static int my_strcasecmp (const char *a, const char *b);
#define PROCESS_DONE(ctx, rc) \
((ctx)->flags.in_process_next ? assuan_process_done ((ctx), (rc)) : (rc))
static gpg_error_t
dummy_handler (assuan_context_t ctx, char *line)
{
return
PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_ASSUAN_SERVER_FAULT,
"no handler registered"));
}
static const char std_help_nop[] =
"NOP\n"
"\n"
"No operation. Returns OK without any action.";
static gpg_error_t
std_handler_nop (assuan_context_t ctx, char *line)
{
return PROCESS_DONE (ctx, 0); /* okay */
}
static const char std_help_cancel[] =
"CANCEL\n"
"\n"
"Run the server's cancel handler if one has been registered.";
static gpg_error_t
std_handler_cancel (assuan_context_t ctx, char *line)
{
if (ctx->cancel_notify_fnc)
/* Return value ignored. */
ctx->cancel_notify_fnc (ctx, line);
return PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_NOT_IMPLEMENTED, NULL));
}
static const char std_help_option[] =
"OPTION [ [=] ]\n"
"\n"
"Set option to configure server operation. Leading and\n"
"trailing spaces around and are allowed but should be\n"
"ignored. For compatibility reasons, may be prefixed with two\n"
"dashes. The use of the equal sign is optional but suggested if\n"
" is given.";
static gpg_error_t
std_handler_option (assuan_context_t ctx, char *line)
{
char *key, *value, *p;
for (key=line; spacep (key); key++)
;
if (!*key)
return
PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_ASS_SYNTAX, "argument required"));
if (*key == '=')
return
PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_ASS_SYNTAX,
"no option name given"));
for (value=key; *value && !spacep (value) && *value != '='; value++)
;
if (*value)
{
if (spacep (value))
*value++ = 0; /* terminate key */
for (; spacep (value); value++)
;
if (*value == '=')
{
*value++ = 0; /* terminate key */
for (; spacep (value); value++)
;
if (!*value)
return
PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_ASS_SYNTAX,
"option argument expected"));
}
if (*value)
{
for (p = value + strlen(value) - 1; p > value && spacep (p); p--)
;
if (p > value)
*++p = 0; /* strip trailing spaces */
}
}
if (*key == '-' && key[1] == '-' && key[2])
key += 2; /* the double dashes are optional */
if (*key == '-')
return PROCESS_DONE (ctx,
set_error (ctx, GPG_ERR_ASS_SYNTAX,
"option should not begin with one dash"));
if (ctx->option_handler_fnc)
return PROCESS_DONE (ctx, ctx->option_handler_fnc (ctx, key, value));
return PROCESS_DONE (ctx, 0);
}
static const char std_help_bye[] =
"BYE\n"
"\n"
"Close the connection. The server will reply with OK.";
static gpg_error_t
std_handler_bye (assuan_context_t ctx, char *line)
{
if (ctx->bye_notify_fnc)
/* Return value ignored. */
ctx->bye_notify_fnc (ctx, line);
assuan_close_input_fd (ctx);
assuan_close_output_fd (ctx);
/* pretty simple :-) */
ctx->flags.process_complete = 1;
return PROCESS_DONE (ctx, 0);
}
static const char std_help_auth[] =
"AUTH\n"
"\n"
"Reserved for future extensions.";
static gpg_error_t
std_handler_auth (assuan_context_t ctx, char *line)
{
return PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_NOT_IMPLEMENTED, NULL));
}
static const char std_help_reset[] =
"RESET\n"
"\n"
"Reset the connection but not any existing authentication. The server\n"
"should release all resources associated with the connection.";
static gpg_error_t
std_handler_reset (assuan_context_t ctx, char *line)
{
gpg_error_t err = 0;
if (ctx->reset_notify_fnc)
err = ctx->reset_notify_fnc (ctx, line);
if (! err)
{
assuan_close_input_fd (ctx);
assuan_close_output_fd (ctx);
_assuan_uds_close_fds (ctx);
}
return PROCESS_DONE (ctx, err);
}
static const char std_help_help[] =
"HELP []\n"
"\n"
"Lists all commands that the server understands as comment lines on\n"
"the status channel. If is given, list detailed help for\n"
"that command.";
static gpg_error_t
std_handler_help (assuan_context_t ctx, char *line)
{
unsigned int i;
char buf[ASSUAN_LINELENGTH];
const char *helpstr;
size_t n;
n = strcspn (line, " \t\n");
if (!n)
{
/* Print all commands. If a help string is available and that
starts with the command name, print the first line of the
help string. */
for (i = 0; i < ctx->cmdtbl_used; i++)
{
n = strlen (ctx->cmdtbl[i].name);
helpstr = ctx->cmdtbl[i].helpstr;
if (helpstr
&& !strncmp (ctx->cmdtbl[i].name, helpstr, n)
&& (!helpstr[n] || helpstr[n] == '\n' || helpstr[n] == ' ')
&& (n = strcspn (helpstr, "\n")) )
snprintf (buf, sizeof (buf), "# %.*s", (int)n, helpstr);
else
snprintf (buf, sizeof (buf), "# %s", ctx->cmdtbl[i].name);
buf[ASSUAN_LINELENGTH - 1] = '\0';
assuan_write_line (ctx, buf);
}
}
else
{
/* Print the help for the given command. */
int c = line[n];
line[n] = 0;
for (i=0; ctx->cmdtbl[i].name; i++)
if (!my_strcasecmp (line, ctx->cmdtbl[i].name))
break;
line[n] = c;
if (!ctx->cmdtbl[i].name)
return PROCESS_DONE (ctx, set_error (ctx,GPG_ERR_UNKNOWN_COMMAND,NULL));
helpstr = ctx->cmdtbl[i].helpstr;
if (!helpstr)
return PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_NOT_FOUND, NULL));
do
{
n = strcspn (helpstr, "\n");
snprintf (buf, sizeof (buf), "# %.*s", (int)n, helpstr);
helpstr += n;
if (*helpstr == '\n')
helpstr++;
buf[ASSUAN_LINELENGTH - 1] = '\0';
assuan_write_line (ctx, buf);
}
while (*helpstr);
}
return PROCESS_DONE (ctx, 0);
}
static const char std_help_end[] =
"END\n"
"\n"
"Used by a client to mark the end of raw data.";
static gpg_error_t
std_handler_end (assuan_context_t ctx, char *line)
{
return PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_NOT_IMPLEMENTED, NULL));
}
gpg_error_t
assuan_command_parse_fd (assuan_context_t ctx, char *line, assuan_fd_t *rfd)
{
char *endp;
if ((strncmp (line, "FD", 2) && strncmp (line, "fd", 2))
|| (line[2] != '=' && line[2] != '\0' && !spacep(&line[2])))
return set_error (ctx, GPG_ERR_ASS_SYNTAX, "FD[=] expected");
line += 2;
if (*line == '=')
{
line ++;
if (!digitp (*line))
return set_error (ctx, GPG_ERR_ASS_SYNTAX, "number required");
#if HAVE_W64_SYSTEM
*rfd = (void*)strtoull (line, &endp, 10);
#elif HAVE_W32_SYSTEM
*rfd = (void*)strtoul (line, &endp, 10);
#else
*rfd = strtoul (line, &endp, 10);
#endif
/* Remove that argument so that a notify handler won't see it. */
memset (line, ' ', endp? (endp-line):strlen(line));
if (*rfd == ctx->inbound.fd)
return set_error (ctx, GPG_ERR_ASS_PARAMETER, "fd same as inbound fd");
if (*rfd == ctx->outbound.fd)
return set_error (ctx, GPG_ERR_ASS_PARAMETER, "fd same as outbound fd");
return 0;
}
else
/* Our peer has sent the file descriptor. */
return assuan_receivefd (ctx, rfd);
}
static const char std_help_input[] =
"INPUT FD[=]\n"
"\n"
"Used by a client to pass an input file descriptor to the server.\n"
"The server opens as a local file descriptor. Without , the\n"
"server opens the file descriptor just sent by the client using\n"
"assuan_sendfd.";
static gpg_error_t
std_handler_input (assuan_context_t ctx, char *line)
{
gpg_error_t rc;
assuan_fd_t fd, oldfd;
rc = assuan_command_parse_fd (ctx, line, &fd);
if (rc)
return PROCESS_DONE (ctx, rc);
if (ctx->input_notify_fnc)
{
oldfd = ctx->input_fd;
ctx->input_fd = fd;
rc = ctx->input_notify_fnc (ctx, line);
if (rc)
ctx->input_fd = oldfd;
}
else if (!rc)
ctx->input_fd = fd;
return PROCESS_DONE (ctx, rc);
}
static const char std_help_output[] =
"OUTPUT FD[=]\n"
"\n"
"Used by a client to pass an output file descriptor to the server.\n"
"The server opens as a local file descriptor. Without , the\n"
"server opens the file descriptor just sent by the client using\n"
"assuan_sendfd.";
static gpg_error_t
std_handler_output (assuan_context_t ctx, char *line)
{
gpg_error_t rc;
assuan_fd_t fd, oldfd;
rc = assuan_command_parse_fd (ctx, line, &fd);
if (rc)
return PROCESS_DONE (ctx, rc);
if (ctx->output_notify_fnc)
{
oldfd = ctx->output_fd;
ctx->output_fd = fd;
rc = ctx->output_notify_fnc (ctx, line);
if (rc)
ctx->output_fd = oldfd;
}
else if (!rc)
ctx->output_fd = fd;
return PROCESS_DONE (ctx, rc);
}
+#if HAVE_W32_SYSTEM || HAVE_W64_SYSTEM
+static const char w32_help_sendfd[] =
+ "SENDFD \n"
+ "\n"
+ "Used by a client to pass a file HANDLE to the server.\n"
+ "The server opens as a local file HANDLE.";
+static gpg_error_t
+w32_handler_sendfd (assuan_context_t ctx, char *line)
+{
+ gpg_error_t err = 0;
+ char *endp;
+ intptr_t file_handle;
+ int fd;
+
+#if HAVE_W64_SYSTEM
+ file_handle = strtoull (line, &endp, 16);
+#elif HAVE_W32_SYSTEM
+ file_handle = strtoul (line, &endp, 16);
+#endif
+
+ if (*endp)
+ {
+ err = set_error (ctx, GPG_ERR_ASS_SYNTAX, "hex number required");
+ return PROCESS_DONE (ctx, err);
+ }
+
+ fd = _open_osfhandle ((intptr_t)file_handle, _O_RDWR);
+ if (fd < 0)
+ {
+ CloseHandle ((HANDLE)file_handle);
+ err = GPG_ERR_ASSUAN;
+ }
+
+ ctx->uds.pendingfds[ctx->uds.pendingfdscount++] = (assuan_fd_t)fd;
+ return PROCESS_DONE (ctx, err);
+}
+#endif
+
/* This is a table with the standard commands and handler for them.
The table is used to initialize a new context and associate strings
with default handlers */
static struct {
const char *name;
gpg_error_t (*handler)(assuan_context_t, char *line);
const char *help;
int always; /* always initialize this command */
} std_cmd_table[] = {
{ "NOP", std_handler_nop, std_help_nop, 1 },
{ "CANCEL", std_handler_cancel, std_help_cancel, 1 },
{ "OPTION", std_handler_option, std_help_option, 1 },
{ "BYE", std_handler_bye, std_help_bye, 1 },
{ "AUTH", std_handler_auth, std_help_auth, 1 },
{ "RESET", std_handler_reset, std_help_reset, 1 },
{ "END", std_handler_end, std_help_end, 1 },
{ "HELP", std_handler_help, std_help_help, 1 },
{ "INPUT", std_handler_input, std_help_input, 0 },
{ "OUTPUT", std_handler_output, std_help_output, 0 },
+#if HAVE_W32_SYSTEM
+ { "SENDFD", w32_handler_sendfd, w32_help_sendfd, 1 },
+#endif
{ } };
/**
* assuan_register_command:
* @ctx: the server context
* @cmd_name: A string with the command name
* @handler: The handler function to be called or NULL to use a default
* handler.
* HELPSTRING
*
* Register a handler to be used for a given command. Note that
* several default handlers are already registered with a new context.
* This function however allows to override them.
*
* Return value: 0 on success or an error code
**/
gpg_error_t
assuan_register_command (assuan_context_t ctx, const char *cmd_name,
assuan_handler_t handler, const char *help_string)
{
int i, cmd_index = -1;
const char *s;
if (cmd_name && !*cmd_name)
cmd_name = NULL;
if (!cmd_name)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
if (!handler)
{ /* find a default handler. */
for (i=0; (s=std_cmd_table[i].name) && strcmp (cmd_name, s); i++)
;
if (!s)
{ /* Try again but case insensitive. */
for (i=0; (s=std_cmd_table[i].name)
&& my_strcasecmp (cmd_name, s); i++)
;
}
if (s)
handler = std_cmd_table[i].handler;
if (!handler)
handler = dummy_handler; /* Last resort is the dummy handler. */
}
if (!ctx->cmdtbl)
{
ctx->cmdtbl_size = 50;
ctx->cmdtbl = _assuan_calloc (ctx, ctx->cmdtbl_size, sizeof *ctx->cmdtbl);
if (!ctx->cmdtbl)
return _assuan_error (ctx, gpg_err_code_from_syserror ());
ctx->cmdtbl_used = 0;
}
else if (ctx->cmdtbl_used >= ctx->cmdtbl_size)
{
struct cmdtbl_s *x;
x = _assuan_realloc (ctx, ctx->cmdtbl, (ctx->cmdtbl_size+10) * sizeof *x);
if (!x)
return _assuan_error (ctx, gpg_err_code_from_syserror ());
ctx->cmdtbl = x;
ctx->cmdtbl_size += 50;
}
for (i=0; icmdtbl_used; i++)
{
if (!my_strcasecmp (cmd_name, ctx->cmdtbl[i].name))
{
cmd_index = i;
break;
}
}
if (cmd_index == -1)
cmd_index = ctx->cmdtbl_used++;
ctx->cmdtbl[cmd_index].name = cmd_name;
ctx->cmdtbl[cmd_index].handler = handler;
ctx->cmdtbl[cmd_index].helpstr = help_string;
return 0;
}
/* Return the name of the command currently processed by a handler.
The string returned is valid until the next call to an assuan
function on the same context. Returns NULL if no handler is
executed or the command is not known. */
const char *
assuan_get_command_name (assuan_context_t ctx)
{
return ctx? ctx->current_cmd_name : NULL;
}
gpg_error_t
assuan_register_pre_cmd_notify (assuan_context_t ctx,
gpg_error_t (*fnc)(assuan_context_t,
const char *cmd))
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->pre_cmd_notify_fnc = fnc;
return 0;
}
gpg_error_t
assuan_register_post_cmd_notify (assuan_context_t ctx,
void (*fnc)(assuan_context_t, gpg_error_t))
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->post_cmd_notify_fnc = fnc;
return 0;
}
gpg_error_t
assuan_register_bye_notify (assuan_context_t ctx, assuan_handler_t fnc)
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->bye_notify_fnc = fnc;
return 0;
}
gpg_error_t
assuan_register_reset_notify (assuan_context_t ctx, assuan_handler_t fnc)
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->reset_notify_fnc = fnc;
return 0;
}
gpg_error_t
assuan_register_cancel_notify (assuan_context_t ctx, assuan_handler_t fnc)
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->cancel_notify_fnc = fnc;
return 0;
}
gpg_error_t
assuan_register_option_handler (assuan_context_t ctx,
gpg_error_t (*fnc)(assuan_context_t,
const char*, const char*))
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->option_handler_fnc = fnc;
return 0;
}
gpg_error_t
assuan_register_input_notify (assuan_context_t ctx, assuan_handler_t fnc)
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->input_notify_fnc = fnc;
return 0;
}
gpg_error_t
assuan_register_output_notify (assuan_context_t ctx, assuan_handler_t fnc)
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
ctx->output_notify_fnc = fnc;
return 0;
}
/* Helper to register the standards commands */
gpg_error_t
_assuan_register_std_commands (assuan_context_t ctx)
{
gpg_error_t rc;
int i;
for (i = 0; std_cmd_table[i].name; i++)
{
if (std_cmd_table[i].always)
{
rc = assuan_register_command (ctx, std_cmd_table[i].name, NULL, NULL);
if (rc)
return rc;
}
}
return 0;
}
/* Process the special data lines. The "D " has already been removed
from the line. As all handlers this function may modify the line. */
static gpg_error_t
handle_data_line (assuan_context_t ctx, char *line, int linelen)
{
return set_error (ctx, GPG_ERR_NOT_IMPLEMENTED, NULL);
}
/* like ascii_strcasecmp but assume that B is already uppercase */
static int
my_strcasecmp (const char *a, const char *b)
{
if (a == b)
return 0;
for (; *a && *b; a++, b++)
{
if (((*a >= 'a' && *a <= 'z')? (*a&~0x20):*a) != *b)
break;
}
return *a == *b? 0 : (((*a >= 'a' && *a <= 'z')? (*a&~0x20):*a) - *b);
}
/* Parse the line, break out the command, find it in the command
table, remove leading and white spaces from the arguments, call the
handler with the argument line and return the error. */
static gpg_error_t
dispatch_command (assuan_context_t ctx, char *line, int linelen)
{
gpg_error_t err;
char *p;
const char *s;
int shift, i;
/* Note that as this function is invoked by assuan_process_next as
well, we need to hide non-critical errors with PROCESS_DONE. */
if (*line == 'D' && line[1] == ' ') /* divert to special handler */
/* FIXME: Depending on the final implementation of
handle_data_line, this may be wrong here. For example, if a
user callback is invoked, and that callback is responsible for
calling assuan_process_done, then this is wrong. */
return PROCESS_DONE (ctx, handle_data_line (ctx, line+2, linelen-2));
for (p=line; *p && *p != ' ' && *p != '\t'; p++)
;
if (p==line)
return PROCESS_DONE
(ctx, set_error (ctx, GPG_ERR_ASS_SYNTAX, "leading white-space"));
if (*p)
{ /* Skip over leading WS after the keyword */
*p++ = 0;
while ( *p == ' ' || *p == '\t')
p++;
}
shift = p - line;
for (i=0; (s=ctx->cmdtbl[i].name); i++)
{
if (!strcmp (line, s))
break;
}
if (!s)
{ /* and try case insensitive */
for (i=0; (s=ctx->cmdtbl[i].name); i++)
{
if (!my_strcasecmp (line, s))
break;
}
}
if (!s)
return PROCESS_DONE (ctx, set_error (ctx, GPG_ERR_ASS_UNKNOWN_CMD, NULL));
line += shift;
/* linelen -= shift; -- not needed. */
if (ctx->pre_cmd_notify_fnc) {
err = ctx->pre_cmd_notify_fnc(ctx, ctx->cmdtbl[i].name);
if (err)
return PROCESS_DONE(ctx, err);
}
/* fprintf (stderr, "DBG-assuan: processing %s `%s'\n", s, line); */
ctx->current_cmd_name = ctx->cmdtbl[i].name;
err = ctx->cmdtbl[i].handler (ctx, line);
ctx->current_cmd_name = NULL;
return err;
}
/* Call this to acknowledge the current command. */
gpg_error_t
assuan_process_done (assuan_context_t ctx, gpg_error_t rc)
{
if (!ctx->flags.in_command)
return _assuan_error (ctx, GPG_ERR_ASS_GENERAL);
if (ctx->flags.force_close)
ctx->flags.process_complete = 1;
ctx->flags.in_command = 0;
/* Check for data write errors. */
if (ctx->outbound.data.fp)
{
/* Flush the data lines. */
fclose (ctx->outbound.data.fp);
ctx->outbound.data.fp = NULL;
if (!rc && ctx->outbound.data.error)
rc = ctx->outbound.data.error;
}
else
{
/* Flush any data send without using the data FP. */
assuan_send_data (ctx, NULL, 0);
if (!rc && ctx->outbound.data.error)
rc = ctx->outbound.data.error;
}
/* Error handling. */
if (!rc)
{
if (ctx->flags.process_complete)
{
/* No error checking because the peer may have already
disconnect. */
assuan_write_line (ctx, "OK closing connection");
ctx->finish_handler (ctx);
}
else
rc = assuan_write_line (ctx, ctx->okay_line ? ctx->okay_line : "OK");
}
else
{
char errline[300];
const char *text = ctx->err_no == rc ? ctx->err_str : NULL;
char ebuf[50];
if (ctx->flags.force_close)
text = "[closing connection]";
gpg_strerror_r (rc, ebuf, sizeof (ebuf));
snprintf (errline, sizeof errline, "ERR %d %.50s <%.30s>%s%.100s",
rc, ebuf, gpg_strsource (rc),
text? " - ":"", text?text:"");
rc = assuan_write_line (ctx, errline);
if (ctx->flags.force_close)
ctx->finish_handler (ctx);
}
if (ctx->post_cmd_notify_fnc)
ctx->post_cmd_notify_fnc (ctx, rc);
ctx->flags.confidential = 0;
if (ctx->okay_line)
{
_assuan_free (ctx, ctx->okay_line);
ctx->okay_line = NULL;
}
return rc;
}
static gpg_error_t
process_next (assuan_context_t ctx)
{
gpg_error_t rc;
/* What the next thing to do is depends on the current state.
However, we will always first read the next line. The client is
required to write full lines without blocking long after starting
a partial line. */
rc = _assuan_read_line (ctx);
if (_assuan_error_is_eagain (ctx, rc))
return 0;
if (gpg_err_code (rc) == GPG_ERR_EOF)
{
ctx->flags.process_complete = 1;
return 0;
}
if (rc)
return rc;
if (*ctx->inbound.line == '#' || !ctx->inbound.linelen)
/* Comment lines are ignored. */
return 0;
/* Now we have a line that really means something. It could be one
of the following things: First, if we are not in a command
already, it is the next command to dispatch. Second, if we are
in a command, it can only be the response to an INQUIRE
reply. */
if (!ctx->flags.in_command)
{
ctx->flags.in_command = 1;
ctx->outbound.data.error = 0;
ctx->outbound.data.linelen = 0;
/* Dispatch command and return reply. */
ctx->flags.in_process_next = 1;
rc = dispatch_command (ctx, ctx->inbound.line, ctx->inbound.linelen);
ctx->flags.in_process_next = 0;
}
else if (ctx->flags.in_inquire)
{
/* FIXME: Pick up the continuation. */
rc = _assuan_inquire_ext_cb (ctx);
}
else
{
/* Should not happen. The client is sending data while we are
in a command and not waiting for an inquire. We log an error
and discard it. */
TRACE0 (ctx, ASSUAN_LOG_DATA, "process_next", ctx,
"unexpected client data");
rc = 0;
}
return rc;
}
/* This function should be invoked when the assuan connected FD is
ready for reading. If the equivalent to EWOULDBLOCK is returned
(this should be done by the command handler), assuan_process_next
should be invoked the next time the connected FD is readable.
Eventually, the caller will finish by invoking assuan_process_done.
DONE is set to 1 if the connection has ended. */
gpg_error_t
assuan_process_next (assuan_context_t ctx, int *done)
{
gpg_error_t rc;
if (done)
*done = 0;
ctx->flags.process_complete = 0;
do
{
rc = process_next (ctx);
}
while (!rc && !ctx->flags.process_complete && assuan_pending_line (ctx));
if (done)
*done = !!ctx->flags.process_complete;
return rc;
}
static gpg_error_t
process_request (assuan_context_t ctx)
{
gpg_error_t rc;
if (ctx->flags.in_inquire)
return _assuan_error (ctx, GPG_ERR_ASS_NESTED_COMMANDS);
do
{
rc = _assuan_read_line (ctx);
}
while (_assuan_error_is_eagain (ctx, rc));
if (gpg_err_code (rc) == GPG_ERR_EOF)
{
ctx->flags.process_complete = 1;
return 0;
}
if (rc)
return rc;
if (*ctx->inbound.line == '#' || !ctx->inbound.linelen)
return 0; /* comment line - ignore */
ctx->flags.in_command = 1;
ctx->outbound.data.error = 0;
ctx->outbound.data.linelen = 0;
/* dispatch command and return reply */
rc = dispatch_command (ctx, ctx->inbound.line, ctx->inbound.linelen);
return assuan_process_done (ctx, rc);
}
/**
* assuan_process:
* @ctx: assuan context
*
* This function is used to handle the assuan protocol after a
* connection has been established using assuan_accept(). This is the
* main protocol handler.
*
* Return value: 0 on success or an error code if the assuan operation
* failed. Note, that no error is returned for operational errors.
**/
gpg_error_t
assuan_process (assuan_context_t ctx)
{
gpg_error_t rc;
ctx->flags.process_complete = 0;
do {
rc = process_request (ctx);
} while (!rc && !ctx->flags.process_complete);
return rc;
}
/**
* assuan_get_active_fds:
* @ctx: Assuan context
* @what: 0 for read fds, 1 for write fds
* @fdarray: Caller supplied array to store the FDs
* @fdarraysize: size of that array
*
* Return all active filedescriptors for the given context. This
* function can be used to select on the fds and call
* assuan_process_next() if there is an active one. The first fd in
* the array is the one used for the command connection.
*
* Note, that write FDs are not yet supported.
*
* Return value: number of FDs active and put into @fdarray or -1 on
* error which is most likely a too small fdarray.
**/
int
assuan_get_active_fds (assuan_context_t ctx, int what,
assuan_fd_t *fdarray, int fdarraysize)
{
int n = 0;
if (!ctx || fdarraysize < 2 || what < 0 || what > 1)
return -1;
if (!what)
{
if (ctx->inbound.fd != ASSUAN_INVALID_FD)
fdarray[n++] = ctx->inbound.fd;
}
else
{
if (ctx->outbound.fd != ASSUAN_INVALID_FD)
fdarray[n++] = ctx->outbound.fd;
if (ctx->outbound.data.fp)
#if defined(HAVE_W32_SYSTEM)
fdarray[n++] = (void*)_get_osfhandle (fileno (ctx->outbound.data.fp));
#else
fdarray[n++] = fileno (ctx->outbound.data.fp);
#endif
}
return n;
}
/* Two simple wrappers to make the expected function types match. */
#ifdef HAVE_FUNOPEN
static int
fun1_cookie_write (void *cookie, const char *buffer, int orig_size)
{
return _assuan_cookie_write_data (cookie, buffer, orig_size);
}
#endif /*HAVE_FUNOPEN*/
#ifdef HAVE_FOPENCOOKIE
static ssize_t
fun2_cookie_write (void *cookie, const char *buffer, size_t orig_size)
{
return _assuan_cookie_write_data (cookie, buffer, orig_size);
}
#endif /*HAVE_FOPENCOOKIE*/
/* Return a FP to be used for data output. The FILE pointer is valid
until the end of a handler. So a close is not needed. Assuan does
all the buffering needed to insert the status line as well as the
required line wappping and quoting for data lines.
We use GNU's custom streams here. There should be an alternative
implementaion for systems w/o a glibc, a simple implementation
could use a child process */
FILE *
assuan_get_data_fp (assuan_context_t ctx)
{
#if defined (HAVE_FOPENCOOKIE) || defined (HAVE_FUNOPEN)
if (ctx->outbound.data.fp)
return ctx->outbound.data.fp;
#ifdef HAVE_FUNOPEN
ctx->outbound.data.fp = funopen (ctx, 0, fun1_cookie_write,
0, _assuan_cookie_write_flush);
#else
ctx->outbound.data.fp = funopen (ctx, 0, fun2_cookie_write,
0, _assuan_cookie_write_flush);
#endif
ctx->outbound.data.error = 0;
return ctx->outbound.data.fp;
#else
gpg_err_set_errno (ENOSYS);
return NULL;
#endif
}
/* Set the text used for the next OK response. This string is
automatically reset to NULL after the next command. */
gpg_error_t
assuan_set_okay_line (assuan_context_t ctx, const char *line)
{
if (!ctx)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
if (!line)
{
_assuan_free (ctx, ctx->okay_line);
ctx->okay_line = NULL;
}
else
{
/* FIXME: we need to use gcry_is_secure() to test whether
we should allocate the entire line in secure memory */
char *buf = _assuan_malloc (ctx, 3 + strlen(line) + 1);
if (!buf)
return _assuan_error (ctx, gpg_err_code_from_syserror ());
strcpy (buf, "OK ");
strcpy (buf+3, line);
_assuan_free (ctx, ctx->okay_line);
ctx->okay_line = buf;
}
return 0;
}
gpg_error_t
assuan_write_status (assuan_context_t ctx,
const char *keyword, const char *text)
{
char buffer[256];
char *helpbuf;
size_t n;
gpg_error_t ae;
if ( !ctx || !keyword)
return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
if (!text)
text = "";
n = 2 + strlen (keyword) + 1 + strlen (text) + 1;
if (n < sizeof (buffer))
{
strcpy (buffer, "S ");
strcat (buffer, keyword);
if (*text)
{
strcat (buffer, " ");
strcat (buffer, text);
}
ae = assuan_write_line (ctx, buffer);
}
else if ( (helpbuf = _assuan_malloc (ctx, n)) )
{
strcpy (helpbuf, "S ");
strcat (helpbuf, keyword);
if (*text)
{
strcat (helpbuf, " ");
strcat (helpbuf, text);
}
ae = assuan_write_line (ctx, helpbuf);
_assuan_free (ctx, helpbuf);
}
else
ae = 0;
return ae;
}
diff --git a/src/system-w32.c b/src/system-w32.c
index 2e2d83f..037a924 100644
--- a/src/system-w32.c
+++ b/src/system-w32.c
@@ -1,773 +1,785 @@
/* system-w32.c - System support functions for Windows.
* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
*
* This file is part of Assuan.
*
* Assuan 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.
*
* Assuan 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see .
* SPDX-License-Identifier: LGPL-2.1+
*/
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
#include
#include
#include "assuan-defs.h"
#include "debug.h"
assuan_fd_t
assuan_fdopen (int fd)
{
assuan_fd_t ifd = (assuan_fd_t) _get_osfhandle (fd);
assuan_fd_t ofd;
if (! DuplicateHandle(GetCurrentProcess(), ifd,
GetCurrentProcess(), &ofd, 0,
TRUE, DUPLICATE_SAME_ACCESS))
{
gpg_err_set_errno (EIO);
return ASSUAN_INVALID_FD;
}
return ofd;
}
/* Sleep for the given number of microseconds. Default
implementation. */
void
__assuan_usleep (assuan_context_t ctx, unsigned int usec)
{
if (!usec)
return;
Sleep (usec / 1000);
}
/* Three simple wrappers, only used because thes function are named in
the def file. */
HANDLE
_assuan_w32ce_prepare_pipe (int *r_rvid, int write_end)
{
(void)r_rvid;
(void)write_end;
return INVALID_HANDLE_VALUE;
}
HANDLE
_assuan_w32ce_finish_pipe (int rvid, int write_end)
{
(void)rvid;
(void)write_end;
return INVALID_HANDLE_VALUE;
}
DWORD
_assuan_w32ce_create_pipe (HANDLE *read_hd, HANDLE *write_hd,
LPSECURITY_ATTRIBUTES sec_attr, DWORD size)
{
return CreatePipe (read_hd, write_hd, sec_attr, size);
}
/* Create a pipe with one inheritable end. Default implementation. */
int
__assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx)
{
HANDLE rh;
HANDLE wh;
HANDLE th;
SECURITY_ATTRIBUTES sec_attr;
memset (&sec_attr, 0, sizeof (sec_attr));
sec_attr.nLength = sizeof (sec_attr);
sec_attr.bInheritHandle = FALSE;
if (!CreatePipe (&rh, &wh, &sec_attr, 0))
{
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_pipe", ctx,
"CreatePipe failed: %s", _assuan_w32_strerror (ctx, -1));
gpg_err_set_errno (EIO);
return -1;
}
if (! DuplicateHandle (GetCurrentProcess(), (inherit_idx == 0) ? rh : wh,
GetCurrentProcess(), &th, 0,
TRUE, DUPLICATE_SAME_ACCESS ))
{
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_pipe", ctx,
"DuplicateHandle failed: %s", _assuan_w32_strerror (ctx, -1));
CloseHandle (rh);
CloseHandle (wh);
gpg_err_set_errno (EIO);
return -1;
}
if (inherit_idx == 0)
{
CloseHandle (rh);
rh = th;
}
else
{
CloseHandle (wh);
wh = th;
}
fd[0] = rh;
fd[1] = wh;
return 0;
}
/* Close the given file descriptor, created with _assuan_pipe or one
of the socket functions. Default implementation. */
int
__assuan_close (assuan_context_t ctx, assuan_fd_t fd)
{
int rc;
if (ctx->flags.is_socket)
{
rc = closesocket (HANDLE2SOCKET(fd));
if (rc)
gpg_err_set_errno ( _assuan_sock_wsa2errno (WSAGetLastError ()) );
}
else
{
rc = CloseHandle (fd);
if (rc)
/* FIXME. */
gpg_err_set_errno (EIO);
}
return rc;
}
/* To encode/decode file HANDLE, we use FDPASS_FORMAT */
#define FDPASS_FORMAT "%p"
#define FDPASS_MSG_SIZE (sizeof (uintptr_t)*2 + 1)
static gpg_error_t
get_file_handle (int fd, int server_pid, HANDLE *r_handle)
{
HANDLE prochandle, handle, newhandle;
handle = (void *)_get_osfhandle (fd);
prochandle = OpenProcess (PROCESS_DUP_HANDLE, FALSE, server_pid);
if (!prochandle)
return gpg_error (GPG_ERR_ASS_PARAMETER);/*FIXME: error*/
if (!DuplicateHandle (GetCurrentProcess (), handle, prochandle, &newhandle,
0, TRUE, DUPLICATE_SAME_ACCESS))
{
CloseHandle (prochandle);
return gpg_error (GPG_ERR_ASS_PARAMETER);/*FIXME: error*/
}
CloseHandle (prochandle);
*r_handle = newhandle;
return 0;
}
/*
* On Windows, we can consider two different ways about what FD means:
*
* (1) POSIX fd
* (2) Windows file HANDLE
*
* In assuan, we use assuan_fd_t for socket/pipe.
*
* Here, it refers file object.
*
* If we started the design and implementation now, it would be more
* natural (easy to understand, no confusion) to use the "int" type
* and POSIX fd here. Considering the purpose of sending fd (the file
* object to share), it is better portability-wise, even though use of
* POSIX fd on Windows requires emulation layer. (These days, in
* GnuPG, we use gpgrt's estream for file assces and gpgrt_fileno for
* POSIX fd.)
*
* That is:
*
* - assuan_sendfd/assuan_recvfd sends/receives POSIX fd
* - assuan_get_input_fd/assuan_get_output_fd returns POSIX fd
*
* However, those APIs now uses assuan_fd_t. That's troublesome or it
* allows confusion about the semantics of the APIs.
*
* Perhaps, avoiding API/ABI breaks, we would need introducing new APIs:
*
* - assuan_sendFD/assuan_recvFD sends/receives POSIX fd
* - assuan_get_input_FD/assuan_get_output_FD returns POSIX fd
*
* For this experiment, we don't care about API/ABI breaks, for now.
* And use POSIX fd here.
*
* We use sending MSG_OOB, but it only allows a single-byte in TCP.
* So, it is used to notify other end for fdpassing.
*/
gpg_error_t
w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd)
{
char fdpass_msg[256];
size_t msglen;
int res;
int fd0; /* POSIX fd */
intptr_t fd_converted_to_integer;
HANDLE file_handle;
gpg_error_t err;
fd_converted_to_integer = (intptr_t)fd;
fd0 = (int)fd_converted_to_integer; /* Bit pattern is possibly truncated. */
err = get_file_handle (fd0, ctx->pid, &file_handle);
if (err)
return err;
+#if 0
res = snprintf (fdpass_msg, sizeof (fdpass_msg), FDPASS_FORMAT, file_handle);
if (res < 0)
{
CloseHandle (file_handle);
return gpg_error (GPG_ERR_ASS_PARAMETER);/*FIXME: error*/
}
msglen = (size_t)res + 1; /* Including NUL. */
res = send (HANDLE2SOCKET (ctx->outbound.fd), "!", 1, MSG_OOB);
res = send (HANDLE2SOCKET (ctx->outbound.fd), fdpass_msg, msglen, 0);
return 0;
+#else
+ res = snprintf (fdpass_msg, sizeof (fdpass_msg), "SENDFD %p", file_handle);
+ if (res < 0)
+ {
+ CloseHandle (file_handle);
+ return gpg_error (GPG_ERR_ASS_PARAMETER);/*FIXME: error*/
+ }
+
+ err = assuan_transact (ctx, fdpass_msg, NULL, NULL, NULL, NULL, NULL, NULL);
+ return err;
+#endif
}
static int
process_fdpass_msg (const char *fdpass_msg, size_t msglen, int *r_fd)
{
void *file_handle;
int res;
int fd;
*r_fd = -1;
res = sscanf (fdpass_msg, FDPASS_FORMAT, &file_handle);
if (res != 1)
return -1;
fd = _open_osfhandle ((intptr_t)file_handle, _O_RDWR);
if (fd < 0)
{
CloseHandle (file_handle);
return -1;
}
*r_fd = fd;
return 0;
}
gpg_error_t
w32_fdpass_recv (assuan_context_t ctx, assuan_fd_t *fd)
{
int i;
if (!ctx->uds.pendingfdscount)
{
TRACE0 (ctx, ASSUAN_LOG_SYSIO, "w32_receivefd", ctx,
"no pending file descriptors");
return _assuan_error (ctx, GPG_ERR_ASS_GENERAL);
}
*fd = ctx->uds.pendingfds[0];
for (i=1; i < ctx->uds.pendingfdscount; i++)
ctx->uds.pendingfds[i-1] = ctx->uds.pendingfds[i];
ctx->uds.pendingfdscount--;
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "w32_fdpass_recv", ctx,
"received fd: %p", ctx->uds.pendingfds[0]);
return 0;
}
ssize_t
__assuan_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size)
{
int res;
int ec = 0;
if (ctx->flags.is_socket)
{
fd_set fds;
int tries = 3;
fd_set efds;
FD_ZERO (&fds);
FD_SET (HANDLE2SOCKET (fd), &fds);
FD_ZERO (&efds);
FD_SET (HANDLE2SOCKET (fd), &efds);
res = select (0, &fds, NULL, &efds, NULL);
if (res < 0)
{
gpg_err_set_errno (EIO);
return -1;
}
else if (FD_ISSET (HANDLE2SOCKET (fd), &efds))
{
int fd_recv;
char fdpass_msg[FDPASS_MSG_SIZE];
/* the message of ! */
res = recv (HANDLE2SOCKET (fd), fdpass_msg, sizeof (fdpass_msg), MSG_OOB);
if (res < 0)
{
gpg_err_set_errno (EIO);
return -1;
}
/* the body of message */
res = recv (HANDLE2SOCKET (fd), fdpass_msg, sizeof (fdpass_msg), 0);
if (res < 0)
{
gpg_err_set_errno (EIO);
return -1;
}
res = process_fdpass_msg (fdpass_msg, res, &fd_recv);
if (res < 0)
{
gpg_err_set_errno (EIO);
return -1;
}
ctx->uds.pendingfds[ctx->uds.pendingfdscount++] = (assuan_fd_t)fd_recv;
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_read", ctx,
"received fd: %d", fd_recv);
/* Fall through */
}
again:
ec = 0;
res = recv (HANDLE2SOCKET (fd), buffer, size, 0);
if (res == -1)
ec = WSAGetLastError ();
if (ec == WSAEWOULDBLOCK && tries--)
{
/* EAGAIN: Use select to wait for resources and try again.
We do this 3 times and then give up. The higher level
layer then needs to take care of EAGAIN. No need to
specify a timeout - the socket is not expected to be in
blocking mode. */
FD_ZERO (&fds);
FD_SET (HANDLE2SOCKET (fd), &fds);
select (0, &fds, NULL, NULL, NULL);
goto again;
}
}
else
{
DWORD nread = 0;
if (!ReadFile (fd, buffer, size, &nread, NULL))
{
res = -1;
ec = GetLastError ();
}
else
res = nread;
}
if (res == -1)
{
switch (ec)
{
case WSAENOTSOCK:
gpg_err_set_errno (EBADF);
break;
case WSAEWOULDBLOCK:
gpg_err_set_errno (EAGAIN);
break;
case WSAECONNRESET: /* Due to the use of recv. */
case ERROR_BROKEN_PIPE:
gpg_err_set_errno (EPIPE);
break;
default:
gpg_err_set_errno (EIO);
break;
}
}
return res;
}
ssize_t
__assuan_write (assuan_context_t ctx, assuan_fd_t fd, const void *buffer,
size_t size)
{
int res;
int ec = 0;
if (ctx->flags.is_socket)
{
int tries = 3;
again:
ec = 0;
res = send (HANDLE2SOCKET (fd), buffer, size, 0);
if (res == -1)
ec = WSAGetLastError ();
if (ec == WSAEWOULDBLOCK && tries--)
{
/* EAGAIN: Use select to wait for resources and try again.
We do this 3 times and then give up. The higher level
layer then needs to take care of EAGAIN. No need to
specify a timeout - the socket is not expected to be in
blocking mode. */
fd_set fds;
FD_ZERO (&fds);
FD_SET (HANDLE2SOCKET (fd), &fds);
select (0, NULL, &fds, NULL, NULL);
goto again;
}
}
else
{
DWORD nwrite;
if (!WriteFile (fd, buffer, size, &nwrite, NULL))
{
res = -1;
ec = GetLastError ();
}
else
res = (int)nwrite;
}
if (res == -1)
{
switch (ec)
{
case WSAENOTSOCK:
gpg_err_set_errno (EBADF);
break;
case WSAEWOULDBLOCK:
gpg_err_set_errno (EAGAIN);
break;
case ERROR_BROKEN_PIPE:
case ERROR_NO_DATA:
gpg_err_set_errno (EPIPE);
break;
default:
gpg_err_set_errno (EIO);
break;
}
}
return res;
}
int
__assuan_recvmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg,
int flags)
{
gpg_err_set_errno (ENOSYS);
return -1;
}
int
__assuan_sendmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg,
int flags)
{
gpg_err_set_errno (ENOSYS);
return -1;
}
/* Build a command line for use with W32's CreateProcess. On success
CMDLINE gets the address of a newly allocated string. */
static int
build_w32_commandline (assuan_context_t ctx, const char * const *argv,
char **cmdline)
{
int i, n;
const char *s;
char *buf, *p;
*cmdline = NULL;
n = 0;
for (i=0; (s = argv[i]); i++)
{
n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
for (; *s; s++)
if (*s == '\"')
n++; /* Need to double inner quotes. */
}
n++;
buf = p = _assuan_malloc (ctx, n);
if (! buf)
return -1;
for (i = 0; argv[i]; i++)
{
if (i)
p = stpcpy (p, " ");
if (! *argv[i]) /* Empty string. */
p = stpcpy (p, "\"\"");
else if (strpbrk (argv[i], " \t\n\v\f\""))
{
p = stpcpy (p, "\"");
for (s = argv[i]; *s; s++)
{
*p++ = *s;
if (*s == '\"')
*p++ = *s;
}
*p++ = '\"';
*p = 0;
}
else
p = stpcpy (p, argv[i]);
}
*cmdline= buf;
return 0;
}
int
__assuan_spawn (assuan_context_t ctx, pid_t *r_pid, const char *name,
const char **argv,
assuan_fd_t fd_in, assuan_fd_t fd_out,
assuan_fd_t *fd_child_list,
void (*atfork) (void *opaque, int reserved),
void *atforkvalue, unsigned int flags)
{
SECURITY_ATTRIBUTES sec_attr;
PROCESS_INFORMATION pi =
{
NULL, /* Returns process handle. */
0, /* Returns primary thread handle. */
0, /* Returns pid. */
0 /* Returns tid. */
};
STARTUPINFOW si;
assuan_fd_t fd;
assuan_fd_t *fdp;
char *cmdline;
wchar_t *wcmdline = NULL;
wchar_t *wname = NULL;
HANDLE nullfd = INVALID_HANDLE_VALUE;
int rc;
/* fixme: Actually we should set the "_assuan_pipe_connect_pid" env
variable. However this requires us to write a full environment
handler, because the strings are expected in sorted order. The
suggestion given in the MS Reference Library, to save the old
value, change it, create process and restore it, is not thread
safe. */
/* Build the command line. */
if (build_w32_commandline (ctx, argv, &cmdline))
return -1;
/* Start the process. */
memset (&sec_attr, 0, sizeof sec_attr);
sec_attr.nLength = sizeof sec_attr;
sec_attr.bInheritHandle = FALSE;
memset (&si, 0, sizeof si);
si.cb = sizeof (si);
si.dwFlags = STARTF_USESTDHANDLES;
/* FIXME: Dup to nul if ASSUAN_INVALID_FD. */
si.hStdInput = fd_in;
si.hStdOutput = fd_out;
/* Dup stderr to /dev/null unless it is in the list of FDs to be
passed to the child. */
fd = assuan_fd_from_posix_fd (fileno (stderr));
fdp = fd_child_list;
if (fdp)
{
for (; *fdp != ASSUAN_INVALID_FD && *fdp != fd; fdp++)
;
}
if (!fdp || *fdp == ASSUAN_INVALID_FD)
{
nullfd = CreateFileW (L"nul", GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (nullfd == INVALID_HANDLE_VALUE)
{
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_spawn", ctx,
"can't open `nul': %s", _assuan_w32_strerror (ctx, -1));
_assuan_free (ctx, cmdline);
gpg_err_set_errno (EIO);
return -1;
}
si.hStdError = nullfd;
}
else
si.hStdError = fd;
/* Note: We inherit all handles flagged as inheritable. This seems
to be a security flaw but there seems to be no way of selecting
handles to inherit. A fix for this would be to use a helper
process like we have in gpgme.
Take care: CreateProcessW may modify wpgmname */
/* _assuan_log_printf ("CreateProcess, path=`%s' cmdline=`%s'\n", */
/* name, cmdline); */
if (name && !(wname = _assuan_utf8_to_wchar (name)))
rc = 0;
else if (!(wcmdline = _assuan_utf8_to_wchar (cmdline)))
rc = 0;
else
rc = CreateProcessW (wname, /* Program to start. */
wcmdline, /* Command line arguments. */
&sec_attr, /* Process security attributes. */
&sec_attr, /* Thread security attributes. */
TRUE, /* Inherit handles. */
(CREATE_DEFAULT_ERROR_MODE
| ((flags & 128)? DETACHED_PROCESS : 0)
| GetPriorityClass (GetCurrentProcess ())
| CREATE_SUSPENDED), /* Creation flags. */
NULL, /* Environment. */
NULL, /* Use current drive/directory. */
&si, /* Startup information. */
&pi /* Returns process information. */
);
if (!rc)
{
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "pipe_connect_w32", ctx,
"CreateProcess failed%s: %s", _assuan_w32_strerror (ctx, -1));
free (wname);
free (wcmdline);
_assuan_free (ctx, cmdline);
if (nullfd != INVALID_HANDLE_VALUE)
CloseHandle (nullfd);
gpg_err_set_errno (EIO);
return -1;
}
free (wname);
free (wcmdline);
_assuan_free (ctx, cmdline);
if (nullfd != INVALID_HANDLE_VALUE)
CloseHandle (nullfd);
ResumeThread (pi.hThread);
CloseHandle (pi.hThread);
/* _assuan_log_printf ("CreateProcess ready: hProcess=%p hThread=%p" */
/* " dwProcessID=%d dwThreadId=%d\n", */
/* pi.hProcess, pi.hThread, */
/* (int) pi.dwProcessId, (int) pi.dwThreadId); */
*r_pid = (pid_t) pi.hProcess;
/* No need to modify peer process, as we don't change the handle
names. However this also means we are not safe, as we inherit
too many handles. Should use approach similar to gpgme and glib
using a helper process. */
return 0;
}
/* FIXME: Add some sort of waitpid function that covers GPGME and
gpg-agent's use of assuan. */
pid_t
__assuan_waitpid (assuan_context_t ctx, pid_t pid, int nowait,
int *status, int options)
{
CloseHandle ((HANDLE) pid);
return 0;
}
int
__assuan_socketpair (assuan_context_t ctx, int namespace, int style,
int protocol, assuan_fd_t filedes[2])
{
gpg_err_set_errno (ENOSYS);
return -1;
}
assuan_fd_t
__assuan_socket (assuan_context_t ctx, int domain, int type, int proto)
{
assuan_fd_t res;
res = SOCKET2HANDLE (socket (domain, type, proto));
if (res == SOCKET2HANDLE (INVALID_SOCKET))
gpg_err_set_errno (_assuan_sock_wsa2errno (WSAGetLastError ()));
return res;
}
int
__assuan_connect (assuan_context_t ctx, assuan_fd_t sock,
struct sockaddr *addr, socklen_t length)
{
int res;
res = connect (HANDLE2SOCKET (sock), addr, length);
if (res < 0)
gpg_err_set_errno (_assuan_sock_wsa2errno (WSAGetLastError ()));
return res;
}
/* The default system hooks for assuan contexts. */
struct assuan_system_hooks _assuan_system_hooks =
{
0,
__assuan_usleep,
__assuan_pipe,
__assuan_close,
__assuan_read,
__assuan_write,
__assuan_recvmsg,
__assuan_sendmsg,
__assuan_spawn,
__assuan_waitpid,
__assuan_socketpair,
__assuan_socket,
__assuan_connect
};