Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / tests / progs / slapd-read.c
blobb097c884862dd57e4d0b88d2c8a4a261990a04cf
1 /* $OpenLDAP: pkg/ldap/tests/progs/slapd-read.c,v 1.37.2.6 2008/02/11 23:26:50 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 The OpenLDAP Foundation.
5 * All rights reserved.
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.
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>.
15 /* ACKNOWLEDGEMENTS:
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
20 #include "portable.h"
22 #include <stdio.h>
24 #include "ac/stdlib.h"
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"
33 #include "ldap.h"
34 #include "lutil.h"
36 #include "ldap_pvt.h"
38 #include "slapd-common.h"
40 #define LOOPS 100
41 #define RETRIES 0
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 );
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 );
54 static void
55 usage( char *name )
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 );
80 int
81 main( int argc, char **argv )
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;
102 tester_init( "slapd-read", TESTER_READ );
104 /* by default, tolerate referrals and no such object */
105 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
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;
113 case 'C':
114 chaserefs++;
115 break;
117 case 'H': /* the server uri */
118 uri = strdup( optarg );
119 break;
121 case 'h': /* the servers host */
122 host = strdup( optarg );
123 break;
125 case 'i':
126 tester_ignore_str2errlist( optarg );
127 break;
129 case 'N':
130 nobind++;
131 break;
133 case 'p': /* the servers port */
134 if ( lutil_atoi( &port, optarg ) != 0 ) {
135 usage( argv[0] );
137 break;
139 case 'D': /* the servers manager */
140 manager = strdup( optarg );
141 break;
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;
149 case 'e': /* DN to search for */
150 entry = strdup( optarg );
151 break;
153 case 'f': /* the search request */
154 filter = strdup( optarg );
155 break;
157 case 'F':
158 force++;
159 break;
161 case 'l': /* the number of loops */
162 if ( lutil_atoi( &loops, optarg ) != 0 ) {
163 usage( argv[0] );
165 break;
167 case 'L': /* the number of outerloops */
168 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
169 usage( argv[0] );
171 break;
173 case 'r': /* the number of retries */
174 if ( lutil_atoi( &retries, optarg ) != 0 ) {
175 usage( argv[0] );
177 break;
179 case 't': /* delay in seconds */
180 if ( lutil_atoi( &delay, optarg ) != 0 ) {
181 usage( argv[0] );
183 break;
185 case 'T':
186 attrs = ldap_str2charray( optarg, "," );
187 if ( attrs == NULL ) {
188 usage( argv[0] );
190 break;
192 default:
193 usage( argv[0] );
194 break;
198 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
199 usage( argv[0] );
201 if ( *entry == '\0' ) {
202 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
203 argv[0] );
204 exit( EXIT_FAILURE );
207 if ( argv[optind] != NULL ) {
208 attrs = &argv[optind];
211 uri = tester_uri( uri, host, port );
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 );
219 } else {
220 do_read( uri, manager, &passwd, entry, NULL, attrs,
221 noattrs, nobind, loops, retries, delay, force,
222 chaserefs );
226 exit( EXIT_SUCCESS );
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 )
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;
243 attrs[ 0 ] = LDAP_NO_ATTRS;
244 attrs[ 1 ] = NULL;
246 ldap_initialize( &ld, uri );
247 if ( ld == NULL ) {
248 tester_perror( "ldap_initialize", NULL );
249 exit( EXIT_FAILURE );
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 );
256 if ( do_retry == maxretries ) {
257 fprintf( stderr, "PID=%ld - Read(%d): base=\"%s\", filter=\"%s\".\n",
258 (long) pid, innerloop, sbase, filter );
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;
272 exit( EXIT_FAILURE );
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 );
287 break;
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 ) )
293 values[ i ] = ldap_get_dn( ld, e );
295 values[ i ] = NULL;
297 ldap_msgfree( res );
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 );
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);
310 do_read( uri, manager, passwd, values[ r ], &ld,
311 srchattrs, noattrs, nobind, 1, maxretries,
312 delay, force, chaserefs );
314 free( values );
315 break;
317 default:
318 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
319 break;
322 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
324 if ( ld != NULL ) {
325 ldap_unbind_ext( ld, NULL, NULL );
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 )
334 LDAP *ld = ldp ? *ldp : NULL;
335 int i = 0, do_retry = maxretries;
336 int rc = LDAP_SUCCESS;
337 int version = LDAP_VERSION3;
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 );
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 );
351 if ( do_retry == maxretries ) {
352 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
353 (long) pid, maxloop, entry );
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 );
370 goto retry;
372 /* fallthru */
373 default:
374 break;
376 exit( EXIT_FAILURE );
381 for ( ; i < maxloop; i++ ) {
382 LDAPMessage *res = NULL;
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 );
391 if ( rc ) {
392 unsigned first = tester_ignore_err( rc );
393 char buf[ BUFSIZ ];
395 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
397 /* if ignore.. */
398 if ( first ) {
399 /* only log if first occurrence */
400 if ( force < 2 || first == 1 ) {
401 tester_ldap_error( ld, buf, NULL );
403 continue;
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;
414 break;
418 if ( ldp != NULL ) {
419 *ldp = ld;
421 } else {
422 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
424 if ( ld != NULL ) {
425 ldap_unbind_ext( ld, NULL, NULL );