aboutsummaryrefslogtreecommitdiffstats
path: root/assuan
diff options
context:
space:
mode:
Diffstat (limited to 'assuan')
-rw-r--r--assuan/ChangeLog33
-rw-r--r--assuan/Makefile.am3
-rw-r--r--assuan/assuan-buffer.c27
-rw-r--r--assuan/assuan-defs.h9
-rw-r--r--assuan/assuan-io.c60
-rw-r--r--assuan/assuan-pipe-connect.c69
-rw-r--r--assuan/assuan-socket-connect.c2
-rw-r--r--assuan/assuan-util.c112
-rw-r--r--assuan/assuan.h2
9 files changed, 211 insertions, 106 deletions
diff --git a/assuan/ChangeLog b/assuan/ChangeLog
index 670f7ef75..85dc5ef8b 100644
--- a/assuan/ChangeLog
+++ b/assuan/ChangeLog
@@ -1,3 +1,36 @@
+2002-11-10 Werner Koch <[email protected]>
+
+ * assuan-pipe-connect.c (assuan_pipe_connect): Changed the order
+ of the dups to handle cases where we have already used fd 2 for
+ other things.
+
+2002-10-31 Neal H. Walfield <[email protected]>
+
+ * assuan-util.c: Include <ctype.h>.
+ (_assuan_log_print_buffer): Elide the magic numbers preferring the
+ standard isfoo functions. Use putc_unlocked where possible.
+ (_assuan_log_sanitized_string): Rewrite to use putc_unlocked and
+ the isfoo functions.
+
+2002-09-05 Neal H. Walfield <[email protected]>
+
+ * assuan-defs.h (_assuan_read_wrapper): Depreciated.
+ * assuan-util.c (_assuan_read_wrapper): Removed.
+ * assuan-defs.h (_assuan_write_wrapper): Depreciated.
+ * assuan-util.c (_assuan_write_wrapper): Removed.
+ * assuan.h (assuan_set_io_fun): Depreciated.
+ * assuan-util.c (assuan_set_io_fun): Removed.
+
+ * assuan-defs.h (_assuan_read): New function.
+ (_assuan_write): Likewise.
+ * assuan-io.c: New file.
+
+ * assuan-buffer.c (writen): Use _assuan_write rather than doing
+ the work here.
+ (readline): Likewise for _assuan_read.
+
+ * Makefile.am (libassuan_a_SOURCES): Add assuan-io.c.
+
2002-08-16 Werner Koch <[email protected]>
* assuan.h: Renamed Bad_Certificate_Path to Bad_Certificate_Chain.
diff --git a/assuan/Makefile.am b/assuan/Makefile.am
index 71560c9e3..2207145cf 100644
--- a/assuan/Makefile.am
+++ b/assuan/Makefile.am
@@ -42,7 +42,8 @@ libassuan_a_SOURCES = \
assuan-pipe-server.c \
assuan-socket-server.c \
assuan-pipe-connect.c \
- assuan-socket-connect.c
+ assuan-socket-connect.c \
+ assuan-io.c
assuan-errors.c : assuan.h
diff --git a/assuan/assuan-buffer.c b/assuan/assuan-buffer.c
index df5057543..8017183ea 100644
--- a/assuan/assuan-buffer.c
+++ b/assuan/assuan-buffer.c
@@ -48,9 +48,7 @@ writen ( int fd, const char *buffer, size_t length )
{
while (length)
{
- int nwritten = _assuan_write_wrapper?
- _assuan_write_wrapper (fd, buffer, length):
- write (fd, buffer, length);
+ ssize_t nwritten = _assuan_write (fd, buffer, length);
if (nwritten < 0)
{
@@ -75,9 +73,7 @@ readline (int fd, char *buf, size_t buflen, int *r_nread, int *eof)
*r_nread = 0;
while (nleft > 0)
{
- int n = _assuan_read_wrapper?
- _assuan_read_wrapper (fd, buf, nleft):
- read (fd, buf, nleft);
+ ssize_t n = _assuan_read (fd, buf, nleft);
if (n < 0)
{
@@ -204,13 +200,12 @@ _assuan_read_line (ASSUAN_CONTEXT ctx)
/* Read the next line from the client or server and return a pointer
- to a buffer with holding that line. linelen returns the length of
- the line. This buffer is valid until another read operation is
- done on this buffer. The caller is allowed to modify this buffer.
- He should only use the buffer if the function returns without an
- error.
+ in *LINE to a buffer holding the line. LINELEN is the length of
+ *LINE. The buffer is valid until the next read operation on it.
+ The caller may modify the buffer. The buffer is invalid (i.e. must
+ not be used) if an error is returned.
- Returns: 0 on success or an assuan error code
+ Returns 0 on success or an assuan error code.
See also: assuan_pending_line().
*/
AssuanError
@@ -228,8 +223,8 @@ assuan_read_line (ASSUAN_CONTEXT ctx, char **line, size_t *linelen)
}
-/* Return true when a full line is pending for a read, without the need
- for actual IO */
+/* Return true if a full line is buffered (i.e. an entire line may be
+ read without any I/O). */
int
assuan_pending_line (ASSUAN_CONTEXT ctx)
{
@@ -437,7 +432,3 @@ assuan_send_data (ASSUAN_CONTEXT ctx, const void *buffer, size_t length)
return 0;
}
-
-
-
-
diff --git a/assuan/assuan-defs.h b/assuan/assuan-defs.h
index 3e408b179..f88586925 100644
--- a/assuan/assuan-defs.h
+++ b/assuan/assuan-defs.h
@@ -121,9 +121,6 @@ AssuanError _assuan_read_from_server (ASSUAN_CONTEXT ctx, int *okay, int *off);
/*-- assuan-util.c --*/
-extern ssize_t (*_assuan_read_wrapper)(int,void*,size_t);
-extern ssize_t (*_assuan_write_wrapper)(int,const void*,size_t);
-
void *_assuan_malloc (size_t n);
void *_assuan_calloc (size_t n, size_t m);
void *_assuan_realloc (void *p, size_t n);
@@ -139,6 +136,12 @@ void _assuan_free (void *p);
void _assuan_log_print_buffer (FILE *fp, const void *buffer, size_t length);
void _assuan_log_sanitized_string (const char *string);
+/*-- assuan-io.c --*/
+
+/* Wraps the standard read and write functions to do the Right
+ Thing depending on our linkage. */
+ssize_t _assuan_read (int fd, void *buffer, size_t size);
+ssize_t _assuan_write (int fd, const void *buffer, size_t size);
#endif /*ASSUAN_DEFS_H*/
diff --git a/assuan/assuan-io.c b/assuan/assuan-io.c
new file mode 100644
index 000000000..135cb02d1
--- /dev/null
+++ b/assuan/assuan-io.c
@@ -0,0 +1,60 @@
+/* assuan-buffer.c - Wraps the read and write functions.
+ * Copyright (C) 2002 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <sys/types.h>
+#include <unistd.h>
+
+extern ssize_t pth_read (int fd, void *buffer, size_t size);
+extern ssize_t pth_write (int fd, const void *buffer, size_t size);
+
+#pragma weak pth_read
+#pragma weak pth_write
+
+ssize_t
+_assuan_read (int fd, void *buffer, size_t size)
+{
+ static ssize_t (*reader) (int, void *, size_t);
+
+ if (! reader)
+ {
+ if (pth_read)
+ reader = pth_read;
+ else
+ reader = read;
+ }
+
+ return reader (fd, buffer, size);
+}
+
+ssize_t
+_assuan_write (int fd, const void *buffer, size_t size)
+{
+ static ssize_t (*writer) (int, const void *, size_t);
+
+ if (! writer)
+ {
+ if (pth_write)
+ writer = pth_write;
+ else
+ writer = write;
+ }
+
+ return writer (fd, buffer, size);
+}
diff --git a/assuan/assuan-pipe-connect.c b/assuan/assuan-pipe-connect.c
index 0cb48ca1a..d7595c9f3 100644
--- a/assuan/assuan-pipe-connect.c
+++ b/assuan/assuan-pipe-connect.c
@@ -172,25 +172,23 @@ assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[],
char errbuf[512];
int *fdp;
- /* Close all files which will not be duped and are not in the
- fd_child_list. */
- n = sysconf (_SC_OPEN_MAX);
- if (n < 0)
- n = MAX_OPEN_FDS;
- for (i=0; i < n; i++)
+ /* Dup handles to stdin/stdout. */
+ if (rp[1] != STDOUT_FILENO)
{
- fdp = fd_child_list;
- if (fdp)
- {
- while (*fdp != -1 && *fdp != i)
- fdp++;
- }
-
- if (!(fdp && *fdp != -1)
- && i != rp[1] && i != wp[0])
- close(i);
+ if (dup2 (rp[1], STDOUT_FILENO) == -1)
+ {
+ LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno));
+ _exit (4);
+ }
+ }
+ if (wp[0] != STDIN_FILENO)
+ {
+ if (dup2 (wp[0], STDIN_FILENO) == -1)
+ {
+ LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno));
+ _exit (4);
+ }
}
- errno = 0;
/* Dup stderr to /dev/null unless it is in the list of FDs to be
passed to the child. */
@@ -213,28 +211,29 @@ assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[],
LOGERROR1 ("dup2(dev/null, 2) failed: %s\n", strerror (errno));
_exit (4);
}
- close (fd);
}
- /* Dup handles and to stdin/stdout and exec. */
- if (rp[1] != STDOUT_FILENO)
- {
- 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)
+
+ /* Close all files which will not be duped and are not in the
+ fd_child_list. */
+ n = sysconf (_SC_OPEN_MAX);
+ if (n < 0)
+ n = MAX_OPEN_FDS;
+ for (i=0; i < n; i++)
{
- if (dup2 (wp[0], STDIN_FILENO) == -1)
- {
- LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno));
- _exit (4);
- }
- close (wp[0]);
+ if ( i == STDIN_FILENO || i == STDOUT_FILENO || i == STDERR_FILENO)
+ continue;
+ fdp = fd_child_list;
+ if (fdp)
+ {
+ while (*fdp != -1 && *fdp != i)
+ fdp++;
+ }
+
+ if (!(fdp && *fdp != -1))
+ close(i);
}
+ errno = 0;
execv (name, argv);
/* oops - use the pipe to tell the parent about it */
diff --git a/assuan/assuan-socket-connect.c b/assuan/assuan-socket-connect.c
index 53f4a02d0..64a22bf54 100644
--- a/assuan/assuan-socket-connect.c
+++ b/assuan/assuan-socket-connect.c
@@ -66,7 +66,7 @@ do_deinit (ASSUAN_CONTEXT ctx)
/* Make a connection to the Unix domain socket NAME and return a new
Assuan context in CTX. SERVER_PID is currently not used but may
- becode handy in future. */
+ become handy in the future. */
AssuanError
assuan_socket_connect (ASSUAN_CONTEXT *r_ctx,
const char *name, pid_t server_pid)
diff --git a/assuan/assuan-util.c b/assuan/assuan-util.c
index a335b09f7..76f7f0650 100644
--- a/assuan/assuan-util.c
+++ b/assuan/assuan-util.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#include "assuan-defs.h"
@@ -29,16 +30,10 @@
#include "../jnlib/logging.h"
#endif
-ssize_t (*_assuan_read_wrapper)(int,void*,size_t) = NULL;
-ssize_t (*_assuan_write_wrapper)(int,const void*,size_t) = NULL;
-
-
static void *(*alloc_func)(size_t n) = malloc;
static void *(*realloc_func)(void *p, size_t n) = realloc;
static void (*free_func)(void*) = free;
-
-
void
assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n),
void *(*new_realloc_func)(void *p, size_t n),
@@ -77,18 +72,6 @@ _assuan_free (void *p)
free_func (p);
}
-/* For use with Pth it is required to have special read and write
- functions. We can't assume an ELF based system so we have to
- explicitly set them if we are going to use Pth. */
-void
-assuan_set_io_func (ssize_t (*r)(int,void*,size_t),
- ssize_t (*w)(int,const void*,size_t))
-{
- _assuan_read_wrapper = r;
- _assuan_write_wrapper = w;
-}
-
-
/* Store the error in the context so that the error sending function
can take out a descriptive text. Inside the assuan code, use the
@@ -145,6 +128,8 @@ assuan_end_confidential (ASSUAN_CONTEXT ctx)
}
}
+/* Dump a possibly binary string (used for debugging). Distinguish
+ ascii text from binary and print it accordingly. */
void
_assuan_log_print_buffer (FILE *fp, const void *buffer, size_t length)
{
@@ -152,26 +137,31 @@ _assuan_log_print_buffer (FILE *fp, const void *buffer, size_t length)
int n;
for (n=length,s=buffer; n; n--, s++)
- {
- if (*s < ' ' || (*s >= 0x7f && *s <= 0xa0))
- break;
- }
+ if (!isascii (*s) || iscntrl (*s) || !isprint (*s))
+ break;
+
s = buffer;
if (!n && *s != '[')
fwrite (buffer, length, 1, fp);
else
{
- putc ('[', fp);
+#ifdef HAVE_FLOCKFILE
+ flockfile (fp);
+#endif
+ putc_unlocked ('[', fp);
for (n=0; n < length; n++, s++)
fprintf (fp, " %02x", *s);
- putc (' ', fp);
- putc (']', fp);
+ putc_unlocked (' ', fp);
+ putc_unlocked (']', fp);
+#ifdef HAVE_FUNLOCKFILE
+ funlockfile (fp);
+#endif
}
}
-/* print a user supplied string after filtering out potential bad
- characters*/
+/* Log a user supplied string. Escapes non-printable before
+ printing. */
void
_assuan_log_sanitized_string (const char *string)
{
@@ -182,29 +172,59 @@ _assuan_log_sanitized_string (const char *string)
FILE *fp = stderr;
#endif
+ if (! *s)
+ return;
+
+#ifdef HAVE_FLOCKFILE
+ flockfile (fp);
+#endif
+
for (; *s; s++)
{
- if (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0))
- {
- putc ('\\', fp);
- if (*s == '\n')
- putc ('n', fp);
- else if (*s == '\r')
- putc ('r', fp);
- else if (*s == '\f')
- putc ('f', fp);
- else if (*s == '\v')
- putc ('v', fp);
- else if (*s == '\b')
- putc ('b', fp);
- else if (!*s)
- putc ('0', fp);
- else
- fprintf (fp, "x%02x", *s );
+ int c = 0;
+
+ switch (*s)
+ {
+ case '\r':
+ c = 'r';
+ break;
+
+ case '\n':
+ c = 'n';
+ break;
+
+ case '\f':
+ c = 'f';
+ break;
+
+ case '\v':
+ c = 'v';
+ break;
+
+ case '\b':
+ c = 'b';
+ break;
+
+ default:
+ if (isascii (*s) && isprint (*s))
+ putc_unlocked (*s, fp);
+ else
+ {
+ putc_unlocked ('\\', fp);
+ fprintf (fp, "x%02x", *s);
+ }
+ }
+
+ if (c)
+ {
+ putc_unlocked ('\\', fp);
+ putc_unlocked (c, fp);
}
- else
- putc (*s, fp);
}
+
+#ifdef HAVE_FUNLOCKFILE
+ funlockfile (fp);
+#endif
}
diff --git a/assuan/assuan.h b/assuan/assuan.h
index 51f648a3a..d8b874eb6 100644
--- a/assuan/assuan.h
+++ b/assuan/assuan.h
@@ -214,8 +214,6 @@ AssuanError assuan_send_data (ASSUAN_CONTEXT ctx,
void assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n),
void *(*new_realloc_func)(void *p, size_t n),
void (*new_free_func)(void*) );
-void assuan_set_io_func (ssize_t (*r)(int,void*,size_t),
- ssize_t (*w)(int,const void*,size_t));
void assuan_set_log_stream (ASSUAN_CONTEXT ctx, FILE *fp);
int assuan_set_error (ASSUAN_CONTEXT ctx, int err, const char *text);
void assuan_set_pointer (ASSUAN_CONTEXT ctx, void *pointer);