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]] "
75 static char *filter = "(objectClass=person)";
78 main( int argc, char **argv )
82 char *host = "localhost";
84 struct berval base = { 0, NULL };
85 struct berval pass = { 0, NULL };
95 tester_init( "slapd-bind" );
97 while ( (i = getopt( argc, argv, "a:b:H:h:p:D:w:l:L:f:FIt:" )) != EOF ) {
103 case 'b': /* base DN of a tree of user DNs */
104 ber_str2bv( optarg, 0, 0, &base );
111 case 'H': /* the server uri */
115 case 'h': /* the servers host */
119 case 'p': /* the servers port */
120 if ( lutil_atoi( &port, optarg ) != 0 ) {
130 ber_str2bv( optarg, 0, 0, &pass );
133 case 'l': /* the number of loops */
134 if ( lutil_atoi( &loops, optarg ) != 0 ) {
139 case 'L': /* the number of outerloops */
140 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
154 /* reuse connection */
159 /* sleep between binds */
160 if ( lutil_atoi( &delay, optarg ) != 0 ) {
171 if ( port == -1 && uri == NULL ) {
175 uri = tester_uri( uri, host, port );
177 for ( i = 0; i < outerloops; i++ ) {
178 if ( base.bv_val != NULL ) {
179 do_base( uri, &base, &pass, pwattr, loops,
180 force, chaserefs, noinit, delay );
182 do_bind( uri, dn, &pass, loops,
183 force, chaserefs, noinit, NULL );
187 exit( EXIT_SUCCESS );
192 do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
193 int force, int chaserefs, int noinit, LDAP **ldp )
195 LDAP *ld = ldp ? *ldp : NULL;
196 int i, first = 1, rc = -1;
197 pid_t pid = getpid();
200 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
201 (long) pid, maxloop, dn );
203 for ( i = 0; i < maxloop; i++ ) {
204 if ( !noinit || ld == NULL ) {
205 int version = LDAP_VERSION3;
206 ldap_initialize( &ld, uri );
208 tester_perror( "ldap_initialize", NULL );
213 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
215 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
216 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
219 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
224 case LDAP_INVALID_CREDENTIALS:
225 /* don't log: it's intended */
235 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
239 ldap_unbind_ext( ld, NULL, NULL );
242 if ( rc != LDAP_SUCCESS && !force ) {
248 fprintf( stderr, " PID=%ld - Bind done (%d).\n", (long) pid, rc );
251 if ( ldp && noinit ) {
254 } else if ( ld != NULL ) {
255 ldap_unbind_ext( ld, NULL, NULL );
263 do_base( char *uri, struct berval *base, struct berval *pass, char *pwattr,
264 int maxloop, int force, int chaserefs, int noinit, int delay )
268 pid_t pid = getpid();
269 int rc = LDAP_SUCCESS;
271 LDAPMessage *res, *msg;
273 struct berval *creds = NULL;
274 char *attrs[] = { LDAP_NO_ATTRS, NULL };
279 struct timeval beg, end;
281 int version = LDAP_VERSION3;
282 struct berval pw = { 0, NULL };
287 ldap_initialize( &ld, uri );
289 tester_perror( "ldap_initialize", NULL );
290 exit( EXIT_FAILURE );
293 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
294 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
295 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
297 rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &pw, NULL, NULL, NULL );
298 if ( rc != LDAP_SUCCESS ) {
299 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
300 exit( EXIT_FAILURE );
303 fprintf( stderr, "PID=%ld - Bind(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
304 (long) pid, maxloop, base->bv_val, filter, pwattr );
306 if ( pwattr != NULL ) {
309 rc = ldap_search_ext( ld, base->bv_val, LDAP_SCOPE_SUBTREE,
310 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
311 if ( rc != LDAP_SUCCESS ) {
312 tester_ldap_error( ld, "ldap_search_ext", NULL );
313 exit( EXIT_FAILURE );
316 while ( ( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res ) ) > 0 )
322 for ( msg = ldap_first_message( ld, res ); msg;
323 msg = ldap_next_message( ld, msg ) )
325 switch ( ldap_msgtype( msg ) ) {
326 case LDAP_RES_SEARCH_ENTRY:
327 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
328 dns = realloc( dns, (ndns + 1)*sizeof(char *) );
329 dns[ndns] = ber_strdup( bv.bv_val );
330 if ( pwattr != NULL ) {
331 struct berval **values = ldap_get_values_len( ld, msg, pwattr );
333 creds = realloc( creds, (ndns + 1)*sizeof(struct berval) );
334 if ( values == NULL ) {
336 if ( pass != NULL ) {
337 ber_dupbv( &creds[ndns], pass );
340 creds[ndns].bv_len = 0;
341 creds[ndns].bv_val = nullstr;
345 static struct berval cleartext = BER_BVC( "{CLEARTEXT} " );
346 struct berval value = *values[ 0 ];
348 if ( value.bv_val[ 0 ] == '{' ) {
349 char *end = ber_bvchr( &value, '}' );
352 if ( ber_bvcmp( &value, &cleartext ) == 0 ) {
353 value.bv_val += cleartext.bv_len;
354 value.bv_len -= cleartext.bv_len;
357 ldap_value_free_len( values );
364 ber_dupbv( &creds[ndns], &value );
365 ldap_value_free_len( values );
372 case LDAP_RES_SEARCH_RESULT:
384 beg = GetTickCount();
386 gettimeofday( &beg, NULL );
390 tester_error( "No RDNs" );
394 fprintf( stderr, "PID=%ld - got %d values.\n", (long) pid, ndns );
396 /* Ok, got list of RDNs, now start binding to each */
397 for ( i = 0; i < maxloop; i++ ) {
399 struct berval *cred = pass;
401 for ( j = 0, k = 0; k < ndns; k++) {
405 if ( creds && !BER_BVISEMPTY( &creds[j] ) ) {
408 if ( do_bind( uri, dns[j], cred, 1, force, chaserefs, noinit, &ld )
420 ldap_unbind_ext( ld, NULL, NULL );
425 end = GetTickCount();
428 fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
429 end / 1000, end % 1000 );
431 gettimeofday( &end, NULL );
432 end.tv_usec -= beg.tv_usec;
433 if (end.tv_usec < 0 ) {
434 end.tv_usec += 1000000;
437 end.tv_sec -= beg.tv_sec;
439 fprintf( stderr, "Done %d Binds in %ld.%06ld seconds.\n", i,
440 (long) end.tv_sec, (long) end.tv_usec );
444 for ( i = 0; i < ndns; i++ ) {
451 for ( i = 0; i < ndns; i++ ) {
452 if ( creds[i].bv_val != nullstr ) {
453 free( creds[i].bv_val );