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>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
33 #define LDAP_DEPRECATED 1
40 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop );
43 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop );
45 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
46 * that DN will be used repeatedly for all of the Binds. If instead -b is used
47 * to specify a base DN, a search will be done for all "person" objects under
48 * that base DN. Then DNs from this list will be randomly selected for each
49 * Bind request. All of the users must have identical passwords. Also it is
50 * assumed that the users are all onelevel children of the base.
55 fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN>) -w <passwd> [-l <loops>]\n",
60 static char *filter = "(objectClass=person)";
63 main( int argc, char **argv )
67 char *host = "localhost";
74 while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:" )) != EOF ) {
76 case 'b': /* base DN of a tree of user DNs */
77 base = strdup( optarg );
80 case 'H': /* the server uri */
81 uri = strdup( optarg );
83 case 'h': /* the servers host */
84 host = strdup( optarg );
87 case 'p': /* the servers port */
88 port = atoi( optarg );
92 dn = strdup( optarg );
96 pass = strdup( optarg );
99 case 'l': /* the number of loops */
100 loops = atoi( optarg );
109 if ( port == -1 && uri == NULL )
113 do_base( uri, host, port, base, pass, ( 20 * loops ));
115 do_bind( uri, host, port, dn, pass, ( 20 * loops ));
116 exit( EXIT_SUCCESS );
121 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop )
125 char *attrs[] = { "1.1", NULL };
126 pid_t pid = getpid();
129 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
130 (long) pid, maxloop, dn );
132 for ( i = 0; i < maxloop; i++ ) {
136 ldap_initialize( &ld, uri );
138 ld = ldap_init( host, port );
141 perror( "ldap_init" );
147 int version = LDAP_VERSION3;
148 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
152 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
153 if ( rc != LDAP_SUCCESS ) {
154 ldap_perror( ld, "ldap_bind" );
157 if ( rc != LDAP_SUCCESS ) {
163 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
170 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
174 pid_t pid = getpid();
175 int rc = LDAP_SUCCESS;
177 LDAPMessage *res, *msg;
179 char *attrs[] = { "dn", NULL };
186 ldap_initialize( &ld, uri );
188 ld = ldap_init( host, port );
191 perror( "ldap_init" );
192 exit( EXIT_FAILURE );
196 int version = LDAP_VERSION3;
197 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
200 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
202 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
203 if ( rc != LDAP_SUCCESS ) {
204 ldap_perror( ld, "ldap_bind" );
205 exit( EXIT_FAILURE );
208 rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
209 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
210 if ( rc != LDAP_SUCCESS ) {
211 ldap_perror( ld, "ldap_search_ex" );
212 exit( EXIT_FAILURE );
215 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
221 for (msg = ldap_first_message( ld, res ); msg;
222 msg = ldap_next_message( ld, msg )) {
223 switch ( ldap_msgtype( msg )) {
224 case LDAP_RES_SEARCH_ENTRY:
225 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
226 ptr = strchr( bv.bv_val, ',');
228 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
229 rdns[nrdns] = malloc( i+1 );
230 strncpy(rdns[nrdns], bv.bv_val, i );
231 rdns[nrdns][i] = '\0';
235 case LDAP_RES_SEARCH_RESULT:
248 /* Ok, got list of RDNs, now start binding to each */
249 for (i=0; i<maxloop; i++) {
250 char dn[BUFSIZ], *ptr;
251 int j = rand() % nrdns;
252 ptr = lutil_strcopy(dn, rdns[j]);
255 if ( do_bind( uri, host, port, dn, pass, 1 ))
259 fprintf( stderr, "Done %d Binds in %d seconds.\n", i, end - beg );