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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include "ac/stdlib.h"
28 #include "ac/socket.h"
29 #include "ac/string.h"
30 #include "ac/unistd.h"
38 #include "slapd-common.h"
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
);
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
);
59 "-H <uri> | ([-h <host>] -p <port>) "
81 main( int argc
, char **argv
)
85 char *host
= "localhost";
88 struct berval passwd
= { 0, NULL
};
93 int retries
= RETRIES
;
97 char *srchattrs
[] = { "1.1", NULL
};
98 char **attrs
= srchattrs
;
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
) {
117 case 'H': /* the server uri */
118 uri
= strdup( optarg
);
121 case 'h': /* the servers host */
122 host
= strdup( optarg
);
126 tester_ignore_str2errlist( optarg
);
133 case 'p': /* the servers port */
134 if ( lutil_atoi( &port
, optarg
) != 0 ) {
139 case 'D': /* the servers manager */
140 manager
= strdup( optarg
);
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
);
149 case 'e': /* DN to search for */
150 entry
= strdup( optarg
);
153 case 'f': /* the search request */
154 filter
= strdup( optarg
);
161 case 'l': /* the number of loops */
162 if ( lutil_atoi( &loops
, optarg
) != 0 ) {
167 case 'L': /* the number of outerloops */
168 if ( lutil_atoi( &outerloops
, optarg
) != 0 ) {
173 case 'r': /* the number of retries */
174 if ( lutil_atoi( &retries
, optarg
) != 0 ) {
179 case 't': /* delay in seconds */
180 if ( lutil_atoi( &delay
, optarg
) != 0 ) {
186 attrs
= ldap_str2charray( optarg
, "," );
187 if ( attrs
== NULL
) {
198 if (( entry
== NULL
) || ( port
== -1 && uri
== NULL
))
201 if ( *entry
== '\0' ) {
202 fprintf( stderr
, "%s: invalid EMPTY entry DN.\n",
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
,
220 do_read( uri
, manager
, &passwd
, entry
, NULL
, attrs
,
221 noattrs
, nobind
, loops
, retries
, delay
, force
,
226 exit( EXIT_SUCCESS
);
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
)
235 int i
= 0, do_retry
= maxretries
;
237 int rc
= LDAP_SUCCESS
;
238 int version
= LDAP_VERSION3
;
240 char **values
= NULL
;
241 LDAPMessage
*res
= NULL
, *e
= NULL
;
243 attrs
[ 0 ] = LDAP_NO_ATTRS
;
246 ldap_initialize( &ld
, uri
);
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
);
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
);
267 case LDAP_UNAVAILABLE
:
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
);
279 case LDAP_SIZELIMIT_EXCEEDED
:
280 case LDAP_TIMELIMIT_EXCEEDED
:
282 nvalues
= ldap_count_entries( ld
, res
);
283 if ( nvalues
== 0 ) {
285 tester_ldap_error( ld
, "ldap_search_ext_s", NULL
);
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
);
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
;
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
);
318 tester_ldap_error( ld
, "ldap_search_ext_s", NULL
);
322 fprintf( stderr
, " PID=%ld - Search done (%d).\n", (long) pid
, rc
);
325 ldap_unbind_ext( ld
, NULL
, NULL
);
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
;
341 ldap_initialize( &ld
, uri
);
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
);
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
);
362 case LDAP_UNAVAILABLE
:
363 if ( do_retry
> 0 ) {
364 ldap_unbind_ext( ld
, NULL
, NULL
);
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
);
392 unsigned first
= tester_ignore_err( rc
);
395 snprintf( buf
, sizeof( buf
), "ldap_search_ext_s(%s)", entry
);
399 /* only log if first occurrence */
400 if ( force
< 2 || first
== 1 ) {
401 tester_ldap_error( ld
, buf
, NULL
);
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
);
422 fprintf( stderr
, " PID=%ld - Read done (%d).\n", (long) pid
, rc
);
425 ldap_unbind_ext( ld
, NULL
, NULL
);