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 );
46 do_base( char *uri, char *base, struct berval *pass, int maxloop, int force );
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]\n",
63 static char *filter = "(objectClass=person)";
66 main( int argc, char **argv )
70 char *host = "localhost";
73 struct berval pass = { 0, NULL };
78 tester_init( "slapd-bind" );
80 while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:F" )) != EOF ) {
82 case 'b': /* base DN of a tree of user DNs */
83 base = strdup( optarg );
86 case 'H': /* the server uri */
87 uri = strdup( optarg );
90 case 'h': /* the servers host */
91 host = strdup( optarg );
94 case 'p': /* the servers port */
95 if ( lutil_atoi( &port, optarg ) != 0 ) {
101 dn = strdup( optarg );
105 pass.bv_val = strdup( optarg );
106 pass.bv_len = strlen( optarg );
109 case 'l': /* the number of loops */
110 if ( lutil_atoi( &loops, optarg ) != 0 ) {
129 if ( port == -1 && uri == NULL ) {
133 uri = tester_uri( uri, host, port );
136 do_base( uri, base, &pass, ( 20 * loops ), force );
138 do_bind( uri, dn, &pass, ( 20 * loops ), force );
140 exit( EXIT_SUCCESS );
145 do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force )
149 pid_t pid = getpid();
152 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
153 (long) pid, maxloop, dn );
155 for ( i = 0; i < maxloop; i++ ) {
156 ldap_initialize( &ld, uri );
158 tester_perror( "ldap_initialize" );
164 int version = LDAP_VERSION3;
165 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
169 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
170 if ( rc != LDAP_SUCCESS ) {
171 tester_ldap_error( ld, "ldap_sasl_bind_s" );
173 ldap_unbind_ext( ld, NULL, NULL );
174 if ( rc != LDAP_SUCCESS && !force ) {
180 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
187 do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
191 pid_t pid = getpid();
192 int rc = LDAP_SUCCESS;
194 LDAPMessage *res, *msg;
196 char *attrs[] = { "dn", NULL };
201 struct timeval beg, end;
203 int version = LDAP_VERSION3;
207 ldap_initialize( &ld, uri );
209 tester_perror( "ldap_initialize" );
210 exit( EXIT_FAILURE );
213 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
214 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
216 rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL );
217 if ( rc != LDAP_SUCCESS ) {
218 tester_ldap_error( ld, "ldap_sasl_bind_s" );
219 exit( EXIT_FAILURE );
222 rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
223 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
224 if ( rc != LDAP_SUCCESS ) {
225 tester_ldap_error( ld, "ldap_search_ext" );
226 exit( EXIT_FAILURE );
229 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
235 for (msg = ldap_first_message( ld, res ); msg;
236 msg = ldap_next_message( ld, msg )) {
237 switch ( ldap_msgtype( msg )) {
238 case LDAP_RES_SEARCH_ENTRY:
239 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
240 ptr = strchr( bv.bv_val, ',');
242 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
243 rdns[nrdns] = malloc( i+1 );
244 strncpy(rdns[nrdns], bv.bv_val, i );
245 rdns[nrdns][i] = '\0';
249 case LDAP_RES_SEARCH_RESULT:
259 ldap_unbind_ext( ld, NULL, NULL );
262 beg = GetTickCount();
264 gettimeofday( &beg, NULL );
268 fprintf( stderr, "No RDNs.\n" );
272 /* Ok, got list of RDNs, now start binding to each */
273 for (i=0; i<maxloop; i++) {
274 char dn[BUFSIZ], *ptr;
275 int j = rand() % nrdns;
276 ptr = lutil_strcopy(dn, rdns[j]);
279 if ( do_bind( uri, dn, pass, 1, force ) && !force )
283 end = GetTickCount();
286 fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
287 end / 1000, end % 1000 );
289 gettimeofday( &end, NULL );
290 end.tv_usec -= beg.tv_usec;
291 if (end.tv_usec < 0 ) {
292 end.tv_usec += 1000000;
295 end.tv_sec -= beg.tv_sec;
297 fprintf( stderr, "Done %d Binds in %ld.%06ld seconds.\n", i,
298 (long) end.tv_sec, (long) end.tv_usec );