]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
add missing "break" to previous commit
[openldap] / tests / progs / slapd-search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Kurt Spanier for inclusion
17  * in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/param.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/wait.h>
32
33 #include <ldap.h>
34 #include <lutil.h>
35
36 #include "slapd-common.h"
37
38 #define LOOPS   100
39 #define RETRIES 0
40
41 static void
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 );
45
46 static void
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 );
50
51 static void
52 usage( char *name )
53 {
54         fprintf( stderr,
55                 "usage: %s "
56                 "-H <uri> | ([-h <host>] -p <port>) "
57                 "-D <manager> "
58                 "-w <passwd> "
59                 "-b <searchbase> "
60                 "-f <searchfilter> "
61                 "[-a <attr>] "
62                 "[-A] "
63                 "[-C] "
64                 "[-F] "
65                 "[-l <loops>] "
66                 "[-L <outerloops>] "
67                 "[-r <maxretries>] "
68                 "[-t <delay>]\n",
69                         name );
70         exit( EXIT_FAILURE );
71 }
72
73 int
74 main( int argc, char **argv )
75 {
76         int             i;
77         char            *uri = NULL;
78         char            *host = "localhost";
79         int             port = -1;
80         char            *manager = NULL;
81         struct berval   passwd = { 0, NULL };
82         char            *sbase = NULL;
83         char            *filter  = NULL;
84         char            *attr = NULL;
85         int             loops = LOOPS;
86         int             outerloops = 1;
87         int             retries = RETRIES;
88         int             delay = 0;
89         int             force = 0;
90         int             chaserefs = 0;
91         int             noattrs = 0;
92
93         tester_init( "slapd-search" );
94
95         while ( (i = getopt( argc, argv, "Aa:b:CD:f:FH:h:l:L:p:w:r:t:" )) != EOF ) {
96                 switch( i ) {
97                 case 'A':
98                         noattrs++;
99                         break;
100
101                 case 'C':
102                         chaserefs++;
103                         break;
104
105                 case 'H':               /* the server uri */
106                         uri = strdup( optarg );
107                         break;
108
109                 case 'h':               /* the servers host */
110                         host = strdup( optarg );
111                         break;
112
113                 case 'p':               /* the servers port */
114                         if ( lutil_atoi( &port, optarg ) != 0 ) {
115                                 usage( argv[0] );
116                         }
117                         break;
118
119                 case 'D':               /* the servers manager */
120                         manager = strdup( optarg );
121                         break;
122
123                 case 'w':               /* the server managers password */
124                         passwd.bv_val = strdup( optarg );
125                         passwd.bv_len = strlen( optarg );
126                         break;
127
128                 case 'a':
129                         attr = strdup( optarg );
130                         break;
131
132                 case 'b':               /* file with search base */
133                         sbase = strdup( optarg );
134                         break;
135
136                 case 'f':               /* the search request */
137                         filter = strdup( optarg );
138                         break;
139
140                 case 'F':
141                         force++;
142                         break;
143
144                 case 'l':               /* number of loops */
145                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
146                                 usage( argv[0] );
147                         }
148                         break;
149
150                 case 'L':               /* number of loops */
151                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
152                                 usage( argv[0] );
153                         }
154                         break;
155
156                 case 'r':               /* number of retries */
157                         if ( lutil_atoi( &retries, optarg ) != 0 ) {
158                                 usage( argv[0] );
159                         }
160                         break;
161
162                 case 't':               /* delay in seconds */
163                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
164                                 usage( argv[0] );
165                         }
166                         break;
167
168                 default:
169                         usage( argv[0] );
170                         break;
171                 }
172         }
173
174         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
175                 usage( argv[0] );
176
177         if ( *filter == '\0' ) {
178
179                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
180                                 argv[0] );
181                 exit( EXIT_FAILURE );
182
183         }
184
185         uri = tester_uri( uri, host, port );
186
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 );
191
192                 } else {
193                         do_search( uri, manager, &passwd, sbase, filter, NULL,
194                                 noattrs, loops, retries, delay, force, chaserefs );
195                 }
196         }
197
198         exit( EXIT_SUCCESS );
199 }
200
201
202 static void
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 )
206 {
207         LDAP    *ld = NULL;
208         int     i = 0, do_retry = maxretries;
209         char    *attrs[ 2 ];
210         pid_t   pid = getpid();
211         int     rc = LDAP_SUCCESS;
212         int     version = LDAP_VERSION3;
213         int     nvalues = 0;
214         char    **values = NULL;
215         LDAPMessage *res = NULL, *e = NULL;
216
217         attrs[ 0 ] = attr;
218         attrs[ 1 ] = NULL;
219
220         ldap_initialize( &ld, uri );
221         if ( ld == NULL ) {
222                 tester_perror( "ldap_initialize" );
223                 exit( EXIT_FAILURE );
224         }
225
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 );
229
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 );
233         }
234
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" );
238                 switch ( rc ) {
239                 case LDAP_BUSY:
240                 case LDAP_UNAVAILABLE:
241                 /* fallthru */
242                 default:
243                         break;
244                 }
245                 exit( EXIT_FAILURE );
246         }
247
248         rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
249                 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
250         switch ( rc ) {
251         case LDAP_SIZELIMIT_EXCEEDED:
252         case LDAP_TIMELIMIT_EXCEEDED:
253         case LDAP_SUCCESS:
254                 if ( ldap_count_entries( ld, res ) == 0 ) {
255                         break;
256                 }
257
258                 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
259                 {
260                         struct berval **v = ldap_get_values_len( ld, e, attr );
261
262                         if ( v != NULL ) {
263                                 int n = ldap_count_values_len( v );
264                                 int j;
265
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 );
269                                 }
270                                 values[ nvalues + j ] = NULL;
271                                 nvalues += n;
272                                 ldap_value_free_len( v );
273                         }
274                 }
275
276                 ldap_msgfree( res );
277
278                 if ( do_retry == maxretries ) {
279                         fprintf( stderr, "PID=%ld - got %d values.\n", (long) pid, nvalues );
280                 }
281
282                 for ( i = 0; i < innerloop; i++ ) {
283                         char    buf[ BUFSIZ ];
284
285                         snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ rand() % nvalues ] );
286
287                         do_search( uri, manager, passwd, sbase, buf, &ld, noattrs,
288                                         1, maxretries, delay, force, chaserefs );
289                 }
290                 break;
291
292         default:
293                 tester_ldap_error( ld, "ldap_search_ext_s" );
294                 break;
295         }
296
297         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
298
299         if ( ld != NULL ) {
300                 ldap_unbind_ext( ld, NULL, NULL );
301         }
302 }
303
304 static void
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 )
309 {
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;
316         int     first = 1;
317
318 retry:;
319         if ( ld == NULL ) {
320                 ldap_initialize( &ld, uri );
321                 if ( ld == NULL ) {
322                         tester_perror( "ldap_initialize" );
323                         exit( EXIT_FAILURE );
324                 }
325
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 );
329
330                 if ( do_retry == maxretries ) {
331                         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
332                                         (long) pid, innerloop, sbase, filter );
333                 }
334
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" );
338                         switch ( rc ) {
339                         case LDAP_BUSY:
340                         case LDAP_UNAVAILABLE:
341                                 if ( do_retry > 0 ) {
342                                         ldap_unbind_ext( ld, NULL, NULL );
343                                         do_retry--;
344                                         if ( delay != 0 ) {
345                                             sleep( delay );
346                                         }
347                                         goto retry;
348                                 }
349                         /* fallthru */
350                         default:
351                                 break;
352                         }
353                         exit( EXIT_FAILURE );
354                 }
355         }
356
357         for ( ; i < innerloop; i++ ) {
358                 LDAPMessage *res = NULL;
359
360                 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
361                                 filter, attrs, noattrs, NULL, NULL,
362                                 NULL, LDAP_NO_LIMIT, &res );
363                 if ( res != NULL ) {
364                         ldap_msgfree( res );
365                 }
366
367                 switch ( rc ) {
368                 case LDAP_REFERRAL:
369                         /* don't log: it's intended */
370                         if ( force >= 2 ) {
371                                 if ( !first ) {
372                                         break;
373                                 }
374                                 first = 0;
375                         }
376                         tester_ldap_error( ld, "ldap_search_ext_s" );
377                         /* fallthru */
378
379                 case LDAP_SUCCESS:
380                         break;
381
382                 default:
383                         tester_ldap_error( ld, "ldap_search_ext_s" );
384                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
385                                 ldap_unbind_ext( ld, NULL, NULL );
386                                 do_retry--;
387                                 goto retry;
388                         }
389                         if ( rc != LDAP_NO_SUCH_OBJECT ) {
390                                 goto done;
391                         }
392                         break;
393                 }
394         }
395
396 done:;
397         if ( ldp != NULL ) {
398                 *ldp = ld;
399
400         } else {
401                 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
402
403                 if ( ld != NULL ) {
404                         ldap_unbind_ext( ld, NULL, NULL );
405                 }
406         }
407 }