]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/search.c
Move ldap_pvt_thread_initialize() earlier. (See -devel; same as ITS#3793)
[openldap] / servers / slapd / back-ldap / search.c
index bde4326a9d298daa2b1660c8ad24e73c2940e307..85c334f9c98315df16e4eeae8aa1e08eca89b702 100644 (file)
 
 static int
 ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
-        struct berval *bdn, int flags );
-#define LDAP_BUILD_ENTRY_PRIVATE       0x01
+        struct berval *bdn );
 
 /*
  * Quick'n'dirty rewrite of filter in case of error, to deal with
  * <draft-zeilenga-ldap-t-f>.
  */
 static int
-munge_filter(
+ldap_back_munge_filter(
        Operation       *op,
        struct berval   *filter )
 {
@@ -65,6 +64,7 @@ munge_filter(
                static struct berval
                        bv_true = BER_BVC( "(?=true)" ),
                        bv_false = BER_BVC( "(?=false)" ),
+                       bv_undefined = BER_BVC( "(?=undefined)" ),
                        bv_t = BER_BVC( "(&)" ),
                        bv_f = BER_BVC( "(|)" ),
                        bv_T = BER_BVC( "(objectClass=*)" ),
@@ -92,12 +92,18 @@ munge_filter(
                                newbv = &bv_F;
                        }
 
+               } else if ( strncmp( ptr, bv_undefined.bv_val, bv_undefined.bv_len ) == 0 )
+               {
+                       oldbv = &bv_undefined;
+                       newbv = &bv_F;
+
                } else {
-                       continue;
+                       gotit = 0;
+                       goto done;
                }
 
                oldfilter = *filter;
-               if ( !( li->flags & LDAP_BACK_F_SUPPORT_T_F ) ) {
+               if ( newbv->bv_len > oldbv->bv_len ) {
                        filter->bv_len += newbv->bv_len - oldbv->bv_len;
                        if ( filter->bv_val == op->ors_filterstr.bv_val ) {
                                filter->bv_val = op->o_tmpalloc( filter->bv_len + 1,
@@ -123,6 +129,7 @@ munge_filter(
                gotit = 1;
        }
 
+done:;
        Debug( LDAP_DEBUG_ARGS, "<= ldap_back_munge_filter \"%s\" (%d)\n",
                        filter->bv_val, gotit, 0 );
 
@@ -136,6 +143,7 @@ ldap_back_search(
 {
        struct ldapconn *lc;
        struct timeval  tv;
+       time_t          stoptime;
        LDAPMessage     *res,
                        *e;
        int             rc = 0,
@@ -144,12 +152,12 @@ ldap_back_search(
                        filter = BER_BVNULL;
        int             i;
        char            **attrs = NULL;
-       int             dontfreetext = 0;
+       int             freetext = 0;
        int             do_retry = 1;
        LDAPControl     **ctrls = NULL;
 
        lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
-       if ( !lc ) {
+       if ( !lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
                return rs->sr_err;
        }
 
@@ -157,9 +165,6 @@ ldap_back_search(
         * FIXME: in case of values return filter, we might want
         * to map attrs and maybe rewrite value
         */
-       if ( !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
-               return rs->sr_err;
-       }
 
        /* should we check return values? */
        if ( op->ors_deref != -1 ) {
@@ -170,6 +175,7 @@ ldap_back_search(
        if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
                tv.tv_sec = op->ors_tlimit;
                tv.tv_usec = 0;
+               stoptime = op->o_time + op->ors_tlimit;
 
        } else {
                tv.tv_sec = 0;
@@ -195,7 +201,6 @@ ldap_back_search(
        ctrls = op->o_ctrls;
        rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
        if ( rc != LDAP_SUCCESS ) {
-               dontfreetext = 1;
                goto finish;
        }
 
@@ -224,10 +229,14 @@ fail:;
                        goto finish;
 
                case LDAP_FILTER_ERROR:
-                       if ( munge_filter( op, &filter ) ) {
+                       if ( ldap_back_munge_filter( op, &filter ) ) {
                                goto retry;
                        }
-                       /* fallthru */
+
+                       /* invalid filters return success with no data */
+                       rs->sr_err = LDAP_SUCCESS;
+                       rs->sr_text = NULL;
+                       goto finish;
                
                default:
                        rs->sr_err = slap_map_api2result( rs );
@@ -245,6 +254,9 @@ fail:;
        {
                /* check for abandon */
                if ( op->o_abandon ) {
+                       if ( rc > 0 ) {
+                               ldap_msgfree( res );
+                       }
                        ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
                        rc = SLAPD_ABANDON;
                        goto finish;
@@ -255,43 +267,40 @@ fail:;
                        tv.tv_usec = 100000;
                        ldap_pvt_thread_yield();
 
+                       /* check time limit */
+                       if ( op->ors_tlimit != SLAP_NO_LIMIT
+                                       && slap_get_time() > stoptime )
+                       {
+                               ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
+                               rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
+                               goto finish;
+                       }
+
                } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
-                       Entry           ent = {0};
-                       struct berval   bdn;
+                       Entry           ent = { 0 };
+                       struct berval   bdn = BER_BVNULL;
                        int             abort = 0;
 
                        do_retry = 0;
 
                        e = ldap_first_entry( lc->lc_ld, res );
-                       rc = ldap_build_entry( op, e, &ent, &bdn,
-                                       LDAP_BUILD_ENTRY_PRIVATE );
-                      if ( rc == LDAP_SUCCESS ) {
+                       rc = ldap_build_entry( op, e, &ent, &bdn );
+                       if ( rc == LDAP_SUCCESS ) {
                                rs->sr_entry = &ent;
                                rs->sr_attrs = op->ors_attrs;
                                rs->sr_operational_attrs = NULL;
                                rs->sr_flags = 0;
                                abort = send_search_entry( op, rs );
-                               while ( ent.e_attrs ) {
-                                       Attribute       *a;
-
-                                       a = ent.e_attrs;
-                                       ent.e_attrs = a->a_next;
-
-                                       if ( a->a_nvals != a->a_vals ) {
-                                               ber_bvarray_free( a->a_nvals );
-                                       }
-                                       if ( a->a_vals != &slap_dummy_bv ) {
-                                               ber_bvarray_free( a->a_vals );
-                                       }
-                                       ch_free( a );
+                               if ( !BER_BVISNULL( &ent.e_name ) ) {
+                                       assert( ent.e_name.bv_val != bdn.bv_val );
+                                       free( ent.e_name.bv_val );
+                                       BER_BVZERO( &ent.e_name );
                                }
-                               
-                               if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) ) {
-                                       free( ent.e_dn );
-                               }
-                               if ( ent.e_ndn ) {
-                                       free( ent.e_ndn );
+                               if ( !BER_BVISNULL( &ent.e_nname ) ) {
+                                       free( ent.e_nname.bv_val );
+                                       BER_BVZERO( &ent.e_nname );
                                }
+                               entry_clean( &ent );
                        }
                        ldap_msgfree( res );
                        if ( abort ) {
@@ -301,7 +310,6 @@ fail:;
 
                } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
                        char            **references = NULL;
-                       int             cnt;
 
                        do_retry = 0;
                        rc = ldap_parse_reference( lc->lc_ld, res,
@@ -311,21 +319,31 @@ fail:;
                                continue;
                        }
 
-                       if ( references == NULL ) {
-                               continue;
-                       }
+                       /* FIXME: there MUST be at least one */
+                       if ( references && references[ 0 ] && references[ 0 ][ 0 ] ) {
+                               int             cnt;
 
-                       for ( cnt = 0; references[ cnt ]; cnt++ )
-                               /* NO OP */ ;
-                               
-                       rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
+                               for ( cnt = 0; references[ cnt ]; cnt++ )
+                                       /* NO OP */ ;
 
-                       for ( cnt = 0; references[ cnt ]; cnt++ ) {
-                               ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
-                       }
+                               /* FIXME: there MUST be at least one */
+                               rs->sr_ref = ch_malloc( ( cnt + 1 ) * sizeof( struct berval ) );
 
-                       /* ignore return value by now */
-                       ( void )send_search_reference( op, rs );
+                               for ( cnt = 0; references[ cnt ]; cnt++ ) {
+                                       ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
+                               }
+                               BER_BVZERO( &rs->sr_ref[ cnt ] );
+
+                               /* ignore return value by now */
+                               ( void )send_search_reference( op, rs );
+
+                       } else {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "%s ldap_back_search: "
+                                       "got SEARCH_REFERENCE "
+                                       "with no referrals\n",
+                                       op->o_log_prefix, 0, 0 );
+                       }
 
                        /* cleanup */
                        if ( references ) {
@@ -345,27 +363,40 @@ fail:;
                        rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
                                        &match.bv_val, (char **)&rs->sr_text,
                                        &references, &rs->sr_ctrls, 1 );
+                       freetext = 1;
                        if ( rc != LDAP_SUCCESS ) {
                                rs->sr_err = rc;
                        }
                        rs->sr_err = slap_map_api2result( rs );
 
-                       if ( references ) {
+                       if ( references && references[ 0 ] && references[ 0 ][ 0 ] ) {
                                int     cnt;
 
+                               if ( rs->sr_err != LDAP_REFERRAL ) {
+                                       /* FIXME: error */
+                                       Debug( LDAP_DEBUG_ANY,
+                                               "%s ldap_back_search: "
+                                               "got referrals with %d\n",
+                                               op->o_log_prefix,
+                                               rs->sr_err, 0 );
+                                       rs->sr_err = LDAP_REFERRAL;
+                               }
+
                                for ( cnt = 0; references[ cnt ]; cnt++ )
                                        /* NO OP */ ;
                                
-                               rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
+                               rs->sr_ref = ch_malloc( ( cnt + 1 ) * sizeof( struct berval ) );
 
                                for ( cnt = 0; references[ cnt ]; cnt++ ) {
+                                       /* duplicating ...*/
                                        ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
                                }
+                               BER_BVZERO( &rs->sr_ref[ cnt ] );
+                       }
 
-                               /* cleanup */
-                               if ( references ) {
-                                       ldap_value_free( references );
-                               }
+                       /* cleanup */
+                       if ( references ) {
+                               ldap_value_free( references );
                        }
 
                        rc = 0;
@@ -390,6 +421,7 @@ fail:;
        if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
                rs->sr_matched = match.bv_val;
        }
+
        if ( rs->sr_v2ref ) {
                rs->sr_err = LDAP_REFERRAL;
        }
@@ -416,7 +448,7 @@ finish:;
        }
 
        if ( rs->sr_text ) {
-               if ( !dontfreetext ) {
+               if ( freetext ) {
                        LDAP_FREE( (char *)rs->sr_text );
                }
                rs->sr_text = NULL;
@@ -431,6 +463,10 @@ finish:;
                ch_free( attrs );
        }
 
+       if ( lc != NULL ) {
+               ldap_back_release_conn( op, rs, lc );
+       }
+
        return rc;
 }
 
@@ -439,18 +475,16 @@ ldap_build_entry(
                Operation       *op,
                LDAPMessage     *e,
                Entry           *ent,
-               struct berval   *bdn,
-               int             flags )
+               struct berval   *bdn )
 {
        struct berval   a;
        BerElement      ber = *e->lm_ber;
        Attribute       *attr, **attrp;
        const char      *text;
        int             last;
-       int             private = flags & LDAP_BUILD_ENTRY_PRIVATE;
 
        /* safe assumptions ... */
-       assert( ent );
+       assert( ent != NULL );
        BER_BVZERO( &ent->e_bv );
 
        if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
@@ -524,13 +558,7 @@ ldap_build_entry(
                         * Note: attr->a_vals can be null when using
                         * values result filter
                         */
-                       if ( private ) {
-                               attr->a_vals = (struct berval *)&slap_dummy_bv;
-                               
-                       } else {
-                               attr->a_vals = ch_malloc( sizeof( struct berval ) );
-                               BER_BVZERO( &attr->a_vals[ 0 ] );
-                       }
+                       attr->a_vals = (struct berval *)&slap_dummy_bv;
                        last = 0;
 
                } else {
@@ -702,7 +730,7 @@ retry:
 
        *ent = ch_calloc( 1, sizeof( Entry ) );
 
-       rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
+       rc = ldap_build_entry( op, e, *ent, &bdn );
 
        if ( rc != LDAP_SUCCESS ) {
                ch_free( *ent );
@@ -720,6 +748,10 @@ cleanup:
                ch_free( filter );
        }
 
+       if ( lc != NULL ) {
+               ldap_back_release_conn( op, &rs, lc );
+       }
+
        return rc;
 }