]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/retcode.c
don't leak callbacks if stuff cannot be registered; provide a means to dispose of...
[openldap] / servers / slapd / overlays / retcode.c
index 25e12d366fe1f1e24eee27f03dcedefac0b62515..3e5568552d255293069a853f594afeb010a7e4f6 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.
  *
@@ -39,6 +39,7 @@ static AttributeDescription   *ad_errCode;
 static AttributeDescription    *ad_errText;
 static AttributeDescription    *ad_errOp;
 static AttributeDescription    *ad_errSleepTime;
+static AttributeDescription    *ad_errMatchedDN;
 static ObjectClass             *oc_errAbsObject;
 static ObjectClass             *oc_errObject;
 static ObjectClass             *oc_errAuxObject;
@@ -63,6 +64,7 @@ typedef struct retcode_item_t {
        struct berval           rdi_dn;
        struct berval           rdi_ndn;
        struct berval           rdi_text;
+       struct berval           rdi_matched;
        int                     rdi_err;
        BerVarray               rdi_ref;
        int                     rdi_sleeptime;
@@ -75,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;
@@ -84,7 +88,28 @@ 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 unsigned int
+retcode_sleep( int s )
+{
+       unsigned int r = 0;
+
+       /* sleep as required */
+       if ( s < 0 ) {
+#if 0  /* use high-order bits for better randomness (Numerical Recipes in "C") */
+               r = rand() % (-s);
+#endif
+               r = ((double)(-s))*rand()/(RAND_MAX + 1.0);
+       } else if ( s > 0 ) {
+               r = (unsigned int)s;
+       }
+       if ( r ) {
+               sleep( r );
+       }
+
+       return r;
+}
 
 static int
 retcode_cleanup_cb( Operation *op, SlapReply *rs )
@@ -112,8 +137,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;
                }
@@ -133,16 +156,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;
                        }
                }
@@ -159,10 +180,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;
@@ -181,14 +203,16 @@ 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;
        }
 
        if ( rs->sr_err == LDAP_SUCCESS ) {
-               rdc->rdc_flags = SLAP_CB_CONTINUE;
+               if ( !op->o_abandon ) {
+                       rdc->rdc_flags = SLAP_CB_CONTINUE;
+               }
                return 0;
        }
 
@@ -201,7 +225,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;
@@ -225,6 +248,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;
@@ -234,7 +258,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 );
@@ -257,6 +282,9 @@ retcode_op_func( Operation *op, SlapReply *rs )
 
        slap_callback           *cb = NULL;
 
+       /* sleep as required */
+       retcode_sleep( rd->rd_sleep );
+
        if ( !dnIsSuffix( &op->o_req_ndn, &rd->rd_npdn ) ) {
                if ( RETCODE_INDIR( rd ) ) {
                        switch ( op->o_tag ) {
@@ -264,16 +292,35 @@ retcode_op_func( Operation *op, SlapReply *rs )
                                return retcode_op_add( op, rs );
 
                        case LDAP_REQ_BIND:
+                               /* skip if rootdn */
                                if ( be_isroot_pw( op ) ) {
                                        return SLAP_CB_CONTINUE;
                                }
-                               /* fallthru */
+                               return retcode_op_internal( op, rs );
+
+                       case LDAP_REQ_SEARCH:
+                               if ( op->ors_scope == LDAP_SCOPE_BASE ) {
+                                       rs->sr_err = retcode_op_internal( op, rs );
+                                       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;
+                                       }
+                                       return rs->sr_err;
+                               }
+                               break;
 
                        case LDAP_REQ_MODIFY:
                        case LDAP_REQ_DELETE:
                        case LDAP_REQ_MODRDN:
                        case LDAP_REQ_COMPARE:
-                       case LDAP_REQ_SEARCH:
                                return retcode_op_internal( op, rs );
                        }
                }
@@ -362,6 +409,7 @@ retcode_op_func( Operation *op, SlapReply *rs )
        } else {
                rs->sr_err = rdi->rdi_err;
                rs->sr_text = rdi->rdi_text.bv_val;
+               rs->sr_matched = rdi->rdi_matched.bv_val;
 
                /* FIXME: we only honor the rdi_ref field in case rdi_err
                 * is LDAP_REFERRAL otherwise send_ldap_result() bails out */
@@ -384,9 +432,7 @@ retcode_op_func( Operation *op, SlapReply *rs )
                        }
                }
 
