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