]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
Quick merge: everything from HEAD
[openldap] / tests / progs / slapd-search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2007 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, int nobind,
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 nobind,
49         int innerloop, 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                 "[-N] "
66                 "[-i <ignore>] "
67                 "[-l <loops>] "
68                 "[-L <outerloops>] "
69                 "[-r <maxretries>] "
70                 "[-t <delay>]\n",
71                         name );
72         exit( EXIT_FAILURE );
73 }
74
75 int
76 main( int argc, char **argv )
77 {
78         int             i;
79         char            *uri = NULL;
80         char            *host = "localhost";
81         int             port = -1;
82         char            *manager = NULL;
83         struct berval   passwd = { 0, NULL };
84         char            *sbase = NULL;
85         char            *filter  = NULL;
86         char            *attr = NULL;
87         int             loops = LOOPS;
88         int             outerloops = 1;
89         int             retries = RETRIES;
90         int             delay = 0;
91         int             force = 0;
92         int             chaserefs = 0;
93         int             noattrs = 0;
94         int             nobind = 0;
95
96         tester_init( "slapd-search", TESTER_SEARCH );
97
98         /* by default, tolerate referrals and no such object */
99         tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
100
101         while ( ( i = getopt( argc, argv, "Aa:b:CD:f:FH:h:i:l:L:Np:r:t:w:" ) ) != EOF )
102         {
103                 switch ( i ) {
104                 case 'A':
105                         noattrs++;
106                         break;
107
108                 case 'C':
109                         chaserefs++;
110                         break;
111
112                 case 'H':               /* the server uri */
113                         uri = strdup( optarg );
114                         break;
115
116                 case 'h':               /* the servers host */
117                         host = strdup( optarg );
118                         break;
119
120                 case 'i':
121                         tester_ignore_str2errlist( optarg );
122                         break;
123
124                 case 'N':
125                         nobind++;
126                         break;
127
128                 case 'p':               /* the servers port */
129                         if ( lutil_atoi( &port, optarg ) != 0 ) {
130                                 usage( argv[0] );
131                         }
132                         break;
133
134                 case 'D':               /* the servers manager */
135                         manager = strdup( optarg );
136                         break;
137
138                 case 'w':               /* the server managers password */
139                         passwd.bv_val = strdup( optarg );
140                         passwd.bv_len = strlen( optarg );
141                         memset( optarg, '*', passwd.bv_len );
142                         break;
143
144                 case 'a':
145                         attr = strdup( optarg );
146                         break;
147
148                 case 'b':               /* file with search base */
149                         sbase = strdup( optarg );
150                         break;
151
152                 case 'f':               /* the search request */
153                         filter = strdup( optarg );
154                         break;
155
156                 case 'F':
157                         force++;
158                         break;
159
160                 case 'l':               /* number of loops */
161                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
162                                 usage( argv[0] );
163                         }
164                         break;
165
166                 case 'L':               /* number of loops */
167                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
168                                 usage( argv[0] );
169                         }
170                         break;
171
172                 case 'r':               /* number of retries */
173                         if ( lutil_atoi( &retries, optarg ) != 0 ) {
174                                 usage( argv[0] );
175                         }
176                         break;
177
178                 case 't':               /* delay in seconds */
179                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
180                                 usage( argv[0] );
181                         }
182                         break;
183
184                 default:
185                         usage( argv[0] );
186                         break;
187                 }
188         }
189
190         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
191                 usage( argv[0] );
192
193         if ( *filter == '\0' ) {
194
195                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
196                                 argv[0] );
197                 exit( EXIT_FAILURE );
198
199         }
200
201         uri = tester_uri( uri, host, port );
202
203         for ( i = 0; i < outerloops; i++ ) {
204                 if ( attr != NULL ) {
205                         do_random( uri, manager, &passwd, sbase, filter, attr,
206                                 noattrs, nobind, loops, retries, delay, force, chaserefs );
207
208                 } else {
209                         do_search( uri, manager, &passwd, sbase, filter, NULL,
210                                 noattrs, nobind, loops, retries, delay, force, chaserefs );
211                 }
212         }
213
214         exit( EXIT_SUCCESS );
215 }
216
217
218 static void
219 do_random( char *uri, char *manager, struct berval *passwd,
220         char *sbase, char *filter, char *attr, int noattrs, int nobind,
221         int innerloop, int maxretries, int delay, int force, int chaserefs )
222 {
223         LDAP    *ld = NULL;
224         int     i = 0, do_retry = maxretries;
225         char    *attrs[ 2 ];
226         int     rc = LDAP_SUCCESS;
227         int     version = LDAP_VERSION3;
228         int     nvalues = 0;
229         char    **values = NULL;
230         LDAPMessage *res = NULL, *e = NULL;
231
232         attrs[ 0 ] = attr;
233         attrs[ 1 ] = NULL;
234
235         ldap_initialize( &ld, uri );
236         if ( ld == NULL ) {
237                 tester_perror( "ldap_initialize", NULL );
238                 exit( EXIT_FAILURE );
239         }
240
241         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
242         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
243                 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
244
245         if ( do_retry == maxretries ) {
246                 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
247                                 (long) pid, innerloop, sbase, filter, attr );
248         }
249
250         if ( nobind == 0 ) {
251                 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
252                 if ( rc != LDAP_SUCCESS ) {
253                         tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
254                         switch ( rc ) {
255                         case LDAP_BUSY:
256                         case LDAP_UNAVAILABLE:
257                         /* fallthru */
258                         default:
259                                 break;
260                         }
261                         exit( EXIT_FAILURE );
262                 }
263         }
264
265         rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
266                 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
267         switch ( rc ) {
268         case LDAP_SIZELIMIT_EXCEEDED:
269         case LDAP_TIMELIMIT_EXCEEDED:
270         case LDAP_SUCCESS:
271                 if ( ldap_count_entries( ld, res ) == 0 ) {
272                         if ( rc ) {
273                                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
274                         }
275                         break;
276                 }
277
278                 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
279                 {
280                         struct berval **v = ldap_get_values_len( ld, e, attr );
281
282                         if ( v != NULL ) {
283                                 int n = ldap_count_values_len( v );
284                                 int j;
285
286                                 values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
287                                 for ( j = 0; j < n; j++ ) {
288                                         values[ nvalues + j ] = strdup( v[ j ]->bv_val );
289                                 }
290                                 values[ nvalues + j ] = NULL;
291                                 nvalues += n;
292                                 ldap_value_free_len( v );
293                         }
294                 }
295
296                 ldap_msgfree( res );
297
298                 if ( !values ) {
299                         fprintf( stderr, "  PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
300                                 (long) pid, sbase, filter, nvalues );
301                         exit(EXIT_FAIL);
302                 }
303
304                 if ( do_retry == maxretries ) {
305                         fprintf( stderr, "  PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
306                                 (long) pid, sbase, filter, nvalues );
307                 }
308
309                 for ( i = 0; i < innerloop; i++ ) {
310                         char    buf[ BUFSIZ ];
311 #if 0   /* use high-order bits for better randomness (Numerical Recipes in "C") */
312                         int     r = rand() % nvalues;
313 #endif
314                         int     r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
315
316                         snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
317
318                         do_search( uri, manager, passwd, sbase, buf, &ld, noattrs, nobind,
319                                         1, maxretries, delay, force, chaserefs );
320                 }
321                 break;
322
323         default:
324                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
325                 break;
326         }
327
328         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
329
330         if ( ld != NULL ) {
331                 ldap_unbind_ext( ld, NULL, NULL );
332         }
333 }
334
335 static void
336 do_search( char *uri, char *manager, struct berval *passwd,
337         char *sbase, char *filter, LDAP **ldp, int noattrs, int nobind,
338         int innerloop, int maxretries, int delay, int force, int chaserefs )
339 {
340         LDAP    *ld = ldp ? *ldp : NULL;
341         int     i = 0, do_retry = maxretries;
342         char    *attrs[] = { "cn", "sn", NULL };
343         int     rc = LDAP_SUCCESS;
344         int     version = LDAP_VERSION3;
345         char    buf[ BUFSIZ ];
346
347
348 retry:;
349         if ( ld == NULL ) {
350                 ldap_initialize( &ld, uri );
351                 if ( ld == NULL ) {
352                         tester_perror( "ldap_initialize", NULL );
353                         exit( EXIT_FAILURE );
354                 }
355
356                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
357                 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
358                         chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
359
360                 if ( do_retry == maxretries ) {
361                         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
362                                         (long) pid, innerloop, sbase, filter );
363                 }
364
365                 if ( nobind == 0 ) {
366                         rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
367                         if ( rc != LDAP_SUCCESS ) {
368                                 snprintf( buf, sizeof( buf ),
369                                         "bindDN=\"%s\"", manager );
370                                 tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
371                                 switch ( rc ) {
372                                 case LDAP_BUSY:
373                                 case LDAP_UNAVAILABLE:
374                                         if ( do_retry > 0 ) {
375                                                 ldap_unbind_ext( ld, NULL, NULL );
376                                                 do_retry--;
377                                                 if ( delay != 0 ) {
378                                                     sleep( delay );
379                                                 }
380                                                 goto retry;
381                                         }
382                                 /* fallthru */
383                                 default:
384                                         break;
385                                 }
386                                 exit( EXIT_FAILURE );
387                         }
388                 }
389         }
390
391         for ( ; i < innerloop; i++ ) {
392                 LDAPMessage *res = NULL;
393
394                 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
395                                 filter, attrs, noattrs, NULL, NULL,
396                                 NULL, LDAP_NO_LIMIT, &res );
397                 if ( res != NULL ) {
398                         ldap_msgfree( res );
399                 }
400
401                 if ( rc ) {
402                         unsigned first = tester_ignore_err( rc );
403                         /* if ignore.. */
404                         if ( first ) {
405                                 /* only log if first occurrence */
406                                 if ( force < 2 || first == 1 ) {
407                                         tester_ldap_error( ld, "ldap_search_ext_s", NULL );
408                                 }
409                                 continue;
410                         }
411
412                         /* busy needs special handling */
413                         snprintf( buf, sizeof( buf ),
414                                 "base=\"%s\" filter=\"%s\"\n",
415                                 sbase, filter );
416                         tester_ldap_error( ld, "ldap_search_ext_s", buf );
417                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
418                                 ldap_unbind_ext( ld, NULL, NULL );
419                                 ld = NULL;
420                                 do_retry--;
421                                 goto retry;
422                         }
423                         break;
424                 }
425         }
426
427         if ( ldp != NULL ) {
428                 *ldp = ld;
429
430         } else {
431                 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
432
433                 if ( ld != NULL ) {
434                         ldap_unbind_ext( ld, NULL, NULL );
435                 }
436         }
437 }