2001-11-20 05:27:46 +00:00
|
|
|
|
/* assuan-handler.c - dispatch commands
|
2009-03-06 22:29:49 +00:00
|
|
|
|
* Copyright (C) 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
|
2001-11-20 05:27:46 +00:00
|
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
|
* This file is part of Assuan.
|
2001-11-20 05:27:46 +00:00
|
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
|
* 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.
|
2001-11-20 05:27:46 +00:00
|
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
|
* 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.
|
2001-11-20 05:27:46 +00:00
|
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-03-06 22:29:49 +00:00
|
|
|
|
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2001-11-20 05:27:46 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2004-06-23 13:52:17 +00:00
|
|
|
|
#include <errno.h>
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
|
|
#include "assuan-defs.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-18 19:17:08 +00:00
|
|
|
|
#define spacep(p) (*(p) == ' ' || *(p) == '\t')
|
|
|
|
|
#define digitp(a) ((a) >= '0' && (a) <= '9')
|
2003-02-01 18:53:06 +00:00
|
|
|
|
|
2003-08-18 19:17:08 +00:00
|
|
|
|
static int my_strcasecmp (const char *a, const char *b);
|
2003-02-01 18:53:06 +00:00
|
|
|
|
|
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
#define PROCESS_DONE(ctx, rc) \
|
|
|
|
|
((ctx)->in_process_next ? assuan_process_done ((ctx), (rc)) : (rc))
|
2003-02-01 18:53:06 +00:00
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
dummy_handler (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return
|
|
|
|
|
PROCESS_DONE (ctx, set_error (ctx, Server_Fault, "no handler registered"));
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_nop (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, 0); /* okay */
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_cancel (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (ctx->cancel_notify_fnc)
|
|
|
|
|
ctx->cancel_notify_fnc (ctx);
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, set_error (ctx, Not_Implemented, NULL));
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
2002-01-22 16:29:12 +00:00
|
|
|
|
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_option (assuan_context_t ctx, char *line)
|
2002-01-22 16:29:12 +00:00
|
|
|
|
{
|
|
|
|
|
char *key, *value, *p;
|
|
|
|
|
|
|
|
|
|
for (key=line; spacep (key); key++)
|
|
|
|
|
;
|
|
|
|
|
if (!*key)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return
|
|
|
|
|
PROCESS_DONE (ctx, set_error (ctx, Syntax_Error, "argument required"));
|
2002-01-22 16:29:12 +00:00
|
|
|
|
if (*key == '=')
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return
|
|
|
|
|
PROCESS_DONE (ctx, set_error (ctx, Syntax_Error,
|
|
|
|
|
"no option name given"));
|
2002-01-22 16:29:12 +00:00
|
|
|
|
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)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return
|
|
|
|
|
PROCESS_DONE (ctx, set_error (ctx, Syntax_Error,
|
|
|
|
|
"option argument expected"));
|
2002-01-22 16:29:12 +00:00
|
|
|
|
}
|
|
|
|
|
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 == '-')
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx,
|
|
|
|
|
set_error (ctx, Syntax_Error,
|
|
|
|
|
"option should not begin with one dash"));
|
2002-01-22 16:29:12 +00:00
|
|
|
|
|
|
|
|
|
if (ctx->option_handler_fnc)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, ctx->option_handler_fnc (ctx, key, value));
|
|
|
|
|
return PROCESS_DONE (ctx, 0);
|
2002-01-22 16:29:12 +00:00
|
|
|
|
}
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_bye (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (ctx->bye_notify_fnc)
|
|
|
|
|
ctx->bye_notify_fnc (ctx);
|
2001-12-19 00:23:25 +00:00
|
|
|
|
assuan_close_input_fd (ctx);
|
|
|
|
|
assuan_close_output_fd (ctx);
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, _assuan_error (-1)); /* pretty simple :-) */
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_auth (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, set_error (ctx, Not_Implemented, NULL));
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_reset (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (ctx->reset_notify_fnc)
|
|
|
|
|
ctx->reset_notify_fnc (ctx);
|
2001-12-19 00:23:25 +00:00
|
|
|
|
assuan_close_input_fd (ctx);
|
|
|
|
|
assuan_close_output_fd (ctx);
|
2006-09-19 14:01:54 +00:00
|
|
|
|
_assuan_uds_close_fds (ctx);
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, 0);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
static int
|
|
|
|
|
std_handler_help (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i;
|
|
|
|
|
char buf[ASSUAN_LINELENGTH];
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->cmdtbl_used; i++)
|
|
|
|
|
{
|
|
|
|
|
snprintf (buf, sizeof (buf), "# %s", ctx->cmdtbl[i].name);
|
|
|
|
|
buf[ASSUAN_LINELENGTH - 1] = '\0';
|
|
|
|
|
assuan_write_line (ctx, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PROCESS_DONE (ctx, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_end (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, set_error (ctx, Not_Implemented, NULL));
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
|
assuan_error_t
|
2009-03-06 22:29:49 +00:00
|
|
|
|
assuan_command_parse_fd (assuan_context_t ctx, char *line, assuan_fd_t *rfd)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
char *endp;
|
|
|
|
|
|
2007-07-04 20:03:39 +00:00
|
|
|
|
if ((strncmp (line, "FD", 2) && strncmp (line, "fd", 2))
|
|
|
|
|
|| (line[2] != '=' && line[2] != '\0' && !spacep(&line[2])))
|
2003-08-18 19:17:08 +00:00
|
|
|
|
return set_error (ctx, Syntax_Error, "FD[=<n>] expected");
|
|
|
|
|
line += 2;
|
|
|
|
|
if (*line == '=')
|
|
|
|
|
{
|
|
|
|
|
line ++;
|
|
|
|
|
if (!digitp (*line))
|
|
|
|
|
return set_error (ctx, Syntax_Error, "number required");
|
2009-03-06 22:29:49 +00:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
/* Fixme: For a W32/64bit system we will need to change the cast
|
|
|
|
|
and the conversion fucntion. */
|
|
|
|
|
*rfd = (void*)strtoul (line, &endp, 10);
|
|
|
|
|
#else
|
2003-08-18 19:17:08 +00:00
|
|
|
|
*rfd = strtoul (line, &endp, 10);
|
2009-03-06 22:29:49 +00:00
|
|
|
|
#endif
|
2006-09-19 14:01:54 +00:00
|
|
|
|
/* Remove that argument so that a notify handler won't see it. */
|
2003-08-18 19:17:08 +00:00
|
|
|
|
memset (line, ' ', endp? (endp-line):strlen(line));
|
|
|
|
|
|
|
|
|
|
if (*rfd == ctx->inbound.fd)
|
|
|
|
|
return set_error (ctx, Parameter_Conflict, "fd same as inbound fd");
|
|
|
|
|
if (*rfd == ctx->outbound.fd)
|
|
|
|
|
return set_error (ctx, Parameter_Conflict, "fd same as outbound fd");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
/* Our peer has sent the file descriptor. */
|
|
|
|
|
return assuan_receivefd (ctx, rfd);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
/* Format is INPUT FD=<n> */
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_input (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
int rc;
|
|
|
|
|
assuan_fd_t fd;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
2003-08-18 19:17:08 +00:00
|
|
|
|
rc = assuan_command_parse_fd (ctx, line, &fd);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
if (rc)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, rc);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
ctx->input_fd = fd;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (ctx->input_notify_fnc)
|
|
|
|
|
ctx->input_notify_fnc (ctx, line);
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, 0);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Format is OUTPUT FD=<n> */
|
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
std_handler_output (assuan_context_t ctx, char *line)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
int rc;
|
|
|
|
|
assuan_fd_t fd;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
2003-08-18 19:17:08 +00:00
|
|
|
|
rc = assuan_command_parse_fd (ctx, line, &fd);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
if (rc)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, rc);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
ctx->output_fd = fd;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (ctx->output_notify_fnc)
|
|
|
|
|
ctx->output_notify_fnc (ctx, line);
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, 0);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This is a table with the standard commands and handler for them.
|
2003-08-18 19:17:08 +00:00
|
|
|
|
The table is used to initialize a new context and associate strings
|
|
|
|
|
with default handlers */
|
2001-11-20 05:27:46 +00:00
|
|
|
|
static struct {
|
|
|
|
|
const char *name;
|
2006-09-19 14:01:54 +00:00
|
|
|
|
int (*handler)(assuan_context_t, char *line);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
int always; /* always initialize this command */
|
2001-11-20 05:27:46 +00:00
|
|
|
|
} std_cmd_table[] = {
|
2003-08-18 19:17:08 +00:00
|
|
|
|
{ "NOP", std_handler_nop, 1 },
|
|
|
|
|
{ "CANCEL", std_handler_cancel, 1 },
|
|
|
|
|
{ "OPTION", std_handler_option, 1 },
|
|
|
|
|
{ "BYE", std_handler_bye, 1 },
|
|
|
|
|
{ "AUTH", std_handler_auth, 1 },
|
|
|
|
|
{ "RESET", std_handler_reset, 1 },
|
|
|
|
|
{ "END", std_handler_end, 1 },
|
2009-03-06 22:29:49 +00:00
|
|
|
|
{ "HELP", std_handler_help, 1 },
|
2003-08-18 19:17:08 +00:00
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
{ "INPUT", std_handler_input, 0 },
|
|
|
|
|
{ "OUTPUT", std_handler_output, 0 },
|
2003-08-18 19:17:08 +00:00
|
|
|
|
{ "OPTION", std_handler_option, 1 },
|
2009-03-06 22:29:49 +00:00
|
|
|
|
{ NULL, NULL, 0 }
|
2001-11-20 05:27:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* assuan_register_command:
|
|
|
|
|
* @ctx: the server context
|
|
|
|
|
* @cmd_name: A string with the command name
|
2003-08-18 19:17:08 +00:00
|
|
|
|
* @handler: The handler function to be called or NULL to use a default
|
|
|
|
|
* handler.
|
2001-11-20 05:27:46 +00:00
|
|
|
|
*
|
2003-08-18 19:17:08 +00:00
|
|
|
|
* Register a handler to be used for a given command. Note that
|
|
|
|
|
* several default handlers are already regsitered with a new context.
|
|
|
|
|
* This function however allows to override them.
|
2001-11-20 05:27:46 +00:00
|
|
|
|
*
|
2003-08-18 19:17:08 +00:00
|
|
|
|
* Return value: 0 on success or an error code
|
2001-11-20 05:27:46 +00:00
|
|
|
|
**/
|
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_register_command (assuan_context_t ctx,
|
2003-08-18 19:17:08 +00:00
|
|
|
|
const char *cmd_name,
|
2006-09-19 14:01:54 +00:00
|
|
|
|
int (*handler)(assuan_context_t, char *))
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
2003-08-18 19:17:08 +00:00
|
|
|
|
const char *s;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
|
|
if (cmd_name && !*cmd_name)
|
|
|
|
|
cmd_name = NULL;
|
|
|
|
|
|
2003-08-18 19:17:08 +00:00
|
|
|
|
if (!cmd_name)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
2003-08-18 19:17:08 +00:00
|
|
|
|
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++)
|
|
|
|
|
;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
2003-08-18 19:17:08 +00:00
|
|
|
|
if (s)
|
|
|
|
|
handler = std_cmd_table[i].handler;
|
|
|
|
|
if (!handler)
|
|
|
|
|
handler = dummy_handler; /* Last resort is the dummy handler. */
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ctx->cmdtbl)
|
|
|
|
|
{
|
|
|
|
|
ctx->cmdtbl_size = 50;
|
|
|
|
|
ctx->cmdtbl = xtrycalloc ( ctx->cmdtbl_size, sizeof *ctx->cmdtbl);
|
|
|
|
|
if (!ctx->cmdtbl)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Out_Of_Core);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
ctx->cmdtbl_used = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (ctx->cmdtbl_used >= ctx->cmdtbl_size)
|
|
|
|
|
{
|
|
|
|
|
struct cmdtbl_s *x;
|
|
|
|
|
|
|
|
|
|
x = xtryrealloc ( ctx->cmdtbl, (ctx->cmdtbl_size+10) * sizeof *x);
|
|
|
|
|
if (!x)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Out_Of_Core);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
ctx->cmdtbl = x;
|
|
|
|
|
ctx->cmdtbl_size += 50;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx->cmdtbl[ctx->cmdtbl_used].name = cmd_name;
|
|
|
|
|
ctx->cmdtbl[ctx->cmdtbl_used].handler = handler;
|
|
|
|
|
ctx->cmdtbl_used++;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 16:25:46 +00:00
|
|
|
|
int
|
|
|
|
|
assuan_register_post_cmd_notify (assuan_context_t ctx,
|
|
|
|
|
void (*fnc)(assuan_context_t, int))
|
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
|
|
|
|
ctx->post_cmd_notify_fnc = fnc;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-13 15:04:36 +00:00
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_register_bye_notify (assuan_context_t ctx,
|
|
|
|
|
void (*fnc)(assuan_context_t))
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
ctx->bye_notify_fnc = fnc;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_register_reset_notify (assuan_context_t ctx,
|
|
|
|
|
void (*fnc)(assuan_context_t))
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
ctx->reset_notify_fnc = fnc;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_register_cancel_notify (assuan_context_t ctx,
|
|
|
|
|
void (*fnc)(assuan_context_t))
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
ctx->cancel_notify_fnc = fnc;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_register_option_handler (assuan_context_t ctx,
|
|
|
|
|
int (*fnc)(assuan_context_t,
|
2002-01-22 16:29:12 +00:00
|
|
|
|
const char*, const char*))
|
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2002-01-22 16:29:12 +00:00
|
|
|
|
ctx->option_handler_fnc = fnc;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-13 15:04:36 +00:00
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_register_input_notify (assuan_context_t ctx,
|
|
|
|
|
void (*fnc)(assuan_context_t, const char *))
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
ctx->input_notify_fnc = fnc;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_register_output_notify (assuan_context_t ctx,
|
|
|
|
|
void (*fnc)(assuan_context_t, const char *))
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
ctx->output_notify_fnc = fnc;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
/* Helper to register the standards commands */
|
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
_assuan_register_std_commands (assuan_context_t ctx)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
int i, rc;
|
|
|
|
|
|
|
|
|
|
for (i=0; std_cmd_table[i].name; i++)
|
|
|
|
|
{
|
|
|
|
|
if (std_cmd_table[i].always)
|
|
|
|
|
{
|
2003-08-18 19:17:08 +00:00
|
|
|
|
rc = assuan_register_command (ctx, std_cmd_table[i].name, NULL);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
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 int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
handle_data_line (assuan_context_t ctx, char *line, int linelen)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
return set_error (ctx, Not_Implemented, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
|
/* 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);
|
|
|
|
|
}
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
/* Parse the line, break out the command, find it in the command
|
2005-08-09 13:19:24 +00:00
|
|
|
|
table, remove leading and white spaces from the arguments, call the
|
2009-03-06 22:29:49 +00:00
|
|
|
|
handler with the argument line and return the error. */
|
2001-11-20 05:27:46 +00:00
|
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
dispatch_command (assuan_context_t ctx, char *line, int linelen)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
char *p;
|
|
|
|
|
const char *s;
|
|
|
|
|
int shift, i;
|
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
/* Note that as this function is invoked by assuan_process_next as
|
|
|
|
|
well, we need to hide non-critical errors with PROCESS_DONE. */
|
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
if (*line == 'D' && line[1] == ' ') /* divert to special handler */
|
2009-03-06 22:29:49 +00:00
|
|
|
|
/* 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));
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
|
|
for (p=line; *p && *p != ' ' && *p != '\t'; p++)
|
|
|
|
|
;
|
|
|
|
|
if (p==line)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE
|
|
|
|
|
(ctx, set_error (ctx, Syntax_Error, "leading white-space"));
|
2001-11-20 05:27:46 +00:00
|
|
|
|
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++)
|
2002-01-22 16:29:12 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-11-20 05:27:46 +00:00
|
|
|
|
if (!s)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
return PROCESS_DONE (ctx, set_error (ctx, Unknown_Command, NULL));
|
2001-11-20 05:27:46 +00:00
|
|
|
|
line += shift;
|
|
|
|
|
linelen -= shift;
|
|
|
|
|
|
|
|
|
|
/* fprintf (stderr, "DBG-assuan: processing %s `%s'\n", s, line); */
|
|
|
|
|
return ctx->cmdtbl[i].handler (ctx, line);
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-13 15:04:36 +00:00
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
/* Call this to acknowledge the current command. */
|
|
|
|
|
int
|
|
|
|
|
assuan_process_done (assuan_context_t ctx, int rc)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
if (!ctx->in_command)
|
|
|
|
|
return _assuan_error (ASSUAN_General_Error);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
ctx->in_command = 0;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
/* Check for data write errors. */
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (ctx->outbound.data.fp)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
{
|
|
|
|
|
/* Flush the data lines. */
|
2001-12-13 15:04:36 +00:00
|
|
|
|
fclose (ctx->outbound.data.fp);
|
|
|
|
|
ctx->outbound.data.fp = NULL;
|
|
|
|
|
if (!rc && ctx->outbound.data.error)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
rc = ctx->outbound.data.error;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
2009-03-06 22:29:49 +00:00
|
|
|
|
else
|
2002-04-24 01:55:58 +00:00
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
/* Flush any data send without using the data FP. */
|
2002-04-24 01:55:58 +00:00
|
|
|
|
assuan_send_data (ctx, NULL, 0);
|
|
|
|
|
if (!rc && ctx->outbound.data.error)
|
2009-03-06 22:29:49 +00:00
|
|
|
|
rc = ctx->outbound.data.error;
|
2002-04-24 01:55:58 +00:00
|
|
|
|
}
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
|
|
|
|
/* Error handling. */
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (!rc)
|
|
|
|
|
{
|
2002-01-22 16:29:12 +00:00
|
|
|
|
rc = assuan_write_line (ctx, ctx->okay_line? ctx->okay_line : "OK");
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
2006-09-19 14:01:54 +00:00
|
|
|
|
else if (err_is_eof (rc))
|
|
|
|
|
{ /* No error checking because the peer may have already disconnect. */
|
2001-12-13 15:04:36 +00:00
|
|
|
|
assuan_write_line (ctx, "OK closing connection");
|
2002-01-22 16:29:12 +00:00
|
|
|
|
ctx->finish_handler (ctx);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-08-09 13:19:24 +00:00
|
|
|
|
char errline[300];
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2002-04-24 01:55:58 +00:00
|
|
|
|
if (rc < 100)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
sprintf (errline, "ERR %d server fault (%.50s)",
|
2006-09-19 14:01:54 +00:00
|
|
|
|
_assuan_error (ASSUAN_Server_Fault), assuan_strerror (rc));
|
2001-12-13 15:04:36 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const char *text = ctx->err_no == rc? ctx->err_str:NULL;
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2007-07-04 20:03:39 +00:00
|
|
|
|
#if defined(HAVE_W32_SYSTEM)
|
|
|
|
|
unsigned int source, code;
|
|
|
|
|
char ebuf[50];
|
|
|
|
|
const char *esrc;
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2007-07-04 20:03:39 +00:00
|
|
|
|
source = ((rc >> 24) & 0xff);
|
|
|
|
|
code = (rc & 0x00ffffff);
|
|
|
|
|
if (source
|
|
|
|
|
&& !_assuan_gpg_strerror_r (rc, ebuf, sizeof ebuf)
|
|
|
|
|
&& (esrc=_assuan_gpg_strsource (rc)))
|
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
/* Assume this is an libgpg-error. */
|
2007-07-04 20:03:39 +00:00
|
|
|
|
sprintf (errline, "ERR %d %.50s <%.30s>%s%.100s",
|
|
|
|
|
rc, ebuf, esrc,
|
|
|
|
|
text? " - ":"", text?text:"");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#elif defined(__GNUC__) && defined(__ELF__)
|
2005-08-09 13:19:24 +00:00
|
|
|
|
/* If we have weak symbol support we try to use the error
|
|
|
|
|
strings from libgpg-error without creating a dependency.
|
|
|
|
|
They are used for debugging purposes only, so there is no
|
|
|
|
|
problem if they are not available. We need to make sure
|
2006-09-19 14:01:54 +00:00
|
|
|
|
that we are using ELF because only this guarantees that
|
2005-08-09 13:19:24 +00:00
|
|
|
|
weak symbol support is available in case GNU ld is not
|
2006-11-29 16:25:46 +00:00
|
|
|
|
used. It seems that old gcc versions don't implement the
|
|
|
|
|
weak attribute properly but it works with the weak
|
|
|
|
|
pragma. */
|
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
|
unsigned int source, code;
|
|
|
|
|
|
|
|
|
|
int gpg_strerror_r (unsigned int err, char *buf, size_t buflen)
|
|
|
|
|
__attribute__ ((weak));
|
|
|
|
|
const char *gpg_strsource (unsigned int err)
|
|
|
|
|
__attribute__ ((weak));
|
2007-07-04 20:03:39 +00:00
|
|
|
|
#if __GNUC__ < 3
|
2006-11-29 16:25:46 +00:00
|
|
|
|
#pragma weak gpg_strerror_r
|
|
|
|
|
#pragma weak gpg_strsource
|
|
|
|
|
#endif
|
2005-08-09 13:19:24 +00:00
|
|
|
|
|
|
|
|
|
source = ((rc >> 24) & 0xff);
|
|
|
|
|
code = (rc & 0x00ffffff);
|
|
|
|
|
if (source && gpg_strsource && gpg_strerror_r)
|
|
|
|
|
{
|
|
|
|
|
/* Assume this is an libgpg-error. */
|
|
|
|
|
char ebuf[50];
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
|
gpg_strerror_r (rc, ebuf, sizeof ebuf );
|
|
|
|
|
sprintf (errline, "ERR %d %.50s <%.30s>%s%.100s",
|
|
|
|
|
rc,
|
|
|
|
|
ebuf,
|
|
|
|
|
gpg_strsource (rc),
|
|
|
|
|
text? " - ":"", text?text:"");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif /* __GNUC__ && __ELF__ */
|
|
|
|
|
sprintf (errline, "ERR %d %.50s%s%.100s",
|
|
|
|
|
rc, assuan_strerror (rc), text? " - ":"", text?text:"");
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
rc = assuan_write_line (ctx, errline);
|
|
|
|
|
}
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2006-11-29 16:25:46 +00:00
|
|
|
|
if (ctx->post_cmd_notify_fnc)
|
|
|
|
|
ctx->post_cmd_notify_fnc (ctx, rc);
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
|
ctx->confidential = 0;
|
|
|
|
|
if (ctx->okay_line)
|
|
|
|
|
{
|
|
|
|
|
xfree (ctx->okay_line);
|
|
|
|
|
ctx->okay_line = NULL;
|
|
|
|
|
}
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
process_next (assuan_context_t ctx)
|
|
|
|
|
{
|
|
|
|
|
int 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 (rc))
|
|
|
|
|
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->in_command)
|
|
|
|
|
{
|
|
|
|
|
ctx->in_command = 1;
|
|
|
|
|
|
|
|
|
|
ctx->outbound.data.error = 0;
|
|
|
|
|
ctx->outbound.data.linelen = 0;
|
|
|
|
|
/* Dispatch command and return reply. */
|
|
|
|
|
ctx->in_process_next = 1;
|
|
|
|
|
rc = dispatch_command (ctx, ctx->inbound.line, ctx->inbound.linelen);
|
|
|
|
|
ctx->in_process_next = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (ctx->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. */
|
|
|
|
|
_assuan_log_printf ("unexpected client data\n");
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-13 15:04:36 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
int
|
|
|
|
|
assuan_process_next (assuan_context_t ctx)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
rc = process_next (ctx);
|
|
|
|
|
}
|
|
|
|
|
while (!rc && assuan_pending_line (ctx));
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
process_request (assuan_context_t ctx)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
if (ctx->in_inquire)
|
|
|
|
|
return _assuan_error (ASSUAN_Nested_Commands);
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
rc = _assuan_read_line (ctx);
|
|
|
|
|
}
|
|
|
|
|
while (_assuan_error_is_eagain (rc));
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
if (*ctx->inbound.line == '#' || !ctx->inbound.linelen)
|
|
|
|
|
return 0; /* comment line - ignore */
|
|
|
|
|
|
|
|
|
|
ctx->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);
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* assuan_process:
|
|
|
|
|
* @ctx: assuan context
|
|
|
|
|
*
|
2005-08-09 13:19:24 +00:00
|
|
|
|
* This function is used to handle the assuan protocol after a
|
2001-11-20 05:27:46 +00:00
|
|
|
|
* 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.
|
|
|
|
|
**/
|
|
|
|
|
int
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_process (assuan_context_t ctx)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
do {
|
2001-12-13 15:04:36 +00:00
|
|
|
|
rc = process_request (ctx);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
} while (!rc);
|
|
|
|
|
|
2006-09-19 14:01:54 +00:00
|
|
|
|
if (err_is_eof (rc))
|
2001-11-20 05:27:46 +00:00
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-13 15:04:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
2001-12-19 00:23:25 +00:00
|
|
|
|
* assuan_process_next() if there is an active one. The first fd in
|
|
|
|
|
* the array is the one used for the command connection.
|
2001-12-13 15:04:36 +00:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_get_active_fds (assuan_context_t ctx, int what,
|
2009-03-06 22:29:49 +00:00
|
|
|
|
assuan_fd_t *fdarray, int fdarraysize)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
2001-12-13 17:37:04 +00:00
|
|
|
|
if (!ctx || fdarraysize < 2 || what < 0 || what > 1)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (!what)
|
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
if (ctx->inbound.fd != ASSUAN_INVALID_FD)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
fdarray[n++] = ctx->inbound.fd;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
|
if (ctx->outbound.fd != ASSUAN_INVALID_FD)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
fdarray[n++] = ctx->outbound.fd;
|
|
|
|
|
if (ctx->outbound.data.fp)
|
2007-07-12 18:54:45 +00:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2009-03-06 22:29:49 +00:00
|
|
|
|
fdarray[n++] = (void*)_get_osfhandle (fileno (ctx->outbound.data.fp));
|
2007-07-12 18:54:45 +00:00
|
|
|
|
#else
|
2001-12-13 15:04:36 +00:00
|
|
|
|
fdarray[n++] = fileno (ctx->outbound.data.fp);
|
2007-07-12 18:54:45 +00:00
|
|
|
|
#endif
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 16:25:46 +00:00
|
|
|
|
|
|
|
|
|
/* 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*/
|
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
/* 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 *
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_get_data_fp (assuan_context_t ctx)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
2004-02-18 18:05:38 +00:00
|
|
|
|
#if defined (HAVE_FOPENCOOKIE) || defined (HAVE_FUNOPEN)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
if (ctx->outbound.data.fp)
|
|
|
|
|
return ctx->outbound.data.fp;
|
|
|
|
|
|
2006-11-29 16:25:46 +00:00
|
|
|
|
#ifdef HAVE_FUNOPEN
|
|
|
|
|
ctx->outbound.data.fp = funopen (ctx, 0, fun1_cookie_write,
|
2003-08-18 19:17:08 +00:00
|
|
|
|
0, _assuan_cookie_write_flush);
|
2006-11-29 16:25:46 +00:00
|
|
|
|
#else
|
|
|
|
|
ctx->outbound.data.fp = funopen (ctx, 0, fun2_cookie_write,
|
|
|
|
|
0, _assuan_cookie_write_flush);
|
|
|
|
|
#endif
|
|
|
|
|
|
2001-11-20 05:27:46 +00:00
|
|
|
|
ctx->outbound.data.error = 0;
|
|
|
|
|
return ctx->outbound.data.fp;
|
2004-02-18 18:05:38 +00:00
|
|
|
|
#else
|
|
|
|
|
errno = ENOSYS;
|
|
|
|
|
return NULL;
|
|
|
|
|
#endif
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
|
/* Set the text used for the next OK reponse. This string is
|
|
|
|
|
automatically reset to NULL after the next command. */
|
2005-08-09 13:19:24 +00:00
|
|
|
|
assuan_error_t
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_set_okay_line (assuan_context_t ctx, const char *line)
|
2002-01-22 16:29:12 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2002-01-22 16:29:12 +00:00
|
|
|
|
if (!line)
|
|
|
|
|
{
|
|
|
|
|
xfree (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 = xtrymalloc (3+strlen(line)+1);
|
|
|
|
|
if (!buf)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Out_Of_Core);
|
2002-01-22 16:29:12 +00:00
|
|
|
|
strcpy (buf, "OK ");
|
|
|
|
|
strcpy (buf+3, line);
|
|
|
|
|
xfree (ctx->okay_line);
|
|
|
|
|
ctx->okay_line = buf;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
|
assuan_error_t
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_write_status (assuan_context_t ctx,
|
|
|
|
|
const char *keyword, const char *text)
|
2001-11-20 05:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
char buffer[256];
|
|
|
|
|
char *helpbuf;
|
|
|
|
|
size_t n;
|
2005-08-09 13:19:24 +00:00
|
|
|
|
assuan_error_t ae;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
|
|
if ( !ctx || !keyword)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2005-08-09 13:19:24 +00:00
|
|
|
|
ae = assuan_write_line (ctx, buffer);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
else if ( (helpbuf = xtrymalloc (n)) )
|
|
|
|
|
{
|
|
|
|
|
strcpy (helpbuf, "S ");
|
|
|
|
|
strcat (helpbuf, keyword);
|
|
|
|
|
if (*text)
|
|
|
|
|
{
|
|
|
|
|
strcat (helpbuf, " ");
|
|
|
|
|
strcat (helpbuf, text);
|
|
|
|
|
}
|
2005-08-09 13:19:24 +00:00
|
|
|
|
ae = assuan_write_line (ctx, helpbuf);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
xfree (helpbuf);
|
|
|
|
|
}
|
2005-08-09 13:19:24 +00:00
|
|
|
|
else
|
|
|
|
|
ae = 0;
|
|
|
|
|
return ae;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
}
|