2010-06-09 18:53:51 +02:00
|
|
|
/* dirmngr-ldap.c - The LDAP helper for dirmngr.
|
2010-08-13 12:02:12 +02:00
|
|
|
* Copyright (C) 2004 g10 Code GmbH
|
|
|
|
* Copyright (C) 2010 Free Software Foundation, Inc.
|
2010-06-09 18:53:51 +02:00
|
|
|
*
|
2010-08-13 12:02:12 +02:00
|
|
|
* This file is part of GnuPG.
|
2010-06-09 18:53:51 +02:00
|
|
|
*
|
2010-08-13 12:02:12 +02:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
2010-06-09 18:53:51 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2010-08-13 12:02:12 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-06-09 18:53:51 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2010-08-13 12:02:12 +02:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
2010-06-09 18:53:51 +02:00
|
|
|
* 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
|
2010-08-13 12:02:12 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2010-06-09 18:53:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2010-07-16 15:19:45 +02:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
2010-06-09 18:53:51 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2010-08-02 20:54:53 +02:00
|
|
|
#ifndef USE_LDAPWRAPPER
|
|
|
|
# include <pth.h>
|
|
|
|
#endif
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <winldap.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "ldap-url.h"
|
|
|
|
#else
|
|
|
|
/* For OpenLDAP, to enable the API that we're using. */
|
|
|
|
#define LDAP_DEPRECATED 1
|
|
|
|
#include <ldap.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define JNLIB_NEED_LOG_LOGV
|
|
|
|
#include "../common/logging.h"
|
|
|
|
#include "../common/argparse.h"
|
|
|
|
#include "../common/stringhelp.h"
|
|
|
|
#include "../common/mischelp.h"
|
|
|
|
#include "../common/strlist.h"
|
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2010-08-02 20:54:53 +02:00
|
|
|
/* With the ldap wrapper, there is no need for the pth_enter and leave
|
|
|
|
functions; thus we redefine them to nops. If we are not using the
|
|
|
|
ldap wrapper process we need to include the prototype for our
|
|
|
|
module's main function. */
|
|
|
|
#ifdef USE_LDAPWRAPPER
|
2010-08-03 16:06:42 +02:00
|
|
|
static void pth_enter (void) { }
|
|
|
|
static void pth_leave (void) { }
|
2010-08-02 20:54:53 +02:00
|
|
|
#else
|
|
|
|
# include "./ldap-wrapper.h"
|
2010-07-26 16:01:32 +02:00
|
|
|
#endif
|
|
|
|
|
2010-08-13 12:02:12 +02:00
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
# include "w32-ldap-help.h"
|
|
|
|
# define my_ldap_init(a,b) \
|
|
|
|
_dirmngr_ldap_init ((a), (b))
|
|
|
|
# define my_ldap_simple_bind_s(a,b,c) \
|
|
|
|
_dirmngr_ldap_simple_bind_s ((a),(b),(c))
|
|
|
|
# define my_ldap_search_st(a,b,c,d,e,f,g,h) \
|
|
|
|
_dirmngr_ldap_search_st ((a), (b), (c), (d), (e), (f), (g), (h))
|
|
|
|
# define my_ldap_first_attribute(a,b,c) \
|
|
|
|
_dirmngr_ldap_first_attribute ((a),(b),(c))
|
|
|
|
# define my_ldap_next_attribute(a,b,c) \
|
|
|
|
_dirmngr_ldap_next_attribute ((a),(b),(c))
|
|
|
|
# define my_ldap_get_values_len(a,b,c) \
|
|
|
|
_dirmngr_ldap_get_values_len ((a),(b),(c))
|
|
|
|
# define my_ldap_free_attr(a) \
|
|
|
|
xfree ((a))
|
|
|
|
#else
|
|
|
|
# define my_ldap_init(a,b) ldap_init ((a), (b))
|
|
|
|
# define my_ldap_simple_bind_s(a,b,c) ldap_simple_bind_s ((a), (b), (c))
|
|
|
|
# define my_ldap_search_st(a,b,c,d,e,f,g,h) \
|
|
|
|
ldap_search_st ((a), (b), (c), (d), (e), (f), (g), (h))
|
|
|
|
# define my_ldap_first_attribute(a,b,c) ldap_first_attribute ((a),(b),(c))
|
|
|
|
# define my_ldap_next_attribute(a,b,c) ldap_next_attribute ((a),(b),(c))
|
|
|
|
# define my_ldap_get_values_len(a,b,c) ldap_get_values_len ((a),(b),(c))
|
|
|
|
# define my_ldap_free_attr(a) ldap_memfree ((a))
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
#define DEFAULT_LDAP_TIMEOUT 100 /* Arbitrary long timeout. */
|
|
|
|
|
|
|
|
|
|
|
|
/* Constants for the options. */
|
2011-02-04 12:57:53 +01:00
|
|
|
enum
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
oQuiet = 'q',
|
|
|
|
oVerbose = 'v',
|
|
|
|
|
|
|
|
oTimeout = 500,
|
|
|
|
oMulti,
|
|
|
|
oProxy,
|
|
|
|
oHost,
|
|
|
|
oPort,
|
|
|
|
oUser,
|
|
|
|
oPass,
|
|
|
|
oEnvPass,
|
|
|
|
oDN,
|
|
|
|
oFilter,
|
|
|
|
oAttr,
|
|
|
|
|
|
|
|
oOnlySearchTimeout,
|
|
|
|
oLogWithPID
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* The list of options as used by the argparse.c code. */
|
|
|
|
static ARGPARSE_OPTS opts[] = {
|
|
|
|
{ oVerbose, "verbose", 0, N_("verbose") },
|
|
|
|
{ oQuiet, "quiet", 0, N_("be somewhat more quiet") },
|
|
|
|
{ oTimeout, "timeout", 1, N_("|N|set LDAP timeout to N seconds")},
|
|
|
|
{ oMulti, "multi", 0, N_("return all values in"
|
|
|
|
" a record oriented format")},
|
|
|
|
{ oProxy, "proxy", 2,
|
|
|
|
N_("|NAME|ignore host part and connect through NAME")},
|
|
|
|
{ oHost, "host", 2, N_("|NAME|connect to host NAME")},
|
|
|
|
{ oPort, "port", 1, N_("|N|connect to port N")},
|
|
|
|
{ oUser, "user", 2, N_("|NAME|use user NAME for authentication")},
|
|
|
|
{ oPass, "pass", 2, N_("|PASS|use password PASS"
|
|
|
|
" for authentication")},
|
|
|
|
{ oEnvPass, "env-pass", 0, N_("take password from $DIRMNGR_LDAP_PASS")},
|
|
|
|
{ oDN, "dn", 2, N_("|STRING|query DN STRING")},
|
|
|
|
{ oFilter, "filter", 2, N_("|STRING|use STRING as filter expression")},
|
|
|
|
{ oAttr, "attr", 2, N_("|STRING|return the attribute STRING")},
|
|
|
|
{ oOnlySearchTimeout, "only-search-timeout", 0, "@"},
|
|
|
|
{ oLogWithPID,"log-with-pid", 0, "@"},
|
|
|
|
{ 0, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
/* A structure with module options. This is not a static variable
|
|
|
|
because if we are not build as a standalone binary, each thread
|
|
|
|
using this module needs to handle its own values. */
|
|
|
|
struct my_opt_s
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
int quiet;
|
|
|
|
int verbose;
|
|
|
|
struct timeval timeout; /* Timeout for the LDAP search functions. */
|
|
|
|
unsigned int alarm_timeout; /* And for the alarm based timeout. */
|
|
|
|
int multi;
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
estream_t outstream; /* Send output to thsi stream. */
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
/* Note that we can't use const for the strings because ldap_* are
|
|
|
|
not defined that way. */
|
|
|
|
char *proxy; /* Host and Port override. */
|
|
|
|
char *user; /* Authentication user. */
|
|
|
|
char *pass; /* Authentication password. */
|
|
|
|
char *host; /* Override host. */
|
|
|
|
int port; /* Override port. */
|
|
|
|
char *dn; /* Override DN. */
|
|
|
|
char *filter;/* Override filter. */
|
|
|
|
char *attr; /* Override attribute. */
|
2010-07-26 16:01:32 +02:00
|
|
|
};
|
2010-07-26 16:27:59 +02:00
|
|
|
typedef struct my_opt_s *my_opt_t;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Prototypes. */
|
2010-08-13 12:02:12 +02:00
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2010-06-09 18:53:51 +02:00
|
|
|
static void catch_alarm (int dummy);
|
2010-08-13 12:02:12 +02:00
|
|
|
#endif
|
2010-07-26 16:01:32 +02:00
|
|
|
static int process_url (my_opt_t myopt, const char *url);
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Function called by argparse.c to display information. */
|
2010-08-03 16:06:42 +02:00
|
|
|
#ifdef USE_LDAPWRAPPER
|
2010-06-09 18:53:51 +02:00
|
|
|
static const char *
|
|
|
|
my_strusage (int level)
|
|
|
|
{
|
|
|
|
const char *p;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
switch(level)
|
|
|
|
{
|
|
|
|
case 11: p = "dirmngr_ldap (GnuPG)";
|
|
|
|
break;
|
|
|
|
case 13: p = VERSION; break;
|
|
|
|
case 17: p = PRINTABLE_OS_NAME; break;
|
|
|
|
case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
|
|
|
|
case 49: p = PACKAGE_BUGREPORT; break;
|
|
|
|
case 1:
|
|
|
|
case 40: p =
|
|
|
|
_("Usage: dirmngr_ldap [options] [URL] (-h for help)\n");
|
|
|
|
break;
|
|
|
|
case 41: p =
|
|
|
|
_("Syntax: dirmngr_ldap [options] [URL]\n"
|
|
|
|
"Internal LDAP helper for Dirmngr.\n"
|
|
|
|
"Interface and options may change without notice.\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: p = NULL;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2010-08-02 20:54:53 +02:00
|
|
|
#endif /*!USE_LDAPWRAPPER*/
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2010-07-26 16:01:32 +02:00
|
|
|
#ifdef USE_LDAPWRAPPER
|
|
|
|
main (int argc, char **argv)
|
|
|
|
#else
|
|
|
|
ldap_wrapper_main (char **argv, estream_t outstream)
|
|
|
|
#endif
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
2010-07-26 16:01:32 +02:00
|
|
|
#ifndef USE_LDAPWRAPPER
|
|
|
|
int argc;
|
|
|
|
#endif
|
2010-06-09 18:53:51 +02:00
|
|
|
ARGPARSE_ARGS pargs;
|
|
|
|
int any_err = 0;
|
|
|
|
char *p;
|
|
|
|
int only_search_timeout = 0;
|
2010-07-26 16:01:32 +02:00
|
|
|
struct my_opt_s my_opt_buffer;
|
|
|
|
my_opt_t myopt = &my_opt_buffer;
|
2010-08-13 12:02:12 +02:00
|
|
|
char *malloced_buffer1 = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
memset (&my_opt_buffer, 0, sizeof my_opt_buffer);
|
2010-06-09 18:53:51 +02:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
#ifdef USE_LDAPWRAPPER
|
2010-06-09 18:53:51 +02:00
|
|
|
set_strusage (my_strusage);
|
2011-02-04 12:57:53 +01:00
|
|
|
log_set_prefix ("dirmngr_ldap", JNLIB_LOG_WITH_PREFIX);
|
|
|
|
|
2010-07-24 15:33:03 +02:00
|
|
|
/* Setup I18N and common subsystems. */
|
2010-06-28 15:37:44 +02:00
|
|
|
i18n_init();
|
2010-06-09 18:53:51 +02:00
|
|
|
|
2010-07-24 15:33:03 +02:00
|
|
|
init_common_subsystems (&argc, &argv);
|
|
|
|
|
|
|
|
es_set_binary (es_stdout);
|
2010-07-26 16:01:32 +02:00
|
|
|
myopt->outstream = es_stdout;
|
|
|
|
#else /*!USE_LDAPWRAPPER*/
|
|
|
|
myopt->outstream = outstream;
|
|
|
|
for (argc=0; argv[argc]; argc++)
|
|
|
|
;
|
|
|
|
#endif /*!USE_LDAPWRAPPER*/
|
2010-07-24 15:33:03 +02:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
/* LDAP defaults */
|
2010-07-26 16:01:32 +02:00
|
|
|
myopt->timeout.tv_sec = DEFAULT_LDAP_TIMEOUT;
|
|
|
|
myopt->timeout.tv_usec = 0;
|
|
|
|
myopt->alarm_timeout = 0;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
/* Parse the command line. */
|
|
|
|
pargs.argc = &argc;
|
|
|
|
pargs.argv = &argv;
|
|
|
|
pargs.flags= 1; /* Do not remove the args. */
|
|
|
|
while (arg_parse (&pargs, opts) )
|
|
|
|
{
|
|
|
|
switch (pargs.r_opt)
|
|
|
|
{
|
2010-07-26 16:01:32 +02:00
|
|
|
case oVerbose: myopt->verbose++; break;
|
|
|
|
case oQuiet: myopt->quiet++; break;
|
2011-02-04 12:57:53 +01:00
|
|
|
case oTimeout:
|
|
|
|
myopt->timeout.tv_sec = pargs.r.ret_int;
|
2010-07-26 16:01:32 +02:00
|
|
|
myopt->timeout.tv_usec = 0;
|
|
|
|
myopt->alarm_timeout = pargs.r.ret_int;
|
2010-06-09 18:53:51 +02:00
|
|
|
break;
|
|
|
|
case oOnlySearchTimeout: only_search_timeout = 1; break;
|
2010-07-26 16:01:32 +02:00
|
|
|
case oMulti: myopt->multi = 1; break;
|
|
|
|
case oUser: myopt->user = pargs.r.ret_str; break;
|
|
|
|
case oPass: myopt->pass = pargs.r.ret_str; break;
|
2010-06-09 18:53:51 +02:00
|
|
|
case oEnvPass:
|
2010-07-26 16:01:32 +02:00
|
|
|
myopt->pass = getenv ("DIRMNGR_LDAP_PASS");
|
2010-06-09 18:53:51 +02:00
|
|
|
break;
|
2010-07-26 16:01:32 +02:00
|
|
|
case oProxy: myopt->proxy = pargs.r.ret_str; break;
|
|
|
|
case oHost: myopt->host = pargs.r.ret_str; break;
|
|
|
|
case oPort: myopt->port = pargs.r.ret_int; break;
|
|
|
|
case oDN: myopt->dn = pargs.r.ret_str; break;
|
|
|
|
case oFilter: myopt->filter = pargs.r.ret_str; break;
|
|
|
|
case oAttr: myopt->attr = pargs.r.ret_str; break;
|
2010-06-09 18:53:51 +02:00
|
|
|
case oLogWithPID:
|
|
|
|
{
|
|
|
|
unsigned int oldflags;
|
|
|
|
log_get_prefix (&oldflags);
|
|
|
|
log_set_prefix (NULL, oldflags | JNLIB_LOG_WITH_PID);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
default :
|
|
|
|
#ifdef USE_LDAPWRAPPER
|
|
|
|
pargs.err = ARGPARSE_PRINT_ERROR;
|
|
|
|
#else
|
|
|
|
pargs.err = ARGPARSE_PRINT_WARNING; /* No exit() please. */
|
|
|
|
#endif
|
|
|
|
break;
|
2010-06-09 18:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (only_search_timeout)
|
2010-07-26 16:01:32 +02:00
|
|
|
myopt->alarm_timeout = 0;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->proxy)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
2010-08-13 12:02:12 +02:00
|
|
|
malloced_buffer1 = xtrystrdup (myopt->proxy);
|
|
|
|
if (!malloced_buffer1)
|
|
|
|
{
|
|
|
|
log_error ("error copying string: %s\n", strerror (errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
myopt->host = malloced_buffer1;
|
2010-07-26 16:01:32 +02:00
|
|
|
p = strchr (myopt->host, ':');
|
2010-06-09 18:53:51 +02:00
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p++ = 0;
|
2010-07-26 16:01:32 +02:00
|
|
|
myopt->port = atoi (p);
|
2010-06-09 18:53:51 +02:00
|
|
|
}
|
2010-07-26 16:01:32 +02:00
|
|
|
if (!myopt->port)
|
|
|
|
myopt->port = 389; /* make sure ports gets overridden. */
|
2010-06-09 18:53:51 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->port < 0 || myopt->port > 65535)
|
|
|
|
log_error (_("invalid port number %d\n"), myopt->port);
|
2010-06-09 18:53:51 +02:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
#ifdef USE_LDAPWRAPPER
|
2010-06-09 18:53:51 +02:00
|
|
|
if (log_get_errorcount (0))
|
|
|
|
exit (2);
|
|
|
|
if (argc < 1)
|
|
|
|
usage (1);
|
2010-07-26 16:01:32 +02:00
|
|
|
#else
|
|
|
|
/* All passed arguments should be fine in this case. */
|
|
|
|
assert (argc);
|
|
|
|
#endif
|
2010-06-09 18:53:51 +02:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
#ifdef USE_LDAPWRAPPER
|
|
|
|
if (myopt->alarm_timeout)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
|
|
|
# if defined(HAVE_SIGACTION) && defined(HAVE_STRUCT_SIGACTION)
|
|
|
|
struct sigaction act;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
act.sa_handler = catch_alarm;
|
|
|
|
sigemptyset (&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
if (sigaction (SIGALRM,&act,NULL))
|
2011-02-04 12:57:53 +01:00
|
|
|
# else
|
2010-06-09 18:53:51 +02:00
|
|
|
if (signal (SIGALRM, catch_alarm) == SIG_ERR)
|
|
|
|
# endif
|
|
|
|
log_fatal ("unable to register timeout handler\n");
|
|
|
|
#endif
|
|
|
|
}
|
2010-07-26 16:01:32 +02:00
|
|
|
#endif /*USE_LDAPWRAPPER*/
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
for (; argc; argc--, argv++)
|
2010-07-26 16:01:32 +02:00
|
|
|
if (process_url (myopt, *argv))
|
2010-06-09 18:53:51 +02:00
|
|
|
any_err = 1;
|
|
|
|
|
2010-08-13 12:02:12 +02:00
|
|
|
xfree (malloced_buffer1);
|
2010-06-09 18:53:51 +02:00
|
|
|
return any_err;
|
|
|
|
}
|
|
|
|
|
2010-08-13 12:02:12 +02:00
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2010-06-09 18:53:51 +02:00
|
|
|
static void
|
|
|
|
catch_alarm (int dummy)
|
|
|
|
{
|
|
|
|
(void)dummy;
|
|
|
|
_exit (10);
|
|
|
|
}
|
2010-08-13 12:02:12 +02:00
|
|
|
#endif
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
static void
|
2010-07-26 16:01:32 +02:00
|
|
|
set_timeout (my_opt_t myopt)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
2010-08-02 20:54:53 +02:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2010-06-09 18:53:51 +02:00
|
|
|
/* FIXME for W32. */
|
2010-08-02 20:54:53 +02:00
|
|
|
(void)myopt;
|
|
|
|
#else
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->alarm_timeout)
|
|
|
|
alarm (myopt->alarm_timeout);
|
2010-06-09 18:53:51 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper for fetch_ldap(). */
|
|
|
|
static int
|
2010-07-26 16:01:32 +02:00
|
|
|
print_ldap_entries (my_opt_t myopt, LDAP *ld, LDAPMessage *msg, char *want_attr)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
LDAPMessage *item;
|
|
|
|
int any = 0;
|
|
|
|
|
2010-08-02 20:54:53 +02:00
|
|
|
for (pth_enter (), item = ldap_first_entry (ld, msg), pth_leave ();
|
|
|
|
item;
|
|
|
|
pth_enter (), item = ldap_next_entry (ld, item), pth_leave ())
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
BerElement *berctx;
|
|
|
|
char *attr;
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->verbose > 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
log_info (_("scanning result for attribute `%s'\n"),
|
|
|
|
want_attr? want_attr : "[all]");
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->multi)
|
2010-06-09 18:53:51 +02:00
|
|
|
{ /* Write item marker. */
|
2010-07-26 16:27:59 +02:00
|
|
|
if (es_fwrite ("I\0\0\0\0", 5, 1, myopt->outstream) != 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
log_error (_("error writing to stdout: %s\n"),
|
|
|
|
strerror (errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-08-13 12:02:12 +02:00
|
|
|
for (pth_enter (), attr = my_ldap_first_attribute (ld, item, &berctx),
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_leave ();
|
|
|
|
attr;
|
2010-08-13 12:02:12 +02:00
|
|
|
pth_enter (), attr = my_ldap_next_attribute (ld, item, berctx),
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_leave ())
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
struct berval **values;
|
|
|
|
int idx;
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->verbose > 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
log_info (_(" available attribute `%s'\n"), attr);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
set_timeout (myopt);
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
/* I case we want only one attribute we do a case
|
|
|
|
insensitive compare without the optional extension
|
|
|
|
(i.e. ";binary"). Case insensitive is not really correct
|
|
|
|
but the best we can do. */
|
|
|
|
if (want_attr)
|
|
|
|
{
|
|
|
|
char *cp1, *cp2;
|
|
|
|
int cmpres;
|
|
|
|
|
|
|
|
cp1 = strchr (want_attr, ';');
|
|
|
|
if (cp1)
|
|
|
|
*cp1 = 0;
|
|
|
|
cp2 = strchr (attr, ';');
|
|
|
|
if (cp2)
|
|
|
|
*cp2 = 0;
|
|
|
|
cmpres = ascii_strcasecmp (want_attr, attr);
|
|
|
|
if (cp1)
|
|
|
|
*cp1 = ';';
|
|
|
|
if (cp2)
|
|
|
|
*cp2 = ';';
|
|
|
|
if (cmpres)
|
|
|
|
{
|
2010-08-13 12:02:12 +02:00
|
|
|
my_ldap_free_attr (attr);
|
2010-06-09 18:53:51 +02:00
|
|
|
continue; /* Not found: Try next attribute. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_enter ();
|
2010-08-13 12:02:12 +02:00
|
|
|
values = my_ldap_get_values_len (ld, item, attr);
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_leave ();
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
if (!values)
|
|
|
|
{
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->verbose)
|
2010-06-09 18:53:51 +02:00
|
|
|
log_info (_("attribute `%s' not found\n"), attr);
|
2010-08-13 12:02:12 +02:00
|
|
|
my_ldap_free_attr (attr);
|
2010-06-09 18:53:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->verbose)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
log_info (_("found attribute `%s'\n"), attr);
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->verbose > 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
for (idx=0; values[idx]; idx++)
|
|
|
|
log_info (" length[%d]=%d\n",
|
|
|
|
idx, (int)values[0]->bv_len);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
}
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->multi)
|
2010-06-09 18:53:51 +02:00
|
|
|
{ /* Write attribute marker. */
|
|
|
|
unsigned char tmp[5];
|
|
|
|
size_t n = strlen (attr);
|
|
|
|
|
|
|
|
tmp[0] = 'A';
|
|
|
|
tmp[1] = (n >> 24);
|
|
|
|
tmp[2] = (n >> 16);
|
|
|
|
tmp[3] = (n >> 8);
|
|
|
|
tmp[4] = (n);
|
2011-02-04 12:57:53 +01:00
|
|
|
if (es_fwrite (tmp, 5, 1, myopt->outstream) != 1
|
2010-07-26 16:27:59 +02:00
|
|
|
|| es_fwrite (attr, n, 1, myopt->outstream) != 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
log_error (_("error writing to stdout: %s\n"),
|
|
|
|
strerror (errno));
|
|
|
|
ldap_value_free_len (values);
|
2010-08-13 12:02:12 +02:00
|
|
|
my_ldap_free_attr (attr);
|
2010-06-09 18:53:51 +02:00
|
|
|
ber_free (berctx, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (idx=0; values[idx]; idx++)
|
|
|
|
{
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->multi)
|
2010-06-09 18:53:51 +02:00
|
|
|
{ /* Write value marker. */
|
|
|
|
unsigned char tmp[5];
|
|
|
|
size_t n = values[0]->bv_len;
|
|
|
|
|
|
|
|
tmp[0] = 'V';
|
|
|
|
tmp[1] = (n >> 24);
|
|
|
|
tmp[2] = (n >> 16);
|
|
|
|
tmp[3] = (n >> 8);
|
|
|
|
tmp[4] = (n);
|
|
|
|
|
2010-07-26 16:27:59 +02:00
|
|
|
if (es_fwrite (tmp, 5, 1, myopt->outstream) != 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
log_error (_("error writing to stdout: %s\n"),
|
|
|
|
strerror (errno));
|
|
|
|
ldap_value_free_len (values);
|
2010-08-13 12:02:12 +02:00
|
|
|
my_ldap_free_attr (attr);
|
2010-06-09 18:53:51 +02:00
|
|
|
ber_free (berctx, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 20:54:53 +02:00
|
|
|
|
2010-07-16 15:19:45 +02:00
|
|
|
if (es_fwrite (values[0]->bv_val, values[0]->bv_len,
|
2010-07-26 16:27:59 +02:00
|
|
|
1, myopt->outstream) != 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
log_error (_("error writing to stdout: %s\n"),
|
|
|
|
strerror (errno));
|
|
|
|
ldap_value_free_len (values);
|
2010-08-13 12:02:12 +02:00
|
|
|
my_ldap_free_attr (attr);
|
2010-06-09 18:53:51 +02:00
|
|
|
ber_free (berctx, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-02 20:54:53 +02:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
any = 1;
|
2010-07-26 16:01:32 +02:00
|
|
|
if (!myopt->multi)
|
2010-06-09 18:53:51 +02:00
|
|
|
break; /* Print only the first value. */
|
|
|
|
}
|
|
|
|
ldap_value_free_len (values);
|
2010-08-13 12:02:12 +02:00
|
|
|
my_ldap_free_attr (attr);
|
2010-07-26 16:01:32 +02:00
|
|
|
if (want_attr || !myopt->multi)
|
2010-06-09 18:53:51 +02:00
|
|
|
break; /* We only want to return the first attribute. */
|
|
|
|
}
|
|
|
|
ber_free (berctx, 0);
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|
2010-06-09 18:53:51 +02:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->verbose > 1 && any)
|
2010-06-09 18:53:51 +02:00
|
|
|
log_info ("result has been printed\n");
|
|
|
|
|
|
|
|
return any?0:-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper for the URL based LDAP query. */
|
|
|
|
static int
|
2010-07-26 16:01:32 +02:00
|
|
|
fetch_ldap (my_opt_t myopt, const char *url, const LDAPURLDesc *ludp)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
LDAP *ld;
|
|
|
|
LDAPMessage *msg;
|
|
|
|
int rc = 0;
|
|
|
|
char *host, *dn, *filter, *attrs[2], *attr;
|
|
|
|
int port;
|
2010-08-02 20:54:53 +02:00
|
|
|
int ret;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
host = myopt->host? myopt->host : ludp->lud_host;
|
|
|
|
port = myopt->port? myopt->port : ludp->lud_port;
|
|
|
|
dn = myopt->dn? myopt->dn : ludp->lud_dn;
|
|
|
|
filter = myopt->filter? myopt->filter : ludp->lud_filter;
|
|
|
|
attrs[0] = myopt->attr? myopt->attr : ludp->lud_attrs? ludp->lud_attrs[0]:NULL;
|
2010-06-09 18:53:51 +02:00
|
|
|
attrs[1] = NULL;
|
|
|
|
attr = attrs[0];
|
|
|
|
|
|
|
|
if (!port)
|
|
|
|
port = (ludp->lud_scheme && !strcmp (ludp->lud_scheme, "ldaps"))? 636:389;
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->verbose)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
log_info (_("processing url `%s'\n"), url);
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->user)
|
|
|
|
log_info (_(" user `%s'\n"), myopt->user);
|
|
|
|
if (myopt->pass)
|
|
|
|
log_info (_(" pass `%s'\n"), *myopt->pass?"*****":"");
|
2010-06-09 18:53:51 +02:00
|
|
|
if (host)
|
|
|
|
log_info (_(" host `%s'\n"), host);
|
|
|
|
log_info (_(" port %d\n"), port);
|
|
|
|
if (dn)
|
|
|
|
log_info (_(" DN `%s'\n"), dn);
|
|
|
|
if (filter)
|
|
|
|
log_info (_(" filter `%s'\n"), filter);
|
2010-07-26 16:01:32 +02:00
|
|
|
if (myopt->multi && !myopt->attr && ludp->lud_attrs)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; ludp->lud_attrs[i]; i++)
|
|
|
|
log_info (_(" attr `%s'\n"), ludp->lud_attrs[i]);
|
|
|
|
}
|
|
|
|
else if (attr)
|
|
|
|
log_info (_(" attr `%s'\n"), attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!host || !*host)
|
|
|
|
{
|
|
|
|
log_error (_("no host name in `%s'\n"), url);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-26 16:01:32 +02:00
|
|
|
if (!myopt->multi && !attr)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
log_error (_("no attribute given for query `%s'\n"), url);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
if (!myopt->multi && !myopt->attr
|
2010-06-09 18:53:51 +02:00
|
|
|
&& ludp->lud_attrs && ludp->lud_attrs[0] && ludp->lud_attrs[1])
|
|
|
|
log_info (_("WARNING: using first attribute only\n"));
|
|
|
|
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
set_timeout (myopt);
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_enter ();
|
2010-08-13 12:02:12 +02:00
|
|
|
ld = my_ldap_init (host, port);
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_leave ();
|
2010-06-09 18:53:51 +02:00
|
|
|
if (!ld)
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
log_error (_("LDAP init to `%s:%d' failed: %s\n"),
|
2010-06-09 18:53:51 +02:00
|
|
|
host, port, strerror (errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_enter ();
|
2010-08-13 12:02:12 +02:00
|
|
|
/* Fixme: Can we use MYOPT->user or is it shared with other theeads?. */
|
|
|
|
ret = my_ldap_simple_bind_s (ld, myopt->user, myopt->pass);
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_leave ();
|
|
|
|
if (ret)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
log_error (_("binding to `%s:%d' failed: %s\n"),
|
2010-06-09 18:53:51 +02:00
|
|
|
host, port, strerror (errno));
|
2010-08-13 12:02:12 +02:00
|
|
|
ldap_unbind (ld);
|
2010-06-09 18:53:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
set_timeout (myopt);
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_enter ();
|
2010-08-13 12:02:12 +02:00
|
|
|
rc = my_ldap_search_st (ld, dn, ludp->lud_scope, filter,
|
|
|
|
myopt->multi && !myopt->attr && ludp->lud_attrs?
|
|
|
|
ludp->lud_attrs:attrs,
|
|
|
|
0,
|
|
|
|
&myopt->timeout, &msg);
|
2010-08-02 20:54:53 +02:00
|
|
|
pth_leave ();
|
2010-07-26 16:01:32 +02:00
|
|
|
if (rc == LDAP_SIZELIMIT_EXCEEDED && myopt->multi)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
2010-07-26 16:27:59 +02:00
|
|
|
if (es_fwrite ("E\0\0\0\x09truncated", 14, 1, myopt->outstream) != 1)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
2010-07-26 16:01:32 +02:00
|
|
|
log_error (_("error writing to stdout: %s\n"), strerror (errno));
|
2010-06-09 18:53:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rc)
|
|
|
|
{
|
2010-08-13 12:02:12 +02:00
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
log_error ("searching `%s' failed: %d\n", url, rc);
|
|
|
|
#else
|
2011-02-04 12:57:53 +01:00
|
|
|
log_error (_("searching `%s' failed: %s\n"),
|
2010-06-09 18:53:51 +02:00
|
|
|
url, ldap_err2string (rc));
|
2010-08-13 12:02:12 +02:00
|
|
|
#endif
|
2010-06-09 18:53:51 +02:00
|
|
|
if (rc != LDAP_NO_SUCH_OBJECT)
|
|
|
|
{
|
|
|
|
/* FIXME: Need deinit (ld)? */
|
|
|
|
/* Hmmm: Do we need to released MSG in case of an error? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
rc = print_ldap_entries (myopt, ld, msg, myopt->multi? NULL:attr);
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
ldap_msgfree (msg);
|
2010-08-13 12:02:12 +02:00
|
|
|
ldap_unbind (ld);
|
2010-06-09 18:53:51 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Main processing. Take the URL and run the LDAP query. The result
|
|
|
|
is printed to stdout, errors are logged to the log stream. */
|
|
|
|
static int
|
2010-07-26 16:01:32 +02:00
|
|
|
process_url (my_opt_t myopt, const char *url)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
LDAPURLDesc *ludp = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
if (!ldap_is_ldap_url (url))
|
|
|
|
{
|
|
|
|
log_error (_("`%s' is not an LDAP URL\n"), url);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ldap_url_parse (url, &ludp))
|
|
|
|
{
|
|
|
|
log_error (_("`%s' is an invalid LDAP URL\n"), url);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-26 16:01:32 +02:00
|
|
|
rc = fetch_ldap (myopt, url, ludp);
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
ldap_free_urldesc (ludp);
|
|
|
|
return rc;
|
|
|
|
}
|