static void do_read( LDAP *ld, char *entry, char **attrs, int noattrs, int nobind, int maxloop, int maxretries, int delay, int force, int chaserefs, int idx ) { int i = 0, do_retry = maxretries; int rc = LDAP_SUCCESS; char thrstr[BUFSIZ]; retry:; if ( do_retry == maxretries ) { snprintf( thrstr, BUFSIZ, "Read(%d): entry=\"%s\".\n", maxloop, entry ); thread_verbose( idx, thrstr ); } snprintf(thrstr, BUFSIZ, "LD %p cnt: %d (retried %d) (%s)", \ (void *) ld, maxloop, (do_retry - maxretries), entry); thread_verbose( idx, thrstr ); for ( ; i < maxloop; i++ ) { LDAPMessage *res = NULL; rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE, NULL, attrs, noattrs, NULL, NULL, NULL, LDAP_NO_LIMIT, &res ); if ( res != NULL ) { ldap_msgfree( res ); } if ( rc == 0 ) { rt_pass[idx]++; } else { int first = tester_ignore_err( rc ); char buf[ BUFSIZ ]; rt_fail[idx]++; snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry ); /* if ignore.. */ if ( first ) { /* only log if first occurrence */ if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) { tester_ldap_error( ld, buf, NULL ); } continue; } /* busy needs special handling */ tester_ldap_error( ld, buf, NULL ); if ( rc == LDAP_BUSY && do_retry > 0 ) { do_retry--; goto retry; } break; } } }
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 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; }
void tester_init_ld( LDAP **ldp, struct tester_conn_args *config, int flags ) { LDAP *ld; int rc, do_retry = config->retries; int version = LDAP_VERSION3; retry:; ldap_initialize( &ld, config->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, config->chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF ); if ( !( flags & TESTER_INIT_ONLY ) ) { if ( config->authmethod == LDAP_AUTH_SASL ) { #ifdef HAVE_CYRUS_SASL if ( config->secprops != NULL ) { rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS, config->secprops ); if ( rc != LDAP_OPT_SUCCESS ) { tester_ldap_error( ld, "ldap_set_option(SECPROPS)", NULL ); ldap_unbind_ext( ld, NULL, NULL ); exit( EXIT_FAILURE ); } } rc = ldap_sasl_interactive_bind_s( ld, config->binddn, config->mech, NULL, NULL, LDAP_SASL_QUIET, lutil_sasl_interact, config->defaults ); #else /* HAVE_CYRUS_SASL */ /* caller shouldn't have allowed this */ assert(0); #endif } else if ( config->authmethod == LDAP_AUTH_SIMPLE ) { rc = ldap_sasl_bind_s( ld, config->binddn, LDAP_SASL_SIMPLE, &config->pass, NULL, NULL, NULL ); } if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); switch ( rc ) { case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; if ( config->delay > 0 ) { sleep( config->delay ); } goto retry; } } ldap_unbind_ext( ld, NULL, NULL ); exit( EXIT_FAILURE ); } } *ldp = ld; }
static void do_modify( char *uri, char *manager, struct berval *passwd, char *entry, char* attr, char* value, int maxloop, int maxretries, int delay, int friendly, int chaserefs ) { LDAP *ld = NULL; int i = 0, do_retry = maxretries; int rc = LDAP_SUCCESS; struct ldapmod mod; struct ldapmod *mods[2]; char *values[2]; int version = LDAP_VERSION3; values[0] = value; values[1] = NULL; mod.mod_op = LDAP_MOD_ADD; mod.mod_type = attr; mod.mod_values = values; mods[0] = &mod; mods[1] = NULL; retry: ; 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 ); if ( do_retry == maxretries ) { fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n", (long) pid, maxloop, entry ); } rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); switch ( rc ) { case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; if ( delay > 0 ) { sleep( delay ); } goto retry; } /* fallthru */ default: break; } exit( EXIT_FAILURE ); } for ( ; i < maxloop; i++ ) { mod.mod_op = LDAP_MOD_ADD; rc = ldap_modify_ext_s( ld, entry, mods, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_modify_ext_s", NULL ); switch ( rc ) { case LDAP_TYPE_OR_VALUE_EXISTS: /* NOTE: this likely means * the second modify failed * during the previous round... */ if ( !friendly ) { goto done; } break; case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; goto retry; } /* fall thru */ default: goto done; } } mod.mod_op = LDAP_MOD_DELETE; rc = ldap_modify_ext_s( ld, entry, mods, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_modify_ext_s", NULL ); switch ( rc ) { case LDAP_NO_SUCH_ATTRIBUTE: /* NOTE: this likely means * the first modify failed * during the previous round... */ if ( !friendly ) { goto done; } break; case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; goto retry; } /* fall thru */ default: goto done; } } } done: ; fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc ); ldap_unbind_ext( ld, NULL, NULL ); }
static void do_modrdn( char *uri, char *manager, struct berval *passwd, char *entry, int maxloop, int maxretries, int delay, int friendly, int chaserefs ) { LDAP *ld = NULL; int i, do_retry = maxretries; char *DNs[2]; char *rdns[2]; int rc = LDAP_SUCCESS; char *p1, *p2; int version = LDAP_VERSION3; DNs[0] = entry; DNs[1] = strdup( entry ); /* reverse the RDN, make new DN */ p1 = strchr( entry, '=' ) + 1; p2 = strchr( p1, ',' ); *p2 = '\0'; rdns[1] = strdup( entry ); *p2-- = ','; for (i = p1 - entry;p2 >= p1;) DNs[1][i++] = *p2--; DNs[1][i] = '\0'; rdns[0] = strdup( DNs[1] ); DNs[1][i] = ','; i = 0; retry:; 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 ); if ( do_retry == maxretries ) { fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n", (long) pid, maxloop, entry ); } rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); switch ( rc ) { case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; if ( delay > 0) { sleep( delay ); } goto retry; } /* fallthru */ default: break; } exit( EXIT_FAILURE ); } for ( ; i < maxloop; i++ ) { rc = ldap_rename_s( ld, DNs[0], rdns[0], NULL, 0, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_rename_s", NULL ); switch ( rc ) { case LDAP_NO_SUCH_OBJECT: /* NOTE: this likely means * the second modrdn failed * during the previous round... */ if ( !friendly ) { goto done; } break; case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; goto retry; } /* fall thru */ default: goto done; } } rc = ldap_rename_s( ld, DNs[1], rdns[1], NULL, 1, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_rename_s", NULL ); switch ( rc ) { case LDAP_NO_SUCH_OBJECT: /* NOTE: this likely means * the first modrdn failed * during the previous round... */ if ( !friendly ) { goto done; } break; case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; goto retry; } /* fall thru */ default: goto done; } } } done:; fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc ); ldap_unbind_ext( ld, NULL, NULL ); }
static void do_addel( char *uri, char *manager, struct berval *passwd, char *entry, LDAPMod **attrs, int maxloop, int maxretries, int delay, int friendly, int chaserefs ) { LDAP *ld = NULL; int i = 0, do_retry = maxretries; int rc = LDAP_SUCCESS; int version = LDAP_VERSION3; retry:; 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 ); if ( do_retry == maxretries ) { fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n", (long) pid, maxloop, entry ); } rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); switch ( rc ) { case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; if ( delay != 0 ) { sleep( delay ); } goto retry; } /* fallthru */ default: break; } exit( EXIT_FAILURE ); } for ( ; i < maxloop; i++ ) { /* add the entry */ rc = ldap_add_ext_s( ld, entry, attrs, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_add_ext_s", NULL ); switch ( rc ) { case LDAP_ALREADY_EXISTS: /* NOTE: this likely means * the delete failed * during the previous round... */ if ( !friendly ) { goto done; } break; case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; goto retry; } /* fall thru */ default: goto done; } } #if 0 /* wait a second for the add to really complete */ /* This masks some race conditions though. */ sleep( 1 ); #endif /* now delete the entry again */ rc = ldap_delete_ext_s( ld, entry, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_delete_ext_s", NULL ); switch ( rc ) { case LDAP_NO_SUCH_OBJECT: /* NOTE: this likely means * the add failed * during the previous round... */ if ( !friendly ) { goto done; } break; case LDAP_BUSY: case LDAP_UNAVAILABLE: if ( do_retry > 0 ) { do_retry--; goto retry; } /* fall thru */ default: goto done; } } } done:; fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc ); ldap_unbind_ext( ld, NULL, NULL ); }
static void do_search( char *uri, char *manager, struct berval *passwd, char *sbase, int scope, char *filter, LDAP **ldp, char **attrs, int noattrs, int nobind, int innerloop, int maxretries, int delay, int force, int chaserefs ) { LDAP *ld = ldp ? *ldp : NULL; int i = 0, do_retry = maxretries; int rc = LDAP_SUCCESS; int version = LDAP_VERSION3; char buf[ BUFSIZ ]; int *msgids = NULL, active = 0; /* make room for msgid */ if ( swamp > 1 ) { msgids = (int *)calloc( sizeof(int), innerloop ); } retry:; if ( ld == NULL ) { 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 ); if ( do_retry == maxretries ) { fprintf( stderr, "PID=%ld - Search(%d): " "base=\"%s\" scope=%s filter=\"%s\" " "attrs=%s%s.\n", (long) pid, innerloop, sbase, ldap_pvt_scope2str( scope ), filter, attrs[0], attrs[1] ? " (more...)" : "" ); } if ( nobind == 0 ) { rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { snprintf( buf, sizeof( buf ), "bindDN=\"%s\"", manager ); tester_ldap_error( ld, "ldap_sasl_bind_s", buf ); 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 ); } } } if ( swamp > 1 ) { do { LDAPMessage *res = NULL; int j, msgid; if ( i < innerloop ) { rc = ldap_search_ext( ld, sbase, scope, filter, NULL, noattrs, NULL, NULL, NULL, LDAP_NO_LIMIT, &msgids[i] ); active++; #if 0 fprintf( stderr, ">>> PID=%ld - Search maxloop=%d cnt=%d active=%d msgid=%d: " "base=\"%s\" scope=%s filter=\"%s\"\n", (long) pid, innerloop, i, active, msgids[i], sbase, ldap_pvt_scope2str( scope ), filter ); #endif i++; 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_search_ext", NULL ); } continue; } /* busy needs special handling */ snprintf( buf, sizeof( buf ), "base=\"%s\" filter=\"%s\"\n", sbase, filter ); tester_ldap_error( ld, "ldap_search_ext", buf ); if ( rc == LDAP_BUSY && do_retry > 0 ) { ldap_unbind_ext( ld, NULL, NULL ); ld = NULL; do_retry--; goto retry; } break; } if ( swamp > 2 ) { continue; } } rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ); switch ( rc ) { case -1: /* gone really bad */ goto cleanup; case 0: /* timeout (impossible) */ break; case LDAP_RES_SEARCH_ENTRY: case LDAP_RES_SEARCH_REFERENCE: /* ignore */ break; case LDAP_RES_SEARCH_RESULT: /* just remove, no error checking (TODO?) */ msgid = ldap_msgid( res ); ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, NULL, 1 ); res = NULL; /* linear search, bah */ for ( j = 0; j < i; j++ ) { if ( msgids[ j ] == msgid ) { msgids[ j ] = -1; active--; #if 0 fprintf( stderr, "<<< PID=%ld - SearchDone maxloop=%d cnt=%d active=%d msgid=%d: " "base=\"%s\" scope=%s filter=\"%s\"\n", (long) pid, innerloop, j, active, msgid, sbase, ldap_pvt_scope2str( scope ), filter ); #endif break; } } break; default: /* other messages unexpected */ fprintf( stderr, "### PID=%ld - Search(%d): " "base=\"%s\" scope=%s filter=\"%s\" " "attrs=%s%s. unexpected response tag=%d\n", (long) pid, innerloop, sbase, ldap_pvt_scope2str( scope ), filter, attrs[0], attrs[1] ? " (more...)" : "", rc ); break; } if ( res != NULL ) { ldap_msgfree( res ); } } while ( i < innerloop || active > 0 ); } else { for ( ; i < innerloop; i++ ) { LDAPMessage *res = NULL; if (swamp) { int msgid; rc = ldap_search_ext( ld, sbase, scope, filter, NULL, noattrs, NULL, NULL, NULL, LDAP_NO_LIMIT, &msgid ); if ( rc == LDAP_SUCCESS ) continue; else break; } rc = ldap_search_ext_s( ld, sbase, scope, filter, attrs, noattrs, NULL, NULL, NULL, LDAP_NO_LIMIT, &res ); if ( res != NULL ) { ldap_msgfree( res ); } 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_search_ext_s", NULL ); } continue; } /* busy needs special handling */ snprintf( buf, sizeof( buf ), "base=\"%s\" filter=\"%s\"\n", sbase, filter ); tester_ldap_error( ld, "ldap_search_ext_s", buf ); if ( rc == LDAP_BUSY && do_retry > 0 ) { ldap_unbind_ext( ld, NULL, NULL ); ld = NULL; do_retry--; goto retry; } break; } } } cleanup:; if ( msgids != NULL ) { free( msgids ); } if ( ldp != NULL ) { *ldp = ld; } else { fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc ); if ( ld != NULL ) { ldap_unbind_ext( ld, NULL, NULL ); } } }
static void do_random( char *uri, char *manager, struct berval *passwd, char *sbase, int scope, char *filter, char *attr, char **srchattrs, int noattrs, int nobind, int innerloop, int maxretries, int delay, int force, int chaserefs ) { LDAP *ld = NULL; int i = 0, do_retry = maxretries; char *attrs[ 2 ]; int rc = LDAP_SUCCESS; int version = LDAP_VERSION3; int nvalues = 0; char **values = NULL; LDAPMessage *res = NULL, *e = NULL; attrs[ 0 ] = attr; attrs[ 1 ] = NULL; 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 ); if ( do_retry == maxretries ) { fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n", (long) pid, innerloop, sbase, filter, attr ); } if ( nobind == 0 ) { rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { tester_ldap_error( ld, "ldap_sasl_bind_s", NULL ); switch ( rc ) { case LDAP_BUSY: case LDAP_UNAVAILABLE: /* fallthru */ default: break; } exit( EXIT_FAILURE ); } } rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res ); switch ( rc ) { case LDAP_SIZELIMIT_EXCEEDED: case LDAP_TIMELIMIT_EXCEEDED: case LDAP_SUCCESS: if ( ldap_count_entries( ld, res ) == 0 ) { if ( rc ) { tester_ldap_error( ld, "ldap_search_ext_s", NULL ); } break; } for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) ) { struct berval **v = ldap_get_values_len( ld, e, attr ); if ( v != NULL ) { int n = ldap_count_values_len( v ); int j; values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) ); for ( j = 0; j < n; j++ ) { values[ nvalues + j ] = strdup( v[ j ]->bv_val ); } values[ nvalues + j ] = NULL; nvalues += n; ldap_value_free_len( v ); } } ldap_msgfree( res ); if ( !values ) { fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n", (long) pid, sbase, filter, nvalues ); exit(EXIT_FAILURE); } if ( do_retry == maxretries ) { fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n", (long) pid, sbase, filter, nvalues ); } for ( i = 0; i < innerloop; i++ ) { char buf[ BUFSIZ ]; #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */ int r = rand() % nvalues; #endif int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0); snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] ); do_search( uri, manager, passwd, sbase, scope, buf, &ld, srchattrs, noattrs, nobind, 1, maxretries, delay, force, chaserefs ); } break; default: tester_ldap_error( ld, "ldap_search_ext_s", NULL ); break; } fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc ); if ( ld != NULL ) { ldap_unbind_ext( ld, NULL, NULL ); } }
static void do_read( char *uri, char *manager, struct berval *passwd, char *entry, LDAP **ldp, char **attrs, int noattrs, int nobind, int maxloop, int maxretries, int delay, int force, int chaserefs ) { LDAP *ld = ldp ? *ldp : NULL; int i = 0, do_retry = maxretries; int rc = LDAP_SUCCESS; int version = LDAP_VERSION3; retry:; if ( ld == NULL ) { 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 ); if ( do_retry == maxretries ) { fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n", (long) pid, maxloop, entry ); } if ( nobind == 0 ) { rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { 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 ); } } } for ( ; i < maxloop; i++ ) { LDAPMessage *res = NULL; rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE, NULL, attrs, noattrs, NULL, NULL, NULL, LDAP_NO_LIMIT, &res ); if ( res != NULL ) { ldap_msgfree( res ); } if ( rc ) { int first = tester_ignore_err( rc ); char buf[ BUFSIZ ]; snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry ); /* if ignore.. */ if ( first ) { /* only log if first occurrence */ if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) { tester_ldap_error( ld, buf, NULL ); } continue; } /* busy needs special handling */ tester_ldap_error( ld, buf, NULL ); if ( rc == LDAP_BUSY && do_retry > 0 ) { ldap_unbind_ext( ld, NULL, NULL ); ld = NULL; do_retry--; goto retry; } break; } } if ( ldp != NULL ) { *ldp = ld; } else { fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc ); if ( ld != NULL ) { ldap_unbind_ext( ld, NULL, NULL ); } } }
/* substitute a generated int into the filter */ static void do_random2( LDAP *ld, char *sbase, char *filter, char **srchattrs, int noattrs, int nobind, int innerloop, int maxretries, int delay, int force, int chaserefs, int idx ) { int i = 0, do_retry = maxretries; int rc = LDAP_SUCCESS; int lo, hi, range; int flen; LDAPMessage *res = NULL, *e = NULL; char *ptr, *ftail; char thrstr[BUFSIZ]; char fbuf[BUFSIZ]; snprintf( thrstr, BUFSIZ, "Read(%d): base=\"%s\", filter=\"%s\".\n", innerloop, sbase, filter ); thread_verbose( idx, thrstr ); ptr = strchr(filter, '['); if (!ptr) return; ftail = strchr(filter, ']'); if (!ftail || ftail < ptr) return; sscanf(ptr, "[%d-%d]", &lo, &hi); range = hi - lo + 1; flen = ptr - filter; ftail++; for ( i = 0; i < innerloop; i++ ) { int r = ((double)range)*rand()/(RAND_MAX + 1.0); sprintf(fbuf, "%.*s%d%s", flen, filter, r, ftail); rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE, fbuf, srchattrs, noattrs, NULL, NULL, NULL, LDAP_NO_LIMIT, &res ); if ( res != NULL ) { ldap_msgfree( res ); } if ( rc == 0 ) { rt_pass[idx]++; } else { int first = tester_ignore_err( rc ); char buf[ BUFSIZ ]; rt_fail[idx]++; snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry ); /* if ignore.. */ if ( first ) { /* only log if first occurrence */ if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) { tester_ldap_error( ld, buf, NULL ); } continue; } /* busy needs special handling */ tester_ldap_error( ld, buf, NULL ); if ( rc == LDAP_BUSY && do_retry > 0 ) { do_retry--; continue; } break; } } snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc ); thread_verbose( idx, thrstr ); }
static void do_random( LDAP *ld, char *sbase, char *filter, char **srchattrs, int noattrs, int nobind, int innerloop, int maxretries, int delay, int force, int chaserefs, int idx ) { int i = 0, do_retry = maxretries; char *attrs[ 2 ]; int rc = LDAP_SUCCESS; int nvalues = 0; char **values = NULL; LDAPMessage *res = NULL, *e = NULL; char thrstr[BUFSIZ]; attrs[ 0 ] = LDAP_NO_ATTRS; attrs[ 1 ] = NULL; snprintf( thrstr, BUFSIZ, "Read(%d): base=\"%s\", filter=\"%s\".\n", innerloop, sbase, filter ); thread_verbose( idx, thrstr ); rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res ); switch ( rc ) { case LDAP_SIZELIMIT_EXCEEDED: case LDAP_TIMELIMIT_EXCEEDED: case LDAP_SUCCESS: nvalues = ldap_count_entries( ld, res ); if ( nvalues == 0 ) { if ( rc ) { tester_ldap_error( ld, "ldap_search_ext_s", NULL ); } break; } values = malloc( ( nvalues + 1 ) * sizeof( char * ) ); for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) ) { values[ i ] = ldap_get_dn( ld, e ); } values[ i ] = NULL; ldap_msgfree( res ); if ( do_retry == maxretries ) { snprintf( thrstr, BUFSIZ, "Read base=\"%s\" filter=\"%s\" got %d values.\n", sbase, filter, nvalues ); thread_verbose( idx, thrstr ); } for ( i = 0; i < innerloop; i++ ) { int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0); do_read( ld, values[ r ], srchattrs, noattrs, nobind, 1, maxretries, delay, force, chaserefs, idx ); } for( i = 0; i < nvalues; i++) { if (values[i] != NULL) ldap_memfree( values[i] ); } free( values ); break; default: tester_ldap_error( ld, "ldap_search_ext_s", NULL ); break; } snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc ); thread_verbose( idx, thrstr ); }