Home | History | Annotate | Line # | Download | only in tools
ldapsearch.c revision 1.1
      1  1.1  lukem /* ldapsearch -- a tool for searching LDAP directories */
      2  1.1  lukem /* $OpenLDAP: pkg/ldap/clients/tools/ldapsearch.c,v 1.234.2.9 2008/02/12 19:59:52 quanah Exp $ */
      3  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      4  1.1  lukem  *
      5  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      6  1.1  lukem  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
      7  1.1  lukem  * Portions Copyright 1998-2001 Net Boolean Incorporated.
      8  1.1  lukem  * Portions Copyright 2001-2003 IBM Corporation.
      9  1.1  lukem  * All rights reserved.
     10  1.1  lukem  *
     11  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     12  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     13  1.1  lukem  * Public License.
     14  1.1  lukem  *
     15  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     16  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     17  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     18  1.1  lukem  */
     19  1.1  lukem /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
     20  1.1  lukem  * All rights reserved.
     21  1.1  lukem  *
     22  1.1  lukem  * Redistribution and use in source and binary forms are permitted
     23  1.1  lukem  * provided that this notice is preserved and that due credit is given
     24  1.1  lukem  * to the University of Michigan at Ann Arbor.  The name of the
     25  1.1  lukem  * University may not be used to endorse or promote products derived
     26  1.1  lukem  * from this software without specific prior written permission.  This
     27  1.1  lukem  * software is provided ``as is'' without express or implied warranty.
     28  1.1  lukem  */
     29  1.1  lukem /* ACKNOWLEDGEMENTS:
     30  1.1  lukem  * This work was originally developed by the University of Michigan
     31  1.1  lukem  * (as part of U-MICH LDAP).  Additional significant contributors
     32  1.1  lukem  * include:
     33  1.1  lukem  *   Jong Hyuk Choi
     34  1.1  lukem  *   Lynn Moss
     35  1.1  lukem  *   Mikhail Sahalaev
     36  1.1  lukem  *   Kurt D. Zeilenga
     37  1.1  lukem  */
     38  1.1  lukem 
     39  1.1  lukem #include "portable.h"
     40  1.1  lukem 
     41  1.1  lukem #include <stdio.h>
     42  1.1  lukem 
     43  1.1  lukem #include <ac/stdlib.h>
     44  1.1  lukem 
     45  1.1  lukem #include <ac/ctype.h>
     46  1.1  lukem #include <ac/string.h>
     47  1.1  lukem #include <ac/unistd.h>
     48  1.1  lukem #include <ac/errno.h>
     49  1.1  lukem #include <sys/stat.h>
     50  1.1  lukem 
     51  1.1  lukem #include <ac/signal.h>
     52  1.1  lukem 
     53  1.1  lukem #ifdef HAVE_FCNTL_H
     54  1.1  lukem #include <fcntl.h>
     55  1.1  lukem #endif
     56  1.1  lukem #ifdef HAVE_SYS_TYPES_H
     57  1.1  lukem #include <sys/types.h>
     58  1.1  lukem #endif
     59  1.1  lukem #ifdef HAVE_IO_H
     60  1.1  lukem #include <io.h>
     61  1.1  lukem #endif
     62  1.1  lukem 
     63  1.1  lukem #include <ldap.h>
     64  1.1  lukem 
     65  1.1  lukem #include "ldif.h"
     66  1.1  lukem #include "lutil.h"
     67  1.1  lukem #include "lutil_ldap.h"
     68  1.1  lukem #include "ldap_defaults.h"
     69  1.1  lukem #include "ldap_log.h"
     70  1.1  lukem #include "ldap_pvt.h"
     71  1.1  lukem 
     72  1.1  lukem #include "common.h"
     73  1.1  lukem 
     74  1.1  lukem #if !LDAP_DEPRECATED
     75  1.1  lukem /*
     76  1.1  lukem  * NOTE: we use this deprecated function only because
     77  1.1  lukem  * we want ldapsearch to provide some client-side sorting
     78  1.1  lukem  * capability.
     79  1.1  lukem  */
     80  1.1  lukem /* from ldap.h */
     81  1.1  lukem typedef int (LDAP_SORT_AD_CMP_PROC) LDAP_P(( /* deprecated */
     82  1.1  lukem 	LDAP_CONST char *left,
     83  1.1  lukem 	LDAP_CONST char *right ));
     84  1.1  lukem 
     85  1.1  lukem LDAP_F( int )	/* deprecated */
     86  1.1  lukem ldap_sort_entries LDAP_P(( LDAP *ld,
     87  1.1  lukem 	LDAPMessage **chain,
     88  1.1  lukem 	LDAP_CONST char *attr,
     89  1.1  lukem 	LDAP_SORT_AD_CMP_PROC *cmp ));
     90  1.1  lukem #endif
     91  1.1  lukem 
     92  1.1  lukem static int scope = LDAP_SCOPE_SUBTREE;
     93  1.1  lukem static int deref = -1;
     94  1.1  lukem static int attrsonly;
     95  1.1  lukem static int timelimit = -1;
     96  1.1  lukem static int sizelimit = -1;
     97  1.1  lukem 
     98  1.1  lukem static char *control;
     99  1.1  lukem 
    100  1.1  lukem static char *def_tmpdir;
    101  1.1  lukem static char *def_urlpre;
    102  1.1  lukem 
    103  1.1  lukem #if defined(__CYGWIN__) || defined(__MINGW32__)
    104  1.1  lukem /* Turn off commandline globbing, otherwise you cannot search for
    105  1.1  lukem  * attribute '*'
    106  1.1  lukem  */
    107  1.1  lukem int _CRT_glob = 0;
    108  1.1  lukem #endif
    109  1.1  lukem 
    110  1.1  lukem void
    111  1.1  lukem usage( void )
    112  1.1  lukem {
    113  1.1  lukem 	fprintf( stderr, _("usage: %s [options] [filter [attributes...]]\nwhere:\n"), prog);
    114  1.1  lukem 	fprintf( stderr, _("  filter\tRFC 4515 compliant LDAP search filter\n"));
    115  1.1  lukem 	fprintf( stderr, _("  attributes\twhitespace-separated list of attribute descriptions\n"));
    116  1.1  lukem 	fprintf( stderr, _("    which may include:\n"));
    117  1.1  lukem 	fprintf( stderr, _("      1.1   no attributes\n"));
    118  1.1  lukem 	fprintf( stderr, _("      *     all user attributes\n"));
    119  1.1  lukem 	fprintf( stderr, _("      +     all operational attributes\n"));
    120  1.1  lukem 
    121  1.1  lukem 
    122  1.1  lukem 	fprintf( stderr, _("Search options:\n"));
    123  1.1  lukem 	fprintf( stderr, _("  -a deref   one of never (default), always, search, or find\n"));
    124  1.1  lukem 	fprintf( stderr, _("  -A         retrieve attribute names only (no values)\n"));
    125  1.1  lukem 	fprintf( stderr, _("  -b basedn  base dn for search\n"));
    126  1.1  lukem 	fprintf( stderr, _("  -E [!]<ext>[=<extparam>] search extensions (! indicates criticality)\n"));
    127  1.1  lukem 	fprintf( stderr, _("             [!]domainScope              (domain scope)\n"));
    128  1.1  lukem 	fprintf( stderr, _("             !dontUseCopy                (Don't Use Copy)\n"));
    129  1.1  lukem 	fprintf( stderr, _("             [!]mv=<filter>              (matched values filter)\n"));
    130  1.1  lukem 	fprintf( stderr, _("             [!]pr=<size>[/prompt|noprompt]   (paged results/prompt)\n"));
    131  1.1  lukem 	fprintf( stderr, _("             [!]subentries[=true|false]  (subentries)\n"));
    132  1.1  lukem 	fprintf( stderr, _("             [!]sync=ro[/<cookie>]            (LDAP Sync refreshOnly)\n"));
    133  1.1  lukem 	fprintf( stderr, _("                     rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
    134  1.1  lukem 	fprintf( stderr, _("             [!]<oid>=:<value>           (generic control; no response handling)\n"));
    135  1.1  lukem 	fprintf( stderr, _("  -F prefix  URL prefix for files (default: %s)\n"), def_urlpre);
    136  1.1  lukem 	fprintf( stderr, _("  -l limit   time limit (in seconds, or \"none\" or \"max\") for search\n"));
    137  1.1  lukem 	fprintf( stderr, _("  -L         print responses in LDIFv1 format\n"));
    138  1.1  lukem 	fprintf( stderr, _("  -LL        print responses in LDIF format without comments\n"));
    139  1.1  lukem 	fprintf( stderr, _("  -LLL       print responses in LDIF format without comments\n"));
    140  1.1  lukem 	fprintf( stderr, _("             and version\n"));
    141  1.1  lukem 	fprintf( stderr, _("  -s scope   one of base, one, sub or children (search scope)\n"));
    142  1.1  lukem 	fprintf( stderr, _("  -S attr    sort the results by attribute `attr'\n"));
    143  1.1  lukem 	fprintf( stderr, _("  -t         write binary values to files in temporary directory\n"));
    144  1.1  lukem 	fprintf( stderr, _("  -tt        write all values to files in temporary directory\n"));
    145  1.1  lukem 	fprintf( stderr, _("  -T path    write files to directory specified by path (default: %s)\n"), def_tmpdir);
    146  1.1  lukem 	fprintf( stderr, _("  -u         include User Friendly entry names in the output\n"));
    147  1.1  lukem 	fprintf( stderr, _("  -z limit   size limit (in entries, or \"none\" or \"max\") for search\n"));
    148  1.1  lukem 	tool_common_usage();
    149  1.1  lukem 	exit( EXIT_FAILURE );
    150  1.1  lukem }
    151  1.1  lukem 
    152  1.1  lukem static void print_entry LDAP_P((
    153  1.1  lukem 	LDAP	*ld,
    154  1.1  lukem 	LDAPMessage	*entry,
    155  1.1  lukem 	int		attrsonly));
    156  1.1  lukem 
    157  1.1  lukem static void print_reference(
    158  1.1  lukem 	LDAP *ld,
    159  1.1  lukem 	LDAPMessage *reference );
    160  1.1  lukem 
    161  1.1  lukem static void print_extended(
    162  1.1  lukem 	LDAP *ld,
    163  1.1  lukem 	LDAPMessage *extended );
    164  1.1  lukem 
    165  1.1  lukem static void print_partial(
    166  1.1  lukem 	LDAP *ld,
    167  1.1  lukem 	LDAPMessage *partial );
    168  1.1  lukem 
    169  1.1  lukem static int print_result(
    170  1.1  lukem 	LDAP *ld,
    171  1.1  lukem 	LDAPMessage *result,
    172  1.1  lukem 	int search );
    173  1.1  lukem 
    174  1.1  lukem static int dosearch LDAP_P((
    175  1.1  lukem 	LDAP	*ld,
    176  1.1  lukem 	char	*base,
    177  1.1  lukem 	int		scope,
    178  1.1  lukem 	char	*filtpatt,
    179  1.1  lukem 	char	*value,
    180  1.1  lukem 	char	**attrs,
    181  1.1  lukem 	int		attrsonly,
    182  1.1  lukem 	LDAPControl **sctrls,
    183  1.1  lukem 	LDAPControl **cctrls,
    184  1.1  lukem 	struct timeval *timeout,
    185  1.1  lukem 	int	sizelimit ));
    186  1.1  lukem 
    187  1.1  lukem static char *tmpdir = NULL;
    188  1.1  lukem static char *urlpre = NULL;
    189  1.1  lukem static char	*base = NULL;
    190  1.1  lukem static char	*sortattr = NULL;
    191  1.1  lukem static int  includeufn, vals2tmp = 0;
    192  1.1  lukem 
    193  1.1  lukem static int subentries = 0, valuesReturnFilter = 0;
    194  1.1  lukem static char	*vrFilter = NULL;
    195  1.1  lukem 
    196  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    197  1.1  lukem static int dontUseCopy = 0;
    198  1.1  lukem #endif
    199  1.1  lukem 
    200  1.1  lukem static int domainScope = 0;
    201  1.1  lukem 
    202  1.1  lukem static int ldapsync = 0;
    203  1.1  lukem static struct berval sync_cookie = { 0, NULL };
    204  1.1  lukem static int sync_slimit = -1;
    205  1.1  lukem 
    206  1.1  lukem /* cookie and morePagedResults moved to common.c */
    207  1.1  lukem static int pagedResults = 0;
    208  1.1  lukem static int pagePrompt = 1;
    209  1.1  lukem static ber_int_t pageSize = 0;
    210  1.1  lukem static ber_int_t entriesLeft = 0;
    211  1.1  lukem static int npagedresponses;
    212  1.1  lukem static int npagedentries;
    213  1.1  lukem static int npagedreferences;
    214  1.1  lukem static int npagedextended;
    215  1.1  lukem static int npagedpartial;
    216  1.1  lukem 
    217  1.1  lukem static LDAPControl *c = NULL;
    218  1.1  lukem static int nctrls = 0;
    219  1.1  lukem static int save_nctrls = 0;
    220  1.1  lukem 
    221  1.1  lukem static int
    222  1.1  lukem ctrl_add( void )
    223  1.1  lukem {
    224  1.1  lukem 	LDAPControl	*tmpc;
    225  1.1  lukem 
    226  1.1  lukem 	nctrls++;
    227  1.1  lukem 	tmpc = realloc( c, sizeof( LDAPControl ) * nctrls );
    228  1.1  lukem 	if ( tmpc == NULL ) {
    229  1.1  lukem 		nctrls--;
    230  1.1  lukem 		fprintf( stderr,
    231  1.1  lukem 			_("unable to make room for control; out of memory?\n"));
    232  1.1  lukem 		return -1;
    233  1.1  lukem 	}
    234  1.1  lukem 	c = tmpc;
    235  1.1  lukem 
    236  1.1  lukem 	return 0;
    237  1.1  lukem }
    238  1.1  lukem 
    239  1.1  lukem static void
    240  1.1  lukem urlize(char *url)
    241  1.1  lukem {
    242  1.1  lukem 	char *p;
    243  1.1  lukem 
    244  1.1  lukem 	if (*LDAP_DIRSEP != '/') {
    245  1.1  lukem 		for (p = url; *p; p++) {
    246  1.1  lukem 			if (*p == *LDAP_DIRSEP)
    247  1.1  lukem 				*p = '/';
    248  1.1  lukem 		}
    249  1.1  lukem 	}
    250  1.1  lukem }
    251  1.1  lukem 
    252  1.1  lukem 
    253  1.1  lukem const char options[] = "a:Ab:cE:F:l:Ls:S:tT:uz:"
    254  1.1  lukem 	"Cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
    255  1.1  lukem 
    256  1.1  lukem int
    257  1.1  lukem handle_private_option( int i )
    258  1.1  lukem {
    259  1.1  lukem 	int crit, ival;
    260  1.1  lukem 	char *cvalue, *next;
    261  1.1  lukem 	switch ( i ) {
    262  1.1  lukem 	case 'a':	/* set alias deref option */
    263  1.1  lukem 		if ( strcasecmp( optarg, "never" ) == 0 ) {
    264  1.1  lukem 			deref = LDAP_DEREF_NEVER;
    265  1.1  lukem 		} else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
    266  1.1  lukem 			deref = LDAP_DEREF_SEARCHING;
    267  1.1  lukem 		} else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
    268  1.1  lukem 			deref = LDAP_DEREF_FINDING;
    269  1.1  lukem 		} else if ( strcasecmp( optarg, "always" ) == 0 ) {
    270  1.1  lukem 			deref = LDAP_DEREF_ALWAYS;
    271  1.1  lukem 		} else {
    272  1.1  lukem 			fprintf( stderr,
    273  1.1  lukem 				_("alias deref should be never, search, find, or always\n") );
    274  1.1  lukem 			usage();
    275  1.1  lukem 		}
    276  1.1  lukem 		break;
    277  1.1  lukem 	case 'A':	/* retrieve attribute names only -- no values */
    278  1.1  lukem 		++attrsonly;
    279  1.1  lukem 		break;
    280  1.1  lukem 	case 'b': /* search base */
    281  1.1  lukem 		base = ber_strdup( optarg );
    282  1.1  lukem 		break;
    283  1.1  lukem 	case 'E': /* search extensions */
    284  1.1  lukem 		if( protocol == LDAP_VERSION2 ) {
    285  1.1  lukem 			fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
    286  1.1  lukem 				prog, protocol );
    287  1.1  lukem 			exit( EXIT_FAILURE );
    288  1.1  lukem 		}
    289  1.1  lukem 
    290  1.1  lukem 		/* should be extended to support comma separated list of
    291  1.1  lukem 		 *	[!]key[=value] parameters, e.g.  -E !foo,bar=567
    292  1.1  lukem 		 */
    293  1.1  lukem 
    294  1.1  lukem 		crit = 0;
    295  1.1  lukem 		cvalue = NULL;
    296  1.1  lukem 		if( optarg[0] == '!' ) {
    297  1.1  lukem 			crit = 1;
    298  1.1  lukem 			optarg++;
    299  1.1  lukem 		}
    300  1.1  lukem 
    301  1.1  lukem 		control = ber_strdup( optarg );
    302  1.1  lukem 		if ( (cvalue = strchr( control, '=' )) != NULL ) {
    303  1.1  lukem 			*cvalue++ = '\0';
    304  1.1  lukem 		}
    305  1.1  lukem 
    306  1.1  lukem 		if ( strcasecmp( control, "mv" ) == 0 ) {
    307  1.1  lukem 			/* ValuesReturnFilter control */
    308  1.1  lukem 			if( valuesReturnFilter ) {
    309  1.1  lukem 				fprintf( stderr,
    310  1.1  lukem 					_("ValuesReturnFilter previously specified\n"));
    311  1.1  lukem 				exit( EXIT_FAILURE );
    312  1.1  lukem 			}
    313  1.1  lukem 			valuesReturnFilter= 1 + crit;
    314  1.1  lukem 
    315  1.1  lukem 			if ( cvalue == NULL ) {
    316  1.1  lukem 				fprintf( stderr,
    317  1.1  lukem 					_("missing filter in ValuesReturnFilter control\n"));
    318  1.1  lukem 				exit( EXIT_FAILURE );
    319  1.1  lukem 			}
    320  1.1  lukem 
    321  1.1  lukem 			vrFilter = cvalue;
    322  1.1  lukem 			protocol = LDAP_VERSION3;
    323  1.1  lukem 
    324  1.1  lukem 		} else if ( strcasecmp( control, "pr" ) == 0 ) {
    325  1.1  lukem 			int num, tmp;
    326  1.1  lukem 			/* PagedResults control */
    327  1.1  lukem 			if ( pagedResults != 0 ) {
    328  1.1  lukem 				fprintf( stderr,
    329  1.1  lukem 					_("PagedResultsControl previously specified\n") );
    330  1.1  lukem 				exit( EXIT_FAILURE );
    331  1.1  lukem 			}
    332  1.1  lukem 
    333  1.1  lukem 			if( cvalue != NULL ) {
    334  1.1  lukem 				char *promptp;
    335  1.1  lukem 
    336  1.1  lukem 				promptp = strchr( cvalue, '/' );
    337  1.1  lukem 				if ( promptp != NULL ) {
    338  1.1  lukem 					*promptp++ = '\0';
    339  1.1  lukem 					if ( strcasecmp( promptp, "prompt" ) == 0 ) {
    340  1.1  lukem 						pagePrompt = 1;
    341  1.1  lukem 					} else if ( strcasecmp( promptp, "noprompt" ) == 0) {
    342  1.1  lukem 						pagePrompt = 0;
    343  1.1  lukem 					} else {
    344  1.1  lukem 						fprintf( stderr,
    345  1.1  lukem 							_("Invalid value for PagedResultsControl,"
    346  1.1  lukem 							" %s/%s.\n"), cvalue, promptp );
    347  1.1  lukem 						exit( EXIT_FAILURE );
    348  1.1  lukem 					}
    349  1.1  lukem 				}
    350  1.1  lukem 				num = sscanf( cvalue, "%d", &tmp );
    351  1.1  lukem 				if ( num != 1 ) {
    352  1.1  lukem 					fprintf( stderr,
    353  1.1  lukem 						_("Invalid value for PagedResultsControl, %s.\n"),
    354  1.1  lukem 						cvalue );
    355  1.1  lukem 					exit( EXIT_FAILURE );
    356  1.1  lukem 				}
    357  1.1  lukem 			} else {
    358  1.1  lukem 				fprintf(stderr, _("Invalid value for PagedResultsControl.\n"));
    359  1.1  lukem 				exit( EXIT_FAILURE );
    360  1.1  lukem 			}
    361  1.1  lukem 			pageSize = (ber_int_t) tmp;
    362  1.1  lukem 			pagedResults = 1 + crit;
    363  1.1  lukem 
    364  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    365  1.1  lukem 		} else if ( strcasecmp( control, "dontUseCopy" ) == 0 ) {
    366  1.1  lukem 			if( dontUseCopy ) {
    367  1.1  lukem 				fprintf( stderr,
    368  1.1  lukem 					_("dontUseCopy control previously specified\n"));
    369  1.1  lukem 				exit( EXIT_FAILURE );
    370  1.1  lukem 			}
    371  1.1  lukem 			if( cvalue != NULL ) {
    372  1.1  lukem 				fprintf( stderr,
    373  1.1  lukem 			         _("dontUseCopy: no control value expected\n") );
    374  1.1  lukem 				usage();
    375  1.1  lukem 			}
    376  1.1  lukem 			if( !crit ) {
    377  1.1  lukem 				fprintf( stderr,
    378  1.1  lukem 			         _("dontUseCopy: critical flag required\n") );
    379  1.1  lukem 				usage();
    380  1.1  lukem 			}
    381  1.1  lukem 
    382  1.1  lukem 			dontUseCopy = 1 + crit;
    383  1.1  lukem #endif
    384  1.1  lukem 		} else if ( strcasecmp( control, "domainScope" ) == 0 ) {
    385  1.1  lukem 			if( domainScope ) {
    386  1.1  lukem 				fprintf( stderr,
    387  1.1  lukem 					_("domainScope control previously specified\n"));
    388  1.1  lukem 				exit( EXIT_FAILURE );
    389  1.1  lukem 			}
    390  1.1  lukem 			if( cvalue != NULL ) {
    391  1.1  lukem 				fprintf( stderr,
    392  1.1  lukem 			         _("domainScope: no control value expected\n") );
    393  1.1  lukem 				usage();
    394  1.1  lukem 			}
    395  1.1  lukem 
    396  1.1  lukem 			domainScope = 1 + crit;
    397  1.1  lukem 
    398  1.1  lukem 		} else if ( strcasecmp( control, "subentries" ) == 0 ) {
    399  1.1  lukem 			if( subentries ) {
    400  1.1  lukem 				fprintf( stderr,
    401  1.1  lukem 					_("subentries control previously specified\n"));
    402  1.1  lukem 				exit( EXIT_FAILURE );
    403  1.1  lukem 			}
    404  1.1  lukem 			if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
    405  1.1  lukem 				subentries = 2;
    406  1.1  lukem 			} else if ( strcasecmp( cvalue, "false") == 0 ) {
    407  1.1  lukem 				subentries = 1;
    408  1.1  lukem 			} else {
    409  1.1  lukem 				fprintf( stderr,
    410  1.1  lukem 					_("subentries control value \"%s\" invalid\n"),
    411  1.1  lukem 					cvalue );
    412  1.1  lukem 				exit( EXIT_FAILURE );
    413  1.1  lukem 			}
    414  1.1  lukem 			if( crit ) subentries *= -1;
    415  1.1  lukem 
    416  1.1  lukem 		} else if ( strcasecmp( control, "sync" ) == 0 ) {
    417  1.1  lukem 			char *cookiep;
    418  1.1  lukem 			char *slimitp;
    419  1.1  lukem 			if ( ldapsync ) {
    420  1.1  lukem 				fprintf( stderr, _("sync control previously specified\n") );
    421  1.1  lukem 				exit( EXIT_FAILURE );
    422  1.1  lukem 			}
    423  1.1  lukem 			if ( cvalue == NULL ) {
    424  1.1  lukem 				fprintf( stderr, _("missing specification of sync control\n"));
    425  1.1  lukem 				exit( EXIT_FAILURE );
    426  1.1  lukem 			}
    427  1.1  lukem 			if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
    428  1.1  lukem 				ldapsync = LDAP_SYNC_REFRESH_ONLY;
    429  1.1  lukem 				cookiep = strchr( cvalue, '/' );
    430  1.1  lukem 				if ( cookiep != NULL ) {
    431  1.1  lukem 					cookiep++;
    432  1.1  lukem 					if ( *cookiep != '\0' ) {
    433  1.1  lukem 						ber_str2bv( cookiep, 0, 0, &sync_cookie );
    434  1.1  lukem 					}
    435  1.1  lukem 				}
    436  1.1  lukem 			} else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
    437  1.1  lukem 				ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
    438  1.1  lukem 				cookiep = strchr( cvalue, '/' );
    439  1.1  lukem 				if ( cookiep != NULL ) {
    440  1.1  lukem 					*cookiep++ = '\0';
    441  1.1  lukem 					cvalue = cookiep;
    442  1.1  lukem 				}
    443  1.1  lukem 				slimitp = strchr( cvalue, '/' );
    444  1.1  lukem 				if ( slimitp != NULL ) {
    445  1.1  lukem 					*slimitp++ = '\0';
    446  1.1  lukem 				}
    447  1.1  lukem 				if ( cookiep != NULL && *cookiep != '\0' )
    448  1.1  lukem 					ber_str2bv( cookiep, 0, 0, &sync_cookie );
    449  1.1  lukem 				if ( slimitp != NULL && *slimitp != '\0' ) {
    450  1.1  lukem 					ival = strtol( slimitp, &next, 10 );
    451  1.1  lukem 					if ( next == NULL || next[0] != '\0' ) {
    452  1.1  lukem 						fprintf( stderr, _("Unable to parse sync control value \"%s\"\n"), slimitp );
    453  1.1  lukem 						exit( EXIT_FAILURE );
    454  1.1  lukem 					}
    455  1.1  lukem 					sync_slimit = ival;
    456  1.1  lukem 				}
    457  1.1  lukem 			} else {
    458  1.1  lukem 				fprintf( stderr, _("sync control value \"%s\" invalid\n"),
    459  1.1  lukem 					cvalue );
    460  1.1  lukem 				exit( EXIT_FAILURE );
    461  1.1  lukem 			}
    462  1.1  lukem 			if ( crit ) ldapsync *= -1;
    463  1.1  lukem 
    464  1.1  lukem 		} else if ( tool_is_oid( control ) ) {
    465  1.1  lukem 			if ( ctrl_add() ) {
    466  1.1  lukem 				exit( EXIT_FAILURE );
    467  1.1  lukem 			}
    468  1.1  lukem 
    469  1.1  lukem 			/* OID */
    470  1.1  lukem 			c[ nctrls - 1 ].ldctl_oid = control;
    471  1.1  lukem 
    472  1.1  lukem 			/* value */
    473  1.1  lukem 			if ( cvalue == NULL ) {
    474  1.1  lukem 				c[ nctrls - 1 ].ldctl_value.bv_val = NULL;
    475  1.1  lukem 				c[ nctrls - 1 ].ldctl_value.bv_len = 0;
    476  1.1  lukem 
    477  1.1  lukem 			} else if ( cvalue[ 0 ] == ':' ) {
    478  1.1  lukem 				struct berval	type;
    479  1.1  lukem 				struct berval	value;
    480  1.1  lukem 				int		freeval;
    481  1.1  lukem 
    482  1.1  lukem 				cvalue++;
    483  1.1  lukem 
    484  1.1  lukem 				/* dummy type "x"
    485  1.1  lukem 				 * to use ldif_parse_line2() */
    486  1.1  lukem 				cvalue[ -2 ] = 'x';
    487  1.1  lukem 				ldif_parse_line2( &cvalue[ -2 ], &type,
    488  1.1  lukem 					&value, &freeval );
    489  1.1  lukem 				cvalue[ -2 ] = '\0';
    490  1.1  lukem 
    491  1.1  lukem 				if ( freeval ) {
    492  1.1  lukem 					c[ nctrls - 1 ].ldctl_value = value;
    493  1.1  lukem 
    494  1.1  lukem 				} else {
    495  1.1  lukem 					ber_dupbv( &c[ nctrls - 1 ].ldctl_value, &value );
    496  1.1  lukem 				}
    497  1.1  lukem 			}
    498  1.1  lukem 
    499  1.1  lukem 			/* criticality */
    500  1.1  lukem 			c[ nctrls - 1 ].ldctl_iscritical = crit;
    501  1.1  lukem 
    502  1.1  lukem 		} else {
    503  1.1  lukem 			fprintf( stderr, _("Invalid search extension name: %s\n"),
    504  1.1  lukem 				control );
    505  1.1  lukem 			usage();
    506  1.1  lukem 		}
    507  1.1  lukem 		break;
    508  1.1  lukem 	case 'F':	/* uri prefix */
    509  1.1  lukem 		if( urlpre ) free( urlpre );
    510  1.1  lukem 		urlpre = strdup( optarg );
    511  1.1  lukem 		break;
    512  1.1  lukem 	case 'l':	/* time limit */
    513  1.1  lukem 		if ( strcasecmp( optarg, "none" ) == 0 ) {
    514  1.1  lukem 			timelimit = 0;
    515  1.1  lukem 
    516  1.1  lukem 		} else if ( strcasecmp( optarg, "max" ) == 0 ) {
    517  1.1  lukem 			timelimit = LDAP_MAXINT;
    518  1.1  lukem 
    519  1.1  lukem 		} else {
    520  1.1  lukem 			ival = strtol( optarg, &next, 10 );
    521  1.1  lukem 			if ( next == NULL || next[0] != '\0' ) {
    522  1.1  lukem 				fprintf( stderr,
    523  1.1  lukem 					_("Unable to parse time limit \"%s\"\n"), optarg );
    524  1.1  lukem 				exit( EXIT_FAILURE );
    525  1.1  lukem 			}
    526  1.1  lukem 			timelimit = ival;
    527  1.1  lukem 		}
    528  1.1  lukem 		if( timelimit < 0 || timelimit > LDAP_MAXINT ) {
    529  1.1  lukem 			fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
    530  1.1  lukem 				prog, timelimit );
    531  1.1  lukem 			exit( EXIT_FAILURE );
    532  1.1  lukem 		}
    533  1.1  lukem 		break;
    534  1.1  lukem 	case 'L':	/* print entries in LDIF format */
    535  1.1  lukem 		++ldif;
    536  1.1  lukem 		break;
    537  1.1  lukem 	case 's':	/* search scope */
    538  1.1  lukem 		if ( strncasecmp( optarg, "base", sizeof("base")-1 ) == 0 ) {
    539  1.1  lukem 			scope = LDAP_SCOPE_BASE;
    540  1.1  lukem 		} else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
    541  1.1  lukem 			scope = LDAP_SCOPE_ONELEVEL;
    542  1.1  lukem 		} else if (( strcasecmp( optarg, "subordinate" ) == 0 )
    543  1.1  lukem 			|| ( strcasecmp( optarg, "children" ) == 0 ))
    544  1.1  lukem 		{
    545  1.1  lukem 			scope = LDAP_SCOPE_SUBORDINATE;
    546  1.1  lukem 		} else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
    547  1.1  lukem 			scope = LDAP_SCOPE_SUBTREE;
    548  1.1  lukem 		} else {
    549  1.1  lukem 			fprintf( stderr, _("scope should be base, one, or sub\n") );
    550  1.1  lukem 			usage();
    551  1.1  lukem 		}
    552  1.1  lukem 		break;
    553  1.1  lukem 	case 'S':	/* sort attribute */
    554  1.1  lukem 		sortattr = strdup( optarg );
    555  1.1  lukem 		break;
    556  1.1  lukem 	case 't':	/* write attribute values to TMPDIR files */
    557  1.1  lukem 		++vals2tmp;
    558  1.1  lukem 		break;
    559  1.1  lukem 	case 'T':	/* tmpdir */
    560  1.1  lukem 		if( tmpdir ) free( tmpdir );
    561  1.1  lukem 		tmpdir = strdup( optarg );
    562  1.1  lukem 		break;
    563  1.1  lukem 	case 'u':	/* include UFN */
    564  1.1  lukem 		++includeufn;
    565  1.1  lukem 		break;
    566  1.1  lukem 	case 'z':	/* size limit */
    567  1.1  lukem 		if ( strcasecmp( optarg, "none" ) == 0 ) {
    568  1.1  lukem 			sizelimit = 0;
    569  1.1  lukem 
    570  1.1  lukem 		} else if ( strcasecmp( optarg, "max" ) == 0 ) {
    571  1.1  lukem 			sizelimit = LDAP_MAXINT;
    572  1.1  lukem 
    573  1.1  lukem 		} else {
    574  1.1  lukem 			ival = strtol( optarg, &next, 10 );
    575  1.1  lukem 			if ( next == NULL || next[0] != '\0' ) {
    576  1.1  lukem 				fprintf( stderr,
    577  1.1  lukem 					_("Unable to parse size limit \"%s\"\n"), optarg );
    578  1.1  lukem 				exit( EXIT_FAILURE );
    579  1.1  lukem 			}
    580  1.1  lukem 			sizelimit = ival;
    581  1.1  lukem 		}
    582  1.1  lukem 		if( sizelimit < 0 || sizelimit > LDAP_MAXINT ) {
    583  1.1  lukem 			fprintf( stderr, _("%s: invalid sizelimit (%d) specified\n"),
    584  1.1  lukem 				prog, sizelimit );
    585  1.1  lukem 			exit( EXIT_FAILURE );
    586  1.1  lukem 		}
    587  1.1  lukem 		break;
    588  1.1  lukem 	default:
    589  1.1  lukem 		return 0;
    590  1.1  lukem 	}
    591  1.1  lukem 	return 1;
    592  1.1  lukem }
    593  1.1  lukem 
    594  1.1  lukem 
    595  1.1  lukem static void
    596  1.1  lukem private_conn_setup( LDAP *ld )
    597  1.1  lukem {
    598  1.1  lukem 	if (deref != -1 &&
    599  1.1  lukem 		ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref )
    600  1.1  lukem 			!= LDAP_OPT_SUCCESS )
    601  1.1  lukem 	{
    602  1.1  lukem 		fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
    603  1.1  lukem 		exit( EXIT_FAILURE );
    604  1.1  lukem 	}
    605  1.1  lukem 	if (timelimit > 0 &&
    606  1.1  lukem 		ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit )
    607  1.1  lukem 			!= LDAP_OPT_SUCCESS )
    608  1.1  lukem 	{
    609  1.1  lukem 		fprintf( stderr,
    610  1.1  lukem 			_("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
    611  1.1  lukem 		exit( EXIT_FAILURE );
    612  1.1  lukem 	}
    613  1.1  lukem 	if (sizelimit > 0 &&
    614  1.1  lukem 		ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit )
    615  1.1  lukem 			!= LDAP_OPT_SUCCESS )
    616  1.1  lukem 	{
    617  1.1  lukem 		fprintf( stderr,
    618  1.1  lukem 			_("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
    619  1.1  lukem 		exit( EXIT_FAILURE );
    620  1.1  lukem 	}
    621  1.1  lukem }
    622  1.1  lukem 
    623  1.1  lukem int
    624  1.1  lukem main( int argc, char **argv )
    625  1.1  lukem {
    626  1.1  lukem 	char		*filtpattern, **attrs = NULL, line[BUFSIZ];
    627  1.1  lukem 	FILE		*fp = NULL;
    628  1.1  lukem 	int			rc, rc1, i, first;
    629  1.1  lukem 	LDAP		*ld = NULL;
    630  1.1  lukem 	BerElement	*seber = NULL, *vrber = NULL;
    631  1.1  lukem 
    632  1.1  lukem 	BerElement      *syncber = NULL;
    633  1.1  lukem 	struct berval   *syncbvalp = NULL;
    634  1.1  lukem 	int		err;
    635  1.1  lukem 
    636  1.1  lukem 	tool_init( TOOL_SEARCH );
    637  1.1  lukem 
    638  1.1  lukem 	npagedresponses = npagedentries = npagedreferences =
    639  1.1  lukem 		npagedextended = npagedpartial = 0;
    640  1.1  lukem 
    641  1.1  lukem 	prog = lutil_progname( "ldapsearch", argc, argv );
    642  1.1  lukem 
    643  1.1  lukem 	if((def_tmpdir = getenv("TMPDIR")) == NULL &&
    644  1.1  lukem 	   (def_tmpdir = getenv("TMP")) == NULL &&
    645  1.1  lukem 	   (def_tmpdir = getenv("TEMP")) == NULL )
    646  1.1  lukem 	{
    647  1.1  lukem 		def_tmpdir = LDAP_TMPDIR;
    648  1.1  lukem 	}
    649  1.1  lukem 
    650  1.1  lukem 	if ( !*def_tmpdir )
    651  1.1  lukem 		def_tmpdir = LDAP_TMPDIR;
    652  1.1  lukem 
    653  1.1  lukem 	def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
    654  1.1  lukem 
    655  1.1  lukem 	if( def_urlpre == NULL ) {
    656  1.1  lukem 		perror( "malloc" );
    657  1.1  lukem 		return EXIT_FAILURE;
    658  1.1  lukem 	}
    659  1.1  lukem 
    660  1.1  lukem 	sprintf( def_urlpre, "file:///%s/",
    661  1.1  lukem 		def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
    662  1.1  lukem 
    663  1.1  lukem 	urlize( def_urlpre );
    664  1.1  lukem 
    665  1.1  lukem 	tool_args( argc, argv );
    666  1.1  lukem 
    667  1.1  lukem 	if (( argc - optind < 1 ) ||
    668  1.1  lukem 		( *argv[optind] != '(' /*')'*/ &&
    669  1.1  lukem 		( strchr( argv[optind], '=' ) == NULL ) ) )
    670  1.1  lukem 	{
    671  1.1  lukem 		filtpattern = "(objectclass=*)";
    672  1.1  lukem 	} else {
    673  1.1  lukem 		filtpattern = argv[optind++];
    674  1.1  lukem 	}
    675  1.1  lukem 
    676  1.1  lukem 	if ( argv[optind] != NULL ) {
    677  1.1  lukem 		attrs = &argv[optind];
    678  1.1  lukem 	}
    679  1.1  lukem 
    680  1.1  lukem 	if ( infile != NULL ) {
    681  1.1  lukem 		int percent = 0;
    682  1.1  lukem 
    683  1.1  lukem 		if ( infile[0] == '-' && infile[1] == '\0' ) {
    684  1.1  lukem 			fp = stdin;
    685  1.1  lukem 		} else if (( fp = fopen( infile, "r" )) == NULL ) {
    686  1.1  lukem 			perror( infile );
    687  1.1  lukem 			return EXIT_FAILURE;
    688  1.1  lukem 		}
    689  1.1  lukem 
    690  1.1  lukem 		for( i=0 ; filtpattern[i] ; i++ ) {
    691  1.1  lukem 			if( filtpattern[i] == '%' ) {
    692  1.1  lukem 				if( percent ) {
    693  1.1  lukem 					fprintf( stderr, _("Bad filter pattern \"%s\"\n"),
    694  1.1  lukem 						filtpattern );
    695  1.1  lukem 					return EXIT_FAILURE;
    696  1.1  lukem 				}
    697  1.1  lukem 
    698  1.1  lukem 				percent++;
    699  1.1  lukem 
    700  1.1  lukem 				if( filtpattern[i+1] != 's' ) {
    701  1.1  lukem 					fprintf( stderr, _("Bad filter pattern \"%s\"\n"),
    702  1.1  lukem 						filtpattern );
    703  1.1  lukem 					return EXIT_FAILURE;
    704  1.1  lukem 				}
    705  1.1  lukem 			}
    706  1.1  lukem 		}
    707  1.1  lukem 	}
    708  1.1  lukem 
    709  1.1  lukem 	if ( tmpdir == NULL ) {
    710  1.1  lukem 		tmpdir = def_tmpdir;
    711  1.1  lukem 
    712  1.1  lukem 		if ( urlpre == NULL )
    713  1.1  lukem 			urlpre = def_urlpre;
    714  1.1  lukem 	}
    715  1.1  lukem 
    716  1.1  lukem 	if( urlpre == NULL ) {
    717  1.1  lukem 		urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
    718  1.1  lukem 
    719  1.1  lukem 		if( urlpre == NULL ) {
    720  1.1  lukem 			perror( "malloc" );
    721  1.1  lukem 			return EXIT_FAILURE;
    722  1.1  lukem 		}
    723  1.1  lukem 
    724  1.1  lukem 		sprintf( urlpre, "file:///%s/",
    725  1.1  lukem 			tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
    726  1.1  lukem 
    727  1.1  lukem 		urlize( urlpre );
    728  1.1  lukem 	}
    729  1.1  lukem 
    730  1.1  lukem 	if ( debug )
    731  1.1  lukem 		ldif_debug = debug;
    732  1.1  lukem 
    733  1.1  lukem 	ld = tool_conn_setup( 0, &private_conn_setup );
    734  1.1  lukem 
    735  1.1  lukem 	if ( pw_file || want_bindpw ) {
    736  1.1  lukem 		if ( pw_file ) {
    737  1.1  lukem 			rc = lutil_get_filed_password( pw_file, &passwd );
    738  1.1  lukem 			if( rc ) return EXIT_FAILURE;
    739  1.1  lukem 		} else {
    740  1.1  lukem 			passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
    741  1.1  lukem 			passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
    742  1.1  lukem 		}
    743  1.1  lukem 	}
    744  1.1  lukem 
    745  1.1  lukem 	tool_bind( ld );
    746  1.1  lukem 
    747  1.1  lukem getNextPage:
    748  1.1  lukem 	save_nctrls = nctrls;
    749  1.1  lukem 	i = nctrls;
    750  1.1  lukem 	if ( nctrls > 0
    751  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    752  1.1  lukem 		|| dontUseCopy
    753  1.1  lukem #endif
    754  1.1  lukem 		|| domainScope
    755  1.1  lukem 		|| pagedResults
    756  1.1  lukem 		|| ldapsync
    757  1.1  lukem 		|| subentries
    758  1.1  lukem 		|| valuesReturnFilter )
    759  1.1  lukem 	{
    760  1.1  lukem 
    761  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    762  1.1  lukem 		if ( dontUseCopy ) {
    763  1.1  lukem 			if ( ctrl_add() ) {
    764  1.1  lukem 				return EXIT_FAILURE;
    765  1.1  lukem 			}
    766  1.1  lukem 
    767  1.1  lukem 			c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
    768  1.1  lukem 			c[i].ldctl_value.bv_val = NULL;
    769  1.1  lukem 			c[i].ldctl_value.bv_len = 0;
    770  1.1  lukem 			c[i].ldctl_iscritical = dontUseCopy > 1;
    771  1.1  lukem 			i++;
    772  1.1  lukem 		}
    773  1.1  lukem #endif
    774  1.1  lukem 
    775  1.1  lukem 		if ( domainScope ) {
    776  1.1  lukem 			if ( ctrl_add() ) {
    777  1.1  lukem 				return EXIT_FAILURE;
    778  1.1  lukem 			}
    779  1.1  lukem 
    780  1.1  lukem 			c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
    781  1.1  lukem 			c[i].ldctl_value.bv_val = NULL;
    782  1.1  lukem 			c[i].ldctl_value.bv_len = 0;
    783  1.1  lukem 			c[i].ldctl_iscritical = domainScope > 1;
    784  1.1  lukem 			i++;
    785  1.1  lukem 		}
    786  1.1  lukem 
    787  1.1  lukem 		if ( subentries ) {
    788  1.1  lukem 			if ( ctrl_add() ) {
    789  1.1  lukem 				return EXIT_FAILURE;
    790  1.1  lukem 			}
    791  1.1  lukem 
    792  1.1  lukem 			if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
    793  1.1  lukem 				return EXIT_FAILURE;
    794  1.1  lukem 			}
    795  1.1  lukem 
    796  1.1  lukem 			err = ber_printf( seber, "b", abs(subentries) == 1 ? 0 : 1 );
    797  1.1  lukem 			if ( err == -1 ) {
    798  1.1  lukem 				ber_free( seber, 1 );
    799  1.1  lukem 				fprintf( stderr, _("Subentries control encoding error!\n") );
    800  1.1  lukem 				return EXIT_FAILURE;
    801  1.1  lukem 			}
    802  1.1  lukem 
    803  1.1  lukem 			if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
    804  1.1  lukem 				return EXIT_FAILURE;
    805  1.1  lukem 			}
    806  1.1  lukem 
    807  1.1  lukem 			c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
    808  1.1  lukem 			c[i].ldctl_iscritical = subentries < 1;
    809  1.1  lukem 			i++;
    810  1.1  lukem 		}
    811  1.1  lukem 
    812  1.1  lukem 		if ( ldapsync ) {
    813  1.1  lukem 			if ( ctrl_add() ) {
    814  1.1  lukem 				return EXIT_FAILURE;
    815  1.1  lukem 			}
    816  1.1  lukem 
    817  1.1  lukem 			if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
    818  1.1  lukem 				return EXIT_FAILURE;
    819  1.1  lukem 			}
    820  1.1  lukem 
    821  1.1  lukem 			if ( sync_cookie.bv_len == 0 ) {
    822  1.1  lukem 				err = ber_printf( syncber, "{e}", abs(ldapsync) );
    823  1.1  lukem 			} else {
    824  1.1  lukem 				err = ber_printf( syncber, "{eO}", abs(ldapsync),
    825  1.1  lukem 							&sync_cookie );
    826  1.1  lukem 			}
    827  1.1  lukem 
    828  1.1  lukem 			if ( err == LBER_ERROR ) {
    829  1.1  lukem 				ber_free( syncber, 1 );
    830  1.1  lukem 				fprintf( stderr, _("ldap sync control encoding error!\n") );
    831  1.1  lukem 				return EXIT_FAILURE;
    832  1.1  lukem 			}
    833  1.1  lukem 
    834  1.1  lukem 			if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
    835  1.1  lukem 				return EXIT_FAILURE;
    836  1.1  lukem 			}
    837  1.1  lukem 
    838  1.1  lukem 			c[i].ldctl_oid = LDAP_CONTROL_SYNC;
    839  1.1  lukem 			c[i].ldctl_value = (*syncbvalp);
    840  1.1  lukem 			c[i].ldctl_iscritical = ldapsync < 0;
    841  1.1  lukem 			i++;
    842  1.1  lukem 		}
    843  1.1  lukem 
    844  1.1  lukem 		if ( valuesReturnFilter ) {
    845  1.1  lukem 			if ( ctrl_add() ) {
    846  1.1  lukem 				return EXIT_FAILURE;
    847  1.1  lukem 			}
    848  1.1  lukem 
    849  1.1  lukem 			if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
    850  1.1  lukem 				return EXIT_FAILURE;
    851  1.1  lukem 			}
    852  1.1  lukem 
    853  1.1  lukem 			if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
    854  1.1  lukem 				ber_free( vrber, 1 );
    855  1.1  lukem 				fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
    856  1.1  lukem 				return EXIT_FAILURE;
    857  1.1  lukem 			}
    858  1.1  lukem 
    859  1.1  lukem 			if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
    860  1.1  lukem 				return EXIT_FAILURE;
    861  1.1  lukem 			}
    862  1.1  lukem 
    863  1.1  lukem 			c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
    864  1.1  lukem 			c[i].ldctl_iscritical = valuesReturnFilter > 1;
    865  1.1  lukem 			i++;
    866  1.1  lukem 		}
    867  1.1  lukem 
    868  1.1  lukem 		if ( pagedResults ) {
    869  1.1  lukem 			if ( ctrl_add() ) {
    870  1.1  lukem 				return EXIT_FAILURE;
    871  1.1  lukem 			}
    872  1.1  lukem 
    873  1.1  lukem 			if ( ldap_create_page_control_value( ld,
    874  1.1  lukem 				pageSize, &pr_cookie, &c[i].ldctl_value ) )
    875  1.1  lukem 			{
    876  1.1  lukem 				return EXIT_FAILURE;
    877  1.1  lukem 			}
    878  1.1  lukem 
    879  1.1  lukem 			if ( pr_cookie.bv_val != NULL ) {
    880  1.1  lukem 				ber_memfree( pr_cookie.bv_val );
    881  1.1  lukem 				pr_cookie.bv_val = NULL;
    882  1.1  lukem 				pr_cookie.bv_len = 0;
    883  1.1  lukem 			}
    884  1.1  lukem 
    885  1.1  lukem 			c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
    886  1.1  lukem 			c[i].ldctl_iscritical = pagedResults > 1;
    887  1.1  lukem 			i++;
    888  1.1  lukem 		}
    889  1.1  lukem 	}
    890  1.1  lukem 
    891  1.1  lukem 	tool_server_controls( ld, c, i );
    892  1.1  lukem 
    893  1.1  lukem 	ber_free( seber, 1 );
    894  1.1  lukem 	ber_free( vrber, 1 );
    895  1.1  lukem 
    896  1.1  lukem 	/* step back to the original number of controls, so that
    897  1.1  lukem 	 * those set while parsing args are preserved */
    898  1.1  lukem 	nctrls = save_nctrls;
    899  1.1  lukem 
    900  1.1  lukem 	if ( verbose ) {
    901  1.1  lukem 		fprintf( stderr, _("filter%s: %s\nrequesting: "),
    902  1.1  lukem 			infile != NULL ? _(" pattern") : "",
    903  1.1  lukem 			filtpattern );
    904  1.1  lukem 
    905  1.1  lukem 		if ( attrs == NULL ) {
    906  1.1  lukem 			fprintf( stderr, _("All userApplication attributes") );
    907  1.1  lukem 		} else {
    908  1.1  lukem 			for ( i = 0; attrs[ i ] != NULL; ++i ) {
    909  1.1  lukem 				fprintf( stderr, "%s ", attrs[ i ] );
    910  1.1  lukem 			}
    911  1.1  lukem 		}
    912  1.1  lukem 		fprintf( stderr, "\n" );
    913  1.1  lukem 	}
    914  1.1  lukem 
    915  1.1  lukem 	if ( ldif == 0 ) {
    916  1.1  lukem 		printf( _("# extended LDIF\n") );
    917  1.1  lukem 	} else if ( ldif < 3 ) {
    918  1.1  lukem 		printf( _("version: %d\n\n"), 1 );
    919  1.1  lukem 	}
    920  1.1  lukem 
    921  1.1  lukem 	if (ldif < 2 ) {
    922  1.1  lukem 		char	*realbase = base;
    923  1.1  lukem 
    924  1.1  lukem 		if ( realbase == NULL ) {
    925  1.1  lukem 			ldap_get_option( ld, LDAP_OPT_DEFBASE, (void **)(char *)&realbase );
    926  1.1  lukem 		}
    927  1.1  lukem 
    928  1.1  lukem 		printf( "#\n" );
    929  1.1  lukem 		printf(_("# LDAPv%d\n"), protocol);
    930  1.1  lukem 		printf(_("# base <%s>%s with scope %s\n"),
    931  1.1  lukem 			realbase ? realbase : "",
    932  1.1  lukem 			( realbase == NULL || realbase != base ) ? " (default)" : "",
    933  1.1  lukem 			((scope == LDAP_SCOPE_BASE) ? "baseObject"
    934  1.1  lukem 				: ((scope == LDAP_SCOPE_ONELEVEL) ? "oneLevel"
    935  1.1  lukem 				: ((scope == LDAP_SCOPE_SUBORDINATE) ? "children"
    936  1.1  lukem 				: "subtree" ))));
    937  1.1  lukem 		printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
    938  1.1  lukem 		       filtpattern);
    939  1.1  lukem 		printf(_("# requesting: "));
    940  1.1  lukem 
    941  1.1  lukem 		if ( attrs == NULL ) {
    942  1.1  lukem 			printf( _("ALL") );
    943  1.1  lukem 		} else {
    944  1.1  lukem 			for ( i = 0; attrs[ i ] != NULL; ++i ) {
    945  1.1  lukem 				printf( "%s ", attrs[ i ] );
    946  1.1  lukem 			}
    947  1.1  lukem 		}
    948  1.1  lukem 
    949  1.1  lukem 		if ( manageDSAit ) {
    950  1.1  lukem 			printf(_("\n# with manageDSAit %scontrol"),
    951  1.1  lukem 				manageDSAit > 1 ? _("critical ") : "" );
    952  1.1  lukem 		}
    953  1.1  lukem 		if ( noop ) {
    954  1.1  lukem 			printf(_("\n# with noop %scontrol"),
    955  1.1  lukem 				noop > 1 ? _("critical ") : "" );
    956  1.1  lukem 		}
    957  1.1  lukem 		if ( subentries ) {
    958  1.1  lukem 			printf(_("\n# with subentries %scontrol: %s"),
    959  1.1  lukem 				subentries < 0 ? _("critical ") : "",
    960  1.1  lukem 				abs(subentries) == 1 ? "false" : "true" );
    961  1.1  lukem 		}
    962  1.1  lukem 		if ( valuesReturnFilter ) {
    963  1.1  lukem 			printf(_("\n# with valuesReturnFilter %scontrol: %s"),
    964  1.1  lukem 				valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
    965  1.1  lukem 		}
    966  1.1  lukem 		if ( pagedResults ) {
    967  1.1  lukem 			printf(_("\n# with pagedResults %scontrol: size=%d"),
    968  1.1  lukem 				(pagedResults > 1) ? _("critical ") : "",
    969  1.1  lukem 				pageSize );
    970  1.1  lukem 		}
    971  1.1  lukem 
    972  1.1  lukem 		printf( _("\n#\n\n") );
    973  1.1  lukem 
    974  1.1  lukem 		if ( realbase && realbase != base ) {
    975  1.1  lukem 			ldap_memfree( realbase );
    976  1.1  lukem 		}
    977  1.1  lukem 	}
    978  1.1  lukem 
    979  1.1  lukem 	if ( infile == NULL ) {
    980  1.1  lukem 		rc = dosearch( ld, base, scope, NULL, filtpattern,
    981  1.1  lukem 			attrs, attrsonly, NULL, NULL, NULL, -1 );
    982  1.1  lukem 
    983  1.1  lukem 	} else {
    984  1.1  lukem 		rc = 0;
    985  1.1  lukem 		first = 1;
    986  1.1  lukem 		while ( fgets( line, sizeof( line ), fp ) != NULL ) {
    987  1.1  lukem 			line[ strlen( line ) - 1 ] = '\0';
    988  1.1  lukem 			if ( !first ) {
    989  1.1  lukem 				putchar( '\n' );
    990  1.1  lukem 			} else {
    991  1.1  lukem 				first = 0;
    992  1.1  lukem 			}
    993  1.1  lukem 			rc1 = dosearch( ld, base, scope, filtpattern, line,
    994  1.1  lukem 				attrs, attrsonly, NULL, NULL, NULL, -1 );
    995  1.1  lukem 
    996  1.1  lukem 			if ( rc1 != 0 ) {
    997  1.1  lukem 				rc = rc1;
    998  1.1  lukem 				if ( !contoper )
    999  1.1  lukem 					break;
   1000  1.1  lukem 			}
   1001  1.1  lukem 		}
   1002  1.1  lukem 		if ( fp != stdin ) {
   1003  1.1  lukem 			fclose( fp );
   1004  1.1  lukem 		}
   1005  1.1  lukem 	}
   1006  1.1  lukem 
   1007  1.1  lukem 	if (( rc == LDAP_SUCCESS ) && pageSize && pr_morePagedResults ) {
   1008  1.1  lukem 		char	buf[6];
   1009  1.1  lukem 		int	i, moreEntries, tmpSize;
   1010  1.1  lukem 
   1011  1.1  lukem 		/* Loop to get the next pages when
   1012  1.1  lukem 		 * enter is pressed on the terminal.
   1013  1.1  lukem 		 */
   1014  1.1  lukem 		if ( pagePrompt != 0 ) {
   1015  1.1  lukem 			if ( entriesLeft > 0 ) {
   1016  1.1  lukem 				printf( _("Estimate entries: %d\n"), entriesLeft );
   1017  1.1  lukem 			}
   1018  1.1  lukem 			printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
   1019  1.1  lukem 				(int)pageSize );
   1020  1.1  lukem 			i = 0;
   1021  1.1  lukem 			moreEntries = getchar();
   1022  1.1  lukem 			while ( moreEntries != EOF && moreEntries != '\n' ) {
   1023  1.1  lukem 				if ( i < (int)sizeof(buf) - 1 ) {
   1024  1.1  lukem 					buf[i] = moreEntries;
   1025  1.1  lukem 					i++;
   1026  1.1  lukem 				}
   1027  1.1  lukem 				moreEntries = getchar();
   1028  1.1  lukem 			}
   1029  1.1  lukem 			buf[i] = '\0';
   1030  1.1  lukem 
   1031  1.1  lukem 			if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
   1032  1.1  lukem 				int num = sscanf( buf, "%d", &tmpSize );
   1033  1.1  lukem 				if ( num != 1 ) {
   1034  1.1  lukem 					fprintf( stderr,
   1035  1.1  lukem 						_("Invalid value for PagedResultsControl, %s.\n"), buf);
   1036  1.1  lukem 					return EXIT_FAILURE;
   1037  1.1  lukem 
   1038  1.1  lukem 				}
   1039  1.1  lukem 				pageSize = (ber_int_t)tmpSize;
   1040  1.1  lukem 			}
   1041  1.1  lukem 		}
   1042  1.1  lukem 
   1043  1.1  lukem 		goto getNextPage;
   1044  1.1  lukem 	}
   1045  1.1  lukem 
   1046  1.1  lukem 	tool_unbind( ld );
   1047  1.1  lukem 	tool_destroy();
   1048  1.1  lukem 	if ( base != NULL ) {
   1049  1.1  lukem 		ber_memfree( base );
   1050  1.1  lukem 	}
   1051  1.1  lukem 	if ( control != NULL ) {
   1052  1.1  lukem 		ber_memfree( control );
   1053  1.1  lukem 	}
   1054  1.1  lukem 
   1055  1.1  lukem 	if ( c ) {
   1056  1.1  lukem 		for ( ; save_nctrls-- > 0; ) {
   1057  1.1  lukem 			ber_memfree( c[ save_nctrls ].ldctl_value.bv_val );
   1058  1.1  lukem 		}
   1059  1.1  lukem 		free( c );
   1060  1.1  lukem 		c = NULL;
   1061  1.1  lukem 	}
   1062  1.1  lukem 
   1063  1.1  lukem 	return( rc );
   1064  1.1  lukem }
   1065  1.1  lukem 
   1066  1.1  lukem 
   1067  1.1  lukem static int dosearch(
   1068  1.1  lukem 	LDAP	*ld,
   1069  1.1  lukem 	char	*base,
   1070  1.1  lukem 	int		scope,
   1071  1.1  lukem 	char	*filtpatt,
   1072  1.1  lukem 	char	*value,
   1073  1.1  lukem 	char	**attrs,
   1074  1.1  lukem 	int		attrsonly,
   1075  1.1  lukem 	LDAPControl **sctrls,
   1076  1.1  lukem 	LDAPControl **cctrls,
   1077  1.1  lukem 	struct timeval *timeout,
   1078  1.1  lukem 	int sizelimit )
   1079  1.1  lukem {
   1080  1.1  lukem 	char			*filter;
   1081  1.1  lukem 	int			rc;
   1082  1.1  lukem 	int			nresponses;
   1083  1.1  lukem 	int			nentries;
   1084  1.1  lukem 	int			nreferences;
   1085  1.1  lukem 	int			nextended;
   1086  1.1  lukem 	int			npartial;
   1087  1.1  lukem 	LDAPMessage		*res, *msg;
   1088  1.1  lukem 	ber_int_t		msgid;
   1089  1.1  lukem 	char			*retoid = NULL;
   1090  1.1  lukem 	struct berval		*retdata = NULL;
   1091  1.1  lukem 	int			nresponses_psearch = -1;
   1092  1.1  lukem 	int			cancel_msgid = -1;
   1093  1.1  lukem 
   1094  1.1  lukem 	if( filtpatt != NULL ) {
   1095  1.1  lukem 		size_t max_fsize = strlen( filtpatt ) + strlen( value ) + 1;
   1096  1.1  lukem 		filter = malloc( max_fsize );
   1097  1.1  lukem 		if( filter == NULL ) {
   1098  1.1  lukem 			perror( "malloc" );
   1099  1.1  lukem 			return EXIT_FAILURE;
   1100  1.1  lukem 		}
   1101  1.1  lukem 
   1102  1.1  lukem 		if( snprintf( filter, max_fsize, filtpatt, value ) >= max_fsize ) {
   1103  1.1  lukem 			fprintf( stderr, "Bad filter pattern: \"%s\"\n", filtpatt );
   1104  1.1  lukem 			free( filter );
   1105  1.1  lukem 			return EXIT_FAILURE;
   1106  1.1  lukem 		}
   1107  1.1  lukem 
   1108  1.1  lukem 		if ( verbose ) {
   1109  1.1  lukem 			fprintf( stderr, _("filter: %s\n"), filter );
   1110  1.1  lukem 		}
   1111  1.1  lukem 
   1112  1.1  lukem 		if( ldif < 2 ) {
   1113  1.1  lukem 			printf( _("#\n# filter: %s\n#\n"), filter );
   1114  1.1  lukem 		}
   1115  1.1  lukem 
   1116  1.1  lukem 	} else {
   1117  1.1  lukem 		filter = value;
   1118  1.1  lukem 	}
   1119  1.1  lukem 
   1120  1.1  lukem 	if ( dont ) {
   1121  1.1  lukem 		if ( filtpatt != NULL ) {
   1122  1.1  lukem 			free( filter );
   1123  1.1  lukem 		}
   1124  1.1  lukem 		return LDAP_SUCCESS;
   1125  1.1  lukem 	}
   1126  1.1  lukem 
   1127  1.1  lukem 	rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
   1128  1.1  lukem 		sctrls, cctrls, timeout, sizelimit, &msgid );
   1129  1.1  lukem 
   1130  1.1  lukem 	if ( filtpatt != NULL ) {
   1131  1.1  lukem 		free( filter );
   1132  1.1  lukem 	}
   1133  1.1  lukem 
   1134  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
   1135  1.1  lukem 		fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
   1136  1.1  lukem 			prog, ldap_err2string( rc ), rc );
   1137  1.1  lukem 		return( rc );
   1138  1.1  lukem 	}
   1139  1.1  lukem 
   1140  1.1  lukem 	nresponses = nentries = nreferences = nextended = npartial = 0;
   1141  1.1  lukem 
   1142  1.1  lukem 	res = NULL;
   1143  1.1  lukem 
   1144  1.1  lukem 	while ((rc = ldap_result( ld, LDAP_RES_ANY,
   1145  1.1  lukem 		sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
   1146  1.1  lukem 		NULL, &res )) > 0 )
   1147  1.1  lukem 	{
   1148  1.1  lukem 		rc = tool_check_abandon( ld, msgid );
   1149  1.1  lukem 		if ( rc ) {
   1150  1.1  lukem 			return rc;
   1151  1.1  lukem 		}
   1152  1.1  lukem 
   1153  1.1  lukem 		if( sortattr ) {
   1154  1.1  lukem 			(void) ldap_sort_entries( ld, &res,
   1155  1.1  lukem 				( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
   1156  1.1  lukem 		}
   1157  1.1  lukem 
   1158  1.1  lukem 		for ( msg = ldap_first_message( ld, res );
   1159  1.1  lukem 			msg != NULL;
   1160  1.1  lukem 			msg = ldap_next_message( ld, msg ) )
   1161  1.1  lukem 		{
   1162  1.1  lukem 			if ( nresponses++ ) putchar('\n');
   1163  1.1  lukem 			if ( nresponses_psearch >= 0 )
   1164  1.1  lukem 				nresponses_psearch++;
   1165  1.1  lukem 
   1166  1.1  lukem 			switch( ldap_msgtype( msg ) ) {
   1167  1.1  lukem 			case LDAP_RES_SEARCH_ENTRY:
   1168  1.1  lukem 				nentries++;
   1169  1.1  lukem 				print_entry( ld, msg, attrsonly );
   1170  1.1  lukem 				break;
   1171  1.1  lukem 
   1172  1.1  lukem 			case LDAP_RES_SEARCH_REFERENCE:
   1173  1.1  lukem 				nreferences++;
   1174  1.1  lukem 				print_reference( ld, msg );
   1175  1.1  lukem 				break;
   1176  1.1  lukem 
   1177  1.1  lukem 			case LDAP_RES_EXTENDED:
   1178  1.1  lukem 				nextended++;
   1179  1.1  lukem 				print_extended( ld, msg );
   1180  1.1  lukem 
   1181  1.1  lukem 				if ( ldap_msgid( msg ) == 0 ) {
   1182  1.1  lukem 					/* unsolicited extended operation */
   1183  1.1  lukem 					goto done;
   1184  1.1  lukem 				}
   1185  1.1  lukem 
   1186  1.1  lukem 				if ( cancel_msgid != -1 &&
   1187  1.1  lukem 						cancel_msgid == ldap_msgid( msg ) ) {
   1188  1.1  lukem 					printf(_("Cancelled \n"));
   1189  1.1  lukem 					printf(_("cancel_msgid = %d\n"), cancel_msgid);
   1190  1.1  lukem 					goto done;
   1191  1.1  lukem 				}
   1192  1.1  lukem 				break;
   1193  1.1  lukem 
   1194  1.1  lukem 			case LDAP_RES_SEARCH_RESULT:
   1195  1.1  lukem 				/* pagedResults stuff is dealt with
   1196  1.1  lukem 				 * in tool_print_ctrls(), called by
   1197  1.1  lukem 				 * print_results(). */
   1198  1.1  lukem 				rc = print_result( ld, msg, 1 );
   1199  1.1  lukem 				if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
   1200  1.1  lukem 					break;
   1201  1.1  lukem 				}
   1202  1.1  lukem 
   1203  1.1  lukem 				goto done;
   1204  1.1  lukem 
   1205  1.1  lukem 			case LDAP_RES_INTERMEDIATE:
   1206  1.1  lukem 				npartial++;
   1207  1.1  lukem 				ldap_parse_intermediate( ld, msg,
   1208  1.1  lukem 					&retoid, &retdata, NULL, 0 );
   1209  1.1  lukem 
   1210  1.1  lukem 				nresponses_psearch = 0;
   1211  1.1  lukem 
   1212  1.1  lukem 				if ( strcmp( retoid, LDAP_SYNC_INFO ) == 0 ) {
   1213  1.1  lukem 					printf(_("SyncInfo Received\n"));
   1214  1.1  lukem 					ldap_memfree( retoid );
   1215  1.1  lukem 					ber_bvfree( retdata );
   1216  1.1  lukem 					break;
   1217  1.1  lukem 				}
   1218  1.1  lukem 
   1219  1.1  lukem 				print_partial( ld, msg );
   1220  1.1  lukem 				ldap_memfree( retoid );
   1221  1.1  lukem 				ber_bvfree( retdata );
   1222  1.1  lukem 				goto done;
   1223  1.1  lukem 			}
   1224  1.1  lukem 
   1225  1.1  lukem 			if ( ldapsync && sync_slimit != -1 &&
   1226  1.1  lukem 					nresponses_psearch >= sync_slimit ) {
   1227  1.1  lukem 				BerElement *msgidber = NULL;
   1228  1.1  lukem 				struct berval *msgidvalp = NULL;
   1229  1.1  lukem 				msgidber = ber_alloc_t(LBER_USE_DER);
   1230  1.1  lukem 				ber_printf(msgidber, "{i}", msgid);
   1231  1.1  lukem 				ber_flatten(msgidber, &msgidvalp);
   1232  1.1  lukem 				ldap_extended_operation(ld, LDAP_EXOP_CANCEL,
   1233  1.1  lukem 					msgidvalp, NULL, NULL, &cancel_msgid);
   1234  1.1  lukem 				nresponses_psearch = -1;
   1235  1.1  lukem 			}
   1236  1.1  lukem 		}
   1237  1.1  lukem 
   1238  1.1  lukem 		ldap_msgfree( res );
   1239  1.1  lukem 	}
   1240  1.1  lukem 
   1241  1.1  lukem done:
   1242  1.1  lukem 	if ( rc == -1 ) {
   1243  1.1  lukem 		tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
   1244  1.1  lukem 		return( rc );
   1245  1.1  lukem 	}
   1246  1.1  lukem 
   1247  1.1  lukem 	ldap_msgfree( res );
   1248  1.1  lukem 
   1249  1.1  lukem 	if ( pagedResults ) {
   1250  1.1  lukem 		npagedresponses += nresponses;
   1251  1.1  lukem 		npagedentries += nentries;
   1252  1.1  lukem 		npagedextended += nextended;
   1253  1.1  lukem 		npagedpartial += npartial;
   1254  1.1  lukem 		npagedreferences += nreferences;
   1255  1.1  lukem 		if ( ( pr_morePagedResults == 0 ) && ( ldif < 2 ) ) {
   1256  1.1  lukem 			printf( _("\n# numResponses: %d\n"), npagedresponses );
   1257  1.1  lukem 			if( npagedentries ) {
   1258  1.1  lukem 				printf( _("# numEntries: %d\n"), npagedentries );
   1259  1.1  lukem 			}
   1260  1.1  lukem 			if( npagedextended ) {
   1261  1.1  lukem 				printf( _("# numExtended: %d\n"), npagedextended );
   1262  1.1  lukem 			}
   1263  1.1  lukem 			if( npagedpartial ) {
   1264  1.1  lukem 				printf( _("# numPartial: %d\n"), npagedpartial );
   1265  1.1  lukem 			}
   1266  1.1  lukem 			if( npagedreferences ) {
   1267  1.1  lukem 				printf( _("# numReferences: %d\n"), npagedreferences );
   1268  1.1  lukem 			}
   1269  1.1  lukem 		}
   1270  1.1  lukem 	} else if ( ldif < 2 ) {
   1271  1.1  lukem 		printf( _("\n# numResponses: %d\n"), nresponses );
   1272  1.1  lukem 		if( nentries ) printf( _("# numEntries: %d\n"), nentries );
   1273  1.1  lukem 		if( nextended ) printf( _("# numExtended: %d\n"), nextended );
   1274  1.1  lukem 		if( npartial ) printf( _("# numPartial: %d\n"), npartial );
   1275  1.1  lukem 		if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
   1276  1.1  lukem 	}
   1277  1.1  lukem 
   1278  1.1  lukem 	return( rc );
   1279  1.1  lukem }
   1280  1.1  lukem 
   1281  1.1  lukem /* This is the proposed new way of doing things.
   1282  1.1  lukem  * It is more efficient, but the API is non-standard.
   1283  1.1  lukem  */
   1284  1.1  lukem static void
   1285  1.1  lukem print_entry(
   1286  1.1  lukem 	LDAP	*ld,
   1287  1.1  lukem 	LDAPMessage	*entry,
   1288  1.1  lukem 	int		attrsonly)
   1289  1.1  lukem {
   1290  1.1  lukem 	char		*ufn = NULL;
   1291  1.1  lukem 	char	tmpfname[ 256 ];
   1292  1.1  lukem 	char	url[ 256 ];
   1293  1.1  lukem 	int			i, rc;
   1294  1.1  lukem 	BerElement		*ber = NULL;
   1295  1.1  lukem 	struct berval		bv, *bvals, **bvp = &bvals;
   1296  1.1  lukem 	LDAPControl **ctrls = NULL;
   1297  1.1  lukem 	FILE		*tmpfp;
   1298  1.1  lukem 
   1299  1.1  lukem 	rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
   1300  1.1  lukem 
   1301  1.1  lukem 	if ( ldif < 2 ) {
   1302  1.1  lukem 		ufn = ldap_dn2ufn( bv.bv_val );
   1303  1.1  lukem 		tool_write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
   1304  1.1  lukem 	}
   1305  1.1  lukem 	tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
   1306  1.1  lukem 
   1307  1.1  lukem 	rc = ldap_get_entry_controls( ld, entry, &ctrls );
   1308  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
   1309  1.1  lukem 		fprintf(stderr, _("print_entry: %d\n"), rc );
   1310  1.1  lukem 		tool_perror( "ldap_get_entry_controls", rc, NULL, NULL, NULL, NULL );
   1311  1.1  lukem 		exit( EXIT_FAILURE );
   1312  1.1  lukem 	}
   1313  1.1  lukem 
   1314  1.1  lukem 	if( ctrls ) {
   1315  1.1  lukem 		tool_print_ctrls( ld, ctrls );
   1316  1.1  lukem 		ldap_controls_free( ctrls );
   1317  1.1  lukem 	}
   1318  1.1  lukem 
   1319  1.1  lukem 	if ( includeufn ) {
   1320  1.1  lukem 		if( ufn == NULL ) {
   1321  1.1  lukem 			ufn = ldap_dn2ufn( bv.bv_val );
   1322  1.1  lukem 		}
   1323  1.1  lukem 		tool_write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
   1324  1.1  lukem 	}
   1325  1.1  lukem 
   1326  1.1  lukem 	if( ufn != NULL ) ldap_memfree( ufn );
   1327  1.1  lukem 
   1328  1.1  lukem 	if ( attrsonly ) bvp = NULL;
   1329  1.1  lukem 
   1330  1.1  lukem 	for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
   1331  1.1  lukem 		rc == LDAP_SUCCESS;
   1332  1.1  lukem 		rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
   1333  1.1  lukem 	{
   1334  1.1  lukem 		if (bv.bv_val == NULL) break;
   1335  1.1  lukem 
   1336  1.1  lukem 		if ( attrsonly ) {
   1337  1.1  lukem 			tool_write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
   1338  1.1  lukem 
   1339  1.1  lukem 		} else if ( bvals ) {
   1340  1.1  lukem 			for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
   1341  1.1  lukem 				if ( vals2tmp > 1 || ( vals2tmp &&
   1342  1.1  lukem 					ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len )))
   1343  1.1  lukem 				{
   1344  1.1  lukem 					int tmpfd;
   1345  1.1  lukem 					/* write value to file */
   1346  1.1  lukem 					snprintf( tmpfname, sizeof tmpfname,
   1347  1.1  lukem 						"%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
   1348  1.1  lukem 						tmpdir, bv.bv_val );
   1349  1.1  lukem 					tmpfp = NULL;
   1350  1.1  lukem 
   1351  1.1  lukem 					tmpfd = mkstemp( tmpfname );
   1352  1.1  lukem 
   1353  1.1  lukem 					if ( tmpfd < 0  ) {
   1354  1.1  lukem 						perror( tmpfname );
   1355  1.1  lukem 						continue;
   1356  1.1  lukem 					}
   1357  1.1  lukem 
   1358  1.1  lukem 					if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
   1359  1.1  lukem 						perror( tmpfname );
   1360  1.1  lukem 						continue;
   1361  1.1  lukem 					}
   1362  1.1  lukem 
   1363  1.1  lukem 					if ( fwrite( bvals[ i ].bv_val,
   1364  1.1  lukem 						bvals[ i ].bv_len, 1, tmpfp ) == 0 )
   1365  1.1  lukem 					{
   1366  1.1  lukem 						perror( tmpfname );
   1367  1.1  lukem 						fclose( tmpfp );
   1368  1.1  lukem 						continue;
   1369  1.1  lukem 					}
   1370  1.1  lukem 
   1371  1.1  lukem 					fclose( tmpfp );
   1372  1.1  lukem 
   1373  1.1  lukem 					snprintf( url, sizeof url, "%s%s", urlpre,
   1374  1.1  lukem 						&tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
   1375  1.1  lukem 
   1376  1.1  lukem 					urlize( url );
   1377  1.1  lukem 					tool_write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
   1378  1.1  lukem 
   1379  1.1  lukem 				} else {
   1380  1.1  lukem 					tool_write_ldif( LDIF_PUT_VALUE, bv.bv_val,
   1381  1.1  lukem 						bvals[ i ].bv_val, bvals[ i ].bv_len );
   1382  1.1  lukem 				}
   1383  1.1  lukem 			}
   1384  1.1  lukem 			ber_memfree( bvals );
   1385  1.1  lukem 		}
   1386  1.1  lukem 	}
   1387  1.1  lukem 
   1388  1.1  lukem 	if( ber != NULL ) {
   1389  1.1  lukem 		ber_free( ber, 0 );
   1390  1.1  lukem 	}
   1391  1.1  lukem }
   1392  1.1  lukem 
   1393  1.1  lukem static void print_reference(
   1394  1.1  lukem 	LDAP *ld,
   1395  1.1  lukem 	LDAPMessage *reference )
   1396  1.1  lukem {
   1397  1.1  lukem 	int rc;
   1398  1.1  lukem 	char **refs = NULL;
   1399  1.1  lukem 	LDAPControl **ctrls;
   1400  1.1  lukem 
   1401  1.1  lukem 	if( ldif < 2 ) {
   1402  1.1  lukem 		printf(_("# search reference\n"));
   1403  1.1  lukem 	}
   1404  1.1  lukem 
   1405  1.1  lukem 	rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
   1406  1.1  lukem 
   1407  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
   1408  1.1  lukem 		tool_perror( "ldap_parse_reference", rc, NULL, NULL, NULL, NULL );
   1409  1.1  lukem 		exit( EXIT_FAILURE );
   1410  1.1  lukem 	}
   1411  1.1  lukem 
   1412  1.1  lukem 	if( refs ) {
   1413  1.1  lukem 		int i;
   1414  1.1  lukem 		for( i=0; refs[i] != NULL; i++ ) {
   1415  1.1  lukem 			tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
   1416  1.1  lukem 				"ref", refs[i], strlen(refs[i]) );
   1417  1.1  lukem 		}
   1418  1.1  lukem 		ber_memvfree( (void **) refs );
   1419  1.1  lukem 	}
   1420  1.1  lukem 
   1421  1.1  lukem 	if( ctrls ) {
   1422  1.1  lukem 		tool_print_ctrls( ld, ctrls );
   1423  1.1  lukem 		ldap_controls_free( ctrls );
   1424  1.1  lukem 	}
   1425  1.1  lukem }
   1426  1.1  lukem 
   1427  1.1  lukem static void print_extended(
   1428  1.1  lukem 	LDAP *ld,
   1429  1.1  lukem 	LDAPMessage *extended )
   1430  1.1  lukem {
   1431  1.1  lukem 	int rc;
   1432  1.1  lukem 	char *retoid = NULL;
   1433  1.1  lukem 	struct berval *retdata = NULL;
   1434  1.1  lukem 
   1435  1.1  lukem 	if( ldif < 2 ) {
   1436  1.1  lukem 		printf(_("# extended result response\n"));
   1437  1.1  lukem 	}
   1438  1.1  lukem 
   1439  1.1  lukem 	rc = ldap_parse_extended_result( ld, extended,
   1440  1.1  lukem 		&retoid, &retdata, 0 );
   1441  1.1  lukem 
   1442  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
   1443  1.1  lukem 		tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
   1444  1.1  lukem 		exit( EXIT_FAILURE );
   1445  1.1  lukem 	}
   1446  1.1  lukem 
   1447  1.1  lukem 	if ( ldif < 2 ) {
   1448  1.1  lukem 		tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
   1449  1.1  lukem 			"extended", retoid, retoid ? strlen(retoid) : 0 );
   1450  1.1  lukem 	}
   1451  1.1  lukem 	ber_memfree( retoid );
   1452  1.1  lukem 
   1453  1.1  lukem 	if(retdata) {
   1454  1.1  lukem 		if ( ldif < 2 ) {
   1455  1.1  lukem 			tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
   1456  1.1  lukem 				"data", retdata->bv_val, retdata->bv_len );
   1457  1.1  lukem 		}
   1458  1.1  lukem 		ber_bvfree( retdata );
   1459  1.1  lukem 	}
   1460  1.1  lukem 
   1461  1.1  lukem 	print_result( ld, extended, 0 );
   1462  1.1  lukem }
   1463  1.1  lukem 
   1464  1.1  lukem static void print_partial(
   1465  1.1  lukem 	LDAP *ld,
   1466  1.1  lukem 	LDAPMessage *partial )
   1467  1.1  lukem {
   1468  1.1  lukem 	int rc;
   1469  1.1  lukem 	char *retoid = NULL;
   1470  1.1  lukem 	struct berval *retdata = NULL;
   1471  1.1  lukem 	LDAPControl **ctrls = NULL;
   1472  1.1  lukem 
   1473  1.1  lukem 	if( ldif < 2 ) {
   1474  1.1  lukem 		printf(_("# extended partial response\n"));
   1475  1.1  lukem 	}
   1476  1.1  lukem 
   1477  1.1  lukem 	rc = ldap_parse_intermediate( ld, partial,
   1478  1.1  lukem 		&retoid, &retdata, &ctrls, 0 );
   1479  1.1  lukem 
   1480  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
   1481  1.1  lukem 		tool_perror( "ldap_parse_intermediate", rc, NULL, NULL, NULL, NULL );
   1482  1.1  lukem 		exit( EXIT_FAILURE );
   1483  1.1  lukem 	}
   1484  1.1  lukem 
   1485  1.1  lukem 	if ( ldif < 2 ) {
   1486  1.1  lukem 		tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
   1487  1.1  lukem 			"partial", retoid, retoid ? strlen(retoid) : 0 );
   1488  1.1  lukem 	}
   1489  1.1  lukem 
   1490  1.1  lukem 	ber_memfree( retoid );
   1491  1.1  lukem 
   1492  1.1  lukem 	if( retdata ) {
   1493  1.1  lukem 		if ( ldif < 2 ) {
   1494  1.1  lukem 			tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
   1495  1.1  lukem 				"data", retdata->bv_val, retdata->bv_len );
   1496  1.1  lukem 		}
   1497  1.1  lukem 
   1498  1.1  lukem 		ber_bvfree( retdata );
   1499  1.1  lukem 	}
   1500  1.1  lukem 
   1501  1.1  lukem 	if( ctrls ) {
   1502  1.1  lukem 		tool_print_ctrls( ld, ctrls );
   1503  1.1  lukem 		ldap_controls_free( ctrls );
   1504  1.1  lukem 	}
   1505  1.1  lukem }
   1506  1.1  lukem 
   1507  1.1  lukem static int print_result(
   1508  1.1  lukem 	LDAP *ld,
   1509  1.1  lukem 	LDAPMessage *result, int search )
   1510  1.1  lukem {
   1511  1.1  lukem 	int rc;
   1512  1.1  lukem 	int err;
   1513  1.1  lukem 	char *matcheddn = NULL;
   1514  1.1  lukem 	char *text = NULL;
   1515  1.1  lukem 	char **refs = NULL;
   1516  1.1  lukem 	LDAPControl **ctrls = NULL;
   1517  1.1  lukem 
   1518  1.1  lukem 	if( search ) {
   1519  1.1  lukem 		if ( ldif < 2 ) {
   1520  1.1  lukem 			printf(_("# search result\n"));
   1521  1.1  lukem 		}
   1522  1.1  lukem 		if ( ldif < 1 ) {
   1523  1.1  lukem 			printf("%s: %d\n", _("search"), ldap_msgid(result) );
   1524  1.1  lukem 		}
   1525  1.1  lukem 	}
   1526  1.1  lukem 
   1527  1.1  lukem 	rc = ldap_parse_result( ld, result,
   1528  1.1  lukem 		&err, &matcheddn, &text, &refs, &ctrls, 0 );
   1529  1.1  lukem 
   1530  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
   1531  1.1  lukem 		tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
   1532  1.1  lukem 		exit( EXIT_FAILURE );
   1533  1.1  lukem 	}
   1534  1.1  lukem 
   1535  1.1  lukem 
   1536  1.1  lukem 	if( !ldif ) {
   1537  1.1  lukem 		printf( _("result: %d %s\n"), err, ldap_err2string(err) );
   1538  1.1  lukem 
   1539  1.1  lukem 	} else if ( err != LDAP_SUCCESS ) {
   1540  1.1  lukem 		fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
   1541  1.1  lukem 	}
   1542  1.1  lukem 
   1543  1.1  lukem 	if( matcheddn ) {
   1544  1.1  lukem 		if( *matcheddn ) {
   1545  1.1  lukem 		if( !ldif ) {
   1546  1.1  lukem 			tool_write_ldif( LDIF_PUT_VALUE,
   1547  1.1  lukem 				"matchedDN", matcheddn, strlen(matcheddn) );
   1548  1.1  lukem 		} else {
   1549  1.1  lukem 			fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
   1550  1.1  lukem 		}
   1551  1.1  lukem 		}
   1552  1.1  lukem 
   1553  1.1  lukem 		ber_memfree( matcheddn );
   1554  1.1  lukem 	}
   1555  1.1  lukem 
   1556  1.1  lukem 	if( text ) {
   1557  1.1  lukem 		if( *text ) {
   1558  1.1  lukem 			if( !ldif ) {
   1559  1.1  lukem 				if ( err == LDAP_PARTIAL_RESULTS ) {
   1560  1.1  lukem 					char	*line;
   1561  1.1  lukem 
   1562  1.1  lukem 					for ( line = text; line != NULL; ) {
   1563  1.1  lukem 						char	*next = strchr( line, '\n' );
   1564  1.1  lukem 
   1565  1.1  lukem 						tool_write_ldif( LDIF_PUT_TEXT,
   1566  1.1  lukem 							"text", line,
   1567  1.1  lukem 							next ? next - line : strlen( line ) );
   1568  1.1  lukem 
   1569  1.1  lukem 						line = next ? next + 1 : NULL;
   1570  1.1  lukem 					}
   1571  1.1  lukem 
   1572  1.1  lukem 				} else {
   1573  1.1  lukem 					tool_write_ldif( LDIF_PUT_TEXT, "text",
   1574  1.1  lukem 						text, strlen(text) );
   1575  1.1  lukem 				}
   1576  1.1  lukem 			} else {
   1577  1.1  lukem 				fprintf( stderr, _("Additional information: %s\n"), text );
   1578  1.1  lukem 			}
   1579  1.1  lukem 		}
   1580  1.1  lukem 
   1581  1.1  lukem 		ber_memfree( text );
   1582  1.1  lukem 	}
   1583  1.1  lukem 
   1584  1.1  lukem 	if( refs ) {
   1585  1.1  lukem 		int i;
   1586  1.1  lukem 		for( i=0; refs[i] != NULL; i++ ) {
   1587  1.1  lukem 			if( !ldif ) {
   1588  1.1  lukem 				tool_write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
   1589  1.1  lukem 			} else {
   1590  1.1  lukem 				fprintf( stderr, _("Referral: %s\n"), refs[i] );
   1591  1.1  lukem 			}
   1592  1.1  lukem 		}
   1593  1.1  lukem 
   1594  1.1  lukem 		ber_memvfree( (void **) refs );
   1595  1.1  lukem 	}
   1596  1.1  lukem 
   1597  1.1  lukem 	pr_morePagedResults = 0;
   1598  1.1  lukem 
   1599  1.1  lukem 	if( ctrls ) {
   1600  1.1  lukem 		tool_print_ctrls( ld, ctrls );
   1601  1.1  lukem 		ldap_controls_free( ctrls );
   1602  1.1  lukem 	}
   1603  1.1  lukem 
   1604  1.1  lukem 	return err;
   1605  1.1  lukem }
   1606  1.1  lukem 
   1607