]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-monitor/database.c
address ITS#4332; might remove dynamicObject counting
[openldap] / servers / slapd / back-monitor / database.c
index 0b25c306e14875f9d6ed87fa3f6879135f4f84e8..a5485577e6d2be974bd523034db1c403d0c0f1ed 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2001-2004 The OpenLDAP Foundation.
+ * Copyright 2001-2006 The OpenLDAP Foundation.
  * Portions Copyright 2001-2003 Pierangelo Masarati.
  * All rights reserved.
  *
 
 #include <stdio.h>
 #include <ac/string.h>
+#include <ac/unistd.h>
 
 #include "slap.h"
 #include "back-monitor.h"
 
 #if defined(LDAP_SLAPI)
 #include "slapi.h"
-static int monitor_back_add_plugin( Backend *be, Entry *e );
+static int monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e );
 #endif /* defined(LDAP_SLAPI) */
 
+#if defined(SLAPD_BDB)
+#include "../back-bdb/back-bdb.h"
+#endif /* defined(SLAPD_BDB) */
+#if defined(SLAPD_HDB)
+#include "../back-hdb/back-bdb.h"
+#endif /* defined(SLAPD_HDB) */
 #if defined(SLAPD_LDAP) 
 #include "../back-ldap/back-ldap.h"
 #endif /* defined(SLAPD_LDAP) */
