2010-06-09 16:53:51 +00:00
|
|
|
/* dirmngr-ldap.c - The LDAP helper for dirmngr.
|
2021-06-08 08:46:59 +02:00
|
|
|
* Copyright (C) 2004, 2021 g10 Code GmbH
|
2010-08-13 10:02:12 +00:00
|
|
|
* Copyright (C) 2010 Free Software Foundation, Inc.
|
2010-06-09 16:53:51 +00:00
|
|
|
*
|
2010-08-13 10:02:12 +00:00
|
|
|
* This file is part of GnuPG.
|
2010-06-09 16:53:51 +00:00
|
|
|
*
|
2010-08-13 10:02:12 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
2010-06-09 16:53:51 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2010-08-13 10:02:12 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-06-09 16:53:51 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2010-08-13 10:02:12 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
2010-06-09 16:53:51 +00: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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2020-02-21 21:45:13 +01:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2010-06-09 16:53:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2020-02-21 21:45:13 +01:00
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2010-07-16 13:19:45 +00:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
2010-06-09 16:53:51 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2014-03-07 09:46:44 +01:00
|
|
|
# include <winsock2.h>
|
|
|
|
# include <winldap.h>
|
|
|
|
# include <winber.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include "ldap-url.h"
|
2010-06-09 16:53:51 +00:00
|
|
|
#else
|
2014-03-07 09:46:44 +01:00
|
|
|
/* For OpenLDAP, to enable the API that we're using. */
|
|
|
|
# define LDAP_DEPRECATED 1
|
|
|
|
# include <ldap.h>
|
2010-06-09 16:53:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-08-26 17:47:22 +02:00
|
|
|
#include <gpg-error.h>
|
2010-06-09 16:53:51 +00:00
|
|
|
#include "../common/logging.h"
|
|
|
|
#include "../common/stringhelp.h"
|
|
|
|
#include "../common/mischelp.h"
|
|
|
|
#include "../common/strlist.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/util.h"
|
2012-02-06 20:50:47 +01:00
|
|
|
#include "../common/init.h"
|
2021-06-08 08:46:59 +02:00
|
|
|
#include "ldap-misc.h"
|
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2019-11-11 20:49:04 +01:00
|
|
|
/* There is no need for the npth_unprotect and leave functions here;
|
|
|
|
* thus we redefine them to nops. We keep them in the code just for
|
|
|
|
* the case we ever want to reuse parts of the code in npth programs. */
|
2012-01-24 17:37:01 +01:00
|
|
|
static void npth_unprotect (void) { }
|
|
|
|
static void npth_protect (void) { }
|
2010-07-26 14:01:32 +00:00
|
|
|
|
2010-08-13 10:02:12 +00:00
|
|
|
|
2014-03-07 09:46:44 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
typedef LDAP_TIMEVAL my_ldap_timeval_t;
|
|
|
|
#else
|
|
|
|
typedef struct timeval my_ldap_timeval_t;
|
|
|
|
#endif
|
|
|
|
|
2017-11-07 10:02:53 +01:00
|
|
|
#define DEFAULT_LDAP_TIMEOUT 15 /* Arbitrary long timeout. */
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Constants for the options. */
|
2011-02-04 12:57:53 +01:00
|
|
|
enum
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
|
|
|
oQuiet = 'q',
|
|
|
|
oVerbose = 'v',
|
|
|
|
|
|
|
|
oTimeout = 500,
|
|
|
|
oMulti,
|
|
|
|
oProxy,
|
|
|
|
oHost,
|
|
|
|
oPort,
|
|
|
|
oUser,
|
|
|
|
oPass,
|
|
|
|
oEnvPass,
|
2021-06-08 08:46:59 +02:00
|
|
|
oBase,
|
2010-06-09 16:53:51 +00:00
|
|
|
oAttr,
|
2021-06-08 08:46:59 +02:00
|
|
|
oStartTLS,
|
|
|
|
oLdapTLS,
|
|
|
|
oNtds,
|
2022-09-28 09:43:25 +02:00
|
|
|
oARecOnly,
|
2010-06-09 16:53:51 +00:00
|
|
|
oOnlySearchTimeout,
|
|
|
|
oLogWithPID
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* The list of options as used by the argparse.c code. */
|
2020-02-21 21:45:13 +01:00
|
|
|
static gpgrt_opt_t opts[] = {
|
2021-06-08 08:46:59 +02:00
|
|
|
{ oVerbose, "verbose", 0, "verbose" },
|
|
|
|
{ oQuiet, "quiet", 0, "be somewhat more quiet" },
|
|
|
|
{ oTimeout, "timeout", 1, "|N|set LDAP timeout to N seconds"},
|
|
|
|
{ oMulti, "multi", 0, "return all values in"
|
|
|
|
" a record oriented format"},
|
2010-06-09 16:53:51 +00:00
|
|
|
{ oProxy, "proxy", 2,
|
2021-06-08 08:46:59 +02:00
|
|
|
"|NAME|ignore host part and connect through NAME"},
|
|
|
|
{ oStartTLS, "starttls", 0, "use STARTLS for the connection"},
|
|
|
|
{ oLdapTLS, "ldaptls", 0, "use a TLS for the connection"},
|
|
|
|
{ oNtds, "ntds", 0, "authenticate using AD"},
|
2022-09-28 09:43:25 +02:00
|
|
|
{ oARecOnly, "areconly", 0, "do only an A record lookup"},
|
2021-06-08 08:46:59 +02:00
|
|
|
{ oHost, "host", 2, "|NAME|connect to host NAME"},
|
|
|
|
{ oPort, "port", 1, "|N|connect to port N"},
|
|
|
|
{ oUser, "user", 2, "|NAME|use NAME for authentication"},
|
|
|
|
{ oPass, "pass", 2, "|PASS|use password PASS"
|
|
|
|
" for authentication"},
|
|
|
|
{ oEnvPass, "env-pass", 0, "take password from $DIRMNGR_LDAP_PASS"},
|
|
|
|
{ oBase, "base", 2, "|DN|Start query at DN"},
|
|
|
|
{ oAttr, "attr", 2, "|STRING|return the attribute STRING"},
|
2010-06-09 16:53:51 +00:00
|
|
|
{ oOnlySearchTimeout, "only-search-timeout", 0, "@"},
|
|
|
|
{ oLogWithPID,"log-with-pid", 0, "@"},
|
2017-07-19 13:41:18 +09:00
|
|
|
ARGPARSE_end ()
|
2010-06-09 16:53:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
/* A structure with module options. */
|
|
|
|
static struct
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
|
|
|
int quiet;
|
|
|
|
int verbose;
|
2014-03-07 09:46:44 +01:00
|
|
|
my_ldap_timeval_t timeout;/* Timeout for the LDAP search functions. */
|
2010-06-09 16:53:51 +00:00
|
|
|
unsigned int alarm_timeout; /* And for the alarm based timeout. */
|
|
|
|
int multi;
|
2021-06-08 08:46:59 +02:00
|
|
|
int starttls;
|
|
|
|
int ldaptls;
|
|
|
|
int ntds;
|
2022-09-28 09:43:25 +02:00
|
|
|
int areconly;
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2015-11-16 12:41:46 +01:00
|
|
|
estream_t outstream; /* Send output to this stream. */
|
2010-07-26 14:01:32 +00:00
|
|
|
|
2010-06-09 16:53:51 +00: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. */
|
2021-06-08 08:46:59 +02:00
|
|
|
int port; /* Override port. */
|
|
|
|
char *base; /* Override DN. */
|
2010-06-09 16:53:51 +00:00
|
|
|
char *attr; /* Override attribute. */
|
2021-06-08 08:46:59 +02:00
|
|
|
} opt;
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Prototypes. */
|
2010-08-13 10:02:12 +00:00
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2010-06-09 16:53:51 +00:00
|
|
|
static void catch_alarm (int dummy);
|
2010-08-13 10:02:12 +00:00
|
|
|
#endif
|
2021-06-08 08:46:59 +02:00
|
|
|
static gpg_error_t connect_ldap (LDAP **r_ld);
|
|
|
|
static gpg_error_t process_filter (LDAP *ld, const char *string);
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Function called by argparse.c to display information. */
|
|
|
|
static const char *
|
|
|
|
my_strusage (int level)
|
|
|
|
{
|
|
|
|
const char *p;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2020-02-21 21:45:13 +01:00
|
|
|
switch (level)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2020-02-21 21:45:13 +01:00
|
|
|
case 9: p = "GPL-3.0-or-later"; break;
|
2013-11-18 14:09:47 +01:00
|
|
|
case 11: p = "dirmngr_ldap (@GNUPG@)";
|
2010-06-09 16:53:51 +00:00
|
|
|
break;
|
|
|
|
case 13: p = VERSION; break;
|
2020-02-21 21:45:13 +01:00
|
|
|
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
|
2010-06-09 16:53:51 +00:00
|
|
|
case 17: p = PRINTABLE_OS_NAME; break;
|
2021-06-08 08:46:59 +02:00
|
|
|
case 19: p = "Please report bugs to <@EMAIL@>.\n"; break;
|
2010-06-09 16:53:51 +00:00
|
|
|
case 49: p = PACKAGE_BUGREPORT; break;
|
|
|
|
case 1:
|
|
|
|
case 40: p =
|
2021-06-08 08:46:59 +02:00
|
|
|
"Usage: dirmngr_ldap [options] filters (-h for help)\n";
|
2010-06-09 16:53:51 +00:00
|
|
|
break;
|
|
|
|
case 41: p =
|
2021-06-08 08:46:59 +02:00
|
|
|
("Syntax: dirmngr_ldap [options] filters\n"
|
2012-08-22 12:54:38 -04:00
|
|
|
"Internal LDAP helper for Dirmngr\n"
|
|
|
|
"Interface and options may change without notice\n");
|
2010-06-09 16:53:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: p = NULL;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2010-07-26 14:01:32 +00:00
|
|
|
main (int argc, char **argv)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2020-02-21 21:45:13 +01:00
|
|
|
gpgrt_argparse_t pargs;
|
2010-06-09 16:53:51 +00:00
|
|
|
int any_err = 0;
|
|
|
|
char *p;
|
|
|
|
int only_search_timeout = 0;
|
2010-08-13 10:02:12 +00:00
|
|
|
char *malloced_buffer1 = NULL;
|
2021-06-08 08:46:59 +02:00
|
|
|
LDAP *ld;
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2015-01-28 19:57:22 +01:00
|
|
|
early_system_init ();
|
|
|
|
|
2020-02-21 21:45:13 +01:00
|
|
|
gpgrt_set_strusage (my_strusage);
|
2015-04-24 15:49:18 +02:00
|
|
|
log_set_prefix ("dirmngr_ldap", GPGRT_LOG_WITH_PREFIX);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-07-24 13:33:03 +00:00
|
|
|
init_common_subsystems (&argc, &argv);
|
|
|
|
|
|
|
|
es_set_binary (es_stdout);
|
2021-06-08 08:46:59 +02:00
|
|
|
opt.outstream = es_stdout;
|
2010-07-24 13:33:03 +00:00
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
/* LDAP defaults */
|
2021-06-08 08:46:59 +02:00
|
|
|
opt.timeout.tv_sec = DEFAULT_LDAP_TIMEOUT;
|
|
|
|
opt.timeout.tv_usec = 0;
|
|
|
|
opt.alarm_timeout = 0;
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
/* Parse the command line. */
|
|
|
|
pargs.argc = &argc;
|
|
|
|
pargs.argv = &argv;
|
2020-02-21 21:45:13 +01:00
|
|
|
pargs.flags= ARGPARSE_FLAG_KEEP;
|
|
|
|
while (gpgrt_argparse (NULL, &pargs, opts))
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
|
|
|
switch (pargs.r_opt)
|
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
case oVerbose: opt.verbose++; break;
|
|
|
|
case oQuiet: opt.quiet++; break;
|
2011-02-04 12:57:53 +01:00
|
|
|
case oTimeout:
|
2021-06-08 08:46:59 +02:00
|
|
|
opt.timeout.tv_sec = pargs.r.ret_int;
|
|
|
|
opt.timeout.tv_usec = 0;
|
|
|
|
opt.alarm_timeout = pargs.r.ret_int;
|
2010-06-09 16:53:51 +00:00
|
|
|
break;
|
|
|
|
case oOnlySearchTimeout: only_search_timeout = 1; break;
|
2021-06-08 08:46:59 +02:00
|
|
|
case oStartTLS: opt.starttls = 1; opt.ldaptls = 0; break;
|
|
|
|
case oLdapTLS: opt.starttls = 0; opt.ldaptls = 1; break;
|
|
|
|
case oNtds: opt.ntds = 1; break;
|
2022-09-28 09:43:25 +02:00
|
|
|
case oARecOnly: opt.areconly = 1; break;
|
2021-06-08 08:46:59 +02:00
|
|
|
case oMulti: opt.multi = 1; break;
|
|
|
|
case oUser: opt.user = pargs.r.ret_str; break;
|
|
|
|
case oPass: opt.pass = pargs.r.ret_str; break;
|
2010-06-09 16:53:51 +00:00
|
|
|
case oEnvPass:
|
2021-06-08 08:46:59 +02:00
|
|
|
opt.pass = getenv ("DIRMNGR_LDAP_PASS");
|
2010-06-09 16:53:51 +00:00
|
|
|
break;
|
2021-06-08 08:46:59 +02:00
|
|
|
case oProxy: opt.proxy = pargs.r.ret_str; break;
|
|
|
|
case oHost: opt.host = pargs.r.ret_str; break;
|
|
|
|
case oPort: opt.port = pargs.r.ret_int; break;
|
|
|
|
case oBase: opt.base = pargs.r.ret_str; break;
|
|
|
|
case oAttr: opt.attr = pargs.r.ret_str; break;
|
2010-06-09 16:53:51 +00:00
|
|
|
case oLogWithPID:
|
|
|
|
{
|
|
|
|
unsigned int oldflags;
|
|
|
|
log_get_prefix (&oldflags);
|
2015-04-24 15:49:18 +02:00
|
|
|
log_set_prefix (NULL, oldflags | GPGRT_LOG_WITH_PID);
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-07-26 14:01:32 +00:00
|
|
|
default :
|
|
|
|
pargs.err = ARGPARSE_PRINT_ERROR;
|
|
|
|
break;
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-21 21:45:13 +01:00
|
|
|
gpgrt_argparse (NULL, &pargs, NULL);
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
if (only_search_timeout)
|
2021-06-08 08:46:59 +02:00
|
|
|
opt.alarm_timeout = 0;
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.proxy)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
malloced_buffer1 = xtrystrdup (opt.proxy);
|
2010-08-13 10:02:12 +00:00
|
|
|
if (!malloced_buffer1)
|
|
|
|
{
|
|
|
|
log_error ("error copying string: %s\n", strerror (errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2021-06-08 08:46:59 +02:00
|
|
|
opt.host = malloced_buffer1;
|
|
|
|
p = strchr (opt.host, ':');
|
2010-06-09 16:53:51 +00:00
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p++ = 0;
|
2021-06-08 08:46:59 +02:00
|
|
|
opt.port = atoi (p);
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|
2021-06-08 08:46:59 +02:00
|
|
|
if (!opt.port)
|
|
|
|
opt.port = 389; /* make sure ports gets overridden. */
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.port < 0 || opt.port > 65535)
|
|
|
|
log_error ("invalid port number %d\n", opt.port);
|
|
|
|
|
|
|
|
if (!opt.port)
|
|
|
|
opt.port = opt.ldaptls? 636 : 389;
|
|
|
|
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
|
|
|
if (!opt.host)
|
|
|
|
opt.host = "localhost";
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
if (log_get_errorcount (0))
|
|
|
|
exit (2);
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.alarm_timeout)
|
2010-06-09 16:53:51 +00: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 16:53:51 +00: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 16:53:51 +00:00
|
|
|
if (signal (SIGALRM, catch_alarm) == SIG_ERR)
|
|
|
|
# endif
|
|
|
|
log_fatal ("unable to register timeout handler\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (connect_ldap (&ld))
|
|
|
|
any_err = 1;
|
|
|
|
else
|
|
|
|
{
|
2021-06-09 10:23:42 +02:00
|
|
|
if (!argc)
|
|
|
|
{
|
|
|
|
if (process_filter (ld, "(objectClass=*)"))
|
|
|
|
any_err = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (; argc; argc--, argv++)
|
|
|
|
if (process_filter (ld, *argv))
|
|
|
|
any_err = 1;
|
|
|
|
}
|
2021-06-08 08:46:59 +02:00
|
|
|
ldap_unbind (ld);
|
|
|
|
}
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2010-08-13 10:02:12 +00:00
|
|
|
xfree (malloced_buffer1);
|
2010-06-09 16:53:51 +00:00
|
|
|
return any_err;
|
|
|
|
}
|
|
|
|
|
2010-08-13 10:02:12 +00:00
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2010-06-09 16:53:51 +00:00
|
|
|
static void
|
|
|
|
catch_alarm (int dummy)
|
|
|
|
{
|
|
|
|
(void)dummy;
|
|
|
|
_exit (10);
|
|
|
|
}
|
2010-08-13 10:02:12 +00:00
|
|
|
#endif
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2018-05-02 18:40:01 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
static DWORD CALLBACK
|
|
|
|
alarm_thread (void *arg)
|
|
|
|
{
|
|
|
|
HANDLE timer = arg;
|
|
|
|
|
|
|
|
WaitForSingleObject (timer, INFINITE);
|
|
|
|
_exit (10);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
static void
|
2021-06-08 08:46:59 +02:00
|
|
|
set_timeout (void)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.alarm_timeout)
|
2018-05-02 18:40:01 +02:00
|
|
|
{
|
2010-08-02 18:54:53 +00:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2018-05-02 18:40:01 +02:00
|
|
|
static HANDLE timer;
|
|
|
|
LARGE_INTEGER due_time;
|
|
|
|
|
|
|
|
/* A negative value is a relative time. */
|
2021-06-08 08:46:59 +02:00
|
|
|
due_time.QuadPart = (unsigned long long)-10000000 * opt.alarm_timeout;
|
2018-05-02 18:40:01 +02:00
|
|
|
|
|
|
|
if (!timer)
|
|
|
|
{
|
|
|
|
SECURITY_ATTRIBUTES sec_attr;
|
|
|
|
DWORD tid;
|
|
|
|
|
|
|
|
memset (&sec_attr, 0, sizeof sec_attr);
|
|
|
|
sec_attr.nLength = sizeof sec_attr;
|
|
|
|
sec_attr.bInheritHandle = FALSE;
|
|
|
|
|
2018-10-24 15:56:18 -04:00
|
|
|
/* Create a manual resettable timer. */
|
2018-05-02 18:40:01 +02:00
|
|
|
timer = CreateWaitableTimer (NULL, TRUE, NULL);
|
2018-10-24 15:56:18 -04:00
|
|
|
/* Initially set the timer. */
|
2018-05-02 18:40:01 +02:00
|
|
|
SetWaitableTimer (timer, &due_time, 0, NULL, NULL, 0);
|
|
|
|
|
|
|
|
if (CreateThread (&sec_attr, 0, alarm_thread, timer, 0, &tid))
|
|
|
|
log_error ("failed to create alarm thread\n");
|
|
|
|
}
|
|
|
|
else /* Retrigger the timer. */
|
|
|
|
SetWaitableTimer (timer, &due_time, 0, NULL, NULL, 0);
|
2010-08-02 18:54:53 +00:00
|
|
|
#else
|
2021-06-08 08:46:59 +02:00
|
|
|
alarm (opt.alarm_timeout);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Connect to the ldap server. On success the connection handle is
|
|
|
|
* stored at R_LD. */
|
|
|
|
static gpg_error_t
|
|
|
|
connect_ldap (LDAP **r_ld)
|
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
int lerr;
|
|
|
|
LDAP *ld = NULL;
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
|
|
|
char *tmpstr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*r_ld = NULL;
|
|
|
|
|
|
|
|
if (opt.starttls || opt.ldaptls)
|
|
|
|
{
|
|
|
|
#ifndef HAVE_LDAP_START_TLS_S
|
|
|
|
log_error ("ldap: can't connect to the server: no TLS support.");
|
|
|
|
err = GPG_ERR_LDAP_NOT_SUPPORTED;
|
|
|
|
goto leave;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set_timeout ();
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
npth_unprotect ();
|
|
|
|
ld = ldap_sslinit (opt.host, opt.port, opt.ldaptls);
|
|
|
|
npth_protect ();
|
|
|
|
if (!ld)
|
|
|
|
{
|
|
|
|
lerr = LdapGetLastError ();
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
log_error ("error initializing LDAP '%s:%d': %s\n",
|
|
|
|
opt.host, opt.port, ldap_err2string (lerr));
|
|
|
|
goto leave;
|
|
|
|
}
|
2022-09-28 09:43:25 +02:00
|
|
|
if (opt.areconly)
|
|
|
|
{
|
|
|
|
lerr = ldap_set_option (ld, LDAP_OPT_AREC_EXCLUSIVE, LDAP_OPT_ON);
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
log_error ("ldap: unable to set AREC_EXLUSIVE: %s\n",
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 08:46:59 +02:00
|
|
|
#else /* Unix */
|
|
|
|
tmpstr = xtryasprintf ("%s://%s:%d",
|
|
|
|
opt.ldaptls? "ldaps" : "ldap",
|
|
|
|
opt.host, opt.port);
|
|
|
|
if (!tmpstr)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
npth_unprotect ();
|
|
|
|
lerr = ldap_initialize (&ld, tmpstr);
|
|
|
|
npth_protect ();
|
|
|
|
if (lerr || !ld)
|
|
|
|
{
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
log_error ("error initializing LDAP '%s': %s\n",
|
|
|
|
tmpstr, ldap_err2string (lerr));
|
|
|
|
xfree (tmpstr);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
xfree (tmpstr);
|
|
|
|
#endif /* Unix */
|
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info ("LDAP connected to '%s:%d'%s\n",
|
|
|
|
opt.host, opt.port,
|
|
|
|
opt.starttls? " using STARTTLS" :
|
|
|
|
opt.ldaptls? " using LDAP-over-TLS" : "");
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LDAP_SET_OPTION
|
|
|
|
{
|
|
|
|
int ver = LDAP_VERSION3;
|
|
|
|
|
|
|
|
lerr = ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ver);
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
log_error ("unable to go to LDAP 3: %s\n", ldap_err2string (lerr));
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LDAP_START_TLS_S
|
|
|
|
if (opt.starttls)
|
|
|
|
{
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
|
|
|
int check_cert = LDAP_OPT_X_TLS_HARD; /* LDAP_OPT_X_TLS_NEVER */
|
|
|
|
|
|
|
|
lerr = ldap_set_option (ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &check_cert);
|
|
|
|
if (lerr)
|
|
|
|
{
|
|
|
|
log_error ("ldap: error setting an TLS option: %s\n",
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* On Windows, the certificates are checked by default. If the
|
|
|
|
option to disable checking mentioned above is ever
|
|
|
|
implemented, the way to do that on Windows is to install a
|
|
|
|
callback routine using ldap_set_option (..,
|
|
|
|
LDAP_OPT_SERVER_CERTIFICATE, ..); */
|
2010-06-09 16:53:51 +00:00
|
|
|
#endif
|
2021-06-08 08:46:59 +02:00
|
|
|
|
|
|
|
npth_unprotect ();
|
|
|
|
lerr = ldap_start_tls_s (ld,
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
/* ServerReturnValue, result */
|
|
|
|
NULL, NULL,
|
|
|
|
#endif
|
|
|
|
/* ServerControls, ClientControls */
|
|
|
|
NULL, NULL);
|
|
|
|
npth_protect ();
|
|
|
|
if (lerr)
|
|
|
|
{
|
|
|
|
log_error ("ldap: error switching to STARTTLS mode: %s\n",
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (opt.ntds)
|
|
|
|
{
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info ("binding to current user via AD\n");
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
npth_unprotect ();
|
|
|
|
lerr = ldap_bind_s (ld, NULL, NULL, LDAP_AUTH_NEGOTIATE);
|
|
|
|
npth_protect ();
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
log_error ("error binding to LDAP via AD: %s\n",
|
|
|
|
ldap_err2string (lerr));
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
#else /* Unix */
|
|
|
|
err = gpg_error (GPG_ERR_NOT_SUPPORTED);
|
|
|
|
goto leave;
|
|
|
|
#endif /* Unix */
|
|
|
|
}
|
|
|
|
else if (opt.user)
|
|
|
|
{
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info ("LDAP bind to '%s', password '%s'\n",
|
|
|
|
opt.user, opt.pass ? ">not_shown<" : ">none<");
|
|
|
|
|
|
|
|
npth_unprotect ();
|
|
|
|
lerr = ldap_simple_bind_s (ld, opt.user, opt.pass);
|
|
|
|
npth_protect ();
|
|
|
|
if (lerr != LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
log_error ("error binding to LDAP: %s\n", ldap_err2string (lerr));
|
|
|
|
err = ldap_err_to_gpg_err (lerr);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* By default we don't bind as there is usually no need to. */
|
2018-05-02 18:40:01 +02:00
|
|
|
}
|
2021-06-08 08:46:59 +02:00
|
|
|
|
|
|
|
leave:
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
if (ld)
|
|
|
|
ldap_unbind (ld);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*r_ld = ld;
|
|
|
|
return err;
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper for fetch_ldap(). */
|
|
|
|
static int
|
2021-06-08 08:46:59 +02:00
|
|
|
print_ldap_entries (LDAP *ld, LDAPMessage *msg, char *want_attr)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
|
|
|
LDAPMessage *item;
|
|
|
|
int any = 0;
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
for (npth_unprotect (), item = ldap_first_entry (ld, msg), npth_protect ();
|
2010-08-02 18:54:53 +00:00
|
|
|
item;
|
2012-01-24 17:37:01 +01:00
|
|
|
npth_unprotect (), item = ldap_next_entry (ld, item), npth_protect ())
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
|
|
|
BerElement *berctx;
|
|
|
|
char *attr;
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.verbose > 1)
|
|
|
|
log_info ("scanning result for attribute '%s'\n",
|
2010-06-09 16:53:51 +00:00
|
|
|
want_attr? want_attr : "[all]");
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.multi)
|
2010-06-09 16:53:51 +00:00
|
|
|
{ /* Write item marker. */
|
2021-06-08 08:46:59 +02:00
|
|
|
if (es_fwrite ("I\0\0\0\0", 5, 1, opt.outstream) != 1)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_error ("error writing to stdout: %s\n",
|
2010-06-09 16:53:51 +00:00
|
|
|
strerror (errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2019-11-11 20:49:04 +01:00
|
|
|
for (npth_unprotect (), attr = ldap_first_attribute (ld, item, &berctx),
|
2012-01-24 17:37:01 +01:00
|
|
|
npth_protect ();
|
2010-08-02 18:54:53 +00:00
|
|
|
attr;
|
2019-11-11 20:49:04 +01:00
|
|
|
npth_unprotect (), attr = ldap_next_attribute (ld, item, berctx),
|
2012-01-24 17:37:01 +01:00
|
|
|
npth_protect ())
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
|
|
|
struct berval **values;
|
|
|
|
int idx;
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.verbose > 1)
|
|
|
|
log_info (" available attribute '%s'\n", attr);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
set_timeout ();
|
2010-06-09 16:53:51 +00: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)
|
|
|
|
{
|
2019-11-11 20:49:04 +01:00
|
|
|
ldap_memfree (attr);
|
2010-06-09 16:53:51 +00:00
|
|
|
continue; /* Not found: Try next attribute. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-24 17:37:01 +01:00
|
|
|
npth_unprotect ();
|
2019-11-11 20:49:04 +01:00
|
|
|
values = ldap_get_values_len (ld, item, attr);
|
2012-01-24 17:37:01 +01:00
|
|
|
npth_protect ();
|
2010-08-02 18:54:53 +00:00
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
if (!values)
|
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.verbose)
|
|
|
|
log_info ("attribute '%s' not found\n", attr);
|
2019-11-11 20:49:04 +01:00
|
|
|
ldap_memfree (attr);
|
2010-06-09 16:53:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.verbose)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_info ("found attribute '%s'\n", attr);
|
|
|
|
if (opt.verbose > 1)
|
2010-06-09 16:53:51 +00: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 16:53:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.multi)
|
2010-06-09 16:53:51 +00: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);
|
2021-06-08 08:46:59 +02:00
|
|
|
if (es_fwrite (tmp, 5, 1, opt.outstream) != 1
|
|
|
|
|| es_fwrite (attr, n, 1, opt.outstream) != 1)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_error ("error writing to stdout: %s\n",
|
2010-06-09 16:53:51 +00:00
|
|
|
strerror (errno));
|
|
|
|
ldap_value_free_len (values);
|
2019-11-11 20:49:04 +01:00
|
|
|
ldap_memfree (attr);
|
2010-06-09 16:53:51 +00:00
|
|
|
ber_free (berctx, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (idx=0; values[idx]; idx++)
|
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.multi)
|
2010-06-09 16:53:51 +00: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);
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (es_fwrite (tmp, 5, 1, opt.outstream) != 1)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_error ("error writing to stdout: %s\n",
|
2010-06-09 16:53:51 +00:00
|
|
|
strerror (errno));
|
|
|
|
ldap_value_free_len (values);
|
2019-11-11 20:49:04 +01:00
|
|
|
ldap_memfree (attr);
|
2010-06-09 16:53:51 +00:00
|
|
|
ber_free (berctx, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 18:54:53 +00:00
|
|
|
|
2010-07-16 13:19:45 +00:00
|
|
|
if (es_fwrite (values[0]->bv_val, values[0]->bv_len,
|
2021-06-08 08:46:59 +02:00
|
|
|
1, opt.outstream) != 1)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_error ("error writing to stdout: %s\n",
|
2010-06-09 16:53:51 +00:00
|
|
|
strerror (errno));
|
|
|
|
ldap_value_free_len (values);
|
2019-11-11 20:49:04 +01:00
|
|
|
ldap_memfree (attr);
|
2010-06-09 16:53:51 +00:00
|
|
|
ber_free (berctx, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-02 18:54:53 +00:00
|
|
|
|
2010-06-09 16:53:51 +00:00
|
|
|
any = 1;
|
2021-06-08 08:46:59 +02:00
|
|
|
if (!opt.multi)
|
2010-06-09 16:53:51 +00:00
|
|
|
break; /* Print only the first value. */
|
|
|
|
}
|
|
|
|
ldap_value_free_len (values);
|
2019-11-11 20:49:04 +01:00
|
|
|
ldap_memfree (attr);
|
2021-06-08 08:46:59 +02:00
|
|
|
if (want_attr || !opt.multi)
|
2010-06-09 16:53:51 +00: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 16:53:51 +00:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.verbose > 1 && any)
|
2010-06-09 16:53:51 +00:00
|
|
|
log_info ("result has been printed\n");
|
|
|
|
|
|
|
|
return any?0:-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
/* Fetch data from the server at LD using FILTER. */
|
2010-06-09 16:53:51 +00:00
|
|
|
static int
|
2021-06-08 08:46:59 +02:00
|
|
|
fetch_ldap (LDAP *ld, const char *base, int scope, const char *filter)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
gpg_error_t err;
|
|
|
|
int lerr;
|
2010-06-09 16:53:51 +00:00
|
|
|
LDAPMessage *msg;
|
2021-06-08 08:46:59 +02:00
|
|
|
char *attrs[2];
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (filter && !*filter)
|
|
|
|
filter = NULL;
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
if (opt.verbose)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_info ("fetching using");
|
|
|
|
if (base)
|
|
|
|
log_printf (" base '%s'", base);
|
2010-06-09 16:53:51 +00:00
|
|
|
if (filter)
|
2021-06-08 08:46:59 +02:00
|
|
|
log_printf (" filter '%s'", filter);
|
|
|
|
log_printf ("\n");
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|
2019-11-09 11:29:59 +01:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
attrs[0] = opt.attr;
|
|
|
|
attrs[1] = NULL;
|
2010-06-09 16:53:51 +00:00
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
set_timeout ();
|
2012-01-24 17:37:01 +01:00
|
|
|
npth_unprotect ();
|
2021-06-08 08:46:59 +02:00
|
|
|
lerr = ldap_search_st (ld, base, scope, filter,
|
|
|
|
attrs,
|
|
|
|
0,
|
|
|
|
&opt.timeout, &msg);
|
2012-01-24 17:37:01 +01:00
|
|
|
npth_protect ();
|
2021-06-08 08:46:59 +02:00
|
|
|
if (lerr == LDAP_SIZELIMIT_EXCEEDED && opt.multi)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
if (es_fwrite ("E\0\0\0\x09truncated", 14, 1, opt.outstream) != 1)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_error ("error writing to stdout: %s\n", strerror (errno));
|
2010-06-09 16:53:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 08:46:59 +02:00
|
|
|
else if (lerr)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
log_error ("searching '%s' failed: %s\n",
|
|
|
|
filter, ldap_err2string (lerr));
|
|
|
|
if (lerr != LDAP_NO_SUCH_OBJECT)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
|
|
|
/* FIXME: Need deinit (ld)? */
|
|
|
|
/* Hmmm: Do we need to released MSG in case of an error? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
err = print_ldap_entries (ld, msg, opt.multi? NULL:opt.attr);
|
2010-06-09 16:53:51 +00:00
|
|
|
|
|
|
|
ldap_msgfree (msg);
|
2021-06-08 08:46:59 +02:00
|
|
|
return err;
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-08 08:46:59 +02:00
|
|
|
/* Main processing. Take the filter and run the LDAP query. The
|
|
|
|
* result is printed to stdout, errors are logged to the log stream.
|
|
|
|
* To allow searching with a different base it is possible to extend
|
2023-03-20 19:24:49 +01:00
|
|
|
* the filter. For example:
|
2021-06-08 08:46:59 +02:00
|
|
|
*
|
|
|
|
* ^CN=foo, OU=My Users&(objectClasses=*)
|
|
|
|
*
|
|
|
|
* Uses "CN=foo, OU=My Users" as base DN and "(objectClasses=*)" as
|
|
|
|
* filter. If the base prefix includes an ampersand, it needs to be
|
|
|
|
* doubled. The usual escaping rules for DNs (for the base) and
|
|
|
|
* filters apply. If no scope is given (see ldap_parse_extfilter for
|
|
|
|
* the syntax) subtree scope is used.
|
|
|
|
*/
|
|
|
|
static gpg_error_t
|
|
|
|
process_filter (LDAP *ld, const char *string)
|
2010-06-09 16:53:51 +00:00
|
|
|
{
|
2021-06-08 08:46:59 +02:00
|
|
|
gpg_error_t err;
|
|
|
|
char *base, *filter;
|
|
|
|
int scope = -1;
|
|
|
|
|
|
|
|
err = ldap_parse_extfilter (string, 0, &base, &scope, &filter);
|
|
|
|
if (!err)
|
|
|
|
err = fetch_ldap (ld,
|
|
|
|
base? base : opt.base,
|
|
|
|
scope == -1? LDAP_SCOPE_SUBTREE : scope,
|
|
|
|
filter);
|
|
|
|
|
|
|
|
xfree (base);
|
|
|
|
xfree (filter);
|
|
|
|
return err;
|
2010-06-09 16:53:51 +00:00
|
|
|
}
|