2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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>
35 #define LDAP_DEPRECATED 1
42 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
46 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
49 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
50 * that DN will be used repeatedly for all of the Binds. If instead -b is used
51 * to specify a base DN, a search will be done for all "person" objects under
52 * that base DN. Then DNs from this list will be randomly selected for each
53 * Bind request. All of the users must have identical passwords. Also it is
54 * assumed that the users are all onelevel children of the base.
59 fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>] [-F]\n",
64 static char *filter = "(objectClass=person)";
67 main( int argc, char **argv )
71 char *host = "localhost";
79 while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:F" )) != EOF ) {
81 case 'b': /* base DN of a tree of user DNs */
82 base = strdup( optarg );
85 case 'H': /* the server uri */
86 uri = strdup( optarg );
88 case 'h': /* the servers host */
89 host = strdup( optarg );
92 case 'p': /* the servers port */
93 port = atoi( optarg );
97 dn = strdup( optarg );
101 pass = strdup( optarg );
104 case 'l': /* the number of loops */
105 loops = atoi( optarg );
122 if ( port == -1 && uri == NULL )
126 do_base( uri, host, port, base, pass, ( 20 * loops ), force );
128 do_bind( uri, host, port, dn, pass, ( 20 * loops ), force );
129 exit( EXIT_SUCCESS );
134 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
139 pid_t pid = getpid();
142 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
143 (long) pid, maxloop, dn );
145 for ( i = 0; i < maxloop; i++ ) {
147 ldap_initialize( &ld, uri );
149 ld = ldap_init( host, port );
152 perror( "ldap_init" );
158 int version = LDAP_VERSION3;
159 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
163 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
164 if ( rc != LDAP_SUCCESS ) {
165 ldap_perror( ld, "ldap_bind" );
168 if ( rc != LDAP_SUCCESS && !force ) {
174 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
181 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
186 pid_t pid = getpid();
187 int rc = LDAP_SUCCESS;
189 LDAPMessage *res, *msg;
191 char *attrs[] = { "dn", NULL };
196 struct timeval beg, end;
202 ldap_initialize( &ld, uri );
204 ld = ldap_init( host, port );
207 perror( "ldap_init" );
208 exit( EXIT_FAILURE );
212 int version = LDAP_VERSION3;
213 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
216 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
218 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
219 if ( rc != LDAP_SUCCESS ) {
220 ldap_perror( ld, "ldap_bind" );
221 exit( EXIT_FAILURE );
224 rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
225 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
226 if ( rc != LDAP_SUCCESS ) {
227 ldap_perror( ld, "ldap_search_ex" );
228 exit( EXIT_FAILURE );
231 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
237 for (msg = ldap_first_message( ld, res ); msg;
238 msg = ldap_next_message( ld, msg )) {
239 switch ( ldap_msgtype( msg )) {
240 case LDAP_RES_SEARCH_ENTRY:
241 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
242 ptr = strchr( bv.bv_val, ',');
244 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
245 rdns[nrdns] = malloc( i+1 );
246 strncpy(rdns[nrdns], bv.bv_val, i );
247 rdns[nrdns][i] = '\0';
251 case LDAP_RES_SEARCH_RESULT:
264 beg = GetTickCount();
266 gettimeofday( &beg, NULL );
270 fprintf( stderr, "No RDNs.\n" );
274 /* Ok, got list of RDNs, now start binding to each */
275 for (i=0; i<maxloop; i++) {
276 char dn[BUFSIZ], *ptr;
277 int j = rand() % nrdns;
278 ptr = lutil_strcopy(dn, rdns[j]);
281 if ( do_bind( uri, host, port, dn, pass, 1, force ) && !force )
285 end = GetTickCount();
288 fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
289 end / 1000, end % 1000 );
291 gettimeofday( &end, NULL );
292 end.tv_usec -= beg.tv_usec;
293 if (end.tv_usec < 0 ) {
294 end.tv_usec += 1000000;
297 end.tv_sec -= beg.tv_sec;
299 fprintf( stderr, "Done %d Binds in %d.%06d seconds.\n", i,
300 end.tv_sec, end.tv_usec );