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