]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/retcode.c
fix pre-allocated entry/attribute handling
[openldap] / servers / slapd / overlays / retcode.c
index 5041c25f10e8192a709dfc882929233aebd5daa1..add5a75235424f3a1ae2099f76f7a9846f7b8446 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2005 The OpenLDAP Foundation.
+ * Copyright 2005-2006 The OpenLDAP Foundation.
  * Portions Copyright 2005 Pierangelo Masarati <ando@sys-net.it>
  * All rights reserved.
  *
@@ -77,6 +77,8 @@ typedef struct retcode_t {
        struct berval           rd_pdn;
        struct berval           rd_npdn;
 
+       int                     rd_sleep;
+
        retcode_item_t          *rd_item;
 
        unsigned                rd_flags;
@@ -86,7 +88,7 @@ typedef struct retcode_t {
 } retcode_t;
 
 static int
-retcode_entry_response( Operation *op, SlapReply *rs, Entry *e );
+retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e );
 
 static int
 retcode_cleanup_cb( Operation *op, SlapReply *rs )
@@ -114,8 +116,6 @@ retcode_send_onelevel( Operation *op, SlapReply *rs )
        retcode_item_t  *rdi;
        
        for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
-               int     rc;
-
                if ( op->o_abandon ) {
                        return rs->sr_err = SLAPD_ABANDON;
                }
@@ -135,16 +135,14 @@ retcode_send_onelevel( Operation *op, SlapReply *rs )
                        rs->sr_err = LDAP_SUCCESS;
                        rs->sr_entry = &rdi->rdi_e;
 
-                       rc = send_search_entry( op, rs );
+                       rs->sr_err = send_search_entry( op, rs );
+                       rs->sr_entry = NULL;
 
-                       switch ( rc ) {
-                       case 0:         /* entry sent ok */
-                               break;
-                       case 1:         /* entry not sent */
-                               break;
-                       case -1:        /* connection closed */
-                               rs->sr_entry = NULL;
+                       switch ( rs->sr_err ) {
+                       case LDAP_UNAVAILABLE:  /* connection closed */
                                rs->sr_err = LDAP_OTHER;
+                               /* fallthru */
+                       case LDAP_SIZELIMIT_EXCEEDED:
                                goto done;
                        }
                }