+#if 0 && defined(SLAPD_LDBM) 
+#include "../back-ldbm/back-ldbm.h"
+#endif /* defined(SLAPD_LDBM) */
+#if defined(SLAPD_META) 
+#include "../back-meta/back-meta.h"
+#endif /* defined(SLAPD_META) */
+
+/* for PATH_MAX on some systems (e.g. Solaris) */
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif /* HAVE_LIMITS_H */
+#ifndef PATH_MAX
+#define PATH_MAX       4095
+#endif /* ! PATH_MAX */
+
+static int
+monitor_subsys_database_modify(
+       Operation       *op,
+       SlapReply       *rs,
+       Entry           *e );
 
 static struct restricted_ops_t {
        struct berval   op;
@@ -58,23 +85,24 @@ static struct restricted_ops_t {
 };
 
 static int
-init_readOnly( struct monitorinfo *mi, Entry *e, slap_mask_t restrictops )
+init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
 {
        struct berval   *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ?
                (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
 
-       return attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
+       return attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
 }
 
 static int
-init_restrictedOperation( struct monitorinfo *mi, Entry *e, slap_mask_t restrictops )
+init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
 {
        int     i, rc;
 
        for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) {
                if ( restrictops & restricted_ops[ i ].tag ) {
                        rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
-                                       &restricted_ops[ i ].op, NULL );
+                                       &restricted_ops[ i ].op,
+                                       &restricted_ops[ i ].op );
                        if ( rc ) {
                                return rc;
                        }
@@ -84,7 +112,8 @@ init_restrictedOperation( struct monitorinfo *mi, Entry *e, slap_mask_t restrict
        for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) {
                if ( restrictops & restricted_exops[ i ].tag ) {
                        rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
-                                       &restricted_exops[ i ].op, NULL );
+                                       &restricted_exops[ i ].op,
+                                       &restricted_exops[ i ].op );
                        if ( rc ) {
                                return rc;
                        }
@@ -96,51 +125,74 @@ init_restrictedOperation( struct monitorinfo *mi, Entry *e, slap_mask_t restrict
 
 int
 monitor_subsys_database_init(
-       BackendDB       *be
+       BackendDB               *be,
+       monitor_subsys_t        *ms
 )
 {
-       struct monitorinfo      *mi;
-       Entry                   *e, *e_database, *e_tmp;
+       monitor_info_t          *mi;
+       Entry                   *e_database, **ep;
        int                     i;
-       struct monitorentrypriv *mp;
+       monitor_entry_t         *mp;
+       monitor_subsys_t        *ms_backend,
+                               *ms_overlay;
 
        assert( be != NULL );
 
-       mi = ( struct monitorinfo * )be->be_private;
+       ms->mss_modify = monitor_subsys_database_modify;
 
-       if ( monitor_cache_get( mi, 
-                               &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
-                               &e_database ) ) {
-#ifdef NEW_LOGGING
-               LDAP_LOG( OPERATION, CRIT,
+       mi = ( monitor_info_t * )be->be_private;
+
+       ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
+       if ( ms_backend == NULL ) {
+               Debug( LDAP_DEBUG_ANY,
                        "monitor_subsys_database_init: "
-                       "unable to get entry '%s'\n",
-                       monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
-#else
+                       "unable to get "
+                       "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
+                       "subsystem\n",
+                       0, 0, 0 );
+               return -1;
+       }
+
+       ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
+       if ( ms_overlay == NULL ) {
                Debug( LDAP_DEBUG_ANY,
                        "monitor_subsys_database_init: "
-                       "unable to get entry '%s'\n%s%s",
-                       monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
-                       "", "" );
-#endif
+                       "unable to get "
+                       "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
+                       "subsystem\n",
+                       0, 0, 0 );
+               return -1;
+       }
+
+       if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "monitor_subsys_database_init: "
+                       "unable to get entry \"%s\"\n",
+                       ms->mss_ndn.bv_val, 0, 0 );
                return( -1 );
        }
 
        (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
        (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
 
-       e_tmp = NULL;
-       for ( i = nBackendDB; i--; ) {
+       mp = ( monitor_entry_t * )e_database->e_private;
+       mp->mp_children = NULL;
+       ep = &mp->mp_children;
+
+       i = -1;
+       LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
                char            buf[ BACKMONITOR_BUFSIZE ];
                int             j;
                slap_overinfo   *oi = NULL;
-               BackendInfo     *bi;
+               BackendInfo     *bi, *bi2;
+               Entry           *e;
+               struct berval bv;
 
-               be = &backendDB[i];
+               i++;
 
                bi = be->bd_info;
 
-               if ( strcmp( be->bd_info->bi_type, "over" ) == 0 ) {
+               if ( overlay_is_over( be ) ) {
                        oi = (slap_overinfo *)be->bd_info->bi_private;
                        bi = oi->oi_orig;
                }
@@ -150,53 +202,40 @@ monitor_subsys_database_init(
                        continue;
                }
 
-               snprintf( buf, sizeof( buf ),
-                               "dn: cn=Database %d,%s\n"
-                               "objectClass: %s\n"
-                               "structuralObjectClass: %s\n"
-                               "cn: Database %d\n"
-                               "description: This object contains the type of the database.\n"
-                               "%s: %s\n"
-                               "creatorsName: %s\n"
-                               "modifiersName: %s\n"
-                               "createTimestamp: %s\n"
-                               "modifyTimestamp: %s\n",
-                               i,
-                               monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
-                               mi->mi_oc_monitoredObject->soc_cname.bv_val,
-                               mi->mi_oc_monitoredObject->soc_cname.bv_val,
-                               i,
-                               mi->mi_ad_monitoredInfo->ad_cname.bv_val,
-                               bi->bi_type,
-                               mi->mi_creatorsName.bv_val,
-                               mi->mi_creatorsName.bv_val,
-                               mi->mi_startTime.bv_val,
-                               mi->mi_startTime.bv_val );
-               
-               e = str2entry( buf );
+               bv.bv_len = snprintf( buf, sizeof( buf ),
+                               "cn=Database %d", i );
+               bv.bv_val = buf;
+               e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
+                       mi->mi_oc_monitoredObject, mi, NULL, NULL );
+
                if ( e == NULL ) {
-#ifdef NEW_LOGGING
-                       LDAP_LOG( OPERATION, CRIT,
-                               "monitor_subsys_database_init: "
-                               "unable to create entry 'cn=Database %d,%s'\n",
-                               i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
-#else
                        Debug( LDAP_DEBUG_ANY,
                                "monitor_subsys_database_init: "
-                               "unable to create entry 'cn=Database %d,%s'\n%s",
-                               i, 
-                               monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
-                               "" );
-#endif
+                               "unable to create entry \"cn=Database %d,%s\"\n",
+                               i, ms->mss_dn.bv_val, 0 );
                        return( -1 );
                }
-               
-               if ( SLAP_MONITOR(be) ) {
+
+               ber_str2bv( bi->bi_type, 0, 0, &bv );
+               attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
+               attr_merge_one( e, mi->mi_ad_monitorIsShadow,
+                       SLAP_SHADOW( be ) ? (struct berval *)&slap_true_bv :
+                               (struct berval *)&slap_false_bv, NULL );
+
+               if ( SLAP_MONITOR( be ) ) {
                        attr_merge( e, slap_schema.si_ad_monitorContext,
                                        be->be_suffix, be->be_nsuffix );
                        attr_merge( e_database, slap_schema.si_ad_monitorContext,
                                        be->be_suffix, be->be_nsuffix );
+
                } else {
+                       if ( be->be_suffix == NULL ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "monitor_subsys_database_init: "
+                                       "missing suffix for database %d\n",
+                                       i, 0, 0 );
+                               return -1;
+                       }
                        attr_merge( e, slap_schema.si_ad_namingContexts,
                                        be->be_suffix, be->be_nsuffix );
                        attr_merge( e_database, slap_schema.si_ad_namingContexts,
@@ -206,57 +245,167 @@ monitor_subsys_database_init(
                (void)init_readOnly( mi, e, be->be_restrictops );
                (void)init_restrictedOperation( mi, e, be->be_restrictops );
 
+               if ( SLAP_SHADOW( be ) && be->be_update_refs ) {
+                       attr_merge_normalize( e, mi->mi_ad_monitorUpdateRef,
+                                       be->be_update_refs, NULL );
+               }
+
                if ( oi != NULL ) {
-                       slap_overinst *on = oi->oi_list;
+                       slap_overinst   *on = oi->oi_list,
+                                       *on1 = on;
 
                        for ( ; on; on = on->on_next ) {
                                struct berval           bv;
                                slap_overinst           *on2;
+
+                               for ( on2 = on1; on2 != on; on2 = on2->on_next ) {
+                                       if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
+                                               break;
+                                       }
+                               }
+
+                               if ( on2 != on ) {
+                                       break;
+                               }
                                
-                               bv.bv_val = on->on_bi.bi_type;
-                               bv.bv_len = strlen( bv.bv_val );
+                               ber_str2bv( on->on_bi.bi_type, 0, 0, &bv );
                                attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
                                                &bv, NULL );
 
+                               /* find the overlay number, j */
                                for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
                                        if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
                                                break;
                                        }
                                }
-                               assert( on2 );
+                               assert( on2 != NULL );
 
                                snprintf( buf, sizeof( buf ), 
                                        "cn=Overlay %d,%s", 
-                                       j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
-                               bv.bv_val = buf;
-                               bv.bv_len = strlen( buf );
-                               attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
+                                       j, ms_overlay->mss_dn.bv_val );
+                               ber_str2bv( buf, 0, 0, &bv );
+                               attr_merge_normalize_one( e,
+                                               slap_schema.si_ad_seeAlso,
                                                &bv, NULL );
                        }
                }
 
-#if defined(SLAPD_LDAP) 
-               if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
-                       struct ldapinfo         *li = (struct ldapinfo *)be->be_private;
-                       struct berval           bv;
 
-                       bv.bv_val = li->url;
-                       bv.bv_len = strlen( bv.bv_val );
-                       attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
+               if ( 0 ) {
+                       assert( 0 );
+
+#if defined(SLAPD_BDB) || defined(SLAPD_HDB) 
+               } else if ( strcmp( bi->bi_type, "bdb" ) == 0
+                               || strcmp( bi->bi_type, "hdb" ) == 0 )
+               {
+                       struct berval   bv;
+                       ber_len_t       pathlen = 0, len = 0;
+                       char            path[ PATH_MAX ] = { '\0' };
+                       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+                       char            *fname = bdb->bi_dbenv_home;
+
+                       len = strlen( fname );
+                       if ( fname[ 0 ] != '/' ) {
+                               /* get full path name */
+                               getcwd( path, sizeof( path ) );
+                               pathlen = strlen( path );
+
+                               if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
+                                       fname += 2;
+                                       len -= 2;
+                               }
+                       }
+
+                       bv.bv_len = STRLENOF( "file://" ) + pathlen
+                               + STRLENOF( "/" ) + len;
+                       bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
+                       AC_MEMCPY( bv.bv_val, "file://", STRLENOF( "file://" ) );
+                       if ( pathlen ) {
+                               AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) ],
+                                               path, pathlen );
+                               bv.bv_val[ STRLENOF( "file://" ) + pathlen ] = '/';
+                               pathlen++;
+                       }
+                       AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) + pathlen ],
+                                       fname, len );
+                       if ( bv.bv_val[ bv.bv_len - 1 ] != '/' ) {
+                               bv.bv_val[ bv.bv_len ] = '/';
+                               bv.bv_len++;
+                       }
+                       bv.bv_val[ bv.bv_len ] = '\0';
+
+                       attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
                                        &bv, NULL );
