2001-12-13 15:04:36 +00:00
|
|
|
|
/* assuan-client.c - client functions
|
2005-10-01 19:51:27 +00:00
|
|
|
|
* Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
|
2001-12-13 15:04:36 +00:00
|
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
|
* This file is part of Assuan.
|
2001-12-13 15:04:36 +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-12-13 15:04:36 +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-12-13 15:04:36 +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-12-13 15:04:36 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#include "assuan-defs.h"
|
|
|
|
|
|
|
|
|
|
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
|
|
|
|
|
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
|
|
|
|
|
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
|
|
|
|
|
|
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
|
assuan_error_t
|
2006-09-19 14:01:54 +00:00
|
|
|
|
_assuan_read_from_server (assuan_context_t ctx, int *okay, int *off)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
char *line;
|
|
|
|
|
int linelen;
|
2005-08-09 13:19:24 +00:00
|
|
|
|
assuan_error_t rc;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
|
|
|
|
|
*okay = 0;
|
|
|
|
|
*off = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
assuan/
2009-04-08 Marcus Brinkmann <marcus@g10code.de>
* assuan.h (_gpgme_io_socket): New prototype.
(_ASSUAN_CUSTOM_IO, _assuan_custom_close, _assuan_custom_read)
(_assuan_custom_write, _assuan_custom_pipe, _assuan_custom_socket)
(_assuan_custom_connect): New macros.
* assuan-socket.c (_assuan_close, _assuan_sock_new)
(_assuan_sock_connect) [_ASSUAN_CUSTOM_IO]: Use custom I/O function.
* assuan-buffer.c (assuan_read_line): Do not handle EAGAIN anymore.
* assuan-client.c (_assuan_read_from_server): Likewise.
* assuan-handler.c (process_next): Likewise
* assuan-inquire.c (assuan_inquire): Likewise.
src/
2009-04-08 Marcus Brinkmann <marcus@g10code.de>
* w32-glib-io.c (giochannel_table): New members used, fd, socket.
(find_channel): Drop CREATE argument.
(new_dummy_channel_from_fd, new_channel_from_fd)
(new_channel_from_socket): New functions.
(_gpgm_io_fd2str): Implement for sockets.
(_gpgme_io_write, _gpgme_io_read): Translate EAGAIN errors
correctly.
(_gpgme_io_pipe): Fix for new channel bookkeeping.
(_gpgme_io_close, _gpgme_io_dup): Likewise.
(wsa2errno, _gpgme_io_socket, _gpgme_io_connect): New.
* w32-io.c (MAX_READERS, MAX_WRITERS): Bump up to 40.
(wsa2errno, _gpgme_io_socket, _gpgme_io_connect): New.
* w32-qt-io.cpp (_gpgme_io_socket, _gpgme_io_connect): New stubs.
* version.c [HAVE_W32_SYSTEM]: Include "windows.h.
(do_subsystem_inits) [HAVE_W32_SYSTEM]: Call WSAStartup.
* engine-assuan.c (llass_status_handler): Ignore EAGAIN errors.
2009-04-08 18:53:57 +00:00
|
|
|
|
rc = _assuan_read_line (ctx);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
line = ctx->inbound.line;
|
|
|
|
|
linelen = ctx->inbound.linelen;
|
assuan/
2009-04-08 Marcus Brinkmann <marcus@g10code.de>
* assuan.h (_gpgme_io_socket): New prototype.
(_ASSUAN_CUSTOM_IO, _assuan_custom_close, _assuan_custom_read)
(_assuan_custom_write, _assuan_custom_pipe, _assuan_custom_socket)
(_assuan_custom_connect): New macros.
* assuan-socket.c (_assuan_close, _assuan_sock_new)
(_assuan_sock_connect) [_ASSUAN_CUSTOM_IO]: Use custom I/O function.
* assuan-buffer.c (assuan_read_line): Do not handle EAGAIN anymore.
* assuan-client.c (_assuan_read_from_server): Likewise.
* assuan-handler.c (process_next): Likewise
* assuan-inquire.c (assuan_inquire): Likewise.
src/
2009-04-08 Marcus Brinkmann <marcus@g10code.de>
* w32-glib-io.c (giochannel_table): New members used, fd, socket.
(find_channel): Drop CREATE argument.
(new_dummy_channel_from_fd, new_channel_from_fd)
(new_channel_from_socket): New functions.
(_gpgm_io_fd2str): Implement for sockets.
(_gpgme_io_write, _gpgme_io_read): Translate EAGAIN errors
correctly.
(_gpgme_io_pipe): Fix for new channel bookkeeping.
(_gpgme_io_close, _gpgme_io_dup): Likewise.
(wsa2errno, _gpgme_io_socket, _gpgme_io_connect): New.
* w32-io.c (MAX_READERS, MAX_WRITERS): Bump up to 40.
(wsa2errno, _gpgme_io_socket, _gpgme_io_connect): New.
* w32-qt-io.cpp (_gpgme_io_socket, _gpgme_io_connect): New stubs.
* version.c [HAVE_W32_SYSTEM]: Include "windows.h.
(do_subsystem_inits) [HAVE_W32_SYSTEM]: Call WSAStartup.
* engine-assuan.c (llass_status_handler): Ignore EAGAIN errors.
2009-04-08 18:53:57 +00:00
|
|
|
|
}
|
2001-12-13 15:04:36 +00:00
|
|
|
|
while (*line == '#' || !linelen);
|
|
|
|
|
|
|
|
|
|
if (linelen >= 1
|
|
|
|
|
&& line[0] == 'D' && line[1] == ' ')
|
|
|
|
|
{
|
|
|
|
|
*okay = 2; /* data line */
|
|
|
|
|
*off = 2;
|
|
|
|
|
}
|
2002-04-24 01:55:58 +00:00
|
|
|
|
else if (linelen >= 1
|
|
|
|
|
&& line[0] == 'S'
|
|
|
|
|
&& (line[1] == '\0' || line[1] == ' '))
|
|
|
|
|
{
|
|
|
|
|
*okay = 4;
|
|
|
|
|
*off = 1;
|
|
|
|
|
while (line[*off] == ' ')
|
|
|
|
|
++*off;
|
|
|
|
|
}
|
2001-12-13 15:04:36 +00:00
|
|
|
|
else if (linelen >= 2
|
|
|
|
|
&& line[0] == 'O' && line[1] == 'K'
|
|
|
|
|
&& (line[2] == '\0' || line[2] == ' '))
|
|
|
|
|
{
|
|
|
|
|
*okay = 1;
|
|
|
|
|
*off = 2;
|
2002-01-22 16:29:12 +00:00
|
|
|
|
while (line[*off] == ' ')
|
|
|
|
|
++*off;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
else if (linelen >= 3
|
|
|
|
|
&& line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
|
|
|
|
|
&& (line[3] == '\0' || line[3] == ' '))
|
|
|
|
|
{
|
|
|
|
|
*okay = 0;
|
|
|
|
|
*off = 3;
|
2002-01-22 16:29:12 +00:00
|
|
|
|
while (line[*off] == ' ')
|
|
|
|
|
++*off;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
else if (linelen >= 7
|
|
|
|
|
&& line[0] == 'I' && line[1] == 'N' && line[2] == 'Q'
|
|
|
|
|
&& line[3] == 'U' && line[4] == 'I' && line[5] == 'R'
|
|
|
|
|
&& line[6] == 'E'
|
|
|
|
|
&& (line[7] == '\0' || line[7] == ' '))
|
|
|
|
|
{
|
|
|
|
|
*okay = 3;
|
|
|
|
|
*off = 7;
|
2002-01-22 16:29:12 +00:00
|
|
|
|
while (line[*off] == ' ')
|
|
|
|
|
++*off;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
2002-04-24 01:55:58 +00:00
|
|
|
|
else if (linelen >= 3
|
|
|
|
|
&& line[0] == 'E' && line[1] == 'N' && line[2] == 'D'
|
|
|
|
|
&& (line[3] == '\0' || line[3] == ' '))
|
|
|
|
|
{
|
|
|
|
|
*okay = 5; /* end line */
|
|
|
|
|
*off = 3;
|
|
|
|
|
}
|
2001-12-13 15:04:36 +00:00
|
|
|
|
else
|
2006-09-19 14:01:54 +00:00
|
|
|
|
rc = _assuan_error (ASSUAN_Invalid_Response);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* assuan_transact:
|
|
|
|
|
* @ctx: The Assuan context
|
2006-09-19 14:01:54 +00:00
|
|
|
|
* @command: Command line to be send to the server
|
2001-12-13 15:04:36 +00:00
|
|
|
|
* @data_cb: Callback function for data lines
|
|
|
|
|
* @data_cb_arg: first argument passed to @data_cb
|
|
|
|
|
* @inquire_cb: Callback function for a inquire response
|
|
|
|
|
* @inquire_cb_arg: first argument passed to @inquire_cb
|
2002-04-24 01:55:58 +00:00
|
|
|
|
* @status_cb: Callback function for a status response
|
|
|
|
|
* @status_cb_arg: first argument passed to @status_cb
|
2001-12-13 15:04:36 +00:00
|
|
|
|
*
|
|
|
|
|
* FIXME: Write documentation
|
|
|
|
|
*
|
|
|
|
|
* Return value: 0 on success or error code. The error code may be
|
|
|
|
|
* the one one returned by the server in error lines or from the
|
2006-09-19 14:01:54 +00:00
|
|
|
|
* callback functions. Take care: When a callback returns an error
|
|
|
|
|
* this function returns immediately with an error and thus the caller
|
|
|
|
|
* will altter return an Assuan error (write erro in most cases).
|
2001-12-13 15:04:36 +00:00
|
|
|
|
**/
|
2005-08-09 13:19:24 +00:00
|
|
|
|
assuan_error_t
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_transact (assuan_context_t ctx,
|
2001-12-13 15:04:36 +00:00
|
|
|
|
const char *command,
|
2006-09-19 14:01:54 +00:00
|
|
|
|
int (*data_cb)(void *, const void *, size_t),
|
2001-12-13 15:04:36 +00:00
|
|
|
|
void *data_cb_arg,
|
2006-09-19 14:01:54 +00:00
|
|
|
|
int (*inquire_cb)(void*, const char *),
|
2002-04-24 01:55:58 +00:00
|
|
|
|
void *inquire_cb_arg,
|
2006-09-19 14:01:54 +00:00
|
|
|
|
int (*status_cb)(void*, const char *),
|
2002-04-24 01:55:58 +00:00
|
|
|
|
void *status_cb_arg)
|
2001-12-13 15:04:36 +00:00
|
|
|
|
{
|
2006-09-19 14:01:54 +00:00
|
|
|
|
assuan_error_t rc;
|
|
|
|
|
int okay, off;
|
2005-10-01 19:51:27 +00:00
|
|
|
|
char *line;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
int linelen;
|
|
|
|
|
|
|
|
|
|
rc = assuan_write_line (ctx, command);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
|
if (*command == '#' || !*command)
|
|
|
|
|
return 0; /* Don't expect a response for a comment line. */
|
|
|
|
|
|
2001-12-13 15:04:36 +00:00
|
|
|
|
again:
|
|
|
|
|
rc = _assuan_read_from_server (ctx, &okay, &off);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc; /* error reading from server */
|
|
|
|
|
|
|
|
|
|
line = ctx->inbound.line + off;
|
|
|
|
|
linelen = ctx->inbound.linelen - off;
|
|
|
|
|
|
|
|
|
|
if (!okay)
|
|
|
|
|
{
|
2006-11-29 16:25:46 +00:00
|
|
|
|
rc = atoi (line);
|
|
|
|
|
if (rc > 0 && rc < 100)
|
|
|
|
|
rc = _assuan_error (ASSUAN_Server_Fault);
|
2007-07-04 20:03:39 +00:00
|
|
|
|
else if (rc > 0 && rc <= 405)
|
2006-11-29 16:25:46 +00:00
|
|
|
|
rc = _assuan_error (rc);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
else if (okay == 2)
|
|
|
|
|
{
|
|
|
|
|
if (!data_cb)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
rc = _assuan_error (ASSUAN_No_Data_Callback);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2005-10-01 19:51:27 +00:00
|
|
|
|
char *s, *d;
|
2001-12-13 15:04:36 +00:00
|
|
|
|
|
|
|
|
|
for (s=d=line; linelen; linelen--)
|
|
|
|
|
{
|
|
|
|
|
if (*s == '%' && linelen > 2)
|
|
|
|
|
{ /* handle escaping */
|
|
|
|
|
s++;
|
|
|
|
|
*d++ = xtoi_2 (s);
|
|
|
|
|
s += 2;
|
|
|
|
|
linelen -= 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
*d++ = *s++;
|
|
|
|
|
}
|
|
|
|
|
*d = 0; /* add a hidden string terminator */
|
|
|
|
|
rc = data_cb (data_cb_arg, line, d - line);
|
|
|
|
|
if (!rc)
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (okay == 3)
|
|
|
|
|
{
|
|
|
|
|
if (!inquire_cb)
|
|
|
|
|
{
|
|
|
|
|
assuan_write_line (ctx, "END"); /* get out of inquire mode */
|
|
|
|
|
_assuan_read_from_server (ctx, &okay, &off); /* dummy read */
|
2006-09-19 14:01:54 +00:00
|
|
|
|
rc = _assuan_error (ASSUAN_No_Inquire_Callback);
|
2001-12-13 15:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rc = inquire_cb (inquire_cb_arg, line);
|
|
|
|
|
if (!rc)
|
|
|
|
|
rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */
|
|
|
|
|
if (!rc)
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-04-24 01:55:58 +00:00
|
|
|
|
else if (okay == 4)
|
|
|
|
|
{
|
|
|
|
|
if (status_cb)
|
|
|
|
|
rc = status_cb (status_cb_arg, line);
|
|
|
|
|
if (!rc)
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
else if (okay == 5)
|
|
|
|
|
{
|
|
|
|
|
if (!data_cb)
|
2006-09-19 14:01:54 +00:00
|
|
|
|
rc = _assuan_error (ASSUAN_No_Data_Callback);
|
2002-04-24 01:55:58 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rc = data_cb (data_cb_arg, NULL, 0);
|
|
|
|
|
if (!rc)
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-12-13 15:04:36 +00:00
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2002-04-24 01:55:58 +00:00
|
|
|
|
|