2003-11-26 07:17:08 +08:00
|
|
|
/* ldapsearch -- a tool for searching LDAP directories */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2005-01-02 04:49:32 +08:00
|
|
|
* Copyright 1998-2005 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* Portions Copyright 1998-2003 Kurt D. Zeilenga.
|
|
|
|
* Portions Copyright 1998-2001 Net Boolean Incorporated.
|
|
|
|
* Portions Copyright 2001-2003 IBM Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the
|
|
|
|
* University may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission. This
|
|
|
|
* software is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
2003-11-26 10:58:56 +08:00
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was originally developed by the University of Michigan
|
2003-11-26 07:17:08 +08:00
|
|
|
* (as part of U-MICH LDAP). Additional significant contributors
|
|
|
|
* include:
|
|
|
|
* Jong Hyuk Choi
|
|
|
|
* Lynn Moss
|
|
|
|
* Mikhail Sahalaev
|
|
|
|
* Kurt D. Zeilenga
|
1999-08-05 07:55:45 +08:00
|
|
|
*/
|
2000-04-20 17:23:51 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1998-11-15 14:54:30 +08:00
|
|
|
#include <ac/ctype.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/unistd.h>
|
2000-04-25 00:41:36 +08:00
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <sys/stat.h>
|
1998-08-20 10:18:28 +08:00
|
|
|
|
2005-02-18 10:40:06 +08:00
|
|
|
#include <ac/signal.h>
|
|
|
|
|
2000-04-25 00:41:36 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
1999-10-31 10:26:41 +08:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <ldap.h>
|
|
|
|
|
1999-08-04 02:14:24 +08:00
|
|
|
#include "ldif.h"
|
2000-06-25 08:35:17 +08:00
|
|
|
#include "lutil.h"
|
2000-07-14 06:54:38 +08:00
|
|
|
#include "lutil_ldap.h"
|
1999-07-31 07:00:02 +08:00
|
|
|
#include "ldap_defaults.h"
|
2001-12-18 06:37:47 +08:00
|
|
|
#include "ldap_log.h"
|
2003-04-29 22:42:58 +08:00
|
|
|
#include "ldap_pvt.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int scope = LDAP_SCOPE_SUBTREE;
|
|
|
|
static int deref = -1;
|
|
|
|
static int attrsonly;
|
|
|
|
static int timelimit = -1;
|
|
|
|
static int sizelimit = -1;
|
|
|
|
|
2001-12-07 10:22:07 +08:00
|
|
|
static char *def_tmpdir;
|
|
|
|
static char *def_urlpre;
|
|
|
|
|
2004-01-16 05:27:32 +08:00
|
|
|
#if defined(__CYGWIN__) || defined(__MINGW32__)
|
|
|
|
/* Turn off commandline globbing, otherwise you cannot search for
|
|
|
|
* attribute '*'
|
|
|
|
*/
|
|
|
|
int _CRT_glob = 0;
|
|
|
|
#endif
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
usage( void )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("usage: %s [options] [filter [attributes...]]\nwhere:\n"), prog);
|
|
|
|
fprintf( stderr, _(" filter\tRFC-2254 compliant LDAP search filter\n"));
|
|
|
|
fprintf( stderr, _(" attributes\twhitespace-separated list of attribute descriptions\n"));
|
|
|
|
fprintf( stderr, _(" which may include:\n"));
|
|
|
|
fprintf( stderr, _(" 1.1 no attributes\n"));
|
|
|
|
fprintf( stderr, _(" * all user attributes\n"));
|
|
|
|
fprintf( stderr, _(" + all operational attributes\n"));
|
|
|
|
|
|
|
|
|
|
|
|
fprintf( stderr, _("Search options:\n"));
|
|
|
|
fprintf( stderr, _(" -a deref one of never (default), always, search, or find\n"));
|
|
|
|
fprintf( stderr, _(" -A retrieve attribute names only (no values)\n"));
|
|
|
|
fprintf( stderr, _(" -b basedn base dn for search\n"));
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr, _(" -E [!]<ext>[=<extparam>] search extensions (! indicates criticality)\n"));
|
2003-02-09 14:14:06 +08:00
|
|
|
#ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _(" [!]domainScope (domain scope)\n"));
|
2003-02-09 14:14:06 +08:00
|
|
|
#endif
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _(" [!]mv=<filter> (matched values filter)\n"));
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2004-03-01 17:13:36 +08:00
|
|
|
fprintf( stderr, _(" [!]pr=<size>[/prompt|noprompt] (paged results/prompt)\n"));
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2002-08-30 15:05:12 +08:00
|
|
|
#ifdef LDAP_CONTROL_SUBENTRIES
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _(" [!]subentries[=true|false] (subentries)\n"));
|
2002-08-30 15:05:12 +08:00
|
|
|
#endif
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _(" [!]sync=ro[/<cookie>] (LDAP Sync refreshOnly)\n"));
|
|
|
|
fprintf( stderr, _(" rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
|
|
|
|
fprintf( stderr, _(" -F prefix URL prefix for files (default: %s)\n"), def_urlpre);
|
2004-06-19 03:47:53 +08:00
|
|
|
fprintf( stderr, _(" -l limit time limit (in seconds, or \"none\" or \"max\") for search\n"));
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _(" -L print responses in LDIFv1 format\n"));
|
|
|
|
fprintf( stderr, _(" -LL print responses in LDIF format without comments\n"));
|
|
|
|
fprintf( stderr, _(" -LLL print responses in LDIF format without comments\n"));
|
|
|
|
fprintf( stderr, _(" and version\n"));
|
2004-10-04 05:12:25 +08:00
|
|
|
#ifdef LDAP_SCOPE_SUBORDINATE
|
|
|
|
fprintf( stderr, _(" -s scope one of base, one, sub or children (search scope)\n"));
|
|
|
|
#else /* ! LDAP_SCOPE_SUBORDINATE */
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _(" -s scope one of base, one, or sub (search scope)\n"));
|
2004-10-04 05:12:25 +08:00
|
|
|
#endif /* ! LDAP_SCOPE_SUBORDINATE */
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _(" -S attr sort the results by attribute `attr'\n"));
|
|
|
|
fprintf( stderr, _(" -t write binary values to files in temporary directory\n"));
|
|
|
|
fprintf( stderr, _(" -tt write all values to files in temporary directory\n"));
|
|
|
|
fprintf( stderr, _(" -T path write files to directory specified by path (default: %s)\n"), def_tmpdir);
|
|
|
|
fprintf( stderr, _(" -u include User Friendly entry names in the output\n"));
|
2004-06-19 03:47:53 +08:00
|
|
|
fprintf( stderr, _(" -z limit size limit (in entries, or \"none\" or \"max\") for search\n"));
|
2002-12-13 00:32:26 +08:00
|
|
|
tool_common_usage();
|
1999-07-31 07:00:02 +08:00
|
|
|
exit( EXIT_FAILURE );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-06-25 08:35:17 +08:00
|
|
|
static void print_entry LDAP_P((
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *entry,
|
|
|
|
int attrsonly));
|
|
|
|
|
|
|
|
static void print_reference(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *reference );
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
static void print_extended(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *extended );
|
|
|
|
|
|
|
|
static void print_partial(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *partial );
|
|
|
|
|
2000-06-25 08:35:17 +08:00
|
|
|
static int print_result(
|
2000-06-24 09:40:39 +08:00
|
|
|
LDAP *ld,
|
2000-06-25 08:35:17 +08:00
|
|
|
LDAPMessage *result,
|
|
|
|
int search );
|
2000-06-24 09:40:39 +08:00
|
|
|
|
2000-06-25 08:35:17 +08:00
|
|
|
static void print_ctrls(
|
|
|
|
LDAPControl **ctrls );
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
static int write_ldif LDAP_P((
|
|
|
|
int type,
|
|
|
|
char *name,
|
1998-10-25 09:41:42 +08:00
|
|
|
char *value,
|
1999-07-31 07:00:02 +08:00
|
|
|
ber_len_t vallen ));
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
static int dosearch LDAP_P((
|
|
|
|
LDAP *ld,
|
1999-07-31 07:00:02 +08:00
|
|
|
char *base,
|
|
|
|
int scope,
|
2000-06-26 02:07:15 +08:00
|
|
|
char *filtpatt,
|
|
|
|
char *value,
|
1999-07-31 07:00:02 +08:00
|
|
|
char **attrs,
|
|
|
|
int attrsonly,
|
2000-06-26 02:07:15 +08:00
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls,
|
2001-01-24 04:38:19 +08:00
|
|
|
struct timeval *timeout,
|
2000-06-26 02:07:15 +08:00
|
|
|
int sizelimit ));
|
1999-07-31 07:00:02 +08:00
|
|
|
|
|
|
|
static char *tmpdir = NULL;
|
|
|
|
static char *urlpre = NULL;
|
1998-12-14 18:10:41 +08:00
|
|
|
static char *base = NULL;
|
|
|
|
static char *sortattr = NULL;
|
2002-12-13 00:32:26 +08:00
|
|
|
static int includeufn, vals2tmp = 0, ldif = 0;
|
|
|
|
|
|
|
|
static int subentries = 0, valuesReturnFilter = 0;
|
|
|
|
static char *vrFilter = NULL;
|
2002-11-28 10:26:55 +08:00
|
|
|
|
2003-02-09 14:14:06 +08:00
|
|
|
#ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
|
|
|
|
static int domainScope = 0;
|
|
|
|
#endif
|
|
|
|
|
2003-06-28 01:39:48 +08:00
|
|
|
static int ldapsync = 0;
|
2003-02-05 15:37:02 +08:00
|
|
|
static struct berval sync_cookie = { 0, NULL };
|
|
|
|
static int sync_slimit = -1;
|
2002-12-19 06:09:25 +08:00
|
|
|
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2002-11-28 10:26:55 +08:00
|
|
|
static int pagedResults = 0;
|
2004-02-27 18:00:45 +08:00
|
|
|
static int pagePrompt = 1;
|
2002-11-28 10:26:55 +08:00
|
|
|
static ber_int_t pageSize = 0;
|
2002-11-22 04:49:02 +08:00
|
|
|
static ber_int_t entriesLeft = 0;
|
2002-11-21 10:16:53 +08:00
|
|
|
static ber_int_t morePagedResults = 1;
|
2004-12-21 07:02:17 +08:00
|
|
|
static struct berval page_cookie = { 0, NULL };
|
2002-11-21 10:16:53 +08:00
|
|
|
static int npagedresponses;
|
|
|
|
static int npagedentries;
|
|
|
|
static int npagedreferences;
|
|
|
|
static int npagedextended;
|
|
|
|
static int npagedpartial;
|
2002-11-28 10:26:55 +08:00
|
|
|
|
|
|
|
static int parse_page_control(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *result,
|
|
|
|
struct berval *cookie );
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-12-07 10:22:07 +08:00
|
|
|
static void
|
|
|
|
urlize(char *url)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
2002-07-24 02:31:42 +08:00
|
|
|
if (*LDAP_DIRSEP != '/') {
|
|
|
|
for (p = url; *p; p++) {
|
2001-12-07 10:22:07 +08:00
|
|
|
if (*p == *LDAP_DIRSEP)
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-20 02:07:27 +08:00
|
|
|
const char options[] = "a:Ab:cE:F:l:Ls:S:tT:uz:"
|
2003-01-21 04:11:57 +08:00
|
|
|
"Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
|
2001-12-07 10:22:07 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
int
|
|
|
|
handle_private_option( int i )
|
|
|
|
{
|
2004-06-18 16:30:46 +08:00
|
|
|
int crit, ival;
|
|
|
|
char *control, *cvalue, *next;
|
2002-12-13 00:32:26 +08:00
|
|
|
switch ( i ) {
|
2000-07-16 07:25:46 +08:00
|
|
|
case 'a': /* set alias deref option */
|
|
|
|
if ( strcasecmp( optarg, "never" ) == 0 ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
deref = LDAP_DEREF_NEVER;
|
2000-07-16 07:25:46 +08:00
|
|
|
} else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
deref = LDAP_DEREF_SEARCHING;
|
2000-07-16 07:25:46 +08:00
|
|
|
} else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
deref = LDAP_DEREF_FINDING;
|
2000-07-16 07:25:46 +08:00
|
|
|
} else if ( strcasecmp( optarg, "always" ) == 0 ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
deref = LDAP_DEREF_ALWAYS;
|
2000-07-16 07:25:46 +08:00
|
|
|
} else {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("alias deref should be never, search, find, or always\n") );
|
|
|
|
usage();
|
2000-07-16 07:25:46 +08:00
|
|
|
}
|
1999-07-31 07:00:02 +08:00
|
|
|
break;
|
1998-12-14 18:10:41 +08:00
|
|
|
case 'A': /* retrieve attribute names only -- no values */
|
1999-07-31 07:00:02 +08:00
|
|
|
++attrsonly;
|
|
|
|
break;
|
2000-07-16 08:14:08 +08:00
|
|
|
case 'b': /* search base */
|
2004-03-15 07:32:00 +08:00
|
|
|
base = ber_strdup( optarg );
|
2000-07-16 08:14:08 +08:00
|
|
|
break;
|
2004-03-13 06:34:07 +08:00
|
|
|
case 'E': /* search extensions */
|
2003-01-21 04:11:57 +08:00
|
|
|
if( protocol == LDAP_VERSION2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
|
2003-01-21 04:11:57 +08:00
|
|
|
prog, protocol );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2002-06-02 12:15:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* should be extended to support comma separated list of
|
2002-08-30 05:36:36 +08:00
|
|
|
* [!]key[=value] parameters, e.g. -E !foo,bar=567
|
2002-06-02 12:15:38 +08:00
|
|
|
*/
|
|
|
|
|
2002-08-30 05:36:36 +08:00
|
|
|
crit = 0;
|
|
|
|
cvalue = NULL;
|
|
|
|
if( optarg[0] == '!' ) {
|
|
|
|
crit = 1;
|
|
|
|
optarg++;
|
|
|
|
}
|
|
|
|
|
2004-03-15 07:32:00 +08:00
|
|
|
control = ber_strdup( optarg );
|
2002-08-30 05:36:36 +08:00
|
|
|
if ( (cvalue = strchr( control, '=' )) != NULL ) {
|
|
|
|
*cvalue++ = '\0';
|
2002-06-02 12:15:38 +08:00
|
|
|
}
|
|
|
|
|
2002-08-30 05:36:36 +08:00
|
|
|
if ( strcasecmp( control, "mv" ) == 0 ) {
|
2002-06-02 12:15:38 +08:00
|
|
|
/* ValuesReturnFilter control */
|
2002-08-30 15:05:12 +08:00
|
|
|
if( valuesReturnFilter ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("ValuesReturnFilter previously specified\n"));
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2002-06-02 12:15:38 +08:00
|
|
|
}
|
2002-08-30 05:36:36 +08:00
|
|
|
valuesReturnFilter= 1 + crit;
|
2002-06-02 12:15:38 +08:00
|
|
|
|
2002-08-30 05:36:36 +08:00
|
|
|
if ( cvalue == NULL ) {
|
|
|
|
fprintf( stderr,
|
2003-04-06 10:59:15 +08:00
|
|
|
_("missing filter in ValuesReturnFilter control\n"));
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2002-08-29 18:41:10 +08:00
|
|
|
}
|
|
|
|
|
2002-08-30 05:36:36 +08:00
|
|
|
vrFilter = cvalue;
|
2003-01-21 04:11:57 +08:00
|
|
|
protocol = LDAP_VERSION3;
|
2002-06-02 12:15:38 +08:00
|
|
|
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2002-11-21 10:16:53 +08:00
|
|
|
} else if ( strcasecmp( control, "pr" ) == 0 ) {
|
2002-11-28 12:38:41 +08:00
|
|
|
int num, tmp;
|
2002-11-21 10:16:53 +08:00
|
|
|
/* PagedResults control */
|
2002-11-28 10:26:55 +08:00
|
|
|
if ( pagedResults != 0 ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("PagedResultsControl previously specified\n") );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2002-11-21 10:16:53 +08:00
|
|
|
}
|
|
|
|
|
2004-02-27 17:32:16 +08:00
|
|
|
if( cvalue != NULL ) {
|
2004-02-27 18:00:45 +08:00
|
|
|
char *promptp;
|
|
|
|
|
|
|
|
promptp = strchr( cvalue, '/' );
|
|
|
|
if ( promptp != NULL ) {
|
|
|
|
*promptp++ = '\0';
|
2004-03-01 17:13:36 +08:00
|
|
|
if ( strcasecmp( promptp, "prompt" ) == 0 ) {
|
2004-02-27 18:00:45 +08:00
|
|
|
pagePrompt = 1;
|
2004-03-01 17:13:36 +08:00
|
|
|
} else if ( strcasecmp( promptp, "noprompt" ) == 0) {
|
2004-02-27 18:00:45 +08:00
|
|
|
pagePrompt = 0;
|
|
|
|
} else {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("Invalid value for PagedResultsControl,"
|
|
|
|
" %s/%s.\n"), cvalue, promptp );
|
2004-02-27 18:00:45 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
}
|
2004-02-27 17:32:16 +08:00
|
|
|
num = sscanf( cvalue, "%d", &tmp );
|
|
|
|
if ( num != 1 ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("Invalid value for PagedResultsControl, %s.\n"),
|
|
|
|
cvalue );
|
2004-02-27 17:32:16 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
} else {
|
2005-07-17 19:49:27 +08:00
|
|
|
fprintf(stderr, _("Invalid value for PagedResultsControl.\n"));
|
2004-02-27 17:32:16 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2002-11-21 10:16:53 +08:00
|
|
|
}
|
2002-11-28 10:26:55 +08:00
|
|
|
pageSize = (ber_int_t) tmp;
|
|
|
|
pagedResults = 1 + crit;
|
2002-11-21 10:16:53 +08:00
|
|
|
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2003-02-09 14:14:06 +08:00
|
|
|
#ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
|
|
|
|
} else if ( strcasecmp( control, "domainScope" ) == 0 ) {
|
|
|
|
if( domainScope ) {
|
|
|
|
fprintf( stderr,
|
2003-04-06 10:59:15 +08:00
|
|
|
_("domainScope control previously specified\n"));
|
2003-02-09 14:14:06 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( cvalue != NULL ) {
|
|
|
|
fprintf( stderr,
|
2003-04-06 10:59:15 +08:00
|
|
|
_("domainScope: no control value expected\n") );
|
2003-02-09 14:14:06 +08:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
domainScope = 1 + crit;
|
|
|
|
#endif
|
2005-04-02 07:29:02 +08:00
|
|
|
|
2002-08-30 15:05:12 +08:00
|
|
|
#ifdef LDAP_CONTROL_SUBENTRIES
|
|
|
|
} else if ( strcasecmp( control, "subentries" ) == 0 ) {
|
|
|
|
if( subentries ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("subentries control previously specified\n"));
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2002-08-30 15:05:12 +08:00
|
|
|
}
|
|
|
|
if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
|
|
|
|
subentries = 2;
|
|
|
|
} else if ( strcasecmp( cvalue, "false") == 0 ) {
|
|
|
|
subentries = 1;
|
|
|
|
} else {
|
|
|
|
fprintf( stderr,
|
2003-04-06 10:59:15 +08:00
|
|
|
_("subentries control value \"%s\" invalid\n"),
|
2002-12-13 00:32:26 +08:00
|
|
|
cvalue );
|
|
|
|
exit( EXIT_FAILURE );
|
2002-08-30 15:05:12 +08:00
|
|
|
}
|
|
|
|
if( crit ) subentries *= -1;
|
|
|
|
#endif
|
|
|
|
|
2005-01-26 00:11:26 +08:00
|
|
|
} else if ( strcasecmp( control, "sync" ) == 0 ) {
|
2003-02-05 15:37:02 +08:00
|
|
|
char *cookiep;
|
|
|
|
char *slimitp;
|
|
|
|
if ( ldapsync ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr, _("sync control previously specified\n") );
|
2003-02-05 15:37:02 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if ( cvalue == NULL ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr, _("missing specification of sync control\n"));
|
2003-02-05 15:37:02 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
|
|
|
|
ldapsync = LDAP_SYNC_REFRESH_ONLY;
|
|
|
|
cookiep = strchr( cvalue, '/' );
|
|
|
|
if ( cookiep != NULL ) {
|
|
|
|
cookiep++;
|
|
|
|
if ( *cookiep != '\0' ) {
|
|
|
|
ber_str2bv( cookiep, 0, 0, &sync_cookie );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
|
|
|
|
ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
|
|
|
|
cookiep = strchr( cvalue, '/' );
|
|
|
|
if ( cookiep != NULL ) {
|
|
|
|
*cookiep++ = '\0';
|
|
|
|
cvalue = cookiep;
|
|
|
|
}
|
|
|
|
slimitp = strchr( cvalue, '/' );
|
|
|
|
if ( slimitp != NULL ) {
|
|
|
|
*slimitp++ = '\0';
|
|
|
|
}
|
|
|
|
if ( cookiep != NULL && *cookiep != '\0' )
|
|
|
|
ber_str2bv( cookiep, 0, 0, &sync_cookie );
|
2004-06-18 16:30:46 +08:00
|
|
|
if ( slimitp != NULL && *slimitp != '\0' ) {
|
|
|
|
ival = strtol( slimitp, &next, 10 );
|
|
|
|
if ( next == NULL || next[0] != '\0' ) {
|
|
|
|
fprintf( stderr, _("Unable to parse sync control value \"%s\"\n"), slimitp );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
sync_slimit = ival;
|
|
|
|
}
|
2003-02-05 15:37:02 +08:00
|
|
|
} else {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr, _("sync control value \"%s\" invalid\n"),
|
2003-02-05 15:37:02 +08:00
|
|
|
cvalue );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if ( crit ) ldapsync *= -1;
|
|
|
|
|
2002-06-02 12:15:38 +08:00
|
|
|
} else {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr, _("Invalid search extension name: %s\n"),
|
|
|
|
control );
|
2002-12-13 00:32:26 +08:00
|
|
|
usage();
|
2002-06-02 12:15:38 +08:00
|
|
|
}
|
2002-12-02 03:07:05 +08:00
|
|
|
break;
|
2001-01-19 12:55:38 +08:00
|
|
|
case 'F': /* uri prefix */
|
|
|
|
if( urlpre ) free( urlpre );
|
|
|
|
urlpre = strdup( optarg );
|
|
|
|
break;
|
2000-07-16 07:25:46 +08:00
|
|
|
case 'l': /* time limit */
|
2004-05-08 01:43:22 +08:00
|
|
|
if ( strcasecmp( optarg, "none" ) == 0 ) {
|
2004-06-18 15:18:27 +08:00
|
|
|
timelimit = 0;
|
2004-06-19 03:47:53 +08:00
|
|
|
|
|
|
|
} else if ( strcasecmp( optarg, "max" ) == 0 ) {
|
2004-06-19 04:14:28 +08:00
|
|
|
timelimit = LDAP_MAXINT;
|
2004-06-19 03:47:53 +08:00
|
|
|
|
2004-05-08 01:43:22 +08:00
|
|
|
} else {
|
2004-06-18 16:30:46 +08:00
|
|
|
ival = strtol( optarg, &next, 10 );
|
|
|
|
if ( next == NULL || next[0] != '\0' ) {
|
2004-06-19 04:14:28 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("Unable to parse time limit \"%s\"\n"), optarg );
|
2004-06-18 16:30:46 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
timelimit = ival;
|
2004-05-08 01:43:22 +08:00
|
|
|
}
|
2004-06-19 04:14:28 +08:00
|
|
|
if( timelimit < 0 || timelimit > LDAP_MAXINT ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
|
2001-01-24 04:51:14 +08:00
|
|
|
prog, timelimit );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2001-01-24 04:51:14 +08:00
|
|
|
}
|
2000-07-16 07:25:46 +08:00
|
|
|
break;
|
1998-12-14 18:10:41 +08:00
|
|
|
case 'L': /* print entries in LDIF format */
|
1999-07-31 07:00:02 +08:00
|
|
|
++ldif;
|
|
|
|
break;
|
1998-08-09 08:43:13 +08:00
|
|
|
case 's': /* search scope */
|
2005-07-17 19:47:00 +08:00
|
|
|
if ( strncasecmp( optarg, "base", sizeof("base")-1 ) == 0 ) {
|
2003-12-18 01:55:27 +08:00
|
|
|
scope = LDAP_SCOPE_BASE;
|
2000-06-24 09:40:39 +08:00
|
|
|
} else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
|
2003-12-18 01:55:27 +08:00
|
|
|
scope = LDAP_SCOPE_ONELEVEL;
|
2004-03-18 09:06:39 +08:00
|
|
|
#ifdef LDAP_SCOPE_SUBORDINATE
|
2003-12-18 01:55:27 +08:00
|
|
|
} else if (( strcasecmp( optarg, "subordinate" ) == 0 )
|
|
|
|
|| ( strcasecmp( optarg, "children" ) == 0 ))
|
|
|
|
{
|
|
|
|
scope = LDAP_SCOPE_SUBORDINATE;
|
2004-03-18 09:06:39 +08:00
|
|
|
#endif
|
2000-06-24 09:40:39 +08:00
|
|
|
} else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
|
2003-12-18 01:55:27 +08:00
|
|
|
scope = LDAP_SCOPE_SUBTREE;
|
1999-07-31 07:00:02 +08:00
|
|
|
} else {
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr, _("scope should be base, one, or sub\n") );
|
|
|
|
usage();
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
|
|
|
break;
|
2000-07-16 07:25:46 +08:00
|
|
|
case 'S': /* sort attribute */
|
|
|
|
sortattr = strdup( optarg );
|
|
|
|
break;
|
|
|
|
case 't': /* write attribute values to TMPDIR files */
|
|
|
|
++vals2tmp;
|
1999-07-31 07:00:02 +08:00
|
|
|
break;
|
2000-06-25 08:35:17 +08:00
|
|
|
case 'T': /* tmpdir */
|
1999-07-31 07:00:02 +08:00
|
|
|
if( tmpdir ) free( tmpdir );
|
|
|
|
tmpdir = strdup( optarg );
|
|
|
|
break;
|
2002-12-13 00:32:26 +08:00
|
|
|
case 'u': /* include UFN */
|
|
|
|
++includeufn;
|
|
|
|
break;
|
2000-07-16 07:25:46 +08:00
|
|
|
case 'z': /* size limit */
|
2004-05-08 01:43:22 +08:00
|
|
|
if ( strcasecmp( optarg, "none" ) == 0 ) {
|
2004-06-18 15:18:27 +08:00
|
|
|
sizelimit = 0;
|
2004-06-19 03:47:53 +08:00
|
|
|
|
|
|
|
} else if ( strcasecmp( optarg, "max" ) == 0 ) {
|
2004-06-19 04:14:28 +08:00
|
|
|
sizelimit = LDAP_MAXINT;
|
2004-06-19 03:47:53 +08:00
|
|
|
|
2004-05-08 01:43:22 +08:00
|
|
|
} else {
|
2004-06-18 16:30:46 +08:00
|
|
|
ival = strtol( optarg, &next, 10 );
|
|
|
|
if ( next == NULL || next[0] != '\0' ) {
|
2004-06-19 04:14:28 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("Unable to parse size limit \"%s\"\n"), optarg );
|
2004-06-18 16:30:46 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
sizelimit = ival;
|
2004-05-08 01:43:22 +08:00
|
|
|
}
|
2004-06-19 04:14:28 +08:00
|
|
|
if( sizelimit < 0 || sizelimit > LDAP_MAXINT ) {
|
2004-06-18 15:18:27 +08:00
|
|
|
fprintf( stderr, _("%s: invalid sizelimit (%d) specified\n"),
|
2004-06-19 04:14:28 +08:00
|
|
|
prog, sizelimit );
|
2004-06-18 15:18:27 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
1999-07-31 07:00:02 +08:00
|
|
|
break;
|
2002-12-13 00:32:26 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2000-07-16 07:25:46 +08:00
|
|
|
|
2002-08-30 05:36:36 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
static void
|
|
|
|
private_conn_setup( LDAP *ld )
|
|
|
|
{
|
|
|
|
if (deref != -1 &&
|
2004-03-15 07:32:00 +08:00
|
|
|
ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref )
|
|
|
|
!= LDAP_OPT_SUCCESS )
|
2002-12-13 00:32:26 +08:00
|
|
|
{
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2004-06-18 15:18:27 +08:00
|
|
|
if (timelimit > 0 &&
|
2004-03-15 07:32:00 +08:00
|
|
|
ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit )
|
|
|
|
!= LDAP_OPT_SUCCESS )
|
2002-12-13 00:32:26 +08:00
|
|
|
{
|
2004-04-10 03:48:02 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2004-06-18 15:18:27 +08:00
|
|
|
if (sizelimit > 0 &&
|
2004-03-15 07:32:00 +08:00
|
|
|
ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit )
|
|
|
|
!= LDAP_OPT_SUCCESS )
|
2002-12-13 00:32:26 +08:00
|
|
|
{
|
2004-03-15 07:32:00 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
_("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
}
|
2002-08-30 05:36:36 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
char *filtpattern, **attrs = NULL, line[BUFSIZ];
|
|
|
|
FILE *fp = NULL;
|
|
|
|
int rc, i, first;
|
|
|
|
LDAP *ld = NULL;
|
2002-12-19 03:16:31 +08:00
|
|
|
BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
|
2002-11-28 13:56:19 +08:00
|
|
|
|
2003-02-05 15:37:02 +08:00
|
|
|
BerElement *syncber = NULL;
|
|
|
|
struct berval *syncbvalp = NULL;
|
|
|
|
|
2003-04-06 10:59:15 +08:00
|
|
|
tool_init();
|
|
|
|
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2002-12-13 00:32:26 +08:00
|
|
|
npagedresponses = npagedentries = npagedreferences =
|
|
|
|
npagedextended = npagedpartial = 0;
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2002-12-02 23:46:53 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
prog = lutil_progname( "ldapsearch", argc, argv );
|
2002-08-30 05:36:36 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if((def_tmpdir = getenv("TMPDIR")) == NULL &&
|
|
|
|
(def_tmpdir = getenv("TMP")) == NULL &&
|
|
|
|
(def_tmpdir = getenv("TEMP")) == NULL )
|
|
|
|
{
|
|
|
|
def_tmpdir = LDAP_TMPDIR;
|
|
|
|
}
|
2002-08-30 05:36:36 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if ( !*def_tmpdir )
|
|
|
|
def_tmpdir = LDAP_TMPDIR;
|
2002-08-30 13:43:47 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
|
1999-06-30 02:59:11 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if( def_urlpre == NULL ) {
|
|
|
|
perror( "malloc" );
|
2000-07-16 07:25:46 +08:00
|
|
|
return EXIT_FAILURE;
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
sprintf( def_urlpre, "file:///%s/",
|
|
|
|
def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
|
|
|
|
|
|
|
|
urlize( def_urlpre );
|
|
|
|
|
|
|
|
tool_args( argc, argv );
|
1999-12-12 13:05:20 +08:00
|
|
|
|
2000-07-16 07:25:46 +08:00
|
|
|
if (( argc - optind < 1 ) ||
|
2000-07-28 07:08:48 +08:00
|
|
|
( *argv[optind] != '(' /*')'*/ &&
|
|
|
|
( strchr( argv[optind], '=' ) == NULL ) ) )
|
2000-07-16 07:25:46 +08:00
|
|
|
{
|
2000-06-25 08:35:17 +08:00
|
|
|
filtpattern = "(objectclass=*)";
|
|
|
|
} else {
|
2005-08-01 11:44:52 +08:00
|
|
|
filtpattern = argv[optind++];
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
|
|
|
|
2001-09-04 14:06:44 +08:00
|
|
|
if ( argv[optind] != NULL ) {
|
2000-06-25 08:35:17 +08:00
|
|
|
attrs = &argv[optind];
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( infile != NULL ) {
|
|
|
|
if ( infile[0] == '-' && infile[1] == '\0' ) {
|
|
|
|
fp = stdin;
|
|
|
|
} else if (( fp = fopen( infile, "r" )) == NULL ) {
|
|
|
|
perror( infile );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-07 10:22:07 +08:00
|
|
|
if ( tmpdir == NULL ) {
|
|
|
|
tmpdir = def_tmpdir;
|
|
|
|
|
|
|
|
if ( urlpre == NULL )
|
|
|
|
urlpre = def_urlpre;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
if( urlpre == NULL ) {
|
2000-05-12 05:21:18 +08:00
|
|
|
urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
|
1999-07-31 07:00:02 +08:00
|
|
|
|
|
|
|
if( urlpre == NULL ) {
|
|
|
|
perror( "malloc" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( urlpre, "file:///%s/",
|
2001-12-07 10:22:07 +08:00
|
|
|
tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
|
1999-07-31 07:00:02 +08:00
|
|
|
|
2001-12-07 10:22:07 +08:00
|
|
|
urlize( urlpre );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if ( debug )
|
1998-12-22 09:34:01 +08:00
|
|
|
ldif_debug = debug;
|
1998-12-14 18:10:41 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
ld = tool_conn_setup( 0, &private_conn_setup );
|
2000-04-20 17:23:51 +08:00
|
|
|
|
Patch: 'ldapmodify -y file' reads password from file (ITS#2031)
================
Written by Hallvard B. Furuseth and placed into the public domain.
This software is not subject to any license of the University of Oslo.
================
Adapted by Kurt Zeilenga for inclusion in OpenLDAP. My comments are
marked with enclosed with square brackets (e.g. [Kurt's comment] below.
================
If I run ldapmodify & co from a script, I don't want to use '-W password'
because the password shows up in the output of 'ps' for everyone,
and I can't pipe the password to 'ldapmodify -w' because -w uses
getpassphrase() which reads from the tty instead of stdin.
So I added '-y file' which reads the password from file. The programs
exit if the file cannot be read.
[Complete contents of file is used as password. Use:
echo -n "secret" > password
to create a file with "secret" as the password. The -n avoids
adding a newline (which would invalidate the password). Note
that echo is a builtin and hence its arguments are not visible
to 'ps'.]
I changed ldapmodify, ldapmodrdn, ldapdelete, ldapsearch, ldapcompare.
I did not bother to change ldappasswd and ldapwhoami, because they
prompt for many passwords. [I fixed up ldapwhoami.]
Rerun autoconf after applying this patch. [Done.]
Note: I do not know if Windows NT has fstat(), so I set HAVE_FSTAT to
undef in portable.nt. (fstat() is used to warn if the file is publicly
readable or writeable.) [I used fstat() to set the buffer size to
read.]
[Note: using the contents of a file extends the tools to support
passwords which could not normally be provided using getpassphrase()
or via the command line.]
Hallvard B. Furuseth <h.b.furuseth@usit.uio.no>, Aug 2002.
[Kurt D. Zeilenga <kurt@openldap.org>, Aug 2002.]
2002-08-24 13:47:17 +08:00
|
|
|
if ( pw_file || want_bindpw ) {
|
|
|
|
if ( pw_file ) {
|
|
|
|
rc = lutil_get_filed_password( pw_file, &passwd );
|
|
|
|
if( rc ) return EXIT_FAILURE;
|
|
|
|
} else {
|
2003-04-06 10:59:15 +08:00
|
|
|
passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
|
Patch: 'ldapmodify -y file' reads password from file (ITS#2031)
================
Written by Hallvard B. Furuseth and placed into the public domain.
This software is not subject to any license of the University of Oslo.
================
Adapted by Kurt Zeilenga for inclusion in OpenLDAP. My comments are
marked with enclosed with square brackets (e.g. [Kurt's comment] below.
================
If I run ldapmodify & co from a script, I don't want to use '-W password'
because the password shows up in the output of 'ps' for everyone,
and I can't pipe the password to 'ldapmodify -w' because -w uses
getpassphrase() which reads from the tty instead of stdin.
So I added '-y file' which reads the password from file. The programs
exit if the file cannot be read.
[Complete contents of file is used as password. Use:
echo -n "secret" > password
to create a file with "secret" as the password. The -n avoids
adding a newline (which would invalidate the password). Note
that echo is a builtin and hence its arguments are not visible
to 'ps'.]
I changed ldapmodify, ldapmodrdn, ldapdelete, ldapsearch, ldapcompare.
I did not bother to change ldappasswd and ldapwhoami, because they
prompt for many passwords. [I fixed up ldapwhoami.]
Rerun autoconf after applying this patch. [Done.]
Note: I do not know if Windows NT has fstat(), so I set HAVE_FSTAT to
undef in portable.nt. (fstat() is used to warn if the file is publicly
readable or writeable.) [I used fstat() to set the buffer size to
read.]
[Note: using the contents of a file extends the tools to support
passwords which could not normally be provided using getpassphrase()
or via the command line.]
Hallvard B. Furuseth <h.b.furuseth@usit.uio.no>, Aug 2002.
[Kurt D. Zeilenga <kurt@openldap.org>, Aug 2002.]
2002-08-24 13:47:17 +08:00
|
|
|
passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
|
|
|
|
}
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
1998-12-15 10:00:09 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
tool_bind( ld );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-11-21 10:16:53 +08:00
|
|
|
getNextPage:
|
2003-07-30 18:21:16 +08:00
|
|
|
if ( assertion || authzid || manageDSAit || noop
|
2003-02-09 14:14:06 +08:00
|
|
|
#ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
|
2003-07-30 18:21:16 +08:00
|
|
|
|| domainScope
|
2003-02-09 14:14:06 +08:00
|
|
|
#endif
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2004-08-24 17:17:10 +08:00
|
|
|
|| pagedResults
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2005-01-26 00:11:26 +08:00
|
|
|
#ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
|
|
|
|
|| chaining
|
|
|
|
#endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
|
2003-07-30 18:21:16 +08:00
|
|
|
|| ldapsync
|
|
|
|
|| subentries || valuesReturnFilter )
|
|
|
|
{
|
1999-07-16 10:45:46 +08:00
|
|
|
int err;
|
2002-06-02 12:15:38 +08:00
|
|
|
int i=0;
|
2005-01-26 00:11:26 +08:00
|
|
|
LDAPControl c[10];
|
2003-02-09 14:14:06 +08:00
|
|
|
|
|
|
|
#ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
|
2005-01-26 00:11:26 +08:00
|
|
|
if ( domainScope ) {
|
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
|
|
|
|
c[i].ldctl_value.bv_val = NULL;
|
|
|
|
c[i].ldctl_value.bv_len = 0;
|
|
|
|
c[i].ldctl_iscritical = domainScope > 1;
|
|
|
|
i++;
|
|
|
|
}
|
2003-02-09 14:14:06 +08:00
|
|
|
#endif
|
2002-08-30 12:14:26 +08:00
|
|
|
|
2002-08-30 15:43:47 +08:00
|
|
|
#ifdef LDAP_CONTROL_SUBENTRIES
|
|
|
|
if ( subentries ) {
|
2005-01-26 00:11:26 +08:00
|
|
|
if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
|
2002-08-30 15:43:47 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-05-16 06:14:12 +08:00
|
|
|
err = ber_printf( seber, "b", abs(subentries) == 1 ? 0 : 1 );
|
2005-04-02 07:29:02 +08:00
|
|
|
if ( err == -1 ) {
|
2002-12-19 03:16:31 +08:00
|
|
|
ber_free( seber, 1 );
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("Subentries control encoding error!\n") );
|
2002-08-30 15:43:47 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-01-19 21:10:17 +08:00
|
|
|
if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
|
2002-08-30 15:43:47 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-11-28 10:26:55 +08:00
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
|
|
|
|
c[i].ldctl_iscritical = subentries < 1;
|
2002-12-13 00:32:26 +08:00
|
|
|
i++;
|
2002-08-30 15:43:47 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-02-05 15:37:02 +08:00
|
|
|
if ( ldapsync ) {
|
|
|
|
if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
|
2002-12-19 06:09:25 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2003-02-05 15:37:02 +08:00
|
|
|
|
|
|
|
if ( sync_cookie.bv_len == 0 ) {
|
|
|
|
err = ber_printf( syncber, "{e}", abs(ldapsync) );
|
|
|
|
} else {
|
|
|
|
err = ber_printf( syncber, "{eO}", abs(ldapsync),
|
|
|
|
&sync_cookie );
|
|
|
|
}
|
|
|
|
|
2002-12-19 06:09:25 +08:00
|
|
|
if ( err == LBER_ERROR ) {
|
2003-02-05 15:37:02 +08:00
|
|
|
ber_free( syncber, 1 );
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("ldap sync control encoding error!\n") );
|
2002-12-19 06:09:25 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-02-05 15:37:02 +08:00
|
|
|
if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
|
2002-12-19 06:09:25 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-02-05 15:37:02 +08:00
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_SYNC;
|
|
|
|
c[i].ldctl_value = (*syncbvalp);
|
|
|
|
c[i].ldctl_iscritical = ldapsync < 0;
|
2002-12-19 06:09:25 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2002-08-30 15:43:47 +08:00
|
|
|
if ( valuesReturnFilter ) {
|
2002-12-19 03:16:31 +08:00
|
|
|
if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
|
2002-06-09 14:12:24 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2002-06-02 12:15:38 +08:00
|
|
|
|
2002-12-19 03:16:31 +08:00
|
|
|
if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
|
|
|
|
ber_free( vrber, 1 );
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
|
2002-06-09 14:12:24 +08:00
|
|
|
return EXIT_FAILURE;
|
2002-06-02 12:15:38 +08:00
|
|
|
}
|
1999-07-16 10:45:46 +08:00
|
|
|
|
2003-01-19 21:10:17 +08:00
|
|
|
if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
|
2002-06-09 14:12:24 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
1999-07-16 10:45:46 +08:00
|
|
|
|
2002-11-28 10:26:55 +08:00
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
|
|
|
|
c[i].ldctl_iscritical = valuesReturnFilter > 1;
|
2002-12-13 00:32:26 +08:00
|
|
|
i++;
|
2002-06-02 12:15:38 +08:00
|
|
|
}
|
2002-08-29 18:41:10 +08:00
|
|
|
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2002-11-28 10:26:55 +08:00
|
|
|
if ( pagedResults ) {
|
2002-12-19 03:16:31 +08:00
|
|
|
if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
|
2002-11-21 10:16:53 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-12-21 07:02:17 +08:00
|
|
|
ber_printf( prber, "{iO}", pageSize, &page_cookie );
|
2003-01-19 21:10:17 +08:00
|
|
|
if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
|
2002-11-21 10:16:53 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2004-12-21 07:02:17 +08:00
|
|
|
if ( page_cookie.bv_val != NULL ) {
|
|
|
|
ber_memfree( page_cookie.bv_val );
|
|
|
|
page_cookie.bv_val = NULL;
|
|
|
|
}
|
2002-11-21 10:16:53 +08:00
|
|
|
|
2002-11-28 10:26:55 +08:00
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
|
|
|
|
c[i].ldctl_iscritical = pagedResults > 1;
|
2002-12-13 00:32:26 +08:00
|
|
|
i++;
|
2002-11-21 10:16:53 +08:00
|
|
|
}
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2002-11-21 10:16:53 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
tool_server_controls( ld, c, i );
|
2002-11-28 10:26:55 +08:00
|
|
|
|
2004-03-15 07:32:00 +08:00
|
|
|
#ifdef LDAP_CONTROL_SUBENTRIES
|
2002-12-19 03:16:31 +08:00
|
|
|
ber_free( seber, 1 );
|
2004-03-15 07:32:00 +08:00
|
|
|
#endif
|
2002-12-19 03:16:31 +08:00
|
|
|
ber_free( vrber, 1 );
|
2004-03-15 07:32:00 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2002-12-19 03:16:31 +08:00
|
|
|
ber_free( prber, 1 );
|
2004-03-15 07:32:00 +08:00
|
|
|
#endif
|
1999-07-16 10:45:46 +08:00
|
|
|
}
|
2002-08-30 12:14:26 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
if ( verbose ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("filter%s: %s\nrequesting: "),
|
|
|
|
infile != NULL ? _(" pattern") : "",
|
1999-07-31 07:00:02 +08:00
|
|
|
filtpattern );
|
|
|
|
|
|
|
|
if ( attrs == NULL ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("ALL") );
|
1999-07-31 07:00:02 +08:00
|
|
|
} else {
|
|
|
|
for ( i = 0; attrs[ i ] != NULL; ++i ) {
|
|
|
|
fprintf( stderr, "%s ", attrs[ i ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf( stderr, "\n" );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-07-31 07:00:02 +08:00
|
|
|
|
2001-06-22 04:32:04 +08:00
|
|
|
if ( ldif == 0 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("# extended LDIF\n") );
|
2001-06-22 04:32:04 +08:00
|
|
|
} else if ( ldif < 3 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("version: %d\n\n"), 1 );
|
2000-06-24 09:40:39 +08:00
|
|
|
}
|
1999-07-31 07:00:02 +08:00
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if (ldif < 2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( "#\n" );
|
|
|
|
printf(_("# LDAPv%d\n"), protocol);
|
|
|
|
printf(_("# base <%s> with scope %s\n"),
|
2003-12-18 01:55:27 +08:00
|
|
|
base ? base : "",
|
|
|
|
((scope == LDAP_SCOPE_BASE) ? "baseObject"
|
|
|
|
: ((scope == LDAP_SCOPE_ONELEVEL) ? "oneLevel"
|
2004-03-18 09:06:39 +08:00
|
|
|
#ifdef LDAP_SCOPE_SUBORDINATE
|
2003-12-18 01:55:27 +08:00
|
|
|
: ((scope == LDAP_SCOPE_SUBORDINATE) ? "children"
|
2004-03-18 09:06:39 +08:00
|
|
|
#endif
|
|
|
|
: "subtree"
|
|
|
|
#ifdef LDAP_SCOPE_SUBORDINATE
|
|
|
|
)
|
|
|
|
#endif
|
|
|
|
)));
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
|
|
|
|
filtpattern);
|
|
|
|
printf(_("# requesting: "));
|
1999-07-31 07:00:02 +08:00
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if ( attrs == NULL ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("ALL") );
|
2000-06-24 09:40:39 +08:00
|
|
|
} else {
|
|
|
|
for ( i = 0; attrs[ i ] != NULL; ++i ) {
|
|
|
|
printf( "%s ", attrs[ i ] );
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
if ( manageDSAit ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("\n# with manageDSAit %scontrol"),
|
|
|
|
manageDSAit > 1 ? _("critical ") : "" );
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
2002-08-30 12:22:22 +08:00
|
|
|
if ( noop ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("\n# with noop %scontrol"),
|
|
|
|
noop > 1 ? _("critical ") : "" );
|
2002-08-30 12:22:22 +08:00
|
|
|
}
|
2002-08-30 15:05:12 +08:00
|
|
|
if ( subentries ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("\n# with subentries %scontrol: %s"),
|
|
|
|
subentries < 0 ? _("critical ") : "",
|
2002-08-30 15:05:12 +08:00
|
|
|
abs(subentries) == 1 ? "false" : "true" );
|
|
|
|
}
|
2002-08-30 12:22:22 +08:00
|
|
|
if ( valuesReturnFilter ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("\n# with valuesReturnFilter %scontrol: %s"),
|
|
|
|
valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
|
2002-08-30 12:22:22 +08:00
|
|
|
}
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2004-08-24 17:17:10 +08:00
|
|
|
if ( pagedResults ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("\n# with pagedResults %scontrol: size=%d"),
|
|
|
|
(pagedResults > 1) ? _("critical ") : "",
|
2002-11-28 10:26:55 +08:00
|
|
|
pageSize );
|
2002-11-21 11:36:37 +08:00
|
|
|
}
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2000-06-25 08:35:17 +08:00
|
|
|
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("\n#\n\n") );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
if ( infile == NULL ) {
|
2000-06-26 02:07:15 +08:00
|
|
|
rc = dosearch( ld, base, scope, NULL, filtpattern,
|
|
|
|
attrs, attrsonly, NULL, NULL, NULL, -1 );
|
1998-11-06 09:18:49 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
first = 1;
|
2000-06-25 08:35:17 +08:00
|
|
|
while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
|
1999-07-31 07:00:02 +08:00
|
|
|
line[ strlen( line ) - 1 ] = '\0';
|
|
|
|
if ( !first ) {
|
|
|
|
putchar( '\n' );
|
|
|
|
} else {
|
|
|
|
first = 0;
|
|
|
|
}
|
2000-06-26 02:07:15 +08:00
|
|
|
rc = dosearch( ld, base, scope, filtpattern, line,
|
|
|
|
attrs, attrsonly, NULL, NULL, NULL, -1 );
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
|
|
|
if ( fp != stdin ) {
|
|
|
|
fclose( fp );
|
|
|
|
}
|
|
|
|
}
|
1999-05-30 02:53:40 +08:00
|
|
|
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2004-04-09 18:26:30 +08:00
|
|
|
if ( ( rc == LDAP_SUCCESS ) && ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
|
2002-11-21 11:36:37 +08:00
|
|
|
char buf[6];
|
|
|
|
int i, moreEntries, tmpSize;
|
|
|
|
|
2002-11-21 10:16:53 +08:00
|
|
|
/* Loop to get the next pages when
|
|
|
|
* enter is pressed on the terminal.
|
|
|
|
*/
|
2004-02-27 18:00:45 +08:00
|
|
|
if ( pagePrompt != 0 ) {
|
|
|
|
if ( entriesLeft > 0 ) {
|
|
|
|
printf( _("Estimate entries: %d\n"), entriesLeft );
|
2002-11-21 11:36:37 +08:00
|
|
|
}
|
2004-02-27 18:00:45 +08:00
|
|
|
printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
|
|
|
|
(int)pageSize );
|
|
|
|
i = 0;
|
2002-11-21 10:16:53 +08:00
|
|
|
moreEntries = getchar();
|
2004-02-27 18:00:45 +08:00
|
|
|
while ( moreEntries != EOF && moreEntries != '\n' ) {
|
|
|
|
if ( i < (int)sizeof(buf) - 1 ) {
|
|
|
|
buf[i] = moreEntries;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
moreEntries = getchar();
|
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
2002-11-21 11:36:37 +08:00
|
|
|
|
2004-02-27 18:00:45 +08:00
|
|
|
if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
|
|
|
|
int num = sscanf( buf, "%d", &tmpSize );
|
|
|
|
if ( num != 1 ) {
|
|
|
|
fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), buf);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
}
|
|
|
|
pageSize = (ber_int_t)tmpSize;
|
2002-11-21 11:36:37 +08:00
|
|
|
}
|
|
|
|
}
|
2002-11-21 10:16:53 +08:00
|
|
|
|
2004-08-24 17:17:10 +08:00
|
|
|
goto getNextPage;
|
2002-11-21 10:16:53 +08:00
|
|
|
}
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2002-11-21 10:16:53 +08:00
|
|
|
|
2005-04-02 07:29:02 +08:00
|
|
|
tool_unbind( ld );
|
|
|
|
tool_destroy();
|
1999-05-30 02:53:40 +08:00
|
|
|
return( rc );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
static int dosearch(
|
|
|
|
LDAP *ld,
|
1999-07-31 07:00:02 +08:00
|
|
|
char *base,
|
|
|
|
int scope,
|
2000-06-26 02:07:15 +08:00
|
|
|
char *filtpatt,
|
|
|
|
char *value,
|
1999-07-31 07:00:02 +08:00
|
|
|
char **attrs,
|
|
|
|
int attrsonly,
|
2000-06-26 02:07:15 +08:00
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls,
|
2001-01-24 04:38:19 +08:00
|
|
|
struct timeval *timeout,
|
2000-06-26 02:07:15 +08:00
|
|
|
int sizelimit )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2002-03-19 00:16:47 +08:00
|
|
|
char *filter;
|
2000-09-15 10:48:10 +08:00
|
|
|
int rc;
|
2000-06-24 09:40:39 +08:00
|
|
|
int nresponses;
|
|
|
|
int nentries;
|
|
|
|
int nreferences;
|
|
|
|
int nextended;
|
|
|
|
int npartial;
|
|
|
|
LDAPMessage *res, *msg;
|
2003-02-05 15:37:02 +08:00
|
|
|
ber_int_t msgid;
|
|
|
|
char *retoid = NULL;
|
|
|
|
struct berval *retdata = NULL;
|
|
|
|
int nresponses_psearch = -1;
|
|
|
|
int cancel_msgid = -1;
|
1999-07-31 07:00:02 +08:00
|
|
|
|
|
|
|
if( filtpatt != NULL ) {
|
2002-03-19 00:16:47 +08:00
|
|
|
filter = malloc( strlen( filtpatt ) + strlen( value ) );
|
|
|
|
if( filter == NULL ) {
|
|
|
|
perror( "malloc" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
sprintf( filter, filtpatt, value );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
if ( verbose ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("filter: %s\n"), filter );
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-06-25 08:35:17 +08:00
|
|
|
if( ldif < 2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("#\n# filter: %s\n#\n"), filter );
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
} else {
|
2002-03-19 00:16:47 +08:00
|
|
|
filter = value;
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
if ( not ) {
|
2000-06-25 08:35:17 +08:00
|
|
|
return LDAP_SUCCESS;
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
|
|
|
|
2000-06-26 02:07:15 +08:00
|
|
|
rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
|
2001-01-24 04:38:19 +08:00
|
|
|
sctrls, cctrls, timeout, sizelimit, &msgid );
|
2000-06-26 02:07:15 +08:00
|
|
|
|
2002-03-19 00:16:47 +08:00
|
|
|
if ( filtpatt != NULL ) {
|
|
|
|
free( filter );
|
|
|
|
}
|
|
|
|
|
2000-06-26 02:07:15 +08:00
|
|
|
if( rc != LDAP_SUCCESS ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
|
2000-07-18 05:54:20 +08:00
|
|
|
prog, ldap_err2string( rc ), rc );
|
2000-06-26 02:07:15 +08:00
|
|
|
return( rc );
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
nresponses = nentries = nreferences = nextended = npartial = 0;
|
|
|
|
|
1999-05-21 05:53:56 +08:00
|
|
|
res = NULL;
|
2000-07-05 05:49:00 +08:00
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
while ((rc = ldap_result( ld, LDAP_RES_ANY,
|
2000-07-05 05:49:00 +08:00
|
|
|
sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
|
|
|
|
NULL, &res )) > 0 )
|
2000-06-24 09:40:39 +08:00
|
|
|
{
|
2005-04-20 05:21:51 +08:00
|
|
|
rc = tool_check_abandon( ld, msgid );
|
|
|
|
if ( rc ) {
|
|
|
|
return rc;
|
2005-04-20 02:07:27 +08:00
|
|
|
}
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if( sortattr ) {
|
|
|
|
(void) ldap_sort_entries( ld, &res,
|
|
|
|
( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( msg = ldap_first_message( ld, res );
|
|
|
|
msg != NULL;
|
|
|
|
msg = ldap_next_message( ld, msg ) )
|
|
|
|
{
|
2003-02-05 15:37:02 +08:00
|
|
|
if ( nresponses++ ) putchar('\n');
|
|
|
|
if ( nresponses_psearch >= 0 )
|
|
|
|
nresponses_psearch++;
|
2000-06-24 09:40:39 +08:00
|
|
|
|
|
|
|
switch( ldap_msgtype( msg ) ) {
|
|
|
|
case LDAP_RES_SEARCH_ENTRY:
|
|
|
|
nentries++;
|
|
|
|
print_entry( ld, msg, attrsonly );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_RES_SEARCH_REFERENCE:
|
|
|
|
nreferences++;
|
|
|
|
print_reference( ld, msg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_RES_EXTENDED:
|
|
|
|
nextended++;
|
|
|
|
print_extended( ld, msg );
|
|
|
|
|
|
|
|
if( ldap_msgid( msg ) == 0 ) {
|
|
|
|
/* unsolicited extended operation */
|
|
|
|
goto done;
|
|
|
|
}
|
2003-02-05 15:37:02 +08:00
|
|
|
|
|
|
|
if ( cancel_msgid != -1 &&
|
|
|
|
cancel_msgid == ldap_msgid( msg ) ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("Cancelled \n"));
|
|
|
|
printf(_("cancel_msgid = %d\n"), cancel_msgid);
|
2003-02-05 15:37:02 +08:00
|
|
|
goto done;
|
|
|
|
}
|
2000-06-24 09:40:39 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_RES_SEARCH_RESULT:
|
2000-06-25 08:35:17 +08:00
|
|
|
rc = print_result( ld, msg, 1 );
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2004-06-22 02:40:20 +08:00
|
|
|
if ( pageSize != 0 ) {
|
|
|
|
if ( rc == LDAP_SUCCESS ) {
|
2004-12-21 07:02:17 +08:00
|
|
|
rc = parse_page_control( ld, msg, &page_cookie );
|
2004-06-22 02:40:20 +08:00
|
|
|
} else {
|
|
|
|
morePagedResults = 0;
|
|
|
|
}
|
2004-08-24 17:17:10 +08:00
|
|
|
} else {
|
|
|
|
morePagedResults = 0;
|
2002-11-21 10:16:53 +08:00
|
|
|
}
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|
2003-02-05 15:37:02 +08:00
|
|
|
|
|
|
|
if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
goto done;
|
2003-02-05 15:37:02 +08:00
|
|
|
|
2003-05-31 13:01:49 +08:00
|
|
|
case LDAP_RES_INTERMEDIATE:
|
|
|
|
npartial++;
|
|
|
|
ldap_parse_intermediate( ld, msg,
|
|
|
|
&retoid, &retdata, NULL, 0 );
|
|
|
|
|
2003-02-05 15:37:02 +08:00
|
|
|
nresponses_psearch = 0;
|
|
|
|
|
2003-05-31 13:01:49 +08:00
|
|
|
if ( strcmp( retoid, LDAP_SYNC_INFO ) == 0 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("SyncInfo Received\n"));
|
2003-02-05 15:37:02 +08:00
|
|
|
ldap_memfree( retoid );
|
|
|
|
ber_bvfree( retdata );
|
|
|
|
break;
|
|
|
|
}
|
2003-05-31 13:01:49 +08:00
|
|
|
|
|
|
|
print_partial( ld, msg );
|
|
|
|
ldap_memfree( retoid );
|
|
|
|
ber_bvfree( retdata );
|
|
|
|
goto done;
|
2000-06-24 09:40:39 +08:00
|
|
|
}
|
2003-02-05 15:37:02 +08:00
|
|
|
|
|
|
|
if ( ldapsync && sync_slimit != -1 &&
|
|
|
|
nresponses_psearch >= sync_slimit ) {
|
|
|
|
BerElement *msgidber = NULL;
|
|
|
|
struct berval *msgidvalp = NULL;
|
|
|
|
msgidber = ber_alloc_t(LBER_USE_DER);
|
|
|
|
ber_printf(msgidber, "{i}", msgid);
|
|
|
|
ber_flatten(msgidber, &msgidvalp);
|
|
|
|
ldap_extended_operation(ld, LDAP_EXOP_X_CANCEL,
|
|
|
|
msgidvalp, NULL, NULL, &cancel_msgid);
|
|
|
|
nresponses_psearch = -1;
|
|
|
|
}
|
2000-06-24 09:40:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ldap_msgfree( res );
|
1999-07-31 07:00:02 +08:00
|
|
|
}
|
2000-06-24 09:40:39 +08:00
|
|
|
|
2005-04-20 02:07:27 +08:00
|
|
|
done:
|
1999-07-31 07:00:02 +08:00
|
|
|
if ( rc == -1 ) {
|
2000-06-24 09:40:39 +08:00
|
|
|
ldap_perror( ld, "ldap_result" );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
2003-04-06 08:48:48 +08:00
|
|
|
ldap_msgfree( res );
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2004-08-24 17:17:10 +08:00
|
|
|
if ( pagedResults ) {
|
|
|
|
npagedresponses += nresponses;
|
|
|
|
npagedentries += nentries;
|
|
|
|
npagedextended += nextended;
|
|
|
|
npagedpartial += npartial;
|
|
|
|
npagedreferences += nreferences;
|
2002-11-21 10:16:53 +08:00
|
|
|
if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("\n# numResponses: %d\n"), npagedresponses );
|
2004-08-24 17:17:10 +08:00
|
|
|
if( npagedentries ) printf( _("# numEntries: %d\n"), npagedentries );
|
|
|
|
if( npagedextended ) printf( _("# numExtended: %d\n"), npagedextended );
|
|
|
|
if( npagedpartial ) printf( _("# numPartial: %d\n"), npagedpartial );
|
|
|
|
if( npagedreferences ) printf( _("# numReferences: %d\n"), npagedreferences );
|
2002-11-21 10:16:53 +08:00
|
|
|
}
|
2003-02-09 05:53:05 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if ( ldif < 2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("\n# numResponses: %d\n"), nresponses );
|
|
|
|
if( nentries ) printf( _("# numEntries: %d\n"), nentries );
|
|
|
|
if( nextended ) printf( _("# numExtended: %d\n"), nextended );
|
|
|
|
if( npartial ) printf( _("# numPartial: %d\n"), npartial );
|
|
|
|
if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
|
2000-06-24 09:40:39 +08:00
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( rc );
|
2000-06-24 09:40:39 +08:00
|
|
|
}
|
|
|
|
|
2002-09-06 12:57:21 +08:00
|
|
|
/* This is the proposed new way of doing things.
|
2002-11-28 10:26:55 +08:00
|
|
|
* It is more efficient, but the API is non-standard.
|
2002-09-06 12:57:21 +08:00
|
|
|
*/
|
2002-09-05 19:38:07 +08:00
|
|
|
static void
|
|
|
|
print_entry(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *entry,
|
|
|
|
int attrsonly)
|
|
|
|
{
|
2002-09-06 12:57:21 +08:00
|
|
|
char *ufn = NULL;
|
2002-09-05 19:38:07 +08:00
|
|
|
char tmpfname[ 256 ];
|
|
|
|
char url[ 256 ];
|
|
|
|
int i, rc;
|
|
|
|
BerElement *ber = NULL;
|
2002-09-06 12:57:21 +08:00
|
|
|
struct berval bv, *bvals, **bvp = &bvals;
|
2002-09-05 19:38:07 +08:00
|
|
|
LDAPControl **ctrls = NULL;
|
|
|
|
FILE *tmpfp;
|
|
|
|
|
|
|
|
rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
|
|
|
|
|
|
|
|
if ( ldif < 2 ) {
|
|
|
|
ufn = ldap_dn2ufn( bv.bv_val );
|
|
|
|
write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
|
|
|
|
}
|
|
|
|
write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
|
|
|
|
|
2003-10-18 20:59:55 +08:00
|
|
|
rc = ldap_get_entry_controls( ld, entry, &ctrls );
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
fprintf(stderr, _("print_entry: %d\n"), rc );
|
|
|
|
ldap_perror( ld, "ldap_get_entry_controls" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctrls ) {
|
|
|
|
print_ctrls( ctrls );
|
|
|
|
ldap_controls_free( ctrls );
|
|
|
|
}
|
|
|
|
|
2002-09-05 19:38:07 +08:00
|
|
|
if ( includeufn ) {
|
|
|
|
if( ufn == NULL ) {
|
|
|
|
ufn = ldap_dn2ufn( bv.bv_val );
|
|
|
|
}
|
|
|
|
write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ufn != NULL ) ldap_memfree( ufn );
|
|
|
|
|
2002-09-06 12:57:21 +08:00
|
|
|
if ( attrsonly ) bvp = NULL;
|
|
|
|
|
2002-11-28 10:44:38 +08:00
|
|
|
for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
|
|
|
|
rc == LDAP_SUCCESS;
|
2002-09-06 12:57:21 +08:00
|
|
|
rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
|
2002-09-05 19:38:07 +08:00
|
|
|
{
|
|
|
|
if (bv.bv_val == NULL) break;
|
|
|
|
|
2003-09-23 19:29:20 +08:00
|
|
|
if ( attrsonly ) {
|
2002-09-05 19:38:07 +08:00
|
|
|
write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
|
|
|
|
|
2003-09-23 19:29:20 +08:00
|
|
|
} else if ( bvals ) {
|
2002-09-05 19:38:07 +08:00
|
|
|
for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
|
2004-03-15 07:32:00 +08:00
|
|
|
if ( vals2tmp > 1 || ( vals2tmp &&
|
|
|
|
ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len )))
|
2002-09-05 19:38:07 +08:00
|
|
|
{
|
|
|
|
int tmpfd;
|
|
|
|
/* write value to file */
|
|
|
|
snprintf( tmpfname, sizeof tmpfname,
|
|
|
|
"%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
|
|
|
|
tmpdir, bv.bv_val );
|
|
|
|
tmpfp = NULL;
|
|
|
|
|
|
|
|
tmpfd = mkstemp( tmpfname );
|
|
|
|
|
|
|
|
if ( tmpfd < 0 ) {
|
|
|
|
perror( tmpfname );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
|
|
|
|
perror( tmpfname );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( fwrite( bvals[ i ].bv_val,
|
|
|
|
bvals[ i ].bv_len, 1, tmpfp ) == 0 )
|
|
|
|
{
|
|
|
|
perror( tmpfname );
|
|
|
|
fclose( tmpfp );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( tmpfp );
|
|
|
|
|
|
|
|
snprintf( url, sizeof url, "%s%s", urlpre,
|
|
|
|
&tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
|
|
|
|
|
|
|
|
urlize( url );
|
|
|
|
write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
write_ldif( LDIF_PUT_VALUE, bv.bv_val,
|
|
|
|
bvals[ i ].bv_val, bvals[ i ].bv_len );
|
|
|
|
}
|
|
|
|
}
|
2003-05-14 21:16:56 +08:00
|
|
|
ber_memfree( bvals );
|
2002-09-05 19:38:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ber != NULL ) {
|
|
|
|
ber_free( ber, 0 );
|
|
|
|
}
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-06-25 08:35:17 +08:00
|
|
|
static void print_reference(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *reference )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char **refs = NULL;
|
|
|
|
LDAPControl **ctrls;
|
|
|
|
|
|
|
|
if( ldif < 2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("# search reference\n"));
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
|
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror(ld, "ldap_parse_reference");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( refs ) {
|
|
|
|
int i;
|
|
|
|
for( i=0; refs[i] != NULL; i++ ) {
|
|
|
|
write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
|
|
|
|
"ref", refs[i], strlen(refs[i]) );
|
|
|
|
}
|
|
|
|
ber_memvfree( (void **) refs );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctrls ) {
|
|
|
|
print_ctrls( ctrls );
|
|
|
|
ldap_controls_free( ctrls );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_extended(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *extended )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *retoid = NULL;
|
|
|
|
struct berval *retdata = NULL;
|
|
|
|
|
|
|
|
if( ldif < 2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("# extended result response\n"));
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_parse_extended_result( ld, extended,
|
|
|
|
&retoid, &retdata, 0 );
|
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror(ld, "ldap_parse_extended_result");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2002-11-22 06:13:07 +08:00
|
|
|
if ( ldif < 2 ) {
|
|
|
|
write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
|
|
|
|
"extended", retoid, retoid ? strlen(retoid) : 0 );
|
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
ber_memfree( retoid );
|
|
|
|
|
|
|
|
if(retdata) {
|
2002-11-22 06:13:07 +08:00
|
|
|
if ( ldif < 2 ) {
|
|
|
|
write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
|
|
|
|
"data", retdata->bv_val, retdata->bv_len );
|
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
ber_bvfree( retdata );
|
|
|
|
}
|
|
|
|
|
|
|
|
print_result( ld, extended, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_partial(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *partial )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *retoid = NULL;
|
|
|
|
struct berval *retdata = NULL;
|
|
|
|
LDAPControl **ctrls = NULL;
|
|
|
|
|
|
|
|
if( ldif < 2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("# extended partial response\n"));
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
|
|
|
|
2003-05-31 13:01:49 +08:00
|
|
|
rc = ldap_parse_intermediate( ld, partial,
|
2000-06-25 08:35:17 +08:00
|
|
|
&retoid, &retdata, &ctrls, 0 );
|
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
2003-05-31 13:01:49 +08:00
|
|
|
ldap_perror(ld, "ldap_parse_intermediate");
|
2000-06-25 08:35:17 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2002-11-22 06:13:07 +08:00
|
|
|
if ( ldif < 2 ) {
|
|
|
|
write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
|
|
|
|
"partial", retoid, retoid ? strlen(retoid) : 0 );
|
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
ber_memfree( retoid );
|
|
|
|
|
|
|
|
if( retdata ) {
|
2002-11-22 06:13:07 +08:00
|
|
|
if ( ldif < 2 ) {
|
|
|
|
write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
|
|
|
|
"data", retdata->bv_val, retdata->bv_len );
|
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
ber_bvfree( retdata );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctrls ) {
|
|
|
|
print_ctrls( ctrls );
|
|
|
|
ldap_controls_free( ctrls );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_result(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *result, int search )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int err;
|
|
|
|
char *matcheddn = NULL;
|
|
|
|
char *text = NULL;
|
|
|
|
char **refs = NULL;
|
|
|
|
LDAPControl **ctrls = NULL;
|
|
|
|
|
|
|
|
if( search ) {
|
|
|
|
if ( ldif < 2 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf(_("# search result\n"));
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
|
|
|
if ( ldif < 1 ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf("%s: %d\n", _("search"), ldap_msgid(result) );
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_parse_result( ld, result,
|
|
|
|
&err, &matcheddn, &text, &refs, &ctrls, 0 );
|
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror(ld, "ldap_parse_result");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( !ldif ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
printf( _("result: %d %s\n"), err, ldap_err2string(err) );
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
} else if ( err != LDAP_SUCCESS ) {
|
|
|
|
fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
|
|
|
|
}
|
|
|
|
|
2003-04-06 08:48:48 +08:00
|
|
|
if( matcheddn ) {
|
|
|
|
if( *matcheddn ) {
|
2000-06-25 08:35:17 +08:00
|
|
|
if( !ldif ) {
|
|
|
|
write_ldif( LDIF_PUT_VALUE,
|
|
|
|
"matchedDN", matcheddn, strlen(matcheddn) );
|
|
|
|
} else {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
2003-04-06 08:48:48 +08:00
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
ber_memfree( matcheddn );
|
|
|
|
}
|
|
|
|
|
2003-04-06 08:48:48 +08:00
|
|
|
if( text ) {
|
|
|
|
if( *text ) {
|
2000-06-25 08:35:17 +08:00
|
|
|
if( !ldif ) {
|
|
|
|
write_ldif( LDIF_PUT_TEXT, "text",
|
|
|
|
text, strlen(text) );
|
|
|
|
} else {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("Additional information: %s\n"), text );
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
2003-04-06 08:48:48 +08:00
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
ber_memfree( text );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( refs ) {
|
|
|
|
int i;
|
|
|
|
for( i=0; refs[i] != NULL; i++ ) {
|
|
|
|
if( !ldif ) {
|
|
|
|
write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
|
|
|
|
} else {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("Referral: %s\n"), refs[i] );
|
2000-06-25 08:35:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_memvfree( (void **) refs );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctrls ) {
|
|
|
|
print_ctrls( ctrls );
|
|
|
|
ldap_controls_free( ctrls );
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2000-10-12 07:42:53 +08:00
|
|
|
static void print_ctrls(
|
|
|
|
LDAPControl **ctrls )
|
|
|
|
{
|
2000-06-25 08:35:17 +08:00
|
|
|
int i;
|
|
|
|
for(i=0; ctrls[i] != NULL; i++ ) {
|
|
|
|
/* control: OID criticality base64value */
|
|
|
|
struct berval *b64 = NULL;
|
|
|
|
ber_len_t len;
|
|
|
|
char *str;
|
2002-11-22 06:13:07 +08:00
|
|
|
|
|
|
|
len = ldif ? 2 : 0;
|
|
|
|
len += strlen( ctrls[i]->ldctl_oid );
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
/* add enough for space after OID and the critical value itself */
|
|
|
|
len += ctrls[i]->ldctl_iscritical
|
|
|
|
? sizeof("true") : sizeof("false");
|
|
|
|
|
|
|
|
/* convert to base64 */
|
|
|
|
if( ctrls[i]->ldctl_value.bv_len ) {
|
|
|
|
b64 = ber_memalloc( sizeof(struct berval) );
|
|
|
|
|
|
|
|
b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
|
|
|
|
ctrls[i]->ldctl_value.bv_len ) + 1;
|
|
|
|
b64->bv_val = ber_memalloc( b64->bv_len + 1 );
|
|
|
|
|
|
|
|
b64->bv_len = lutil_b64_ntop(
|
2003-05-02 21:29:28 +08:00
|
|
|
(unsigned char *) ctrls[i]->ldctl_value.bv_val,
|
|
|
|
ctrls[i]->ldctl_value.bv_len,
|
2000-06-25 08:35:17 +08:00
|
|
|
b64->bv_val, b64->bv_len );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( b64 ) {
|
|
|
|
len += 1 + b64->bv_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = malloc( len + 1 );
|
2002-11-22 06:13:07 +08:00
|
|
|
if ( ldif ) {
|
|
|
|
strcpy( str, ": " );
|
|
|
|
} else {
|
|
|
|
str[0] = '\0';
|
|
|
|
}
|
|
|
|
strcat( str, ctrls[i]->ldctl_oid );
|
2000-06-25 08:35:17 +08:00
|
|
|
strcat( str, ctrls[i]->ldctl_iscritical
|
|
|
|
? " true" : " false" );
|
|
|
|
|
|
|
|
if( b64 ) {
|
|
|
|
strcat(str, " ");
|
|
|
|
strcat(str, b64->bv_val );
|
|
|
|
}
|
|
|
|
|
2002-11-22 06:13:07 +08:00
|
|
|
if ( ldif < 2 ) {
|
|
|
|
write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
|
|
|
|
"control", str, len );
|
|
|
|
}
|
2000-06-25 08:35:17 +08:00
|
|
|
|
|
|
|
free( str );
|
|
|
|
ber_bvfree( b64 );
|
|
|
|
}
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-09-02 16:05:17 +08:00
|
|
|
static int
|
1999-07-31 07:00:02 +08:00
|
|
|
write_ldif( int type, char *name, char *value, ber_len_t vallen )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-07-31 07:00:02 +08:00
|
|
|
char *ldif;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
|
|
|
|
return( -1 );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
fputs( ldif, stdout );
|
|
|
|
ber_memfree( ldif );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-07-31 07:00:02 +08:00
|
|
|
return( 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2002-11-21 10:16:53 +08:00
|
|
|
|
|
|
|
|
2003-02-09 05:53:05 +08:00
|
|
|
#ifdef LDAP_CONTROL_PAGEDRESULTS
|
2002-11-21 10:16:53 +08:00
|
|
|
static int
|
|
|
|
parse_page_control(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAPMessage *result,
|
|
|
|
struct berval *cookie )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int err;
|
|
|
|
LDAPControl **ctrl = NULL;
|
|
|
|
LDAPControl *ctrlp = NULL;
|
|
|
|
BerElement *ber;
|
|
|
|
ber_tag_t tag;
|
|
|
|
|
|
|
|
rc = ldap_parse_result( ld, result,
|
|
|
|
&err, NULL, NULL, NULL, &ctrl, 0 );
|
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror(ld, "ldap_parse_result");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( err != LDAP_SUCCESS ) {
|
|
|
|
fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctrl ) {
|
|
|
|
/* Parse the control value
|
|
|
|
* searchResult ::= SEQUENCE {
|
|
|
|
* size INTEGER (0..maxInt),
|
|
|
|
* -- result set size estimate from server - unused
|
|
|
|
* cookie OCTET STRING
|
2003-02-09 05:53:05 +08:00
|
|
|
* }
|
2002-11-21 10:16:53 +08:00
|
|
|
*/
|
|
|
|
ctrlp = *ctrl;
|
|
|
|
ber = ber_init( &ctrlp->ldctl_value );
|
|
|
|
if ( ber == NULL ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fprintf( stderr, _("Internal error.\n") );
|
2002-11-21 10:16:53 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-12-21 07:02:17 +08:00
|
|
|
tag = ber_scanf( ber, "{io}", &entriesLeft, cookie );
|
2002-11-21 10:16:53 +08:00
|
|
|
(void) ber_free( ber, 1 );
|
|
|
|
|
|
|
|
if( tag == LBER_ERROR ) {
|
2003-02-09 05:53:05 +08:00
|
|
|
fprintf( stderr,
|
2003-04-06 10:59:15 +08:00
|
|
|
_("Paged results response control could not be decoded.\n") );
|
2002-11-21 10:16:53 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( entriesLeft < 0 ) {
|
2003-02-09 05:53:05 +08:00
|
|
|
fprintf( stderr,
|
2003-04-06 10:59:15 +08:00
|
|
|
_("Invalid entries estimate in paged results response.\n") );
|
2002-11-21 10:16:53 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-12-21 07:02:17 +08:00
|
|
|
if ( cookie->bv_len == 0 ) {
|
2004-02-27 16:16:36 +08:00
|
|
|
morePagedResults = 0;
|
|
|
|
}
|
|
|
|
|
2002-11-21 10:16:53 +08:00
|
|
|
ldap_controls_free( ctrl );
|
2003-02-09 05:53:05 +08:00
|
|
|
|
2002-11-21 10:16:53 +08:00
|
|
|
} else {
|
|
|
|
morePagedResults = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2003-02-09 05:53:05 +08:00
|
|
|
#endif
|