diff --git a/assuan/ChangeLog b/assuan/ChangeLog index dc7403a4a..28f1b2f4f 100644 --- a/assuan/ChangeLog +++ b/assuan/ChangeLog @@ -1,3 +1,11 @@ +2001-12-04 Werner Koch + + * assuan-connect.c (assuan_pipe_connect): Add more error reporting. + * assuan-client.c: New. + + * assuan-inquire.c: New. + * assuan-handler.c (process_request): Check for nested invocations. + 2001-11-27 Werner Koch * assuan-handler.c (assuan_register_input_notify): New. diff --git a/assuan/Makefile.am b/assuan/Makefile.am index 3c576900b..bc14e8f60 100644 --- a/assuan/Makefile.am +++ b/assuan/Makefile.am @@ -34,8 +34,10 @@ libassuan_a_SOURCES = \ assuan-errors.c \ assuan-buffer.c \ assuan-handler.c \ + assuan-inquire.c \ assuan-listen.c \ assuan-connect.c \ + assuan-client.c \ assuan-pipe-server.c diff --git a/assuan/assuan-buffer.c b/assuan/assuan-buffer.c index f3fe2b188..eec4876f8 100644 --- a/assuan/assuan-buffer.c +++ b/assuan/assuan-buffer.c @@ -265,5 +265,50 @@ _assuan_cookie_write_flush (void *cookie) } +/** + * assuan_send_data: + * @ctx: An assuan context + * @buffer: Data to send or NULL to flush + * @length: length of the data to send/ + * + * This function may be used by the server or the client to send data + * lines. The data will be escaped as required by the Assuan protocol + * and may get buffered until a line is full. To force sending the + * data out @buffer may be passed as NULL (in which case @length must + * also be 0); however when used by a client this flush operation does + * also send the terminating "END" command to terminate the reponse on + * a INQUIRE response. However, when assuan_transact() is used, this + * function takes care of sending END itself. + * + * Return value: 0 on success or an error code + **/ + +AssuanError +assuan_send_data (ASSUAN_CONTEXT ctx, const void *buffer, size_t length) +{ + if (!ctx) + return ASSUAN_Invalid_Value; + if (!buffer && length) + return ASSUAN_Invalid_Value; + + if (!buffer) + { /* flush what we have */ + _assuan_cookie_write_flush (ctx); + if (ctx->outbound.data.error) + return ctx->outbound.data.error; + if (!ctx->is_server) + return _assuan_write_line (ctx, "END"); + } + else + { + _assuan_cookie_write_data (ctx, buffer, length); + if (ctx->outbound.data.error) + return ctx->outbound.data.error; + } + + return 0; +} + + diff --git a/assuan/assuan-client.c b/assuan/assuan-client.c new file mode 100644 index 000000000..03c7c099a --- /dev/null +++ b/assuan/assuan-client.c @@ -0,0 +1,180 @@ +/* assuan-client.c - client functions + * Copyright (C) 2001 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include +#include +#include +#include +#include +#include + +#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)) + + +static AssuanError +read_from_server (ASSUAN_CONTEXT ctx, int *okay, int *off) +{ + char *line; + int linelen; + AssuanError rc; + + *okay = 0; + *off = 0; + do + { + rc = _assuan_read_line (ctx); + if (rc) + return rc; + line = ctx->inbound.line; + linelen = ctx->inbound.linelen; + } + while (*line == '#' || !linelen); + + if (linelen >= 1 + && line[0] == 'D' && line[1] == ' ') + { + *okay = 2; /* data line */ + *off = 2; + } + else if (linelen >= 2 + && line[0] == 'O' && line[1] == 'K' + && (line[2] == '\0' || line[2] == ' ')) + { + *okay = 1; + *off = 2; + } + else if (linelen >= 3 + && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' + && (line[3] == '\0' || line[3] == ' ')) + { + *okay = 0; + *off = 3; + } + 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; + } + else + rc = ASSUAN_Invalid_Response; + return rc; +} + + + +/** + * assuan_transact: + * @ctx: The Assuan context + * @command: Coimmand line to be send to server + * @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 + * + * 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 + * callback functions. + **/ +AssuanError +assuan_transact (ASSUAN_CONTEXT ctx, + const char *command, + AssuanError (*data_cb)(void *, const void *, size_t), + void *data_cb_arg, + AssuanError (*inquire_cb)(void*, const char *), + void *inquire_cb_arg) +{ + int rc, okay, off; + unsigned char *line; + int linelen; + + rc = _assuan_write_line (ctx, command); + if (rc) + return rc; + + again: + rc = 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) + { + rc = atoi (line); + if (rc < 100) + rc = ASSUAN_Server_Fault; + } + else if (okay == 2) + { + if (!data_cb) + rc = ASSUAN_No_Data_Callback; + else + { + unsigned char *s, *d; + + 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 */ + read_from_server (ctx, &okay, &off); /* dummy read the response */ + rc = ASSUAN_No_Inquire_Callback; + } + 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; + } + } + + return rc; +} diff --git a/assuan/assuan-connect.c b/assuan/assuan-connect.c index 37d426255..abc5d74e2 100644 --- a/assuan/assuan-connect.c +++ b/assuan/assuan-connect.c @@ -18,17 +18,56 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ +#ifdef HAVE_CONFIG_H #include +#endif + #include #include #include #include #include +#include #include #include #include "assuan-defs.h" +#ifdef _POSIX_OPEN_MAX +#define MAX_OPEN_FDS _POSIX_OPEN_MAX +#else +#define MAX_OPEN_FDS 20 +#endif + +#ifdef HAVE_JNLIB_LOGGING +#define LOGERROR1(a,b) log_error ((a), (b)) +#else +#define LOGERROR1(a,b) fprintf (stderr, (a), (b)) +#endif + + + +static int +writen ( int fd, const char *buffer, size_t length ) +{ + while (length) + { + int nwritten = write (fd, buffer, length); + + if (nwritten < 0) + { + if (errno == EINTR) + continue; + return -1; /* write error */ + } + length -= nwritten; + buffer += nwritten; + } + return 0; /* okay */ +} + + + /* Connect to a server over a pipe, creating the assuan context and returning it in CTX. The server filename is NAME, the argument vector in ARGV. */ @@ -41,8 +80,8 @@ assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[]) int wp[2]; int fd[2]; - if (!name || !argv || !argv[0]) - return ASSUAN_General_Error; + if (!ctx || !name || !argv || !argv[0]) + return ASSUAN_Invalid_Value; if (!fixed_signals) { @@ -69,7 +108,7 @@ assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[]) close (rp[1]); return ASSUAN_General_Error; } - + fd[0] = rp[0]; /* Our inbound is read end of read pipe. */ fd[1] = wp[1]; /* Our outbound is write end of write pipe. */ @@ -82,6 +121,7 @@ assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[]) close (wp[1]); return err; } + (*ctx)->is_server = 0; (*ctx)->pid = fork (); if ((*ctx)->pid < 0) @@ -96,25 +136,80 @@ assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[]) if ((*ctx)->pid == 0) { - close (rp[0]); - close (wp[1]); + int i, n; + char errbuf[512]; +#ifdef HAVE_JNLIB_LOGGING + int log_fd = log_get_fd (); +#endif + /* close all files which will not be duped but keep stderr + and log_stream for now */ + n = sysconf (_SC_OPEN_MAX); + if (n < 0) + n = MAX_OPEN_FDS; + for (i=0; i < n; i++) + { + if (i != fileno (stderr) +#ifdef HAVE_JNLIB_LOGGING + && i != log_fd +#endif + && i != rp[1] && i != wp[0]) + close(i); + } + errno = 0; + + /* Dup handles and to stdin/stdout and exec */ if (rp[1] != STDOUT_FILENO) - { - dup2 (rp[1], STDOUT_FILENO); /* Child's outbound is write end of read pipe. */ - close (rp[1]); - } + { + if (dup2 (rp[1], STDOUT_FILENO) == -1) + { + LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno)); + _exit (4); + } + close (rp[1]); + } if (wp[0] != STDIN_FILENO) - { - dup2 (wp[0], STDIN_FILENO); /* Child's inbound is read end of write pipe. */ - close (wp[0]); - } - execv (name, argv); - _exit (1); + { + if (dup2 (wp[0], STDIN_FILENO) == -1) + { + LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno)); + _exit (4); + } + close (wp[0]); + } + + execv (name, argv); + /* oops - use the pipe to tell the parent about it */ + snprintf (errbuf, sizeof(errbuf)-1, "ERR %d can't exec `%s': %.50s\n", + ASSUAN_Problem_Starting_Server, name, strerror (errno)); + errbuf[sizeof(errbuf)-1] = 0; + writen (1, errbuf, strlen (errbuf)); + _exit (4); } close (rp[1]); close (wp[0]); _assuan_read_line (*ctx); /* FIXME: Handshake. */ + +#if 0 /* old stuff */ + inbound.eof = 0; + inbound.linelen = 0; + inbound.attic.linelen = 0; + + /* The server is available - read the greeting */ + rc = read_from_agent (&okay); + if (rc) + { + log_error ("can't connect to the agent: %s\n", gnupg_strerror (rc)); + } + else if (!okay) + { + log_error ("can't connect to the agent: %s\n", inbound.line); + rc = seterr (No_Agent); + } + else +#endif + + return 0; } @@ -133,3 +228,5 @@ assuan_get_pid (ASSUAN_CONTEXT ctx) { return ctx ? ctx->pid : -1; } + + diff --git a/assuan/assuan-defs.h b/assuan/assuan-defs.h index 2b64bf1cb..5d61707a5 100644 --- a/assuan/assuan-defs.h +++ b/assuan/assuan-defs.h @@ -35,6 +35,9 @@ struct cmdtbl_s { struct assuan_context_s { AssuanError err_no; const char *err_str; + + int is_server; /* set if this is context belongs to a server */ + int in_inquire; void *user_pointer; /* for assuan_[gs]et_pointer () */ diff --git a/assuan/assuan-handler.c b/assuan/assuan-handler.c index 13f1f9d3e..aee2407b2 100644 --- a/assuan/assuan-handler.c +++ b/assuan/assuan-handler.c @@ -353,6 +353,9 @@ process_request (ASSUAN_CONTEXT ctx) { int rc; + if (ctx->in_inquire) + return ASSUAN_Nested_Commands; + rc = _assuan_read_line (ctx); if (rc) return rc; diff --git a/assuan/assuan-inquire.c b/assuan/assuan-inquire.c new file mode 100644 index 000000000..c4514dfcc --- /dev/null +++ b/assuan/assuan-inquire.c @@ -0,0 +1,220 @@ +/* assuan-inquire.c - handle inquire stuff + * Copyright (C) 2001 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include +#include +#include +#include + +#include "assuan-defs.h" + +#define digitp(a) ((a) >= '0' && (a) <= '9') +#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)) + + +struct membuf { + size_t len; + size_t size; + char *buf; + int out_of_core; + int too_large; + size_t maxlen; +}; + + + +/* A simple implemnation of a dynamic buffer. Use init_membuf() to + create a buffer, put_membuf to append bytes and get_membuf to + release and return the buffer. Allocation errors are detected but + only returned at the final get_membuf(), this helps not to clutter + the code with out of core checks. */ + +static void +init_membuf (struct membuf *mb, int initiallen, size_t maxlen) +{ + mb->len = 0; + mb->size = initiallen; + mb->out_of_core = 0; + mb->too_large = 0; + mb->maxlen = maxlen; + mb->buf = xtrymalloc (initiallen); + if (!mb->buf) + mb->out_of_core = 1; +} + +static void +put_membuf (struct membuf *mb, const void *buf, size_t len) +{ + if (mb->out_of_core || mb->too_large) + return; + + if (mb->maxlen && mb->len + len > mb->maxlen) + { + mb->too_large = 1; + return; + } + + if (mb->len + len >= mb->size) + { + char *p; + + mb->size += len + 1024; + p = xtryrealloc (mb->buf, mb->size); + if (!p) + { + mb->out_of_core = 1; + return; + } + mb->buf = p; + } + memcpy (mb->buf + mb->len, buf, len); + mb->len += len; +} + +static void * +get_membuf (struct membuf *mb, size_t *len) +{ + char *p; + + if (mb->out_of_core || mb->too_large) + { + xfree (mb->buf); + mb->buf = NULL; + return NULL; + } + + p = mb->buf; + *len = mb->len; + mb->buf = NULL; + mb->out_of_core = 1; /* don't allow a reuse */ + return p; +} + +static void +free_membuf (struct membuf *mb) +{ + xfree (mb->buf); + mb->buf = NULL; +} + + +/** + * assuan_inquire: + * @ctx: An assuan context + * @keyword: The keyword used for the inquire + * @r_buffer: Returns an allocated buffer + * @r_length: Returns the length of this buffer + * @maxlen: If no 0, the size limit of the inquired data. + * + * A Server may use this to Send an inquire + * + * Return value: 0 on success or an ASSUAN error code + **/ +AssuanError +assuan_inquire (ASSUAN_CONTEXT ctx, const char *keyword, + char **r_buffer, size_t *r_length, size_t maxlen) +{ + AssuanError rc; + struct membuf mb; + char cmdbuf[100]; + unsigned char *line, *p; + int linelen; + + if (!ctx || !keyword || (10 + strlen (keyword) >= sizeof (cmdbuf)) + || !r_buffer || !r_length ) + return ASSUAN_Invalid_Value; + if (!ctx->is_server) + return ASSUAN_Not_A_Server; + if (ctx->in_inquire) + return ASSUAN_Nested_Commands; + + ctx->in_inquire = 1; + init_membuf (&mb, maxlen? maxlen:1024, maxlen); + + strcpy (stpcpy (cmdbuf, "INQUIRE "), keyword); + rc = _assuan_write_line (ctx, cmdbuf); + if (rc) + goto leave; + + for (;;) + { + do + { + rc = _assuan_read_line (ctx); + if (rc) + goto leave; + line = ctx->inbound.line; + linelen = ctx->inbound.linelen; + } + while (*line == '#' || !linelen); + if (line[0] == 'E' && line[1] == 'N' && line[2] == 'D' + && (!line[3] || line[3] == ' ')) + break; /* END command received*/ + if (line[0] != 'D' || line[1] != ' ') + { + rc = ASSUAN_Unexpected_Command; + goto leave; + } + if (linelen < 3) + continue; + line += 2; + linelen -= 2; + + p = line; + while (linelen) + { + for (;linelen && *p != '%'; linelen--, p++) + ; + put_membuf (&mb, line, p-line); + if (linelen > 2) + { /* handle escaping */ + unsigned char tmp[1]; + p++; + *tmp = xtoi_2 (p); + p += 2; + linelen -= 3; + put_membuf (&mb, tmp, 1); + } + line = p; + } + if (mb.too_large) + { + rc = ASSUAN_Too_Much_Data; + goto leave; + } + } + + *r_buffer = get_membuf (&mb, r_length); + if (!*r_buffer) + rc = ASSUAN_Out_Of_Core; + + leave: + free_membuf (&mb); + ctx->in_inquire = 0; + return rc; +} + + + + + + diff --git a/assuan/assuan-pipe-server.c b/assuan/assuan-pipe-server.c index 018d05df4..58d9aa142 100644 --- a/assuan/assuan-pipe-server.c +++ b/assuan/assuan-pipe-server.c @@ -35,6 +35,7 @@ assuan_init_pipe_server (ASSUAN_CONTEXT *r_ctx, int filedes[2]) ctx = xtrycalloc (1, sizeof *ctx); if (!ctx) return ASSUAN_Out_Of_Core; + ctx->is_server = 1; ctx->input_fd = -1; ctx->output_fd = -1; diff --git a/assuan/assuan.h b/assuan/assuan.h index 10eb3a531..cebae6673 100644 --- a/assuan/assuan.h +++ b/assuan/assuan.h @@ -40,6 +40,12 @@ typedef enum { ASSUAN_Read_Error = 5, ASSUAN_Write_Error = 6, ASSUAN_Problem_Starting_Server = 7, + ASSUAN_Not_A_Server = 8, + ASSUAN_Not_A_Client = 9, + ASSUAN_Nested_Commands = 10, + ASSUAN_Invalid_Response = 11, + ASSUAN_No_Data_Callback = 12, + ASSUAN_No_Inquire_Callback = 13, /* error codes above 99 are meant as status codes */ ASSUAN_Not_Implemented = 100, @@ -59,6 +65,9 @@ typedef enum { ASSUAN_Server_IO_Error = 114, ASSUAN_Server_Bug = 115, ASSUAN_No_Data_Available = 116, + ASSUAN_Invalid_Data = 117, + ASSUAN_Unexpected_Command = 118, + ASSUAN_Too_Much_Data = 119, ASSUAN_Bad_Certificate = 201, ASSUAN_Bad_Certificate_Path = 202, @@ -71,7 +80,7 @@ typedef enum { ASSUAN_Cert_Revoked = 301, ASSUAN_No_CRL_For_Cert = 302, - ASSUNA_CRL_Too_Old = 303, + ASSUAN_CRL_Too_Old = 303, } AssuanError; @@ -136,6 +145,25 @@ AssuanError assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, void assuan_pipe_disconnect (ASSUAN_CONTEXT ctx); pid_t assuan_get_pid (ASSUAN_CONTEXT ctx); +/*-- assuan-client.c --*/ +AssuanError +assuan_transact (ASSUAN_CONTEXT ctx, + const char *command, + AssuanError (*data_cb)(void *, const void *, size_t), + void *data_cb_arg, + AssuanError (*inquire_cb)(void*, const char *), + void *inquire_cb_arg); + + +/*-- assuan-inquire.c --*/ +AssuanError assuan_inquire (ASSUAN_CONTEXT ctx, const char *keyword, + char **r_buffer, size_t *r_length, size_t maxlen); + +/*-- assuan-buffer.c --*/ +AssuanError assuan_send_data (ASSUAN_CONTEXT ctx, + const void *buffer, size_t length); + + /*-- assuan-util.c --*/ void assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n), void *(*new_realloc_func)(void *p, size_t n), @@ -153,3 +181,5 @@ const char *assuan_strerror (AssuanError err); } #endif #endif /*ASSUAN_H*/ + +