]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_utils.c
Sync with HEAD
[openldap] / servers / slapd / slapi / slapi_utils.c
index c52e2bf2a96b9d259fd22792c7d126fe5fcd1edf..7805a319a1506518fc1651bcec9815154a2253ef 100644 (file)
@@ -1,20 +1,23 @@
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- */
-/*
- * (C) Copyright IBM Corp. 1997,2002
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is 
- * given to IBM Corporation. This software is provided ``as is'' 
- * without express or implied warranty.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2002-2003 The OpenLDAP Foundation.
+ * Portions Copyright 1997,2002-2003 IBM Corporation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/*
- * Portions (C) Copyright PADL Software Pty Ltd. 2003
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is 
- * given to PADL Software Pty Ltd. This software is provided ``as is'' 
- * without express or implied warranty.
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by IBM Corporation for use in
+ * IBM products and subsequently ported to OpenLDAP Software by
+ * Steve Omrani.  Additional significant contributors include:
+ *   Luke Howard
  */
 
 #include "portable.h"
 #include <slap.h>
 #include <slapi.h>
 
-struct berval *ns_get_supported_extop( int );
-
-#ifdef _SPARC  
-#include <sys/systeminfo.h>
-#endif
-
 #include <netdb.h>
 
 /*
@@ -83,6 +80,8 @@ bvptr2obj(
                tmpberval[i].bv_val = bvptr[i]->bv_val;
                tmpberval[i].bv_len = bvptr[i]->bv_len;
        }
+       tmpberval[i].bv_val = NULL;
+       tmpberval[i].bv_len = 0;
 
        if ( rc == LDAP_SUCCESS ) {
                *bvobj = tmpberval;
@@ -96,7 +95,7 @@ slapi_str2entry(
        char            *s, 
        int             check_dup )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        Slapi_Entry     *e = NULL;
        char            *pTmpS;
 
@@ -107,9 +106,9 @@ slapi_str2entry(
        }
 
        return e;
-#else /* !defined(LDAP_SLAPI) */
+#else
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char *
@@ -117,7 +116,7 @@ slapi_entry2str(
        Slapi_Entry     *e, 
        int             *len ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        char            *ret;
 
        ldap_pvt_thread_mutex_lock( &entry2str_mutex );
@@ -125,29 +124,29 @@ slapi_entry2str(
        ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
 
        return ret;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_entry_get_dn( Slapi_Entry *e ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return e->e_name.bv_val;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_x_entry_get_id( Slapi_Entry *e )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return e->e_id;
 #else
        return NOID;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void 
@@ -155,43 +154,24 @@ slapi_entry_set_dn(
        Slapi_Entry     *e, 
        char            *ldn )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        struct berval   dn = { 0, NULL };
 
        dn.bv_val = ldn;
        dn.bv_len = strlen( ldn );
 
-       dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
-#endif /* defined(LDAP_SLAPI) */
+       dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_Entry *
 slapi_entry_dup( Slapi_Entry *e ) 
 {
-#if defined(LDAP_SLAPI)
-       char            *tmp = NULL;
-       Slapi_Entry     *tmpEnt;
-       int             len = 0;
-       
-       tmp = slapi_entry2str( e, &len );
-       if ( tmp == NULL ) {
-               return (Slapi_Entry *)NULL;
-       }
-
-       tmpEnt = (Slapi_Entry *)str2entry( tmp );
-       if ( tmpEnt == NULL ) { 
-               slapi_ch_free( (void **)&tmp );
-               return (Slapi_Entry *)NULL;
-       }
-       
-       if (tmp != NULL) {
-               slapi_ch_free( (void **)&tmp );
-       }
-
-       return tmpEnt;
-#else /* !defined(LDAP_SLAPI) */
+#ifdef LDAP_SLAPI
+       return entry_dup( e );
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -199,7 +179,7 @@ slapi_entry_attr_delete(
        Slapi_Entry     *e,             
        char            *type ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
 
@@ -212,27 +192,27 @@ slapi_entry_attr_delete(
        } else {
                return -1;      /* something went wrong */
        }
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_Entry *
 slapi_entry_alloc( void ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_entry_free( Slapi_Entry *e ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        entry_free( e );
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -241,7 +221,7 @@ slapi_entry_attr_merge(
        char            *type, 
        struct berval   **vals ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
        BerVarray               bv;
@@ -257,13 +237,13 @@ slapi_entry_attr_merge(
                return -1;
        }
        
-       rc = attr_merge( e, ad, bv );
+       rc = attr_merge_normalize_one( e, ad, bv, NULL );
        ch_free( bv );
 
        return rc;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int
@@ -272,7 +252,7 @@ slapi_entry_attr_find(
        char            *type, 
        Slapi_Attr      **attr ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
        int                     rc;
@@ -288,9 +268,9 @@ slapi_entry_attr_find(
        }
 
        return 0;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char *
@@ -312,7 +292,7 @@ slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
                return NULL;
        }
 
-       if ( attr->a_vals != NULL && attr->a_vals[0].bv_val != NULL ) {
+       if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
                return slapi_ch_strdup( attr->a_vals[0].bv_val );
        }
 
@@ -445,7 +425,7 @@ slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
        bv.bv_val = (char *)value;
        bv.bv_len = strlen( value );
 
-       return slapi_attr_value_find( attr, &bv );
+       return ( slapi_attr_value_find( attr, &bv ) != -1 );
 #else
        return 0;
 #endif
@@ -469,7 +449,7 @@ slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value
        if ( value != NULL ) {
                bv.bv_val = (char *)value;
                bv.bv_len = strlen(value);
-               attr_merge_one ( e, ad, &bv );
+               attr_merge_normalize_one( e, ad, &bv, NULL );
        }
 #endif /* LDAP_SLAPI */
 }
@@ -556,15 +536,18 @@ slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
        }
 
        if ( vals == NULL ) {
-               /* Apparently vals can be NULL */ 
+               /* Apparently vals can be NULL
+                * FIXME: sm_bvalues = NULL ? */
                mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
                mod.sm_bvalues->bv_val = NULL;
+
        } else {
                rc = bvptr2obj( vals, &mod.sm_bvalues );
                if ( rc != LDAP_SUCCESS ) {
                        return LDAP_CONSTRAINT_VIOLATION;
                }
        }
+       mod.sm_nvalues = NULL;
 
        rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
 
@@ -599,7 +582,7 @@ slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
                return -1;
        }
 
-       return attr_merge( e, ad, *vs );
+       return attr_merge_normalize( e, ad, *vs, NULL );
 #else
        return -1;
 #endif /* LDAP_SLAPI */
@@ -630,14 +613,15 @@ slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **val
        }
 
        if ( vals[0] == NULL ) {
-               /* SLAPI doco says LDAP_OPERATIONS_ERROR */
-               return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OPERATIONS_ERROR : LDAP_SUCCESS;
+               /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
+               return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
        }
 
        rc = bvptr2obj( vals, &mod.sm_bvalues );
        if ( rc != LDAP_SUCCESS ) {
                return LDAP_CONSTRAINT_VIOLATION;
        }
+       mod.sm_nvalues = NULL;
 
        rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
 
@@ -682,7 +666,7 @@ slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value
                return -1;
        }
 
-       rc = attr_merge_one( e, ad, (Slapi_Value *)value );
+       rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
        if ( rc != LDAP_SUCCESS ) {
                return -1;
        }
@@ -794,7 +778,7 @@ slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **val
                return -1;
        }
        
-       rc = attr_merge( e, ad, bv );
+       rc = attr_merge_normalize( e, ad, bv, NULL );
        slapi_ch_free( (void **)&bv );
        if ( rc != LDAP_SUCCESS ) {
                return -1;
@@ -815,7 +799,7 @@ slapi_attr_get_values(
        Slapi_Attr      *attr, 
        struct berval   ***vals ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             i, j;
        struct berval   **bv;
 
@@ -836,54 +820,53 @@ slapi_attr_get_values(
        *vals = (struct berval **)bv;
 
        return 0;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_dn_normalize( char *dn ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        struct berval   bdn;
-       struct berval   ndn;
+       struct berval   pdn;
 
        assert( dn != NULL );
        
        bdn.bv_val = dn;
        bdn.bv_len = strlen( dn );
 
-       dnNormalize2( NULL, &bdn, &ndn );
+       if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
+               return NULL;
+       }
 
-       /*
-        * FIXME: ain't it safe to set dn = ndn.bv_val ?
-        */
-       dn = ch_strdup( ndn.bv_val );
-       ch_free( ndn.bv_val );
-       
-       return dn;
-#else /* !defined(LDAP_SLAPI) */
+       return pdn.bv_val;
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
-/*
- * FIXME: this function is dangerous and should be deprecated;
- * DN normalization is a lot more than lower-casing, and BTW
- * OpenLDAP's DN normalization for case insensitive attributes
- * is already lower case
- */
 char *
 slapi_dn_normalize_case( char *dn ) 
 {
-#if defined(LDAP_SLAPI)
-       slapi_dn_normalize( dn );
-       ldap_pvt_str2lower( dn );
+#ifdef LDAP_SLAPI
+       struct berval   bdn;
+       struct berval   ndn;
 
-       return dn;
-#else /* defined(LDAP_SLAPI) */
+       assert( dn != NULL );
+       
+       bdn.bv_val = dn;
+       bdn.bv_len = strlen( dn );
+
+       if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
+               return NULL;
+       }
+
+       return ndn.bv_val;
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -891,9 +874,10 @@ slapi_dn_issuffix(
        char            *dn, 
        char            *suffix )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        struct berval   bdn, ndn;
        struct berval   bsuffix, nsuffix;
+       int rc;
 
        assert( dn != NULL );
        assert( suffix != NULL );
@@ -904,50 +888,196 @@ slapi_dn_issuffix(
        bsuffix.bv_val = suffix;
        bsuffix.bv_len = strlen( suffix );
 
-       dnNormalize2( NULL, &bdn, &ndn );
-       dnNormalize2( NULL, &bsuffix, &nsuffix );
+       if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
+               return 0;
+       }
+
+       if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
+               != LDAP_SUCCESS )
+       {
+               slapi_ch_free( (void **)&ndn.bv_val );
+               return 0;
+       }
+
+       rc = dnIsSuffix( &ndn, &nsuffix );
+
+       slapi_ch_free( (void **)&ndn.bv_val );
+       slapi_ch_free( (void **)&nsuffix.bv_val );
+
+       return rc;
+#else /* LDAP_SLAPI */
+       return 0;
+#endif /* LDAP_SLAPI */
+}
+
+int
+slapi_dn_isparent(
+       const char      *parentdn,
+       const char      *childdn )
+{
+#ifdef LDAP_SLAPI
+       struct berval   assertedParentDN, normalizedAssertedParentDN;
+       struct berval   childDN, normalizedChildDN;
+       struct berval   normalizedParentDN;
+       int             match;
+
+       assert( parentdn != NULL );
+       assert( childdn != NULL );
+
+       assertedParentDN.bv_val = (char *)parentdn;
+       assertedParentDN.bv_len = strlen( parentdn );
+
+       if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
+               &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
+       {
+               return 0;
+       }
+
+       childDN.bv_val = (char *)childdn;
+       childDN.bv_len = strlen( childdn );
+
+       if ( dnNormalize( 0, NULL, NULL, &childDN,
+               &normalizedChildDN, NULL ) != LDAP_SUCCESS )
+       {
+               slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
+               return 0;
+       }
+
+       dnParent( &normalizedChildDN, &normalizedParentDN );
 
-       return dnIsSuffix( &ndn, &nsuffix );
-#else /* !defined(LDAP_SLAPI) */
+       if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
+               &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
+       {
+               match = -1;
+       }
+
+       slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
+       slapi_ch_free( (void **)&normalizedChildDN.bv_val );
+
+       return ( match == 0 );
+#else
        return 0;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
+}
+
+/*
+ * Returns DN of the parent entry, or NULL if the DN is
+ * an empty string or NULL, or has no parent.
+ */
+char *
+slapi_dn_parent( const char *_dn )
+{
+#ifdef LDAP_SLAPI
+       struct berval   dn, prettyDN;
+       struct berval   parentDN;
+
+       if ( _dn == NULL ) {
+               return NULL;
+       }
+
+       dn.bv_val = (char *)_dn;
+       dn.bv_len = strlen( _dn );
+
+       if ( dn.bv_len == 0 ) {
+               return NULL;
+       }
+
+       if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
+               return NULL;
+       }
+
+       dnParent( &prettyDN, &parentDN ); /* in-place */
+
+       slapi_ch_free( (void **)&prettyDN.bv_val );
+
+       if ( parentDN.bv_len == 0 ) {
+               return NULL;
+       }
+
+       return slapi_ch_strdup( parentDN.bv_val );
+#else
+       return NULL;
+#endif /* LDAP_SLAPI */
+}
+
+/*
+ * Returns DN of the parent entry; or NULL if the DN is
+ * an empty string, if the DN has no parent, or if the
+ * DN is the suffix of the backend database
+ */
+char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
+{
+#ifdef LDAP_SLAPI
+       Backend         *be;
+       struct berval   dn, prettyDN;
+       struct berval   normalizedDN, parentDN;
+
+       if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
+               be = NULL;
+
+       dn.bv_val = (char *)_dn;
+       dn.bv_len = strlen( _dn );
+
+       if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
+               return NULL;
+       }
+
+       if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
+               slapi_ch_free( (void **)&prettyDN.bv_val );
+               slapi_ch_free( (void **)&normalizedDN.bv_val );
+               return NULL;
+       }
+
+       dnParent( &prettyDN, &parentDN );
+
+       slapi_ch_free( (void **)&prettyDN.bv_val );
+       slapi_ch_free( (void **)&normalizedDN.bv_val );
+
+       if ( parentDN.bv_len == 0 ) {
+               return NULL;
+       }
+
+       return slapi_ch_strdup( parentDN.bv_val );
+#else
+       return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_dn_ignore_case( char *dn )
 {       
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return slapi_dn_normalize_case( dn );
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_ch_malloc( unsigned long size ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return ch_malloc( size );       
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_ch_free( void **ptr ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        ch_free( *ptr );
        *ptr = NULL;
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_ch_free_string( char **ptr ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        slapi_ch_free( (void **)ptr );
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void
@@ -1014,11 +1144,11 @@ slapi_ch_calloc(
        unsigned long nelem, 
        unsigned long size ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return ch_calloc( nelem, size );
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char *
@@ -1026,31 +1156,31 @@ slapi_ch_realloc(
        char *block, 
        unsigned long size ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return ch_realloc( block, size );
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_ch_strdup( char *s ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return ch_strdup( (const char *)s );
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 size_t
 slapi_ch_stlen( char *s ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return strlen( (const char *)s );
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return 0;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -1060,7 +1190,7 @@ slapi_control_present(
        struct berval   **val, 
        int             *iscritical ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             i;
        int             rc = 0;
 
@@ -1116,21 +1246,106 @@ slapi_control_present(
        }
 
        return rc;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return 0;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
+#ifdef LDAP_SLAPI
+static void
+slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
+       unsigned long *slapi_mask)
+{
+       *slapi_mask = SLAPI_OPERATION_NONE;
+
+       if ( slap_mask & SLAP_CTRL_ABANDON ) 
+               *slapi_mask |= SLAPI_OPERATION_ABANDON;
+
+       if ( slap_mask & SLAP_CTRL_ADD )
+               *slapi_mask |= SLAPI_OPERATION_ADD;
+
+       if ( slap_mask & SLAP_CTRL_BIND )
+               *slapi_mask |= SLAPI_OPERATION_BIND;
+
+       if ( slap_mask & SLAP_CTRL_COMPARE )
+               *slapi_mask |= SLAPI_OPERATION_COMPARE;
+
+       if ( slap_mask & SLAP_CTRL_DELETE )
+               *slapi_mask |= SLAPI_OPERATION_DELETE;
+
+       if ( slap_mask & SLAP_CTRL_MODIFY )
+               *slapi_mask |= SLAPI_OPERATION_MODIFY;
+
+       if ( slap_mask & SLAP_CTRL_RENAME )
+               *slapi_mask |= SLAPI_OPERATION_MODDN;
+
+       if ( slap_mask & SLAP_CTRL_SEARCH )
+               *slapi_mask |= SLAPI_OPERATION_SEARCH;
+
+       if ( slap_mask & SLAP_CTRL_UNBIND )
+               *slapi_mask |= SLAPI_OPERATION_UNBIND;
+}
+
+static void
+slapiControlOp2SlapControlMask(unsigned long slapi_mask,
+       slap_mask_t *slap_mask)
+{
+       *slap_mask = 0;
+
+       if ( slapi_mask & SLAPI_OPERATION_BIND )
+               *slap_mask |= SLAP_CTRL_BIND;
+
+       if ( slapi_mask & SLAPI_OPERATION_UNBIND )
+               *slap_mask |= SLAP_CTRL_UNBIND;
+
+       if ( slapi_mask & SLAPI_OPERATION_SEARCH )
+               *slap_mask |= SLAP_CTRL_SEARCH;
+
+       if ( slapi_mask & SLAPI_OPERATION_MODIFY )
+               *slap_mask |= SLAP_CTRL_MODIFY;
+
+       if ( slapi_mask & SLAPI_OPERATION_ADD )
+               *slap_mask |= SLAP_CTRL_ADD;
+
+       if ( slapi_mask & SLAPI_OPERATION_DELETE )
+               *slap_mask |= SLAP_CTRL_DELETE;
+
+       if ( slapi_mask & SLAPI_OPERATION_MODDN )
+               *slap_mask |= SLAP_CTRL_RENAME;
+
+       if ( slapi_mask & SLAPI_OPERATION_COMPARE )
+               *slap_mask |= SLAP_CTRL_COMPARE;
+
+       if ( slapi_mask & SLAPI_OPERATION_ABANDON )
+               *slap_mask |= SLAP_CTRL_ABANDON;
+
+       *slap_mask |= SLAP_CTRL_FRONTEND;
+}
+
+static int
+parseSlapiControl(
+       Operation *op,
+       SlapReply *rs,
+       LDAPControl *ctrl )
+{
+       /* Plugins must deal with controls themselves. */
+
+       return LDAP_SUCCESS;
+}
+#endif /* LDAP_SLAPI */
+
 void 
 slapi_register_supported_control(
        char            *controloid, 
        unsigned long   controlops )
 {
-#if defined(LDAP_SLAPI)
-       /* FIXME -- can not add controls to OpenLDAP dynamically */
-       slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
-                       "OpenLDAP does not support dynamic registration of LDAP controls\n" );
-#endif /* defined(LDAP_SLAPI) */
+#ifdef LDAP_SLAPI
+       slap_mask_t controlmask;
+
+       slapiControlOp2SlapControlMask( controlops, &controlmask );
+
+       register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -1138,98 +1353,70 @@ slapi_get_supported_controls(
        char            ***ctrloidsp, 
        unsigned long   **ctrlopsp ) 
 {
-#if defined(LDAP_SLAPI)
-       int             i, n;
-       int             rc = 1;
-       char            **oids = NULL;
-       unsigned long   *masks = NULL;
-
-       for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
-               ; /* count them */
-       }
-       
-       if ( n == 0 ) {
-               /* no controls */
-               *ctrloidsp = NULL;
-               *ctrlopsp = NULL;
-               return LDAP_SUCCESS;
-       }
-
+#ifdef LDAP_SLAPI
+       int i, rc;
 
-       oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
-       if ( oids == NULL ) {
-               rc = LDAP_NO_MEMORY;
-               goto error_return;
+       rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
        }
 
-       masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
-       if ( masks == NULL ) {
-               rc = LDAP_NO_MEMORY;
-               goto error_return;
+       for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
+               /* In place, naughty. */
+               slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
        }
 
-       for ( i = 0; i < n; i++ ) {
-               /*
-                * FIXME: Netscape's specification says nothing about
-                * memory; should we copy the OIDs or return pointers
-                * to internal values? In OpenLDAP the latter is safe
-                * since we do not allow to register coltrols runtime
-                */
-               oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
-               if ( oids[ i ] == NULL ) {
-                       rc = LDAP_NO_MEMORY;
-                       goto error_return;
-               }
-               masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
-       }
-
-       *ctrloidsp = oids;
-       *ctrlopsp = masks;
        return LDAP_SUCCESS;
+#else /* LDAP_SLAPI */
+       return 1;
+#endif /* LDAP_SLAPI */
+}
 
-error_return:
-       if ( rc != LDAP_SUCCESS ) {
-               for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
-                       ch_free( oids[ i ] );
-               }
-               ch_free( oids );
-               ch_free( masks );
-       }
+LDAPControl *
+slapi_dup_control( LDAPControl *ctrl )
+{
+#ifdef LDAP_SLAPI
+       LDAPControl *ret;
 
-       return rc;
-#else /* !defined(LDAP_SLAPI) */
-       return 1;
-#endif /* !defined(LDAP_SLAPI) */
+       ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
+       ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
+       ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
+       ret->ldctl_iscritical = ctrl->ldctl_iscritical;
+
+       return ret;
+#else
+       return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_register_supported_saslmechanism( char *mechanism )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
-       slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
+       slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
                        "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char **
 slapi_get_supported_saslmechanisms( void )
 {
-#if defined(LDAP_SLAPI)
-       /* FIXME -- can not get the saslmechanism wihtout a connection. */
-       slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
-                       "can not get the saslmechanism "
-                       "wihtout a connection\n" );
+#ifdef LDAP_SLAPI
+       /* FIXME -- can not get the saslmechanism without a connection. */
+       slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
+                       "can not get the SASL mechanism list "
+                       "without a connection\n" );
        return NULL;
-#else /* defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 char **
 slapi_get_supported_extended_ops( void )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             i, j, k;
        char            **ppExtOpOID = NULL;
        int             numExtOps = 0;
@@ -1268,9 +1455,9 @@ slapi_get_supported_extended_ops( void )
        ppExtOpOID[ i + k ] = NULL;
 
        return ppExtOpOID;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void 
@@ -1282,33 +1469,43 @@ slapi_send_ldap_result(
        int             nentries, 
        struct berval   **urls ) 
 {
-#if defined(LDAP_SLAPI)
-       Connection      *conn;
+#ifdef LDAP_SLAPI
        Operation       *op;
        struct berval   *s;
        char            *extOID = NULL;
        struct berval   *extValue = NULL;
        int             rc;
+       SlapReply       rs = { REP_RESULT };
 
-       slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
        slapi_pblock_get( pb, SLAPI_OPERATION, &op );
+
+       rs.sr_err = err;
+       rs.sr_matched = matched;
+       rs.sr_text = text;
+       rs.sr_ref = NULL;
+       rs.sr_ctrls = NULL;
+
+       slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
+
        if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
-               slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
-               rc = LDAP_SASL_BIND_IN_PROGRESS;
-               send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
+               slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
+               send_ldap_sasl( op, &rs );
                return;
        }
 
        slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
        if ( extOID != NULL ) {
-               slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
-               slapi_send_ldap_extended_response( conn, op, err, extOID,
-                               extValue );
+               rs.sr_rspoid = extOID;
+               slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
+               send_ldap_extended( op, &rs );
                return;
        }
 
-       send_ldap_result( conn, op, err, matched, text, NULL, NULL );
-#endif /* defined(LDAP_SLAPI) */
+       if (op->o_tag == LDAP_REQ_SEARCH)
+               rs.sr_nentries = nentries;
+
+       send_ldap_result( op, &rs );
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -1319,22 +1516,23 @@ slapi_send_ldap_search_entry(
        char            **attrs, 
        int             attrsonly )
 {
-#if defined(LDAP_SLAPI)
-       Backend         *be;
-       Connection      *pConn;
+#ifdef LDAP_SLAPI
        Operation       *pOp;
-       int             rc;
-
+       SlapReply       rs = { REP_RESULT };
        int             i;
        AttributeName   *an = NULL;
        const char      *text;
 
-       for ( i = 0; attrs[ i ] != NULL; i++ ) {
-               ; /* empty */
+       if ( attrs != NULL ) {
+               for ( i = 0; attrs[ i ] != NULL; i++ ) {
+                       ; /* empty */
+               }
+       } else {
+               i = 0;
        }
 
        if ( i > 0 ) {
-               an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
+               an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
                for ( i = 0; attrs[i] != NULL; i++ ) {
                        an[i].an_name.bv_val = ch_strdup( attrs[i] );
                        an[i].an_name.bv_len = strlen( attrs[i] );
@@ -1342,32 +1540,92 @@ slapi_send_ldap_search_entry(
                        if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
                                return -1;
                }
+               an[i].an_name.bv_len = 0;
+               an[i].an_name.bv_val = NULL;
        }
 
-       if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
-                       ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
-                       ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
-               rc = LDAP_OTHER;
+       rs.sr_err = LDAP_SUCCESS;
+       rs.sr_matched = NULL;
+       rs.sr_text = NULL;
+       rs.sr_ref = NULL;
+       rs.sr_ctrls = ectrls;
+       rs.sr_attrs = an;
+       rs.sr_entry = e;
+       rs.sr_v2ref = NULL;
+
+       if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
+               return LDAP_OTHER;
+       }
+
+       return send_search_entry( pOp, &rs );
+#else /* LDAP_SLAPI */
+       return -1;
+#endif /* LDAP_SLAPI */
+}
+
+int 
+slapi_send_ldap_search_reference(
+       Slapi_PBlock    *pb,
+       Slapi_Entry     *e,
+       struct berval   **references,
+       LDAPControl     **ectrls, 
+       struct berval   **v2refs
+       )
+{
+#ifdef LDAP_SLAPI
+       Operation       *pOp;
+       SlapReply       rs = { REP_SEARCHREF };
+       int             rc;
+
+       rs.sr_err = LDAP_SUCCESS;
+       rs.sr_matched = NULL;
+       rs.sr_text = NULL;
+
+       rc = bvptr2obj( references, &rs.sr_ref );
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
+       }
+
+       rs.sr_ctrls = ectrls;
+       rs.sr_attrs = NULL;
+       rs.sr_entry = e;
+
+       if ( v2refs != NULL ) {
+               rc = bvptr2obj( v2refs, &rs.sr_v2ref );
+               if ( rc != LDAP_SUCCESS ) {
+                       slapi_ch_free( (void **)&rs.sr_ref );
+                       return rc;
+               }
        } else {
-               rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
+               rs.sr_v2ref = NULL;
        }
 
-       return rc;
+       if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
+               return LDAP_OTHER;
+       }
+
+       rc = send_search_reference( pOp, &rs );
+
+       if ( rs.sr_ref != NULL )
+               slapi_ch_free( (void **)&rs.sr_ref );
+
+       if ( rs.sr_v2ref != NULL )
+               slapi_ch_free( (void **)&rs.sr_v2ref );
 
-#else /* !defined(LDAP_SLAPI) */
+       return rc;
+#else
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
-
 Slapi_Filter *
 slapi_str2filter( char *str ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return str2filter( str );
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void 
@@ -1375,9 +1633,9 @@ slapi_filter_free(
        Slapi_Filter    *f, 
        int             recurse ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        filter_free( f );
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
@@ -1424,18 +1682,24 @@ slapi_filter_dup( Slapi_Filter *filter )
                f->f_mra->ma_desc = filter->f_mra->ma_desc;
                f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
                ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
+               break;
        case LDAP_FILTER_SUBSTRINGS: {
                int i;
 
                f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
+               f->f_sub->sa_desc = filter->f_sub->sa_desc;
                ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
-               for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
-                       ;
-               f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
-               for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
-                       ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
+               if ( filter->f_sub_any != NULL ) {
+                       for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
+                               ;
+                       f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
+                       for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
+                               ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
+                       }
+                       f->f_sub_any[i].bv_val = NULL;
+               } else {
+                       f->f_sub_any = NULL;
                }
-               f->f_sub_any[i].bv_val = NULL;
                ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
                break;
        }
@@ -1457,7 +1721,7 @@ slapi_filter_dup( Slapi_Filter *filter )
 int 
 slapi_filter_get_choice( Slapi_Filter *f )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             rc;
 
        if ( f != NULL ) {
@@ -1467,9 +1731,9 @@ slapi_filter_get_choice( Slapi_Filter *f )
        }
 
        return rc;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;              /* invalid filter type */
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -1478,7 +1742,7 @@ slapi_filter_get_ava(
        char            **type, 
        struct berval   **bval )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             ftype;
        int             rc = LDAP_SUCCESS;
 
@@ -1504,15 +1768,15 @@ slapi_filter_get_ava(
        }
 
        return rc;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
 slapi_filter_list_first( Slapi_Filter *f )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             ftype;
 
        if ( f == NULL ) {
@@ -1523,13 +1787,13 @@ slapi_filter_list_first( Slapi_Filter *f )
        if ( ftype == LDAP_FILTER_AND
                        || ftype == LDAP_FILTER_OR
                        || ftype == LDAP_FILTER_NOT ) {
-               return (Slapi_Filter *)f->f_and;
+               return (Slapi_Filter *)f->f_list;
        } else {
                return NULL;
        }
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
@@ -1537,7 +1801,7 @@ slapi_filter_list_next(
        Slapi_Filter    *f, 
        Slapi_Filter    *fprev )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             ftype;
 
        if ( f == NULL ) {
@@ -1553,9 +1817,9 @@ slapi_filter_list_next(
        }
 
        return NULL;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int
@@ -1613,13 +1877,17 @@ slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
         */
        *type = f->f_sub_desc->ad_cname.bv_val;
        *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
-       for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
-               ;
-       *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
-       for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
-               (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
+       if ( f->f_sub_any != NULL ) {
+               for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
+                       ;
+               *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
+               for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
+                       (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
+               }
+               (*any)[i] = NULL;
+       } else {
+               *any = NULL;
        }
-       (*any)[i] = NULL;
        *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
 
        return 0;
@@ -1629,7 +1897,7 @@ slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
 }
 
 Slapi_Filter *
-slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
+slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
 {
 #ifdef LDAP_SLAPI
        Slapi_Filter *f = NULL;
@@ -1641,7 +1909,8 @@ slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
                f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
                f->f_choice = ftype;
                f->f_list = f1;
-               f->f_next = f2;
+               f->f_list->f_next = f2;
+               f->f_next = NULL;
        }
 
        return f;
@@ -1650,13 +1919,42 @@ slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
 #endif /* LDAP_SLAPI */
 }
 
+int
+slapi_x_filter_append( int ftype,
+       Slapi_Filter **pContainingFilter, /* NULL on first call */
+       Slapi_Filter **pNextFilter,
+       Slapi_Filter *filterToAppend )
+{
+#ifdef LDAP_SLAPI
+       if ( ftype == LDAP_FILTER_AND ||
+            ftype == LDAP_FILTER_OR ||
+            ftype == LDAP_FILTER_NOT )
+       {
+               if ( *pContainingFilter == NULL ) {
+                       *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
+                       (*pContainingFilter)->f_choice = ftype;
+                       (*pContainingFilter)->f_list = filterToAppend;
+                       (*pContainingFilter)->f_next = NULL;
+               } else {
+                       if ( (*pContainingFilter)->f_choice != ftype ) {
+                               /* Sanity check */
+                               return -1;
+                       }
+                       (*pNextFilter)->f_next = filterToAppend;
+               }
+               *pNextFilter = filterToAppend;
+
+               return 0;
+       }
+#endif /* LDAP_SLAPI */
+       return -1;
+}
+
 int
 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
        int verify_access )
 {
 #ifdef LDAP_SLAPI
-       Backend *be = NULL;
-       Connection *conn;
        Operation *op;
        int rc;
 
@@ -1666,24 +1964,18 @@ slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
        }
 
        if ( verify_access ) {
-               (void) slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be);
-               rc = slapi_pblock_get(pb, SLAPI_CONNECTION, (void *)&conn);
-               if ( rc != 0 ) {
-                       return LDAP_PARAM_ERROR;
-               }
                rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
                if ( rc != 0 ) {
                        return LDAP_PARAM_ERROR;
                }
        } else {
-               conn = NULL;
                op = NULL;
        }
        /*
         * According to acl.c it is safe to call test_filter() with
         * NULL arguments...
         */
-       rc = test_filter( be, conn, op, e, f );
+       rc = test_filter( op, e, f );
        switch (rc) {
        case LDAP_COMPARE_TRUE:
                rc = 0;
@@ -1724,8 +2016,10 @@ slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_c
        case LDAP_FILTER_NOT:
        case LDAP_FILTER_OR: {
                int rc;
-               Filter *f;
 
+               /*
+                * FIXME: altering f; should we use a temporary?
+                */
                for ( f = f->f_list; f != NULL; f = f->f_next ) {
                        rc = slapi_filter_apply( f, fn, arg, error_code );
                        if ( rc != 0 ) {
@@ -1770,13 +2064,23 @@ slapi_send_ldap_extended_response(
        char            *respName,
        struct berval   *response )
 {
-#if defined(LDAP_SLAPI)
-       send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
-                       respName,response, NULL );
+#ifdef LDAP_SLAPI
+       SlapReply       rs;
+
+       rs.sr_err = errornum;
+       rs.sr_matched = NULL;
+       rs.sr_text = NULL;
+       rs.sr_ref = NULL;
+       rs.sr_ctrls = NULL;
+       rs.sr_rspoid = respName;
+       rs.sr_rspdata = response;
+
+       send_ldap_extended( op, &rs );
+
        return LDAP_SUCCESS;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int 
@@ -1784,14 +2088,14 @@ slapi_pw_find(
        struct berval   **vals, 
        struct berval   *v ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        /*
         * FIXME: what's the point?
         */
        return 1;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return 1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 #define MAX_HOSTNAME 512
@@ -1799,26 +2103,8 @@ slapi_pw_find(
 char *
 slapi_get_hostname( void ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        char            *hn = NULL;
-
-       /*
-        * FIXME: I'd prefer a different check ...
-        */
-#if defined _SPARC 
-       hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
-       if ( hn == NULL) {
-               slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
-                               "can't malloc memory for hostname\n" );
-               hn = NULL;
-               
-       } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
-               slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
-                               "can't get hostname\n" );
-               slapi_ch_free( (void **)&hn );
-               hn = NULL;
-       }
-#else /* !_SPARC */
        static int      been_here = 0;   
        static char     *static_hn = NULL;
 
@@ -1826,8 +2112,8 @@ slapi_get_hostname( void )
        if ( !been_here ) {
                static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
                if ( static_hn == NULL) {
-                       slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
-                                       "can't malloc memory for hostname\n" );
+                       slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
+                                       "Cannot allocate memory for hostname\n" );
                        static_hn = NULL;
                        ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
 
@@ -1836,7 +2122,7 @@ slapi_get_hostname( void )
                } else { 
                        if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
                                slapi_log_error( SLAPI_LOG_FATAL,
-                                               "SLAPI_SYSINFO",
+                                               "SLAPI",
                                                "can't get hostname\n" );
                                slapi_ch_free( (void **)&static_hn );
                                static_hn = NULL;
@@ -1852,12 +2138,11 @@ slapi_get_hostname( void )
        ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
        
        hn = ch_strdup( static_hn );
-#endif /* !_SPARC */
 
        return hn;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return NULL;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 /*
@@ -1872,7 +2157,7 @@ slapi_log_error(
        char            *fmt, 
        ... ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int             rc = LDAP_SUCCESS;
        va_list         arglist;
 
@@ -1881,16 +2166,16 @@ slapi_log_error(
        va_end( arglist );
 
        return rc;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return -1;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 
 unsigned long
 slapi_timer_current_time( void ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        static int      first_time = 1;
 #if !defined (_WIN32)
        struct timeval  now;
@@ -1927,9 +2212,9 @@ slapi_timer_current_time( void )
        QueryPerformanceCounter( &now );
        return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
 #endif /* _WIN32 */
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return 0;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 /*
@@ -1938,13 +2223,13 @@ slapi_timer_current_time( void )
 unsigned long
 slapi_timer_get_time( char *label ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        unsigned long start = slapi_timer_current_time();
        printf("%10ld %10ld usec %s\n", start, 0, label);
        return start;
-#else /* !defined(LDAP_SLAPI) */
+#else /* LDAP_SLAPI */
        return 0;
-#endif /* !defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 /*
@@ -1955,16 +2240,16 @@ slapi_timer_elapsed_time(
        char *label,
        unsigned long start ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        unsigned long stop = slapi_timer_current_time();
        printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 void
 slapi_free_search_results_internal( Slapi_PBlock *pb ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        Slapi_Entry     **entries;
        int             k = 0, nEnt = 0;
 
@@ -1983,15 +2268,15 @@ slapi_free_search_results_internal( Slapi_PBlock *pb )
        }
        
        slapi_ch_free( (void **)&entries );
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
+#ifdef LDAP_SLAPI
 /*
  * Internal API to prime a Slapi_PBlock with a Backend.
  */
-int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
+static int initBackendPB( Slapi_PBlock *pb, Backend *be )
 {
-#if defined(LDAP_SLAPI)
        int rc;
        
        rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
@@ -2005,12 +2290,8 @@ int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
        }
 
        return LDAP_SUCCESS;
-#else
-       return -1;
-#endif /* defined(LDAP_SLAPI) */
 }
 
-#if defined(LDAP_SLAPI)
 /*
  * If oldStyle is TRUE, then a value suitable for setting to
  * the deprecated SLAPI_CONN_AUTHTYPE value is returned 
@@ -2052,14 +2333,12 @@ static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls
 
        return authType;
 }
-#endif
 
 /*
  * Internal API to prime a Slapi_PBlock with a Connection.
  */
-int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
+static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
 {
-#if defined(LDAP_SLAPI)
        char *connAuthType;
        int rc;
 
@@ -2098,7 +2377,13 @@ int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
                return rc;
 
        /* Returns pointer to static string */
-       connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
+       connAuthType = Authorization2AuthType( &conn->c_authz,
+#ifdef HAVE_TLS
+               conn->c_is_tls,
+#else
+               0,
+#endif
+               1 );
        if ( connAuthType != NULL ) {
                rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
                if ( rc != LDAP_SUCCESS )
@@ -2106,46 +2391,66 @@ int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
        }
 
        /* Returns pointer to allocated string */
-       connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
+       connAuthType = Authorization2AuthType( &conn->c_authz,
+#ifdef HAVE_TLS
+               conn->c_is_tls,
+#else
+               0,
+#endif
+               0 );
        if ( connAuthType != NULL ) {
                rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
+               /* slapi_pblock_set dups this itself */
+               slapi_ch_free( (void **)&connAuthType );
                if ( rc != LDAP_SUCCESS )
                        return rc;
        }
 
        if ( conn->c_authz.sai_dn.bv_val != NULL ) {
-               char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
-               rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
+               /* slapi_pblock_set dups this itself */
+               rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
                if ( rc != LDAP_SUCCESS )
                        return rc;
        }
 
+       rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
+       if ( rc != LDAP_SUCCESS )
+               return rc;
+
+       rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
+               ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
+                                                conn->c_sasl_sockctx ) );
+       if ( rc != LDAP_SUCCESS )
+               return rc;
+
        return rc;
-#else
-       return -1;
-#endif /* defined(LDAP_SLAPI) */
 }
+#endif /* LDAP_SLAPI */
 
 /*
  * Internal API to prime a Slapi_PBlock with an Operation.
  */
-int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
+int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        int isRoot = 0;
        int isUpdateDn = 0;
        int rc;
-       Backend *be;
        char *opAuthType;
 
-       if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
-               be = NULL;
-       }
-       if (be != NULL) {
-               isRoot = be_isroot( be, &op->o_ndn );
-               isUpdateDn = be_isupdate( be, &op->o_ndn );
+       if ( op->o_bd != NULL ) {
+               isRoot = be_isroot( op->o_bd, &op->o_ndn );
+               isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
        }
-               
+
+       rc = initBackendPB( pb, op->o_bd );
+       if ( rc != LDAP_SUCCESS )
+               return rc;
+
+       rc = initConnectionPB( pb, op->o_conn );
+       if ( rc != LDAP_SUCCESS )
+               return rc;
+
        rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
        if ( rc != LDAP_SUCCESS )
                return rc;
@@ -2194,16 +2499,20 @@ int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
 
 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
 {
-#if defined( LDAP_SLAPI )
+#ifdef LDAP_SLAPI
        Connection *conn;
 
        slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
+#ifdef HAVE_TLS
        *isSSL = conn->c_is_tls;
+#else
+       *isSSL = 0;
+#endif
 
        return LDAP_SUCCESS;
 #else
        return -1;
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 /*
@@ -2212,7 +2521,7 @@ int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
 
 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
 {
-#if defined( LDAP_SLAPI )
+#ifdef LDAP_SLAPI
        AttributeType *at;
 
        if ( attr == NULL )
@@ -2236,12 +2545,12 @@ int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
        return LDAP_SUCCESS;
 #else
        return -1;
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
 {
-#if defined( LDAP_SLAPI )
+#ifdef LDAP_SLAPI
        unsigned long flags;
 
        if ( slapi_attr_get_flags( attr, &flags ) != 0 )
@@ -2249,7 +2558,7 @@ int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
        return (flags & flag) ? 1 : 0;
 #else
        return 0;
-#endif /* defined(LDAP_SLAPI) */
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_Attr *slapi_attr_new( void )
@@ -2277,6 +2586,7 @@ Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
 
        a->a_desc = ad;
        a->a_vals = NULL;
+       a->a_nvals = NULL;
        a->a_next = NULL;
        a->a_flags = 0;
 
@@ -2306,6 +2616,9 @@ Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
 {
 #ifdef LDAP_SLAPI
+       /*
+        * FIXME: here we may lose alignment between a_vals/a_nvals
+        */
        return value_add_one( &a->a_vals, (Slapi_Value *)v );
 #else
        return -1;
@@ -2357,7 +2670,8 @@ int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const st
        const char *text;
 
        mr = a->a_desc->ad_type->sat_equality;
-       rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
+       rc = value_match( &ret, a->a_desc, mr,
+                       SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
                (struct berval *)v1, (void *)v2, &text );
        if ( rc != LDAP_SUCCESS ) 
                return -1;
@@ -2378,10 +2692,13 @@ int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
        int rc;
        int ret;
 
+       if ( a ->a_vals == NULL ) {
+               return -1;
+       }
        mr = a->a_desc->ad_type->sat_equality;
        for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
                rc = value_match( &ret, a->a_desc, mr,
-                       SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
+                       SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
                if ( rc != LDAP_SUCCESS ) {
                        return -1;
                }
@@ -2389,7 +2706,7 @@ int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
                        return 0;
                }
        }
-#endif
+#endif /* LDAP_SLAPI */
        return -1;
 }
 
@@ -2925,14 +3242,14 @@ void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
        struct berval *val, int access )
 {
-#ifdef LDAPI_SLAPI
+#ifdef LDAP_SLAPI
        Backend *be;
        Connection *conn;
        Operation *op;
        int ret;
        slap_access_t slap_access;
        AttributeDescription *ad = NULL;
-       char *text;
+       const char *text;
 
        ret = slap_str2ad( attr, &ad, &text );
        if ( ret != LDAP_SUCCESS ) {
@@ -2972,7 +3289,7 @@ int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
                return LDAP_PARAM_ERROR;
        }
 
-       ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
+       ret = access_allowed( op, e, ad, val, slap_access, NULL );
 
        return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
 #else
@@ -2983,42 +3300,40 @@ int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
 {
 #ifdef LDAP_SLAPI
-       Backend *be;
-       Connection *conn;
        Operation *op;
-       int ret;
-       Modifications *ml;
-        Modifications *next;
-
-       if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
-               return LDAP_PARAM_ERROR;
-       }
-
-       if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
-               return LDAP_PARAM_ERROR;
-       }
+       int rc = LDAP_SUCCESS;
+       Modifications *ml, *mp;
 
        if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
                return LDAP_PARAM_ERROR;
        }
 
-       ml = slapi_x_ldapmods2modifications( mods );
+       ml = slapi_int_ldapmods2modifications( mods );
        if ( ml == NULL ) {
                return LDAP_OTHER;
        }
 
-       ret = acl_check_modlist( be, conn, op, e, ml );
+       for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
+               rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
+               if ( rc != LDAP_SUCCESS ) {
+                       break;
+               }
+       }
+
+       if ( rc == LDAP_SUCCESS ) {
+               rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
+       }
 
        /* Careful when freeing the modlist because it has pointers into the mods array. */
-       for ( ; ml != NULL; ml = next ) {
-               next = ml->sml_next;
+       for ( ; ml != NULL; ml = mp ) {
+               mp = ml->sml_next;
 
                /* just free the containing array */
                slapi_ch_free( (void **)&ml->sml_bvalues );
                slapi_ch_free( (void **)&ml );
        }
 
-       return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
+       return rc;
 #else
        return LDAP_UNWILLING_TO_PERFORM;
 #endif
@@ -3032,7 +3347,7 @@ int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char
  * 
  * This function must also be called before slap_mods_check().
  */
-LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
+LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
 {
 #ifdef LDAP_SLAPI
        Modifications *ml, *modlist;
@@ -3092,12 +3407,12 @@ LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
  * 
  * The returned Modification list contains pointers into the
  * LDAPMods array; the latter MUST be freed with
- * slapi_x_free_ldapmods() (see below).
+ * slapi_int_free_ldapmods() (see below).
  */
-Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
+Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
 {
 #ifdef LDAP_SLAPI
-       Modifications *modlist, **modtail;
+       Modifications *modlist = NULL, **modtail;
        LDAPMod **modp;
 
        modtail = &modlist;
@@ -3116,28 +3431,33 @@ Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
                mod->sml_next = NULL;
 
                if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
-                       for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ )
+                       for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
                                ;
                } else {
-                       for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ )
+                       for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
                                ;
                }
 
-               mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
-
-               /* NB: This implicitly trusts a plugin to return valid modifications. */
-               if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
-                       for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ ) {
-                               mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
-                               mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
-                       }
+               if ( i == 0 ) {
+                       mod->sml_bvalues = NULL;
                } else {
-                       for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ ) {
-                               mod->sml_bvalues[i].bv_val = *p;
-                               mod->sml_bvalues[i].bv_len = strlen( *p );
+                       mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
+
+                       /* NB: This implicitly trusts a plugin to return valid modifications. */
+                       if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
+                               for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
+                                       mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
+                                       mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
+                               }
+                       } else {
+                               for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
+                                       mod->sml_bvalues[i].bv_val = *p;
+                                       mod->sml_bvalues[i].bv_len = strlen( *p );
+                               }
                        }
+                       mod->sml_bvalues[i].bv_val = NULL;
                }
-               mod->sml_bvalues[i].bv_val = NULL;
+               mod->sml_nvalues = NULL;
 
                *modtail = mod;
                modtail = &mod->sml_next;
@@ -3152,10 +3472,10 @@ Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
 /*
  * This function only frees the parts of the mods array that
  * are not shared with the Modification list that was created
- * by slapi_x_ldapmods2modifications(). 
+ * by slapi_int_ldapmods2modifications(). 
  *
  */
-void slapi_x_free_ldapmods (LDAPMod **mods)
+void slapi_int_free_ldapmods (LDAPMod **mods)
 {
 #ifdef LDAP_SLAPI
        int i, j;
@@ -3169,7 +3489,7 @@ void slapi_x_free_ldapmods (LDAPMod **mods)
                 * Modification list. Do free the containing array.
                 */
                if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
-                       for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ ) {
+                       for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
                                ch_free( mods[i]->mod_bvalues[j] );
                        }
                        ch_free( mods[i]->mod_bvalues );
@@ -3195,11 +3515,9 @@ void slapi_x_free_ldapmods (LDAPMod **mods)
  * op->o_callback->sc_sendentry, if you wish to make computed
  * attributes available to it.
  */
-int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
+int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
 {
 #ifdef LDAP_SLAPI
-       Backend *be = NULL;
-       Connection *conn = NULL;
        Operation *op = NULL;
        BerElement *ber;
        AttributeDescription *desc = NULL;
@@ -3218,16 +3536,6 @@ int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_En
                return 1;
        }
 
-       rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
-       if ( rc != 0 ) {
-               be = NULL; /* no backend for root DSE */
-       }
-
-       rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
-       if ( rc != 0 || conn == NULL ) {
-               return rc;
-       }
-
        rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
        if ( rc != 0 || op == NULL ) {
                return rc;
@@ -3254,8 +3562,8 @@ int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_En
                }
        }
 
-       if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
-               slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
+       if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
+               slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
                        "acl: access to attribute %s not allowed\n",
                        desc->ad_cname.bv_val );
                return 0;
