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>
38 #include "slapd-common.h"
43 do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force, int noinit, LDAP **ldp );
46 do_base( char *uri, struct berval *base, struct berval *pass, int maxloop, int force, int noinit, int delay );
48 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
49 * that DN will be used repeatedly for all of the Binds. If instead -b is used
50 * to specify a base DN, a search will be done for all "person" objects under
51 * that base DN. Then DNs from this list will be randomly selected for each
52 * Bind request. All of the users must have identical passwords. Also it is
53 * assumed that the users are all onelevel children of the base.
58 fprintf( stderr, "usage: %s "
61 "(-D <dn>|-b <baseDN> [-f <searchfilter>]) "
72 static char *filter = "(objectClass=person)";
75 main( int argc, char **argv )
79 char *host = "localhost";
81 struct berval base = { 0, NULL };
82 struct berval pass = { 0, NULL };
90 tester_init( "slapd-bind" );
92 while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:L:f:FIt:" )) != EOF ) {
94 case 'b': /* base DN of a tree of user DNs */
95 ber_str2bv( optarg, 0, 0, &base );
98 case 'H': /* the server uri */
99 uri = strdup( optarg );
102 case 'h': /* the servers host */
103 host = strdup( optarg );
106 case 'p': /* the servers port */
107 if ( lutil_atoi( &port, optarg ) != 0 ) {
113 dn = strdup( optarg );
117 pass.bv_val = strdup( optarg );
118 pass.bv_len = strlen( optarg );
121 case 'l': /* the number of loops */
122 if ( lutil_atoi( &loops, optarg ) != 0 ) {
127 case 'L': /* the number of outerloops */
128 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
142 /* reuse connection */
147 /* sleep between binds */
148 if ( lutil_atoi( &delay, optarg ) != 0 ) {
159 if ( port == -1 && uri == NULL ) {
163 uri = tester_uri( uri, host, port );
165 for ( i = 0; i < outerloops; i++ ) {
166 if ( base.bv_val != NULL ) {
167 do_base( uri, &base, &pass, loops, force, noinit, delay );
169 do_bind( uri, dn, &pass, loops, force, noinit, NULL );
173 exit( EXIT_SUCCESS );
178 do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force, int noinit, LDAP **ldp )
180 LDAP *ld = ldp ? *ldp : NULL;
181 int i, first = 1, rc = -1;
182 pid_t pid = getpid();
185 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
186 (long) pid, maxloop, dn );
188 for ( i = 0; i < maxloop; i++ ) {
189 if ( !noinit || ld == NULL ) {
190 int version = LDAP_VERSION3;
191 ldap_initialize( &ld, uri );
193 tester_perror( "ldap_initialize" );
198 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
202 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
207 case LDAP_INVALID_CREDENTIALS:
208 /* don't log: it's intended */
218 tester_ldap_error( ld, "ldap_sasl_bind_s" );
222 ldap_unbind_ext( ld, NULL, NULL );
225 if ( rc != LDAP_SUCCESS && !force ) {
231 fprintf( stderr, " PID=%ld - Bind done (%d).\n", (long) pid, rc );
237 } else if ( ld != NULL ) {
238 ldap_unbind_ext( ld, NULL, NULL );
246 do_base( char *uri, struct berval *base, struct berval *pass, int maxloop, int force, int noinit, int delay )
250 pid_t pid = getpid();
251 int rc = LDAP_SUCCESS;
253 LDAPMessage *res, *msg;
254 struct berval *rdns = NULL;
255 char *attrs[] = { LDAP_NO_ATTRS, NULL };
260 struct timeval beg, end;
262 int version = LDAP_VERSION3;
263 struct berval pw = { 0, NULL };
267 ldap_initialize( &ld, uri );
269 tester_perror( "ldap_initialize" );
270 exit( EXIT_FAILURE );
273 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
274 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
276 rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &pw, NULL, NULL, NULL );
277 if ( rc != LDAP_SUCCESS ) {
278 tester_ldap_error( ld, "ldap_sasl_bind_s" );
279 exit( EXIT_FAILURE );
282 rc = ldap_search_ext( ld, base->bv_val, LDAP_SCOPE_ONE,
283 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
284 if ( rc != LDAP_SUCCESS ) {
285 tester_ldap_error( ld, "ldap_search_ext" );
286 exit( EXIT_FAILURE );
289 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
295 for (msg = ldap_first_message( ld, res ); msg;
296 msg = ldap_next_message( ld, msg )) {
297 switch ( ldap_msgtype( msg )) {
298 case LDAP_RES_SEARCH_ENTRY:
299 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
300 ptr = strchr( bv.bv_val, ',');
301 assert( ptr != NULL );
302 bv.bv_len = ptr - bv.bv_val + 1;
303 rdns = realloc( rdns, (nrdns+1)*sizeof(struct berval));
304 ber_dupbv( &rdns[nrdns], &bv );
308 case LDAP_RES_SEARCH_RESULT:
320 beg = GetTickCount();
322 gettimeofday( &beg, NULL );
326 tester_error( "No RDNs" );
330 /* Ok, got list of RDNs, now start binding to each */
331 for ( i = 0; i < maxloop; i++ ) {
332 char dn[BUFSIZ], *ptr;
335 for ( k = 0; k < nrdns; k++) {
337 if ( base->bv_len + rdns[j].bv_len < sizeof( dn ) ) {
346 ptr = lutil_strcopy(dn, rdns[j].bv_val);
347 strcpy(ptr, base->bv_val);
348 if ( do_bind( uri, dn, pass, 1, force, noinit, &ld ) && !force ) {
358 ldap_unbind_ext( ld, NULL, NULL );
363 end = GetTickCount();
366 fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
367 end / 1000, end % 1000 );
369 gettimeofday( &end, NULL );
370 end.tv_usec -= beg.tv_usec;
371 if (end.tv_usec < 0 ) {
372 end.tv_usec += 1000000;
375 end.tv_sec -= beg.tv_sec;
377 fprintf( stderr, "Done %d Binds in %ld.%06ld seconds.\n", i,
378 (long) end.tv_sec, (long) end.tv_usec );
382 for ( i = 0; i < nrdns; i++ ) {
383 free( rdns[i].bv_val );