mirror of
https://git.openldap.org/openldap/openldap.git
synced 2025-12-21 23:29:34 -05:00
allow to reuse the same connection for multiple binds
This commit is contained in:
parent
f0d6ac3e0b
commit
61da1918a6
3 changed files with 76 additions and 37 deletions
|
|
@ -40,10 +40,10 @@
|
||||||
#define LOOPS 100
|
#define LOOPS 100
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force );
|
do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force, int noinit, LDAP **ldp );
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_base( char *uri, char *base, struct berval *pass, int maxloop, int force );
|
do_base( char *uri, struct berval *base, struct berval *pass, int maxloop, int force, int noinit );
|
||||||
|
|
||||||
/* This program can be invoked two ways: if -D is used to specify a Bind DN,
|
/* 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
|
* that DN will be used repeatedly for all of the Binds. If instead -b is used
|
||||||
|
|
@ -55,7 +55,7 @@ do_base( char *uri, char *base, struct berval *pass, int maxloop, int force );
|
||||||
static void
|
static void
|
||||||
usage( char *name )
|
usage( char *name )
|
||||||
{
|
{
|
||||||
fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>] [-F]\n",
|
fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>] [-F] [-I]\n",
|
||||||
name );
|
name );
|
||||||
exit( EXIT_FAILURE );
|
exit( EXIT_FAILURE );
|
||||||
}
|
}
|
||||||
|
|
@ -69,18 +69,19 @@ main( int argc, char **argv )
|
||||||
char *uri = NULL;
|
char *uri = NULL;
|
||||||
char *host = "localhost";
|
char *host = "localhost";
|
||||||
char *dn = NULL;
|
char *dn = NULL;
|
||||||
char *base = NULL;
|
struct berval base = { 0, NULL };
|
||||||
struct berval pass = { 0, NULL };
|
struct berval pass = { 0, NULL };
|
||||||
int port = -1;
|
int port = -1;
|
||||||
int loops = LOOPS;
|
int loops = LOOPS;
|
||||||
int force = 0;
|
int force = 0;
|
||||||
|
int noinit = 0;
|
||||||
|
|
||||||
tester_init( "slapd-bind" );
|
tester_init( "slapd-bind" );
|
||||||
|
|
||||||
while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:F" )) != EOF ) {
|
while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:FI" )) != EOF ) {
|
||||||
switch( i ) {
|
switch( i ) {
|
||||||
case 'b': /* base DN of a tree of user DNs */
|
case 'b': /* base DN of a tree of user DNs */
|
||||||
base = strdup( optarg );
|
ber_str2bv( optarg, 0, 0, &base );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'H': /* the server uri */
|
case 'H': /* the server uri */
|
||||||
|
|
@ -120,6 +121,11 @@ main( int argc, char **argv )
|
||||||
force++;
|
force++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'I':
|
||||||
|
/* reuse connection */
|
||||||
|
noinit++;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
usage( argv[0] );
|
usage( argv[0] );
|
||||||
break;
|
break;
|
||||||
|
|
@ -132,19 +138,19 @@ main( int argc, char **argv )
|
||||||
|
|
||||||
uri = tester_uri( uri, host, port );
|
uri = tester_uri( uri, host, port );
|
||||||
|
|
||||||
if ( base ) {
|
if ( base.bv_val != NULL ) {
|
||||||
do_base( uri, base, &pass, ( 20 * loops ), force );
|
do_base( uri, &base, &pass, ( 20 * loops ), force, noinit );
|
||||||
} else {
|
} else {
|
||||||
do_bind( uri, dn, &pass, ( 20 * loops ), force );
|
do_bind( uri, dn, &pass, ( 20 * loops ), force, noinit, NULL );
|
||||||
}
|
}
|
||||||
exit( EXIT_SUCCESS );
|
exit( EXIT_SUCCESS );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force )
|
do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force, int noinit, LDAP **ldp )
|
||||||
{
|
{
|
||||||
LDAP *ld = NULL;
|
LDAP *ld = ldp ? *ldp : NULL;
|
||||||
int i, first = 1, rc = -1;
|
int i, first = 1, rc = -1;
|
||||||
pid_t pid = getpid();
|
pid_t pid = getpid();
|
||||||
|
|
||||||
|
|
@ -153,6 +159,8 @@ do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force )
|
||||||
(long) pid, maxloop, dn );
|
(long) pid, maxloop, dn );
|
||||||
|
|
||||||
for ( i = 0; i < maxloop; i++ ) {
|
for ( i = 0; i < maxloop; i++ ) {
|
||||||
|
if ( !noinit || ld == NULL ) {
|
||||||
|
int version = LDAP_VERSION3;
|
||||||
ldap_initialize( &ld, uri );
|
ldap_initialize( &ld, uri );
|
||||||
if ( ld == NULL ) {
|
if ( ld == NULL ) {
|
||||||
tester_perror( "ldap_initialize" );
|
tester_perror( "ldap_initialize" );
|
||||||
|
|
@ -160,8 +168,6 @@ do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force )
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
int version = LDAP_VERSION3;
|
|
||||||
(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
|
(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
|
||||||
&version );
|
&version );
|
||||||
}
|
}
|
||||||
|
|
@ -184,21 +190,33 @@ do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force )
|
||||||
default:
|
default:
|
||||||
tester_ldap_error( ld, "ldap_sasl_bind_s" );
|
tester_ldap_error( ld, "ldap_sasl_bind_s" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !noinit ) {
|
||||||
ldap_unbind_ext( ld, NULL, NULL );
|
ldap_unbind_ext( ld, NULL, NULL );
|
||||||
|
ld = NULL;
|
||||||
|
}
|
||||||
if ( rc != LDAP_SUCCESS && !force ) {
|
if ( rc != LDAP_SUCCESS && !force ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( maxloop > 1 )
|
if ( maxloop > 1 ) {
|
||||||
fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
|
fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ldp ) {
|
||||||
|
*ldp = ld;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ldap_unbind_ext( ld, NULL, NULL );
|
||||||
|
}
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
|
do_base( char *uri, struct berval *base, struct berval *pass, int maxloop, int force, int noinit )
|
||||||
{
|
{
|
||||||
LDAP *ld = NULL;
|
LDAP *ld = NULL;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
@ -206,8 +224,8 @@ do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
|
||||||
int rc = LDAP_SUCCESS;
|
int rc = LDAP_SUCCESS;
|
||||||
ber_int_t msgid;
|
ber_int_t msgid;
|
||||||
LDAPMessage *res, *msg;
|
LDAPMessage *res, *msg;
|
||||||
char **rdns = NULL;
|
struct berval *rdns = NULL;
|
||||||
char *attrs[] = { "dn", NULL };
|
char *attrs[] = { LDAP_NO_ATTRS, NULL };
|
||||||
int nrdns = 0;
|
int nrdns = 0;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
DWORD beg, end;
|
DWORD beg, end;
|
||||||
|
|
@ -215,6 +233,7 @@ do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
|
||||||
struct timeval beg, end;
|
struct timeval beg, end;
|
||||||
#endif
|
#endif
|
||||||
int version = LDAP_VERSION3;
|
int version = LDAP_VERSION3;
|
||||||
|
struct berval pw = { 0, NULL };
|
||||||
|
|
||||||
srand(pid);
|
srand(pid);
|
||||||
|
|
||||||
|
|
@ -227,13 +246,13 @@ do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
|
||||||
(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
|
(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
|
||||||
(void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
|
(void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
|
||||||
|
|
||||||
rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL );
|
rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &pw, NULL, NULL, NULL );
|
||||||
if ( rc != LDAP_SUCCESS ) {
|
if ( rc != LDAP_SUCCESS ) {
|
||||||
tester_ldap_error( ld, "ldap_sasl_bind_s" );
|
tester_ldap_error( ld, "ldap_sasl_bind_s" );
|
||||||
exit( EXIT_FAILURE );
|
exit( EXIT_FAILURE );
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
|
rc = ldap_search_ext( ld, base->bv_val, LDAP_SCOPE_ONE,
|
||||||
filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
|
filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
|
||||||
if ( rc != LDAP_SUCCESS ) {
|
if ( rc != LDAP_SUCCESS ) {
|
||||||
tester_ldap_error( ld, "ldap_search_ext" );
|
tester_ldap_error( ld, "ldap_search_ext" );
|
||||||
|
|
@ -252,11 +271,10 @@ do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
|
||||||
case LDAP_RES_SEARCH_ENTRY:
|
case LDAP_RES_SEARCH_ENTRY:
|
||||||
rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
|
rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
|
||||||
ptr = strchr( bv.bv_val, ',');
|
ptr = strchr( bv.bv_val, ',');
|
||||||
i = ptr-bv.bv_val;
|
assert( ptr != NULL );
|
||||||
rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
|
bv.bv_len = ptr - bv.bv_val + 1;
|
||||||
rdns[nrdns] = malloc( i+1 );
|
rdns = realloc( rdns, (nrdns+1)*sizeof(struct berval));
|
||||||
strncpy(rdns[nrdns], bv.bv_val, i );
|
ber_dupbv( &rdns[nrdns], &bv );
|
||||||
rdns[nrdns][i] = '\0';
|
|
||||||
nrdns++;
|
nrdns++;
|
||||||
ber_free( ber, 0 );
|
ber_free( ber, 0 );
|
||||||
break;
|
break;
|
||||||
|
|
@ -271,6 +289,7 @@ do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
|
||||||
if ( done ) break;
|
if ( done ) break;
|
||||||
}
|
}
|
||||||
ldap_unbind_ext( ld, NULL, NULL );
|
ldap_unbind_ext( ld, NULL, NULL );
|
||||||
|
ld = NULL;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
beg = GetTickCount();
|
beg = GetTickCount();
|
||||||
|
|
@ -279,20 +298,33 @@ do_base( char *uri, char *base, struct berval *pass, int maxloop, int force )
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ( nrdns == 0 ) {
|
if ( nrdns == 0 ) {
|
||||||
fprintf( stderr, "No RDNs.\n" );
|
tester_error( "No RDNs" );
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ok, got list of RDNs, now start binding to each */
|
/* Ok, got list of RDNs, now start binding to each */
|
||||||
for (i=0; i<maxloop; i++) {
|
for (i=0; i<maxloop; i++) {
|
||||||
char dn[BUFSIZ], *ptr;
|
char dn[BUFSIZ], *ptr;
|
||||||
int j = rand() % nrdns;
|
int j, k;
|
||||||
ptr = lutil_strcopy(dn, rdns[j]);
|
|
||||||
*ptr++ = ',';
|
for ( k = 0; k < nrdns; k++) {
|
||||||
strcpy(ptr, base);
|
j = rand() % nrdns;
|
||||||
if ( do_bind( uri, dn, pass, 1, force ) && !force )
|
if ( base->bv_len + rdns[j].bv_len < sizeof( dn ) ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( k == nrdns ) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = lutil_strcopy(dn, rdns[j].bv_val);
|
||||||
|
strcpy(ptr, base->bv_val);
|
||||||
|
if ( do_bind( uri, dn, pass, 1, force, noinit, &ld ) && !force ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
end = GetTickCount();
|
end = GetTickCount();
|
||||||
end -= beg;
|
end -= beg;
|
||||||
|
|
|
||||||
|
|
@ -119,3 +119,9 @@ tester_perror( const char *fname )
|
||||||
AC_STRERROR_R( save_errno, buf, sizeof( buf ) ) );
|
AC_STRERROR_R( save_errno, buf, sizeof( buf ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
tester_error( const char *msg )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "%s: %s\n", progname, msg );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
|
|
||||||
extern void tester_init( const char *pname );
|
extern void tester_init( const char *pname );
|
||||||
extern char * tester_uri( char *uri, char *host, int port );
|
extern char * tester_uri( char *uri, char *host, int port );
|
||||||
|
extern void tester_error( const char *msg );
|
||||||
extern void tester_perror( const char *fname );
|
extern void tester_perror( const char *fname );
|
||||||
extern void tester_ldap_error( LDAP *ld, const char *fname );
|
extern void tester_ldap_error( LDAP *ld, const char *fname );
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue