]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
Unconditionalize gmtime_mutex, always use it when calling lutil_csnstr
[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 ( do_retry == maxretries ) {
299                         fprintf( stderr, "  PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
300                                 (long) pid, sbase, filter, nvalues );
301                 }
302
303                 for ( i = 0; i < innerloop; i++ ) {
304                         char    buf[ BUFSIZ ];
305 #if 0   /* use high-order bits for better randomness (Numerical Recipes in "C") */
306                         int     r = rand() % nvalues;
307 #endif
308                         int     r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
309
310                         snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
311
312                         do_search( uri, manager, passwd, sbase, buf, &ld, noattrs, nobind,
313                                         1, maxretries, delay, force, chaserefs );
314                 }
315                 break;
316
317         default:
318                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
319                 break;
320         }
321
322         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
323
324         if ( ld != NULL ) {
325                 ldap_unbind_ext( ld, NULL, NULL );
326         }
327 }
328
329 static void
330 do_search( char *uri, char *manager, struct berval *passwd,
331         char *sbase, char *filter, LDAP **ldp, int noattrs, int nobind,
332         int innerloop, int maxretries, int delay, int force, int chaserefs )
333 {
334         LDAP    *ld = ldp ? *ldp : NULL;
335         int     i = 0, do_retry = maxretries;
336         char    *attrs[] = { "cn", "sn", NULL };
337         int     rc = LDAP_SUCCESS;
338         int     version = LDAP_VERSION3;
339         char    buf[ BUFSIZ ];
340
341
342 retry:;
343         if ( ld == NULL ) {
344                 ldap_initialize( &ld, uri );
345                 if ( ld == NULL ) {
346                         tester_perror( "ldap_initialize", NULL );
347                         exit( EXIT_FAILURE );
348                 }
349
350                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
351                 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
352                         chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
353
354                 if ( do_retry == maxretries ) {
355                         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
356                                         (long) pid, innerloop, sbase, filter );
357                 }
358
359                 if ( nobind == 0 ) {
360                         rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
361                         if ( rc != LDAP_SUCCESS ) {
362                                 snprintf( buf, sizeof( buf ),
363                                         "bindDN=\"%s\"", manager );
364                                 tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
365                                 switch ( rc ) {
366                                 case LDAP_BUSY:
367                                 case LDAP_UNAVAILABLE:
368                                         if ( do_retry > 0 ) {
369                                                 ldap_unbind_ext( ld, NULL, NULL );
370                                                 do_retry--;
371                                                 if ( delay != 0 ) {
372                                                     sleep( delay );
373                                                 }
374                                                 goto retry;
375                                         }
376                                 /* fallthru */
377                                 default:
378                                         break;
379                                 }
380                                 exit( EXIT_FAILURE );
381                         }
382                 }
383         }
384
385         for ( ; i < innerloop; i++ ) {
386                 LDAPMessage *res = NULL;
387
388                 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
389                                 filter, attrs, noattrs, NULL, NULL,
390                                 NULL, LDAP_NO_LIMIT, &res );
391                 if ( res != NULL ) {
392                         ldap_msgfree( res );
393                 }
394
395                 if ( rc ) {
396                         unsigned first = tester_ignore_err( rc );
397                         /* if ignore.. */
398                         if ( first ) {
399                                 /* only log if first occurrence */
400                                 if ( force < 2 || first == 1 ) {
401                                         tester_ldap_error( ld, "ldap_search_ext_s", NULL );
402                                 }
403                                 continue;
404                         }
405
406                         /* busy needs special handling */
407                         snprintf( buf, sizeof( buf ),
408                                 "base=\"%s\" filter=\"%s\"\n",
409                                 sbase, filter );
410                         tester_ldap_error( ld, "ldap_search_ext_s", buf );
411                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
412                                 ldap_unbind_ext( ld, NULL, NULL );
413                                 ld = NULL;
414                                 do_retry--;
415                                 goto retry;
416                         }
417                         break;
418                 }
419         }
420
421         if ( ldp != NULL ) {
422                 *ldp = ld;
423
424         } else {
425                 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
426
427                 if ( ld != NULL ) {
428                         ldap_unbind_ext( ld, NULL, NULL );
429                 }
430         }
431 }