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