2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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 Howard Chu for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
39 #include "slapd-common.h"
44 do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
45 int force, int chaserefs, int noinit, LDAP **ldp );
48 do_base( char *uri, struct berval *base, struct berval *pass, char *pwattr,
49 int maxloop, int force, int chaserefs, int noinit, int delay );
51 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
52 * that DN will be used repeatedly for all of the Binds. If instead -b is used
53 * to specify a base DN, a search will be done for all "person" objects under
54 * that base DN. Then DNs from this list will be randomly selected for each
55 * Bind request. All of the users must have identical passwords. Also it is
56 * assumed that the users are all onelevel children of the base.
61 fprintf( stderr, "usage: %s "
62 "[-H uri | -h <host> [-p port]] "
63 "[-D <dn> [-w <passwd>]] "
64 "[-b <baseDN> [-f <searchfilter>] [-a pwattr]] "
76 static char *filter = "(objectClass=person)";
79 main( int argc, char **argv )
83 char *host = "localhost";
85 struct berval base = { 0, NULL };
86 struct berval pass = { 0, NULL };
96 tester_init( "slapd-bind", TESTER_BIND );
98 /* by default, tolerate invalid credentials */
99 tester_ignore_str2errlist( "INVALID_CREDENTIALS" );
101 while ( (i = getopt( argc, argv, "a:b:H:h:i:p:D:w:l:L:f:FIt:" )) != EOF ) {
107 case 'b': /* base DN of a tree of user DNs */
108 ber_str2bv( optarg, 0, 0, &base );
115 case 'H': /* the server uri */
119 case 'h': /* the servers host */
124 tester_ignore_str2errlist( optarg );
127 case 'p': /* the servers port */
128 if ( lutil_atoi( &port, optarg ) != 0 ) {
138 ber_str2bv( optarg, 0, 0, &pass );
141 case 'l': /* the number of loops */
142 if ( lutil_atoi( &loops, optarg ) != 0 ) {
147 case 'L': /* the number of outerloops */
148 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
162 /* reuse connection */
167 /* sleep between binds */
168 if ( lutil_atoi( &delay, optarg ) != 0 ) {
179 if ( port == -1 && uri == NULL ) {
183 uri = tester_uri( uri, host, port );
185 for ( i = 0; i < outerloops; i++ ) {
186 if ( base.bv_val != NULL ) {
187 do_base( uri, &base, &pass, pwattr, loops,
188 force, chaserefs, noinit, delay );
190 do_bind( uri, dn, &pass, loops,
191 force, chaserefs, noinit, NULL );
195 exit( EXIT_SUCCESS );
200 do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
201 int force, int chaserefs, int noinit, LDAP **ldp )
203 LDAP *ld = ldp ? *ldp : NULL;
205 pid_t pid = getpid();
208 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
209 (long) pid, maxloop, dn );
211 for ( i = 0; i < maxloop; i++ ) {
212 if ( !noinit || ld == NULL ) {
213 int version = LDAP_VERSION3;
214 ldap_initialize( &ld, uri );
216 tester_perror( "ldap_initialize", NULL );
221 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
223 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
224 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
227 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
229 unsigned first = tester_ignore_err( rc );
233 /* only log if first occurrence */
234 if ( force < 2 || first == 1 ) {
235 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
240 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
245 ldap_unbind_ext( ld, NULL, NULL );
249 if ( rc != LDAP_SUCCESS ) {
255 fprintf( stderr, " PID=%ld - Bind done (%d).\n", (long) pid, rc );
258 if ( ldp && noinit ) {
261 } else if ( ld != NULL ) {
262 ldap_unbind_ext( ld, NULL, NULL );
270 do_base( char *uri, struct berval *base, struct berval *pass, char *pwattr,
271 int maxloop, int force, int chaserefs, int noinit, int delay )
275 pid_t pid = getpid();
276 int rc = LDAP_SUCCESS;
278 LDAPMessage *res, *msg;
280 struct berval *creds = NULL;
281 char *attrs[] = { LDAP_NO_ATTRS, NULL };
286 struct timeval beg, end;
288 int version = LDAP_VERSION3;
289 struct berval pw = { 0, NULL };
294 ldap_initialize( &ld, uri );
296 tester_perror( "ldap_initialize", NULL );
297 exit( EXIT_FAILURE );
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 );
304 rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &pw, NULL, NULL, NULL );
305 if ( rc != LDAP_SUCCESS ) {
306 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
307 exit( EXIT_FAILURE );
310 fprintf( stderr, "PID=%ld - Bind(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
311 (long) pid, maxloop, base->bv_val, filter, pwattr );
313 if ( pwattr != NULL ) {
316 rc = ldap_search_ext( ld, base->bv_val, LDAP_SCOPE_SUBTREE,
317 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
318 if ( rc != LDAP_SUCCESS ) {
319 tester_ldap_error( ld, "ldap_search_ext", NULL );
320 exit( EXIT_FAILURE );
323 while ( ( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res ) ) > 0 )
329 for ( msg = ldap_first_message( ld, res ); msg;
330 msg = ldap_next_message( ld, msg ) )
332 switch ( ldap_msgtype( msg ) ) {
333 case LDAP_RES_SEARCH_ENTRY:
334 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
335 dns = realloc( dns, (ndns + 1)*sizeof(char *) );
336 dns[ndns] = ber_strdup( bv.bv_val );
337 if ( pwattr != NULL ) {
338 struct berval **values = ldap_get_values_len( ld, msg, pwattr );
340 creds = realloc( creds, (ndns + 1)*sizeof(struct berval) );
341 if ( values == NULL ) {
343 if ( pass != NULL ) {
344 ber_dupbv( &creds[ndns], pass );
347 creds[ndns].bv_len = 0;
348 creds[ndns].bv_val = nullstr;
352 static struct berval cleartext = BER_BVC( "{CLEARTEXT} " );
353 struct berval value = *values[ 0 ];
355 if ( value.bv_val[ 0 ] == '{' ) {
356 char *end = ber_bvchr( &value, '}' );
359 if ( ber_bvcmp( &value, &cleartext ) == 0 ) {
360 value.bv_val += cleartext.bv_len;
361 value.bv_len -= cleartext.bv_len;
364 ldap_value_free_len( values );
371 ber_dupbv( &creds[ndns], &value );
372 ldap_value_free_len( values );
379 case LDAP_RES_SEARCH_RESULT:
391 beg = GetTickCount();
393 gettimeofday( &beg, NULL );
397 tester_error( "No RDNs" );
401 fprintf( stderr, " PID=%ld - got %d values.\n", (long) pid, ndns );
403 /* Ok, got list of RDNs, now start binding to each */
404 for ( i = 0; i < maxloop; i++ ) {
406 struct berval *cred = pass;
408 for ( j = 0, k = 0; k < ndns; k++) {
412 if ( creds && !BER_BVISEMPTY( &creds[j] ) ) {
415 if ( do_bind( uri, dns[j], cred, 1, force, chaserefs, noinit, &ld )
427 ldap_unbind_ext( ld, NULL, NULL );
432 end = GetTickCount();
435 fprintf( stderr, " PID=%ld - Bind done %d in %d.%03d seconds.\n",
436 (long) pid, i, end / 1000, end % 1000 );
438 gettimeofday( &end, NULL );
439 end.tv_usec -= beg.tv_usec;
440 if (end.tv_usec < 0 ) {
441 end.tv_usec += 1000000;
444 end.tv_sec -= beg.tv_sec;
446 fprintf( stderr, " PID=%ld - Bind done %d in %ld.%06ld seconds.\n",
447 (long) pid, i, (long) end.tv_sec, (long) end.tv_usec );
451 for ( i = 0; i < ndns; i++ ) {
458 for ( i = 0; i < ndns; i++ ) {
459 if ( creds[i].bv_val != nullstr ) {
460 free( creds[i].bv_val );