-               }
+
+                       ch_free( bv.bv_val );
+
+#endif /* defined(SLAPD_BDB) || defined(SLAPD_HDB) */
+#if defined(SLAPD_LDAP) 
+               } else if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
+                       ldapinfo_t      *li = (ldapinfo_t *)be->be_private;
+#if 0
+                       attr_merge_normalize( e, slap_schema.si_ad_labeledURI,
+                                       li->li_bvuri, NULL );
+#else
+                       char            **urls = ldap_str2charray( li->li_uri, " " );
+
+                       if ( urls != NULL ) {
+                               int             u;
+
+                               for ( u = 0; urls[ u ] != NULL; u++ ) {
+                                       struct berval   bv;
+
+                                       ber_str2bv( urls[ u ], 0, 0, &bv );
+
+                                       attr_merge_normalize_one( e,
+                                               slap_schema.si_ad_labeledURI,
+                                               &bv, NULL );
+                               }
+
+                               ldap_charray_free( urls );
+                       }
+#endif
+
 #endif /* defined(SLAPD_LDAP) */
+#if defined(SLAPD_META) 
+               } else if ( strcmp( bi->bi_type, "meta" ) == 0 ) {
+                       metainfo_t      *mi = (metainfo_t *)be->be_private;
+                       int             t;
+
+                       for ( t = 0; t < mi->mi_ntargets; t++ ) {
+                               char            **urls = ldap_str2charray( mi->mi_targets[ t ].mt_uri, " " );
+
+                               if ( urls != NULL ) {
+                                       int             u;
+
+                                       for ( u = 0; urls[ u ] != NULL; u++ ) {
+                                               struct berval   bv;
 
-               for ( j = nBackendInfo; j--; ) {
-                       if ( backendInfo[ j ].bi_type == bi->bi_type ) {
+                                               ber_str2bv( urls[ u ], 0, 0, &bv );
+
+                                               attr_merge_normalize_one( e,
+                                                       slap_schema.si_ad_labeledURI,
+                                                       &bv, NULL );
+                                       }
+                                       ldap_charray_free( urls );
+                               }
+                       }
+#endif /* defined(SLAPD_META) */
+               }
+
+               j = -1;
+               LDAP_STAILQ_FOREACH( bi2, &backendInfo, bi_next ) {
+                       j++;
+                       if ( bi2->bi_type == bi->bi_type ) {
                                struct berval           bv;
 
                                snprintf( buf, sizeof( buf ), 
                                        "cn=Backend %d,%s", 
-                                       j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
+                                       j, ms_backend->mss_dn.bv_val );
                                bv.bv_val = buf;
                                bv.bv_len = strlen( buf );
-                               attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
+                               attr_merge_normalize_one( e,
+                                               slap_schema.si_ad_seeAlso,
                                                &bv, NULL );
                                break;
                        }
@@ -264,41 +413,95 @@ monitor_subsys_database_init(
                /* we must find it! */
                assert( j >= 0 );
 
-               mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
+               mp = monitor_entrypriv_create();
+               if ( mp == NULL ) {
+                       return -1;
+               }
                e->e_private = ( void * )mp;
-               mp->mp_next = e_tmp;
-               mp->mp_children = NULL;
-               mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
-               mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
+               mp->mp_info = ms;
+               mp->mp_flags = ms->mss_flags
                        | MONITOR_F_SUB;
 
                if ( monitor_cache_add( mi, e ) ) {
-#ifdef NEW_LOGGING
-                       LDAP_LOG( OPERATION, CRIT,
-                               "monitor_subsys_database_init: "
-                               "unable to add entry 'cn=Database %d,%s'\n",
-                               i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
-#else
                        Debug( LDAP_DEBUG_ANY,
                                "monitor_subsys_database_init: "
-                               "unable to add entry 'cn=Database %d,%s'\n",
-                               i, 
-                               monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
-                               0 );
-#endif
+                               "unable to add entry \"cn=Database %d,%s\"\n",
+                               i, ms->mss_dn.bv_val, 0 );
                        return( -1 );
                }
 
 #if defined(LDAP_SLAPI)
-               monitor_back_add_plugin( be, e );
+               monitor_back_add_plugin( mi, be, e );
 #endif /* defined(LDAP_SLAPI) */
 
-               e_tmp = e;
+               if ( oi != NULL ) {
+                       Entry           **ep_overlay = &mp->mp_children;
+                       monitor_entry_t *mp_overlay;
+                       slap_overinst   *on = oi->oi_list;
+                       int             o;
+
+                       for ( o = 0; on; o++, on = on->on_next ) {
+                               Entry                   *e_overlay;
+                               slap_overinst           *on2;
+
+                               /* find the overlay number, j */
+                               for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
+                                       if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
+                                               break;
+                                       }
+                               }
+                               assert( on2 != NULL );
+
+                               bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d", o );
+                               bv.bv_val = buf;
+
+                               e_overlay = monitor_entry_stub( &e->e_name, &e->e_nname, &bv,
+                                       mi->mi_oc_monitoredObject, mi, NULL, NULL );
+
+                               if ( e_overlay == NULL ) {
+                                       Debug( LDAP_DEBUG_ANY,
+                                               "monitor_subsys_database_init: "
+                                               "unable to create entry "
+                                               "\"cn=Overlay %d,cn=Database %d,%s\"\n",
+                                               o, i, ms->mss_dn.bv_val );
+                                       return( -1 );
+                               }
+                               ber_str2bv( on->on_bi.bi_type, 0, 0, &bv );
+                               attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
+
+                               bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d,%s",
+                                       j, ms_overlay->mss_dn.bv_val );
+                               bv.bv_val = buf;
+                               attr_merge_normalize_one( e, slap_schema.si_ad_seeAlso,
+                                       &bv, NULL );
+
+                               mp_overlay = monitor_entrypriv_create();
+                               if ( mp_overlay == NULL ) {
+                                       return -1;
+                               }
+                               e_overlay->e_private = ( void * )mp_overlay;
+                               mp_overlay->mp_info = ms;
+                               mp_overlay->mp_flags = ms->mss_flags
+                                       | MONITOR_F_SUB;
+               
+                               if ( monitor_cache_add( mi, e_overlay ) ) {
+                                       Debug( LDAP_DEBUG_ANY,
+                                               "monitor_subsys_database_init: "
+                                               "unable to add entry "
+                                               "\"cn=Overlay %d,cn=Database %d,%s\"\n",
+                                               o, i, ms->mss_dn.bv_val );
+                                       return( -1 );
+                               }
+
+                               *ep_overlay = e_overlay;
+                               ep_overlay = &mp_overlay->mp_next;
+                       }
+               }
+
+               *ep = e;
+               ep = &mp->mp_next;
        }
        
-       mp = ( struct monitorentrypriv * )e_database->e_private;
-       mp->mp_children = e_tmp;
-
        monitor_cache_release( mi, e_database );
 
        return( 0 );
@@ -350,40 +553,49 @@ value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
        return LDAP_SUCCESS;
 }
 
-int
+static int
 monitor_subsys_database_modify(
        Operation       *op,
-       Entry           *e
-)
+       SlapReply       *rs,
+       Entry           *e )
 {
-       struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
-       int rc = LDAP_OTHER;
-       Attribute *save_attrs, *a;
-       Modifications *modlist = op->oq_modify.rs_modlist;
-       Modifications *ml;
-       Backend *be;
-       int ro_gotval = 1, i, n;
+       monitor_info_t  *mi = (monitor_info_t *)op->o_bd->be_private;
+       int             rc = LDAP_OTHER;
+       Attribute       *save_attrs, *a;
+       Modifications   *ml;
+       Backend         *be;
+       int             ro_gotval = 1, i, n;
        slap_mask_t     rp_add = 0, rp_delete = 0, rp_cur;
-       struct berval *tf;
+       struct berval   *tf;
        
        i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
-       if ( i != 1 )
-               return LDAP_UNWILLING_TO_PERFORM;
+       if ( i != 1 ) {
+               return SLAP_CB_CONTINUE;
+       }
 
-       if ( n < 0 || n >= nBackendDB )
-               return LDAP_NO_SUCH_OBJECT;
+       if ( n < 0 || n >= nBackendDB ) {
+               rs->sr_text = "invalid database index";
+               return ( rs->sr_err = LDAP_NO_SUCH_OBJECT );
+       }
 
+       LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
+               if ( n == 0 ) {
+                       break;
+               }
+               n--;
+       }
        /* do not allow some changes on back-monitor (needs work)... */
-       be = &backendDB[n];
-       if ( SLAP_MONITOR( be ) )
-               return LDAP_UNWILLING_TO_PERFORM;
+       if ( SLAP_MONITOR( be ) ) {
+               rs->sr_text = "no modifications allowed to monitor database entry";
+               return ( rs->sr_err = LDAP_UNWILLING_TO_PERFORM );
+       }
                
        rp_cur = be->be_restrictops;
 
        save_attrs = e->e_attrs;
        e->e_attrs = attrs_dup( e->e_attrs );
 
-       for ( ml=modlist; ml; ml=ml->sml_next ) {
+       for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
                Modification *mod = &ml->sml_mod;
 
                if ( mod->sm_desc == mi->mi_ad_readOnly ) {
@@ -391,7 +603,8 @@ monitor_subsys_database_modify(
 
                        if ( mod->sm_values ) {
                                if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
-                                       rc = LDAP_CONSTRAINT_VIOLATION;
+                                       rs->sr_text = "attempting to modify multiple values of single-valued attribute";
+                                       rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
                                        goto done;
                                }
 
@@ -402,7 +615,8 @@ monitor_subsys_database_modify(
                                        val = 0;
 
                                } else {
-                                       rc = LDAP_INVALID_SYNTAX;
+                                       assert( 0 );
+                                       rc = rs->sr_err = LDAP_INVALID_SYNTAX;
                                        goto done;
                                }
                        }
@@ -410,18 +624,18 @@ monitor_subsys_database_modify(
                        switch ( mod->sm_op ) {
                        case LDAP_MOD_DELETE:
                                if ( ro_gotval < 1 ) {
-                                       rc = LDAP_CONSTRAINT_VIOLATION;
+                                       rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
                                        goto done;
                                }
                                ro_gotval--;
 
                                if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
-                                       rc = LDAP_NO_SUCH_ATTRIBUTE;
+                                       rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
                                        goto done;
                                }
                                
                                if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
-                                       rc = LDAP_NO_SUCH_ATTRIBUTE;
+                                       rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
                                        goto done;
                                }
                                
@@ -433,7 +647,7 @@ monitor_subsys_database_modify(
 
                        case LDAP_MOD_ADD:
                                if ( ro_gotval > 0 ) {
-                                       rc = LDAP_CONSTRAINT_VIOLATION;
+                                       rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
                                        goto done;
                                }
                                ro_gotval++;
@@ -451,7 +665,7 @@ monitor_subsys_database_modify(
                                break;
 
                        default:
-                               rc = LDAP_OTHER;
+                               rc = rs->sr_err = LDAP_OTHER;
                                goto done;
                        }
 
@@ -491,12 +705,12 @@ monitor_subsys_database_modify(
                                        rp_delete &= ~mask;
 
                                } else if ( rc == LDAP_OTHER ) {
-                                       rc = LDAP_TYPE_OR_VALUE_EXISTS;
+                                       rc = rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
                                }
                                break;
 
                        default:
-                               rc = LDAP_OTHER;
+                               rc = rs->sr_err = LDAP_OTHER;
                                break;
                        }
 
@@ -510,29 +724,29 @@ monitor_subsys_database_modify(
                        rc = attr_merge( e, mod->sm_desc, mod->sm_values,
                                mod->sm_nvalues );
                        if ( rc ) {
-                               rc = LDAP_OTHER;
+                               rc = rs->sr_err = LDAP_OTHER;
                                break;
                        }
 
                } else {
-                       rc = LDAP_UNWILLING_TO_PERFORM;
+                       rc = rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                        break;
                }
        }
 
        /* sanity checks: */
        if ( ro_gotval < 1 ) {
-               rc = LDAP_CONSTRAINT_VIOLATION;
+               rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
                goto done;
        }
 
        if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
-               rc = LDAP_CONSTRAINT_VIOLATION;
+               rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
                goto done;
        }
 
        if ( rp_delete & rp_add ) {
-               rc = LDAP_OTHER;
+               rc = rs->sr_err = LDAP_OTHER;
                goto done;
        }
 
@@ -550,9 +764,9 @@ monitor_subsys_database_modify(
                goto done;
        }
 
-       if ( !bvmatch( &a->a_vals[0], tf ) ) {
+       if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
                attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
-               rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
+               rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
        }
 
        if ( rc == LDAP_SUCCESS ) {
@@ -563,7 +777,7 @@ monitor_subsys_database_modify(
                        } else {
                                a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
                                if ( a == NULL ) {
-                                       rc = LDAP_OTHER;
+                                       rc = rs->sr_err = LDAP_OTHER;
                                        goto done;
                                }
 
@@ -623,14 +837,16 @@ monitor_subsys_database_modify(
                        for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
                                if ( rp_add & restricted_ops[ i ].tag ) {
                                        attr_merge_one( e, mi->mi_ad_restrictedOperation,
-                                                       &restricted_ops[ i ].op, NULL );
+                                                       &restricted_ops[ i ].op,
+                                                       &restricted_ops[ i ].op );
                                }
                        }
 
                        for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
                                if ( rp_add & restricted_exops[ i ].tag ) {
                                        attr_merge_one( e, mi->mi_ad_restrictedOperation,
-                                                       &restricted_exops[ i ].op, NULL );
+                                                       &restricted_exops[ i ].op,
+                                                       &restricted_exops[ i ].op );
                                }
                        }
                }
@@ -641,6 +857,7 @@ monitor_subsys_database_modify(
 done:;
        if ( rc == LDAP_SUCCESS ) {
                attrs_free( save_attrs );
+               rc = SLAP_CB_CONTINUE;
 
        } else {
                Attribute *tmp = e->e_attrs;
@@ -652,11 +869,10 @@ done:;
 
 #if defined(LDAP_SLAPI)
 static int
-monitor_back_add_plugin( Backend *be, Entry *e_database )
+monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database )
 {
-       Slapi_PBlock            *pCurrentPB; 
-       int                     i, rc = LDAP_SUCCESS;
-       struct monitorinfo      *mi = ( struct monitorinfo * )be->be_private;
+       Slapi_PBlock    *pCurrentPB; 
+       int             i, rc = LDAP_SUCCESS;
 
        if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
                /*
@@ -689,8 +905,7 @@ monitor_back_add_plugin( Backend *be, Entry *e_database )
                                srchdesc->spd_version,
                                srchdesc->spd_description );
 
-               bv.bv_val = buf;
-               bv.bv_len = strlen( buf );
+               ber_str2bv( buf, 0, 0, &bv );
                attr_merge_normalize_one( e_database,
                                mi->mi_ad_monitoredInfo, &bv, NULL );