]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
allow selective handling of errors
[openldap] / tests / progs / slapd-read.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_read( char *uri, char *manager, struct berval *passwd,
43         char *entry, LDAP **ld, int noattrs, int maxloop,
44         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, int noattrs,
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                 "-e <entry> "
60                 "[-A] "
61                 "[-C] "
62                 "[-F] "
63                 "[-f filter] "
64                 "[-i <ignore>] "
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            *entry = NULL;
83         char            *filter  = NULL;
84         int             loops = LOOPS;
85         int             outerloops = 1;
86         int             retries = RETRIES;
87         int             delay = 0;
88         int             force = 0;
89         int             chaserefs = 0;
90         int             noattrs = 0;
91
92         tester_init( "slapd-read", TESTER_READ );
93
94         /* by default, tolerate referrals and no such object */
95         tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
96
97         while ( (i = getopt( argc, argv, "ACD:H:h:i:p:e:Ff:l:L:r:t:w:" )) != EOF ) {
98                 switch( i ) {
99                 case 'A':
100                         noattrs++;
101                         break;
102
103                 case 'C':
104                         chaserefs++;
105                         break;
106
107                 case 'H':               /* the server uri */
108                         uri = strdup( optarg );
109                         break;
110
111                 case 'h':               /* the servers host */
112                         host = strdup( optarg );
113                         break;
114
115                 case 'i':
116                         tester_ignore_str2errlist( optarg );
117                         break;
118
119                 case 'p':               /* the servers port */
120                         if ( lutil_atoi( &port, optarg ) != 0 ) {
121                                 usage( argv[0] );
122                         }
123                         break;
124
125                 case 'D':               /* the servers manager */
126                         manager = strdup( optarg );
127                         break;
128
129                 case 'w':               /* the server managers password */
130                         passwd.bv_val = strdup( optarg );
131                         passwd.bv_len = strlen( optarg );
132                         break;
133
134                 case 'e':               /* DN to search for */
135                         entry = strdup( optarg );
136                         break;
137
138                 case 'f':               /* the search request */
139                         filter = strdup( optarg );
140                         break;
141
142                 case 'F':
143                         force++;
144                         break;
145
146                 case 'l':               /* the number of loops */
147                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
148                                 usage( argv[0] );
149                         }
150                         break;
151
152                 case 'L':               /* the number of outerloops */
153                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
154                                 usage( argv[0] );
155                         }
156                         break;
157
158                 case 'r':               /* the number of retries */
159                         if ( lutil_atoi( &retries, optarg ) != 0 ) {
160                                 usage( argv[0] );
161                         }
162                         break;
163
164                 case 't':               /* delay in seconds */
165                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
166                                 usage( argv[0] );
167                         }
168                         break;
169
170                 default:
171                         usage( argv[0] );
172                         break;
173                 }
174         }
175
176         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
177                 usage( argv[0] );
178
179         if ( *entry == '\0' ) {
180                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
181                                 argv[0] );
182                 exit( EXIT_FAILURE );
183         }
184
185         uri = tester_uri( uri, host, port );
186
187         for ( i = 0; i < outerloops; i++ ) {
188                 if ( filter != NULL ) {
189                         do_random( uri, manager, &passwd, entry, filter,
190                                 noattrs, loops, retries, delay, force,
191                                 chaserefs );
192
193                 } else {
194                         do_read( uri, manager, &passwd, entry, NULL, noattrs,
195                                 loops, retries, delay, force, chaserefs );
196                 }
197         }
198
199         exit( EXIT_SUCCESS );
200 }
201
202 static void
203 do_random( char *uri, char *manager, struct berval *passwd,
204         char *sbase, char *filter, 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 ] = LDAP_NO_ATTRS;
218         attrs[ 1 ] = NULL;
219
220         ldap_initialize( &ld, uri );
221         if ( ld == NULL ) {
222                 tester_perror( "ldap_initialize", NULL );
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 - Read(%d): base=\"%s\", filter=\"%s\".\n",
232                                 (long) pid, innerloop, sbase, filter );
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", NULL );
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                 nvalues = ldap_count_entries( ld, res );
255                 if ( nvalues == 0 ) {
256                         if ( rc ) {
257                                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
258                         }
259                         break;
260                 }
261
262                 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
263                 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
264                 {
265                         values[ i ] = ldap_get_dn( ld, e );
266                 }
267                 values[ i ] = NULL;
268
269                 ldap_msgfree( res );
270
271                 if ( do_retry == maxretries ) {
272                         fprintf( stderr, "  PID=%ld - got %d values.\n", (long) pid, nvalues );
273                 }
274
275                 for ( i = 0; i < innerloop; i++ ) {
276                         do_read( uri, manager, passwd, values[ rand() % nvalues ], &ld,
277                                 noattrs, 1, maxretries, delay, force,
278                                 chaserefs );
279                 }
280                 break;
281
282         default:
283                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
284                 break;
285         }
286
287         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
288
289         if ( ld != NULL ) {
290                 ldap_unbind_ext( ld, NULL, NULL );
291         }
292 }
293
294 static void
295 do_read( char *uri, char *manager, struct berval *passwd, char *entry,
296         LDAP **ldp, int noattrs, int maxloop,
297         int maxretries, int delay, int force, int chaserefs )
298 {
299         LDAP    *ld = ldp ? *ldp : NULL;
300         int     i = 0, do_retry = maxretries;
301         char    *attrs[] = { "1.1", NULL };
302         pid_t   pid = getpid();
303         int     rc = LDAP_SUCCESS;
304         int     version = LDAP_VERSION3;
305
306 retry:;
307         if ( ld == NULL ) {
308                 ldap_initialize( &ld, uri );
309                 if ( ld == NULL ) {
310                         tester_perror( "ldap_initialize", NULL );
311                         exit( EXIT_FAILURE );
312                 }
313
314                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
315                 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
316                         chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
317
318                 if ( do_retry == maxretries ) {
319                         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
320                                 (long) pid, maxloop, entry );
321                 }
322
323                 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
324                 if ( rc != LDAP_SUCCESS ) {
325                         tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
326                         switch ( rc ) {
327                         case LDAP_BUSY:
328                         case LDAP_UNAVAILABLE:
329                                 if ( do_retry > 0 ) {
330                                         ldap_unbind_ext( ld, NULL, NULL );
331                                         do_retry--;
332                                         if ( delay != 0 ) {
333                                             sleep( delay );
334                                         }
335                                         goto retry;
336                                 }
337                         /* fallthru */
338                         default:
339                                 break;
340                         }
341                         exit( EXIT_FAILURE );
342                 }
343         }
344
345         for ( ; i < maxloop; i++ ) {
346                 LDAPMessage *res = NULL;
347
348                 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
349                                 NULL, attrs, noattrs, NULL, NULL, NULL,
350                                 LDAP_NO_LIMIT, &res );
351                 if ( res != NULL ) {
352                         ldap_msgfree( res );
353                 }
354
355                 if ( rc ) {
356                         unsigned first = tester_ignore_err( rc );
357                         /* if ignore.. */
358                         if ( first ) {
359                                 /* only log if first occurrence */
360                                 if ( force < 2 || first == 1 ) {
361                                         tester_ldap_error( ld, "ldap_search_ext_s", NULL );
362                                 }
363                                 continue;
364                         }
365
366                         /* busy needs special handling */
367                         tester_ldap_error( ld, "ldap_search_ext_s", NULL );
368                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
369                                 ldap_unbind_ext( ld, NULL, NULL );
370                                 ld = NULL;
371                                 do_retry--;
372                                 goto retry;
373                         }
374                         break;
375                 }
376         }
377
378         if ( ldp != NULL ) {
379                 *ldp = ld;
380
381         } else {
382                 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
383
384                 if ( ld != NULL ) {
385                         ldap_unbind_ext( ld, NULL, NULL );
386                 }
387         }
388 }
389