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" );
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" );
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 ) {
258 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
260 struct berval **v = ldap_get_values_len( ld, e, attr );
263 int n = ldap_count_values_len( v );
266 values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
267 for ( j = 0; j < n; j++ ) {
268 values[ nvalues + j ] = strdup( v[ j ]->bv_val );
270 values[ nvalues + j ] = NULL;
272 ldap_value_free_len( v );
278 if ( do_retry == maxretries ) {
279 fprintf( stderr, "PID=%ld - got %d values.\n", (long) pid, nvalues );
282 for ( i = 0; i < innerloop; i++ ) {
285 snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ rand() % nvalues ] );
287 do_search( uri, manager, passwd, sbase, buf, &ld, noattrs,
288 1, maxretries, delay, force, chaserefs );
293 tester_ldap_error( ld, "ldap_search_ext_s" );
297 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
300 ldap_unbind_ext( ld, NULL, NULL );
305 do_search( char *uri, char *manager, struct berval *passwd,
306 char *sbase, char *filter, LDAP **ldp,
307 int noattrs, int innerloop, int maxretries, int delay,
308 int force, int chaserefs )
310 LDAP *ld = ldp ? *ldp : NULL;
311 int i = 0, do_retry = maxretries;
312 char *attrs[] = { "cn", "sn", NULL };
313 pid_t pid = getpid();
314 int rc = LDAP_SUCCESS;
315 int version = LDAP_VERSION3;
320 ldap_initialize( &ld, uri );
322 tester_perror( "ldap_initialize" );
323 exit( EXIT_FAILURE );
326 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
327 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
328 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
330 if ( do_retry == maxretries ) {
331 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
332 (long) pid, innerloop, sbase, filter );
335 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
336 if ( rc != LDAP_SUCCESS ) {
337 tester_ldap_error( ld, "ldap_sasl_bind_s" );
340 case LDAP_UNAVAILABLE:
341 if ( do_retry > 0 ) {
342 ldap_unbind_ext( ld, NULL, NULL );
353 exit( EXIT_FAILURE );
357 for ( ; i < innerloop; i++ ) {
358 LDAPMessage *res = NULL;
360 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
361 filter, attrs, noattrs, NULL, NULL,
362 NULL, LDAP_NO_LIMIT, &res );
369 /* don't log: it's intended */
376 tester_ldap_error( ld, "ldap_search_ext_s" );
383 tester_ldap_error( ld, "ldap_search_ext_s" );
384 if ( rc == LDAP_BUSY && do_retry > 0 ) {
385 ldap_unbind_ext( ld, NULL, NULL );
389 if ( rc != LDAP_NO_SUCH_OBJECT ) {
401 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
404 ldap_unbind_ext( ld, NULL, NULL );