-               if ( rdi->rdi_sleeptime > 0 ) {
-                       sleep( rdi->rdi_sleeptime );
-               }
+               retcode_sleep( rdi->rdi_sleeptime );
        }
 
        switch ( op->o_tag ) {
@@ -447,10 +493,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;
@@ -501,17 +545,17 @@ 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 );
-               if ( next != a->a_nvals[ 0 ].bv_val && next[ 0 ] == '\0' ) {
-                       sleep( sleepTime );
+               if ( lutil_atoi( &sleepTime, a->a_nvals[ 0 ].bv_val ) == 0 ) {
+                       retcode_sleep( sleepTime );
                }
        }
 
        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 */
@@ -520,7 +564,19 @@ retcode_entry_response( Operation *op, SlapReply *rs, Entry *e )
                        rs->sr_text = a->a_vals[ 0 ].bv_val;
                }
 
-               db.bd_info = on->on_info->oi_orig;
+               /* matched DN */
+               a = attr_find( e->e_attrs, ad_errMatchedDN );
+               if ( a != NULL ) {
+                       rs->sr_matched = a->a_vals[ 0 ].bv_val;
+               }
+
+               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;
 
@@ -544,6 +600,8 @@ 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;
        }
        
@@ -565,7 +623,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
@@ -574,6 +632,8 @@ retcode_db_init( BackendDB *be )
        slap_overinst   *on = (slap_overinst *)be->bd_info;
        retcode_t       *rd;
 
+       srand( getpid() );
+
        rd = (retcode_t *)ch_malloc( sizeof( retcode_t ) );
        memset( rd, 0, sizeof( retcode_t ) );
 
@@ -711,13 +771,15 @@ retcode_db_config(
                                                } else if ( strcasecmp( ops[ j ], "compare" ) == 0 ) {
                                                        rdi.rdi_mask |= SN_DG_OP_COMPARE;
 
-                                               } else if ( strcasecmp( ops[ j ], "add" ) == 0 ) {
+                                               } else if ( strcasecmp( ops[ j ], "delete" ) == 0 ) {
                                                        rdi.rdi_mask |= SN_DG_OP_DELETE;
 
                                                } else if ( strcasecmp( ops[ j ], "modify" ) == 0 ) {
                                                        rdi.rdi_mask |= SN_DG_OP_MODIFY;
 
-                                               } else if ( strcasecmp( ops[ j ], "rename" ) == 0 ) {
+                                               } else if ( strcasecmp( ops[ j ], "rename" ) == 0
+                                                       || strcasecmp( ops[ j ], "modrdn" ) == 0 )
+                                               {
                                                        rdi.rdi_mask |= SN_DG_OP_RENAME;
 
                                                } else if ( strcasecmp( ops[ j ], "search" ) == 0 ) {
@@ -741,6 +803,7 @@ retcode_db_config(
                                                } else {
                                                        fprintf( stderr, "retcode: unknown op \"%s\"\n",
                                                                ops[ j ] );
+                                                       ldap_charray_free( ops );
                                                        return 1;
                                                }
                                        }
@@ -757,6 +820,24 @@ retcode_db_config(
                                        }
                                        ber_str2bv( &argv[ i ][ STRLENOF( "text=" ) ], 0, 1, &rdi.rdi_text );
 
