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