@@ -161,10 +159,11 @@ done:;
 static int
 retcode_op_add( Operation *op, SlapReply *rs )
 {
-       return retcode_entry_response( op, rs, op->ora_e );
+       return retcode_entry_response( op, rs, NULL, op->ora_e );
 }
 
 typedef struct retcode_cb_t {
+       BackendInfo     *rdc_info;
        unsigned        rdc_flags;
        ber_tag_t       rdc_tag;
        AttributeName   *rdc_attrs;
@@ -183,7 +182,7 @@ retcode_cb_response( Operation *op, SlapReply *rs )
                if ( op->o_tag == LDAP_REQ_SEARCH ) {
                        rs->sr_attrs = rdc->rdc_attrs;
                }
-               rc = retcode_entry_response( op, rs, rs->sr_entry );
+               rc = retcode_entry_response( op, rs, rdc->rdc_info, rs->sr_entry );
                op->o_tag = o_tag;
 
                return rc;
@@ -205,7 +204,6 @@ retcode_op_internal( Operation *op, SlapReply *rs )
        slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
 
        Operation       op2 = *op;
-       SlapReply       rs2 = { 0 };
        BackendDB       db = *op->o_bd;
        slap_callback   sc = { 0 };
        retcode_cb_t    rdc;
@@ -229,6 +227,7 @@ retcode_op_internal( Operation *op, SlapReply *rs )
        db.bd_info = on->on_info->oi_orig;
        op2.o_bd = &db;
 
+       rdc.rdc_info = on->on_info->oi_orig;
        rdc.rdc_flags = RETCODE_FINDIR;
        if ( op->o_tag == LDAP_REQ_SEARCH ) {
                rdc.rdc_attrs = op->ors_attrs;
@@ -238,7 +237,8 @@ retcode_op_internal( Operation *op, SlapReply *rs )
        sc.sc_private = &rdc;
        op2.o_callback = &sc;
 
-       rc = op2.o_bd->be_search( &op2, &rs2 );
+       rc = op2.o_bd->be_search( &op2, rs );
+       op->o_abandon = op2.o_abandon;
 
        filter_free_x( &op2, op2.ors_filter );
        ber_memfree_x( op2.ors_filterstr.bv_val, op2.o_tmpmemctx );
@@ -261,6 +261,14 @@ retcode_op_func( Operation *op, SlapReply *rs )
 
        slap_callback           *cb = NULL;
 
+       /* sleep as required */
+       if ( rd->rd_sleep < 0 ) {
+               sleep( rand() % ( - rd->rd_sleep ) );
+
+       } else if ( rd->rd_sleep > 0 ) {
+               sleep( rd->rd_sleep );
+       }
+
        if ( !dnIsSuffix( &op->o_req_ndn, &rd->rd_npdn ) ) {
                if ( RETCODE_INDIR( rd ) ) {
                        switch ( op->o_tag ) {
@@ -277,10 +285,18 @@ retcode_op_func( Operation *op, SlapReply *rs )
                        case LDAP_REQ_SEARCH:
                                if ( op->ors_scope == LDAP_SCOPE_BASE ) {
                                        rs->sr_err = retcode_op_internal( op, rs );
-                                       if ( rs->sr_err == SLAP_CB_CONTINUE ) {
+                                       switch ( rs->sr_err ) {
+                                       case SLAP_CB_CONTINUE:
+                                               if ( rs->sr_nentries == 0 ) {
+                                                       break;
+                                               }
                                                rs->sr_err = LDAP_SUCCESS;
+                                               /* fallthru */
+
+                                       default:
+                                               send_ldap_result( op, rs );
+                                               break;
                                        }
-                                       send_ldap_result( op, rs );
                                        return rs->sr_err;
                                }
                                break;
@@ -463,10 +479,8 @@ retcode_op2str( ber_tag_t op, struct berval *bv )
 }
 
 static int
-retcode_entry_response( Operation *op, SlapReply *rs, Entry *e )
+retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e )
 {
-       slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
-
        Attribute       *a;
        int             err;
        char            *next;
@@ -517,7 +531,7 @@ retcode_entry_response( Operation *op, SlapReply *rs, Entry *e )
 
        /* sleep time */
        a = attr_find( e->e_attrs, ad_errSleepTime );
-       if ( a != NULL ) {
+       if ( a != NULL && a->a_nvals[ 0 ].bv_val[ 0 ] != '-' ) {
                int     sleepTime;
 
                sleepTime = strtoul( a->a_nvals[ 0 ].bv_val, &next, 0 );
@@ -527,7 +541,8 @@ retcode_entry_response( Operation *op, SlapReply *rs, Entry *e )
        }
 
        if ( rs->sr_err != LDAP_SUCCESS ) {
-               BackendDB       db = *op->o_bd;
+               BackendDB       db = *op->o_bd,
+                               *o_bd = op->o_bd;
                void            *o_callback = op->o_callback;
 
                /* message text */
@@ -542,7 +557,13 @@ retcode_entry_response( Operation *op, SlapReply *rs, Entry *e )
                        rs->sr_matched = a->a_vals[ 0 ].bv_val;
                }
 
-               db.bd_info = on->on_info->oi_orig;
+               if ( bi == NULL ) {
+                       slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
+
+                       bi = on->on_info->oi_orig;
+               }
+
+               db.bd_info = bi;
                op->o_bd = &db;
                op->o_callback = NULL;
 
@@ -567,6 +588,7 @@ retcode_entry_response( Operation *op, SlapReply *rs, Entry *e )
 
                rs->sr_text = NULL;
                rs->sr_matched = NULL;
+               op->o_bd = o_bd;
                op->o_callback = o_callback;
        }
        
@@ -588,7 +610,7 @@ retcode_response( Operation *op, SlapReply *rs )
                return SLAP_CB_CONTINUE;
        }
 
-       return retcode_entry_response( op, rs, rs->sr_entry );
+       return retcode_entry_response( op, rs, NULL, rs->sr_entry );
 }
 
 static int
@@ -766,6 +788,7 @@ retcode_db_config(
                                                } else {
                                                        fprintf( stderr, "retcode: unknown op \"%s\"\n",
                                                                ops[ j ] );
+                                                       ldap_charray_free( ops );
                                                        return 1;
                                                }
                                        }
@@ -870,6 +893,31 @@ retcode_db_config(
        } else if ( strcasecmp( argv0, "indir" ) == 0 ) {
                rd->rd_flags |= RETCODE_FINDIR;
 
+       } else if ( strcasecmp( argv0, "sleep" ) == 0 ) {
+               switch ( argc ) {
+               case 1:
+                       fprintf( stderr, "%s: line %d: retcode: "
+                               "\"retcode-sleep <time>\": missing <time>\n",
+                               fname, lineno );
+                       return 1;
+
+               case 2:
+                       break;
+
+               default:
+                       fprintf( stderr, "%s: line %d: retcode: "
+                               "\"retcode-sleep <time>\": extra cruft after <time>\n",
+                               fname, lineno );
+                       return 1;
+               }
+
+               if ( lutil_atoi( &rd->rd_sleep, argv[ 1 ] ) != 0 ) {
+                       fprintf( stderr, "%s: line %d: retcode: "
+                               "\"retcode-sleep <time>\": unable to parse <time>\n",
+                               fname, lineno );
+                       return 1;
+               }
+
        } else {
                return SLAP_CONF_UNKNOWN;
        }
@@ -1022,6 +1070,10 @@ retcode_db_destroy( BackendDB *be )
                                ber_memfree( rdi->rdi_matched.bv_val );
                        }
 
+                       if ( rdi->rdi_ref ) {
+                               ber_bvarray_free( rdi->rdi_ref );
+                       }
+
                        BER_BVZERO( &rdi->rdi_e.e_name );
                        BER_BVZERO( &rdi->rdi_e.e_nname );
 
@@ -1032,6 +1084,14 @@ retcode_db_destroy( BackendDB *be )
                        ch_free( rdi );
                }
 
+               if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
+                       ber_memfree( rd->rd_pdn.bv_val );
+               }
+
+               if ( !BER_BVISNULL( &rd->rd_npdn ) ) {
+                       ber_memfree( rd->rd_npdn.bv_val );
+               }
+
                ber_memfree( rd );
        }
 
@@ -1042,7 +1102,7 @@ retcode_db_destroy( BackendDB *be )
 static
 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
 int
-retcode_init( void )
+retcode_initialize( void )
 {
        int             i, code;
        const char      *err;
@@ -1219,7 +1279,7 @@ retcode_init( void )
 int
 init_module( int argc, char *argv[] )
 {
-       return retcode_init();
+       return retcode_initialize();
 }
 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */