From: Howard Chu Date: Wed, 9 Apr 2003 16:52:03 +0000 (+0000) Subject: First cut at thread-local malloc. Only used by search() for now... X-Git-Tag: OPENLDAP_REL_ENG_2_2_0ALPHA~412 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=813d5c8ed82e3df2eecae99f62dae2d687616121;p=openldap First cut at thread-local malloc. Only used by search() for now... Needs work in normalizers, etc. --- diff --git a/servers/slapd/Makefile.in b/servers/slapd/Makefile.in index d4e76b33de..8266bcb5c1 100644 --- a/servers/slapd/Makefile.in +++ b/servers/slapd/Makefile.in @@ -19,7 +19,7 @@ SRCS = main.c globals.c config.c daemon.c \ schema.c schema_check.c schema_init.c schema_prep.c \ schemaparse.c ad.c at.c mr.c syntax.c oc.c saslauthz.c \ oidm.c starttls.c index.c sets.c referral.c \ - root_dse.c sasl.c module.c mra.c mods.c \ + root_dse.c sasl.c module.c mra.c mods.c sl_malloc.c \ limits.c backglue.c operational.c matchedValues.c cancel.c \ $(@PLAT@_SRCS) @@ -33,7 +33,7 @@ OBJS = main.o globals.o config.o daemon.o \ schema.o schema_check.o schema_init.o schema_prep.o \ schemaparse.o ad.o at.o mr.o syntax.o oc.o saslauthz.o \ oidm.o starttls.o index.o sets.o referral.o \ - root_dse.o sasl.o module.o mra.o mods.o \ + root_dse.o sasl.o module.o mra.o mods.o sl_malloc.o \ limits.o backglue.o operational.o matchedValues.o cancel.o \ $(@PLAT@_OBJS) diff --git a/servers/slapd/ava.c b/servers/slapd/ava.c index 9cfde4b98d..bf43ec5dea 100644 --- a/servers/slapd/ava.c +++ b/servers/slapd/ava.c @@ -17,20 +17,23 @@ void ava_free( - AttributeAssertion *ava, - int freeit + Operation *op, + AttributeAssertion *ava, + int freeit ) { - free( ava->aa_value.bv_val ); + /* op->o_tmpfree( ava->aa_value.bv_val, op->o_tmpmemctx ); */ + ch_free( ava->aa_value.bv_val ); if ( freeit ) { - ch_free( (char *) ava ); + op->o_tmpfree( (char *) ava, op->o_tmpmemctx ); } } int get_ava( - BerElement *ber, - AttributeAssertion **ava, + Operation *op, + BerElement *ber, + AttributeAssertion **ava, unsigned usage, const char **text ) @@ -52,14 +55,14 @@ get_ava( return SLAPD_DISCONNECT; } - aa = ch_malloc( sizeof( AttributeAssertion ) ); + aa = op->o_tmpalloc( sizeof( AttributeAssertion ), op->o_tmpmemctx ); aa->aa_desc = NULL; aa->aa_value.bv_val = NULL; rc = slap_bv2ad( &type, &aa->aa_desc, text ); if( rc != LDAP_SUCCESS ) { - ch_free( aa ); + op->o_tmpfree( aa, op->o_tmpmemctx ); return rc; } @@ -68,7 +71,7 @@ get_ava( usage, &value, &aa->aa_value, text ); if( rc != LDAP_SUCCESS ) { - ch_free( aa ); + op->o_tmpfree( aa, op->o_tmpmemctx ); return rc; } diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c index 956180747a..b0a6378fbf 100644 --- a/servers/slapd/connection.c +++ b/servers/slapd/connection.c @@ -910,6 +910,21 @@ connection_operation( void *ctx, void *arg_v ) goto operations_error; } + /* For all operations besides Add, we can use thread-local + * storage for most mallocs. + */ + if ( tag == LDAP_REQ_SEARCH ) { + sl_mem_create( ber_len( op->o_ber ) * 16, ctx ); + ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, ctx ); + op->o_tmpmemctx = ctx; + op->o_tmpalloc = sl_malloc; + op->o_tmpfree = sl_free; + } else { + op->o_tmpmemctx = NULL; + op->o_tmpalloc = (BER_MEMALLOC_FN *)ch_malloc; + op->o_tmpfree = (BER_MEMFREE_FN *)ch_free; + } + switch ( tag ) { case LDAP_REQ_BIND: INCR_OP(num_ops_initiated_, SLAP_OP_BIND); diff --git a/servers/slapd/controls.c b/servers/slapd/controls.c index 51b5abd5ac..bf01492aef 100644 --- a/servers/slapd/controls.c +++ b/servers/slapd/controls.c @@ -326,6 +326,19 @@ find_ctrl( const char *oid ) return NULL; } +void slap_free_ctrls( + Operation *op, + LDAPControl **ctrls +) +{ + int i; + + for (i=0; ctrls[i]; i++) { + op->o_tmpfree(ctrls[i], op->o_tmpmemctx ); + } + op->o_tmpfree( ctrls, op->o_tmpmemctx ); +} + int get_ctrls( Operation *op, SlapReply *rs, @@ -337,6 +350,10 @@ int get_ctrls( char *opaque; BerElement *ber = op->o_ber; struct slap_control *sc; + struct berval bv; + BER_MEMREALLOC_FN *reallo; + + reallo = op->o_tmpmemctx ? sl_realloc : (BER_MEMREALLOC_FN *)ch_realloc; len = ber_pvt_ber_remaining(ber); @@ -370,7 +387,7 @@ int get_ctrls( } /* one for first control, one for termination */ - op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) ); + op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx ); #if 0 if( op->ctrls == NULL ) { @@ -390,24 +407,14 @@ int get_ctrls( LDAPControl *c; LDAPControl **tctrls; - c = ch_calloc( 1, sizeof(LDAPControl) ); - -#if 0 - if( c == NULL ) { - ldap_controls_free(op->o_ctrls); - op->o_ctrls = NULL; - - rs->sr_err = LDAP_NO_MEMORY; - rs->sr_text = "no memory"; - goto return_results; - } -#endif + c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx ); + memset(c, 0, sizeof(LDAPControl)); /* allocate pointer space for current controls (nctrls) * + this control + extra NULL */ - tctrls = ch_realloc( op->o_ctrls, - (nctrls+2) * sizeof(LDAPControl *)); + tctrls = reallo( op->o_ctrls, + (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx ); #if 0 if( tctrls == NULL ) { @@ -425,7 +432,8 @@ int get_ctrls( op->o_ctrls[nctrls++] = c; op->o_ctrls[nctrls] = NULL; - tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid ); + tag = ber_scanf( ber, "{m" /*}*/, &bv ); + c->ldctl_oid = bv.bv_val; if( tag == LBER_ERROR ) { #ifdef NEW_LOGGING @@ -436,7 +444,7 @@ int get_ctrls( 0, 0, 0 ); #endif - ldap_controls_free( op->o_ctrls ); + slap_free_ctrls( op, op->o_ctrls ); op->o_ctrls = NULL; rs->sr_err = SLAPD_DISCONNECT; rs->sr_text = "decoding controls error"; @@ -453,7 +461,7 @@ int get_ctrls( op->o_connid, 0, 0 ); #endif - ldap_controls_free( op->o_ctrls ); + slap_free_ctrls( op, op->o_ctrls ); op->o_ctrls = NULL; rs->sr_err = LDAP_PROTOCOL_ERROR; rs->sr_text = "OID field is empty"; @@ -475,7 +483,7 @@ int get_ctrls( Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n", 0, 0, 0 ); #endif - ldap_controls_free( op->o_ctrls ); + slap_free_ctrls( op, op->o_ctrls ); op->o_ctrls = NULL; rs->sr_err = SLAPD_DISCONNECT; rs->sr_text = "decoding controls error"; @@ -487,7 +495,7 @@ int get_ctrls( } if( tag == LBER_OCTETSTRING ) { - tag = ber_scanf( ber, "o", &c->ldctl_value ); + tag = ber_scanf( ber, "m", &c->ldctl_value ); if( tag == LBER_ERROR ) { #ifdef NEW_LOGGING @@ -501,7 +509,7 @@ int get_ctrls( op->o_connid, c->ldctl_oid, c->ldctl_iscritical ? "" : "non" ); #endif - ldap_controls_free( op->o_ctrls ); + slap_free_ctrls( op, op->o_ctrls ); op->o_ctrls = NULL; rs->sr_err = SLAPD_DISCONNECT; rs->sr_text = "decoding controls error"; @@ -711,7 +719,7 @@ static int parseProxyAuthz ( return LDAP_SUCCESS; } - rc = slap_sasl_getdn( op->o_conn, + rc = slap_sasl_getdn( op->o_conn, op, ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len, NULL, &dn, SLAP_GETDN_AUTHZID ); @@ -888,7 +896,7 @@ int parseValuesReturnFilter ( return LDAP_OTHER; } - rs->sr_err = get_vrFilter( op->o_conn, ber, &(op->vrFilter), &rs->sr_text); + rs->sr_err = get_vrFilter( op, ber, &(op->vrFilter), &rs->sr_text); if( rs->sr_err != LDAP_SUCCESS ) { if( rs->sr_err == SLAPD_DISCONNECT ) { @@ -898,11 +906,12 @@ int parseValuesReturnFilter ( } else { send_ldap_result( op, rs ); } - if( fstr.bv_val != NULL) free( fstr.bv_val ); - if( op->vrFilter != NULL) vrFilter_free( op->vrFilter ); + if( op->vrFilter != NULL) vrFilter_free( op, op->vrFilter ); - } else { - vrFilter2bv( op->vrFilter, &fstr ); + } +#ifdef LDAP_DEBUG + else { + vrFilter2bv( op, op->vrFilter, &fstr ); } #ifdef NEW_LOGGING @@ -913,6 +922,8 @@ int parseValuesReturnFilter ( Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n", fstr.bv_len ? fstr.bv_val : "empty", 0, 0 ); #endif + op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx ); +#endif op->o_valuesreturnfilter = ctrl->ldctl_iscritical ? SLAP_CRITICAL_CONTROL diff --git a/servers/slapd/filter.c b/servers/slapd/filter.c index f9db732ba2..8de5d6e94e 100644 --- a/servers/slapd/filter.c +++ b/servers/slapd/filter.c @@ -15,30 +15,31 @@ #include "slap.h" static int get_filter_list( - Connection *conn, + Operation *op, BerElement *ber, Filter **f, const char **text ); static int get_ssa( - Connection *conn, + Operation *op, BerElement *ber, SubstringsAssertion **s, const char **text ); static void simple_vrFilter2bv( + Operation *op, ValuesReturnFilter *f, struct berval *fstr ); static int get_simple_vrFilter( - Connection *conn, + Operation *op, BerElement *ber, ValuesReturnFilter **f, const char **text ); int get_filter( - Connection *conn, + Operation *op, BerElement *ber, Filter **filt, const char **text ) @@ -49,7 +50,7 @@ get_filter( Filter f; #ifdef NEW_LOGGING - LDAP_LOG( FILTER, ENTRY, "get_filter: conn %d\n", conn->c_connid, 0, 0 ); + LDAP_LOG( FILTER, ENTRY, "get_filter: conn %d\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 ); #endif @@ -102,11 +103,11 @@ get_filter( case LDAP_FILTER_EQUALITY: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL2, - "get_filter: conn %d EQUALITY\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d EQUALITY\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 ); #endif - err = get_ava( ber, &f.f_ava, SLAP_MR_EQUALITY, text ); + err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -117,11 +118,11 @@ get_filter( case LDAP_FILTER_SUBSTRINGS: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d SUBSTRINGS\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 ); #endif - err = get_ssa( conn, ber, &f.f_sub, text ); + err = get_ssa( op, ber, &f.f_sub, text ); if( err != LDAP_SUCCESS ) { break; } @@ -131,11 +132,11 @@ get_filter( case LDAP_FILTER_GE: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d GE\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d GE\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 ); #endif - err = get_ava( ber, &f.f_ava, SLAP_MR_ORDERING, text ); + err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -145,11 +146,11 @@ get_filter( case LDAP_FILTER_LE: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d LE\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d LE\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 ); #endif - err = get_ava( ber, &f.f_ava, SLAP_MR_ORDERING, text ); + err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -161,7 +162,7 @@ get_filter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d PRESENT\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d PRESENT\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 ); #endif @@ -189,11 +190,11 @@ get_filter( case LDAP_FILTER_APPROX: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d APPROX\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d APPROX\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 ); #endif - err = get_ava( ber, &f.f_ava, SLAP_MR_EQUALITY_APPROX, text ); + err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY_APPROX, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -203,11 +204,11 @@ get_filter( case LDAP_FILTER_AND: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d AND\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d AND\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 ); #endif - err = get_filter_list( conn, ber, &f.f_and, text ); + err = get_filter_list( op, ber, &f.f_and, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -217,11 +218,11 @@ get_filter( case LDAP_FILTER_OR: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d OR\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d OR\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 ); #endif - err = get_filter_list( conn, ber, &f.f_or, text ); + err = get_filter_list( op, ber, &f.f_or, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -231,12 +232,12 @@ get_filter( case LDAP_FILTER_NOT: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d NOT\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d NOT\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 ); #endif (void) ber_skip_tag( ber, &len ); - err = get_filter( conn, ber, &f.f_not, text ); + err = get_filter( op, ber, &f.f_not, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -247,12 +248,12 @@ get_filter( case LDAP_FILTER_EXT: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_filter: conn %d EXTENSIBLE\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 ); #endif - err = get_mra( ber, &f.f_mra, text ); + err = get_mra( op, ber, &f.f_mra, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -265,7 +266,7 @@ get_filter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, ERR, "get_filter: conn %d unknown filter type=%lu\n", - conn->c_connid, f.f_choice, 0 ); + op->o_connid, f.f_choice, 0 ); #else Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n", f.f_choice, 0, 0 ); @@ -283,13 +284,13 @@ get_filter( } if ( err == LDAP_SUCCESS ) { - *filt = ch_malloc( sizeof(f) ); + *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx ); **filt = f; } #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL2, - "get_filter: conn %d exit\n", conn->c_connid, 0, 0 ); + "get_filter: conn %d exit\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 ); #endif @@ -298,7 +299,7 @@ get_filter( } static int -get_filter_list( Connection *conn, BerElement *ber, +get_filter_list( Operation *op, BerElement *ber, Filter **f, const char **text ) { @@ -310,7 +311,7 @@ get_filter_list( Connection *conn, BerElement *ber, #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_filter_list: conn %d start\n", conn->c_connid, 0, 0 ); + "get_filter_list: conn %d start\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 ); #endif @@ -319,7 +320,7 @@ get_filter_list( Connection *conn, BerElement *ber, tag != LBER_DEFAULT; tag = ber_next_element( ber, &len, last ) ) { - err = get_filter( conn, ber, new, text ); + err = get_filter( op, ber, new, text ); if ( err != LDAP_SUCCESS ) return( err ); new = &(*new)->f_next; @@ -328,7 +329,7 @@ get_filter_list( Connection *conn, BerElement *ber, #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_filter_list: conn %d exit\n", conn->c_connid, 0, 0 ); + "get_filter_list: conn %d exit\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 ); #endif @@ -337,7 +338,7 @@ get_filter_list( Connection *conn, BerElement *ber, static int get_ssa( - Connection *conn, + Operation *op, BerElement *ber, SubstringsAssertion **out, const char **text ) @@ -353,7 +354,7 @@ get_ssa( #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_ssa: conn %d begin\n", conn->c_connid, 0, 0 ); + "get_ssa: conn %d begin\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 ); #endif @@ -412,7 +413,7 @@ get_ssa( #ifdef NEW_LOGGING LDAP_LOG( FILTER, ERR, "get_filter_substring: conn %d unknown substring choice=%ld\n", - conn->c_connid, (long)tag, 0 ); + op->o_connid, (long)tag, 0 ); #else Debug( LDAP_DEBUG_FILTER, " unknown substring choice=%ld\n", @@ -438,7 +439,7 @@ get_ssa( #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, "get_ssa: conn %d INITIAL\n", - conn->c_connid, 0, 0 ); + op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 ); #endif @@ -458,7 +459,7 @@ get_ssa( #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, "get_ssa: conn %d ANY\n", - conn->c_connid, 0, 0 ); + op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 ); #endif @@ -468,14 +469,18 @@ get_ssa( goto return_error; } +#ifdef notyet + ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx ); +#else ber_bvarray_add( &ssa.sa_any, &nvalue ); +#endif break; case LDAP_SUBSTRING_FINAL: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, "get_ssa: conn %d FINAL\n", - conn->c_connid, 0, 0 ); + op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 ); #endif @@ -492,7 +497,7 @@ get_ssa( #ifdef NEW_LOGGING LDAP_LOG( FILTER, INFO, "get_ssa: conn %d unknown substring type %ld\n", - conn->c_connid, (long)tag, 0 ); + op->o_connid, (long)tag, 0 ); #else Debug( LDAP_DEBUG_FILTER, " unknown substring type=%ld\n", @@ -506,7 +511,7 @@ return_error: #ifdef NEW_LOGGING LDAP_LOG( FILTER, INFO, "get_ssa: conn %d error %ld\n", - conn->c_connid, (long)rc, 0 ); + op->o_connid, (long)rc, 0 ); #else Debug( LDAP_DEBUG_FILTER, " error=%ld\n", (long) rc, 0, 0 ); @@ -521,13 +526,13 @@ return_error: } if( rc == LDAP_SUCCESS ) { - *out = ch_malloc( sizeof( ssa ) ); + *out = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx ); **out = ssa; } #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_ssa: conn %d exit\n", conn->c_connid, 0, 0 ); + "get_ssa: conn %d exit\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 ); #endif @@ -536,7 +541,7 @@ return_error: } void -filter_free( Filter *f ) +filter_free_x( Operation *op, Filter *f ) { Filter *p, *next; @@ -552,18 +557,26 @@ filter_free( Filter *f ) case LDAP_FILTER_GE: case LDAP_FILTER_LE: case LDAP_FILTER_APPROX: - ava_free( f->f_ava, 1 ); + ava_free( op, f->f_ava, 1 ); break; case LDAP_FILTER_SUBSTRINGS: if ( f->f_sub_initial.bv_val != NULL ) { - free( f->f_sub_initial.bv_val ); +#ifdef notyet + op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx ); +#else + ch_free( f->f_sub_initial.bv_val ); +#endif } ber_bvarray_free( f->f_sub_any ); if ( f->f_sub_final.bv_val != NULL ) { - free( f->f_sub_final.bv_val ); +#ifdef notyet + op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx ); +#else + ch_free( f->f_sub_final.bv_val ); +#endif } - ch_free( f->f_sub ); + op->o_tmpfree( f->f_sub, op->o_tmpmemctx ); break; case LDAP_FILTER_AND: @@ -571,12 +584,12 @@ filter_free( Filter *f ) case LDAP_FILTER_NOT: for ( p = f->f_list; p != NULL; p = next ) { next = p->f_next; - filter_free( p ); + filter_free_x( op, p ); } break; case LDAP_FILTER_EXT: - mra_free( f->f_mra, 1 ); + mra_free( op, f->f_mra, 1 ); break; case SLAPD_FILTER_COMPUTED: @@ -593,82 +606,94 @@ filter_free( Filter *f ) break; } - free( f ); + op->o_tmpfree( f, op->o_tmpmemctx ); } void -filter2bv( Filter *f, struct berval *fstr ) +filter_free( Filter *f ) +{ + Operation op; + + op.o_tmpmemctx = NULL; + op.o_tmpfree = (BER_MEMFREE_FN *)ch_free; + filter_free_x( &op, f ); +} + +void +filter2bv_x( Filter *f, struct berval *fstr, void *ctx ) { int i; Filter *p; struct berval tmp; ber_len_t len; + BER_MEMALLOC_FN *alloc = ctx ? sl_malloc : (BER_MEMALLOC_FN *)ch_malloc; + BER_MEMREALLOC_FN *reallo = ctx ? sl_realloc : (BER_MEMREALLOC_FN *)ch_realloc; if ( f == NULL ) { - ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr ); + ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, ctx ); return; } switch ( f->f_choice ) { case LDAP_FILTER_EQUALITY: - filter_escape_value( &f->f_av_value, &tmp ); + filter_escape_value_x( &f->f_av_value, &tmp, ctx ); fstr->bv_len = f->f_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = alloc( fstr->bv_len + 1, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)", f->f_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); break; case LDAP_FILTER_GE: - filter_escape_value( &f->f_av_value, &tmp ); + filter_escape_value_x( &f->f_av_value, &tmp, ctx ); fstr->bv_len = f->f_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(>=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = alloc( fstr->bv_len + 1, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)", f->f_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); break; case LDAP_FILTER_LE: - filter_escape_value( &f->f_av_value, &tmp ); + filter_escape_value_x( &f->f_av_value, &tmp, ctx ); fstr->bv_len = f->f_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(<=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = alloc( fstr->bv_len + 1, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)", f->f_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); break; case LDAP_FILTER_APPROX: - filter_escape_value( &f->f_av_value, &tmp ); + filter_escape_value_x( &f->f_av_value, &tmp, ctx ); fstr->bv_len = f->f_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(~=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = alloc( fstr->bv_len + 1, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)", f->f_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); break; case LDAP_FILTER_SUBSTRINGS: fstr->bv_len = f->f_sub_desc->ad_cname.bv_len + ( sizeof("(=*)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); + fstr->bv_val = alloc( fstr->bv_len + 128, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", f->f_sub_desc->ad_cname.bv_val ); @@ -676,46 +701,46 @@ filter2bv( Filter *f, struct berval *fstr ) if ( f->f_sub_initial.bv_val != NULL ) { len = fstr->bv_len; - filter_escape_value( &f->f_sub_initial, &tmp ); + filter_escape_value_x( &f->f_sub_initial, &tmp, ctx ); fstr->bv_len += tmp.bv_len; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx ); snprintf( &fstr->bv_val[len-2], tmp.bv_len+3, /* "(attr=" */ "%s*)", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); } if ( f->f_sub_any != NULL ) { for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) { len = fstr->bv_len; - filter_escape_value( &f->f_sub_any[i], &tmp ); + filter_escape_value_x( &f->f_sub_any[i], &tmp, ctx ); fstr->bv_len += tmp.bv_len + 1; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx ); snprintf( &fstr->bv_val[len-1], tmp.bv_len+3, /* "(attr=[init]*[any*]" */ "%s*)", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); } } if ( f->f_sub_final.bv_val != NULL ) { len = fstr->bv_len; - filter_escape_value( &f->f_sub_final, &tmp ); + filter_escape_value_x( &f->f_sub_final, &tmp, ctx ); fstr->bv_len += tmp.bv_len; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx ); snprintf( &fstr->bv_val[len-1], tmp.bv_len+3, /* "(attr=[init*][any*]" */ "%s)", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); } break; @@ -723,7 +748,7 @@ filter2bv( Filter *f, struct berval *fstr ) case LDAP_FILTER_PRESENT: fstr->bv_len = f->f_desc->ad_cname.bv_len + ( sizeof("(=*)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = alloc( fstr->bv_len + 1, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", f->f_desc->ad_cname.bv_val ); @@ -733,7 +758,7 @@ filter2bv( Filter *f, struct berval *fstr ) case LDAP_FILTER_OR: case LDAP_FILTER_NOT: fstr->bv_len = sizeof("(%)") - 1; - fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); + fstr->bv_val = alloc( fstr->bv_len + 128, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)", f->f_choice == LDAP_FILTER_AND ? '&' : @@ -745,7 +770,7 @@ filter2bv( Filter *f, struct berval *fstr ) filter2bv( p, &tmp ); fstr->bv_len += tmp.bv_len; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, ctx ); snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, /*"("*/ "%s)", tmp.bv_val ); @@ -757,7 +782,7 @@ filter2bv( Filter *f, struct berval *fstr ) case LDAP_FILTER_EXT: { struct berval ad; - filter_escape_value( &f->f_mr_value, &tmp ); + filter_escape_value_x( &f->f_mr_value, &tmp, ctx ); if ( f->f_mr_desc ) { ad = f->f_mr_desc->ad_cname; @@ -770,7 +795,7 @@ filter2bv( Filter *f, struct berval *fstr ) ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) + ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) + tmp.bv_len + ( sizeof("(:=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = alloc( fstr->bv_len + 1, ctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)", ad.bv_val, @@ -778,11 +803,11 @@ filter2bv( Filter *f, struct berval *fstr ) f->f_mr_rule_text.bv_len ? ":" : "", f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, ctx ); } break; case SLAPD_FILTER_COMPUTED: - ber_str2bv( + ber_str2bv_x( f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" : f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" : f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" : @@ -791,25 +816,33 @@ filter2bv( Filter *f, struct berval *fstr ) f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 : f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1, - 1, fstr ); + 1, fstr, ctx ); break; default: - ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr ); + ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr, ctx ); break; } } +void +filter2bv( Filter *f, struct berval *fstr ) +{ + filter2bv_x( f, fstr, NULL ); +} + int -filter_escape_value( +filter_escape_value_x( struct berval *in, - struct berval *out ) + struct berval *out, + void *ctx ) { ber_len_t i; assert( in ); assert( out ); - out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 ); + i = in->bv_len * 3 + 1; + out->bv_val = ctx ? sl_malloc( i, ctx ) : ch_malloc( i ); out->bv_len = 0; for( i=0; i < in->bv_len ; i++ ) { @@ -826,9 +859,17 @@ filter_escape_value( return LDAP_SUCCESS; } +int +filter_escape_value( + struct berval *in, + struct berval *out ) +{ + return filter_escape_value_x( in, out, NULL ); +} + static int get_simple_vrFilter( - Connection *conn, + Operation *op, BerElement *ber, ValuesReturnFilter **filt, const char **text ) @@ -840,7 +881,7 @@ get_simple_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_simple_vrFilter: conn %d\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 ); #endif @@ -861,11 +902,11 @@ get_simple_vrFilter( case LDAP_FILTER_EQUALITY: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL2, - "get_simple_vrFilter: conn %d EQUALITY\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d EQUALITY\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 ); #endif - err = get_ava( ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text ); + err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -876,21 +917,21 @@ get_simple_vrFilter( case LDAP_FILTER_SUBSTRINGS: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_simple_vrFilter: conn %d SUBSTRINGS\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 ); #endif - err = get_ssa( conn, ber, &vrf.vrf_sub, text ); + err = get_ssa( op, ber, &vrf.vrf_sub, text ); break; case LDAP_FILTER_GE: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_simple_vrFilter: conn %d GE\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d GE\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 ); #endif - err = get_ava( ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text ); + err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -899,11 +940,11 @@ get_simple_vrFilter( case LDAP_FILTER_LE: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_simple_vrFilter: conn %d LE\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d LE\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 ); #endif - err = get_ava( ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text ); + err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -914,7 +955,7 @@ get_simple_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_simple_vrFilter: conn %d PRESENT\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 ); #endif @@ -939,11 +980,11 @@ get_simple_vrFilter( case LDAP_FILTER_APPROX: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_simple_vrFilter: conn %d APPROX\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d APPROX\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 ); #endif - err = get_ava( ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text ); + err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -952,12 +993,12 @@ get_simple_vrFilter( case LDAP_FILTER_EXT: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_simple_vrFilter: conn %d EXTENSIBLE\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 ); #endif - err = get_mra( ber, &vrf.vrf_mra, text ); + err = get_mra( op, ber, &vrf.vrf_mra, text ); if ( err != LDAP_SUCCESS ) { break; } @@ -970,7 +1011,7 @@ get_simple_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, ERR, "get_simple_vrFilter: conn %d unknown filter type=%lu\n", - conn->c_connid, vrf.vrf_choice, 0 ); + op->o_connid, vrf.vrf_choice, 0 ); #else Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n", vrf.vrf_choice, 0, 0 ); @@ -994,7 +1035,7 @@ get_simple_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL2, - "get_simple_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 ); + "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 ); #endif @@ -1003,7 +1044,7 @@ get_simple_vrFilter( } int -get_vrFilter( Connection *conn, BerElement *ber, +get_vrFilter( Operation *op, BerElement *ber, ValuesReturnFilter **vrf, const char **text ) { @@ -1043,7 +1084,7 @@ get_vrFilter( Connection *conn, BerElement *ber, #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_vrFilter: conn %d start\n", conn->c_connid, 0, 0 ); + "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 ); #endif @@ -1065,7 +1106,7 @@ get_vrFilter( Connection *conn, BerElement *ber, tag != LBER_DEFAULT; tag = ber_next_element( ber, &len, last ) ) { - int err = get_simple_vrFilter( conn, ber, n, text ); + int err = get_simple_vrFilter( op, ber, n, text ); if ( err != LDAP_SUCCESS ) return( err ); @@ -1075,7 +1116,7 @@ get_vrFilter( Connection *conn, BerElement *ber, #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_vrFilter: conn %d exit\n", conn->c_connid, 0, 0 ); + "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 ); #endif @@ -1083,7 +1124,7 @@ get_vrFilter( Connection *conn, BerElement *ber, } void -vrFilter_free( ValuesReturnFilter *vrf ) +vrFilter_free( Operation *op, ValuesReturnFilter *vrf ) { ValuesReturnFilter *p, *next; @@ -1102,22 +1143,30 @@ vrFilter_free( ValuesReturnFilter *vrf ) case LDAP_FILTER_GE: case LDAP_FILTER_LE: case LDAP_FILTER_APPROX: - ava_free( vrf->vrf_ava, 1 ); + ava_free( op, vrf->vrf_ava, 1 ); break; case LDAP_FILTER_SUBSTRINGS: if ( vrf->vrf_sub_initial.bv_val != NULL ) { - free( vrf->vrf_sub_initial.bv_val ); +#ifdef notyet + op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx ); +#else + ch_free( vrf->vrf_sub_initial.bv_val ); +#endif } - ber_bvarray_free( vrf->vrf_sub_any ); + ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx ); if ( vrf->vrf_sub_final.bv_val != NULL ) { - free( vrf->vrf_sub_final.bv_val ); +#ifdef notyet + op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx ); +#else + ch_free( vrf->vrf_sub_final.bv_val ); +#endif } - ch_free( vrf->vrf_sub ); + op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx ); break; case LDAP_FILTER_EXT: - mra_free( vrf->vrf_mra, 1 ); + mra_free( op, vrf->vrf_mra, 1 ); break; case SLAPD_FILTER_COMPUTED: @@ -1134,114 +1183,117 @@ vrFilter_free( ValuesReturnFilter *vrf ) break; } - free( vrf ); + op->o_tmpfree( vrf, op->o_tmpmemctx ); } } - void -vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) +vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr ) { ValuesReturnFilter *p; struct berval tmp; ber_len_t len; + BER_MEMREALLOC_FN *reallo = op->o_tmpmemctx ? sl_realloc : + (BER_MEMREALLOC_FN *)ch_realloc; if ( vrf == NULL ) { - ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr ); + ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx ); return; } fstr->bv_len = sizeof("()") - 1; - fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "()"); for ( p = vrf; p != NULL; p = p->vrf_next ) { len = fstr->bv_len; - simple_vrFilter2bv( p, &tmp ); + simple_vrFilter2bv( op, p, &tmp ); fstr->bv_len += tmp.bv_len; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, /*"("*/ "%s)", tmp.bv_val ); - ch_free( tmp.bv_val ); + op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx ); } } static void -simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) +simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr ) { struct berval tmp; ber_len_t len; + BER_MEMREALLOC_FN *reallo = op->o_tmpmemctx ? sl_realloc : + (BER_MEMREALLOC_FN *) ch_realloc; if ( vrf == NULL ) { - ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr ); + ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx ); return; } switch ( vrf->vrf_choice ) { case LDAP_FILTER_EQUALITY: - filter_escape_value( &vrf->vrf_av_value, &tmp ); + filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx ); fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)", vrf->vrf_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; case LDAP_FILTER_GE: - filter_escape_value( &vrf->vrf_av_value, &tmp ); + filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx ); fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(>=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)", vrf->vrf_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; case LDAP_FILTER_LE: - filter_escape_value( &vrf->vrf_av_value, &tmp ); + filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx ); fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(<=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)", vrf->vrf_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; case LDAP_FILTER_APPROX: - filter_escape_value( &vrf->vrf_av_value, &tmp ); + filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx ); fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(~=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)", vrf->vrf_av_desc->ad_cname.bv_val, tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; case LDAP_FILTER_SUBSTRINGS: fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len + ( sizeof("(=*)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", vrf->vrf_sub_desc->ad_cname.bv_val ); @@ -1249,47 +1301,47 @@ simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) if ( vrf->vrf_sub_initial.bv_val != NULL ) { len = fstr->bv_len; - filter_escape_value( &vrf->vrf_sub_initial, &tmp ); + filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx ); fstr->bv_len += tmp.bv_len; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( &fstr->bv_val[len-2], tmp.bv_len+3, /* "(attr=" */ "%s*)", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } if ( vrf->vrf_sub_any != NULL ) { int i; for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) { len = fstr->bv_len; - filter_escape_value( &vrf->vrf_sub_any[i], &tmp ); + filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp, op->o_tmpmemctx ); fstr->bv_len += tmp.bv_len + 1; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( &fstr->bv_val[len-1], tmp.bv_len+3, /* "(attr=[init]*[any*]" */ "%s*)", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } } if ( vrf->vrf_sub_final.bv_val != NULL ) { len = fstr->bv_len; - filter_escape_value( &vrf->vrf_sub_final, &tmp ); + filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx ); fstr->bv_len += tmp.bv_len; - fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 ); + fstr->bv_val = reallo( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( &fstr->bv_val[len-1], tmp.bv_len+3, /* "(attr=[init*][any*]" */ "%s)", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } break; @@ -1297,7 +1349,7 @@ simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) case LDAP_FILTER_PRESENT: fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len + ( sizeof("(=*)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", vrf->vrf_desc->ad_cname.bv_val ); @@ -1305,7 +1357,7 @@ simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) case LDAP_FILTER_EXT: { struct berval ad; - filter_escape_value( &vrf->vrf_mr_value, &tmp ); + filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx ); if ( vrf->vrf_mr_desc ) { ad = vrf->vrf_mr_desc->ad_cname; @@ -1318,7 +1370,7 @@ simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) + ( vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) + tmp.bv_len + ( sizeof("(:=)") - 1 ); - fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); + fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx ); snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)", ad.bv_val, @@ -1327,11 +1379,11 @@ simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "", tmp.bv_val ); - ber_memfree( tmp.bv_val ); + ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } break; case SLAPD_FILTER_COMPUTED: - ber_str2bv( + ber_str2bv_x( vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" : vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" : vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" : @@ -1340,11 +1392,11 @@ simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 : vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1, - 1, fstr ); + 1, fstr, op->o_tmpmemctx ); break; default: - ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr ); + ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr, op->o_tmpmemctx ); break; } } @@ -1352,7 +1404,7 @@ simple_vrFilter2bv( ValuesReturnFilter *vrf, struct berval *fstr ) #if 0 /* unused */ static int get_substring_vrFilter( - Connection *conn, + Operation *op, BerElement *ber, ValuesReturnFilter *vrf, const char **text ) @@ -1367,7 +1419,7 @@ get_substring_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_substring_filter: conn %d begin\n", conn->c_connid, 0, 0 ); + "get_substring_filter: conn %d begin\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 ); #endif @@ -1426,7 +1478,7 @@ get_substring_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, ERR, "get_filter_substring: conn %d unknown substring choice=%ld\n", - conn->c_connid, (long)tag, 0 ); + op->o_connid, (long)tag, 0 ); #else Debug( LDAP_DEBUG_FILTER, " unknown substring choice=%ld\n", @@ -1452,7 +1504,7 @@ get_substring_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, "get_substring_filter: conn %d INITIAL\n", - conn->c_connid, 0, 0 ); + op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 ); #endif @@ -1471,7 +1523,7 @@ get_substring_vrFilter( case LDAP_SUBSTRING_ANY: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_substring_filter: conn %d ANY\n", conn->c_connid, 0, 0 ); + "get_substring_filter: conn %d ANY\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 ); #endif @@ -1487,7 +1539,7 @@ get_substring_vrFilter( case LDAP_SUBSTRING_FINAL: #ifdef NEW_LOGGING LDAP_LOG( FILTER, DETAIL1, - "get_substring_filter: conn %d FINAL\n", conn->c_connid, 0, 0 ); + "get_substring_filter: conn %d FINAL\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 ); #endif @@ -1504,7 +1556,7 @@ get_substring_vrFilter( #ifdef NEW_LOGGING LDAP_LOG( FILTER, INFO, "get_substring_filter: conn %d unknown substring type %ld\n", - conn->c_connid, (long)tag, 0 ); + op->o_connid, (long)tag, 0 ); #else Debug( LDAP_DEBUG_FILTER, " unknown substring type=%ld\n", @@ -1517,7 +1569,7 @@ return_error: #ifdef NEW_LOGGING LDAP_LOG( FILTER, INFO, "get_substring_filter: conn %d error %ld\n", - conn->c_connid, (long)rc, 0 ); + op->o_connid, (long)rc, 0 ); #else Debug( LDAP_DEBUG_FILTER, " error=%ld\n", (long) rc, 0, 0 ); @@ -1532,7 +1584,7 @@ return_error: #ifdef NEW_LOGGING LDAP_LOG( FILTER, ENTRY, - "get_substring_filter: conn %d exit\n", conn->c_connid, 0, 0 ); + "get_substring_filter: conn %d exit\n", op->o_connid, 0, 0 ); #else Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 ); #endif diff --git a/servers/slapd/main.c b/servers/slapd/main.c index 0b39928890..4028953a7d 100644 --- a/servers/slapd/main.c +++ b/servers/slapd/main.c @@ -151,6 +151,8 @@ int main( int argc, char **argv ) } #endif + sl_mem_init(); + #ifdef HAVE_NT_SERVICE_MANAGER { int *i; diff --git a/servers/slapd/mra.c b/servers/slapd/mra.c index fc2e3e7993..7e3a179030 100644 --- a/servers/slapd/mra.c +++ b/servers/slapd/mra.c @@ -16,18 +16,21 @@ void mra_free( + Operation *op, MatchingRuleAssertion *mra, int freeit ) { + /* op->o_tmpfree( mra->ma_value.bv_val, op->o_tmpmemctx ); */ ch_free( mra->ma_value.bv_val ); if ( freeit ) { - ch_free( (char *) mra ); + op->o_tmpfree( (char *) mra, op->o_tmpmemctx ); } } int get_mra( + Operation *op, BerElement *ber, MatchingRuleAssertion **mra, const char **text @@ -218,7 +221,7 @@ get_mra( length = sizeof(ma); /* Append rule_text to end of struct */ if (rule_text.bv_val) length += rule_text.bv_len + 1; - *mra = ch_malloc( length ); + *mra = op->o_tmpalloc( length, op->o_tmpmemctx ); **mra = ma; if (rule_text.bv_val) { (*mra)->ma_rule_text.bv_len = rule_text.bv_len; diff --git a/servers/slapd/operation.c b/servers/slapd/operation.c index 30ee7830a5..95e28288b5 100644 --- a/servers/slapd/operation.c +++ b/servers/slapd/operation.c @@ -57,7 +57,7 @@ slap_op_free( Operation *op ) free( op->o_authmech.bv_val ); } if ( op->o_ctrls != NULL ) { - ldap_controls_free( op->o_ctrls ); + slap_free_ctrls( op, op->o_ctrls ); } #ifdef LDAP_CONNECTIONLESS diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index 4844df4fdc..47709eec27 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -167,11 +167,13 @@ LDAP_SLAPD_F (Attribute *) attrs_dup LDAP_P(( Attribute *a )); * ava.c */ LDAP_SLAPD_F (int) get_ava LDAP_P(( + Operation *op, BerElement *ber, AttributeAssertion **ava, unsigned usage, const char **text )); LDAP_SLAPD_F (void) ava_free LDAP_P(( + Operation *op, AttributeAssertion *ava, int freeit )); @@ -488,20 +490,22 @@ LDAP_SLAPD_F ( SLAP_EXTOP_MAIN_FN ) cancel_extop; * filter.c */ LDAP_SLAPD_F (int) get_filter LDAP_P(( - Connection *conn, + Operation *op, BerElement *ber, Filter **filt, const char **text )); LDAP_SLAPD_F (void) filter_free LDAP_P(( Filter *f )); +LDAP_SLAPD_F (void) filter_free_x LDAP_P(( Operation *op, Filter *f )); LDAP_SLAPD_F (void) filter2bv LDAP_P(( Filter *f, struct berval *bv )); +LDAP_SLAPD_F (void) filter2bv_x LDAP_P(( Filter *f, struct berval *bv, void *ctx )); -LDAP_SLAPD_F (int) get_vrFilter LDAP_P(( Connection *conn, BerElement *ber, +LDAP_SLAPD_F (int) get_vrFilter LDAP_P(( Operation *op, BerElement *ber, ValuesReturnFilter **f, const char **text )); -LDAP_SLAPD_F (void) vrFilter_free LDAP_P(( ValuesReturnFilter *f )); -LDAP_SLAPD_F (void) vrFilter2bv LDAP_P(( ValuesReturnFilter *f, struct berval *fstr )); +LDAP_SLAPD_F (void) vrFilter_free LDAP_P(( Operation *op, ValuesReturnFilter *f )); +LDAP_SLAPD_F (void) vrFilter2bv LDAP_P(( Operation *op, ValuesReturnFilter *f, struct berval *fstr )); LDAP_SLAPD_F (int) filter_has_subordinates LDAP_P(( Filter *filter )); LDAP_SLAPD_F (int) filter_escape_value LDAP_P(( struct berval *in, @@ -669,10 +673,12 @@ LDAP_SLAPD_F (int) mr_usable_with_at( MatchingRule *mr, * mra.c */ LDAP_SLAPD_F (int) get_mra LDAP_P(( + Operation *op, BerElement *ber, MatchingRuleAssertion **mra, const char **text )); LDAP_SLAPD_F (void) mra_free LDAP_P(( + Operation *op, MatchingRuleAssertion *mra, int freeit )); @@ -863,7 +869,7 @@ LDAP_SLAPD_F (int) slap_sasl_config( const char *fname, int lineno ); -LDAP_SLAPD_F (int) slap_sasl_getdn( Connection *conn, +LDAP_SLAPD_F (int) slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len, char *user_realm, struct berval *dn, int flags ); @@ -871,7 +877,7 @@ LDAP_SLAPD_F (int) slap_sasl_getdn( Connection *conn, * saslauthz.c */ LDAP_SLAPD_F (void) slap_sasl2dn LDAP_P(( - Connection *conn, + Operation *op, struct berval *saslname, struct berval *dn )); LDAP_SLAPD_F (int) slap_sasl_authorized LDAP_P(( @@ -961,6 +967,14 @@ LDAP_SLAPD_F (char *) scherr2str LDAP_P((int code)) LDAP_GCCATTR((const)); LDAP_SLAPD_F (int) dscompare LDAP_P(( const char *s1, const char *s2del, char delim )); +/* + * sl_malloc.c + */ +LDAP_SLAPD_F (void *) sl_malloc LDAP_P(( ber_len_t size, void *ctx )); +LDAP_SLAPD_F (void *) sl_realloc LDAP_P(( void *block, ber_len_t size, void *ctx )); +LDAP_SLAPD_F (void *) sl_calloc LDAP_P(( ber_len_t nelem, ber_len_t size, void *ctx )); +LDAP_SLAPD_F (void) sl_free LDAP_P(( void *, void *ctx )); + /* * starttls.c */ @@ -970,6 +984,7 @@ LDAP_SLAPD_F (SLAP_EXTOP_MAIN_FN) starttls_extop; * str2filter.c */ LDAP_SLAPD_F (Filter *) str2filter LDAP_P(( const char *str )); +LDAP_SLAPD_F (Filter *) str2filter_x LDAP_P(( Operation *op, const char *str )); /* syntax.c */ LDAP_SLAPD_F (Syntax *) syn_find LDAP_P(( diff --git a/servers/slapd/sasl.c b/servers/slapd/sasl.c index d8c0c06305..96a5b59878 100644 --- a/servers/slapd/sasl.c +++ b/servers/slapd/sasl.c @@ -438,6 +438,9 @@ slap_auxprop_lookup( op.o_do_not_cache = 1; op.o_is_auth_check = 1; op.o_threadctx = conn->c_sasl_bindop->o_threadctx; + op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx; + op.o_tmpalloc = conn->c_sasl_bindop->o_tmpalloc; + op.o_tmpfree = conn->c_sasl_bindop->o_tmpfree; op.o_conn = conn; op.o_connid = conn->c_connid; op.ors_scope = LDAP_SCOPE_BASE; @@ -528,7 +531,7 @@ slap_sasl_checkpass( * find an answer here. */ - rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &op.o_req_ndn, + rc = slap_sasl_getdn( conn, NULL, (char *)username, 0, NULL, &op.o_req_ndn, SLAP_GETDN_AUTHCID ); if ( rc != LDAP_SUCCESS ) { sasl_seterror( sconn, 0, ldap_err2string( rc ) ); @@ -561,6 +564,9 @@ slap_sasl_checkpass( op.o_do_not_cache = 1; op.o_is_auth_check = 1; op.o_threadctx = conn->c_sasl_bindop->o_threadctx; + op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx; + op.o_tmpalloc = conn->c_sasl_bindop->o_tmpalloc; + op.o_tmpfree = conn->c_sasl_bindop->o_tmpfree; op.o_conn = conn; op.o_connid = conn->c_connid; op.ors_scope = LDAP_SCOPE_BASE; @@ -671,7 +677,7 @@ slap_sasl_canonicalize( if ( !rc ) goto done; } - rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn, + rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn, (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID ); if ( rc != LDAP_SUCCESS ) { sasl_seterror( sconn, 0, ldap_err2string( rc ) ); @@ -836,7 +842,7 @@ slap_sasl_authorize( /* Convert the identities to DN's. If no authzid was given, client will be bound as the DN matching their username */ - rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, + rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm, &authcDN, SLAP_GETDN_AUTHCID ); if( rc != LDAP_SUCCESS ) { *errstr = ldap_err2string( rc ); @@ -855,7 +861,7 @@ slap_sasl_authorize( conn->c_sasl_dn = authcDN; goto ok; } - rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, + rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm, &authzDN, SLAP_GETDN_AUTHZID ); if( rc != LDAP_SUCCESS ) { ch_free( authcDN.bv_val ); @@ -1512,7 +1518,7 @@ done: static struct berval ext_bv = BER_BVC( "EXTERNAL" ); -int slap_sasl_getdn( Connection *conn, char *id, int len, +int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len, char *user_realm, struct berval *dn, int flags ) { char *c1; @@ -1663,8 +1669,12 @@ int slap_sasl_getdn( Connection *conn, char *id, int len, *dn = dn2; } + if ( !op ) { + op = conn->c_sasl_bindop; + } + /* Run thru regexp */ - slap_sasl2dn( conn, dn, &dn2 ); + slap_sasl2dn( op, dn, &dn2 ); if( dn2.bv_val ) { ch_free( dn->bv_val ); *dn = dn2; diff --git a/servers/slapd/saslauthz.c b/servers/slapd/saslauthz.c index 80ec85de23..094523d683 100644 --- a/servers/slapd/saslauthz.c +++ b/servers/slapd/saslauthz.c @@ -67,7 +67,7 @@ int slap_sasl_setpolicy( const char *arg ) /* URI format: ldap:///[?[][?[][?[]]]] */ -static int slap_parseURI( struct berval *uri, +static int slap_parseURI( Operation *op, struct berval *uri, struct berval *searchbase, int *scope, Filter **filter ) { struct berval bv; @@ -124,7 +124,7 @@ is_dn: bv.bv_len = uri->bv_len - (bv.bv_val - uri->bv_val); /* Grab the filter */ if ( ludp->lud_filter ) { - *filter = str2filter( ludp->lud_filter ); + *filter = str2filter_x( op, ludp->lud_filter ); if ( *filter == NULL ) { rc = LDAP_PROTOCOL_ERROR; goto done; @@ -138,7 +138,7 @@ is_dn: bv.bv_len = uri->bv_len - (bv.bv_val - uri->bv_val); done: if( rc != LDAP_SUCCESS ) { - if( *filter ) filter_free( *filter ); + if( *filter ) filter_free_x( op, *filter ); } ldap_free_urldesc( ludp ); @@ -383,7 +383,7 @@ static int sasl_sc_smatch( Operation *o, SlapReply *rs ) */ static -int slap_sasl_match(Connection *conn, struct berval *rule, struct berval *assertDN, struct berval *authc ) +int slap_sasl_match(Operation *opx, struct berval *rule, struct berval *assertDN, struct berval *authc ) { int rc; regex_t reg; @@ -402,7 +402,7 @@ int slap_sasl_match(Connection *conn, struct berval *rule, struct berval *assert assertDN->bv_val, rule->bv_val, 0 ); #endif - rc = slap_parseURI( rule, &op.o_req_ndn, &op.oq_search.rs_scope, &op.oq_search.rs_filter ); + rc = slap_parseURI( opx, rule, &op.o_req_ndn, &op.oq_search.rs_scope, &op.oq_search.rs_filter ); if( rc != LDAP_SUCCESS ) goto CONCLUDED; /* Massive shortcut: search scope == base */ @@ -450,9 +450,12 @@ int slap_sasl_match(Connection *conn, struct berval *rule, struct berval *assert op.o_time = slap_get_time(); op.o_do_not_cache = 1; op.o_is_auth_check = 1; - op.o_threadctx = conn->c_sasl_bindop->o_threadctx; - op.o_conn = conn; - op.o_connid = conn->c_connid; + op.o_threadctx = opx->o_threadctx; + op.o_tmpmemctx = opx->o_tmpmemctx; + op.o_tmpalloc = opx->o_tmpalloc; + op.o_tmpfree = opx->o_tmpfree; + op.o_conn = opx->o_conn; + op.o_connid = opx->o_connid; op.o_bd->be_search( &op, &rs ); @@ -464,7 +467,7 @@ int slap_sasl_match(Connection *conn, struct berval *rule, struct berval *assert CONCLUDED: if( op.o_req_ndn.bv_len ) ch_free( op.o_req_ndn.bv_val ); - if( op.oq_search.rs_filter ) filter_free( op.oq_search.rs_filter ); + if( op.oq_search.rs_filter ) filter_free_x( opx, op.oq_search.rs_filter ); #ifdef NEW_LOGGING LDAP_LOG( TRANSPORT, ENTRY, @@ -512,7 +515,7 @@ slap_sasl_check_authz( Connection *conn, /* Check if the *assertDN matches any **vals */ for( i=0; vals[i].bv_val != NULL; i++ ) { - rc = slap_sasl_match( conn, &vals[i], assertDN, authc ); + rc = slap_sasl_match( conn->c_sasl_bindop, &vals[i], assertDN, authc ); if ( rc == LDAP_SUCCESS ) goto COMPLETE; } rc = LDAP_INAPPROPRIATE_AUTH; @@ -541,7 +544,7 @@ COMPLETE: * an internal search must be done, and if that search returns exactly one * entry, return the DN of that one entry. */ -void slap_sasl2dn( Connection *conn, +void slap_sasl2dn( Operation *opx, struct berval *saslname, struct berval *sasldn ) { int rc; @@ -569,7 +572,7 @@ void slap_sasl2dn( Connection *conn, goto FINISHED; } - rc = slap_parseURI( ®out, &op.o_req_ndn, &op.oq_search.rs_scope, &op.oq_search.rs_filter ); + rc = slap_parseURI( opx, ®out, &op.o_req_ndn, &op.oq_search.rs_scope, &op.oq_search.rs_filter ); if( regout.bv_val ) ch_free( regout.bv_val ); if( rc != LDAP_SUCCESS ) { goto FINISHED; @@ -600,17 +603,19 @@ void slap_sasl2dn( Connection *conn, goto FINISHED; } - op.o_conn = conn; - op.o_connid = conn->c_connid; + op.o_conn = opx->o_conn; + op.o_connid = opx->o_connid; op.o_tag = LDAP_REQ_SEARCH; op.o_protocol = LDAP_VERSION3; - op.o_ndn = conn->c_ndn; + op.o_ndn = opx->o_conn->c_ndn; op.o_callback = &cb; op.o_time = slap_get_time(); op.o_do_not_cache = 1; op.o_is_auth_check = 1; - op.o_threadctx = conn->c_sasl_bindop ? conn->c_sasl_bindop->o_threadctx: - ldap_pvt_thread_pool_context( &connection_pool ); + op.o_threadctx = opx->o_threadctx; + op.o_tmpmemctx = opx->o_tmpmemctx; + op.o_tmpalloc = opx->o_tmpalloc; + op.o_tmpfree = opx->o_tmpfree; op.oq_search.rs_deref = LDAP_DEREF_NEVER; op.oq_search.rs_slimit = 1; op.oq_search.rs_attrsonly = 1; @@ -619,10 +624,10 @@ void slap_sasl2dn( Connection *conn, FINISHED: if( sasldn->bv_len ) { - conn->c_authz_backend = op.o_bd; + opx->o_conn->c_authz_backend = op.o_bd; } if( op.o_req_ndn.bv_len ) ch_free( op.o_req_ndn.bv_val ); - if( op.oq_search.rs_filter ) filter_free( op.oq_search.rs_filter ); + if( op.oq_search.rs_filter ) filter_free_x( opx, op.oq_search.rs_filter ); #ifdef NEW_LOGGING LDAP_LOG( TRANSPORT, ENTRY, diff --git a/servers/slapd/search.c b/servers/slapd/search.c index 1b85eb0e65..61e4dd8f57 100644 --- a/servers/slapd/search.c +++ b/servers/slapd/search.c @@ -28,7 +28,7 @@ #ifdef LDAP_SLAPI #include "slapi.h" -static char **anlist2charray( AttributeName *an ); +static char **anlist2charray( Operation *op, AttributeName *an ); static void initSearchPlugin( Operation *op, char **attrs, int managedsait ); static int doPreSearchPluginFNs( Operation *op ); static int doSearchRewriteFNs( Operation *op ); @@ -135,7 +135,7 @@ do_search( #endif /* filter - returns a "normalized" version */ - rs->sr_err = get_filter( op->o_conn, op->o_ber, &op->ors_filter, &rs->sr_text ); + rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text ); if( rs->sr_err != LDAP_SUCCESS ) { if( rs->sr_err == SLAPD_DISCONNECT ) { rs->sr_err = LDAP_PROTOCOL_ERROR; @@ -145,7 +145,7 @@ do_search( } goto return_results; } - filter2bv( op->ors_filter, &op->ors_filterstr ); + filter2bv_x( op->ors_filter, &op->ors_filterstr, op->o_tmpmemctx ); #ifdef NEW_LOGGING LDAP_LOG( OPERATION, ARGS, @@ -259,7 +259,7 @@ do_search( } #ifdef LDAP_SLAPI - attrs = anlist2charray( op->ors_attrs ); + attrs = anlist2charray( op, op->ors_attrs ); initSearchPlugin( op, attrs, manageDSAit ); rs->sr_err = doPreSearchPluginFNs( op ); if ( rs->sr_err == LDAP_SUCCESS ) { @@ -278,7 +278,7 @@ do_search( } #ifdef LDAP_SLAPI - attrs = anlist2charray( op->ors_attrs ); + attrs = anlist2charray( op, op->ors_attrs ); initSearchPlugin( op, attrs, manageDSAit ); rs->sr_err = doPreSearchPluginFNs( op ); if ( rs->sr_err == LDAP_SUCCESS ) { @@ -356,7 +356,7 @@ do_search( } #ifdef LDAP_SLAPI - attrs = anlist2charray( op->ors_attrs ); + attrs = anlist2charray( op, op->ors_attrs ); initSearchPlugin( op, attrs, manageDSAit ); rs->sr_err = doPreSearchPluginFNs( op ); if ( rs->sr_err != LDAP_SUCCESS ) { @@ -395,11 +395,11 @@ return_results:; if( op->o_req_dn.bv_val != NULL) free( op->o_req_dn.bv_val ); if( op->o_req_ndn.bv_val != NULL) free( op->o_req_ndn.bv_val ); - if( op->ors_filterstr.bv_val != NULL) free( op->ors_filterstr.bv_val ); - if( op->ors_filter != NULL) filter_free( op->ors_filter ); - if( op->ors_attrs != NULL ) free( op->ors_attrs ); + if( op->ors_filterstr.bv_val != NULL) op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx ); + if( op->ors_filter != NULL) filter_free_x( op, op->ors_filter ); + if( op->ors_attrs != NULL ) op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx ); #ifdef LDAP_SLAPI - if( attrs != NULL) ch_free( attrs ); + if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx ); #endif /* LDAP_SLAPI */ return rs->sr_err; @@ -407,7 +407,7 @@ return_results:; #ifdef LDAP_SLAPI -static char **anlist2charray( AttributeName *an ) +static char **anlist2charray( Operation *op, AttributeName *an ) { char **attrs; int i; @@ -415,7 +415,7 @@ static char **anlist2charray( AttributeName *an ) if ( an != NULL ) { for ( i = 0; an[i].an_name.bv_val != NULL; i++ ) ; - attrs = (char **)ch_malloc( (i + 1) * sizeof(char *) ); + attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx ); for ( i = 0; an[i].an_name.bv_val != NULL; i++ ) { attrs[i] = an[i].an_name.bv_val; } @@ -477,8 +477,8 @@ static int doSearchRewriteFNs( Operation *op ) * SLAPI_SEARCH_STRFILER is not normative. */ slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter); - ch_free( op->ors_filterstr.bv_val ); - filter2bv( op->ors_filter, &op->ors_filterstr ); + op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx ); + filter2bv_x( op->ors_filter, &op->ors_filterstr, op->o_tmpmemctx ); #ifdef NEW_LOGGING LDAP_LOG( OPERATION, ARGS, "doSearchRewriteFNs: after compute_rewrite_search filter: %s\n", diff --git a/servers/slapd/sl_malloc.c b/servers/slapd/sl_malloc.c new file mode 100644 index 0000000000..fff4270418 --- /dev/null +++ b/servers/slapd/sl_malloc.c @@ -0,0 +1,181 @@ +/* sl_malloc.c - malloc routines using a per-thread slab */ +/* $OpenLDAP$ */ +/* + * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ + +#include "portable.h" + +#include +#include + +#include "slap.h" + +struct slab_heap { + void *h_base; + void *h_last; + void *h_end; +}; + +static void +sl_mem_destroy( + void *key, + void *data +) +{ + struct slab_heap *sh = data; + + ch_free(sh->h_base); + ch_free(sh); +} + +BER_MEMALLOC_FN sl_malloc; +BER_MEMCALLOC_FN sl_calloc; +BER_MEMREALLOC_FN sl_realloc; +BER_MEMFREE_FN sl_free; + +static BerMemoryFunctions sl_bmf = + { sl_malloc, sl_calloc, sl_realloc, sl_free }; + +void +sl_mem_init() +{ + ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &sl_bmf ); +} + +void +sl_mem_create( + ber_len_t size, + void *ctx +) +{ + struct slab_heap *sh = NULL; + int pad = 2*sizeof(int)-1; + + ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL ); + + /* round up to doubleword boundary */ + size += pad; + size &= ~pad; + + if (!sh) { + sh = ch_malloc( sizeof(struct slab_heap) ); + sh->h_base = ch_malloc( size ); + ldap_pvt_thread_pool_setkey( ctx, sl_mem_init, (void *)sh, sl_mem_destroy ); + } else if ( size > sh->h_end - sh->h_base ) { + sh->h_base = ch_realloc( sh->h_base, size ); + } + sh->h_last = sh->h_base; + sh->h_end = sh->h_base + size; +} + +void * +sl_malloc( + ber_len_t size, + void *ctx +) +{ + struct slab_heap *sh = NULL; + int pad = 2*sizeof(int)-1; + ber_len_t *new; + + /* ber_set_option calls us like this */ + if (!ctx) return ber_memalloc_x( size, NULL ); + + ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL ); + + /* round up to doubleword boundary */ + size += pad + sizeof( ber_len_t ); + size &= ~pad; + + if (sh->h_last + size >= sh->h_end ) { +#ifdef NEW_LOGGING + LDAP_LOG( OPERATION, ERR, + "sl_malloc: allocation of %lu bytes failed\n", (long)size, 0,0 ); +#else + Debug( LDAP_DEBUG_ANY, "sl_malloc of %lu bytes failed\n", + (long) size, 0, 0 ); +#endif + assert( 0 ); + exit( EXIT_FAILURE ); + } + new = sh->h_last; + *new++ = size - sizeof(ber_len_t); + sh->h_last += size; + + return( (void *)new ); +} + +void * +sl_calloc( ber_len_t n, ber_len_t size, void *ctx ) +{ + void *new; + + new = sl_malloc( n*size, ctx ); + if ( new ) { + memset( new, 0, n*size ); + } + return new; +} + +void * +sl_realloc( void *ptr, ber_len_t size, void *ctx ) +{ + struct slab_heap *sh = NULL; + int pad = 2*sizeof(int)-1; + ber_len_t *p = (ber_len_t *)ptr; + ber_len_t *new; + + ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL ); + + if ( ptr == NULL ) return sl_malloc( size, ctx ); + + if ( ptr < sh->h_base || ptr >= sh->h_end ) { +#ifdef NEW_LOGGING + LDAP_LOG( OPERATION, ERR, + "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 ); +#else + Debug( LDAP_DEBUG_ANY, + "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 ); +#endif + assert( 0 ); + exit( EXIT_FAILURE ); + } + + if ( size == 0 ) return NULL; + + /* round up to doubleword boundary */ + size += pad + sizeof( ber_len_t ); + size &= ~pad; + + /* We always alloc a new block */ + if (size <= p[-1]) { + p[-1] = size; + new = p; + } else { + new = sl_malloc( size, ctx ); + AC_MEMCPY( new, ptr, p[-1] ); + } + return new; +} + +void +sl_free( void *ptr, void *ctx ) +{ + struct slab_heap *sh = NULL; + + ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL ); + + if ( ptr < sh->h_base || ptr >= sh->h_end ) { +#ifdef NEW_LOGGING + LDAP_LOG( OPERATION, ERR, + "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 ); +#else + Debug( LDAP_DEBUG_ANY, + "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 ); +#endif + assert( 0 ); + exit( EXIT_FAILURE ); + } +} diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h index 0a27f92f04..22faaff70c 100644 --- a/servers/slapd/slap.h +++ b/servers/slapd/slap.h @@ -1839,6 +1839,9 @@ typedef struct slap_op { LDAPControl **o_ctrls; /* controls */ void *o_threadctx; /* thread pool thread context */ + void *o_tmpmemctx; /* slab malloc context */ + BER_MEMALLOC_FN *o_tmpalloc; + BER_MEMFREE_FN *o_tmpfree; void *o_private; /* anything the backend needs */ LDAP_STAILQ_ENTRY(slap_op) o_next; /* next operation in list */ diff --git a/servers/slapd/str2filter.c b/servers/slapd/str2filter.c index 04667cc345..710bd13b10 100644 --- a/servers/slapd/str2filter.c +++ b/servers/slapd/str2filter.c @@ -24,13 +24,12 @@ static Filter *str2simple( const char *str); static int str2subvals( const char *val, Filter *f); Filter * -str2filter( const char *str ) +str2filter_x( Operation *op, const char *str ) { int rc; Filter *f = NULL; - char berbuf[256]; + char berbuf[LBER_ELEMENT_SIZEOF]; BerElement *ber = (BerElement *)berbuf; - Connection conn; const char *text = NULL; #ifdef NEW_LOGGING @@ -44,6 +43,9 @@ str2filter( const char *str ) } ber_init2( ber, NULL, LBER_USE_DER ); + if ( op->o_tmpmemctx ) { + ber_set_option( ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx ); + } rc = ldap_pvt_put_filter( ber, str ); if( rc < 0 ) { @@ -52,12 +54,22 @@ str2filter( const char *str ) ber_reset( ber, 1 ); - conn.c_connid = 0; - - rc = get_filter( &conn, ber, &f, &text ); + rc = get_filter( op, ber, &f, &text ); done: ber_free_buf( ber ); return f; } + +Filter * +str2filter( const char *str ) +{ + Operation op = {0}; + + op.o_tmpmemctx = NULL; + op.o_tmpalloc = ch_malloc; + op.o_tmpfree = ch_free; + + return str2filter_x( &op, str ); +}