]> 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 3a21f56c9ea68a27d958cd20d114980b93b09c09..85c334f9c98315df16e4eeae8aa1e08eca89b702 100644 (file)
@@ -152,7 +152,7 @@ ldap_back_search(
                        filter = BER_BVNULL;
        int             i;
        char            **attrs = NULL;
-       int             dontfreetext = 0;
+       int             freetext = 0;
        int             do_retry = 1;
        LDAPControl     **ctrls = NULL;
 
@@ -201,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;
        }
 
@@ -255,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;
@@ -308,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,
@@ -318,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 ) );
+
+                               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 );
 
-                       /* 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 ) {
@@ -352,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;
@@ -397,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;
        }
@@ -423,7 +448,7 @@ finish:;
        }
 
        if ( rs->sr_text ) {
-               if ( !dontfreetext ) {
+               if ( freetext ) {
                        LDAP_FREE( (char *)rs->sr_text );
                }
                rs->sr_text = NULL;