+                               } else if ( strncasecmp( argv[ i ], "matched=", STRLENOF( "matched=" ) ) == 0 )
+                               {
+                                       struct berval   dn;
+
+                                       if ( !BER_BVISNULL( &rdi.rdi_matched ) ) {
+                                               fprintf( stderr, "%s: line %d: retcode: "
+                                                       "\"matched\" already provided.\n",
+                                                       fname, lineno );
+                                               return 1;
+                                       }
+                                       ber_str2bv( &argv[ i ][ STRLENOF( "matched=" ) ], 0, 0, &dn );
+                                       if ( dnPretty( NULL, &dn, &rdi.rdi_matched, NULL ) != LDAP_SUCCESS ) {
+                                               fprintf( stderr, "%s: line %d: retcode: "
+                                                       "unable to prettify matched DN \"%s\".\n",
+                                                       fname, lineno, &argv[ i ][ STRLENOF( "matched=" ) ] );
+                                               return 1;
+                                       }
+
                                } else if ( strncasecmp( argv[ i ], "ref=", STRLENOF( "ref=" ) ) == 0 )
                                {
                                        char            **refs;
@@ -792,7 +873,6 @@ retcode_db_config(
 
                                } else if ( strncasecmp( argv[ i ], "sleeptime=", STRLENOF( "sleeptime=" ) ) == 0 )
                                {
-                                       char            *next;
                                        if ( rdi.rdi_sleeptime != 0 ) {
                                                fprintf( stderr, "%s: line %d: retcode: "
                                                        "\"sleeptime\" already provided.\n",
@@ -800,8 +880,7 @@ retcode_db_config(
                                                return 1;
                                        }
 
-                                       rdi.rdi_sleeptime = strtol( &argv[ i ][ STRLENOF( "sleeptime=" ) ], &next, 10 );
-                                       if ( next == argv[ i ] || next[ 0 ] != '\0' ) {
+                                       if ( lutil_atoi( &rdi.rdi_sleeptime, &argv[ i ][ STRLENOF( "sleeptime=" ) ] ) ) {
                                                fprintf( stderr, "%s: line %d: retcode: "
                                                        "unable to parse \"sleeptime=%s\".\n",
                                                        fname, lineno, &argv[ i ][ STRLENOF( "sleeptime=" ) ] );
@@ -827,6 +906,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;
        }
@@ -898,8 +1002,15 @@ retcode_db_open( BackendDB *be )
                        attr_merge_normalize_one( &rdi->rdi_e, ad_errText, &val[ 0 ], NULL );
                }
 
+               /* matched */
+               if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
+                       val[ 0 ] = rdi->rdi_matched;
+
+                       attr_merge_normalize_one( &rdi->rdi_e, ad_errMatchedDN, &val[ 0 ], NULL );
+               }
+
                /* sleep time */
-               if ( rdi->rdi_sleeptime > 0 ) {
+               if ( rdi->rdi_sleeptime ) {
                        snprintf( buf, sizeof( buf ), "%d", rdi->rdi_sleeptime );
                        ber_str2bv( buf, 0, 0, &val[ 0 ] );
 
@@ -968,6 +1079,14 @@ retcode_db_destroy( BackendDB *be )
                                ber_memfree( rdi->rdi_text.bv_val );
                        }
 
+                       if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
+                               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 );
 
@@ -978,6 +1097,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 );
        }
 
@@ -988,17 +1115,16 @@ 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;
 
        static struct {
-               char                    *name;
                char                    *desc;
                AttributeDescription    **ad;
        } retcode_at[] = {
-               { "errCode", "( 1.3.6.1.4.1.4203.666.11.4.1.1 "
+               { "( 1.3.6.1.4.1.4203.666.11.4.1.1 "
                        "NAME ( 'errCode' ) "
                        "DESC 'LDAP error code' "
                        "EQUALITY integerMatch "
@@ -1006,14 +1132,14 @@ retcode_init( void )
                        "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
                        "SINGLE-VALUE )",
                        &ad_errCode },
-               { "errOp", "( 1.3.6.1.4.1.4203.666.11.4.1.2 "
+               { "( 1.3.6.1.4.1.4203.666.11.4.1.2 "
                        "NAME ( 'errOp' ) "
                        "DESC 'Operations the errObject applies to' "
                        "EQUALITY caseIgnoreMatch "
                        "SUBSTR caseIgnoreSubstringsMatch "
                        "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
                        &ad_errOp},
-               { "errText", "( 1.3.6.1.4.1.4203.666.11.4.1.3 "
+               { "( 1.3.6.1.4.1.4203.666.11.4.1.3 "
                        "NAME ( 'errText' ) "
                        "DESC 'LDAP error textual description' "
                        "EQUALITY caseIgnoreMatch "
@@ -1021,22 +1147,28 @@ retcode_init( void )
                        "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
                        "SINGLE-VALUE )",
                        &ad_errText },
-               { "errSleepTime", "( 1.3.6.1.4.1.4203.666.11.4.1.4 "
+               { "( 1.3.6.1.4.1.4203.666.11.4.1.4 "
                        "NAME ( 'errSleepTime' ) "
                        "DESC 'Time to wait before returning the error' "
                        "EQUALITY integerMatch "
                        "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
                        "SINGLE-VALUE )",
                        &ad_errSleepTime },
+               { "( 1.3.6.1.4.1.4203.666.11.4.1.5 "
+                       "NAME ( 'errMatchedDN' ) "
+                       "DESC 'Value to be returned as matched DN' "
+                       "EQUALITY distinguishedNameMatch "
+                       "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
+                       "SINGLE-VALUE )",
+                       &ad_errMatchedDN },
                { NULL }
        };
 
        static struct {
-               char            *name;
                char            *desc;
                ObjectClass     **oc;
        } retcode_oc[] = {
-               { "errAbsObject", "( 1.3.6.1.4.1.4203.666.11.4.3.0 "
+               { "( 1.3.6.1.4.1.4203.666.11.4.3.0 "
                        "NAME ( 'errAbsObject' ) "
                        "SUP top ABSTRACT "
                        "MUST ( errCode ) "
@@ -1046,14 +1178,15 @@ retcode_init( void )
                                "$ errOp "
                                "$ errText "
                                "$ errSleepTime "
+                               "$ errMatchedDN "
                        ") )",
                        &oc_errAbsObject },
-               { "errObject", "( 1.3.6.1.4.1.4203.666.11.4.3.1 "
+               { "( 1.3.6.1.4.1.4203.666.11.4.3.1 "
                        "NAME ( 'errObject' ) "
                        "SUP errAbsObject STRUCTURAL "
                        ")",
                        &oc_errObject },
-               { "errAuxObject", "( 1.3.6.1.4.1.4203.666.11.4.3.2 "
+               { "( 1.3.6.1.4.1.4203.666.11.4.3.2 "
                        "NAME ( 'errAuxObject' ) "
                        "SUP errAbsObject AUXILIARY "
                        ")",
@@ -1062,73 +1195,22 @@ retcode_init( void )
        };
 
 
-       for ( i = 0; retcode_at[ i ].name != NULL; i++ ) {
-               LDAPAttributeType       *at;
-
-               at = ldap_str2attributetype( retcode_at[ i ].desc,
-                       &code, &err, LDAP_SCHEMA_ALLOW_ALL );
-               if ( !at ) {
-                       fprintf( stderr, "retcode: "
-                               "AttributeType load failed: %s %s\n",
-                               ldap_scherr2str( code ), err );
-                       return code;
-               }
-
-#if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 2
-               code = at_add( at, 0, NULL, &err );
-#else
-               code = at_add( at, &err );
-#endif
-               ldap_memfree( at );
-               if ( code != LDAP_SUCCESS ) {
-                       fprintf( stderr, "retcode: "
-                               "AttributeType load failed: %s %s\n",
-                               scherr2str( code ), err );
+       for ( i = 0; retcode_at[ i ].desc != NULL; i++ ) {
+               code = register_at( retcode_at[ i ].desc, retcode_at[ i ].ad, 0 );
+               if ( code ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "retcode: register_at failed\n", 0, 0, 0 );
                        return code;
                }
-
-               code = slap_str2ad( retcode_at[ i ].name,
-                               retcode_at[ i ].ad, &err );
-               if ( code != LDAP_SUCCESS ) {
-                       fprintf( stderr, "retcode: unable to find "
-                               "AttributeDescription \"%s\": %d (%s)\n",
-                               retcode_at[ i ].name, code, err );
-                       return 1;
-               }
        }
 
-       for ( i = 0; retcode_oc[ i ].name != NULL; i++ ) {
-               LDAPObjectClass *oc;
-
-               oc = ldap_str2objectclass( retcode_oc[ i ].desc,
-                               &code, &err, LDAP_SCHEMA_ALLOW_ALL );
-               if ( !oc ) {
-                       fprintf( stderr, "retcode: "
-                               "ObjectClass load failed: %s %s\n",
-                               ldap_scherr2str( code ), err );
+       for ( i = 0; retcode_oc[ i ].desc != NULL; i++ ) {
+               code = register_oc( retcode_oc[ i ].desc, retcode_oc[ i ].oc, 0 );
+               if ( code ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "retcode: register_oc failed\n", 0, 0, 0 );
                        return code;
                }
-
-#if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 2
-               code = oc_add( oc, 0, NULL, &err );
-#else
-               code = oc_add( oc, &err );
-#endif
-               ldap_memfree(oc);
-               if ( code != LDAP_SUCCESS ) {
-                       fprintf( stderr, "retcode: "
-                               "ObjectClass load failed: %s %s\n",
-                               scherr2str( code ), err );
-                       return code;
-               }
-
-               *retcode_oc[ i ].oc = oc_find( retcode_oc[ i ].name );
-               if ( *retcode_oc[ i ].oc == NULL ) {
-                       fprintf( stderr, "retcode: unable to find "
-                               "objectClass \"%s\"\n",
-                               retcode_oc[ i ].name );
-                       return 1;
-               }
        }
 
        retcode.on_bi.bi_type = "retcode";
@@ -1157,7 +1239,7 @@ retcode_init( void )
 int
 init_module( int argc, char *argv[] )
 {
-       return retcode_init();
+       return retcode_initialize();
 }
 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */