]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
8bb7067f4dae3ed6b4971e16aba7ef1b19b37740
[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
291         default:
292                 tester_ldap_error( ld, "ldap_search_ext_s" );
293                 break;
294         }
295
296         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
297
298         if ( ld != NULL ) {
299                 ldap_unbind_ext( ld, NULL, NULL );
300         }
301 }
302
303 static void
304 do_search( char *uri, char *manager, struct berval *passwd,
305                 char *sbase, char *filter, LDAP **ldp,
306                 int noattrs, int innerloop, int maxretries, int delay,
307                 int force, int chaserefs )
308 {
309         LDAP    *ld = ldp ? *ldp : NULL;
310         int     i = 0, do_retry = maxretries;
311         char    *attrs[] = { "cn", "sn", NULL };
312         pid_t   pid = getpid();
313         int     rc = LDAP_SUCCESS;
314         int     version = LDAP_VERSION3;
315         int     first = 1;
316
317 retry:;
318         if ( ld == NULL ) {
319                 ldap_initialize( &ld, uri );
320                 if ( ld == NULL ) {
321                         tester_perror( "ldap_initialize" );
322                         exit( EXIT_FAILURE );
323                 }
324
325                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
326                 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
327                         chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
328
329                 if ( do_retry == maxretries ) {
330                         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
331                                         (long) pid, innerloop, sbase, filter );
332                 }
333
334                 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
335                 if ( rc != LDAP_SUCCESS ) {
336                         tester_ldap_error( ld, "ldap_sasl_bind_s" );
337                         switch ( rc ) {
338                         case LDAP_BUSY:
339                         case LDAP_UNAVAILABLE:
340                                 if ( do_retry > 0 ) {
341                                         ldap_unbind_ext( ld, NULL, NULL );
342                                         do_retry--;
343                                         if ( delay != 0 ) {
344                                             sleep( delay );
345                                         }
346                                         goto retry;
347                                 }
348                         /* fallthru */
349                         default:
350                                 break;
351                         }
352                         exit( EXIT_FAILURE );
353                 }
354         }
355
356         for ( ; i < innerloop; i++ ) {
357                 LDAPMessage *res = NULL;
358
359                 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
360                                 filter, attrs, noattrs, NULL, NULL,
361                                 NULL, LDAP_NO_LIMIT, &res );
362                 if ( res != NULL ) {
363                         ldap_msgfree( res );
364                 }
365
366                 switch ( rc ) {
367                 case LDAP_REFERRAL:
368                         /* don't log: it's intended */
369                         if ( force >= 2 ) {
370                                 if ( !first ) {
371                                         break;
372                                 }
373                                 first = 0;
374                         }
375                         tester_ldap_error( ld, "ldap_search_ext_s" );
376                         /* fallthru */
377
378                 case LDAP_SUCCESS:
379                         break;
380
381                 default:
382                         tester_ldap_error( ld, "ldap_search_ext_s" );
383                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
384                                 ldap_unbind_ext( ld, NULL, NULL );
385                                 do_retry--;
386                                 goto retry;
387                         }
388                         if ( rc != LDAP_NO_SUCH_OBJECT ) {
389                                 goto done;
390                         }
391                         break;
392                 }
393         }
394
395 done:;
396         if ( ldp != NULL ) {
397                 *ldp = ld;
398
399         } else {
400                 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
401
402                 if ( ld != NULL ) {
403                         ldap_unbind_ext( ld, NULL, NULL );
404                 }
405         }
406 }