2010-07-23 16:16:14 +00:00
|
|
|
|
/* ldap-wrapper-ce.c - LDAP access via W32 threads
|
|
|
|
|
* Copyright (C) 2010 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 3 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
|
2016-11-05 12:02:19 +01:00
|
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2010-07-23 16:16:14 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
/*
|
2010-07-23 16:16:14 +00:00
|
|
|
|
Alternative wrapper for use with WindowsCE. Under WindowsCE the
|
|
|
|
|
number of processes is strongly limited (32 processes including the
|
|
|
|
|
kernel processes) and thus we don't use the process approach but
|
|
|
|
|
implement a wrapper based on native threads.
|
|
|
|
|
|
|
|
|
|
See ldap-wrapper.c for the standard wrapper interface.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <time.h>
|
Port to npth.
* configure.ac: Don't check for PTH but for NPTH.
(AH_BOTTOM): Remove PTH_SYSCALL_SOFT.
(have_pth): Rename to ...
(have_npth): ... this.
(USE_GNU_NPTH): Rename to ...
(USE_GNU_PTH): ... this.
* m4/npth.m4: New file.
* agent/Makefile.am, agent/cache.c, agent/call-pinentry.c,
agent/call-scd.c, agent/findkey.c, agent/gpg-agent.c,
agent/trustlist.c, common/Makefile.am, common/estream.c,
common/exechelp-posix.c, common/exechelp-w32.c,
common/exechelp-w32ce.c, common/http.c, common/init.c,
common/sysutils.c, dirmngr/Makefile.am, dirmngr/crlfetch.c,
dirmngr/dirmngr.c, dirmngr/dirmngr_ldap.c, dirmngr/ldap-wrapper-ce.c,
dirmngr/ldap-wrapper.c, dirmngr/ldap.c, g13/Makefile.am,
g13/call-gpg.c, g13/g13.c, g13/runner.c, scd/Makefile.am,
scd/apdu.c, scd/app.c, scd/ccid-driver.c, scd/command.c,
scd/scdaemon.c, tools/Makefile.am: Port to npth.
2012-01-03 22:12:37 +01:00
|
|
|
|
#include <npth.h>
|
2010-08-02 18:54:53 +00:00
|
|
|
|
#include <assert.h>
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
#include "dirmngr.h"
|
|
|
|
|
#include "misc.h"
|
|
|
|
|
#include "ldap-wrapper.h"
|
|
|
|
|
|
2010-07-26 14:01:32 +00:00
|
|
|
|
#ifdef USE_LDAPWRAPPER
|
|
|
|
|
# error This module is not expected to be build.
|
|
|
|
|
#endif
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
|
|
|
|
/* Read a fixed amount of data from READER into BUFFER. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
read_buffer (ksba_reader_t reader, unsigned char *buffer, size_t count)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
gpg_error_t err;
|
|
|
|
|
size_t nread;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
while (count)
|
|
|
|
|
{
|
|
|
|
|
err = ksba_reader_read (reader, buffer, count, &nread);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
buffer += nread;
|
|
|
|
|
count -= nread;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* Start the reaper thread for this wrapper. */
|
|
|
|
|
void
|
|
|
|
|
ldap_wrapper_launch_thread (void)
|
|
|
|
|
{
|
|
|
|
|
/* Not required. */
|
|
|
|
|
}
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* Wait until all ldap wrappers have terminated. We assume that the
|
|
|
|
|
kill has already been sent to all of them. */
|
2010-07-23 16:16:14 +00:00
|
|
|
|
void
|
2010-08-02 18:54:53 +00:00
|
|
|
|
ldap_wrapper_wait_connections ()
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* Not required. */
|
|
|
|
|
}
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* Cleanup all resources held by the connection associated with
|
|
|
|
|
CTRL. This is used after a cancel to kill running wrappers. */
|
|
|
|
|
void
|
|
|
|
|
ldap_wrapper_connection_cleanup (ctrl_t ctrl)
|
|
|
|
|
{
|
|
|
|
|
(void)ctrl;
|
|
|
|
|
|
|
|
|
|
/* Not required. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The cookie we use to implement the outstream of the wrapper thread. */
|
|
|
|
|
struct outstream_cookie_s
|
|
|
|
|
{
|
|
|
|
|
int refcount; /* Reference counter - possible values are 1 and 2. */
|
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
/* We don't need a mutex for the conditions, as npth provides a
|
|
|
|
|
simpler condition interface that relies on the global lock. This
|
|
|
|
|
can be used if we never yield between testing the condition and
|
|
|
|
|
waiting on it. */
|
|
|
|
|
npth_cond_t wait_data; /* Condition that data is available. */
|
|
|
|
|
npth_cond_t wait_space; /* Condition that space is available. */
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
int eof_seen; /* EOF indicator. */
|
2012-01-24 17:37:01 +01:00
|
|
|
|
char buffer[4000]; /* Data ring buffer. */
|
|
|
|
|
size_t buffer_len; /* The amount of data in the BUFFER. */
|
2010-08-02 18:54:53 +00:00
|
|
|
|
size_t buffer_pos; /* The next read position of the BUFFER. */
|
2017-04-26 09:39:06 +02:00
|
|
|
|
size_t buffer_read_pos; /* The next read position of the BUFFER. */
|
2010-08-02 18:54:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
#define BUFFER_EMPTY(c) ((c)->buffer_len == 0)
|
|
|
|
|
#define BUFFER_FULL(c) ((c)->buffer_len == DIM((c)->buffer))
|
|
|
|
|
#define BUFFER_DATA_AVAILABLE(c) ((c)->buffer_len)
|
|
|
|
|
#define BUFFER_SPACE_AVAILABLE(c) (DIM((c)->buffer) - (c)->buffer_len)
|
|
|
|
|
#define BUFFER_INC_POS(c,n) (c)->buffer_pos = ((c)->buffer_pos + (n)) % DIM((c)->buffer)
|
|
|
|
|
#define BUFFER_CUR_POS(c) (&(c)->buffer[(c)->buffer_pos])
|
2017-04-26 09:39:06 +02:00
|
|
|
|
#define BUFFER_INC_READ_POS(c,n) (c)->buffer_read_pos = ((c)->buffer_read_pos + (n)) % DIM((c)->buffer)
|
|
|
|
|
#define BUFFER_CUR_READ_POS(c) (&(c)->buffer[(c)->buffer_read_pos])
|
2012-01-24 17:37:01 +01:00
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
buffer_get_data (struct outstream_cookie_s *cookie, char *dst, int cnt)
|
|
|
|
|
{
|
|
|
|
|
int amount;
|
|
|
|
|
int left;
|
|
|
|
|
int chunk;
|
|
|
|
|
|
|
|
|
|
amount = cnt;
|
|
|
|
|
if (BUFFER_DATA_AVAILABLE (cookie) < amount)
|
|
|
|
|
amount = BUFFER_DATA_AVAILABLE (cookie);
|
|
|
|
|
left = amount;
|
|
|
|
|
|
|
|
|
|
/* How large is the part up to the end of the buffer array? */
|
|
|
|
|
chunk = DIM(cookie->buffer) - cookie->buffer_pos;
|
|
|
|
|
if (chunk > left)
|
|
|
|
|
chunk = left;
|
|
|
|
|
|
2017-04-26 09:39:06 +02:00
|
|
|
|
memcpy (dst, BUFFER_CUR_READ_POS (cookie), chunk);
|
|
|
|
|
BUFFER_INC_READ_POS (cookie, chunk);
|
2012-01-24 17:37:01 +01:00
|
|
|
|
left -= chunk;
|
|
|
|
|
dst += chunk;
|
|
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
|
{
|
2017-04-26 09:39:06 +02:00
|
|
|
|
memcpy (dst, BUFFER_CUR_READ_POS (cookie), left);
|
|
|
|
|
BUFFER_INC_READ_POS (cookie, left);
|
2012-01-24 17:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
buffer_put_data (struct outstream_cookie_s *cookie, const char *src, int cnt)
|
|
|
|
|
{
|
|
|
|
|
int amount;
|
|
|
|
|
int remain;
|
|
|
|
|
int left;
|
|
|
|
|
int chunk;
|
|
|
|
|
|
|
|
|
|
remain = DIM(cookie->buffer) - cookie->buffer_len;
|
|
|
|
|
|
|
|
|
|
amount = cnt;
|
|
|
|
|
if (remain < amount)
|
|
|
|
|
amount = remain;
|
|
|
|
|
left = amount;
|
|
|
|
|
|
|
|
|
|
/* How large is the part up to the end of the buffer array? */
|
|
|
|
|
chunk = DIM(cookie->buffer) - cookie->buffer_pos;
|
|
|
|
|
if (chunk > left)
|
|
|
|
|
chunk = left;
|
|
|
|
|
|
|
|
|
|
memcpy (BUFFER_CUR_POS (cookie), src, chunk);
|
|
|
|
|
BUFFER_INC_POS (cookie, chunk);
|
|
|
|
|
left -= chunk;
|
|
|
|
|
src += chunk;
|
|
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
|
{
|
|
|
|
|
memcpy (BUFFER_CUR_POS (cookie), src, left);
|
|
|
|
|
BUFFER_INC_POS (cookie, left);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cookie->buffer_len -= amount;
|
|
|
|
|
return amount;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* The writer function for the outstream. This is used to transfer
|
|
|
|
|
the output of the ldap wrapper thread to the ksba reader object. */
|
2017-04-11 16:12:34 +09:00
|
|
|
|
static gpgrt_ssize_t
|
2010-08-02 18:54:53 +00:00
|
|
|
|
outstream_cookie_writer (void *cookie_arg, const void *buffer, size_t size)
|
|
|
|
|
{
|
|
|
|
|
struct outstream_cookie_s *cookie = cookie_arg;
|
|
|
|
|
const char *src;
|
|
|
|
|
ssize_t nwritten = 0;
|
2012-01-24 17:37:01 +01:00
|
|
|
|
int res;
|
|
|
|
|
ssize_t amount = 0;
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
|
|
|
|
src = buffer;
|
|
|
|
|
do
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2012-01-24 17:37:01 +01:00
|
|
|
|
int was_empty = 0;
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* Wait for free space. */
|
2012-01-24 17:37:01 +01:00
|
|
|
|
while (BUFFER_FULL(cookie))
|
2010-08-02 18:54:53 +00:00
|
|
|
|
{
|
|
|
|
|
/* Buffer is full: Wait for space. */
|
2012-01-24 17:37:01 +01:00
|
|
|
|
res = npth_cond_wait (&cookie->wait_space, NULL);
|
|
|
|
|
if (res)
|
|
|
|
|
{
|
|
|
|
|
gpg_err_set_errno (res);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2010-08-02 18:54:53 +00:00
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
if (BUFFER_EMPTY(cookie))
|
|
|
|
|
was_empty = 1;
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* Copy data. */
|
2012-01-24 17:37:01 +01:00
|
|
|
|
nwritten = buffer_put_data (cookie, buffer, size);
|
|
|
|
|
size -= nwritten;
|
|
|
|
|
src += nwritten;
|
|
|
|
|
amount += nwritten;
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
if (was_empty)
|
|
|
|
|
npth_cond_signal (&cookie->wait_data);
|
2010-08-02 18:54:53 +00:00
|
|
|
|
}
|
2012-01-24 17:37:01 +01:00
|
|
|
|
while (size); /* Until done. */
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
return amount;
|
2010-08-02 18:54:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
outstream_release_cookie (struct outstream_cookie_s *cookie)
|
|
|
|
|
{
|
|
|
|
|
cookie->refcount--;
|
|
|
|
|
if (!cookie->refcount)
|
2012-01-24 17:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
npth_cond_destroy (&cookie->wait_data);
|
|
|
|
|
npth_cond_destroy (&cookie->wait_space);
|
|
|
|
|
xfree (cookie);
|
|
|
|
|
}
|
2010-07-23 16:16:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/* Closer function for the outstream. This deallocates the cookie if
|
|
|
|
|
it won't be used anymore. */
|
|
|
|
|
static int
|
|
|
|
|
outstream_cookie_closer (void *cookie_arg)
|
|
|
|
|
{
|
|
|
|
|
struct outstream_cookie_s *cookie = cookie_arg;
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
if (!cookie)
|
|
|
|
|
return 0; /* Nothing to do. */
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
cookie->eof_seen = 1; /* (only useful if refcount > 1) */
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
assert (cookie->refcount > 0);
|
|
|
|
|
outstream_release_cookie (cookie);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The KSBA reader callback which takes the output of the ldap thread
|
|
|
|
|
form the outstream_cookie_writer and make it available to the ksba
|
|
|
|
|
reader. */
|
|
|
|
|
static int
|
|
|
|
|
outstream_reader_cb (void *cb_value, char *buffer, size_t count,
|
|
|
|
|
size_t *r_nread)
|
|
|
|
|
{
|
|
|
|
|
struct outstream_cookie_s *cookie = cb_value;
|
|
|
|
|
size_t nread = 0;
|
2012-01-24 17:37:01 +01:00
|
|
|
|
int was_full = 0;
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
2010-08-06 13:52:01 +00:00
|
|
|
|
if (!buffer && !count && !r_nread)
|
2010-08-02 18:54:53 +00:00
|
|
|
|
return gpg_error (GPG_ERR_NOT_SUPPORTED); /* Rewind is not supported. */
|
|
|
|
|
|
|
|
|
|
*r_nread = 0;
|
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
while (BUFFER_EMPTY(cookie))
|
2010-08-02 18:54:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (cookie->eof_seen)
|
|
|
|
|
return gpg_error (GPG_ERR_EOF);
|
|
|
|
|
|
|
|
|
|
/* Wait for data to become available. */
|
2012-01-24 17:37:01 +01:00
|
|
|
|
npth_cond_wait (&cookie->wait_data, NULL);
|
2010-08-02 18:54:53 +00:00
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
if (BUFFER_FULL(cookie))
|
|
|
|
|
was_full = 1;
|
|
|
|
|
|
|
|
|
|
nread = buffer_get_data (cookie, buffer, count);
|
|
|
|
|
|
|
|
|
|
if (was_full)
|
2010-08-02 18:54:53 +00:00
|
|
|
|
{
|
2012-01-24 17:37:01 +01:00
|
|
|
|
npth_cond_signal (&cookie->wait_space);
|
2010-08-02 18:54:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*r_nread = nread;
|
|
|
|
|
return 0; /* Success. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function is called by ksba_reader_release. */
|
|
|
|
|
static void
|
|
|
|
|
outstream_reader_released (void *cb_value, ksba_reader_t r)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
struct outstream_cookie_s *cookie = cb_value;
|
|
|
|
|
|
|
|
|
|
(void)r;
|
|
|
|
|
|
|
|
|
|
assert (cookie->refcount > 0);
|
|
|
|
|
outstream_release_cookie (cookie);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
2010-07-23 16:16:14 +00:00
|
|
|
|
/* This function is to be used to release a context associated with the
|
2010-08-02 18:54:53 +00:00
|
|
|
|
given reader object. This does not release the reader object, though. */
|
2010-07-23 16:16:14 +00:00
|
|
|
|
void
|
|
|
|
|
ldap_wrapper_release_context (ksba_reader_t reader)
|
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
(void)reader;
|
|
|
|
|
/* Nothing to do. */
|
2010-07-23 16:16:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Free a NULL terminated array of malloced strings and the array
|
|
|
|
|
itself. */
|
|
|
|
|
static void
|
|
|
|
|
free_arg_list (char **arg_list)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
int i;
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
if (arg_list)
|
|
|
|
|
{
|
|
|
|
|
for (i=0; arg_list[i]; i++)
|
|
|
|
|
xfree (arg_list[i]);
|
|
|
|
|
xfree (arg_list);
|
|
|
|
|
}
|
2010-07-23 16:16:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
|
|
|
|
/* Copy ARGV into a new array and prepend one element as name of the
|
|
|
|
|
program (which is more or less a stub). We need to allocate all
|
|
|
|
|
the strings to get ownership of them. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
create_arg_list (const char *argv[], char ***r_arg_list)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
2010-08-02 18:54:53 +00:00
|
|
|
|
char **arg_list;
|
|
|
|
|
int i, j;
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; argv[i]; i++)
|
|
|
|
|
;
|
|
|
|
|
arg_list = xtrycalloc (i + 2, sizeof *arg_list);
|
|
|
|
|
if (!arg_list)
|
2010-08-02 18:54:53 +00:00
|
|
|
|
goto outofcore;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
arg_list[i] = xtrystrdup ("<ldap-wrapper-thread>");
|
|
|
|
|
if (!arg_list[i])
|
|
|
|
|
goto outofcore;
|
|
|
|
|
i++;
|
|
|
|
|
for (j=0; argv[j]; j++)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
arg_list[i] = xtrystrdup (argv[j]);
|
|
|
|
|
if (!arg_list[i])
|
|
|
|
|
goto outofcore;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
arg_list[i] = NULL;
|
|
|
|
|
*r_arg_list = arg_list;
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
outofcore:
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("error allocating memory: %s\n"), strerror (errno));
|
|
|
|
|
free_arg_list (arg_list);
|
|
|
|
|
*r_arg_list = NULL;
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parameters passed to the wrapper thread. */
|
|
|
|
|
struct ldap_wrapper_thread_parms
|
|
|
|
|
{
|
|
|
|
|
char **arg_list;
|
|
|
|
|
estream_t outstream;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* The thread which runs the LDAP wrapper. */
|
|
|
|
|
static void *
|
|
|
|
|
ldap_wrapper_thread (void *opaque)
|
|
|
|
|
{
|
|
|
|
|
struct ldap_wrapper_thread_parms *parms = opaque;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
/*err =*/ ldap_wrapper_main (parms->arg_list, parms->outstream);
|
|
|
|
|
|
|
|
|
|
/* FIXME: Do we need to return ERR? */
|
|
|
|
|
|
|
|
|
|
free_arg_list (parms->arg_list);
|
|
|
|
|
es_fclose (parms->outstream);
|
|
|
|
|
xfree (parms);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Start a new LDAP thread and returns a new libksba reader
|
|
|
|
|
object at READER. ARGV is a NULL terminated list of arguments for
|
|
|
|
|
the wrapper. The function returns 0 on success or an error code. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
ldap_wrapper (ctrl_t ctrl, ksba_reader_t *r_reader, const char *argv[])
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
struct ldap_wrapper_thread_parms *parms;
|
2012-01-24 17:37:01 +01:00
|
|
|
|
npth_attr_t tattr;
|
2010-08-02 18:54:53 +00:00
|
|
|
|
es_cookie_io_functions_t outstream_func = { NULL };
|
|
|
|
|
struct outstream_cookie_s *outstream_cookie;
|
|
|
|
|
ksba_reader_t reader;
|
2012-01-24 17:37:01 +01:00
|
|
|
|
int res;
|
|
|
|
|
npth_t thread;
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
|
|
|
|
(void)ctrl;
|
|
|
|
|
|
|
|
|
|
*r_reader = NULL;
|
|
|
|
|
|
|
|
|
|
parms = xtrycalloc (1, sizeof *parms);
|
|
|
|
|
if (!parms)
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
|
|
|
|
|
err = create_arg_list (argv, &parms->arg_list);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
xfree (parms);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
outstream_cookie = xtrycalloc (1, sizeof *outstream_cookie);
|
|
|
|
|
if (!outstream_cookie)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
2010-08-02 18:54:53 +00:00
|
|
|
|
free_arg_list (parms->arg_list);
|
|
|
|
|
xfree (parms);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
2010-08-02 18:54:53 +00:00
|
|
|
|
outstream_cookie->refcount++;
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
res = npth_cond_init (&outstream_cookie->wait_data, NULL);
|
|
|
|
|
if (res)
|
|
|
|
|
{
|
|
|
|
|
free_arg_list (parms->arg_list);
|
|
|
|
|
xfree (parms);
|
|
|
|
|
return gpg_error_from_errno (res);
|
|
|
|
|
}
|
|
|
|
|
res = npth_cond_init (&outstream_cookie->wait_space, NULL);
|
|
|
|
|
if (res)
|
|
|
|
|
{
|
|
|
|
|
npth_cond_destroy (&outstream_cookie->wait_data);
|
|
|
|
|
free_arg_list (parms->arg_list);
|
|
|
|
|
xfree (parms);
|
|
|
|
|
return gpg_error_from_errno (res);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
err = ksba_reader_new (&reader);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
if (!err)
|
2010-08-02 18:54:53 +00:00
|
|
|
|
err = ksba_reader_set_release_notify (reader,
|
|
|
|
|
outstream_reader_released,
|
|
|
|
|
outstream_cookie);
|
|
|
|
|
if (!err)
|
|
|
|
|
err = ksba_reader_set_cb (reader,
|
|
|
|
|
outstream_reader_cb, outstream_cookie);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
log_error (_("error initializing reader object: %s\n"),
|
|
|
|
|
gpg_strerror (err));
|
|
|
|
|
ksba_reader_release (reader);
|
|
|
|
|
outstream_release_cookie (outstream_cookie);
|
|
|
|
|
free_arg_list (parms->arg_list);
|
|
|
|
|
xfree (parms);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
|
|
|
|
|
outstream_func.func_write = outstream_cookie_writer;
|
|
|
|
|
outstream_func.func_close = outstream_cookie_closer;
|
|
|
|
|
parms->outstream = es_fopencookie (outstream_cookie, "wb", outstream_func);
|
|
|
|
|
if (!parms->outstream)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
err = gpg_error_from_syserror ();
|
2012-01-24 17:37:01 +01:00
|
|
|
|
ksba_reader_release (reader);
|
2010-08-02 18:54:53 +00:00
|
|
|
|
outstream_release_cookie (outstream_cookie);
|
2012-01-24 17:37:01 +01:00
|
|
|
|
free_arg_list (parms->arg_list);
|
2010-08-02 18:54:53 +00:00
|
|
|
|
xfree (parms);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
2010-08-02 18:54:53 +00:00
|
|
|
|
outstream_cookie->refcount++;
|
2010-07-23 16:16:14 +00:00
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
res = npth_attr_init(&tattr);
|
|
|
|
|
if (res)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_errno (res);
|
|
|
|
|
ksba_reader_release (reader);
|
|
|
|
|
free_arg_list (parms->arg_list);
|
|
|
|
|
es_fclose (parms->outstream);
|
|
|
|
|
xfree (parms);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
npth_attr_setdetachstate (&tattr, NPTH_CREATE_DETACHED);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
|
res = npth_create (&thread, &tattr, ldap_wrapper_thread, parms);
|
|
|
|
|
npth_attr_destroy (&tattr);
|
|
|
|
|
if (res)
|
2010-07-23 16:16:14 +00:00
|
|
|
|
{
|
2012-01-24 17:37:01 +01:00
|
|
|
|
err = gpg_error_from_errno (res);
|
2010-08-02 18:54:53 +00:00
|
|
|
|
log_error ("error spawning ldap wrapper thread: %s\n",
|
2012-01-24 17:37:01 +01:00
|
|
|
|
strerror (res) );
|
2010-08-02 18:54:53 +00:00
|
|
|
|
}
|
2012-01-24 17:37:01 +01:00
|
|
|
|
else
|
|
|
|
|
parms = NULL; /* Now owned by the thread. */
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
if (parms)
|
|
|
|
|
{
|
|
|
|
|
free_arg_list (parms->arg_list);
|
|
|
|
|
es_fclose (parms->outstream);
|
|
|
|
|
xfree (parms);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
}
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
ksba_reader_release (reader);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Need to wait for the first byte so we are able to detect an empty
|
|
|
|
|
output and not let the consumer see an EOF without further error
|
|
|
|
|
indications. The CRL loading logic assumes that after return
|
|
|
|
|
from this function, a failed search (e.g. host not found ) is
|
|
|
|
|
indicated right away. */
|
|
|
|
|
{
|
|
|
|
|
unsigned char c;
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
err = read_buffer (reader, &c, 1);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
|
ksba_reader_release (reader);
|
|
|
|
|
reader = NULL;
|
2010-07-23 16:16:14 +00:00
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_EOF)
|
|
|
|
|
return gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
else
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2010-08-02 18:54:53 +00:00
|
|
|
|
ksba_reader_unread (reader, &c, 1);
|
2010-07-23 16:16:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-02 18:54:53 +00:00
|
|
|
|
*r_reader = reader;
|
|
|
|
|
|
2010-07-23 16:16:14 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|