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 );
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 [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>] [-F] [-I]\n",
63 static char *filter = "(objectClass=person)";
66 main( int argc, char **argv )
70 char *host = "localhost";
72 struct berval base = { 0, NULL };
73 struct berval pass = { 0, NULL };
79 tester_init( "slapd-bind" );
81 while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:FI" )) != EOF ) {
83 case 'b': /* base DN of a tree of user DNs */
84 ber_str2bv( optarg, 0, 0, &base );
87 case 'H': /* the server uri */
88 uri = strdup( optarg );
91 case 'h': /* the servers host */
92 host = strdup( optarg );
95 case 'p': /* the servers port */
96 if ( lutil_atoi( &port, optarg ) != 0 ) {
102 dn = strdup( optarg );
106 pass.bv_val = strdup( optarg );
107 pass.bv_len = strlen( optarg );
110 case 'l': /* the number of loops */
111 if ( lutil_atoi( &loops, optarg ) != 0 ) {
125 /* reuse connection */
135 if ( port == -1 && uri == NULL ) {
139 uri = tester_uri( uri, host, port );
141 if ( base.bv_val != NULL ) {
142 do_base( uri, &base, &pass, ( 20 * loops ), force, noinit );
144 do_bind( uri, dn, &pass, ( 20 * loops ), force, noinit, NULL );
146 exit( EXIT_SUCCESS );
151 do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force, int noinit, LDAP **ldp )
153 LDAP *ld = ldp ? *ldp : NULL;
154 int i, first = 1, rc = -1;
155 pid_t pid = getpid();
158 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
159 (long) pid, maxloop, dn );
161 for ( i = 0; i < maxloop; i++ ) {
162 if ( !noinit || ld == NULL ) {
163 int version = LDAP_VERSION3;
164 ldap_initialize( &ld, uri );
166 tester_perror( "ldap_initialize" );
171 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
175 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
180 case LDAP_INVALID_CREDENTIALS:
181 /* don't log: it's intended */
191 tester_ldap_error( ld, "ldap_sasl_bind_s" );
195 ldap_unbind_ext( ld, NULL, NULL );
198 if ( rc != LDAP_SUCCESS && !force ) {
204 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
211 ldap_unbind_ext( ld, NULL, NULL );
219 do_base( char *uri, struct berval *base, struct berval *pass, int maxloop, int force, int noinit )
223 pid_t pid = getpid();
224 int rc = LDAP_SUCCESS;
226 LDAPMessage *res, *msg;
227 struct berval *rdns = NULL;
228 char *attrs[] = { LDAP_NO_ATTRS, NULL };
233 struct timeval beg, end;
235 int version = LDAP_VERSION3;
236 struct berval pw = { 0, NULL };
240 ldap_initialize( &ld, uri );
242 tester_perror( "ldap_initialize" );
243 exit( EXIT_FAILURE );
246 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
247 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
249 rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &pw, NULL, NULL, NULL );
250 if ( rc != LDAP_SUCCESS ) {
251 tester_ldap_error( ld, "ldap_sasl_bind_s" );
252 exit( EXIT_FAILURE );
255 rc = ldap_search_ext( ld, base->bv_val, LDAP_SCOPE_ONE,
256 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
257 if ( rc != LDAP_SUCCESS ) {
258 tester_ldap_error( ld, "ldap_search_ext" );
259 exit( EXIT_FAILURE );
262 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
268 for (msg = ldap_first_message( ld, res ); msg;
269 msg = ldap_next_message( ld, msg )) {
270 switch ( ldap_msgtype( msg )) {
271 case LDAP_RES_SEARCH_ENTRY:
272 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
273 ptr = strchr( bv.bv_val, ',');
274 assert( ptr != NULL );
275 bv.bv_len = ptr - bv.bv_val + 1;
276 rdns = realloc( rdns, (nrdns+1)*sizeof(struct berval));
277 ber_dupbv( &rdns[nrdns], &bv );
281 case LDAP_RES_SEARCH_RESULT:
291 ldap_unbind_ext( ld, NULL, NULL );
295 beg = GetTickCount();
297 gettimeofday( &beg, NULL );
301 tester_error( "No RDNs" );
305 /* Ok, got list of RDNs, now start binding to each */
306 for (i=0; i<maxloop; i++) {
307 char dn[BUFSIZ], *ptr;
310 for ( k = 0; k < nrdns; k++) {
312 if ( base->bv_len + rdns[j].bv_len < sizeof( dn ) ) {
321 ptr = lutil_strcopy(dn, rdns[j].bv_val);
322 strcpy(ptr, base->bv_val);
323 if ( do_bind( uri, dn, pass, 1, force, noinit, &ld ) && !force ) {
329 end = GetTickCount();
332 fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
333 end / 1000, end % 1000 );
335 gettimeofday( &end, NULL );
336 end.tv_usec -= beg.tv_usec;
337 if (end.tv_usec < 0 ) {
338 end.tv_usec += 1000000;
341 end.tv_sec -= beg.tv_sec;
343 fprintf( stderr, "Done %d Binds in %ld.%06ld seconds.\n", i,
344 (long) end.tv_sec, (long) end.tv_usec );