diff --git a/configure.ac b/configure.ac index b5c2cc7..a46e933 100644 --- a/configure.ac +++ b/configure.ac @@ -1,456 +1,464 @@ # configure.ac - for libassuan # Copyright (C) 2001, 2002, 2003, 2006, 2007, 2009, # 2011, 2012, 2013 Free Software Foundation, Inc. # Copyright (C) 2013, 2014, 2015 g10 Code GmbH # # 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, see . # SPDX-License-Identifier: LGPL-2.1+ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) min_automake_version="1.14" # To build a release you need to create a tag with the version number # (git tag -s libassuan-n.m.k) and run "./autogen.sh --force". Please # bump the version number immediately after the release and do another # commit and push so that the git magic is able to work. See below # for the LT versions. m4_define([mym4_package],[libassuan]) m4_define([mym4_major], [2]) m4_define([mym4_minor], [5]) m4_define([mym4_micro], [6]) # To start a new development series, i.e a new major or minor number # you need to mark an arbitrary commit before the first beta release # with an annotated tag. For example a 2.1 branch starts off with # the tag "foo-2.1-base". This is used as the base for counting # beta numbers before the first release of a series. # Below is m4 magic to extract and compute the git revision number, # the decimalized short revision number, a beta version string and a # flag indicating a development version (mym4_isbeta). Note that the # m4 processing is done by autoconf and not during the configure run. m4_define([mym4_verslist], m4_split(m4_esyscmd([./autogen.sh --find-version] \ mym4_package mym4_major mym4_minor mym4_micro),[:])) m4_define([mym4_isbeta], m4_argn(2, mym4_verslist)) m4_define([mym4_version], m4_argn(4, mym4_verslist)) m4_define([mym4_revision], m4_argn(7, mym4_verslist)) m4_define([mym4_revision_dec], m4_argn(8, mym4_verslist)) m4_esyscmd([echo ]mym4_version[>VERSION]) AC_INIT([mym4_package],[mym4_version],[https://bugs.gnupg.org]) # LT Version numbers, remember to change them just *before* a release. # (Code changed: REVISION++) # (Interfaces added/removed/changed: CURRENT++, REVISION=0) # (Interfaces added: AGE++) # (Interfaces removed/changed: AGE=0) # LIBASSUAN_LT_CURRENT=8 LIBASSUAN_LT_AGE=8 LIBASSUAN_LT_REVISION=5 # If the API is changed in an incompatible way: increment the next counter. LIBASSUAN_CONFIG_API_VERSION=2 ############################################## AC_SUBST(LIBASSUAN_LT_CURRENT) AC_SUBST(LIBASSUAN_LT_AGE) AC_SUBST(LIBASSUAN_LT_REVISION) AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE([serial-tests dist-bzip2 no-dist-gzip]) AM_MAINTAINER_MODE AC_CONFIG_SRCDIR(src/assuan.h.in) AC_CONFIG_MACRO_DIR(m4) AC_CONFIG_HEADERS([config.h]) AC_CANONICAL_HOST AM_SILENT_RULES AB_INIT AC_USE_SYSTEM_EXTENSIONS # Taken from mpfr-4.0.1, then modified for LDADD_FOR_TESTS_KLUDGE dnl Under Linux, make sure that the old dtags are used if LD_LIBRARY_PATH dnl is defined. The issue is that with the new dtags, LD_LIBRARY_PATH has dnl the precedence over the run path, so that if a compatible MPFR library dnl is installed in some directory from $LD_LIBRARY_PATH, then the tested dnl MPFR library will be this library instead of the MPFR library from the dnl build tree. Other OS with the same issue might be added later. dnl dnl References: dnl https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=859732 dnl http://lists.gnu.org/archive/html/libtool/2017-05/msg00000.html dnl dnl We need to check whether --disable-new-dtags is supported as alternate dnl linkers may be used (e.g., with tcc: CC=tcc LD=tcc). dnl case $host in *-*-linux*) if test -n "$LD_LIBRARY_PATH"; then saved_LDFLAGS="$LDFLAGS" LDADD_FOR_TESTS_KLUDGE="-Wl,--disable-new-dtags" LDFLAGS="$LDFLAGS $LDADD_FOR_TESTS_KLUDGE" AC_MSG_CHECKING(whether --disable-new-dtags is supported by the linker) AC_LINK_IFELSE([AC_LANG_SOURCE([[ int main (void) { return 0; } ]])], [AC_MSG_RESULT(yes (use it since LD_LIBRARY_PATH is set))], [AC_MSG_RESULT(no) LDADD_FOR_TESTS_KLUDGE="" ]) LDFLAGS="$saved_LDFLAGS" fi ;; esac AC_SUBST([LDADD_FOR_TESTS_KLUDGE]) VERSION_NUMBER=m4_esyscmd(printf "0x%02x%02x%02x" mym4_major \ mym4_minor mym4_micro) AC_SUBST(VERSION_NUMBER) # Don't default to build static libs. LT_PREREQ([2.2.6]) LT_INIT([win32-dll disable-static]) LT_LANG([Windows Resource]) # For now we hardcode the use of version scripts. It would be better # to write a test for this or even implement this within libtool. have_ld_version_script=no -check_descriptor_passing=yes case "${host}" in - *-*-cygwin*) - check_descriptor_passing=no - ;; *-*-linux*) have_ld_version_script=yes ;; *-*-gnu*) have_ld_version_script=yes ;; *-apple-darwin*) AC_DEFINE(_XOPEN_SOURCE, 500, Activate POSIX interface on MacOS X) AC_DEFINE(_DARWIN_C_SOURCE, 1, Expose all libc features (__DARWIN_C_FULL)) ;; esac AM_CONDITIONAL(HAVE_LD_VERSION_SCRIPT, test "$have_ld_version_script" = "yes") AH_TOP([ #ifndef _ASSUAN_CONFIG_H_INCLUDED #define _ASSUAN_CONFIG_H_INCLUDED /* Provide the es_ macro for estream. */ #define GPGRT_ENABLE_ES_MACROS 1 ]) AH_BOTTOM([ #endif /*_ASSUAN_CONFIG_H_INCLUDED*/ ]) # Checks for programs. missing_dir=`cd $ac_aux_dir && pwd` AM_MISSING_PROG(ACLOCAL, aclocal, $missing_dir) AM_MISSING_PROG(AUTOCONF, autoconf, $missing_dir) AM_MISSING_PROG(AUTOMAKE, automake, $missing_dir) AM_MISSING_PROG(AUTOHEADER, autoheader, $missing_dir) AM_MISSING_PROG(MAKEINFO, makeinfo, $missing_dir) AC_PROG_AWK AC_PROG_CC AC_PROG_CPP AM_PROG_CC_C_O if test "x$ac_cv_prog_cc_c89" = "xno" ; then AC_MSG_ERROR([[No C-89 compiler found]]) fi AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET #AC_ARG_PROGRAM # We need to compile and run a program on the build machine. AX_CC_FOR_BUILD if test "$GCC" = yes; then CFLAGS="$CFLAGS -Wall -Wcast-align -Wshadow -Wstrict-prototypes" AC_MSG_CHECKING([if gcc supports -Wpointer-arith]) _gcc_cflags_save=$CFLAGS CFLAGS="-Wpointer-arith" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],_gcc_wopt=yes,_gcc_wopt=no) AC_MSG_RESULT($_gcc_wopt) CFLAGS=$_gcc_cflags_save; if test x"$_gcc_wopt" = xyes ; then CFLAGS="$CFLAGS -Wpointer-arith" fi fi # # Options depending on the host OS. # have_dosish_system=no have_w32_system=no have_w64_system=no case "${host}" in *-linux*) if test "$GCC" = yes; then CFLAGS="$CFLAGS -fPIC -DPIC" fi ;; x86_64-*mingw32*) have_dosish_system=yes have_w32_system=yes have_w64_system=yes ;; *-mingw32*) have_dosish_system=yes have_w32_system=yes ;; *-solaris*) AC_DEFINE(_XOPEN_SOURCE, 500, Activate extensions on Solaris) AC_DEFINE(_XOPEN_SOURCE_EXTENDED, 1, Activate extensions on Solaris) AC_DEFINE(__EXTENSIONS__, 1, Activate extensions on Solaris) ;; esac if test "$have_dosish_system" = yes; then AC_DEFINE(HAVE_DOSISH_SYSTEM,1, [Defined if we run on some of the PCDOS like systems (DOS, Windoze. OS/2) with special properties like no file modes]) fi if test "$have_w32_system" = yes; then AC_DEFINE(HAVE_W32_SYSTEM,1,[Defined if we run on a W32 API based system]) if test "$have_w64_system" = yes; then AC_DEFINE(HAVE_W64_SYSTEM,1, [Defined if we run on a 64 bit W32 API based system]) fi AC_CHECK_HEADERS([winsock2.h]) fi AM_CONDITIONAL(HAVE_W32_SYSTEM, test "$have_w32_system" = yes) AM_CONDITIONAL(HAVE_W64_SYSTEM, test "$have_w64_system" = yes) # # Provide information about the build. # BUILD_REVISION="mym4_revision" AC_SUBST(BUILD_REVISION) AC_DEFINE_UNQUOTED(BUILD_REVISION, "$BUILD_REVISION", [GIT commit id revision used to build this package]) changequote(,)dnl BUILD_FILEVERSION=`echo "$VERSION" | sed 's/\([0-9.]*\).*/\1./;s/\./,/g'` changequote([,])dnl BUILD_FILEVERSION="${BUILD_FILEVERSION}mym4_revision_dec" AC_SUBST(BUILD_FILEVERSION) AC_ARG_ENABLE([build-timestamp], AS_HELP_STRING([--enable-build-timestamp], [set an explicit build timestamp for reproducibility. (default is the current time in ISO-8601 format)]), [if test "$enableval" = "yes"; then BUILD_TIMESTAMP=`date -u +%Y-%m-%dT%H:%M+0000 2>/dev/null || date` else BUILD_TIMESTAMP="$enableval" fi], [BUILD_TIMESTAMP=""]) AC_SUBST(BUILD_TIMESTAMP) AC_DEFINE_UNQUOTED(BUILD_TIMESTAMP, "$BUILD_TIMESTAMP", [The time this package was configured for a build]) # # Check for network libraries. They are needed for tests. # AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt, [NETLIBS="-lsocket $NETLIBS"])) AC_SUBST(NETLIBS) if test "$have_w32_system" = yes; then # FIXME: Check why we need to use ws2_32 and document that. NETLIBS="-lws2_32 $NETLIBS" fi # # Provide info for src/libassuan-config.in # LIBASSUAN_CONFIG_LIBS="-lassuan" LIBASSUAN_CONFIG_CFLAGS="" LIBASSUAN_CONFIG_HOST="$host" AC_SUBST(LIBASSUAN_CONFIG_LIBS) AC_SUBST(LIBASSUAN_CONFIG_CFLAGS) AC_SUBST(LIBASSUAN_CONFIG_HOST) AC_SUBST(LIBASSUAN_CONFIG_API_VERSION) # # Checks for header files. # AC_CHECK_HEADERS([locale.h sys/uio.h stdint.h inttypes.h \ sys/types.h sys/stat.h unistd.h sys/time.h fcntl.h \ sys/select.h ucred.h sys/ucred.h]) AC_TYPE_UINTPTR_T AC_TYPE_UINT16_T # # Checks for typedefs, structures, and compiler characteristics. # AC_C_CONST AC_C_INLINE AC_TYPE_SIZE_T gl_TYPE_SOCKLEN_T +# Windows has different method for "descriptor passing" (actually, +# it's file handle passing), so, don't check the feature. +check_descriptor_passing=yes +case "${host}" in + *-*-cygwin*) + check_descriptor_passing=no + ;; + *-mingw32*) + check_descriptor_passing=no + ;; +esac + if test $check_descriptor_passing != yes; then use_descriptor_passing=no else AC_CHECK_MEMBER(struct cmsghdr.cmsg_len, [use_descriptor_passing=yes], [use_descriptor_passing=no AC_MSG_WARN([ *** *** Data structure for sending ancillary data missing. *** Descriptor passing won't work. ***])],[ #include #include #include #include #include #include #if HAVE_SYS_UIO_H #include #endif #include ]) fi if test "$use_descriptor_passing" = "yes"; then AC_DEFINE(USE_DESCRIPTOR_PASSING, 1, [Defined if descriptor passing is supported]) fi AM_CONDITIONAL(USE_DESCRIPTOR_PASSING, test "$use_descriptor_passing" = "yes") # Checking for libgpg-error. AM_PATH_GPG_ERROR(1.17,, AC_MSG_ERROR([libgpg-error was not found])) AM_CONDITIONAL(USE_GPGRT_CONFIG, [test -n "$GPGRT_CONFIG" \ -a "$ac_cv_path_GPG_ERROR_CONFIG" = no]) # # Checks for library functions. # AC_CHECK_FUNCS([flockfile funlockfile inet_pton stat getaddrinfo \ getrlimit ]) # If we didn't find inet_pton, it might be in -lsocket (which might # require -lnsl) if test X"$ac_cv_func_inet_pton" != X"yes" ; then AC_SEARCH_LIBS([inet_pton],[socket],[],[],[-lnsl]) if test X"$ac_cv_search_inet_pton" != X"no" ; then AC_DEFINE([HAVE_INET_PTON],1,[Define to 1 if you have `inet_pton'.]) fi fi # On some systems (e.g. Solaris) nanosleep requires linking to librl. # Given that we use nanosleep only as an optimization over a select # based wait function we want it only if it is available in libc. _save_libs="$LIBS" AC_SEARCH_LIBS([nanosleep], [], [AC_DEFINE(HAVE_NANOSLEEP,1, [Define to 1 if you have the `nanosleep' function in libc.])]) LIBS="$_save_libs" # Check for funopen AC_CHECK_FUNCS(funopen) if test $ac_cv_func_funopen != yes; then # No funopen but we can implement that in terms of fopencookie. AC_CHECK_FUNCS(fopencookie) if test $ac_cv_func_fopencookie = yes; then AC_LIBOBJ([funopen]) else AC_MSG_WARN([ *** *** No implementation of fopencookie or funopen available. *** The assuan_get_data_fp function won't work; see the *** manual for details. GnuPG does not require this feature. ***]) fi fi AC_REPLACE_FUNCS(isascii) AC_REPLACE_FUNCS(memrchr) AC_REPLACE_FUNCS(stpcpy) AC_CHECK_HEADERS(unistd.h) AC_REPLACE_FUNCS(setenv) # # Check for the getsockopt SO_PEERCRED, etc. # AC_CHECK_MEMBER(struct sockpeercred.pid, [AC_DEFINE(HAVE_STRUCT_SOCKPEERCRED_PID, 1, Define if struct sockpeercred contains the pid member.)], [], [#include #include ]) # (Open)Solaris AC_CHECK_FUNCS([getpeerucred]) # FreeBSD AC_CHECK_FUNCS([getpeereid]) # # Extra features # build_doc=yes AC_ARG_ENABLE([doc], AS_HELP_STRING([--disable-doc], [do not build the documentation]), build_doc=$enableval, build_doc=yes) AM_CONDITIONAL([BUILD_DOC], [test "x$build_doc" != xno]) # # Create the config files. # AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([m4/Makefile]) AC_CONFIG_FILES([src/Makefile]) AC_CONFIG_FILES([doc/Makefile]) AC_CONFIG_FILES([tests/Makefile]) AC_CONFIG_FILES([src/libassuan-config], [chmod +x src/libassuan-config]) AC_CONFIG_FILES([src/versioninfo.rc]) AC_CONFIG_FILES([src/libassuan.pc]) AC_OUTPUT echo " Libassuan v${VERSION} has been configured as follows: Revision: mym4_revision (mym4_revision_dec) Platform: $host " diff --git a/src/assuan-buffer.c b/src/assuan-buffer.c index 895eb93..987ba45 100644 --- a/src/assuan-buffer.c +++ b/src/assuan-buffer.c @@ -1,550 +1,550 @@ /* assuan-buffer.c - read and send data * Copyright (C) 2001-2004, 2006, 2009, 2010 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, see . * * SPDX-License-Identifier: LGPL-2.1+ */ #include #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #include #ifdef HAVE_W32_SYSTEM # include #endif #include "assuan-defs.h" /* Extended version of write(2) to guarantee that all bytes are written. Returns 0 on success or -1 and ERRNO on failure. NOTE: This function does not return the number of bytes written, so any error must be treated as fatal for this connection as the state of the receiver is unknown. This works best if blocking is allowed (so EAGAIN cannot occur). */ static int writen (assuan_context_t ctx, const char *buffer, size_t length) { while (length) { ssize_t nwritten = ctx->engine.writefnc (ctx, buffer, length); if (nwritten < 0) { if (errno == EINTR) continue; return -1; /* write error */ } length -= nwritten; buffer += nwritten; } return 0; /* okay */ } /* Read an entire line. Returns 0 on success or -1 and ERRNO on failure. EOF is indictated by setting the integer at address R_EOF. Note: BUF, R_NREAD and R_EOF contain a valid result even if an error is returned. */ static int readline (assuan_context_t ctx, char *buf, size_t buflen, int *r_nread, int *r_eof) { size_t nleft = buflen; char *p; *r_eof = 0; *r_nread = 0; while (nleft > 0) { ssize_t n = ctx->engine.readfnc (ctx, buf, nleft); if (n < 0) { if (errno == EINTR) continue; #ifdef HAVE_W32_SYSTEM if (errno == EPIPE) { /* Under Windows we get EPIPE (actually ECONNRESET) after termination of the client. Assume an EOF. */ *r_eof = 1; break; /* allow incomplete lines */ } #endif /*HAVE_W32_SYSTEM*/ return -1; /* read error */ } else if (!n) { *r_eof = 1; break; /* allow incomplete lines */ } p = buf; nleft -= n; buf += n; *r_nread += n; p = memrchr (p, '\n', n); if (p) break; /* at least one full line available - that's enough for now */ } return 0; } /* Read a line with buffering of partial lines. Function returns an Assuan error. */ gpg_error_t _assuan_read_line (assuan_context_t ctx) { gpg_error_t rc = 0; char *line = ctx->inbound.line; int nread, atticlen; char *endp = 0; if (ctx->inbound.eof) return _assuan_error (ctx, GPG_ERR_EOF); atticlen = ctx->inbound.attic.linelen; if (atticlen) { memcpy (line, ctx->inbound.attic.line, atticlen); ctx->inbound.attic.linelen = 0; endp = memchr (line, '\n', atticlen); if (endp) { /* Found another line in the attic. */ nread = atticlen; atticlen = 0; } else { /* There is pending data but not a full line. */ assert (atticlen < LINELENGTH); rc = readline (ctx, line + atticlen, LINELENGTH - atticlen, &nread, &ctx->inbound.eof); } } else /* No pending data. */ rc = readline (ctx, line, LINELENGTH, &nread, &ctx->inbound.eof); if (rc) { int saved_errno = errno; char buf[100]; snprintf (buf, sizeof buf, "error: %s", strerror (errno)); _assuan_log_control_channel (ctx, 0, buf, NULL, 0, NULL, 0); if (saved_errno == EAGAIN) { /* We have to save a partial line. Due to readline's behaviour, we know that this is not a complete line yet (no newline). So we don't set PENDING to true. */ memcpy (ctx->inbound.attic.line, line, atticlen + nread); ctx->inbound.attic.pending = 0; ctx->inbound.attic.linelen = atticlen + nread; } gpg_err_set_errno (saved_errno); return _assuan_error (ctx, gpg_err_code_from_syserror ()); } if (!nread) { assert (ctx->inbound.eof); _assuan_log_control_channel (ctx, 0, "eof", NULL, 0, NULL, 0); return _assuan_error (ctx, GPG_ERR_EOF); } ctx->inbound.attic.pending = 0; nread += atticlen; if (! endp) endp = memchr (line, '\n', nread); if (endp) { unsigned monitor_result; int n = endp - line + 1; if (n < nread) /* LINE contains more than one line. We copy it to the attic now as handlers are allowed to modify the passed buffer. */ { int len = nread - n; memcpy (ctx->inbound.attic.line, endp + 1, len); ctx->inbound.attic.pending = memrchr (endp + 1, '\n', len) ? 1 : 0; ctx->inbound.attic.linelen = len; } if (endp != line && endp[-1] == '\r') endp --; *endp = 0; ctx->inbound.linelen = endp - line; monitor_result = 0; if (ctx->io_monitor) monitor_result = ctx->io_monitor (ctx, ctx->io_monitor_data, 0, ctx->inbound.line, ctx->inbound.linelen); if (monitor_result & ASSUAN_IO_MONITOR_IGNORE) ctx->inbound.linelen = 0; if ( !(monitor_result & ASSUAN_IO_MONITOR_NOLOG)) _assuan_log_control_channel (ctx, 0, NULL, ctx->inbound.line, ctx->inbound.linelen, NULL, 0); return 0; } else { _assuan_log_control_channel (ctx, 0, "invalid line", NULL, 0, NULL, 0); *line = 0; ctx->inbound.linelen = 0; return _assuan_error (ctx, ctx->inbound.eof ? GPG_ERR_ASS_INCOMPLETE_LINE : GPG_ERR_ASS_LINE_TOO_LONG); } } /* Read the next line from the client or server and return a pointer 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. See also: assuan_pending_line(). */ gpg_error_t assuan_read_line (assuan_context_t ctx, char **line, size_t *linelen) { gpg_error_t err; if (!ctx) return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE); do { err = _assuan_read_line (ctx); } while (_assuan_error_is_eagain (ctx, err)); *line = ctx->inbound.line; *linelen = ctx->inbound.linelen; return err; } /* 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_t ctx) { return ctx && ctx->inbound.attic.pending; } gpg_error_t _assuan_write_line (assuan_context_t ctx, const char *prefix, const char *line, size_t len) { gpg_error_t rc = 0; size_t prefixlen = prefix? strlen (prefix):0; unsigned int monitor_result; /* Make sure that the line is short enough. */ if (len + prefixlen + 2 > ASSUAN_LINELENGTH) { _assuan_log_control_channel (ctx, 1, "supplied line too long - truncated", NULL, 0, NULL, 0); if (prefixlen > 5) prefixlen = 5; if (len > ASSUAN_LINELENGTH - prefixlen - 2) len = ASSUAN_LINELENGTH - prefixlen - 2 - 1; } monitor_result = 0; if (ctx->io_monitor) monitor_result = ctx->io_monitor (ctx, ctx->io_monitor_data, 1, line, len); /* Fixme: we should do some kind of line buffering. */ if (!(monitor_result & ASSUAN_IO_MONITOR_NOLOG)) _assuan_log_control_channel (ctx, 1, NULL, prefixlen? prefix:NULL, prefixlen, line, len); if (prefixlen && !(monitor_result & ASSUAN_IO_MONITOR_IGNORE)) { rc = writen (ctx, prefix, prefixlen); if (rc) rc = _assuan_error (ctx, gpg_err_code_from_syserror ()); } if (!rc && !(monitor_result & ASSUAN_IO_MONITOR_IGNORE)) { rc = writen (ctx, line, len); if (rc) rc = _assuan_error (ctx, gpg_err_code_from_syserror ()); if (!rc) { rc = writen (ctx, "\n", 1); if (rc) rc = _assuan_error (ctx, gpg_err_code_from_syserror ()); } } return rc; } gpg_error_t assuan_write_line (assuan_context_t ctx, const char *line) { size_t len; const char *str; if (! ctx) return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE); /* Make sure that we never take a LF from the user - this might violate the protocol. */ str = strchr (line, '\n'); len = str ? (str - line) : strlen (line); if (str) _assuan_log_control_channel (ctx, 1, "supplied line with LF - truncated", NULL, 0, NULL, 0); return _assuan_write_line (ctx, NULL, line, len); } /* Write out the data in buffer as datalines with line wrapping and percent escaping. This function is used for GNU's custom streams. */ int _assuan_cookie_write_data (void *cookie, const char *buffer, size_t orig_size) { assuan_context_t ctx = cookie; size_t size = orig_size; char *line; size_t linelen; if (ctx->outbound.data.error) return 0; line = ctx->outbound.data.line; linelen = ctx->outbound.data.linelen; line += linelen; while (size) { unsigned int monitor_result; /* Insert data line header. */ if (!linelen) { *line++ = 'D'; *line++ = ' '; linelen += 2; } /* Copy data, keep space for the CRLF and to escape one character. */ while (size && linelen < LINELENGTH-2-2) { if (*buffer == '%' || *buffer == '\r' || *buffer == '\n') { sprintf (line, "%%%02X", *(unsigned char*)buffer); line += 3; linelen += 3; buffer++; } else { *line++ = *buffer++; linelen++; } size--; } monitor_result = 0; if (ctx->io_monitor) monitor_result = ctx->io_monitor (ctx, ctx->io_monitor_data, 1, ctx->outbound.data.line, linelen); if (linelen >= LINELENGTH-2-2) { if (!(monitor_result & ASSUAN_IO_MONITOR_NOLOG)) _assuan_log_control_channel (ctx, 1, NULL, ctx->outbound.data.line, linelen, NULL, 0); *line++ = '\n'; linelen++; if ( !(monitor_result & ASSUAN_IO_MONITOR_IGNORE) && writen (ctx, ctx->outbound.data.line, linelen)) { ctx->outbound.data.error = gpg_err_code_from_syserror (); return 0; } line = ctx->outbound.data.line; linelen = 0; } } ctx->outbound.data.linelen = linelen; return (int) orig_size; } /* Write out any buffered data This function is used for GNU's custom streams */ int _assuan_cookie_write_flush (void *cookie) { assuan_context_t ctx = cookie; char *line; size_t linelen; unsigned int monitor_result; if (ctx->outbound.data.error) return 0; line = ctx->outbound.data.line; linelen = ctx->outbound.data.linelen; line += linelen; monitor_result = 0; if (ctx->io_monitor) monitor_result = ctx->io_monitor (ctx, ctx->io_monitor_data, 1, ctx->outbound.data.line, linelen); if (linelen) { if (!(monitor_result & ASSUAN_IO_MONITOR_NOLOG)) _assuan_log_control_channel (ctx, 1, NULL, ctx->outbound.data.line, linelen, NULL, 0); *line++ = '\n'; linelen++; if (! (monitor_result & ASSUAN_IO_MONITOR_IGNORE) && writen (ctx, ctx->outbound.data.line, linelen)) { ctx->outbound.data.error = gpg_err_code_from_syserror (); return 0; } ctx->outbound.data.linelen = 0; } return 0; } /** * 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 response on * a INQUIRE response. However, when assuan_transact() is used, this * function takes care of sending END itself. * * If BUFFER is NULL and LENGTH is 1 and we are a client, a "CAN" is * send instead of an "END". * * Return value: 0 on success or an error code **/ gpg_error_t assuan_send_data (assuan_context_t ctx, const void *buffer, size_t length) { if (!ctx) return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE); if (!buffer && length > 1) return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE); if (!buffer) { /* flush what we have */ _assuan_cookie_write_flush (ctx); if (ctx->flags.confidential) wipememory (ctx->outbound.data.line, LINELENGTH); if (ctx->outbound.data.error) return ctx->outbound.data.error; if (!ctx->flags.is_server) return assuan_write_line (ctx, length == 1? "CAN":"END"); } else { _assuan_cookie_write_data (ctx, buffer, length); if (ctx->outbound.data.error) return ctx->outbound.data.error; } return 0; } gpg_error_t assuan_sendfd (assuan_context_t ctx, assuan_fd_t fd) { /* It is explicitly allowed to use (NULL, -1) as a runtime test to check whether descriptor passing is available. */ if (!ctx && fd == ASSUAN_INVALID_FD) -#ifdef USE_DESCRIPTOR_PASSING +#if defined(USE_DESCRIPTOR_PASSING) || defined(HAVE_W32_SYSTEM) return 0; #else - return _assuan_error (ctx, GPG_ERR_NOT_IMPLEMENTED); + return _assuan_error (ctx, GPG_ERR_NOT_IMPLEMENTED); #endif if (!ctx) return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE); if (! ctx->engine.sendfd) return set_error (ctx, GPG_ERR_NOT_IMPLEMENTED, "server does not support sending and receiving " "of file descriptors"); return ctx->engine.sendfd (ctx, fd); } gpg_error_t assuan_receivefd (assuan_context_t ctx, assuan_fd_t *fd) { if (!ctx) return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE); if (! ctx->engine.receivefd) return set_error (ctx, GPG_ERR_NOT_IMPLEMENTED, "server does not support sending and receiving " "of file descriptors"); return ctx->engine.receivefd (ctx, fd); }