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>
34 #define LDAP_DEPRECATED 1
41 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop );
44 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop );
46 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
47 * that DN will be used repeatedly for all of the Binds. If instead -b is used
48 * to specify a base DN, a search will be done for all "person" objects under
49 * that base DN. Then DNs from this list will be randomly selected for each
50 * Bind request. All of the users must have identical passwords. Also it is
51 * assumed that the users are all onelevel children of the base.
56 fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>]\n",
61 static char *filter = "(objectClass=person)";
64 main( int argc, char **argv )
68 char *host = "localhost";
75 while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:" )) != EOF ) {
77 case 'b': /* base DN of a tree of user DNs */
78 base = strdup( optarg );
81 case 'H': /* the server uri */
82 uri = strdup( optarg );
84 case 'h': /* the servers host */
85 host = strdup( optarg );
88 case 'p': /* the servers port */
89 port = atoi( optarg );
93 dn = strdup( optarg );
97 pass = strdup( optarg );
100 case 'l': /* the number of loops */
101 loops = atoi( optarg );
114 if ( port == -1 && uri == NULL )
118 do_base( uri, host, port, base, pass, ( 20 * loops ));
120 do_bind( uri, host, port, dn, pass, ( 20 * loops ));
121 exit( EXIT_SUCCESS );
126 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop )
130 pid_t pid = getpid();
133 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
134 (long) pid, maxloop, dn );
136 for ( i = 0; i < maxloop; i++ ) {
138 ldap_initialize( &ld, uri );
140 ld = ldap_init( host, port );
143 perror( "ldap_init" );
149 int version = LDAP_VERSION3;
150 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
154 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
155 if ( rc != LDAP_SUCCESS ) {
156 ldap_perror( ld, "ldap_bind" );
159 if ( rc != LDAP_SUCCESS ) {
165 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
172 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
176 pid_t pid = getpid();
177 int rc = LDAP_SUCCESS;
179 LDAPMessage *res, *msg;
181 char *attrs[] = { "dn", NULL };
188 ldap_initialize( &ld, uri );
190 ld = ldap_init( host, port );
193 perror( "ldap_init" );
194 exit( EXIT_FAILURE );
198 int version = LDAP_VERSION3;
199 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
202 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
204 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
205 if ( rc != LDAP_SUCCESS ) {
206 ldap_perror( ld, "ldap_bind" );
207 exit( EXIT_FAILURE );
210 rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
211 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
212 if ( rc != LDAP_SUCCESS ) {
213 ldap_perror( ld, "ldap_search_ex" );
214 exit( EXIT_FAILURE );
217 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
223 for (msg = ldap_first_message( ld, res ); msg;
224 msg = ldap_next_message( ld, msg )) {
225 switch ( ldap_msgtype( msg )) {
226 case LDAP_RES_SEARCH_ENTRY:
227 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
228 ptr = strchr( bv.bv_val, ',');
230 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
231 rdns[nrdns] = malloc( i+1 );
232 strncpy(rdns[nrdns], bv.bv_val, i );
233 rdns[nrdns][i] = '\0';
237 case LDAP_RES_SEARCH_RESULT:
250 fprintf( stderr, "No RDNs.\n" );
255 /* Ok, got list of RDNs, now start binding to each */
256 for (i=0; i<maxloop; i++) {
257 char dn[BUFSIZ], *ptr;
258 int j = rand() % nrdns;
259 ptr = lutil_strcopy(dn, rdns[j]);
262 if ( do_bind( uri, host, port, dn, pass, 1 ))
266 fprintf( stderr, "Done %d Binds in %d seconds.\n", i, end - beg );