@@ -3263,24 +3571,24 @@ int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_En
 
        rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
        if (rc == -1 ) {
-               slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
+               slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
                        "ber_printf failed\n");
                return 1;
        }
 
        if ( !c->cac_attrsonly ) {
                for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
-                       if ( !access_allowed( be, conn, op, e,
+                       if ( !access_allowed( op, e,
                                desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
-                               slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
-                                       "slapi_x_compute_output_ber: conn %lu "
+                               slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
+                                       "conn %lu "
                                        "acl: access to %s, value %d not allowed\n",
                                        op->o_connid, desc->ad_cname.bv_val, i  );
                                continue;
                        }
        
                        if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
-                               slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
+                               slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
                                        "ber_printf failed\n");
                                return 1;
                        }
@@ -3288,7 +3596,7 @@ int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_En
        }
 
        if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
-               slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
+               slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
                        "ber_printf failed\n" );
                return 1;
        }
@@ -3575,3 +3883,66 @@ int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
 #endif
 }
 
+int slapi_int_access_allowed( Operation *op,
+       Entry *entry,
+       AttributeDescription *desc,
+       struct berval *val,
+       slap_access_t access,
+       AccessControlState *state )
+{
+#ifdef LDAP_SLAPI
+       int rc, slap_access = 0;
+       slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
+
+       if ( op->o_pb == NULL ) {
+               /* internal operation */
+               return 1;
+       }
+
+       switch ( access ) {
+       case ACL_WRITE:
+               slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
+               break;
+       case ACL_READ:
+               slap_access |= SLAPI_ACL_READ;
+               break;
+       case ACL_SEARCH:
+               slap_access |= SLAPI_ACL_SEARCH;
+               break;
+       case ACL_COMPARE:
+                slap_access = ACL_COMPARE;
+               break;
+       default:
+               break;
+        }
+
+       rc = getAllPluginFuncs( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
+       if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
+               /* nothing to do; allowed access */
+               return 1;
+       }
+
+       slapi_int_pblock_set_operation( op->o_pb, op );
+
+       rc = 1; /* default allow policy */
+
+       for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
+               /*
+                * 0    access denied
+                * 1    access granted
+                */
+               rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
+                                       val, slap_access, (void *)state );
+               if ( rc == 0 ) {
+                       break;
+               }
+       }
+
+       slapi_ch_free( (void **)&tmpPlugin );
+
+       return rc;
+#else
+       return 1;
+#endif /* LDAP_SLAPI */
+}
+