]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/search.c
ITS#5860 - more for entry cache counts
[openldap] / servers / slapd / back-ldap / search.c
index 08a56920615ed65b3fa5511f3558197ff26bccbb..b3a68b31411e21f23fe4793354cf1e228e3138b6 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1999-2008 The OpenLDAP Foundation.
+ * Copyright 1999-2009 The OpenLDAP Foundation.
  * Portions Copyright 1999-2003 Howard Chu.
  * Portions Copyright 2000-2003 Pierangelo Masarati.
  * All rights reserved.
@@ -47,7 +47,8 @@ ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
 static int
 ldap_back_munge_filter(
        Operation       *op,
-       struct berval   *filter )
+       struct berval   *filter,
+       int     *freeit )
 {
        ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
 
@@ -122,6 +123,7 @@ ldap_back_munge_filter(
                        AC_MEMCPY( filter->bv_val, op->ors_filterstr.bv_val,
                                        op->ors_filterstr.bv_len + 1 );
 
+                       *freeit = 1;
                } else {
                        filter->bv_val = op->o_tmprealloc( filter->bv_val,
                                        filter->bv_len + 1, op->o_tmpmemctx );
@@ -163,7 +165,7 @@ ldap_back_search(
                        filter = BER_BVNULL;
        int             i;
        char            **attrs = NULL;
-       int             freetext = 0;
+       int             freetext = 0, freefilter = 0;
        int             do_retry = 1, dont_retry = 0;
        LDAPControl     **ctrls = NULL;
        char            **references = NULL;
@@ -180,12 +182,6 @@ ldap_back_search(
         * to map attrs and maybe rewrite value
         */
 
-       /* should we check return values? */
-       if ( op->ors_deref != -1 ) {
-               ldap_set_option( lc->lc_ld, LDAP_OPT_DEREF,
-                               (void *)&op->ors_deref );
-       }
-
        if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
                tv.tv_sec = op->ors_tlimit;
                tv.tv_usec = 0;
@@ -221,11 +217,11 @@ ldap_back_search(
        /* deal with <draft-zeilenga-ldap-t-f> filters */
        filter = op->ors_filterstr;
 retry:
-       rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_dn.bv_val,
+       rs->sr_err = ldap_pvt_search( lc->lc_ld, op->o_req_dn.bv_val,
                        op->ors_scope, filter.bv_val,
                        attrs, op->ors_attrsonly, ctrls, NULL,
                        tv.tv_sec ? &tv : NULL,
-                       op->ors_slimit, &msgid );
+                       op->ors_slimit, op->ors_deref, &msgid );
 
        if ( rs->sr_err != LDAP_SUCCESS ) {
                switch ( rs->sr_err ) {
@@ -248,7 +244,7 @@ retry:
                        goto finish;
 
                case LDAP_FILTER_ERROR:
-                       if (ldap_back_munge_filter( op, &filter ) > 0 ) {
+                       if (ldap_back_munge_filter( op, &filter, &freefilter ) > 0 ) {
                                goto retry;
                        }
 
@@ -429,6 +425,36 @@ retry:
                                rs->sr_ctrls = NULL;
                        }
 
+               } else if ( rc == LDAP_RES_INTERMEDIATE ) {
+                       /* FIXME: response controls
+                        * are passed without checks */
+                       rc = ldap_parse_intermediate( lc->lc_ld,
+                               res,
+                               (char **)&rs->sr_rspoid,
+                               &rs->sr_rspdata,
+                               &rs->sr_ctrls,
+                               0 );
+                       if ( rc != LDAP_SUCCESS ) {
+                               continue;
+                       }
+
+                       slap_send_ldap_intermediate( op, rs );
+
+                       if ( rs->sr_rspoid != NULL ) {
+                               ber_memfree( (char *)rs->sr_rspoid );
+                               rs->sr_rspoid = NULL;
+                       }
+
+                       if ( rs->sr_rspdata != NULL ) {
+                               ber_bvfree( rs->sr_rspdata );
+                               rs->sr_rspdata = NULL;
+                       }
+
+                       if ( rs->sr_ctrls != NULL ) {
+                               ldap_controls_free( rs->sr_ctrls );
+                               rs->sr_ctrls = NULL;
+                       }
+
                } else {
                        char            *err = NULL;
 
@@ -537,7 +563,7 @@ finish:;
                ldap_back_quarantine( op, rs );
        }
 
-       if ( filter.bv_val != op->ors_filterstr.bv_val ) {
+       if ( freefilter && filter.bv_val != op->ors_filterstr.bv_val ) {
                op->o_tmpfree( filter.bv_val, op->o_tmpmemctx );
        }
 
@@ -646,7 +672,7 @@ ldap_build_entry(
 
                attr = attr_alloc( NULL );
                if ( attr == NULL ) {
-                       continue;
+                       return LDAP_OTHER;
                }
                if ( slap_bv2ad( &a, &attr->a_desc, &text ) 
                                != LDAP_SUCCESS )
@@ -658,6 +684,8 @@ ldap_build_entry(
                                        "%s ldap_build_entry: "
                                        "slap_bv2undef_ad(%s): %s\n",
                                        op->o_log_prefix, a.bv_val, text );
+
+                               ( void )ber_scanf( &ber, "x" /* [W] */ );
                                attr_free( attr );
                                continue;
                        }
@@ -680,7 +708,6 @@ ldap_build_entry(
                         * present...
                         */
                        ( void )ber_scanf( &ber, "x" /* [W] */ );
-
                        attr_free( attr );
                        continue;
                }
@@ -704,7 +731,14 @@ ldap_build_entry(
                        goto next_attr;
                }
 
-               for ( i = 0; !BER_BVISNULL( &attr->a_vals[i] ); i++ ) {
+               for ( i = 0; !BER_BVISNULL( &attr->a_vals[i] ); i++ ) ;
+               last = i;
+
+               /*
+                * check that each value is valid per syntax
+                * and pretty if appropriate
+                */
+               for ( i = 0; i<last; i++ ) {
                        struct berval   pval;
                        int             rc;
 
@@ -727,18 +761,27 @@ ldap_build_entry(
                                        ber_dupbv( &pval, &oc->soc_cname );
 
                                } else {
-                                       attr->a_nvals = NULL;
-                                       attr_free( attr );
-                                       goto next_attr;
+                                       LBER_FREE( attr->a_vals[i].bv_val );
+                                       if ( --last == i ) {
+                                               BER_BVZERO( &attr->a_vals[i] );
+                                               break;
+                                       }
+                                       attr->a_vals[i] = attr->a_vals[last];
+                                       BER_BVZERO( &attr->a_vals[last] );
+                                       i--;
                                }
-                       }
 
-                       if ( pretty ) {
+                       } else if ( pretty ) {
                                LBER_FREE( attr->a_vals[i].bv_val );
                                attr->a_vals[i] = pval;
                        }
                }
                attr->a_numvals = last = i;
+               if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
+                       attr->a_nvals = NULL;
+                       attr_free( attr );
+                       goto next_attr;
+               }
 
                if ( last && attr->a_desc->ad_type->sat_equality &&
                                attr->a_desc->ad_type->sat_equality->smr_normalize )
@@ -747,10 +790,6 @@ ldap_build_entry(
                        for ( i = 0; i < last; i++ ) {
                                int             rc;
 
-                               /*
-                                * check that each value is valid per syntax
-                                * and pretty if appropriate
-                                */
                                rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
                                        SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
                                        attr->a_desc->ad_type->sat_syntax,
@@ -759,16 +798,52 @@ ldap_build_entry(
                                        NULL );
 
                                if ( rc != LDAP_SUCCESS ) {
-                                       BER_BVZERO( &attr->a_nvals[i] );
-                                       attr_free( attr );
-                                       goto next_attr;
+                                       LBER_FREE( attr->a_vals[i].bv_val );
+                                       if ( --last == i ) {
+                                               BER_BVZERO( &attr->a_vals[i] );
+                                               break;
+                                       }
+                                       attr->a_vals[i] = attr->a_vals[last];
+                                       BER_BVZERO( &attr->a_vals[last] );
+                                       i--;
                                }
                        }
                        BER_BVZERO( &attr->a_nvals[i] );
+                       if ( last == 0 ) {
+                               attr_free( attr );
+                               goto next_attr;
+                       }
 
                } else {
                        attr->a_nvals = attr->a_vals;
                }
+
+               attr->a_numvals = last;
+
+               /* Handle sorted vals, strip dups but keep the attr */
+               if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
+                       while ( attr->a_numvals > 1 ) {
+                               int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
+                               if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
+                                       break;
+
+                               /* Strip duplicate values */
+                               if ( attr->a_nvals != attr->a_vals )
+                                       LBER_FREE( attr->a_nvals[i].bv_val );
+                               LBER_FREE( attr->a_vals[i].bv_val );
+                               attr->a_numvals--;
+                               if ( i < attr->a_numvals ) {
+                                       attr->a_vals[i] = attr->a_vals[attr->a_numvals];
+                                       if ( attr->a_nvals != attr->a_vals )
+                                               attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
+                               }
+                               BER_BVZERO(&attr->a_vals[attr->a_numvals]);
+                               if ( attr->a_nvals != attr->a_vals )
+                                       BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
+                       }
+                       attr->a_flags |= SLAP_ATTR_SORTED_VALS;
+               }
+
                *attrp = attr;
                attrp = &attr->a_next;
 
@@ -853,9 +928,9 @@ retry:
        }
 
        /* TODO: timeout? */
-       rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
+       rc = ldap_pvt_search_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
                                attrp, 0, ctrls, NULL,
-                               NULL, LDAP_NO_LIMIT, &result );
+                               NULL, LDAP_NO_LIMIT, op->ors_deref, &result );
        if ( rc != LDAP_SUCCESS ) {
                if ( rc == LDAP_SERVER_DOWN && do_retry ) {
                        do_retry = 0;