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, char *dn, struct berval *pass, char *base, char *filter, 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]] "
77 main( int argc, char **argv )
81 char *host = "localhost";
84 char *filter = "(objectClass=person)";
85 struct berval pass = { 0, NULL };
95 tester_init( "slapd-bind", TESTER_BIND );
97 /* by default, tolerate invalid credentials */
98 tester_ignore_str2errlist( "INVALID_CREDENTIALS" );
100 while ( (i = getopt( argc, argv, "a:b:H:h:i:p:D:w:l:L:f:FIt:" )) != EOF ) {
106 case 'b': /* base DN of a tree of user DNs */
114 case 'H': /* the server uri */
118 case 'h': /* the servers host */
123 tester_ignore_str2errlist( optarg );
126 case 'p': /* the servers port */
127 if ( lutil_atoi( &port, optarg ) != 0 ) {
137 ber_str2bv( optarg, 0, 0, &pass );
138 memset( optarg, '*', pass.bv_len );
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 != NULL ) {
187 do_base( uri, dn, &pass, base, filter, 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, char *dn, struct berval *pass, char *base, char *filter, 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;
293 ldap_initialize( &ld, uri );
295 tester_perror( "ldap_initialize", NULL );
296 exit( EXIT_FAILURE );
299 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
300 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
301 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
303 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
304 if ( rc != LDAP_SUCCESS ) {
305 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
306 exit( EXIT_FAILURE );
309 fprintf( stderr, "PID=%ld - Bind(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
310 (long) pid, maxloop, base, filter, pwattr );
312 if ( pwattr != NULL ) {
315 rc = ldap_search_ext( ld, base, LDAP_SCOPE_SUBTREE,
316 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
317 if ( rc != LDAP_SUCCESS ) {
318 tester_ldap_error( ld, "ldap_search_ext", NULL );
319 exit( EXIT_FAILURE );
322 while ( ( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res ) ) > 0 )
328 for ( msg = ldap_first_message( ld, res ); msg;
329 msg = ldap_next_message( ld, msg ) )
331 switch ( ldap_msgtype( msg ) ) {
332 case LDAP_RES_SEARCH_ENTRY:
333 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
334 dns = realloc( dns, (ndns + 1)*sizeof(char *) );
335 dns[ndns] = ber_strdup( bv.bv_val );
336 if ( pwattr != NULL ) {
337 struct berval **values = ldap_get_values_len( ld, msg, pwattr );
339 creds = realloc( creds, (ndns + 1)*sizeof(struct berval) );
340 if ( values == NULL ) {
342 creds[ndns].bv_len = 0;
343 creds[ndns].bv_val = nullstr;
346 static struct berval cleartext = BER_BVC( "{CLEARTEXT} " );
347 struct berval value = *values[ 0 ];
349 if ( value.bv_val[ 0 ] == '{' ) {
350 char *end = ber_bvchr( &value, '}' );
353 if ( ber_bvcmp( &value, &cleartext ) == 0 ) {
354 value.bv_val += cleartext.bv_len;
355 value.bv_len -= cleartext.bv_len;
358 ldap_value_free_len( values );
365 ber_dupbv( &creds[ndns], &value );
366 ldap_value_free_len( values );
373 case LDAP_RES_SEARCH_RESULT:
385 beg = GetTickCount();
387 gettimeofday( &beg, NULL );
391 tester_error( "No DNs" );
395 fprintf( stderr, " PID=%ld - Bind base=\"%s\" filter=\"%s\" got %d values.\n",
396 (long) pid, base, filter, ndns );
398 /* Ok, got list of DNs, now start binding to each */
399 for ( i = 0; i < maxloop; i++ ) {
401 struct berval cred = { 0, NULL };
403 for ( j = 0, k = 0; k < ndns; k++) {
407 if ( creds && !BER_BVISEMPTY( &creds[j] ) ) {
410 if ( do_bind( uri, dns[j], &cred, 1, force, chaserefs, noinit, &ld )
422 ldap_unbind_ext( ld, NULL, NULL );
427 end = GetTickCount();
430 fprintf( stderr, " PID=%ld - Bind done %d in %d.%03d seconds.\n",
431 (long) pid, i, end / 1000, end % 1000 );
433 gettimeofday( &end, NULL );
434 end.tv_usec -= beg.tv_usec;
435 if (end.tv_usec < 0 ) {
436 end.tv_usec += 1000000;
439 end.tv_sec -= beg.tv_sec;
441 fprintf( stderr, " PID=%ld - Bind done %d in %ld.%06ld seconds.\n",
442 (long) pid, i, (long) end.tv_sec, (long) end.tv_usec );
446 for ( i = 0; i < ndns; i++ ) {
453 for ( i = 0; i < ndns; i++ ) {
454 if ( creds[i].bv_val != nullstr ) {
455 free( creds[i].bv_val );