Home | History | Annotate | Line # | Download | only in progs
slapd-search.c revision 1.1.1.8
      1  1.1.1.6  christos /*	$NetBSD: slapd-search.c,v 1.1.1.8 2020/08/11 13:12:11 christos Exp $	*/
      2  1.1.1.2     lukem 
      3  1.1.1.4      tron /* $OpenLDAP$ */
      4      1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5      1.1     lukem  *
      6  1.1.1.8  christos  * Copyright 1999-2020 The OpenLDAP Foundation.
      7      1.1     lukem  * All rights reserved.
      8      1.1     lukem  *
      9      1.1     lukem  * Redistribution and use in source and binary forms, with or without
     10      1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     11      1.1     lukem  * Public License.
     12      1.1     lukem  *
     13      1.1     lukem  * A copy of this license is available in file LICENSE in the
     14      1.1     lukem  * top-level directory of the distribution or, alternatively, at
     15      1.1     lukem  * <http://www.OpenLDAP.org/license.html>.
     16      1.1     lukem  */
     17      1.1     lukem /* ACKNOWLEDGEMENTS:
     18      1.1     lukem  * This work was initially developed by Kurt Spanier for inclusion
     19      1.1     lukem  * in OpenLDAP Software.
     20      1.1     lukem  */
     21      1.1     lukem 
     22  1.1.1.5  christos #include <sys/cdefs.h>
     23  1.1.1.6  christos __RCSID("$NetBSD: slapd-search.c,v 1.1.1.8 2020/08/11 13:12:11 christos Exp $");
     24  1.1.1.5  christos 
     25      1.1     lukem #include "portable.h"
     26      1.1     lukem 
     27      1.1     lukem #include <stdio.h>
     28      1.1     lukem 
     29      1.1     lukem #include "ac/stdlib.h"
     30      1.1     lukem 
     31      1.1     lukem #include "ac/ctype.h"
     32      1.1     lukem #include "ac/param.h"
     33      1.1     lukem #include "ac/socket.h"
     34      1.1     lukem #include "ac/string.h"
     35      1.1     lukem #include "ac/unistd.h"
     36      1.1     lukem #include "ac/wait.h"
     37      1.1     lukem 
     38      1.1     lukem #include "ldap.h"
     39      1.1     lukem #include "lutil.h"
     40      1.1     lukem #include "ldap_pvt.h"
     41      1.1     lukem 
     42      1.1     lukem #include "slapd-common.h"
     43      1.1     lukem 
     44      1.1     lukem #define LOOPS	100
     45      1.1     lukem #define RETRIES	0
     46      1.1     lukem 
     47      1.1     lukem static void
     48      1.1     lukem do_search( char *uri, char *manager, struct berval *passwd,
     49      1.1     lukem 	char *sbase, int scope, char *filter, LDAP **ldp,
     50      1.1     lukem 	char **attrs, int noattrs, int nobind,
     51      1.1     lukem 	int innerloop, int maxretries, int delay, int force, int chaserefs );
     52      1.1     lukem 
     53      1.1     lukem static void
     54      1.1     lukem do_random( char *uri, char *manager, struct berval *passwd,
     55      1.1     lukem 	char *sbase, int scope, char *filter, char *attr,
     56      1.1     lukem 	char **attrs, int noattrs, int nobind,
     57      1.1     lukem 	int innerloop, int maxretries, int delay, int force, int chaserefs );
     58      1.1     lukem 
     59      1.1     lukem static void
     60      1.1     lukem usage( char *name, char o )
     61      1.1     lukem {
     62      1.1     lukem 	if ( o != '\0' ) {
     63      1.1     lukem 		fprintf( stderr, "unknown/incorrect option \"%c\"\n", o );
     64      1.1     lukem 	}
     65      1.1     lukem 
     66      1.1     lukem         fprintf( stderr,
     67      1.1     lukem 		"usage: %s "
     68      1.1     lukem 		"-H <uri> | ([-h <host>] -p <port>) "
     69      1.1     lukem 		"-D <manager> "
     70      1.1     lukem 		"-w <passwd> "
     71      1.1     lukem 		"-b <searchbase> "
     72      1.1     lukem 		"-s <scope> "
     73      1.1     lukem 		"-f <searchfilter> "
     74      1.1     lukem 		"[-a <attr>] "
     75      1.1     lukem 		"[-A] "
     76      1.1     lukem 		"[-C] "
     77      1.1     lukem 		"[-F] "
     78      1.1     lukem 		"[-N] "
     79  1.1.1.4      tron 		"[-S[S[S]]] "
     80      1.1     lukem 		"[-i <ignore>] "
     81      1.1     lukem 		"[-l <loops>] "
     82      1.1     lukem 		"[-L <outerloops>] "
     83      1.1     lukem 		"[-r <maxretries>] "
     84      1.1     lukem 		"[-t <delay>] "
     85      1.1     lukem 		"[<attrs>] "
     86      1.1     lukem 		"\n",
     87      1.1     lukem 			name );
     88      1.1     lukem 	exit( EXIT_FAILURE );
     89      1.1     lukem }
     90      1.1     lukem 
     91  1.1.1.4      tron /* -S: just send requests without reading responses
     92  1.1.1.4      tron  * -SS: send all requests asynchronous and immediately start reading responses
     93  1.1.1.4      tron  * -SSS: send all requests asynchronous; then read responses
     94  1.1.1.4      tron  */
     95  1.1.1.2     lukem static int swamp;
     96  1.1.1.2     lukem 
     97      1.1     lukem int
     98      1.1     lukem main( int argc, char **argv )
     99      1.1     lukem {
    100      1.1     lukem 	int		i;
    101      1.1     lukem 	char		*uri = NULL;
    102      1.1     lukem 	char		*host = "localhost";
    103      1.1     lukem 	int		port = -1;
    104      1.1     lukem 	char		*manager = NULL;
    105      1.1     lukem 	struct berval	passwd = { 0, NULL };
    106      1.1     lukem 	char		*sbase = NULL;
    107      1.1     lukem 	int		scope = LDAP_SCOPE_SUBTREE;
    108      1.1     lukem 	char		*filter  = NULL;
    109      1.1     lukem 	char		*attr = NULL;
    110      1.1     lukem 	char		*srchattrs[] = { "cn", "sn", NULL };
    111      1.1     lukem 	char		**attrs = srchattrs;
    112      1.1     lukem 	int		loops = LOOPS;
    113      1.1     lukem 	int		outerloops = 1;
    114      1.1     lukem 	int		retries = RETRIES;
    115      1.1     lukem 	int		delay = 0;
    116      1.1     lukem 	int		force = 0;
    117      1.1     lukem 	int		chaserefs = 0;
    118      1.1     lukem 	int		noattrs = 0;
    119      1.1     lukem 	int		nobind = 0;
    120      1.1     lukem 
    121      1.1     lukem 	tester_init( "slapd-search", TESTER_SEARCH );
    122      1.1     lukem 
    123      1.1     lukem 	/* by default, tolerate referrals and no such object */
    124      1.1     lukem 	tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
    125      1.1     lukem 
    126  1.1.1.2     lukem 	while ( ( i = getopt( argc, argv, "Aa:b:CD:f:FH:h:i:l:L:Np:r:Ss:t:T:w:" ) ) != EOF )
    127      1.1     lukem 	{
    128      1.1     lukem 		switch ( i ) {
    129      1.1     lukem 		case 'A':
    130      1.1     lukem 			noattrs++;
    131      1.1     lukem 			break;
    132      1.1     lukem 
    133      1.1     lukem 		case 'C':
    134      1.1     lukem 			chaserefs++;
    135      1.1     lukem 			break;
    136      1.1     lukem 
    137      1.1     lukem 		case 'H':		/* the server uri */
    138      1.1     lukem 			uri = strdup( optarg );
    139      1.1     lukem 			break;
    140      1.1     lukem 
    141      1.1     lukem 		case 'h':		/* the servers host */
    142      1.1     lukem 			host = strdup( optarg );
    143      1.1     lukem 			break;
    144      1.1     lukem 
    145      1.1     lukem 		case 'i':
    146      1.1     lukem 			tester_ignore_str2errlist( optarg );
    147      1.1     lukem 			break;
    148      1.1     lukem 
    149      1.1     lukem 		case 'N':
    150      1.1     lukem 			nobind++;
    151      1.1     lukem 			break;
    152      1.1     lukem 
    153      1.1     lukem 		case 'p':		/* the servers port */
    154      1.1     lukem 			if ( lutil_atoi( &port, optarg ) != 0 ) {
    155      1.1     lukem 				usage( argv[0], i );
    156      1.1     lukem 			}
    157      1.1     lukem 			break;
    158      1.1     lukem 
    159      1.1     lukem 		case 'D':		/* the servers manager */
    160      1.1     lukem 			manager = strdup( optarg );
    161      1.1     lukem 			break;
    162      1.1     lukem 
    163      1.1     lukem 		case 'w':		/* the server managers password */
    164      1.1     lukem 			passwd.bv_val = strdup( optarg );
    165      1.1     lukem 			passwd.bv_len = strlen( optarg );
    166      1.1     lukem 			memset( optarg, '*', passwd.bv_len );
    167      1.1     lukem 			break;
    168      1.1     lukem 
    169      1.1     lukem 		case 'a':
    170      1.1     lukem 			attr = strdup( optarg );
    171      1.1     lukem 			break;
    172      1.1     lukem 
    173      1.1     lukem 		case 'b':		/* file with search base */
    174      1.1     lukem 			sbase = strdup( optarg );
    175      1.1     lukem 			break;
    176      1.1     lukem 
    177      1.1     lukem 		case 'f':		/* the search request */
    178      1.1     lukem 			filter = strdup( optarg );
    179      1.1     lukem 			break;
    180      1.1     lukem 
    181      1.1     lukem 		case 'F':
    182      1.1     lukem 			force++;
    183      1.1     lukem 			break;
    184      1.1     lukem 
    185      1.1     lukem 		case 'l':		/* number of loops */
    186      1.1     lukem 			if ( lutil_atoi( &loops, optarg ) != 0 ) {
    187      1.1     lukem 				usage( argv[0], i );
    188      1.1     lukem 			}
    189      1.1     lukem 			break;
    190      1.1     lukem 
    191      1.1     lukem 		case 'L':		/* number of loops */
    192      1.1     lukem 			if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
    193      1.1     lukem 				usage( argv[0], i );
    194      1.1     lukem 			}
    195      1.1     lukem 			break;
    196      1.1     lukem 
    197      1.1     lukem 		case 'r':		/* number of retries */
    198      1.1     lukem 			if ( lutil_atoi( &retries, optarg ) != 0 ) {
    199      1.1     lukem 				usage( argv[0], i );
    200      1.1     lukem 			}
    201      1.1     lukem 			break;
    202      1.1     lukem 
    203      1.1     lukem 		case 't':		/* delay in seconds */
    204      1.1     lukem 			if ( lutil_atoi( &delay, optarg ) != 0 ) {
    205      1.1     lukem 				usage( argv[0], i );
    206      1.1     lukem 			}
    207      1.1     lukem 			break;
    208      1.1     lukem 
    209      1.1     lukem 		case 'T':
    210      1.1     lukem 			attrs = ldap_str2charray( optarg, "," );
    211      1.1     lukem 			if ( attrs == NULL ) {
    212      1.1     lukem 				usage( argv[0], i );
    213      1.1     lukem 			}
    214      1.1     lukem 			break;
    215      1.1     lukem 
    216  1.1.1.2     lukem 		case 'S':
    217  1.1.1.2     lukem 			swamp++;
    218  1.1.1.2     lukem 			break;
    219  1.1.1.2     lukem 
    220      1.1     lukem 		case 's':
    221      1.1     lukem 			scope = ldap_pvt_str2scope( optarg );
    222      1.1     lukem 			if ( scope == -1 ) {
    223      1.1     lukem 				usage( argv[0], i );
    224      1.1     lukem 			}
    225      1.1     lukem 			break;
    226      1.1     lukem 
    227      1.1     lukem 		default:
    228      1.1     lukem 			usage( argv[0], i );
    229      1.1     lukem 			break;
    230      1.1     lukem 		}
    231      1.1     lukem 	}
    232      1.1     lukem 
    233      1.1     lukem 	if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
    234      1.1     lukem 		usage( argv[0], '\0' );
    235      1.1     lukem 
    236      1.1     lukem 	if ( *filter == '\0' ) {
    237      1.1     lukem 
    238      1.1     lukem 		fprintf( stderr, "%s: invalid EMPTY search filter.\n",
    239      1.1     lukem 				argv[0] );
    240      1.1     lukem 		exit( EXIT_FAILURE );
    241      1.1     lukem 
    242      1.1     lukem 	}
    243      1.1     lukem 
    244      1.1     lukem 	if ( argv[optind] != NULL ) {
    245      1.1     lukem 		attrs = &argv[optind];
    246      1.1     lukem 	}
    247      1.1     lukem 
    248      1.1     lukem 	uri = tester_uri( uri, host, port );
    249      1.1     lukem 
    250      1.1     lukem 	for ( i = 0; i < outerloops; i++ ) {
    251      1.1     lukem 		if ( attr != NULL ) {
    252      1.1     lukem 			do_random( uri, manager, &passwd,
    253      1.1     lukem 				sbase, scope, filter, attr,
    254      1.1     lukem 				attrs, noattrs, nobind,
    255      1.1     lukem 				loops, retries, delay, force, chaserefs );
    256      1.1     lukem 
    257      1.1     lukem 		} else {
    258      1.1     lukem 			do_search( uri, manager, &passwd,
    259      1.1     lukem 				sbase, scope, filter, NULL,
    260      1.1     lukem 				attrs, noattrs, nobind,
    261      1.1     lukem 				loops, retries, delay, force, chaserefs );
    262      1.1     lukem 		}
    263      1.1     lukem 	}
    264      1.1     lukem 
    265      1.1     lukem 	exit( EXIT_SUCCESS );
    266      1.1     lukem }
    267      1.1     lukem 
    268      1.1     lukem 
    269      1.1     lukem static void
    270      1.1     lukem do_random( char *uri, char *manager, struct berval *passwd,
    271      1.1     lukem 	char *sbase, int scope, char *filter, char *attr,
    272      1.1     lukem 	char **srchattrs, int noattrs, int nobind,
    273      1.1     lukem 	int innerloop, int maxretries, int delay, int force, int chaserefs )
    274      1.1     lukem {
    275      1.1     lukem 	LDAP	*ld = NULL;
    276      1.1     lukem 	int  	i = 0, do_retry = maxretries;
    277      1.1     lukem 	char	*attrs[ 2 ];
    278      1.1     lukem 	int     rc = LDAP_SUCCESS;
    279      1.1     lukem 	int	version = LDAP_VERSION3;
    280      1.1     lukem 	int	nvalues = 0;
    281      1.1     lukem 	char	**values = NULL;
    282      1.1     lukem 	LDAPMessage *res = NULL, *e = NULL;
    283      1.1     lukem 
    284      1.1     lukem 	attrs[ 0 ] = attr;
    285      1.1     lukem 	attrs[ 1 ] = NULL;
    286      1.1     lukem 
    287      1.1     lukem 	ldap_initialize( &ld, uri );
    288      1.1     lukem 	if ( ld == NULL ) {
    289      1.1     lukem 		tester_perror( "ldap_initialize", NULL );
    290      1.1     lukem 		exit( EXIT_FAILURE );
    291      1.1     lukem 	}
    292      1.1     lukem 
    293      1.1     lukem 	(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
    294      1.1     lukem 	(void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
    295      1.1     lukem 		chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
    296      1.1     lukem 
    297      1.1     lukem 	if ( do_retry == maxretries ) {
    298      1.1     lukem 		fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
    299      1.1     lukem 				(long) pid, innerloop, sbase, filter, attr );
    300      1.1     lukem 	}
    301      1.1     lukem 
    302      1.1     lukem 	if ( nobind == 0 ) {
    303      1.1     lukem 		rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
    304      1.1     lukem 		if ( rc != LDAP_SUCCESS ) {
    305      1.1     lukem 			tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
    306      1.1     lukem 			switch ( rc ) {
    307      1.1     lukem 			case LDAP_BUSY:
    308      1.1     lukem 			case LDAP_UNAVAILABLE:
    309      1.1     lukem 			/* fallthru */
    310      1.1     lukem 			default:
    311      1.1     lukem 				break;
    312      1.1     lukem 			}
    313      1.1     lukem 			exit( EXIT_FAILURE );
    314      1.1     lukem 		}
    315      1.1     lukem 	}
    316      1.1     lukem 
    317      1.1     lukem 	rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
    318      1.1     lukem 		filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
    319      1.1     lukem 	switch ( rc ) {
    320      1.1     lukem 	case LDAP_SIZELIMIT_EXCEEDED:
    321      1.1     lukem 	case LDAP_TIMELIMIT_EXCEEDED:
    322      1.1     lukem 	case LDAP_SUCCESS:
    323      1.1     lukem 		if ( ldap_count_entries( ld, res ) == 0 ) {
    324      1.1     lukem 			if ( rc ) {
    325      1.1     lukem 				tester_ldap_error( ld, "ldap_search_ext_s", NULL );
    326      1.1     lukem 			}
    327      1.1     lukem 			break;
    328      1.1     lukem 		}
    329      1.1     lukem 
    330      1.1     lukem 		for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
    331      1.1     lukem 		{
    332      1.1     lukem 			struct berval **v = ldap_get_values_len( ld, e, attr );
    333      1.1     lukem 
    334      1.1     lukem 			if ( v != NULL ) {
    335      1.1     lukem 				int n = ldap_count_values_len( v );
    336      1.1     lukem 				int j;
    337      1.1     lukem 
    338      1.1     lukem 				values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
    339      1.1     lukem 				for ( j = 0; j < n; j++ ) {
    340      1.1     lukem 					values[ nvalues + j ] = strdup( v[ j ]->bv_val );
    341      1.1     lukem 				}
    342      1.1     lukem 				values[ nvalues + j ] = NULL;
    343      1.1     lukem 				nvalues += n;
    344      1.1     lukem 				ldap_value_free_len( v );
    345      1.1     lukem 			}
    346      1.1     lukem 		}
    347      1.1     lukem 
    348      1.1     lukem 		ldap_msgfree( res );
    349      1.1     lukem 
    350      1.1     lukem 		if ( !values ) {
    351      1.1     lukem 			fprintf( stderr, "  PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
    352      1.1     lukem 				(long) pid, sbase, filter, nvalues );
    353      1.1     lukem 			exit(EXIT_FAILURE);
    354      1.1     lukem 		}
    355      1.1     lukem 
    356      1.1     lukem 		if ( do_retry == maxretries ) {
    357      1.1     lukem 			fprintf( stderr, "  PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
    358      1.1     lukem 				(long) pid, sbase, filter, nvalues );
    359      1.1     lukem 		}
    360      1.1     lukem 
    361      1.1     lukem 		for ( i = 0; i < innerloop; i++ ) {
    362      1.1     lukem 			char	buf[ BUFSIZ ];
    363      1.1     lukem #if 0	/* use high-order bits for better randomness (Numerical Recipes in "C") */
    364      1.1     lukem 			int	r = rand() % nvalues;
    365      1.1     lukem #endif
    366      1.1     lukem 			int	r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
    367      1.1     lukem 
    368      1.1     lukem 			snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
    369      1.1     lukem 
    370      1.1     lukem 			do_search( uri, manager, passwd,
    371      1.1     lukem 				sbase, scope, buf, &ld,
    372      1.1     lukem 				srchattrs, noattrs, nobind,
    373      1.1     lukem 				1, maxretries, delay, force, chaserefs );
    374      1.1     lukem 		}
    375      1.1     lukem 		break;
    376      1.1     lukem 
    377      1.1     lukem 	default:
    378      1.1     lukem 		tester_ldap_error( ld, "ldap_search_ext_s", NULL );
    379      1.1     lukem 		break;
    380      1.1     lukem 	}
    381      1.1     lukem 
    382  1.1.1.2     lukem 	fprintf( stderr, "  PID=%ld - Search done (%d).\n", (long) pid, rc );
    383      1.1     lukem 
    384      1.1     lukem 	if ( ld != NULL ) {
    385      1.1     lukem 		ldap_unbind_ext( ld, NULL, NULL );
    386      1.1     lukem 	}
    387      1.1     lukem }
    388      1.1     lukem 
    389      1.1     lukem static void
    390      1.1     lukem do_search( char *uri, char *manager, struct berval *passwd,
    391      1.1     lukem 	char *sbase, int scope, char *filter, LDAP **ldp,
    392      1.1     lukem 	char **attrs, int noattrs, int nobind,
    393      1.1     lukem 	int innerloop, int maxretries, int delay, int force, int chaserefs )
    394      1.1     lukem {
    395      1.1     lukem 	LDAP	*ld = ldp ? *ldp : NULL;
    396      1.1     lukem 	int  	i = 0, do_retry = maxretries;
    397      1.1     lukem 	int     rc = LDAP_SUCCESS;
    398      1.1     lukem 	int	version = LDAP_VERSION3;
    399      1.1     lukem 	char	buf[ BUFSIZ ];
    400  1.1.1.4      tron 	int		*msgids = NULL, active = 0;
    401      1.1     lukem 
    402  1.1.1.4      tron 	/* make room for msgid */
    403  1.1.1.4      tron 	if ( swamp > 1 ) {
    404  1.1.1.4      tron 		msgids = (int *)calloc( sizeof(int), innerloop );
    405  1.1.1.4      tron 	}
    406      1.1     lukem 
    407      1.1     lukem retry:;
    408      1.1     lukem 	if ( ld == NULL ) {
    409      1.1     lukem 		ldap_initialize( &ld, uri );
    410      1.1     lukem 		if ( ld == NULL ) {
    411      1.1     lukem 			tester_perror( "ldap_initialize", NULL );
    412      1.1     lukem 			exit( EXIT_FAILURE );
    413      1.1     lukem 		}
    414      1.1     lukem 
    415      1.1     lukem 		(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
    416      1.1     lukem 		(void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
    417      1.1     lukem 			chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
    418      1.1     lukem 
    419      1.1     lukem 		if ( do_retry == maxretries ) {
    420      1.1     lukem 			fprintf( stderr,
    421      1.1     lukem 				"PID=%ld - Search(%d): "
    422      1.1     lukem 				"base=\"%s\" scope=%s filter=\"%s\" "
    423      1.1     lukem 				"attrs=%s%s.\n",
    424      1.1     lukem 				(long) pid, innerloop,
    425      1.1     lukem 				sbase, ldap_pvt_scope2str( scope ), filter,
    426      1.1     lukem 				attrs[0], attrs[1] ? " (more...)" : "" );
    427      1.1     lukem 		}
    428      1.1     lukem 
    429      1.1     lukem 		if ( nobind == 0 ) {
    430      1.1     lukem 			rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
    431      1.1     lukem 			if ( rc != LDAP_SUCCESS ) {
    432      1.1     lukem 				snprintf( buf, sizeof( buf ),
    433      1.1     lukem 					"bindDN=\"%s\"", manager );
    434      1.1     lukem 				tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
    435      1.1     lukem 				switch ( rc ) {
    436      1.1     lukem 				case LDAP_BUSY:
    437      1.1     lukem 				case LDAP_UNAVAILABLE:
    438      1.1     lukem 					if ( do_retry > 0 ) {
    439      1.1     lukem 						ldap_unbind_ext( ld, NULL, NULL );
    440      1.1     lukem 						ld = NULL;
    441      1.1     lukem 						do_retry--;
    442      1.1     lukem 						if ( delay != 0 ) {
    443      1.1     lukem 						    sleep( delay );
    444      1.1     lukem 						}
    445      1.1     lukem 						goto retry;
    446      1.1     lukem 					}
    447      1.1     lukem 				/* fallthru */
    448      1.1     lukem 				default:
    449      1.1     lukem 					break;
    450      1.1     lukem 				}
    451      1.1     lukem 				exit( EXIT_FAILURE );
    452      1.1     lukem 			}
    453      1.1     lukem 		}
    454      1.1     lukem 	}
    455      1.1     lukem 
    456  1.1.1.4      tron 	if ( swamp > 1 ) {
    457  1.1.1.4      tron 		do {
    458  1.1.1.4      tron 			LDAPMessage *res = NULL;
    459  1.1.1.4      tron 			int j, msgid;
    460  1.1.1.4      tron 
    461  1.1.1.4      tron 			if ( i < innerloop ) {
    462  1.1.1.4      tron 				rc = ldap_search_ext( ld, sbase, scope,
    463  1.1.1.4      tron 						filter, NULL, noattrs, NULL, NULL,
    464  1.1.1.4      tron 						NULL, LDAP_NO_LIMIT, &msgids[i] );
    465  1.1.1.4      tron 
    466  1.1.1.4      tron 				active++;
    467  1.1.1.4      tron #if 0
    468  1.1.1.4      tron 				fprintf( stderr,
    469  1.1.1.4      tron 					">>> PID=%ld - Search maxloop=%d cnt=%d active=%d msgid=%d: "
    470  1.1.1.4      tron 					"base=\"%s\" scope=%s filter=\"%s\"\n",
    471  1.1.1.4      tron 					(long) pid, innerloop, i, active, msgids[i],
    472  1.1.1.4      tron 					sbase, ldap_pvt_scope2str( scope ), filter );
    473  1.1.1.4      tron #endif
    474  1.1.1.4      tron 				i++;
    475  1.1.1.4      tron 
    476  1.1.1.4      tron 				if ( rc ) {
    477  1.1.1.4      tron 					int first = tester_ignore_err( rc );
    478  1.1.1.4      tron 					/* if ignore.. */
    479  1.1.1.4      tron 					if ( first ) {
    480  1.1.1.4      tron 						/* only log if first occurrence */
    481  1.1.1.4      tron 						if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
    482  1.1.1.4      tron 							tester_ldap_error( ld, "ldap_search_ext", NULL );
    483  1.1.1.4      tron 						}
    484  1.1.1.4      tron 						continue;
    485  1.1.1.4      tron 					}
    486  1.1.1.4      tron 
    487  1.1.1.4      tron 					/* busy needs special handling */
    488  1.1.1.4      tron 					snprintf( buf, sizeof( buf ),
    489  1.1.1.4      tron 						"base=\"%s\" filter=\"%s\"\n",
    490  1.1.1.4      tron 						sbase, filter );
    491  1.1.1.4      tron 					tester_ldap_error( ld, "ldap_search_ext", buf );
    492  1.1.1.4      tron 					if ( rc == LDAP_BUSY && do_retry > 0 ) {
    493  1.1.1.4      tron 						ldap_unbind_ext( ld, NULL, NULL );
    494  1.1.1.4      tron 						ld = NULL;
    495  1.1.1.4      tron 						do_retry--;
    496  1.1.1.4      tron 						goto retry;
    497  1.1.1.4      tron 					}
    498  1.1.1.4      tron 					break;
    499  1.1.1.4      tron 				}
    500      1.1     lukem 
    501  1.1.1.4      tron 				if ( swamp > 2 ) {
    502  1.1.1.4      tron 					continue;
    503      1.1     lukem 				}
    504      1.1     lukem 			}
    505      1.1     lukem 
    506  1.1.1.4      tron 			rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res );
    507  1.1.1.4      tron 			switch ( rc ) {
    508  1.1.1.4      tron 			case -1:
    509  1.1.1.4      tron 				/* gone really bad */
    510  1.1.1.4      tron 				goto cleanup;
    511  1.1.1.4      tron 
    512  1.1.1.4      tron 			case 0:
    513  1.1.1.4      tron 				/* timeout (impossible) */
    514  1.1.1.4      tron 				break;
    515  1.1.1.4      tron 
    516  1.1.1.4      tron 			case LDAP_RES_SEARCH_ENTRY:
    517  1.1.1.4      tron 			case LDAP_RES_SEARCH_REFERENCE:
    518  1.1.1.4      tron 				/* ignore */
    519  1.1.1.4      tron 				break;
    520  1.1.1.4      tron 
    521  1.1.1.4      tron 			case LDAP_RES_SEARCH_RESULT:
    522  1.1.1.4      tron 				/* just remove, no error checking (TODO?) */
    523  1.1.1.4      tron 				msgid = ldap_msgid( res );
    524  1.1.1.4      tron 				ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, NULL, 1 );
    525  1.1.1.4      tron 				res = NULL;
    526  1.1.1.4      tron 
    527  1.1.1.4      tron 				/* linear search, bah */
    528  1.1.1.4      tron 				for ( j = 0; j < i; j++ ) {
    529  1.1.1.4      tron 					if ( msgids[ j ] == msgid ) {
    530  1.1.1.4      tron 						msgids[ j ] = -1;
    531  1.1.1.4      tron 						active--;
    532  1.1.1.4      tron #if 0
    533  1.1.1.4      tron 						fprintf( stderr,
    534  1.1.1.4      tron 							"<<< PID=%ld - SearchDone maxloop=%d cnt=%d active=%d msgid=%d: "
    535  1.1.1.4      tron 							"base=\"%s\" scope=%s filter=\"%s\"\n",
    536  1.1.1.4      tron 							(long) pid, innerloop, j, active, msgid,
    537  1.1.1.4      tron 							sbase, ldap_pvt_scope2str( scope ), filter );
    538  1.1.1.4      tron #endif
    539  1.1.1.4      tron 						break;
    540  1.1.1.4      tron 					}
    541  1.1.1.4      tron 				}
    542  1.1.1.4      tron 				break;
    543  1.1.1.4      tron 
    544  1.1.1.4      tron 			default:
    545  1.1.1.4      tron 				/* other messages unexpected */
    546  1.1.1.4      tron 				fprintf( stderr,
    547  1.1.1.4      tron 					"### PID=%ld - Search(%d): "
    548  1.1.1.4      tron 					"base=\"%s\" scope=%s filter=\"%s\" "
    549  1.1.1.4      tron 					"attrs=%s%s. unexpected response tag=%d\n",
    550  1.1.1.4      tron 					(long) pid, innerloop,
    551  1.1.1.4      tron 					sbase, ldap_pvt_scope2str( scope ), filter,
    552  1.1.1.4      tron 					attrs[0], attrs[1] ? " (more...)" : "", rc );
    553  1.1.1.4      tron 				break;
    554  1.1.1.4      tron 			}
    555  1.1.1.4      tron 
    556  1.1.1.4      tron 			if ( res != NULL ) {
    557  1.1.1.4      tron 				ldap_msgfree( res );
    558  1.1.1.4      tron 			}
    559  1.1.1.4      tron 		} while ( i < innerloop || active > 0 );
    560  1.1.1.4      tron 
    561  1.1.1.4      tron 	} else {
    562  1.1.1.4      tron 		for ( ; i < innerloop; i++ ) {
    563  1.1.1.4      tron 			LDAPMessage *res = NULL;
    564  1.1.1.4      tron 
    565  1.1.1.4      tron 			if (swamp) {
    566  1.1.1.4      tron 				int msgid;
    567  1.1.1.4      tron 				rc = ldap_search_ext( ld, sbase, scope,
    568  1.1.1.4      tron 						filter, NULL, noattrs, NULL, NULL,
    569  1.1.1.4      tron 						NULL, LDAP_NO_LIMIT, &msgid );
    570  1.1.1.4      tron 				if ( rc == LDAP_SUCCESS ) continue;
    571  1.1.1.4      tron 				else break;
    572  1.1.1.4      tron 			}
    573  1.1.1.4      tron 
    574  1.1.1.4      tron 			rc = ldap_search_ext_s( ld, sbase, scope,
    575  1.1.1.4      tron 					filter, attrs, noattrs, NULL, NULL,
    576  1.1.1.4      tron 					NULL, LDAP_NO_LIMIT, &res );
    577  1.1.1.4      tron 			if ( res != NULL ) {
    578  1.1.1.4      tron 				ldap_msgfree( res );
    579  1.1.1.4      tron 			}
    580  1.1.1.4      tron 
    581  1.1.1.4      tron 			if ( rc ) {
    582  1.1.1.4      tron 				int first = tester_ignore_err( rc );
    583  1.1.1.4      tron 				/* if ignore.. */
    584  1.1.1.4      tron 				if ( first ) {
    585  1.1.1.4      tron 					/* only log if first occurrence */
    586  1.1.1.4      tron 					if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
    587  1.1.1.4      tron 						tester_ldap_error( ld, "ldap_search_ext_s", NULL );
    588  1.1.1.4      tron 					}
    589  1.1.1.4      tron 					continue;
    590  1.1.1.4      tron 				}
    591  1.1.1.4      tron 
    592  1.1.1.4      tron 				/* busy needs special handling */
    593  1.1.1.4      tron 				snprintf( buf, sizeof( buf ),
    594  1.1.1.4      tron 					"base=\"%s\" filter=\"%s\"\n",
    595  1.1.1.4      tron 					sbase, filter );
    596  1.1.1.4      tron 				tester_ldap_error( ld, "ldap_search_ext_s", buf );
    597  1.1.1.4      tron 				if ( rc == LDAP_BUSY && do_retry > 0 ) {
    598  1.1.1.4      tron 					ldap_unbind_ext( ld, NULL, NULL );
    599  1.1.1.4      tron 					ld = NULL;
    600  1.1.1.4      tron 					do_retry--;
    601  1.1.1.4      tron 					goto retry;
    602  1.1.1.4      tron 				}
    603  1.1.1.4      tron 				break;
    604      1.1     lukem 			}
    605      1.1     lukem 		}
    606      1.1     lukem 	}
    607      1.1     lukem 
    608  1.1.1.4      tron cleanup:;
    609  1.1.1.4      tron 	if ( msgids != NULL ) {
    610  1.1.1.4      tron 		free( msgids );
    611  1.1.1.4      tron 	}
    612  1.1.1.4      tron 
    613      1.1     lukem 	if ( ldp != NULL ) {
    614      1.1     lukem 		*ldp = ld;
    615      1.1     lukem 
    616      1.1     lukem 	} else {
    617  1.1.1.2     lukem 		fprintf( stderr, "  PID=%ld - Search done (%d).\n", (long) pid, rc );
    618      1.1     lukem 
    619      1.1     lukem 		if ( ld != NULL ) {
    620      1.1     lukem 			ldap_unbind_ext( ld, NULL, NULL );
    621      1.1     lukem 		}
    622      1.1     lukem 	}
    623      1.1     lukem }
    624