2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
36 #include "slapd-common.h"
42 do_search( char *uri, char *manager, struct berval *passwd,
43 char *sbase, char *filter, LDAP **ldp, int noattrs,
44 int innerloop, int maxretries, int delay, int force, int chaserefs );
47 do_random( char *uri, char *manager, struct berval *passwd,
48 char *sbase, char *filter, char *attr, int noattrs, int innerloop,
49 int maxretries, int delay, int force, int chaserefs );
56 "-H <uri> | ([-h <host>] -p <port>) "
74 main( int argc, char **argv )
78 char *host = "localhost";
81 struct berval passwd = { 0, NULL };
87 int retries = RETRIES;
93 tester_init( "slapd-search" );
95 while ( (i = getopt( argc, argv, "Aa:b:CD:f:FH:h:l:L:p:w:r:t:" )) != EOF ) {
105 case 'H': /* the server uri */
106 uri = strdup( optarg );
109 case 'h': /* the servers host */
110 host = strdup( optarg );
113 case 'p': /* the servers port */
114 if ( lutil_atoi( &port, optarg ) != 0 ) {
119 case 'D': /* the servers manager */
120 manager = strdup( optarg );
123 case 'w': /* the server managers password */
124 passwd.bv_val = strdup( optarg );
125 passwd.bv_len = strlen( optarg );
129 attr = strdup( optarg );
132 case 'b': /* file with search base */
133 sbase = strdup( optarg );
136 case 'f': /* the search request */
137 filter = strdup( optarg );
144 case 'l': /* number of loops */
145 if ( lutil_atoi( &loops, optarg ) != 0 ) {
150 case 'L': /* number of loops */
151 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
156 case 'r': /* number of retries */
157 if ( lutil_atoi( &retries, optarg ) != 0 ) {
162 case 't': /* delay in seconds */
163 if ( lutil_atoi( &delay, optarg ) != 0 ) {
174 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
177 if ( *filter == '\0' ) {
179 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
181 exit( EXIT_FAILURE );
185 uri = tester_uri( uri, host, port );
187 for ( i = 0; i < outerloops; i++ ) {
188 if ( attr != NULL ) {
189 do_random( uri, manager, &passwd, sbase, filter, attr,
190 noattrs, loops, retries, delay, force, chaserefs );
193 do_search( uri, manager, &passwd, sbase, filter, NULL,
194 noattrs, loops, retries, delay, force, chaserefs );
198 exit( EXIT_SUCCESS );
203 do_random( char *uri, char *manager, struct berval *passwd,
204 char *sbase, char *filter, char *attr, int noattrs,
205 int innerloop, int maxretries, int delay, int force, int chaserefs )
208 int i = 0, do_retry = maxretries;
210 pid_t pid = getpid();
211 int rc = LDAP_SUCCESS;
212 int version = LDAP_VERSION3;
214 char **values = NULL;
215 LDAPMessage *res = NULL, *e = NULL;
220 ldap_initialize( &ld, uri );
222 tester_perror( "ldap_initialize", NULL );
223 exit( EXIT_FAILURE );
226 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
227 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
228 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
230 if ( do_retry == maxretries ) {
231 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
232 (long) pid, innerloop, sbase, filter, attr );
235 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
236 if ( rc != LDAP_SUCCESS ) {
237 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
240 case LDAP_UNAVAILABLE:
245 exit( EXIT_FAILURE );
248 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
249 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
251 case LDAP_SIZELIMIT_EXCEEDED:
252 case LDAP_TIMELIMIT_EXCEEDED:
254 if ( ldap_count_entries( ld, res ) == 0 ) {
256 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
261 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
263 struct berval **v = ldap_get_values_len( ld, e, attr );
266 int n = ldap_count_values_len( v );
269 values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
270 for ( j = 0; j < n; j++ ) {
271 values[ nvalues + j ] = strdup( v[ j ]->bv_val );
273 values[ nvalues + j ] = NULL;
275 ldap_value_free_len( v );
281 if ( do_retry == maxretries ) {
282 fprintf( stderr, "PID=%ld - got %d values.\n", (long) pid, nvalues );
285 for ( i = 0; i < innerloop; i++ ) {
288 snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ rand() % nvalues ] );
290 do_search( uri, manager, passwd, sbase, buf, &ld, noattrs,
291 1, maxretries, delay, force, chaserefs );
296 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
300 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
303 ldap_unbind_ext( ld, NULL, NULL );
308 do_search( char *uri, char *manager, struct berval *passwd,
309 char *sbase, char *filter, LDAP **ldp,
310 int noattrs, int innerloop, int maxretries, int delay,
311 int force, int chaserefs )
313 LDAP *ld = ldp ? *ldp : NULL;
314 int i = 0, do_retry = maxretries;
315 char *attrs[] = { "cn", "sn", NULL };
316 pid_t pid = getpid();
317 int rc = LDAP_SUCCESS;
318 int version = LDAP_VERSION3;
325 ldap_initialize( &ld, uri );
327 tester_perror( "ldap_initialize", NULL );
328 exit( EXIT_FAILURE );
331 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
332 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
333 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
335 if ( do_retry == maxretries ) {
336 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
337 (long) pid, innerloop, sbase, filter );
340 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
341 if ( rc != LDAP_SUCCESS ) {
342 snprintf( buf, sizeof( buf ),
343 "bindDN=\"%s\"", manager );
344 tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
347 case LDAP_UNAVAILABLE:
348 if ( do_retry > 0 ) {
349 ldap_unbind_ext( ld, NULL, NULL );
360 exit( EXIT_FAILURE );
364 for ( ; i < innerloop; i++ ) {
365 LDAPMessage *res = NULL;
367 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
368 filter, attrs, noattrs, NULL, NULL,
369 NULL, LDAP_NO_LIMIT, &res );
376 /* don't log: it's intended */
383 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
390 snprintf( buf, sizeof( buf ),
391 "base=\"%s\" filter=\"%s\"\n",
393 tester_ldap_error( ld, "ldap_search_ext_s", buf );
394 if ( rc == LDAP_BUSY && do_retry > 0 ) {
395 ldap_unbind_ext( ld, NULL, NULL );
399 if ( rc != LDAP_NO_SUCH_OBJECT ) {
411 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
414 ldap_unbind_ext( ld, NULL, NULL );