static int rc_cf_gen( ConfigArgs *c ) { slap_overinst *on = (slap_overinst *)c->bi; retcode_t *rd = (retcode_t *)on->on_bi.bi_private; int rc = ARG_BAD_CONF; if ( c->op == SLAP_CONFIG_EMIT ) { switch( c->type ) { case RC_PARENT: if ( !BER_BVISEMPTY( &rd->rd_pdn )) { rc = value_add_one( &c->rvalue_vals, &rd->rd_pdn ); if ( rc == 0 ) { rc = value_add_one( &c->rvalue_nvals, &rd->rd_npdn ); } return rc; } rc = 0; break; case RC_ITEM: { retcode_item_t *rdi; int i; for ( rdi = rd->rd_item, i = 0; rdi; rdi = rdi->rdi_next, i++ ) { char buf[4096]; struct berval bv; char *ptr; bv.bv_len = snprintf( buf, sizeof( buf ), SLAP_X_ORDERED_FMT, i ); bv.bv_len += rdi->rdi_line.bv_len; ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 ); ptr = lutil_strcopy( ptr, buf ); ptr = lutil_strncopy( ptr, rdi->rdi_line.bv_val, rdi->rdi_line.bv_len ); ber_bvarray_add( &c->rvalue_vals, &bv ); } rc = 0; } break; default: LDAP_BUG(); break; } return rc; } else if ( c->op == LDAP_MOD_DELETE ) { switch( c->type ) { case RC_PARENT: if ( rd->rd_pdn.bv_val ) { ber_memfree ( rd->rd_pdn.bv_val ); rc = 0; } if ( rd->rd_npdn.bv_val ) { ber_memfree ( rd->rd_npdn.bv_val ); } break; case RC_ITEM: if ( c->valx == -1 ) { retcode_item_t *rdi, *next; for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) { next = rdi->rdi_next; retcode_item_destroy( rdi ); } } else { retcode_item_t **rdip, *rdi; int i; for ( rdip = &rd->rd_item, i = 0; i <= c->valx && *rdip; i++, rdip = &(*rdip)->rdi_next ) ; if ( *rdip == NULL ) { return 1; } rdi = *rdip; *rdip = rdi->rdi_next; retcode_item_destroy( rdi ); } rc = 0; break; default: LDAP_BUG(); break; } return rc; /* FIXME */ } switch( c->type ) { case RC_PARENT: if ( rd->rd_pdn.bv_val ) { ber_memfree ( rd->rd_pdn.bv_val ); } if ( rd->rd_npdn.bv_val ) { ber_memfree ( rd->rd_npdn.bv_val ); } rd->rd_pdn = c->value_dn; rd->rd_npdn = c->value_ndn; rc = 0; break; case RC_ITEM: { retcode_item_t rdi = { BER_BVNULL }, **rdip; struct berval bv, rdn, nrdn; char *next = NULL; int i; if ( c->argc < 3 ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "\"retcode-item <RDN> <retcode> [<text>]\": " "missing args" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } ber_str2bv( c->argv[ 1 ], 0, 0, &bv ); rc = dnPrettyNormal( NULL, &bv, &rdn, &nrdn, NULL ); if ( rc != LDAP_SUCCESS ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "unable to normalize RDN \"%s\": %d", c->argv[ 1 ], rc ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } if ( !dnIsOneLevelRDN( &nrdn ) ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "value \"%s\" is not a RDN", c->argv[ 1 ] ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } if ( BER_BVISNULL( &rd->rd_npdn ) ) { /* FIXME: we use the database suffix */ if ( c->be->be_nsuffix == NULL ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "either \"retcode-parent\" " "or \"suffix\" must be defined" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } ber_dupbv( &rd->rd_pdn, &c->be->be_suffix[ 0 ] ); ber_dupbv( &rd->rd_npdn, &c->be->be_nsuffix[ 0 ] ); } build_new_dn( &rdi.rdi_dn, &rd->rd_pdn, &rdn, NULL ); build_new_dn( &rdi.rdi_ndn, &rd->rd_npdn, &nrdn, NULL ); ch_free( rdn.bv_val ); ch_free( nrdn.bv_val ); rdi.rdi_err = strtol( c->argv[ 2 ], &next, 0 ); if ( next == c->argv[ 2 ] || next[ 0 ] != '\0' ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "unable to parse return code \"%s\"", c->argv[ 2 ] ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } rdi.rdi_mask = SN_DG_OP_ALL; if ( c->argc > 3 ) { for ( i = 3; i < c->argc; i++ ) { if ( strncasecmp( c->argv[ i ], "op=", STRLENOF( "op=" ) ) == 0 ) { char **ops; int j; ops = ldap_str2charray( &c->argv[ i ][ STRLENOF( "op=" ) ], "," ); assert( ops != NULL ); rdi.rdi_mask = SN_DG_OP_NONE; for ( j = 0; ops[ j ] != NULL; j++ ) { if ( strcasecmp( ops[ j ], "add" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_ADD; } else if ( strcasecmp( ops[ j ], "bind" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_BIND; } else if ( strcasecmp( ops[ j ], "compare" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_COMPARE; } else if ( strcasecmp( ops[ j ], "delete" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_DELETE; } else if ( strcasecmp( ops[ j ], "modify" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_MODIFY; } else if ( strcasecmp( ops[ j ], "rename" ) == 0 || strcasecmp( ops[ j ], "modrdn" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_RENAME; } else if ( strcasecmp( ops[ j ], "search" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_SEARCH; } else if ( strcasecmp( ops[ j ], "extended" ) == 0 ) { rdi.rdi_mask |= SN_DG_EXTENDED; } else if ( strcasecmp( ops[ j ], "auth" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_AUTH; } else if ( strcasecmp( ops[ j ], "read" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_READ; } else if ( strcasecmp( ops[ j ], "write" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_WRITE; } else if ( strcasecmp( ops[ j ], "all" ) == 0 ) { rdi.rdi_mask |= SN_DG_OP_ALL; } else { snprintf( c->cr_msg, sizeof(c->cr_msg), "unknown op \"%s\"", ops[ j ] ); ldap_charray_free( ops ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } } ldap_charray_free( ops ); } else if ( strncasecmp( c->argv[ i ], "text=", STRLENOF( "text=" ) ) == 0 ) { if ( !BER_BVISNULL( &rdi.rdi_text ) ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "\"text\" already provided" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } ber_str2bv( &c->argv[ i ][ STRLENOF( "text=" ) ], 0, 1, &rdi.rdi_text ); } else if ( strncasecmp( c->argv[ i ], "matched=", STRLENOF( "matched=" ) ) == 0 ) { struct berval dn; if ( !BER_BVISNULL( &rdi.rdi_matched ) ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "\"matched\" already provided" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } ber_str2bv( &c->argv[ i ][ STRLENOF( "matched=" ) ], 0, 0, &dn ); if ( dnPretty( NULL, &dn, &rdi.rdi_matched, NULL ) != LDAP_SUCCESS ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "unable to prettify matched DN \"%s\"", &c->argv[ i ][ STRLENOF( "matched=" ) ] ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } } else if ( strncasecmp( c->argv[ i ], "ref=", STRLENOF( "ref=" ) ) == 0 ) { char **refs; int j; if ( rdi.rdi_ref != NULL ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "\"ref\" already provided" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } if ( rdi.rdi_err != LDAP_REFERRAL ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "providing \"ref\" " "along with a non-referral " "resultCode may cause slapd failures " "related to internal checks" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); } refs = ldap_str2charray( &c->argv[ i ][ STRLENOF( "ref=" ) ], " " ); assert( refs != NULL ); for ( j = 0; refs[ j ] != NULL; j++ ) { struct berval bv; ber_str2bv( refs[ j ], 0, 1, &bv ); ber_bvarray_add( &rdi.rdi_ref, &bv ); } ldap_charray_free( refs ); } else if ( strncasecmp( c->argv[ i ], "sleeptime=", STRLENOF( "sleeptime=" ) ) == 0 ) { if ( rdi.rdi_sleeptime != 0 ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "\"sleeptime\" already provided" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } if ( lutil_atoi( &rdi.rdi_sleeptime, &c->argv[ i ][ STRLENOF( "sleeptime=" ) ] ) ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "unable to parse \"sleeptime=%s\"", &c->argv[ i ][ STRLENOF( "sleeptime=" ) ] ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } } else if ( strncasecmp( c->argv[ i ], "unsolicited=", STRLENOF( "unsolicited=" ) ) == 0 ) { char *data; if ( !BER_BVISNULL( &rdi.rdi_unsolicited_oid ) ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "\"unsolicited\" already provided" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } data = strchr( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ], ':' ); if ( data != NULL ) { struct berval oid; if ( ldif_parse_line2( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ], &oid, &rdi.rdi_unsolicited_data, NULL ) ) { snprintf( c->cr_msg, sizeof(c->cr_msg), "unable to parse \"unsolicited\"" ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } ber_dupbv( &rdi.rdi_unsolicited_oid, &oid ); } else { ber_str2bv( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ], 0, 1, &rdi.rdi_unsolicited_oid ); } } else if ( strncasecmp( c->argv[ i ], "flags=", STRLENOF( "flags=" ) ) == 0 ) { char *arg = &c->argv[ i ][ STRLENOF( "flags=" ) ]; if ( strcasecmp( arg, "disconnect" ) == 0 ) { rdi.rdi_flags |= RDI_PRE_DISCONNECT; } else if ( strcasecmp( arg, "pre-disconnect" ) == 0 ) { rdi.rdi_flags |= RDI_PRE_DISCONNECT; } else if ( strcasecmp( arg, "post-disconnect" ) == 0 ) { rdi.rdi_flags |= RDI_POST_DISCONNECT; } else { snprintf( c->cr_msg, sizeof(c->cr_msg), "unknown flag \"%s\"", arg ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } } else { snprintf( c->cr_msg, sizeof(c->cr_msg), "unknown option \"%s\"", c->argv[ i ] ); Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n", c->log, c->cr_msg ); return ARG_BAD_CONF; } } } rdi.rdi_line.bv_len = 2*(c->argc - 1) + c->argc - 2; for ( i = 1; i < c->argc; i++ ) { rdi.rdi_line.bv_len += strlen( c->argv[ i ] ); } next = rdi.rdi_line.bv_val = ch_malloc( rdi.rdi_line.bv_len + 1 ); for ( i = 1; i < c->argc; i++ ) { *next++ = '"'; next = lutil_strcopy( next, c->argv[ i ] ); *next++ = '"'; *next++ = ' '; } *--next = '\0'; for ( rdip = &rd->rd_item; *rdip; rdip = &(*rdip)->rdi_next ) /* go to last */ ; *rdip = ( retcode_item_t * )ch_malloc( sizeof( retcode_item_t ) ); *(*rdip) = rdi; rc = 0; } break; default: rc = SLAP_CONF_UNKNOWN; break; } return rc; }
int limits_parse_one( const char *arg, struct slap_limits_set *limit ) { assert( arg != NULL ); assert( limit != NULL ); if ( STRSTART( arg, "time" ) ) { arg += STRLENOF( "time" ); if ( arg[0] == '.' ) { arg++; if ( STRSTART( arg, "soft=" ) ) { arg += STRLENOF( "soft=" ); if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_t_soft = -1; } else { int soft; if ( lutil_atoi( &soft, arg ) != 0 || soft < -1 ) { return( 1 ); } if ( soft == -1 ) { /* FIXME: use "unlimited" instead; issue warning? */ } limit->lms_t_soft = soft; } } else if ( STRSTART( arg, "hard=" ) ) { arg += STRLENOF( "hard=" ); if ( strcasecmp( arg, "soft" ) == 0 ) { limit->lms_t_hard = 0; } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_t_hard = -1; } else { int hard; if ( lutil_atoi( &hard, arg ) != 0 || hard < -1 ) { return( 1 ); } if ( hard == -1 ) { /* FIXME: use "unlimited" instead */ } if ( hard == 0 ) { /* FIXME: use "soft" instead */ } limit->lms_t_hard = hard; } } else { return( 1 ); } } else if ( arg[0] == '=' ) { arg++; if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_t_soft = -1; } else { if ( lutil_atoi( &limit->lms_t_soft, arg ) != 0 || limit->lms_t_soft < -1 ) { return( 1 ); } } limit->lms_t_hard = 0; } else { return( 1 ); } } else if ( STRSTART( arg, "size" ) ) { arg += STRLENOF( "size" ); if ( arg[0] == '.' ) { arg++; if ( STRSTART( arg, "soft=" ) ) { arg += STRLENOF( "soft=" ); if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_s_soft = -1; } else { int soft; if ( lutil_atoi( &soft, arg ) != 0 || soft < -1 ) { return( 1 ); } if ( soft == -1 ) { /* FIXME: use "unlimited" instead */ } limit->lms_s_soft = soft; } } else if ( STRSTART( arg, "hard=" ) ) { arg += STRLENOF( "hard=" ); if ( strcasecmp( arg, "soft" ) == 0 ) { limit->lms_s_hard = 0; } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_s_hard = -1; } else { int hard; if ( lutil_atoi( &hard, arg ) != 0 || hard < -1 ) { return( 1 ); } if ( hard == -1 ) { /* FIXME: use "unlimited" instead */ } if ( hard == 0 ) { /* FIXME: use "soft" instead */ } limit->lms_s_hard = hard; } } else if ( STRSTART( arg, "unchecked=" ) ) { arg += STRLENOF( "unchecked=" ); if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_s_unchecked = -1; } else if ( strcasecmp( arg, "disabled" ) == 0 ) { limit->lms_s_unchecked = 0; } else { int unchecked; if ( lutil_atoi( &unchecked, arg ) != 0 || unchecked < -1 ) { return( 1 ); } if ( unchecked == -1 ) { /* FIXME: use "unlimited" instead */ } limit->lms_s_unchecked = unchecked; } } else if ( STRSTART( arg, "pr=" ) ) { arg += STRLENOF( "pr=" ); if ( strcasecmp( arg, "noEstimate" ) == 0 ) { limit->lms_s_pr_hide = 1; } else if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_s_pr = -1; } else { int pr; if ( lutil_atoi( &pr, arg ) != 0 || pr < -1 ) { return( 1 ); } if ( pr == -1 ) { /* FIXME: use "unlimited" instead */ } limit->lms_s_pr = pr; } } else if ( STRSTART( arg, "prtotal=" ) ) { arg += STRLENOF( "prtotal=" ); if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_s_pr_total = -1; } else if ( strcasecmp( arg, "disabled" ) == 0 ) { limit->lms_s_pr_total = -2; } else if ( strcasecmp( arg, "hard" ) == 0 ) { limit->lms_s_pr_total = 0; } else { int total; if ( lutil_atoi( &total, arg ) != 0 || total < -1 ) { return( 1 ); } if ( total == -1 ) { /* FIXME: use "unlimited" instead */ } if ( total == 0 ) { /* FIXME: use "pr=disable" instead */ } limit->lms_s_pr_total = total; } } else { return( 1 ); } } else if ( arg[0] == '=' ) { arg++; if ( strcasecmp( arg, "unlimited" ) == 0 || strcasecmp( arg, "none" ) == 0 ) { limit->lms_s_soft = -1; } else { if ( lutil_atoi( &limit->lms_s_soft, arg ) != 0 || limit->lms_s_soft < -1 ) { return( 1 ); } } limit->lms_s_hard = 0; } else { return( 1 ); } } return 0; }
static int retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e ) { Attribute *a; int err; char *next; int disconnect = 0; if ( get_manageDSAit( op ) ) { return SLAP_CB_CONTINUE; } if ( !is_entry_objectclass_or_sub( e, oc_errAbsObject ) ) { return SLAP_CB_CONTINUE; } /* operation */ a = attr_find( e->e_attrs, ad_errOp ); if ( a != NULL ) { int i, gotit = 0; struct berval bv = BER_BVNULL; (void)retcode_op2str( op->o_tag, &bv ); if ( BER_BVISNULL( &bv ) ) { return SLAP_CB_CONTINUE; } for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) { if ( bvmatch( &a->a_nvals[ i ], &bv ) ) { gotit = 1; break; } } if ( !gotit ) { return SLAP_CB_CONTINUE; } } /* disconnect */ a = attr_find( e->e_attrs, ad_errDisconnect ); if ( a != NULL ) { if ( bvmatch( &a->a_nvals[ 0 ], &slap_true_bv ) ) { return rs->sr_err = SLAPD_DISCONNECT; } disconnect = 1; } /* error code */ a = attr_find( e->e_attrs, ad_errCode ); if ( a == NULL ) { return SLAP_CB_CONTINUE; } err = strtol( a->a_nvals[ 0 ].bv_val, &next, 0 ); if ( next == a->a_nvals[ 0 ].bv_val || next[ 0 ] != '\0' ) { return SLAP_CB_CONTINUE; } rs->sr_err = err; /* sleep time */ a = attr_find( e->e_attrs, ad_errSleepTime ); if ( a != NULL && a->a_nvals[ 0 ].bv_val[ 0 ] != '-' ) { int sleepTime; if ( lutil_atoi( &sleepTime, a->a_nvals[ 0 ].bv_val ) == 0 ) { retcode_sleep( sleepTime ); } } if ( rs->sr_err != LDAP_SUCCESS && !LDAP_API_ERROR( rs->sr_err )) { BackendDB db = *op->o_bd, *o_bd = op->o_bd; void *o_callback = op->o_callback; /* message text */ a = attr_find( e->e_attrs, ad_errText ); if ( a != NULL ) { rs->sr_text = a->a_vals[ 0 ].bv_val; } /* matched DN */ a = attr_find( e->e_attrs, ad_errMatchedDN ); if ( a != NULL ) { rs->sr_matched = a->a_vals[ 0 ].bv_val; } if ( bi == NULL ) { slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; bi = on->on_info->oi_orig; } db.bd_info = bi; op->o_bd = &db; op->o_callback = NULL; /* referral */ if ( rs->sr_err == LDAP_REFERRAL ) { BerVarray refs = default_referral; a = attr_find( e->e_attrs, slap_schema.si_ad_ref ); if ( a != NULL ) { refs = a->a_vals; } rs->sr_ref = referral_rewrite( refs, NULL, &op->o_req_dn, op->oq_search.rs_scope ); send_search_reference( op, rs ); ber_bvarray_free( rs->sr_ref ); rs->sr_ref = NULL; } else { a = attr_find( e->e_attrs, ad_errUnsolicitedOID ); if ( a != NULL ) { struct berval oid = BER_BVNULL, data = BER_BVNULL; ber_int_t msgid = op->o_msgid; /* RFC 4511 unsolicited response */ op->o_msgid = 0; oid = a->a_nvals[ 0 ]; a = attr_find( e->e_attrs, ad_errUnsolicitedData ); if ( a != NULL ) { data = a->a_nvals[ 0 ]; } if ( strcmp( oid.bv_val, "0" ) == 0 ) { send_ldap_result( op, rs ); } else { ber_tag_t tag = op->o_tag; op->o_tag = LDAP_REQ_EXTENDED; rs->sr_rspoid = oid.bv_val; if ( !BER_BVISNULL( &data ) ) { rs->sr_rspdata = &data; } send_ldap_extended( op, rs ); rs->sr_rspoid = NULL; rs->sr_rspdata = NULL; op->o_tag = tag; } op->o_msgid = msgid; } else { send_ldap_result( op, rs ); } } rs->sr_text = NULL; rs->sr_matched = NULL; op->o_bd = o_bd; op->o_callback = o_callback; } if ( rs->sr_err != LDAP_SUCCESS ) { if ( disconnect ) { return rs->sr_err = SLAPD_DISCONNECT; } op->o_abandon = 1; return rs->sr_err; } return SLAP_CB_CONTINUE; }
/* * ldap_parse_ldif_record_x() will convert an LDIF record read with ldif_read_record() * into an array of LDAPMod* and an array of LDAPControl*, suitable for passing * directly to any other LDAP API function that takes LDAPMod** and LDAPControl** * arguments, such as ldap_modify_s(). * * rbuf - the ldif record buffer returned from ldif_read_record - rbuf.bv_val must be * writable - will use ldif_getline to read from it * linenum - the ldif line number returned from ldif_read_record * - used for logging errors (e.g. error at line N) * lr - holds the data to return * errstr - a string used for logging (usually the program name e.g. "ldapmodify" * flags - 0 or some combination of LDIF_DEFAULT_ADD LDIF_ENTRIES_ONLY LDIF_NO_CONTROLS * ctx is the memory allocation context - if NULL, use the standard memory allocator */ int ldap_parse_ldif_record_x( struct berval *rbuf, unsigned long linenum, LDIFRecord *lr, const char *errstr, unsigned int flags, void *ctx ) { char *line, *dn; int rc, modop; int expect_modop, expect_sep; int ldapadd, new_entry, delete_entry, got_all; LDAPMod **pmods; int version; LDAPControl **pctrls; int i, j, k, idn, nmods; struct berval **bvl, bv; assert( lr != NULL ); assert( rbuf != NULL ); memset( lr, 0, sizeof(LDIFRecord) ); lr->lr_ctx = ctx; /* save memory context for later */ ldapadd = flags & LDIF_DEFAULT_ADD; new_entry = ldapadd; rc = got_all = delete_entry = modop = expect_modop = 0; expect_sep = 0; version = 0; pmods = NULL; pctrls = NULL; dn = NULL; lr->lr_lines = ldif_countlines( rbuf->bv_val ); lr->lr_btype = ber_memcalloc_x( 1, (lr->lr_lines+1)*2*sizeof(struct berval)+lr->lr_lines, ctx ); if ( !lr->lr_btype ) return LDAP_NO_MEMORY; lr->lr_vals = lr->lr_btype+lr->lr_lines+1; lr->lr_freeval = (char *)(lr->lr_vals+lr->lr_lines+1); i = -1; while ( rc == 0 && ( line = ldif_getline( &rbuf->bv_val )) != NULL ) { int freev; if ( *line == '\n' || *line == '\0' ) { break; } ++i; if ( line[0] == '-' && !line[1] ) { BER_BVZERO( lr->lr_btype+i ); lr->lr_freeval[i] = 0; continue; } if ( ( rc = ldif_parse_line2( line, lr->lr_btype+i, lr->lr_vals+i, &freev ) ) < 0 ) { fprintf( stderr, _("%s: invalid format (line %lu) entry: \"%s\"\n"), errstr, linenum+i, dn == NULL ? "" : dn ); rc = LDAP_PARAM_ERROR; goto leave; } lr->lr_freeval[i] = freev; if ( dn == NULL ) { if ( linenum+i == 1 && BV_CASEMATCH( lr->lr_btype+i, &BV_VERSION )) { /* lutil_atoi() introduces a dependence of libldap * on liblutil; we only allow version 1 by now (ITS#6654) */ #if 0 int v; if( lr->lr_vals[i].bv_len == 0 || lutil_atoi( &v, lr->lr_vals[i].bv_val) != 0 || v != 1 ) #endif static const struct berval version1 = { 1, "1" }; if ( lr->lr_vals[i].bv_len != version1.bv_len || strncmp( lr->lr_vals[i].bv_val, version1.bv_val, version1.bv_len ) != 0 ) { fprintf( stderr, _("%s: invalid version %s, line %lu (ignored)\n"), errstr, lr->lr_vals[i].bv_val, linenum ); } version++; } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) { lr->lr_dn = lr->lr_vals[i]; dn = lr->lr_dn.bv_val; /* primarily for logging */ idn = i; } /* skip all lines until we see "dn:" */ } } /* check to make sure there was a dn: line */ if ( !dn ) { rc = 0; goto leave; } lr->lr_lines = i+1; if( lr->lr_lines == 0 ) { rc = 0; goto leave; } if( version && lr->lr_lines == 1 ) { rc = 0; goto leave; } i = idn+1; /* Check for "control" tag after dn and before changetype. */ if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CONTROL )) { /* Parse and add it to the list of controls */ if ( !( flags & LDIF_NO_CONTROLS ) ) { rc = parse_ldif_control( lr->lr_vals+i, &pctrls ); if (rc != 0) { fprintf( stderr, _("%s: Error processing %s line, line %lu: %s\n"), errstr, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) ); } } i++; if ( i>= lr->lr_lines ) { short_input: fprintf( stderr, _("%s: Expecting more input after %s line, line %lu\n"), errstr, lr->lr_btype[i-1].bv_val, linenum+i ); rc = LDAP_PARAM_ERROR; goto leave; } } /* Check for changetype */ if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CHANGETYPE )) { #ifdef LIBERAL_CHANGETYPE_MODOP /* trim trailing spaces (and log warning ...) */ int icnt; for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) { if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) { break; } } if ( ++icnt != lr->lr_vals[i].bv_len ) { fprintf( stderr, _("%s: illegal trailing space after" " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"), errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn ); lr->lr_vals[i].bv_val[icnt] = '\0'; } #endif /* LIBERAL_CHANGETYPE_MODOP */ /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */ if ( flags & LDIF_ENTRIES_ONLY ) { if ( !( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) ) { ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, _("%s: skipping LDIF record beginning at line %lu: " "changetype '%.*s' found but entries only was requested\n"), errstr, linenum, (int)lr->lr_vals[i].bv_len, (const char *)lr->lr_vals[i].bv_val ); goto leave; } } if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODIFYCT )) { new_entry = 0; expect_modop = 1; } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) { new_entry = 1; modop = LDAP_MOD_ADD; } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODRDNCT ) || BV_CASEMATCH( lr->lr_vals+i, &BV_MODDNCT ) || BV_CASEMATCH( lr->lr_vals+i, &BV_RENAMECT )) { i++; if ( i >= lr->lr_lines ) goto short_input; if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWRDN )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %lu, entry \"%s\")\n"), errstr, BV_NEWRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } lr->lrop_newrdn = lr->lr_vals[i]; i++; if ( i >= lr->lr_lines ) goto short_input; if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_DELETEOLDRDN )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %lu, entry \"%s\")\n"), errstr, BV_DELETEOLDRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } lr->lrop_delold = ( lr->lr_vals[i].bv_val[0] == '0' ) ? 0 : 1; i++; if ( i < lr->lr_lines ) { if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWSUP )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %lu, entry \"%s\")\n"), errstr, BV_NEWSUP.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } lr->lrop_newsup = lr->lr_vals[i]; i++; } got_all = 1; } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_DELETECT )) { got_all = delete_entry = 1; } else { fprintf( stderr, _("%s: unknown %s \"%s\" (line %lu, entry \"%s\")\n"), errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } i++; } else if ( ldapadd ) { /* missing changetype => add */ new_entry = 1; modop = LDAP_MOD_ADD; } else { /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */ if ( flags & LDIF_ENTRIES_ONLY ) { ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, _("%s: skipping LDIF record beginning at line %lu: " "no changetype found but entries only was requested and " "the default setting for missing changetype is modify\n"), errstr, linenum ); goto leave; } expect_modop = 1; /* missing changetype => modify */ } if ( got_all ) { if ( i < lr->lr_lines ) { fprintf( stderr, _("%s: extra lines at end (line %lu, entry \"%s\")\n"), errstr, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } goto doit; } nmods = lr->lr_lines - i; idn = i; if ( new_entry ) { int fv; /* Make sure all attributes with multiple values are contiguous */ for (; i<lr->lr_lines; i++) { for (j=i+1; j<lr->lr_lines; j++) { if ( !lr->lr_btype[j].bv_val ) { fprintf( stderr, _("%s: missing attributeDescription (line %lu, entry \"%s\")\n"), errstr, linenum+j, dn ); rc = LDAP_PARAM_ERROR; goto leave; } if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) { nmods--; /* out of order, move intervening attributes down */ if ( j != i+1 ) { bv = lr->lr_vals[j]; fv = lr->lr_freeval[j]; for (k=j; k>i; k--) { lr->lr_btype[k] = lr->lr_btype[k-1]; lr->lr_vals[k] = lr->lr_vals[k-1]; lr->lr_freeval[k] = lr->lr_freeval[k-1]; } k++; lr->lr_btype[k] = lr->lr_btype[i]; lr->lr_vals[k] = bv; lr->lr_freeval[k] = fv; } i++; } } } /* Allocate space for array of mods, array of pointers to mods, * and array of pointers to values, allowing for NULL terminators * for the pointer arrays... */ lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) + (nmods+1) * sizeof(LDAPMod*) + (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx ); pmods = (LDAPMod **)(lr->lr_lm+nmods); bvl = (struct berval **)(pmods+nmods+1); j = 0; k = -1; BER_BVZERO(&bv); for (i=idn; i<lr->lr_lines; i++) { if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) { fprintf( stderr, _("%s: attributeDescription \"%s\":" " (possible missing newline" " after line %lu, entry \"%s\"?)\n"), errstr, lr->lr_btype[i].bv_val, linenum+i - 1, dn ); } if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) { bvl[k++] = NULL; bv = lr->lr_btype[i]; lr->lr_lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES; lr->lr_lm[j].mod_type = bv.bv_val; lr->lr_lm[j].mod_bvalues = bvl+k; pmods[j] = lr->lr_lm+j; j++; } bvl[k++] = lr->lr_vals+i; } bvl[k] = NULL; pmods[j] = NULL; goto doit; } lr->lr_mops = ber_memalloc_x( lr->lr_lines+1, ctx ); lr->lr_mops[lr->lr_lines] = M_SEP; lr->lr_mops[i-1] = M_SEP; for ( ; i<lr->lr_lines; i++ ) { if ( expect_modop ) { #ifdef LIBERAL_CHANGETYPE_MODOP /* trim trailing spaces (and log warning ...) */ int icnt; for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) { if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break; } if ( ++icnt != lr->lr_vals[i].bv_len ) { fprintf( stderr, _("%s: illegal trailing space after" " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"), errstr, type, lr->lr_vals[i].bv_val, linenum+i, dn ); lr->lr_vals[i].bv_val[icnt] = '\0'; } #endif /* LIBERAL_CHANGETYPE_MODOP */ expect_modop = 0; expect_sep = 1; if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) { modop = LDAP_MOD_ADD; lr->lr_mops[i] = M_SEP; nmods--; } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPREPLACE )) { /* defer handling these since they might have no values. * Use the BVALUES flag to signal that these were * deferred. If values are provided later, this * flag will be switched off. */ modop = LDAP_MOD_REPLACE; lr->lr_mops[i] = modop | LDAP_MOD_BVALUES; lr->lr_btype[i] = lr->lr_vals[i]; } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPDELETE )) { modop = LDAP_MOD_DELETE; lr->lr_mops[i] = modop | LDAP_MOD_BVALUES; lr->lr_btype[i] = lr->lr_vals[i]; } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPINCREMENT )) { modop = LDAP_MOD_INCREMENT; lr->lr_mops[i] = M_SEP; nmods--; } else { /* no modify op: invalid LDIF */ fprintf( stderr, _("%s: modify operation type is missing at" " line %lu, entry \"%s\"\n"), errstr, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } bv = lr->lr_vals[i]; } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) { lr->lr_mops[i] = M_SEP; expect_sep = 0; expect_modop = 1; nmods--; } else { if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) { fprintf( stderr, _("%s: wrong attributeType at" " line %lu, entry \"%s\"\n"), errstr, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } lr->lr_mops[i] = modop; /* If prev op was deferred and matches this type, * clear the flag */ if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES) && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 )) { lr->lr_mops[i-1] = M_SEP; nmods--; } } } /* Allocate space for array of mods, array of pointers to mods, * and array of pointers to values, allowing for NULL terminators * for the pointer arrays... */ lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) + (nmods+1) * sizeof(LDAPMod*) + (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx ); pmods = (LDAPMod **)(lr->lr_lm+nmods); bvl = (struct berval **)(pmods+nmods+1); j = 0; k = -1; BER_BVZERO(&bv); lr->lr_mops[idn-1] = M_SEP; for (i=idn; i<lr->lr_lines; i++) { if ( lr->lr_mops[i] == M_SEP ) continue; if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) { bvl[k++] = NULL; bv = lr->lr_btype[i]; lr->lr_lm[j].mod_op = lr->lr_mops[i] | LDAP_MOD_BVALUES; lr->lr_lm[j].mod_type = bv.bv_val; if ( lr->lr_mops[i] & LDAP_MOD_BVALUES ) { lr->lr_lm[j].mod_bvalues = NULL; } else { lr->lr_lm[j].mod_bvalues = bvl+k; } pmods[j] = lr->lr_lm+j; j++; } bvl[k++] = lr->lr_vals+i; } bvl[k] = NULL; pmods[j] = NULL; doit: /* first, set the common fields */ lr->lr_ctrls = pctrls; /* next, set the op */ if ( delete_entry ) { lr->lr_op = LDAP_REQ_DELETE; } else if ( lr->lrop_newrdn.bv_val != NULL ) { lr->lr_op = LDAP_REQ_MODDN; } else { /* for now, either add or modify */ lr->lrop_mods = pmods; if ( new_entry ) { lr->lr_op = LDAP_REQ_ADD; } else { lr->lr_op = LDAP_REQ_MODIFY; } } leave: if ( rc != LDAP_SUCCESS ) { ldap_ldif_record_done( lr ); } return( rc ); }
int main( int argc, char *argv[]) { LDAPURLDesc lud = { 0 }; char *uri = NULL; int gotlud = 0; int nexts = 0; lud.lud_port = -1; lud.lud_scope = -1; while ( 1 ) { int opt = getopt( argc, argv, "S:h:p:b:a:s:f:E:H:" ); if ( opt == EOF ) { break; } if ( opt == 'H' ) { if ( gotlud ) { fprintf( stderr, "option -H incompatible with previous options\n" ); usage(); } if ( uri != NULL ) { fprintf( stderr, "URI already provided\n" ); usage(); } uri = optarg; continue; } switch ( opt ) { case 'S': case 'h': case 'p': case 'b': case 'a': case 's': case 'f': case 'E': if ( uri != NULL ) { fprintf( stderr, "option -%c incompatible with -H\n", opt ); usage(); } gotlud++; } switch ( opt ) { case 'S': if ( lud.lud_scheme != NULL ) { fprintf( stderr, "scheme already provided\n" ); usage(); } lud.lud_scheme = optarg; break; case 'h': if ( lud.lud_host != NULL ) { fprintf( stderr, "host already provided\n" ); usage(); } lud.lud_host = optarg; break; case 'p': if ( lud.lud_port != -1 ) { fprintf( stderr, "port already provided\n" ); usage(); } if ( lutil_atoi( &lud.lud_port, optarg ) ) { fprintf( stderr, "unable to parse port \"%s\"\n", optarg ); usage(); } break; case 'b': if ( lud.lud_dn != NULL ) { fprintf( stderr, "base already provided\n" ); usage(); } lud.lud_dn = optarg; break; case 'a': if ( lud.lud_attrs != NULL ) { fprintf( stderr, "attrs already provided\n" ); usage(); } lud.lud_attrs = ldap_str2charray( optarg, "," ); if ( lud.lud_attrs == NULL ) { fprintf( stderr, "unable to parse attrs list \"%s\"\n", optarg ); usage(); } break; case 's': if ( lud.lud_scope != -1 ) { fprintf( stderr, "scope already provided\n" ); usage(); } lud.lud_scope = ldap_pvt_str2scope( optarg ); if ( lud.lud_scope == -1 ) { fprintf( stderr, "unable to parse scope \"%s\"\n", optarg ); usage(); } break; case 'f': if ( lud.lud_filter != NULL ) { fprintf( stderr, "filter already provided\n" ); usage(); } lud.lud_filter = optarg; break; case 'E': lud.lud_exts = (char **)realloc( lud.lud_exts, sizeof( char * ) * ( nexts + 2 ) ); lud.lud_exts[ nexts++ ] = optarg; lud.lud_exts[ nexts ] = NULL; break; default: assert( opt != 'H' ); usage(); } } if ( uri != NULL ) { return do_uri_explode( uri ); } return do_uri_create( &lud ); }
int tester_config_opt( struct tester_conn_args *config, char opt, char *optarg ) { switch ( opt ) { case 'C': config->chaserefs++; break; case 'D': config->binddn = strdup( optarg ); break; case 'd': { int debug; if ( lutil_atoi( &debug, optarg ) != 0 ) { return -1; } if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) { fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug ); } if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) { fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug ); } break; } case 'H': config->uri = strdup( optarg ); break; case 'h': config->host = strdup( optarg ); break; case 'i': tester_ignore_str2errlist( optarg ); break; case 'L': if ( lutil_atoi( &config->outerloops, optarg ) != 0 ) { return -1; } break; case 'l': if ( lutil_atoi( &config->loops, optarg ) != 0 ) { return -1; } break; #ifdef HAVE_CYRUS_SASL case 'O': if ( config->secprops != NULL ) { return -1; } if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) { return -1; } config->authmethod = LDAP_AUTH_SASL; config->secprops = ber_strdup( optarg ); break; case 'R': if ( config->realm != NULL ) { return -1; } if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) { return -1; } config->authmethod = LDAP_AUTH_SASL; config->realm = ber_strdup( optarg ); break; case 'U': if ( config->authc_id != NULL ) { return -1; } if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) { return -1; } config->authmethod = LDAP_AUTH_SASL; config->authc_id = ber_strdup( optarg ); break; case 'X': if ( config->authz_id != NULL ) { return -1; } if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) { return -1; } config->authmethod = LDAP_AUTH_SASL; config->authz_id = ber_strdup( optarg ); break; case 'Y': if ( config->mech != NULL ) { return -1; } if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) { return -1; } config->authmethod = LDAP_AUTH_SASL; config->mech = ber_strdup( optarg ); break; #endif case 'p': if ( lutil_atoi( &config->port, optarg ) != 0 ) { return -1; } break; case 'r': if ( lutil_atoi( &config->retries, optarg ) != 0 ) { return -1; } break; case 't': if ( lutil_atoi( &config->delay, optarg ) != 0 ) { return -1; } break; case 'w': config->pass.bv_val = strdup( optarg ); config->pass.bv_len = strlen( optarg ); memset( optarg, '*', config->pass.bv_len ); break; case 'x': if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SIMPLE ) { return -1; } config->authmethod = LDAP_AUTH_SIMPLE; break; default: return -1; } return LDAP_SUCCESS; }
int main( int argc, char **argv ) { int i; char *uri = NULL; char *host = "localhost"; char *dn = NULL; char *base = NULL; char *filter = "(objectClass=person)"; struct berval pass = { 0, NULL }; char *pwattr = NULL; int port = -1; int loops = LOOPS; int outerloops = 1; int force = 0; int chaserefs = 0; int noinit = 1; int delay = 0; /* extra action to do after bind... */ struct berval type[] = { BER_BVC( "tester=" ), BER_BVC( "add=" ), BER_BVC( "bind=" ), BER_BVC( "modify=" ), BER_BVC( "modrdn=" ), BER_BVC( "read=" ), BER_BVC( "search=" ), BER_BVNULL }; LDAPURLDesc *extra_ludp = NULL; tester_init( "slapd-bind", TESTER_BIND ); /* by default, tolerate invalid credentials */ tester_ignore_str2errlist( "INVALID_CREDENTIALS" ); while ( ( i = getopt( argc, argv, "a:B:b:D:Ff:H:h:Ii:L:l:p:t:w:" ) ) != EOF ) { switch ( i ) { case 'a': pwattr = optarg; break; case 'b': /* base DN of a tree of user DNs */ base = optarg; break; case 'B': { int c; for ( c = 0; type[c].bv_val; c++ ) { if ( strncasecmp( optarg, type[c].bv_val, type[c].bv_len ) == 0 ) { break; } } if ( type[c].bv_val == NULL ) { usage( argv[0], 'B' ); } switch ( c ) { case TESTER_TESTER: case TESTER_BIND: /* invalid */ usage( argv[0], 'B' ); case TESTER_SEARCH: { if ( ldap_url_parse( &optarg[type[c].bv_len], &extra_ludp ) != LDAP_URL_SUCCESS ) { usage( argv[0], 'B' ); } } break; case TESTER_ADDEL: case TESTER_MODIFY: case TESTER_MODRDN: case TESTER_READ: /* nothing to do */ break; default: assert( 0 ); } } break; case 'C': chaserefs++; break; case 'H': /* the server uri */ uri = optarg; break; case 'h': /* the servers host */ host = optarg; break; case 'i': tester_ignore_str2errlist( optarg ); break; case 'p': /* the servers port */ if ( lutil_atoi( &port, optarg ) != 0 ) { usage( argv[0], 'p' ); } break; case 'D': dn = optarg; break; case 'w': ber_str2bv( optarg, 0, 1, &pass ); memset( optarg, '*', pass.bv_len ); break; case 'l': /* the number of loops */ if ( lutil_atoi( &loops, optarg ) != 0 ) { usage( argv[0], 'l' ); } break; case 'L': /* the number of outerloops */ if ( lutil_atoi( &outerloops, optarg ) != 0 ) { usage( argv[0], 'L' ); } break; case 'f': filter = optarg; break; case 'F': force++; break; case 'I': /* reuse connection */ noinit = 0; break; case 't': /* sleep between binds */ if ( lutil_atoi( &delay, optarg ) != 0 ) { usage( argv[0], 't' ); } break; default: usage( argv[0], i ); break; } } if ( port == -1 && uri == NULL ) { usage( argv[0], '\0' ); } uri = tester_uri( uri, host, port ); for ( i = 0; i < outerloops; i++ ) { int rc; if ( base != NULL ) { rc = do_base( uri, dn, &pass, base, filter, pwattr, loops, force, chaserefs, noinit, delay, -1, NULL ); } else { rc = do_bind( uri, dn, &pass, loops, force, chaserefs, noinit, NULL, -1, NULL ); } if ( rc == LDAP_SERVER_DOWN ) break; } exit( EXIT_SUCCESS ); }
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 *sbase = NULL; int scope = LDAP_SCOPE_SUBTREE; char *filter = NULL; char *attr = NULL; char *srchattrs[] = { "cn", "sn", NULL }; char **attrs = srchattrs; int loops = LOOPS; int outerloops = 1; int retries = RETRIES; int delay = 0; int force = 0; int chaserefs = 0; int noattrs = 0; int nobind = 0; tester_init( "slapd-search", TESTER_SEARCH ); /* by default, tolerate referrals and no such object */ tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" ); while ( ( i = getopt( argc, argv, "Aa:b:CD:f:FH:h:i:l:L:Np:r:Ss:t:T:w:" ) ) != 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 'p': /* the servers port */ if ( lutil_atoi( &port, optarg ) != 0 ) { usage( argv[0], i ); } 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 'a': attr = strdup( optarg ); break; case 'b': /* file with search base */ sbase = strdup( optarg ); break; case 'f': /* the search request */ filter = strdup( optarg ); break; case 'F': force++; break; case 'l': /* number of loops */ if ( lutil_atoi( &loops, optarg ) != 0 ) { usage( argv[0], i ); } break; case 'L': /* number of loops */ if ( lutil_atoi( &outerloops, optarg ) != 0 ) { usage( argv[0], i ); } break; case 'r': /* number of retries */ if ( lutil_atoi( &retries, optarg ) != 0 ) { usage( argv[0], i ); } break; case 't': /* delay in seconds */ if ( lutil_atoi( &delay, optarg ) != 0 ) { usage( argv[0], i ); } break; case 'T': attrs = ldap_str2charray( optarg, "," ); if ( attrs == NULL ) { usage( argv[0], i ); } break; case 'S': swamp++; break; case 's': scope = ldap_pvt_str2scope( optarg ); if ( scope == -1 ) { usage( argv[0], i ); } break; default: usage( argv[0], i ); break; } } if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL )) usage( argv[0], '\0' ); if ( *filter == '\0' ) { fprintf( stderr, "%s: invalid EMPTY search filter.\n", argv[0] ); exit( EXIT_FAILURE ); } if ( argv[optind] != NULL ) { attrs = &argv[optind]; } uri = tester_uri( uri, host, port ); for ( i = 0; i < outerloops; i++ ) { if ( attr != NULL ) { do_random( uri, manager, &passwd, sbase, scope, filter, attr, attrs, noattrs, nobind, loops, retries, delay, force, chaserefs ); } else { do_search( uri, manager, &passwd, sbase, scope, filter, NULL, attrs, noattrs, nobind, loops, retries, delay, force, chaserefs ); } } exit( EXIT_SUCCESS ); }
/* * Parses a config line and takes actions to fit content in rewrite structure; * lines handled are of the form: * * rewriteEngine {on|off} * rewriteMaxPasses numPasses [numPassesPerRule] * rewriteContext contextName [alias aliasedContextName] * rewriteRule pattern substPattern [ruleFlags] * rewriteMap mapType mapName [mapArgs] * rewriteParam paramName paramValue */ int rewrite_parse( struct rewrite_info *info, const char *fname, int lineno, int argc, char **argv ) { int rc = -1; assert( info != NULL ); assert( fname != NULL ); assert( argv != NULL ); assert( argc > 0 ); /* * Switch on the rewrite engine */ if ( strcasecmp( argv[ 0 ], "rewriteEngine" ) == 0 ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteEngine needs 'state'\n%s", fname, lineno, "" ); return -1; } else if ( argc > 2 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields in rewriteEngine" " will be discarded\n%s", fname, lineno, "" ); } if ( strcasecmp( argv[ 1 ], "on" ) == 0 ) { info->li_state = REWRITE_ON; } else if ( strcasecmp( argv[ 1 ], "off" ) == 0 ) { info->li_state = REWRITE_OFF; } else { Debug( LDAP_DEBUG_ANY, "[%s:%d] unknown 'state' in rewriteEngine;" " assuming 'on'\n%s", fname, lineno, "" ); info->li_state = REWRITE_ON; } rc = REWRITE_SUCCESS; /* * Alter max passes */ } else if ( strcasecmp( argv[ 0 ], "rewriteMaxPasses" ) == 0 ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteMaxPasses needs 'value'\n%s", fname, lineno, "" ); return -1; } if ( lutil_atoi( &info->li_max_passes, argv[ 1 ] ) != 0 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] unable to parse rewriteMaxPasses=\"%s\"\n", fname, lineno, argv[ 1 ] ); return -1; } if ( info->li_max_passes <= 0 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] negative or null rewriteMaxPasses\n", fname, lineno, 0 ); return -1; } if ( argc > 2 ) { if ( lutil_atoi( &info->li_max_passes_per_rule, argv[ 2 ] ) != 0 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] unable to parse rewriteMaxPassesPerRule=\"%s\"\n", fname, lineno, argv[ 2 ] ); return -1; } if ( info->li_max_passes_per_rule <= 0 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] negative or null rewriteMaxPassesPerRule\n", fname, lineno, 0 ); return -1; } } else { info->li_max_passes_per_rule = info->li_max_passes; } rc = REWRITE_SUCCESS; /* * Start a new rewrite context and set current context */ } else if ( strcasecmp( argv[ 0 ], "rewriteContext" ) == 0 ) { if ( argc < 2 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteContext needs 'name'\n%s", fname, lineno, "" ); return -1; } /* * Checks for existence (lots of contexts should be * available by default ...) */ rewrite_int_curr_context = rewrite_context_find( info, argv[ 1 ] ); if ( rewrite_int_curr_context == NULL ) { rewrite_int_curr_context = rewrite_context_create( info, argv[ 1 ] ); } if ( rewrite_int_curr_context == NULL ) { return -1; } if ( argc > 2 ) { /* * A context can alias another (e.g., the `builtin' * contexts for backend operations, if not defined, * alias the `default' rewrite context (with the * notable exception of the searchResult context, * which can be undefined) */ if ( strcasecmp( argv[ 2 ], "alias" ) == 0 ) { struct rewrite_context *aliased; if ( argc == 3 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteContext" " needs 'name' after" " 'alias'\n%s", fname, lineno, "" ); return -1; } else if ( argc > 4 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields in" " rewriteContext" " after aliased name" " will be" " discarded\n%s", fname, lineno, "" ); } aliased = rewrite_context_find( info, argv[ 3 ] ); if ( aliased == NULL ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] aliased" " rewriteContext '%s'" " does not exists\n", fname, lineno, argv[ 3 ] ); return -1; } rewrite_int_curr_context->lc_alias = aliased; rewrite_int_curr_context = aliased; } else { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields" " in rewriteContext" " will be discarded\n%s", fname, lineno, "" ); } } rc = REWRITE_SUCCESS; /* * Compile a rule in current context */ } else if ( strcasecmp( argv[ 0 ], "rewriteRule" ) == 0 ) { if ( argc < 3 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteRule needs 'pattern'" " 'subst' ['flags']\n%s", fname, lineno, "" ); return -1; } else if ( argc > 4 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields in rewriteRule" " will be discarded\n%s", fname, lineno, "" ); } if ( rewrite_int_curr_context == NULL ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteRule outside a" " context; will add to default\n%s", fname, lineno, "" ); rewrite_int_curr_context = rewrite_context_find( info, REWRITE_DEFAULT_CONTEXT ); /* * Default context MUST exist in a properly initialized * struct rewrite_info */ assert( rewrite_int_curr_context != NULL ); } rc = rewrite_rule_compile( info, rewrite_int_curr_context, argv[ 1 ], argv[ 2 ], ( argc == 4 ? argv[ 3 ] : "" ) ); /* * Add a plugin map to the map tree */ } else if ( strcasecmp( argv[ 0 ], "rewriteMap" ) == 0 ) { if ( argc < 3 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteMap needs at least 'type'" " and 'name' ['args']\n%s", fname, lineno, "" ); return -1; } rc = rewrite_parse_builtin_map( info, fname, lineno, argc, argv ); /* * Set the value of a global scope parameter */ } else if ( strcasecmp( argv[ 0 ], "rewriteParam" ) == 0 ) { if ( argc < 3 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] rewriteParam needs 'name'" " and 'value'\n%s", fname, lineno, "" ); return -1; } rc = rewrite_param_set( info, argv[ 1 ], argv[ 2 ] ); /* * Error */ } else { Debug( LDAP_DEBUG_ANY, "[%s:%d] unknown command '%s'\n", fname, lineno, "" ); return -1; } 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 *entry = NULL; char *filter = NULL; int loops = LOOPS; int outerloops = 1; int retries = RETRIES; int delay = 0; int force = 0; int chaserefs = 0; char *srchattrs[] = { "1.1", NULL }; char **attrs = srchattrs; int noattrs = 0; int nobind = 0; tester_init( "slapd-read", TESTER_READ ); /* by default, tolerate referrals and no such object */ tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" ); while ( (i = getopt( argc, argv, "ACD:e:Ff:H:h:i:L:l:p:r:St:T:w:" )) != 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 '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 '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 'r': /* the number of retries */ if ( lutil_atoi( &retries, optarg ) != 0 ) { usage( argv[0] ); } break; case 'S': swamp++; 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]; } uri = tester_uri( uri, host, port ); for ( i = 0; i < outerloops; i++ ) { if ( filter != NULL ) { do_random( uri, manager, &passwd, entry, filter, attrs, noattrs, nobind, loops, retries, delay, force, chaserefs ); } else { do_read( uri, manager, &passwd, entry, NULL, attrs, noattrs, nobind, loops, retries, delay, force, chaserefs ); } } exit( EXIT_SUCCESS ); }
int quorum_config(ConfigArgs *c) { int i, type; slap_quorum_t *q = c->be->bd_quorum; assert(quorum_list != QR_POISON); if (c->op == SLAP_CONFIG_EMIT) { if (q) { if ((q->flags & QR_ALL_LINKS) != 0 && value_add_one_str(&c->rvalue_vals, "all-links")) return 1; if ((q->flags & QR_AUTO_SIDS) != 0 && value_add_one_str(&c->rvalue_vals, "auto-sids")) return 1; if ((q->flags & QR_AUTO_RIDS) != 0 && value_add_one_str(&c->rvalue_vals, "auto-rids")) return 1; if (q->qr_requirements && (unparse(&c->rvalue_vals, q, QR_DEMAND_RID, "require-rids") || unparse(&c->rvalue_vals, q, QR_DEMAND_SID, "require-sids") || unparse(&c->rvalue_vals, q, QR_VOTED_RID, "vote-rids") || unparse(&c->rvalue_vals, q, QR_VOTED_SID, "vote-sids"))) return 1; if (q->qr_syncrepl_limit > 0 && (value_add_one_str(&c->rvalue_vals, "limit-concurrent-refresh") || value_add_one_int(&c->rvalue_vals, q->qr_syncrepl_limit))) return 1; } return 0; } else if ( c->op == LDAP_MOD_DELETE ) { lock(); if (q) { q->flags = 0; if(q->qr_requirements) { ch_free(q->qr_requirements); q->qr_requirements = NULL; c->be->bd_quorum_cache = 1; Debug( LDAP_DEBUG_SYNC, "syncrep_quorum: %s requirements-list cleared\n", q->qr_cluster ); } if (q->qr_syncrepl_limit) { Debug( LDAP_DEBUG_SYNC, "syncrep_quorum: %s limit-concurrent-refresh cleared\n", q->qr_cluster ); q->qr_syncrepl_limit = 0; } } unlock(); return 0; } lock(); if (! q) { quorum_be_init(c->be); q = c->be->bd_quorum; } type = -1; for( i = 1; i < c->argc; i++ ) { int id; if (strcasecmp(c->argv[i], "all-links") == 0) { type = -1; if (! (q->flags & QR_ALL_LINKS)) { q->flags |= QR_ALL_LINKS; quorum_invalidate(c->be); } } else if (strcasecmp(c->argv[i], "auto-sids") == 0) { type = -1; if (require_auto_sids(q)) quorum_invalidate(c->be); } else if (strcasecmp(c->argv[i], "auto-rids") == 0) { type = -1; if (require_auto_rids(q)) quorum_invalidate(c->be); } else if (strcasecmp(c->argv[i], "vote-sids") == 0) type = QR_VOTED_SID; else if (strcasecmp(c->argv[i], "vote-rids") == 0) type = QR_VOTED_RID; else if (strcasecmp(c->argv[i], "require-sids") == 0) type = QR_DEMAND_SID; else if (strcasecmp(c->argv[i], "require-rids") == 0) type = QR_DEMAND_RID; else if (strcasecmp(c->argv[i], "limit-concurrent-refresh") == 0) { if (q->qr_syncrepl_limit) { snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> limit-concurrent-refresh was already defined for %s as %d", c->argv[i], q->qr_cluster, q->qr_syncrepl_limit ); unlock(); Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg ); return 1; } #define QR_FAKETYPE_max_concurrent_refresh -2 type = QR_FAKETYPE_max_concurrent_refresh; q->qr_syncrepl_limit = 1; } else if (type == QR_FAKETYPE_max_concurrent_refresh) { int n; if ( lutil_atoi( &n, c->argv[i]) || n < 1 || n > QR_SYNCREPL_MAX ) { snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> illegal limit-concurrent-refresh for %s (maximum is %d)", c->argv[i], q->qr_cluster, QR_SYNCREPL_MAX ); unlock(); Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg ); return 1; } q->qr_syncrepl_limit = n; type = -1; } else if (type < 0) { snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> mode-key must precede an ID for %s quorum-requirements", c->argv[i], q->qr_cluster ); unlock(); Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg ); return 1; } else if (( lutil_atoi( &id, c->argv[i] ) && lutil_atoix( &id, c->argv[i], 16 )) || id < 0 || id > (QR_IS_SID(type) ? SLAP_SYNC_SID_MAX : SLAP_SYNC_RID_MAX) ) { snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> illegal %s-ID or mode-key for %s quorum-requirements", c->argv[i], QR_IS_SID(type) ? "Server" : "Repl", q->qr_cluster ); unlock(); Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg ); return 1; } else { if (! require_append( q, type, id )) { snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> %s-ID already present in %s quorum-requirements", c->argv[i], QR_IS_SID(type) ? "Server" : "Repl", q->qr_cluster ); unlock(); Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg ); return 1; } quorum_invalidate(c->be); } } unlock(); return 0; }
int main( int argc, char *argv[] ) { FILE *fin = NULL; char *rewriteContext = REWRITE_DEFAULT_CONTEXT; int debug = 0; while ( 1 ) { int opt = getopt( argc, argv, "d:f:hr:" ); if ( opt == EOF ) { break; } switch ( opt ) { case 'd': if ( lutil_atoi( &debug, optarg ) != 0 ) { fprintf( stderr, "illegal log level '%s'\n", optarg ); exit( EXIT_FAILURE ); } break; case 'f': fin = fopen( optarg, "r" ); if ( fin == NULL ) { fprintf( stderr, "unable to open file '%s'\n", optarg ); exit( EXIT_FAILURE ); } break; case 'h': fprintf( stderr, "usage: rewrite [options] string\n" "\n" "\t\t-f file\t\tconfiguration file\n" "\t\t-r rule[s]\tlist of comma-separated rules\n" "\n" "\tsyntax:\n" "\t\trewriteEngine\t{on|off}\n" "\t\trewriteContext\tcontextName [alias aliasedContextName]\n" "\t\trewriteRule\tpattern subst [flags]\n" "\n" ); exit( EXIT_SUCCESS ); case 'r': rewriteContext = optarg; break; } } if ( debug != 0 ) { ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug); ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug); } if ( optind >= argc ) { return -1; } apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] ); if ( fin ) { fclose( fin ); } return 0; }
int parse_input( FILE *ifp, FILE *ofp, struct ldop *op ) { char *p, *args, line[ MAXLINELEN + 1 ]; struct inputparams *ip; if ( fgets( line, MAXLINELEN, ifp ) == NULL ) { write_result( ofp, LDAP_OTHER, NULL, "Empty Input" ); } line[ strlen( line ) - 1 ] = '\0'; if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 ) != 0 ) { write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL, "Operation Not Supported" ); return( -1 ); } op->ldop_op = LDOP_SEARCH; while ( fgets( line, MAXLINELEN, ifp ) != NULL ) { line[ strlen( line ) - 1 ] = '\0'; debug_printf( "<< %s\n", line ); args = line; if (( ip = find_input_tag( &args )) == NULL ) { debug_printf( "ignoring %s\n", line ); continue; } switch( ip->ip_type ) { case IP_TYPE_SUFFIX: add_strval( &op->ldop_suffixes, args ); break; case IP_TYPE_BASE: op->ldop_dn = estrdup( args ); break; case IP_TYPE_SCOPE: if ( lutil_atoi( &op->ldop_srch.ldsp_scope, args ) != 0 || ( op->ldop_srch.ldsp_scope != LDAP_SCOPE_BASE && op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL && op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) ) { write_result( ofp, LDAP_OTHER, NULL, "Bad scope" ); return( -1 ); } break; case IP_TYPE_ALIASDEREF: if ( lutil_atoi( &op->ldop_srch.ldsp_aliasderef, args ) != 0 ) { write_result( ofp, LDAP_OTHER, NULL, "Bad alias deref" ); return( -1 ); } break; case IP_TYPE_SIZELIMIT: if ( lutil_atoi( &op->ldop_srch.ldsp_sizelimit, args ) != 0 ) { write_result( ofp, LDAP_OTHER, NULL, "Bad size limit" ); return( -1 ); } break; case IP_TYPE_TIMELIMIT: if ( lutil_atoi( &op->ldop_srch.ldsp_timelimit, args ) != 0 ) { write_result( ofp, LDAP_OTHER, NULL, "Bad time limit" ); return( -1 ); } break; case IP_TYPE_FILTER: op->ldop_srch.ldsp_filter = estrdup( args ); break; case IP_TYPE_ATTRSONLY: op->ldop_srch.ldsp_attrsonly = ( *args != '0' ); break; case IP_TYPE_ATTRS: if ( strcmp( args, "all" ) == 0 ) { op->ldop_srch.ldsp_attrs = NULL; } else { while ( args != NULL ) { if (( p = strchr( args, ' ' )) != NULL ) { *p++ = '\0'; while ( isspace( (unsigned char) *p )) { ++p; } } add_strval( &op->ldop_srch.ldsp_attrs, args ); args = p; } } break; } } if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL || op->ldop_srch.ldsp_filter == NULL ) { write_result( ofp, LDAP_OTHER, NULL, "Required suffix:, base:, or filter: missing" ); return( -1 ); } return( 0 ); }
int main( int argc, char **argv ) { int i; while ( (i = getopt( argc, argv, "b:D:H:w:f:n:i:t:r:R:" )) != EOF ) { switch( i ) { case 'b': /* base DN of a tree of user DNs */ base = strdup( optarg ); break; case 'D': binder = strdup( optarg ); break; case 'H': /* the server uri */ uri = strdup( optarg ); break; case 'w': pass = strdup( optarg ); break; case 't': /* the duration to run */ if ( lutil_atoi( &tdur, optarg ) != 0 ) { usage( argv[0] ); } break; case 'i': /* the time interval */ if ( lutil_atoi( &interval, optarg ) != 0 ) { usage( argv[0] ); } break; case 'r': /* the uid range */ if ( sscanf(optarg, "%d:%d", &r1lo, &r1hi) != 2 ) { usage( argv[0] ); } break; case 'R': /* percentage:2nd uid range */ if ( sscanf(optarg, "%d:%d:%d", &r2per, &r2lo, &r2hi) != 3 ) { usage( argv[0] ); } break; case 'f': filter = optarg; break; case 'n': if ( lutil_atoi( &threads, optarg ) != 0 || threads < 1 ) { usage( argv[0] ); } break; default: usage( argv[0] ); break; } } if ( tdur == 0 || r1hi <= r1lo ) usage( argv[0] ); r1per = 100 - r2per; if ( r1per < 1 ) usage( argv[0] ); r1binds = calloc( threads*4, sizeof( int )); r2binds = r1binds + threads; r1old = (int *)r2binds + threads; r2old = r1old + threads; do_time( ); exit( EXIT_SUCCESS ); }
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 *entry = NULL; char *ava = NULL; char *value = NULL; int loops = LOOPS; int outerloops = 1; int retries = RETRIES; int delay = 0; int friendly = 0; int chaserefs = 0; tester_init( "slapd-modify", TESTER_MODIFY ); while ( ( i = getopt( argc, argv, "a:CD:e:FH:h:i:L:l:p:r:t:w:" ) ) != EOF ) { switch ( i ) { case 'C': chaserefs++; break; case 'F': friendly++; break; case 'H': /* the server uri */ uri = strdup( optarg ); break; case 'h': /* the servers host */ host = strdup( optarg ); break; case 'i': /* ignored (!) by now */ 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 'e': /* entry to modify */ entry = strdup( optarg ); break; case 'a': ava = strdup( optarg ); 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 'r': /* 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; default: usage( argv[0] ); break; } } if (( entry == NULL ) || ( ava == 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 ( *ava == '\0' ) { fprintf( stderr, "%s: invalid EMPTY AVA.\n", argv[0] ); exit( EXIT_FAILURE ); } if ( !( value = strchr( ava, ':' ))) { fprintf( stderr, "%s: invalid AVA.\n", argv[0] ); exit( EXIT_FAILURE ); } *value++ = '\0'; while ( *value && isspace( (unsigned char) *value )) value++; uri = tester_uri( uri, host, port ); for ( i = 0; i < outerloops; i++ ) { do_modify( uri, manager, &passwd, entry, ava, value, loops, retries, delay, friendly, chaserefs ); } exit( EXIT_SUCCESS ); }
int main( int argc, char **argv ) { int i; char *host = "localhost"; char *uri = NULL; int port = -1; char *manager = NULL; struct berval passwd = { 0, NULL }; char *filename = NULL; char *entry = NULL; int loops = LOOPS; int outerloops = 1; int retries = RETRIES; int delay = 0; int friendly = 0; int chaserefs = 0; LDAPMod **attrs = NULL; tester_init( "slapd-addel", TESTER_ADDEL ); while ( ( i = getopt( argc, argv, "CD:Ff:H:h:i:L:l:p:r:t:w:" ) ) != EOF ) { switch ( i ) { case 'C': chaserefs++; break; case 'F': friendly++; break; case 'H': /* the server's URI */ uri = strdup( optarg ); break; case 'h': /* the servers host */ host = strdup( optarg ); break; case 'i': /* ignored (!) by now */ 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 'f': /* file with entry search request */ filename = strdup( optarg ); 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 'r': /* 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; default: usage( argv[0] ); break; } } if (( filename == NULL ) || ( port == -1 && uri == NULL ) || ( manager == NULL ) || ( passwd.bv_val == NULL )) usage( argv[0] ); entry = get_add_entry( filename, &attrs ); if (( entry == NULL ) || ( *entry == '\0' )) { fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n", argv[0], filename ); exit( EXIT_FAILURE ); } if (( attrs == NULL ) || ( *attrs == '\0' )) { fprintf( stderr, "%s: invalid attrs in file \"%s\".\n", argv[0], filename ); exit( EXIT_FAILURE ); } uri = tester_uri( uri, host, port ); for ( i = 0; i < outerloops; i++ ) { do_addel( uri, manager, &passwd, entry, attrs, loops, retries, delay, friendly, chaserefs ); } exit( EXIT_SUCCESS ); }
static void * map_ldap_parse( const char *fname, int lineno, int argc, char **argv ) { struct ldap_map_data *data; char *p, *uri; assert( fname != NULL ); assert( argv != NULL ); data = calloc( sizeof( struct ldap_map_data ), 1 ); if ( data == NULL ) { return NULL; } if ( argc < 1 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] ldap map needs URI\n%s", fname, lineno, "" ); free( data ); return NULL; } uri = argv[ 0 ]; if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) { uri += STRLENOF( "uri=" ); } data->lm_url = strdup( uri ); if ( data->lm_url == NULL ) { map_ldap_free( data ); return NULL; } if ( ldap_url_parse( uri, &data->lm_lud ) != REWRITE_SUCCESS ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] illegal URI '%s'\n", fname, lineno, argv[ 0 ] ); map_ldap_free( data ); return NULL; } /* trim everything after [host][:port] */ p = strchr( data->lm_url, '/' ); assert( p[ 1 ] == '/' ); if ( ( p = strchr( p + 2, '/' ) ) != NULL ) { p[ 0 ] = '\0'; } if ( data->lm_lud->lud_attrs == NULL ) { data->lm_attrs[ 0 ] = LDAP_NO_ATTRS; data->lm_wantdn = 1; } else { if ( data->lm_lud->lud_attrs[ 1 ] != NULL ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] only one attribute allowed in URI\n", fname, lineno, 0 ); map_ldap_free( data ); return NULL; } if ( strcasecmp( data->lm_lud->lud_attrs[ 0 ], "dn" ) == 0 || strcasecmp( data->lm_lud->lud_attrs[ 0 ], "entryDN" ) == 0 ) { ldap_memfree( data->lm_lud->lud_attrs[ 0 ] ); ldap_memfree( data->lm_lud->lud_attrs ); data->lm_lud->lud_attrs = NULL; data->lm_attrs[ 0 ] = LDAP_NO_ATTRS; data->lm_wantdn = 1; } else { data->lm_attrs[ 0 ] = data->lm_lud->lud_attrs[ 0 ]; } } data->lm_attrs[ 1 ] = NULL; /* safe defaults */ data->lm_version = LDAP_VERSION3; for ( argc--, argv++; argc > 0; argc--, argv++ ) { if ( strncasecmp( argv[ 0 ], "binddn=", STRLENOF( "binddn=" ) ) == 0 ) { char *p = argv[ 0 ] + STRLENOF( "binddn=" ); int l; if ( p[ 0 ] == '\"' || p [ 0 ] == '\'' ) { l = strlen( p ) - 2; p++; if ( p[ l ] != p[ 0 ] ) { map_ldap_free( data ); return NULL; } } else { l = strlen( p ); } data->lm_binddn = strdup( p ); if ( data->lm_binddn == NULL ) { map_ldap_free( data ); return NULL; } if ( data->lm_binddn[ l ] == '\"' || data->lm_binddn[ l ] == '\'' ) { data->lm_binddn[ l ] = '\0'; } /* deprecated */ } else if ( strncasecmp( argv[ 0 ], "bindpw=", STRLENOF( "bindpw=" ) ) == 0 ) { ber_str2bv( argv[ 0 ] + STRLENOF( "bindpw=" ), 0, 1, &data->lm_cred ); if ( data->lm_cred.bv_val == NULL ) { map_ldap_free( data ); return NULL; } } else if ( strncasecmp( argv[ 0 ], "credentials=", STRLENOF( "credentials=" ) ) == 0 ) { ber_str2bv( argv[ 0 ] + STRLENOF( "credentials=" ), 0, 1, &data->lm_cred ); if ( data->lm_cred.bv_val == NULL ) { map_ldap_free( data ); return NULL; } } else if ( strncasecmp( argv[ 0 ], "bindwhen=", STRLENOF( "bindwhen=" ) ) == 0 ) { char *p = argv[ 0 ] + STRLENOF( "bindwhen=" ); if ( strcasecmp( p, "now" ) == 0 ) { int rc; data->lm_when = MAP_LDAP_NOW; /* * Init LDAP handler ... */ rc = ldap_initialize( &data->lm_ld, data->lm_url ); if ( rc != LDAP_SUCCESS ) { map_ldap_free( data ); return NULL; } ldap_set_option( data->lm_ld, LDAP_OPT_PROTOCOL_VERSION, (void *)&data->lm_version ); #ifdef USE_REWRITE_LDAP_PVT_THREADS ldap_pvt_thread_mutex_init( &data->lm_mutex ); #endif /* USE_REWRITE_LDAP_PVT_THREADS */ } else if ( strcasecmp( p, "later" ) == 0 ) { data->lm_when = MAP_LDAP_LATER; #ifdef USE_REWRITE_LDAP_PVT_THREADS ldap_pvt_thread_mutex_init( &data->lm_mutex ); #endif /* USE_REWRITE_LDAP_PVT_THREADS */ } else if ( strcasecmp( p, "everytime" ) == 0 ) { data->lm_when = MAP_LDAP_EVERYTIME; } else { /* ignore ... */ } } else if ( strncasecmp( argv[ 0 ], "version=", STRLENOF( "version=" ) ) == 0 ) { if ( lutil_atoi( &data->lm_version, argv[ 0 ] + STRLENOF( "version=" ) ) ) { map_ldap_free( data ); return NULL; } switch ( data->lm_version ) { case LDAP_VERSION2: case LDAP_VERSION3: break; default: Debug( LDAP_DEBUG_ANY, "[%s:%d] unknown version %s\n", fname, lineno, p ); map_ldap_free( data ); return NULL; } } else { Debug( LDAP_DEBUG_ANY, "[%s:%d] unknown option %s (ignored)\n", fname, lineno, argv[0] ); } } if ( data->lm_when == MAP_LDAP_UNKNOWN ) { data->lm_when = MAP_LDAP_EVERYTIME; } return ( void * )data; }
int main( int argc, char **argv ) { int i, j; char *uri = NULL; char *host = "localhost"; char *port = NULL; char *manager = NULL; char *passwd = NULL; char *dirname = NULL; char *progdir = NULL; int loops = LOOPS; char *outerloops = OUTERLOOPS; char *retries = RETRIES; char *delay = "0"; DIR *datadir; struct dirent *file; int friendly = 0; int chaserefs = 0; int noattrs = 0; int nobind = 0; int noinit = 1; char *ignore = NULL; /* search */ char *sfile = NULL; char *sreqs[MAXREQS]; char *sattrs[MAXREQS]; char *sbase[MAXREQS]; LDAPURLDesc *slud[MAXREQS]; int snum = 0; char *sargs[MAXARGS]; int sanum; int sextra_args = 0; char scmd[MAXPATHLEN]; int swamp = 0; char swampopt[sizeof("-SSS")]; /* static so that its address can be used in initializer below. */ static char sloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* read */ char *rfile = NULL; char *rreqs[MAXREQS]; int rnum = 0; char *rargs[MAXARGS]; char *rflts[MAXREQS]; int ranum; int rextra_args = 0; char rcmd[MAXPATHLEN]; static char rloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* addel */ char *afiles[MAXREQS]; int anum = 0; char *aargs[MAXARGS]; int aanum; char acmd[MAXPATHLEN]; static char aloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* modrdn */ char *nfile = NULL; char *nreqs[MAXREQS]; int nnum = 0; char *nargs[MAXARGS]; int nanum; char ncmd[MAXPATHLEN]; static char nloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* modify */ char *mfile = NULL; char *mreqs[MAXREQS]; char *mdn[MAXREQS]; int mnum = 0; char *margs[MAXARGS]; int manum; char mcmd[MAXPATHLEN]; static char mloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* bind */ char *bfile = NULL; char *breqs[MAXREQS]; char *bcreds[MAXREQS]; char *battrs[MAXREQS]; int bnum = 0; char *bargs[MAXARGS]; int banum; char bcmd[MAXPATHLEN]; static char bloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; char **bargs_extra = NULL; char *friendlyOpt = NULL; int pw_ask = 0; char *pw_file = NULL; /* extra action to do after bind... */ typedef struct extra_t { char *action; struct extra_t *next; } extra_t; extra_t *extra = NULL; int nextra = 0; tester_init( "slapd-tester", TESTER_TESTER ); sloops[0] = '\0'; rloops[0] = '\0'; aloops[0] = '\0'; nloops[0] = '\0'; mloops[0] = '\0'; bloops[0] = '\0'; while ( ( i = getopt( argc, argv, "AB:CD:d:FH:h:Ii:j:L:l:NP:p:r:St:Ww:y:" ) ) != EOF ) { switch ( i ) { case 'A': noattrs++; break; case 'B': { char **p, **b = ldap_str2charray( optarg, "," ); extra_t **epp; for ( epp = &extra; *epp; epp = &(*epp)->next ) ; for ( p = b; p[0]; p++ ) { *epp = calloc( 1, sizeof( extra_t ) ); (*epp)->action = p[0]; epp = &(*epp)->next; nextra++; } ldap_memfree( b ); } break; case 'C': chaserefs++; break; case 'D': /* slapd manager */ manager = ArgDup( optarg ); break; case 'd': /* data directory */ dirname = strdup( optarg ); break; case 'F': friendly++; break; case 'H': /* slapd uri */ uri = strdup( optarg ); break; case 'h': /* slapd host */ host = strdup( optarg ); break; case 'I': noinit = 0; break; case 'i': ignore = optarg; break; case 'j': /* the number of parallel clients */ if ( lutil_atoi( &maxkids, optarg ) != 0 ) { usage( argv[0], 'j' ); } break; case 'l': /* the number of loops per client */ if ( !isdigit( (unsigned char) optarg[0] ) ) { char **p, **l = ldap_str2charray( optarg, "," ); for ( p = l; p[0]; p++) { struct { struct berval type; char *buf; } types[] = { { BER_BVC( "add=" ), aloops }, { BER_BVC( "bind=" ), bloops }, { BER_BVC( "modify=" ), mloops }, { BER_BVC( "modrdn=" ), nloops }, { BER_BVC( "read=" ), rloops }, { BER_BVC( "search=" ), sloops }, { BER_BVNULL, NULL } }; int c, n; for ( c = 0; types[c].type.bv_val; c++ ) { if ( strncasecmp( p[0], types[c].type.bv_val, types[c].type.bv_len ) == 0 ) { break; } } if ( types[c].type.bv_val == NULL ) { usage( argv[0], 'l' ); } if ( lutil_atoi( &n, &p[0][types[c].type.bv_len] ) != 0 ) { usage( argv[0], 'l' ); } snprintf( types[c].buf, sizeof( aloops ), "%d", n ); } ldap_charray_free( l ); } else if ( lutil_atoi( &loops, optarg ) != 0 ) { usage( argv[0], 'l' ); } break; case 'L': /* the number of outerloops per client */ outerloops = strdup( optarg ); break; case 'N': nobind++; break; case 'P': /* prog directory */ progdir = strdup( optarg ); break; case 'p': /* the servers port number */ port = strdup( optarg ); break; case 'r': /* the number of retries in case of error */ retries = strdup( optarg ); break; case 'S': swamp++; break; case 't': /* the delay in seconds between each retry */ delay = strdup( optarg ); break; case 'w': /* the managers passwd */ passwd = ArgDup( optarg ); memset( optarg, '*', strlen( optarg ) ); break; case 'W': pw_ask++; break; case 'y': pw_file = optarg; break; default: usage( argv[0], '\0' ); break; } } if (( dirname == NULL ) || ( port == NULL && uri == NULL ) || ( manager == NULL ) || ( passwd == NULL ) || ( progdir == NULL )) { usage( argv[0], '\0' ); } #ifdef HAVE_WINSOCK children = malloc( maxkids * sizeof(HANDLE) ); #endif /* get the file list */ if ( ( datadir = opendir( dirname )) == NULL ) { fprintf( stderr, "%s: couldn't open data directory \"%s\".\n", argv[0], dirname ); exit( EXIT_FAILURE ); } /* look for search, read, modrdn, and add/delete files */ for ( file = readdir( datadir ); file; file = readdir( datadir )) { if ( !strcasecmp( file->d_name, TSEARCHFILE )) { sfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TREADFILE )) { rfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TMODRDNFILE )) { nfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TMODIFYFILE )) { mfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strncasecmp( file->d_name, TADDFILE, strlen( TADDFILE )) && ( anum < MAXREQS )) { afiles[anum++] = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TBINDFILE )) { bfile = get_file_name( dirname, file->d_name ); continue; } } closedir( datadir ); if ( pw_ask ) { passwd = getpassphrase( _("Enter LDAP Password: "******"no data files found.\n" ); exit( EXIT_FAILURE ); } /* look for search requests */ if ( sfile ) { snum = get_search_filters( sfile, sreqs, sattrs, sbase, slud ); if ( snum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", sfile, -2*(snum + 1)); exit( EXIT_FAILURE ); } } /* look for read requests */ if ( rfile ) { rnum = get_read_entries( rfile, rreqs, rflts ); if ( rnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", rfile, -2*(rnum + 1) ); exit( EXIT_FAILURE ); } } /* look for modrdn requests */ if ( nfile ) { nnum = get_read_entries( nfile, nreqs, NULL ); if ( nnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", nfile, -2*(nnum + 1) ); exit( EXIT_FAILURE ); } } /* look for modify requests */ if ( mfile ) { mnum = get_search_filters( mfile, mreqs, NULL, mdn, NULL ); if ( mnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", mfile, -2*(mnum + 1) ); exit( EXIT_FAILURE ); } } /* look for bind requests */ if ( bfile ) { bnum = get_search_filters( bfile, bcreds, battrs, breqs, NULL ); if ( bnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", bfile, -2*(bnum + 1) ); exit( EXIT_FAILURE ); } } /* setup friendly option */ switch ( friendly ) { case 0: break; case 1: friendlyOpt = "-F"; break; default: /* NOTE: right now we don't need it more than twice */ case 2: friendlyOpt = "-FF"; break; } /* setup swamp option */ if ( swamp ) { swampopt[0] = '-'; if ( swamp > 3 ) swamp = 3; swampopt[swamp + 1] = '\0'; for ( ; swamp-- > 0; ) swampopt[swamp + 1] = 'S'; } /* setup loop options */ if ( sloops[0] == '\0' ) snprintf( sloops, sizeof( sloops ), "%d", 10 * loops ); if ( rloops[0] == '\0' ) snprintf( rloops, sizeof( rloops ), "%d", 20 * loops ); if ( aloops[0] == '\0' ) snprintf( aloops, sizeof( aloops ), "%d", loops ); if ( nloops[0] == '\0' ) snprintf( nloops, sizeof( nloops ), "%d", loops ); if ( mloops[0] == '\0' ) snprintf( mloops, sizeof( mloops ), "%d", loops ); if ( bloops[0] == '\0' ) snprintf( bloops, sizeof( bloops ), "%d", 20 * loops ); /* * generate the search clients */ sanum = 0; snprintf( scmd, sizeof scmd, "%s" LDAP_DIRSEP SEARCHCMD, progdir ); sargs[sanum++] = scmd; if ( uri ) { sargs[sanum++] = "-H"; sargs[sanum++] = uri; } else { sargs[sanum++] = "-h"; sargs[sanum++] = host; sargs[sanum++] = "-p"; sargs[sanum++] = port; } sargs[sanum++] = "-D"; sargs[sanum++] = manager; sargs[sanum++] = "-w"; sargs[sanum++] = passwd; sargs[sanum++] = "-l"; sargs[sanum++] = sloops; sargs[sanum++] = "-L"; sargs[sanum++] = outerloops; sargs[sanum++] = "-r"; sargs[sanum++] = retries; sargs[sanum++] = "-t"; sargs[sanum++] = delay; if ( friendly ) { sargs[sanum++] = friendlyOpt; } if ( chaserefs ) { sargs[sanum++] = "-C"; } if ( noattrs ) { sargs[sanum++] = "-A"; } if ( nobind ) { sargs[sanum++] = "-N"; } if ( ignore ) { sargs[sanum++] = "-i"; sargs[sanum++] = ignore; } if ( swamp ) { sargs[sanum++] = swampopt; } sargs[sanum++] = "-b"; sargs[sanum++] = NULL; /* will hold the search base */ sargs[sanum++] = "-s"; sargs[sanum++] = NULL; /* will hold the search scope */ sargs[sanum++] = "-f"; sargs[sanum++] = NULL; /* will hold the search request */ sargs[sanum++] = NULL; sargs[sanum++] = NULL; /* might hold the "attr" request */ sextra_args += 2; sargs[sanum] = NULL; /* * generate the read clients */ ranum = 0; snprintf( rcmd, sizeof rcmd, "%s" LDAP_DIRSEP READCMD, progdir ); rargs[ranum++] = rcmd; if ( uri ) { rargs[ranum++] = "-H"; rargs[ranum++] = uri; } else { rargs[ranum++] = "-h"; rargs[ranum++] = host; rargs[ranum++] = "-p"; rargs[ranum++] = port; } rargs[ranum++] = "-D"; rargs[ranum++] = manager; rargs[ranum++] = "-w"; rargs[ranum++] = passwd; rargs[ranum++] = "-l"; rargs[ranum++] = rloops; rargs[ranum++] = "-L"; rargs[ranum++] = outerloops; rargs[ranum++] = "-r"; rargs[ranum++] = retries; rargs[ranum++] = "-t"; rargs[ranum++] = delay; if ( friendly ) { rargs[ranum++] = friendlyOpt; } if ( chaserefs ) { rargs[ranum++] = "-C"; } if ( noattrs ) { rargs[ranum++] = "-A"; } if ( ignore ) { rargs[ranum++] = "-i"; rargs[ranum++] = ignore; } if ( swamp ) { rargs[ranum++] = swampopt; } rargs[ranum++] = "-e"; rargs[ranum++] = NULL; /* will hold the read entry */ rargs[ranum++] = NULL; rargs[ranum++] = NULL; /* might hold the filter arg */ rextra_args += 2; rargs[ranum] = NULL; /* * generate the modrdn clients */ nanum = 0; snprintf( ncmd, sizeof ncmd, "%s" LDAP_DIRSEP MODRDNCMD, progdir ); nargs[nanum++] = ncmd; if ( uri ) { nargs[nanum++] = "-H"; nargs[nanum++] = uri; } else { nargs[nanum++] = "-h"; nargs[nanum++] = host; nargs[nanum++] = "-p"; nargs[nanum++] = port; } nargs[nanum++] = "-D"; nargs[nanum++] = manager; nargs[nanum++] = "-w"; nargs[nanum++] = passwd; nargs[nanum++] = "-l"; nargs[nanum++] = nloops; nargs[nanum++] = "-L"; nargs[nanum++] = outerloops; nargs[nanum++] = "-r"; nargs[nanum++] = retries; nargs[nanum++] = "-t"; nargs[nanum++] = delay; if ( friendly ) { nargs[nanum++] = friendlyOpt; } if ( chaserefs ) { nargs[nanum++] = "-C"; } if ( ignore ) { nargs[nanum++] = "-i"; nargs[nanum++] = ignore; } nargs[nanum++] = "-e"; nargs[nanum++] = NULL; /* will hold the modrdn entry */ nargs[nanum] = NULL; /* * generate the modify clients */ manum = 0; snprintf( mcmd, sizeof mcmd, "%s" LDAP_DIRSEP MODIFYCMD, progdir ); margs[manum++] = mcmd; if ( uri ) { margs[manum++] = "-H"; margs[manum++] = uri; } else { margs[manum++] = "-h"; margs[manum++] = host; margs[manum++] = "-p"; margs[manum++] = port; } margs[manum++] = "-D"; margs[manum++] = manager; margs[manum++] = "-w"; margs[manum++] = passwd; margs[manum++] = "-l"; margs[manum++] = mloops; margs[manum++] = "-L"; margs[manum++] = outerloops; margs[manum++] = "-r"; margs[manum++] = retries; margs[manum++] = "-t"; margs[manum++] = delay; if ( friendly ) { margs[manum++] = friendlyOpt; } if ( chaserefs ) { margs[manum++] = "-C"; } if ( ignore ) { margs[manum++] = "-i"; margs[manum++] = ignore; } margs[manum++] = "-e"; margs[manum++] = NULL; /* will hold the modify entry */ margs[manum++] = "-a";; margs[manum++] = NULL; /* will hold the ava */ margs[manum] = NULL; /* * generate the add/delete clients */ aanum = 0; snprintf( acmd, sizeof acmd, "%s" LDAP_DIRSEP ADDCMD, progdir ); aargs[aanum++] = acmd; if ( uri ) { aargs[aanum++] = "-H"; aargs[aanum++] = uri; } else { aargs[aanum++] = "-h"; aargs[aanum++] = host; aargs[aanum++] = "-p"; aargs[aanum++] = port; } aargs[aanum++] = "-D"; aargs[aanum++] = manager; aargs[aanum++] = "-w"; aargs[aanum++] = passwd; aargs[aanum++] = "-l"; aargs[aanum++] = aloops; aargs[aanum++] = "-L"; aargs[aanum++] = outerloops; aargs[aanum++] = "-r"; aargs[aanum++] = retries; aargs[aanum++] = "-t"; aargs[aanum++] = delay; if ( friendly ) { aargs[aanum++] = friendlyOpt; } if ( chaserefs ) { aargs[aanum++] = "-C"; } if ( ignore ) { aargs[aanum++] = "-i"; aargs[aanum++] = ignore; } aargs[aanum++] = "-f"; aargs[aanum++] = NULL; /* will hold the add data file */ aargs[aanum] = NULL; /* * generate the bind clients */ banum = 0; snprintf( bcmd, sizeof bcmd, "%s" LDAP_DIRSEP BINDCMD, progdir ); bargs[banum++] = bcmd; if ( !noinit ) { bargs[banum++] = "-I"; /* init on each bind */ } if ( uri ) { bargs[banum++] = "-H"; bargs[banum++] = uri; } else { bargs[banum++] = "-h"; bargs[banum++] = host; bargs[banum++] = "-p"; bargs[banum++] = port; } bargs[banum++] = "-l"; bargs[banum++] = bloops; bargs[banum++] = "-L"; bargs[banum++] = outerloops; #if 0 bargs[banum++] = "-r"; bargs[banum++] = retries; bargs[banum++] = "-t"; bargs[banum++] = delay; #endif if ( friendly ) { bargs[banum++] = friendlyOpt; } if ( chaserefs ) { bargs[banum++] = "-C"; } if ( ignore ) { bargs[banum++] = "-i"; bargs[banum++] = ignore; } if ( nextra ) { bargs[banum++] = "-B"; bargs_extra = &bargs[banum++]; } bargs[banum++] = "-D"; bargs[banum++] = NULL; bargs[banum++] = "-w"; bargs[banum++] = NULL; bargs[banum] = NULL; #define DOREQ(n,j) ((n) && ((maxkids > (n)) ? ((j) < maxkids ) : ((j) < (n)))) for ( j = 0; j < MAXREQS; j++ ) { /* search */ if ( DOREQ( snum, j ) ) { int jj = j % snum; int x = sanum - sextra_args; /* base */ if ( sbase[jj] != NULL ) { sargs[sanum - 7] = sbase[jj]; } else { sargs[sanum - 7] = slud[jj]->lud_dn; } /* scope */ if ( slud[jj] != NULL ) { sargs[sanum - 5] = (char *)ldap_pvt_scope2str( slud[jj]->lud_scope ); } else { sargs[sanum - 5] = "sub"; } /* filter */ if ( sreqs[jj] != NULL ) { sargs[sanum - 3] = sreqs[jj]; } else if ( slud[jj]->lud_filter != NULL ) { sargs[sanum - 3] = slud[jj]->lud_filter; } else { sargs[sanum - 3] = "(objectClass=*)"; } /* extras */ sargs[x] = NULL; /* attr */ if ( sattrs[jj] != NULL ) { sargs[x++] = "-a"; sargs[x++] = sattrs[jj]; } /* attrs */ if ( slud[jj] != NULL && slud[jj]->lud_attrs != NULL ) { int i; for ( i = 0; slud[jj]->lud_attrs[ i ] != NULL && x + i < MAXARGS - 1; i++ ) { sargs[x + i] = slud[jj]->lud_attrs[ i ]; } sargs[x + i] = NULL; } fork_child( scmd, sargs ); } /* read */ if ( DOREQ( rnum, j ) ) { int jj = j % rnum; int x = ranum - rextra_args; rargs[ranum - 3] = rreqs[jj]; if ( rflts[jj] != NULL ) { rargs[x++] = "-f"; rargs[x++] = rflts[jj]; } rargs[x] = NULL; fork_child( rcmd, rargs ); } /* rename */ if ( j < nnum ) { nargs[nanum - 1] = nreqs[j]; fork_child( ncmd, nargs ); } /* modify */ if ( j < mnum ) { margs[manum - 3] = mdn[j]; margs[manum - 1] = mreqs[j]; fork_child( mcmd, margs ); } /* add/delete */ if ( j < anum ) { aargs[aanum - 1] = afiles[j]; fork_child( acmd, aargs ); } /* bind */ if ( DOREQ( bnum, j ) ) { int jj = j % bnum; if ( nextra ) { int n = ((double)nextra)*rand()/(RAND_MAX + 1.0); extra_t *e; for ( e = extra; n-- > 0; e = e->next ) ; *bargs_extra = e->action; } if ( battrs[jj] != NULL ) { bargs[banum - 3] = manager ? manager : ""; bargs[banum - 1] = passwd ? passwd : ""; bargs[banum + 0] = "-b"; bargs[banum + 1] = breqs[jj]; bargs[banum + 2] = "-f"; bargs[banum + 3] = bcreds[jj]; bargs[banum + 4] = "-a"; bargs[banum + 5] = battrs[jj]; bargs[banum + 6] = NULL; } else { bargs[banum - 3] = breqs[jj]; bargs[banum - 1] = bcreds[jj]; bargs[banum] = NULL; } fork_child( bcmd, bargs ); bargs[banum] = NULL; } } wait4kids( -1 ); exit( EXIT_SUCCESS ); }
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 rc; LDAP *ld = NULL; char *matcheddn = NULL, *text = NULL, **refs = NULL; LDAPControl **ctrls = NULL; int id, code; LDAPMessage *res = NULL; tool_init( TOOL_EXOP ); prog = lutil_progname( "ldapexop", argc, argv ); /* LDAPv3 only */ protocol = LDAP_VERSION3; tool_args( argc, argv ); if ( argc - optind < 1 ) { usage(); } ld = tool_conn_setup( 0, 0 ); tool_bind( ld ); argv += optind; argc -= optind; if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) { tool_server_controls( ld, NULL, 0 ); rc = ldap_whoami( ld, NULL, NULL, &id ); if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) { int cancelid; switch ( argc ) { case 2: if ( lutil_atoi( &cancelid, argv[ 1 ] ) != 0 || cancelid < 0 ) { fprintf( stderr, "invalid cancelid=%s\n\n", argv[ 1 ] ); usage(); } break; default: fprintf( stderr, "need cancelid\n\n" ); usage(); } rc = ldap_cancel( ld, cancelid, NULL, NULL, &id ); if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_cancel", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) { fprintf( stderr, "use ldappasswd(1) instead.\n\n" ); usage(); /* TODO? */ } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) { int ttl = 3600; struct berval dn; switch ( argc ) { case 3: ttl = atoi( argv[ 2 ] ); case 2: dn.bv_val = argv[ 1 ]; dn.bv_len = strlen( dn.bv_val ); break; default: fprintf( stderr, _("need DN [ttl]\n\n") ); usage(); } tool_server_controls( ld, NULL, 0 ); rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id ); if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } } else { char *p; if ( argc != 1 ) { usage(); } p = strchr( argv[ 0 ], ':' ); if ( p == argv[ 0 ] ) { usage(); } if ( p != NULL ) *p++ = '\0'; if ( tool_is_oid( argv[ 0 ] ) ) { struct berval reqdata; struct berval type; struct berval value; int freeval; if ( p != NULL ) { p[ -1 ] = ':'; ldif_parse_line2( argv[ 0 ], &type, &value, &freeval ); p[ -1 ] = '\0'; if ( freeval ) { reqdata = value; } else { ber_dupbv( &reqdata, &value ); } } tool_server_controls( ld, NULL, 0 ); rc = ldap_extended_operation( ld, argv[ 0 ], p ? &reqdata : NULL, NULL, NULL, &id ); if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } } else { fprintf( stderr, "unknown exop \"%s\"\n\n", argv[ 0 ] ); usage(); } } for ( ; ; ) { struct timeval tv; if ( tool_check_abandon( ld, id ) ) { tool_exit( ld, LDAP_CANCELLED ); } tv.tv_sec = 0; tv.tv_usec = 100000; rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } if ( rc != 0 ) { break; } } rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 0 ); if ( rc == LDAP_SUCCESS ) { rc = code; } if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs ); rc = EXIT_FAILURE; goto skip; } if ( strcasecmp( argv[ 0 ], "whoami" ) == 0 ) { char *retoid = NULL; struct berval *retdata = NULL; rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 0 ); if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } if ( retdata != NULL ) { if ( retdata->bv_len == 0 ) { printf(_("anonymous\n") ); } else { printf("%s\n", retdata->bv_val ); } } ber_memfree( retoid ); ber_bvfree( retdata ); } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) { /* no extended response; returns specific errors */ assert( 0 ); } else if ( strcasecmp( argv[ 0 ], "passwd" ) == 0 ) { /* TODO */ } else if ( strcasecmp( argv[ 0 ], "refresh" ) == 0 ) { int newttl; rc = ldap_parse_refresh( ld, res, &newttl ); if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_refresh", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } printf( "newttl=%d\n", newttl ); } else if ( tool_is_oid( argv[ 0 ] ) ) { char *retoid = NULL; struct berval *retdata = NULL; if( ldif < 2 ) { printf(_("# extended operation response\n")); } rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 0 ); if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } if ( ldif < 2 && retoid != NULL ) { tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE, "oid", retoid, strlen(retoid) ); } ber_memfree( retoid ); if( retdata != NULL ) { if ( ldif < 2 ) { tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY, "data", retdata->bv_val, retdata->bv_len ); } ber_bvfree( retdata ); } } if( verbose || code != LDAP_SUCCESS || ( matcheddn && *matcheddn ) || ( text && *text ) || refs ) { printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code ); if( text && *text ) { printf( _("Additional info: %s\n"), text ); } if( matcheddn && *matcheddn ) { printf( _("Matched DN: %s\n"), matcheddn ); } if( refs ) { int i; for( i=0; refs[i]; i++ ) { printf(_("Referral: %s\n"), refs[i] ); } } } if (ctrls) { tool_print_ctrls( ld, ctrls ); ldap_controls_free( ctrls ); } ber_memfree( text ); ber_memfree( matcheddn ); ber_memvfree( (void **) refs ); skip: /* disconnect from server */ if ( res ) ldap_msgfree( res ); tool_exit( ld, code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE ); }
static int process_ldif_rec( char *rbuf, int linenum ) { char *line, *dn, *newrdn, *newsup; int rc, modop; int expect_modop, expect_sep; int deleteoldrdn; int new_entry, delete_entry, got_all; LDAPMod **pmods, *lm = NULL; int version; LDAPControl **pctrls; int i, j, k, lines, idn, nmods; struct berval *btype, *vals, **bvl, bv; char *freeval; unsigned char *mops = NULL; new_entry = ldapadd; rc = got_all = delete_entry = modop = expect_modop = 0; expect_sep = 0; version = 0; deleteoldrdn = 1; pmods = NULL; pctrls = NULL; dn = newrdn = newsup = NULL; lines = ldif_countlines( rbuf ); btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines ); if ( !btype ) return LDAP_NO_MEMORY; vals = btype+lines+1; freeval = (char *)(vals+lines+1); i = -1; while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) { int freev; if ( *line == '\n' || *line == '\0' ) { break; } ++i; if ( line[0] == '-' && !line[1] ) { BER_BVZERO( btype+i ); freeval[i] = 0; continue; } if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) { fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"), prog, linenum+i, dn == NULL ? "" : dn ); rc = LDAP_PARAM_ERROR; break; } freeval[i] = freev; if ( dn == NULL ) { if ( linenum+i == 1 && BV_CASEMATCH( btype+i, &BV_VERSION )) { int v; if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) { fprintf( stderr, _("%s: invalid version %s, line %d (ignored)\n"), prog, vals[i].bv_val, linenum ); } version++; } else if ( BV_CASEMATCH( btype+i, &BV_DN )) { dn = vals[i].bv_val; idn = i; } /* skip all lines until we see "dn:" */ } } /* check to make sure there was a dn: line */ if ( !dn ) { rc = 0; goto leave; } lines = i+1; if( lines == 0 ) { rc = 0; goto leave; } if( version && lines == 1 ) { rc = 0; goto leave; } i = idn+1; /* Check for "control" tag after dn and before changetype. */ if ( BV_CASEMATCH( btype+i, &BV_CONTROL )) { /* Parse and add it to the list of controls */ rc = parse_ldif_control( vals+i, &pctrls ); if (rc != 0) { fprintf( stderr, _("%s: Error processing %s line, line %d: %s\n"), prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) ); } i++; if ( i>= lines ) { short_input: fprintf( stderr, _("%s: Expecting more input after %s line, line %d\n"), prog, btype[i-1].bv_val, linenum+i ); rc = LDAP_PARAM_ERROR; goto leave; } } /* Check for changetype */ if ( BV_CASEMATCH( btype+i, &BV_CHANGETYPE )) { #ifdef LIBERAL_CHANGETYPE_MODOP /* trim trailing spaces (and log warning ...) */ int icnt; for ( icnt = vals[i].bv_len; --icnt > 0; ) { if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) { break; } } if ( ++icnt != vals[i].bv_len ) { fprintf( stderr, _("%s: illegal trailing space after" " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"), prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn ); vals[i].bv_val[icnt] = '\0'; } #endif /* LIBERAL_CHANGETYPE_MODOP */ if ( BV_CASEMATCH( vals+i, &BV_MODIFYCT )) { new_entry = 0; expect_modop = 1; } else if ( BV_CASEMATCH( vals+i, &BV_ADDCT )) { new_entry = 1; modop = LDAP_MOD_ADD; } else if ( BV_CASEMATCH( vals+i, &BV_MODRDNCT ) || BV_CASEMATCH( vals+i, &BV_MODDNCT ) || BV_CASEMATCH( vals+i, &BV_RENAMECT )) { i++; if ( i >= lines ) goto short_input; if ( !BV_CASEMATCH( btype+i, &BV_NEWRDN )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %d, entry \"%s\")\n"), prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } newrdn = vals[i].bv_val; i++; if ( i >= lines ) goto short_input; if ( !BV_CASEMATCH( btype+i, &BV_DELETEOLDRDN )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %d, entry \"%s\")\n"), prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1; i++; if ( i < lines ) { if ( !BV_CASEMATCH( btype+i, &BV_NEWSUP )) { fprintf( stderr, _("%s: expecting \"%s:\" but saw" " \"%s:\" (line %d, entry \"%s\")\n"), prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } newsup = vals[i].bv_val; i++; } got_all = 1; } else if ( BV_CASEMATCH( vals+i, &BV_DELETECT )) { got_all = delete_entry = 1; } else { fprintf( stderr, _("%s: unknown %s \"%s\" (line %d, entry \"%s\")\n"), prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } i++; } else if ( ldapadd ) { /* missing changetype => add */ new_entry = 1; modop = LDAP_MOD_ADD; } else { expect_modop = 1; /* missing changetype => modify */ } if ( got_all ) { if ( i < lines ) { fprintf( stderr, _("%s: extra lines at end (line %d, entry \"%s\")\n"), prog, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } goto doit; } nmods = lines - i; idn = i; if ( new_entry ) { int fv; /* Make sure all attributes with multiple values are contiguous */ for (; i<lines; i++) { for (j=i+1; j<lines; j++) { if ( BV_CASEMATCH( btype+i, btype+j )) { nmods--; /* out of order, move intervening attributes down */ if ( j != i+1 ) { bv = vals[j]; fv = freeval[j]; for (k=j; k>i; k--) { btype[k] = btype[k-1]; vals[k] = vals[k-1]; freeval[k] = freeval[k-1]; } k++; btype[k] = btype[i]; vals[k] = bv; freeval[k] = fv; } i++; } } } /* Allocate space for array of mods, array of pointers to mods, * and array of pointers to values, allowing for NULL terminators * for the pointer arrays... */ lm = ber_memalloc( nmods * sizeof(LDAPMod) + (nmods+1) * sizeof(LDAPMod*) + (lines + nmods - idn) * sizeof(struct berval *)); pmods = (LDAPMod **)(lm+nmods); bvl = (struct berval **)(pmods+nmods+1); j = 0; k = -1; BER_BVZERO(&bv); for (i=idn; i<lines; i++) { if ( BV_CASEMATCH( btype+i, &BV_DN )) { fprintf( stderr, _("%s: attributeDescription \"%s\":" " (possible missing newline" " after line %d, entry \"%s\"?)\n"), prog, btype[i].bv_val, linenum+i - 1, dn ); } if ( !BV_CASEMATCH( btype+i, &bv )) { bvl[k++] = NULL; bv = btype[i]; lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES; lm[j].mod_type = bv.bv_val; lm[j].mod_bvalues = bvl+k; pmods[j] = lm+j; j++; } bvl[k++] = vals+i; } bvl[k] = NULL; pmods[j] = NULL; goto doit; } mops = ber_memalloc( lines+1 ); mops[lines] = M_SEP; mops[i-1] = M_SEP; for ( ; i<lines; i++ ) { if ( expect_modop ) { #ifdef LIBERAL_CHANGETYPE_MODOP /* trim trailing spaces (and log warning ...) */ int icnt; for ( icnt = vals[i].bv_len; --icnt > 0; ) { if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break; } if ( ++icnt != vals[i].bv_len ) { fprintf( stderr, _("%s: illegal trailing space after" " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"), prog, type, vals[i].bv_val, linenum+i, dn ); vals[i].bv_val[icnt] = '\0'; } #endif /* LIBERAL_CHANGETYPE_MODOP */ expect_modop = 0; expect_sep = 1; if ( BV_CASEMATCH( btype+i, &BV_MODOPADD )) { modop = LDAP_MOD_ADD; mops[i] = M_SEP; nmods--; } else if ( BV_CASEMATCH( btype+i, &BV_MODOPREPLACE )) { /* defer handling these since they might have no values. * Use the BVALUES flag to signal that these were * deferred. If values are provided later, this * flag will be switched off. */ modop = LDAP_MOD_REPLACE; mops[i] = modop | LDAP_MOD_BVALUES; btype[i] = vals[i]; } else if ( BV_CASEMATCH( btype+i, &BV_MODOPDELETE )) { modop = LDAP_MOD_DELETE; mops[i] = modop | LDAP_MOD_BVALUES; btype[i] = vals[i]; } else if ( BV_CASEMATCH( btype+i, &BV_MODOPINCREMENT )) { modop = LDAP_MOD_INCREMENT; mops[i] = M_SEP; nmods--; } else { /* no modify op: invalid LDIF */ fprintf( stderr, _("%s: modify operation type is missing at" " line %d, entry \"%s\"\n"), prog, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } bv = vals[i]; } else if ( expect_sep && BER_BVISEMPTY( btype+i )) { mops[i] = M_SEP; expect_sep = 0; expect_modop = 1; nmods--; } else { if ( !BV_CASEMATCH( btype+i, &bv )) { fprintf( stderr, _("%s: wrong attributeType at" " line %d, entry \"%s\"\n"), prog, linenum+i, dn ); rc = LDAP_PARAM_ERROR; goto leave; } mops[i] = modop; /* If prev op was deferred and matches this type, * clear the flag */ if ( (mops[i-1] & LDAP_MOD_BVALUES) && BV_CASEMATCH( btype+i, btype+i-1 )) { mops[i-1] = M_SEP; nmods--; } } } #if 0 /* we should faithfully encode the LDIF, not combine */ /* Make sure all modops with multiple values are contiguous */ for (i=idn; i<lines; i++) { if ( mops[i] == M_SEP ) continue; for (j=i+1; j<lines; j++) { if ( mops[j] == M_SEP || mops[i] != mops[j] ) continue; if ( BV_CASEMATCH( btype+i, btype+j )) { nmods--; /* out of order, move intervening attributes down */ if ( j != i+1 ) { int c; struct berval bv; char fv; c = mops[j]; bv = vals[j]; fv = freeval[j]; for (k=j; k>i; k--) { btype[k] = btype[k-1]; vals[k] = vals[k-1]; freeval[k] = freeval[k-1]; mops[k] = mops[k-1]; } k++; btype[k] = btype[i]; vals[k] = bv; freeval[k] = fv; mops[k] = c; } i++; } } } #endif /* Allocate space for array of mods, array of pointers to mods, * and array of pointers to values, allowing for NULL terminators * for the pointer arrays... */ lm = ber_memalloc( nmods * sizeof(LDAPMod) + (nmods+1) * sizeof(LDAPMod*) + (lines + nmods - idn) * sizeof(struct berval *)); pmods = (LDAPMod **)(lm+nmods); bvl = (struct berval **)(pmods+nmods+1); j = 0; k = -1; BER_BVZERO(&bv); mops[idn-1] = M_SEP; for (i=idn; i<lines; i++) { if ( mops[i] == M_SEP ) continue; if ( mops[i] != mops[i-1] || !BV_CASEMATCH( btype+i, &bv )) { bvl[k++] = NULL; bv = btype[i]; lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES; lm[j].mod_type = bv.bv_val; if ( mops[i] & LDAP_MOD_BVALUES ) { lm[j].mod_bvalues = NULL; } else { lm[j].mod_bvalues = bvl+k; } pmods[j] = lm+j; j++; } bvl[k++] = vals+i; } bvl[k] = NULL; pmods[j] = NULL; doit: /* If default controls are set (as with -M option) and controls are specified in the LDIF file, we must add the default controls to the list of controls sent with the ldap operation. */ if ( rc == 0 ) { if (pctrls) { LDAPControl **defctrls = NULL; /* Default server controls */ LDAPControl **newctrls = NULL; ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls); if (defctrls) { int npc=0; /* Num of LDIF controls */ int ndefc=0; /* Num of default controls */ while (pctrls[npc]) npc++; /* Count LDIF controls */ while (defctrls[ndefc]) ndefc++; /* Count default controls */ newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*)); if (newctrls == NULL) { rc = LDAP_NO_MEMORY; } else { int i; pctrls = newctrls; for (i=npc; i<npc+ndefc; i++) { pctrls[i] = ldap_control_dup(defctrls[i-npc]); if (pctrls[i] == NULL) { rc = LDAP_NO_MEMORY; break; } } pctrls[npc+ndefc] = NULL; } ldap_controls_free(defctrls); /* Must be freed by library */ } } } if ( rc == 0 ) { if ( delete_entry ) { rc = dodelete( dn, pctrls ); } else if ( newrdn != NULL ) { rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls ); } else { rc = domodify( dn, pmods, pctrls, new_entry ); } if ( rc == LDAP_SUCCESS ) { rc = 0; } } leave: if (pctrls != NULL) { ldap_controls_free( pctrls ); } if ( lm != NULL ) { ber_memfree( lm ); } if ( mops != NULL ) { ber_memfree( mops ); } for (i=lines-1; i>=0; i--) if ( freeval[i] ) ber_memfree( vals[i].bv_val ); ber_memfree( btype ); 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 ); }