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 if ( lutil_atoi( &port, optarg ) != 0 ) {
99 dn = strdup( optarg );
103 pass = strdup( optarg );
106 case 'l': /* the number of loops */
107 if ( lutil_atoi( &loops, optarg ) != 0 ) {
126 if ( port == -1 && uri == NULL )
130 do_base( uri, host, port, base, pass, ( 20 * loops ), force );
132 do_bind( uri, host, port, dn, pass, ( 20 * loops ), force );
133 exit( EXIT_SUCCESS );
138 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
143 pid_t pid = getpid();
146 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
147 (long) pid, maxloop, dn );
149 for ( i = 0; i < maxloop; i++ ) {
151 ldap_initialize( &ld, uri );
153 ld = ldap_init( host, port );
156 perror( "ldap_init" );
162 int version = LDAP_VERSION3;
163 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
167 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
168 if ( rc != LDAP_SUCCESS ) {
169 ldap_perror( ld, "ldap_bind" );
172 if ( rc != LDAP_SUCCESS && !force ) {
178 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
185 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
190 pid_t pid = getpid();
191 int rc = LDAP_SUCCESS;
193 LDAPMessage *res, *msg;
195 char *attrs[] = { "dn", NULL };
200 struct timeval beg, end;
206 ldap_initialize( &ld, uri );
208 ld = ldap_init( host, port );
211 perror( "ldap_init" );
212 exit( EXIT_FAILURE );
216 int version = LDAP_VERSION3;
217 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
220 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
222 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
223 if ( rc != LDAP_SUCCESS ) {
224 ldap_perror( ld, "ldap_bind" );
225 exit( EXIT_FAILURE );
228 rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
229 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
230 if ( rc != LDAP_SUCCESS ) {
231 ldap_perror( ld, "ldap_search_ex" );
232 exit( EXIT_FAILURE );
235 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
241 for (msg = ldap_first_message( ld, res ); msg;
242 msg = ldap_next_message( ld, msg )) {
243 switch ( ldap_msgtype( msg )) {
244 case LDAP_RES_SEARCH_ENTRY:
245 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
246 ptr = strchr( bv.bv_val, ',');
248 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
249 rdns[nrdns] = malloc( i+1 );
250 strncpy(rdns[nrdns], bv.bv_val, i );
251 rdns[nrdns][i] = '\0';
255 case LDAP_RES_SEARCH_RESULT:
268 beg = GetTickCount();
270 gettimeofday( &beg, NULL );
274 fprintf( stderr, "No RDNs.\n" );
278 /* Ok, got list of RDNs, now start binding to each */
279 for (i=0; i<maxloop; i++) {
280 char dn[BUFSIZ], *ptr;
281 int j = rand() % nrdns;
282 ptr = lutil_strcopy(dn, rdns[j]);
285 if ( do_bind( uri, host, port, dn, pass, 1, force ) && !force )
289 end = GetTickCount();
292 fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
293 end / 1000, end % 1000 );
295 gettimeofday( &end, NULL );
296 end.tv_usec -= beg.tv_usec;
297 if (end.tv_usec < 0 ) {
298 end.tv_usec += 1000000;
301 end.tv_sec -= beg.tv_sec;
303 fprintf( stderr, "Done %d Binds in %ld.%06ld seconds.\n", i,
304 (long) end.tv_sec, (long) end.tv_usec );