Skip to content
Snippets Groups Projects
Commit 2c8790aa authored by Howard Chu's avatar Howard Chu
Browse files

Add -F force option to continue after bind failures. Use gettimeofday

for better timing resolution.
parent a88bfa6a
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,7 @@
#include <ac/string.h>
#include <ac/unistd.h>
#include <ac/wait.h>
#include <ac/time.h>
#define LDAP_DEPRECATED 1
#include <ldap.h>
......@@ -38,10 +39,12 @@
#define LOOPS 100
static int
do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop );
do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
int force );
static int
do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop );
do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
int force );
/* This program can be invoked two ways: if -D is used to specify a Bind DN,
* that DN will be used repeatedly for all of the Binds. If instead -b is used
......@@ -53,7 +56,7 @@ do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop );
static void
usage( char *name )
{
fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>]\n",
fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>] [-F]\n",
name );
exit( EXIT_FAILURE );
}
......@@ -71,8 +74,9 @@ main( int argc, char **argv )
char *pass = NULL;
int port = -1;
int loops = LOOPS;
int force = 0;
while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:" )) != EOF ) {
while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:F" )) != EOF ) {
switch( i ) {
case 'b': /* base DN of a tree of user DNs */
base = strdup( optarg );
......@@ -105,6 +109,10 @@ main( int argc, char **argv )
filter = optarg;
break;
case 'F':
force = 1;
break;
default:
usage( argv[0] );
break;
......@@ -115,15 +123,16 @@ main( int argc, char **argv )
usage( argv[0] );
if ( base )
do_base( uri, host, port, base, pass, ( 20 * loops ));
do_base( uri, host, port, base, pass, ( 20 * loops ), force );
else
do_bind( uri, host, port, dn, pass, ( 20 * loops ));
do_bind( uri, host, port, dn, pass, ( 20 * loops ), force );
exit( EXIT_SUCCESS );
}
static int
do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop )
do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
int force )
{
LDAP *ld = NULL;
int i, rc = -1;
......@@ -156,7 +165,7 @@ do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop )
ldap_perror( ld, "ldap_bind" );
}
ldap_unbind( ld );
if ( rc != LDAP_SUCCESS ) {
if ( rc != LDAP_SUCCESS && !force ) {
break;
}
}
......@@ -169,7 +178,8 @@ do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop )
static int
do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
int force )
{
LDAP *ld = NULL;
int i = 0;
......@@ -180,7 +190,7 @@ do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
char **rdns = NULL;
char *attrs[] = { "dn", NULL };
int nrdns = 0;
time_t beg, end;
struct timeval beg, end;
srand(pid);
......@@ -246,12 +256,13 @@ do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
}
ldap_unbind( ld );
gettimeofday( &beg, NULL );
if ( nrdns == 0 ) {
fprintf( stderr, "No RDNs.\n" );
return 1;
}
beg = time(0L);
/* Ok, got list of RDNs, now start binding to each */
for (i=0; i<maxloop; i++) {
char dn[BUFSIZ], *ptr;
......@@ -259,10 +270,18 @@ do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
ptr = lutil_strcopy(dn, rdns[j]);
*ptr++ = ',';
strcpy(ptr, base);
if ( do_bind( uri, host, port, dn, pass, 1 ))
if ( do_bind( uri, host, port, dn, pass, 1, force ) && !force )
break;
}
end = time(0L);
fprintf( stderr, "Done %d Binds in %d seconds.\n", i, end - beg );
gettimeofday( &end, NULL );
end.tv_usec -= beg.tv_usec;
if (end.tv_usec < 0 ) {
end.tv_usec += 1000000;
end.tv_sec -= 1;
}
end.tv_sec -= beg.tv_sec;
fprintf( stderr, "Done %d Binds in %d.%06d seconds.\n", i,
end.tv_sec, end.tv_usec );
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment