2001-11-20 05:27:46 +00:00
|
|
|
/* assuan-listen.c - Wait for a connection (server)
|
2005-08-09 13:19:24 +00:00
|
|
|
* Copyright (C) 2001, 2002, 2004 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>
|
2001-12-19 00:23:25 +00:00
|
|
|
#include <unistd.h>
|
2006-09-19 14:01:54 +00:00
|
|
|
#include <errno.h>
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
#include "assuan-defs.h"
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_error_t
|
|
|
|
assuan_set_hello_line (assuan_context_t ctx, const char *line)
|
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
|
|
|
if (!line)
|
|
|
|
{
|
|
|
|
xfree (ctx->hello_line);
|
|
|
|
ctx->hello_line = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *buf = xtrymalloc (3+strlen(line)+1);
|
|
|
|
if (!buf)
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Out_Of_Core);
|
2005-08-09 13:19:24 +00:00
|
|
|
if (strchr (line, '\n'))
|
|
|
|
strcpy (buf, line);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy (buf, "OK ");
|
|
|
|
strcpy (buf+3, line);
|
|
|
|
}
|
2001-12-13 15:04:36 +00:00
|
|
|
xfree (ctx->hello_line);
|
|
|
|
ctx->hello_line = buf;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* assuan_accept:
|
|
|
|
* @ctx: context
|
|
|
|
*
|
2003-08-18 19:17:08 +00:00
|
|
|
* Cancel any existing connection and wait for a connection from a
|
2001-11-20 05:27:46 +00:00
|
|
|
* client. The initial handshake is performed which may include an
|
|
|
|
* initial authentication or encryption negotiation.
|
|
|
|
*
|
|
|
|
* Return value: 0 on success or an error if the connection could for
|
|
|
|
* some reason not be established.
|
|
|
|
**/
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_error_t
|
|
|
|
assuan_accept (assuan_context_t ctx)
|
2001-11-20 05:27:46 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2005-08-09 13:19:24 +00:00
|
|
|
const char *p, *pend;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
if (!ctx)
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
|
|
|
if (ctx->pipe_mode > 1)
|
|
|
|
return -1; /* second invocation for pipemode -> terminate */
|
2002-01-22 16:29:12 +00:00
|
|
|
ctx->finish_handler (ctx);
|
2001-11-20 05:27:46 +00:00
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
rc = ctx->accept_handler (ctx);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2001-11-20 05:27:46 +00:00
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
/* Send the hello. */
|
|
|
|
p = ctx->hello_line;
|
|
|
|
if (p && (pend = strchr (p, '\n')))
|
|
|
|
{ /* This is a multi line hello. Send all but the last line as
|
|
|
|
comments. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rc = _assuan_write_line (ctx, "# ", p, pend - p);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
p = pend + 1;
|
|
|
|
pend = strchr (p, '\n');
|
|
|
|
}
|
|
|
|
while (pend);
|
|
|
|
rc = _assuan_write_line (ctx, "OK ", p, strlen (p));
|
|
|
|
}
|
|
|
|
else if (p)
|
|
|
|
rc = assuan_write_line (ctx, p);
|
|
|
|
else
|
|
|
|
rc = assuan_write_line (ctx, "OK Pleased to meet you");
|
2001-11-20 05:27:46 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (ctx->pipe_mode)
|
|
|
|
ctx->pipe_mode = 2;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-13 15:04:36 +00:00
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
assuan_fd_t
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_get_input_fd (assuan_context_t ctx)
|
2001-11-20 05:27:46 +00:00
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
return ctx? ctx->input_fd : ASSUAN_INVALID_FD;
|
2001-11-20 05:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-06 22:29:49 +00:00
|
|
|
assuan_fd_t
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_get_output_fd (assuan_context_t ctx)
|
2001-11-20 05:27:46 +00:00
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
return ctx? ctx->output_fd : ASSUAN_INVALID_FD;
|
2001-11-20 05:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-19 00:23:25 +00:00
|
|
|
/* Close the fd descriptor set by the command INPUT FD=n. We handle
|
|
|
|
this fd inside assuan so that we can do some initial checks */
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_error_t
|
|
|
|
assuan_close_input_fd (assuan_context_t ctx)
|
2001-12-19 00:23:25 +00:00
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
if (!ctx || ctx->input_fd == ASSUAN_INVALID_FD)
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2005-08-09 13:19:24 +00:00
|
|
|
_assuan_close (ctx->input_fd);
|
2009-03-06 22:29:49 +00:00
|
|
|
ctx->input_fd = ASSUAN_INVALID_FD;
|
2001-12-19 00:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the fd descriptor set by the command OUTPUT FD=n. We handle
|
|
|
|
this fd inside assuan so that we can do some initial checks */
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_error_t
|
|
|
|
assuan_close_output_fd (assuan_context_t ctx)
|
2001-12-19 00:23:25 +00:00
|
|
|
{
|
2009-03-06 22:29:49 +00:00
|
|
|
if (!ctx || ctx->output_fd == ASSUAN_INVALID_FD)
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2001-12-19 00:23:25 +00:00
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
_assuan_close (ctx->output_fd);
|
2009-03-06 22:29:49 +00:00
|
|
|
ctx->output_fd = ASSUAN_INVALID_FD;
|
2001-12-19 00:23:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|