static void do_conn( char *uri, char *manager, struct berval *passwd, LDAP **ldp, int nobind, int maxretries, int conn_num ) { LDAP *ld = NULL; int version = LDAP_VERSION3; int i = 0, do_retry = maxretries; int rc = LDAP_SUCCESS; char thrstr[BUFSIZ]; retry:; ldap_initialize( &ld, uri ); if ( ld == NULL ) { snprintf( thrstr, BUFSIZ, "connection: %d", conn_num ); tester_error( thrstr ); tester_perror( "ldap_initialize", NULL ); exit( EXIT_FAILURE ); } (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF ); if ( do_retry == maxretries ) { snprintf( thrstr, BUFSIZ, "do_conn #%d\n", conn_num ); thread_verbose( -1, thrstr ); } if ( nobind == 0 ) { rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { snprintf( thrstr, BUFSIZ, "connection: %d", conn_num ); tester_error( thrstr ); tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); switch ( rc ) { case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { ldap_unbind_ext( ld, NULL, NULL ); ld = NULL; do_retry--; if ( delay != 0 ) { sleep( delay ); } goto retry; } /* fallthru */ default: break; } exit( EXIT_FAILURE ); } } *ldp = ld; }
static void thread_output(int idx, char *string) { char thrstr[BUFSIZ]; snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string); tester_error( thrstr ); }
static void thread_error(int idx, char *string) { char thrstr[BUFSIZ]; snprintf(thrstr, BUFSIZ, "error on tidx: %d: %s", idx, string); tester_error( thrstr ); }
static void thread_verbose(int idx, char *string) { char thrstr[BUFSIZ]; if (!verbose) return; snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string); tester_error( thrstr ); }
void tester_config_finish( struct tester_conn_args *config ) { if ( !config->uri ) { static char uribuf[ BUFSIZ ]; config->uri = uribuf; snprintf( uribuf, sizeof( uribuf ), "ldap://%s:%d", config->host, config->port ); } if ( config->authmethod == -1 ) { #ifdef HAVE_CYRUS_SASL if ( config->binddn != NULL ) { config->authmethod = LDAP_AUTH_SIMPLE; } else { config->authmethod = LDAP_AUTH_SASL; } #else config->authmethod = LDAP_AUTH_SIMPLE; #endif } #ifdef HAVE_CYRUS_SASL if ( config->authmethod == LDAP_AUTH_SASL ) { config->defaults = lutil_sasl_defaults( NULL, config->mech, config->realm, config->authc_id, config->pass.bv_val, config->authz_id ); if ( config->defaults == NULL ) { tester_error( "unable to prepare SASL defaults" ); exit( EXIT_FAILURE ); } } #endif }
static int do_base( char *uri, char *dn, struct berval *pass, char *base, char *filter, char *pwattr, int maxloop, int force, int chaserefs, int noinit, int delay, int action_type, void *action ) { LDAP *ld = NULL; int i = 0; int rc = LDAP_SUCCESS; ber_int_t msgid; LDAPMessage *res, *msg; char **dns = NULL; struct berval *creds = NULL; char *attrs[] = { LDAP_NO_ATTRS, NULL }; int ndns = 0; #ifdef _WIN32 DWORD beg, end; #else struct timeval beg, end; #endif int version = LDAP_VERSION3; char *nullstr = ""; ldap_initialize( &ld, uri ); if ( ld == NULL ) { tester_perror( "ldap_initialize", NULL ); exit( EXIT_FAILURE ); } (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF ); rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); exit( EXIT_FAILURE ); } fprintf( stderr, "PID=%ld - Bind(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n", (long) pid, maxloop, base, filter, pwattr ); if ( pwattr != NULL ) { attrs[ 0 ] = pwattr; } rc = ldap_search_ext( ld, base, LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL, 0, 0, &msgid ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_search_ext", NULL ); exit( EXIT_FAILURE ); } while ( ( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res ) ) > 0 ) { BerElement *ber; struct berval bv; int done = 0; for ( msg = ldap_first_message( ld, res ); msg; msg = ldap_next_message( ld, msg ) ) { switch ( ldap_msgtype( msg ) ) { case LDAP_RES_SEARCH_ENTRY: rc = ldap_get_dn_ber( ld, msg, &ber, &bv ); dns = realloc( dns, (ndns + 1)*sizeof(char *) ); dns[ndns] = ber_strdup( bv.bv_val ); if ( pwattr != NULL ) { struct berval **values = ldap_get_values_len( ld, msg, pwattr ); creds = realloc( creds, (ndns + 1)*sizeof(struct berval) ); if ( values == NULL ) { novals:; creds[ndns].bv_len = 0; creds[ndns].bv_val = nullstr; } else { static struct berval cleartext = BER_BVC( "{CLEARTEXT} " ); struct berval value = *values[ 0 ]; if ( value.bv_val[ 0 ] == '{' ) { char *end = ber_bvchr( &value, '}' ); if ( end ) { if ( ber_bvcmp( &value, &cleartext ) == 0 ) { value.bv_val += cleartext.bv_len; value.bv_len -= cleartext.bv_len; } else { ldap_value_free_len( values ); goto novals; } } } ber_dupbv( &creds[ndns], &value ); ldap_value_free_len( values ); } } ndns++; ber_free( ber, 0 ); break; case LDAP_RES_SEARCH_RESULT: done = 1; break; } if ( done ) break; } ldap_msgfree( res ); if ( done ) break; } #ifdef _WIN32 beg = GetTickCount(); #else gettimeofday( &beg, NULL ); #endif if ( ndns == 0 ) { tester_error( "No DNs" ); return 1; } fprintf( stderr, " PID=%ld - Bind base=\"%s\" filter=\"%s\" got %d values.\n", (long) pid, base, filter, ndns ); /* Ok, got list of DNs, now start binding to each */ for ( i = 0; i < maxloop; i++ ) { int j; struct berval cred = { 0, NULL }; #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */ j = rand() % ndns; #endif j = ((double)ndns)*rand()/(RAND_MAX + 1.0); if ( creds && !BER_BVISEMPTY( &creds[j] ) ) { cred = creds[j]; } if ( do_bind( uri, dns[j], &cred, 1, force, chaserefs, noinit, &ld, action_type, action ) && !force ) { break; } if ( delay ) { sleep( delay ); } } if ( ld != NULL ) { ldap_unbind_ext( ld, NULL, NULL ); ld = NULL; } #ifdef _WIN32 end = GetTickCount(); end -= beg; fprintf( stderr, " PID=%ld - Bind done %d in %d.%03d seconds.\n", (long) pid, i, end / 1000, end % 1000 ); #else 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, " PID=%ld - Bind done %d in %ld.%06ld seconds.\n", (long) pid, i, (long) end.tv_sec, (long) end.tv_usec ); #endif if ( dns ) { for ( i = 0; i < ndns; i++ ) { ber_memfree( dns[i] ); } free( dns ); } if ( creds ) { for ( i = 0; i < ndns; i++ ) { if ( creds[i].bv_val != nullstr ) { ber_memfree( creds[i].bv_val ); } } free( creds ); } return 0; }
static int do_bind( char *uri, char *dn, struct berval *pass, int maxloop, int force, int chaserefs, int noinit, LDAP **ldp, int action_type, void *action ) { LDAP *ld = ldp ? *ldp : NULL; int i, rc = -1; /* for internal search */ int timelimit = 0; int sizelimit = 0; switch ( action_type ) { case -1: break; case TESTER_SEARCH: { LDAPURLDesc *ludp = (LDAPURLDesc *)action; assert( action != NULL ); if ( ludp->lud_exts != NULL ) { for ( i = 0; ludp->lud_exts[ i ] != NULL; i++ ) { char *ext = ludp->lud_exts[ i ]; int crit = 0; if (ext[0] == '!') { crit++; ext++; } if ( strncasecmp( ext, "x-timelimit=", STRLENOF( "x-timelimit=" ) ) == 0 ) { if ( lutil_atoi( &timelimit, &ext[ STRLENOF( "x-timelimit=" ) ] ) && crit ) { tester_error( "unable to parse critical extension x-timelimit" ); } } else if ( strncasecmp( ext, "x-sizelimit=", STRLENOF( "x-sizelimit=" ) ) == 0 ) { if ( lutil_atoi( &sizelimit, &ext[ STRLENOF( "x-sizelimit=" ) ] ) && crit ) { tester_error( "unable to parse critical extension x-sizelimit" ); } } else if ( crit ) { tester_error( "unknown critical extension" ); } } } } break; default: /* nothing to do yet */ break; } if ( maxloop > 1 ) { fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n", (long) pid, maxloop, dn ); } for ( i = 0; i < maxloop; i++ ) { if ( !noinit || ld == NULL ) { int version = LDAP_VERSION3; ldap_initialize( &ld, uri ); if ( ld == NULL ) { tester_perror( "ldap_initialize", NULL ); rc = -1; break; } (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF ); } rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL ); if ( rc ) { int first = tester_ignore_err( rc ); /* if ignore.. */ if ( first ) { /* only log if first occurrence */ if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); } rc = LDAP_SUCCESS; } else { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); } } switch ( action_type ) { case -1: break; case TESTER_SEARCH: { LDAPURLDesc *ludp = (LDAPURLDesc *)action; LDAPMessage *res = NULL; struct timeval tv = { 0 }, *tvp = NULL; if ( timelimit ) { tv.tv_sec = timelimit; tvp = &tv; } assert( action != NULL ); rc = ldap_search_ext_s( ld, ludp->lud_dn, ludp->lud_scope, ludp->lud_filter, ludp->lud_attrs, 0, NULL, NULL, tvp, sizelimit, &res ); ldap_msgfree( res ); } break; default: /* nothing to do yet */ break; } if ( !noinit ) { ldap_unbind_ext( ld, NULL, NULL ); ld = NULL; } if ( rc != LDAP_SUCCESS ) { break; } } if ( maxloop > 1 ) { fprintf( stderr, " PID=%ld - Bind done (%d).\n", (long) pid, rc ); } if ( ldp && noinit ) { *ldp = ld; } else if ( ld != NULL ) { ldap_unbind_ext( ld, NULL, NULL ); } return rc; }
int main( int argc, char **argv ) { int i; char *uri = NULL; char *host = "localhost"; int port = -1; char *manager = NULL; struct berval passwd = { 0, NULL }; char outstr[BUFSIZ]; int ptpass; int testfail = 0; tester_init( "slapd-mtread", TESTER_READ ); /* by default, tolerate referrals and no such object */ tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" ); while ( (i = getopt( argc, argv, "ACc:D:e:Ff:H:h:i:L:l:M:m:p:r:t:T:w:v" )) != EOF ) { switch ( i ) { case 'A': noattrs++; break; case 'C': chaserefs++; break; case 'H': /* the server uri */ uri = strdup( optarg ); break; case 'h': /* the servers host */ host = strdup( optarg ); break; case 'i': tester_ignore_str2errlist( optarg ); break; case 'N': nobind++; break; case 'v': verbose++; break; case 'p': /* the servers port */ if ( lutil_atoi( &port, optarg ) != 0 ) { usage( argv[0] ); } break; case 'D': /* the servers manager */ manager = strdup( optarg ); break; case 'w': /* the server managers password */ passwd.bv_val = strdup( optarg ); passwd.bv_len = strlen( optarg ); memset( optarg, '*', passwd.bv_len ); break; case 'c': /* the number of connections */ if ( lutil_atoi( &noconns, optarg ) != 0 ) { usage( argv[0] ); } break; case 'e': /* DN to search for */ entry = strdup( optarg ); break; case 'f': /* the search request */ filter = strdup( optarg ); break; case 'F': force++; break; case 'l': /* the number of loops */ if ( lutil_atoi( &loops, optarg ) != 0 ) { usage( argv[0] ); } break; case 'L': /* the number of outerloops */ if ( lutil_atoi( &outerloops, optarg ) != 0 ) { usage( argv[0] ); } break; case 'M': /* the number of R/W threads */ if ( lutil_atoi( &rwthreads, optarg ) != 0 ) { usage( argv[0] ); } if (rwthreads > MAX_THREAD) rwthreads = MAX_THREAD; break; case 'm': /* the number of threads */ if ( lutil_atoi( &threads, optarg ) != 0 ) { usage( argv[0] ); } if (threads > MAX_THREAD) threads = MAX_THREAD; break; case 'r': /* the number of retries */ if ( lutil_atoi( &retries, optarg ) != 0 ) { usage( argv[0] ); } break; case 't': /* delay in seconds */ if ( lutil_atoi( &delay, optarg ) != 0 ) { usage( argv[0] ); } break; case 'T': attrs = ldap_str2charray( optarg, "," ); if ( attrs == NULL ) { usage( argv[0] ); } break; default: usage( argv[0] ); break; } } if (( entry == NULL ) || ( port == -1 && uri == NULL )) usage( argv[0] ); if ( *entry == '\0' ) { fprintf( stderr, "%s: invalid EMPTY entry DN.\n", argv[0] ); exit( EXIT_FAILURE ); } if ( argv[optind] != NULL ) { attrs = &argv[optind]; } if (noconns < 1) noconns = 1; if (noconns > MAXCONN) noconns = MAXCONN; lds = (LDAP **) calloc( sizeof(LDAP *), noconns); if (lds == NULL) { fprintf( stderr, "%s: Memory error: calloc noconns.\n", argv[0] ); exit( EXIT_FAILURE ); } uri = tester_uri( uri, host, port ); /* One connection and one connection only */ do_conn( uri, manager, &passwd, &ld, nobind, retries, 0 ); lds[0] = ld; for(i = 1; i < noconns; i++) { do_conn( uri, manager, &passwd, &lds[i], nobind, retries, i ); } ldap_pvt_thread_initialize(); snprintf(outstr, BUFSIZ, "MT Test Start: conns: %d (%s)", noconns, uri); tester_error(outstr); snprintf(outstr, BUFSIZ, "Threads: RO: %d RW: %d", threads, rwthreads); tester_error(outstr); /* Set up read only threads */ for ( i = 0; i < threads; i++ ) { ldap_pvt_thread_create( &rtid[i], 0, do_onethread, &rtid[i]); snprintf(outstr, BUFSIZ, "Created RO thread %d", i); thread_verbose(-1, outstr); } /* Set up read/write threads */ for ( i = 0; i < rwthreads; i++ ) { ldap_pvt_thread_create( &rwtid[i], 0, do_onerwthread, &rwtid[i]); snprintf(outstr, BUFSIZ, "Created RW thread %d", i + MAX_THREAD); thread_verbose(-1, outstr); } ptpass = outerloops * loops; /* wait for read only threads to complete */ for ( i = 0; i < threads; i++ ) ldap_pvt_thread_join(rtid[i], NULL); /* wait for read/write threads to complete */ for ( i = 0; i < rwthreads; i++ ) ldap_pvt_thread_join(rwtid[i], NULL); for(i = 0; i < noconns; i++) { if ( lds[i] != NULL ) { ldap_unbind_ext( lds[i], NULL, NULL ); } } free( lds ); for ( i = 0; i < threads; i++ ) { snprintf(outstr, BUFSIZ, "RO thread %d pass=%d fail=%d", i, rt_pass[i], rt_fail[i]); tester_error(outstr); if (rt_fail[i] != 0 || rt_pass[i] != ptpass) { snprintf(outstr, BUFSIZ, "FAIL RO thread %d", i); tester_error(outstr); testfail++; } } for ( i = 0; i < rwthreads; i++ ) { snprintf(outstr, BUFSIZ, "RW thread %d pass=%d fail=%d", i + MAX_THREAD, rwt_pass[i], rwt_fail[i]); tester_error(outstr); if (rwt_fail[i] != 0 || rwt_pass[i] != ptpass) { snprintf(outstr, BUFSIZ, "FAIL RW thread %d", i); tester_error(outstr); testfail++; } } snprintf(outstr, BUFSIZ, "MT Test complete" ); tester_error(outstr); if (testfail) exit( EXIT_FAILURE ); exit( EXIT_SUCCESS ); }