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 Kurt Spanier 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 );
48 fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN>) -w <passwd> [-l <loops>]\n",
53 static char *filter = "(objectClass=person)";
56 main( int argc, char **argv )
60 char *host = "localhost";
67 while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:" )) != EOF ) {
69 case 'b': /* base DN of a tree of user DNs */
70 base = strdup( optarg );
73 case 'H': /* the server uri */
74 uri = strdup( optarg );
76 case 'h': /* the servers host */
77 host = strdup( optarg );
80 case 'p': /* the servers port */
81 port = atoi( optarg );
85 dn = strdup( optarg );
89 pass = strdup( optarg );
92 case 'l': /* the number of loops */
93 loops = atoi( optarg );
102 if ( port == -1 && uri == NULL )
106 do_base( uri, host, port, base, pass, ( 20 * loops ));
108 do_bind( uri, host, port, dn, pass, ( 20 * loops ));
109 exit( EXIT_SUCCESS );
114 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop )
118 char *attrs[] = { "1.1", NULL };
119 pid_t pid = getpid();
122 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
123 (long) pid, maxloop, dn );
125 for ( i = 0; i < maxloop; i++ ) {
130 ldap_initialize( &ld, uri );
132 ld = ldap_init( host, port );
135 perror( "ldap_init" );
140 int version = LDAP_VERSION3;
141 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
145 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
146 if ( rc != LDAP_SUCCESS ) {
147 ldap_perror( ld, "ldap_bind" );
150 if ( rc != LDAP_SUCCESS ) {
156 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
162 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
166 pid_t pid = getpid();
167 int rc = LDAP_SUCCESS;
169 LDAPMessage *res, *msg;
171 char *attrs[] = { "dn", NULL };
177 ldap_initialize( &ld, uri );
179 ld = ldap_init( host, port );
182 perror( "ldap_init" );
183 exit( EXIT_FAILURE );
187 int version = LDAP_VERSION3;
188 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
191 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
193 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
194 if ( rc != LDAP_SUCCESS ) {
195 ldap_perror( ld, "ldap_bind" );
196 exit( EXIT_FAILURE );
199 rc = ldap_search_ext( ld, base, LDAP_SCOPE_SUBTREE,
200 filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
201 if ( rc != LDAP_SUCCESS ) {
202 ldap_perror( ld, "ldap_search_ex" );
203 exit( EXIT_FAILURE );
206 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
212 for (msg = ldap_first_message( ld, res ); msg;
213 msg = ldap_next_message( ld, msg )) {
214 switch ( ldap_msgtype( msg )) {
215 case LDAP_RES_SEARCH_ENTRY:
216 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
217 ptr = strchr( bv.bv_val, ',');
219 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
220 rdns[nrdns] = malloc( i+1 );
221 strncpy(rdns[nrdns], bv.bv_val, i );
222 rdns[nrdns][i] = '\0';
226 case LDAP_RES_SEARCH_RESULT:
238 /* Ok, got list of RDNs, now start binding to each */
239 for (i=0; i<maxloop; i++) {
240 char dn[BUFSIZ], *ptr;
241 int j = rand() % nrdns;
242 ptr = lutil_strcopy(dn, rdns[j]);
245 do_bind( uri, host, port, dn, pass, 1 );