]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_utils.c
improve usability of global ovrlays for write operations; may need to anticipate...
[openldap] / servers / slapd / slapi / slapi_utils.c
index e6b0f523e3deeb8680fd202d2ddc3e6ae0c2d930..f1e2b4e73a6dc506c671ef35c326e5f18850d6c2 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2002-2003 The OpenLDAP Foundation.
+ * Copyright 2002-2004 The OpenLDAP Foundation.
  * Portions Copyright 1997,2002-2003 IBM Corporation.
  * All rights reserved.
  *
@@ -26,7 +26,6 @@
 #include <ac/stdarg.h>
 #include <ac/ctype.h>
 #include <ac/unistd.h>
-#include <ldap_pvt.h>
 
 #include <slap.h>
 #include <slapi.h>
@@ -49,6 +48,22 @@ struct slapi_condvar {
        ldap_pvt_thread_mutex_t mutex;
 };
 
+#ifdef LDAP_SLAPI
+static int checkBVString(const struct berval *bv)
+{
+       int i;
+
+       for ( i = 0; i < bv->bv_len; i++ ) {
+               if ( bv->bv_val[i] == '\0' )
+                       return 0;
+       }
+       if ( bv->bv_val[i] != '\0' )
+               return 0;
+
+       return 1;
+}
+#endif /* LDAP_SLAPI */
+
 /*
  * This function converts an array of pointers to berval objects to
  * an array of berval objects.
@@ -93,7 +108,7 @@ bvptr2obj(
 Slapi_Entry *
 slapi_str2entry(
        char            *s, 
-       int             check_dup )
+       int             flags )
 {
 #ifdef LDAP_SLAPI
        Slapi_Entry     *e = NULL;
@@ -155,7 +170,7 @@ slapi_entry_set_dn(
        char            *ldn )
 {
 #ifdef LDAP_SLAPI
-       struct berval   dn = { 0, NULL };
+       struct berval   dn = BER_BVNULL;
 
        dn.bv_val = ldn;
        dn.bv_len = strlen( ldn );
@@ -293,7 +308,12 @@ slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
        }
 
        if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
-               return slapi_ch_strdup( attr->a_vals[0].bv_val );
+               const char *p;
+
+               p = slapi_value_get_string( &attr->a_vals[0] );
+               if ( p != NULL ) {
+                       return slapi_ch_strdup( (char *)p );
+               }
        }
 
        return NULL;
@@ -327,7 +347,7 @@ slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
 #endif
 }
 
-int
+long
 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
 {
 #ifdef LDAP_SLAPI
@@ -352,7 +372,7 @@ slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
 #endif
 }
 
-int
+unsigned int
 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
 {
 #ifdef LDAP_SLAPI
@@ -377,7 +397,7 @@ slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
 #endif
 }
 
-int
+unsigned long
 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
 {
 #ifdef LDAP_SLAPI
@@ -508,6 +528,64 @@ slapi_is_rootdse( const char *dn )
 #endif
 }
 
+int
+slapi_entry_has_children(const Slapi_Entry *e)
+{
+#ifdef LDAP_SLAPI
+       Connection *pConn;
+       Operation *op;
+       int hasSubordinates = 0;
+
+       pConn = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
+       if ( pConn == NULL ) {
+               return 0;
+       }
+
+       op = (Operation *)pConn->c_pending_ops.stqh_first;
+       op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
+       if ( op->o_bd == NULL ) {
+               return 0;
+       }
+
+       op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
+
+       slapi_int_connection_destroy( &pConn );
+
+       return ( hasSubordinates == LDAP_COMPARE_TRUE );
+#else
+       return 0;
+#endif
+}
+
+/*
+ * Return approximate size of the entry rounded to the nearest
+ * 1K. Only the size of the attribute values are counted in the
+ * Sun implementation.
+ *
+ * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
+ */
+size_t slapi_entry_size(Slapi_Entry *e)
+{
+#ifdef LDAP_SLAPI
+       size_t size;
+       Attribute *a;
+       int i;
+
+       for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
+               for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
+                       size += a->a_vals[i].bv_len + 1;
+               }
+       }
+
+       size += 1023;
+       size -= (size % 1024);
+
+       return size;
+#else
+       return 0;
+#endif /* LDAP_SLAPI */
+}
+
 /*
  * Add values to entry.
  *
@@ -537,12 +615,12 @@ slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
 
        if ( vals == 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;
+                * FIXME: sm_values = NULL ? */
+               mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
+               mod.sm_values->bv_val = NULL;
 
        } else {
-               rc = bvptr2obj( vals, &mod.sm_bvalues );
+               rc = bvptr2obj( vals, &mod.sm_values );
                if ( rc != LDAP_SUCCESS ) {
                        return LDAP_CONSTRAINT_VIOLATION;
                }
@@ -551,7 +629,7 @@ slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
 
        rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
 
-       ch_free( mod.sm_bvalues );
+       ch_free( mod.sm_values );
 
        return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
 #else
@@ -617,7 +695,7 @@ slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **val
                return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
        }
 
-       rc = bvptr2obj( vals, &mod.sm_bvalues );
+       rc = bvptr2obj( vals, &mod.sm_values );
        if ( rc != LDAP_SUCCESS ) {
                return LDAP_CONSTRAINT_VIOLATION;
        }
@@ -625,7 +703,7 @@ slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **val
 
        rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
 
-       ch_free( mod.sm_bvalues );
+       ch_free( mod.sm_values );
 
        return rc;
 #else
@@ -1344,7 +1422,7 @@ slapi_register_supported_control(
 
        slapiControlOp2SlapControlMask( controlops, &controlmask );
 
-       register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control );
+       register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
 #endif /* LDAP_SLAPI */
 }
 
@@ -1550,8 +1628,10 @@ slapi_send_ldap_search_entry(
        rs.sr_ref = NULL;
        rs.sr_ctrls = ectrls;
        rs.sr_attrs = an;
+       rs.sr_operational_attrs = NULL;
        rs.sr_entry = e;
        rs.sr_v2ref = NULL;
+       rs.sr_flags = 0;
 
        if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
                return LDAP_OTHER;
@@ -1588,6 +1668,7 @@ slapi_send_ldap_search_reference(
 
        rs.sr_ctrls = ectrls;
        rs.sr_attrs = NULL;
+       rs.sr_operational_attrs = NULL;
        rs.sr_entry = e;
 
        if ( v2refs != NULL ) {
@@ -2439,8 +2520,8 @@ int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
        char *opAuthType;
 
        if ( op->o_bd != NULL ) {
-               isRoot = be_isroot( op->o_bd, &op->o_ndn );
-               isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
+               isRoot = be_isroot( op );
+               isUpdateDn = be_isupdate( op );
        }
 
        rc = slapi_int_pblock_set_backend( pb, op->o_bd );
@@ -2616,10 +2697,38 @@ 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 );
+       struct berval nval;
+       struct berval *nvalp;
+       int rc;
+       AttributeDescription *desc = a->a_desc;
+
+       if ( desc->ad_type->sat_equality &&
+            desc->ad_type->sat_equality->smr_normalize ) {
+               rc = (*desc->ad_type->sat_equality->smr_normalize)(
+                       SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
+                       desc->ad_type->sat_syntax,
+                       desc->ad_type->sat_equality,
+                       (Slapi_Value *)v, &nval, NULL );
+               if ( rc != LDAP_SUCCESS ) {
+                       return rc;
+               }
+               nvalp = &nval;
+       } else {
+               nvalp = NULL;
+       }
+
+       rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
+       if ( rc == 0 && nvalp != NULL ) {
+               rc = value_add_one( &a->a_nvals, nvalp );
+       } else {
+               a->a_nvals = a->a_vals;
+       }
+
+       if ( nvalp != NULL ) {
+               slapi_ch_free_string( &nval.bv_val );
+       }
+
+       return rc;
 #else
        return -1;
 #endif
@@ -3012,31 +3121,16 @@ int slapi_value_set_int(Slapi_Value *value, int intVal)
 const char *slapi_value_get_string(const Slapi_Value *value)
 {
 #ifdef LDAP_SLAPI
-       if ( value == NULL ) {
-               return NULL;
-       }
+       if ( value == NULL ) return NULL;
+       if ( value->bv_val == NULL ) return NULL;
+       if ( !checkBVString( value ) ) return NULL;
+
        return value->bv_val;
 #else
        return NULL;
 #endif
 }
 
-#ifdef LDAP_SLAPI
-static int checkBVString(const struct berval *bv)
-{
-       int i;
-
-       for ( i = 0; i < bv->bv_len; i++ ) {
-               if ( bv->bv_val[i] == '\0' )
-                       return 0;
-       }
-       if ( bv->bv_val[i] != '\0' )
-               return 0;
-
-       return 1;
-}
-#endif /* LDAP_SLAPI */
-
 int slapi_value_get_int(const Slapi_Value *value)
 {
 #ifdef LDAP_SLAPI
@@ -3329,7 +3423,7 @@ int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char
                mp = ml->sml_next;
 
                /* just free the containing array */
-               slapi_ch_free( (void **)&ml->sml_bvalues );
+               slapi_ch_free( (void **)&ml->sml_values );
                slapi_ch_free( (void **)&ml );
        }
 
@@ -3370,18 +3464,18 @@ LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
                modp->mod_type = ml->sml_type.bv_val;
                ml->sml_type.bv_val = NULL;
 
-               if ( ml->sml_bvalues != NULL ) {
-                       for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
+               if ( ml->sml_values != NULL ) {
+                       for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
                                ;
                        modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
                                sizeof(struct berval *) );
-                       for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
+                       for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
                                /* Take ownership of original values. */
                                modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
-                               modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
-                               modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
-                               ml->sml_bvalues[j].bv_len = 0;
-                               ml->sml_bvalues[j].bv_val = NULL;
+                               modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
+                               modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
+                               ml->sml_values[j].bv_len = 0;
+                               ml->sml_values[j].bv_val = NULL;
                        }
                        modp->mod_bvalues[j] = NULL;
                } else {
@@ -3415,6 +3509,10 @@ Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
        Modifications *modlist = NULL, **modtail;
        LDAPMod **modp;
 
+       if ( mods == NULL ) {
+               return NULL;
+       }
+
        modtail = &modlist;
 
        for( modp = mods; *modp != NULL; modp++ ) {
@@ -3439,24 +3537,24 @@ Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
                }
 
                if ( i == 0 ) {
-                       mod->sml_bvalues = NULL;
+                       mod->sml_values = NULL;
                } else {
-                       mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
+                       mod->sml_values = (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;
+                                       mod->sml_values[i].bv_val = (*bvp)->bv_val;
+                                       mod->sml_values[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_values[i].bv_val = *p;
+                                       mod->sml_values[i].bv_len = strlen( *p );
                                }
                        }
-                       mod->sml_bvalues[i].bv_val = NULL;
-                       mod->sml_bvalues[i].bv_len = 0;
+                       mod->sml_values[i].bv_val = NULL;
+                       mod->sml_values[i].bv_len = 0;
                }
                mod->sml_nvalues = NULL;
 
@@ -3490,10 +3588,10 @@ void slapi_int_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 != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
-                               ch_free( mods[i]->mod_bvalues[j] );
+                       for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
+                               ch_free( mods[i]->mod_values[j] );
                        }
-                       ch_free( mods[i]->mod_bvalues );
+                       ch_free( mods[i]->mod_values );
                } else {
                        ch_free( mods[i]->mod_values );
                }
@@ -3577,7 +3675,7 @@ int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_
                return 1;
        }
 
-       if ( !c->cac_attrsonly ) {
+       if ( !c->cac_attrsonly && a->a_vals != NULL ) {
                for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
                        if ( !access_allowed( op, e,
                                desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
@@ -3947,3 +4045,225 @@ int slapi_int_access_allowed( Operation *op,
 #endif /* LDAP_SLAPI */
 }
 
+/*
+ * There is no documentation for this.
+ */
+int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
+{
+#ifdef LDAP_SLAPI
+       LDAPRDN lrdn;
+       LDAPAVA *ava;
+       int rc;
+       char *p;
+
+       *type = NULL;
+
+       bv->bv_len = 0;
+       bv->bv_val = NULL;
+
+       rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
+       if ( rc != LDAP_SUCCESS ) {
+               return -1;
+       }
+
+       if ( lrdn[1] != NULL ) {
+               return -1; /* not single valued */
+       }
+
+       ava = lrdn[0];
+
+       *type = slapi_ch_strdup( ava->la_attr.bv_val );
+       ber_dupbv( bv, &ava->la_value );
+
+       ldap_rdnfree(lrdn);
+
+       return 0;
+#else
+       return -1;
+#endif /* LDAP_SLAPI */
+}
+
+char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
+{
+#ifdef LDAP_SLAPI
+       struct berval new_dn, parent_dn, newrdn;
+
+       new_dn.bv_val = NULL;
+
+       parent_dn.bv_val = (char *)dn;
+       parent_dn.bv_len = strlen( dn );
+
+       newrdn.bv_val = (char *)rdn;
+       newrdn.bv_len = strlen( rdn );
+
+       build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
+
+       return new_dn.bv_val;
+#else
+       return NULL;
+#endif /* LDAP_SLAPI */
+}
+
+int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       Backend *be;
+       const char *text;
+       char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
+       size_t textlen = sizeof textbuf;
+       int rc;
+
+       if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
+               return -1;
+
+       rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
+
+       return ( rc == LDAP_SUCCESS ) ? 0 : 1;
+#else
+       return -1;
+#endif /* LDAP_SLAPI */
+}
+
+int slapi_entry_rdn_values_present( const Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       LDAPDN dn;
+       int rc;
+       int i = 0, match = 0;
+
+       rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
+       if ( rc != LDAP_SUCCESS ) {
+               return 0;
+       }
+
+       if ( dn[0] != NULL ) {
+               LDAPRDN rdn = dn[0];
+
+               for ( i = 0; rdn[i] != NULL; i++ ) {
+                       LDAPAVA *ava = &rdn[0][i];
+                       Slapi_Attr *a = NULL;
+
+                       if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
+                            slapi_attr_value_find( a, &ava->la_value ) == 0 )
+                               match++;
+               }
+       }
+
+       ldap_dnfree( dn );
+
+       return ( i == match );
+#else
+       return 0;
+#endif /* LDAP_SLAPI */
+}
+
+int slapi_entry_add_rdn_values( Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       LDAPDN dn;
+       int i, rc;
+
+       rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
+       }
+
+       if ( dn[0] != NULL ) {
+               LDAPRDN rdn = dn[0];
+               struct berval *vals[2];
+
+               for ( i = 0; rdn[i] != NULL; i++ ) {
+                       LDAPAVA *ava = &rdn[0][i];
+                       Slapi_Attr *a = NULL;
+
+                       if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
+                            slapi_attr_value_find( a, &ava->la_value ) == 0 )
+                               continue;
+
+                       vals[0] = &ava->la_value;
+                       vals[1] = NULL;
+
+                       slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
+               }
+       }
+
+       ldap_dnfree( dn );
+
+       return LDAP_SUCCESS;
+#else
+       return LDAP_OTHER;
+#endif /* LDAP_SLAPI */
+}
+
+const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
+{
+#ifdef LDAP_SLAPI
+       Attribute *attr;
+       const char *uniqueid;
+
+       attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
+       if ( attr == NULL ) {
+               return NULL;
+       }
+
+       if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
+               return slapi_value_get_string( &attr->a_vals[0] );
+       }
+#endif /* LDAP_SLAPI */
+
+       return NULL;
+}
+
+void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
+{
+#ifdef LDAP_SLAPI
+       struct berval bv;
+
+       attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
+
+       bv.bv_val = uniqueid;
+       bv.bv_len = strlen( uniqueid );
+       attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
+#endif /* LDAP_SLAPI */
+}
+
+LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
+{
+#ifdef LDAP_SLAPI
+       LDAP *ld;
+       char *url;
+       size_t size;
+       int rc;
+
+       size = sizeof("ldap:///");
+       if ( secure )
+               size++;
+       size += strlen( ldaphost );
+       if ( ldapport != 0 )
+               size += 32;
+
+       url = slapi_ch_malloc( size );
+
+       if ( ldapport != 0 ) {
+               sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
+       } else {
+               sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
+       }
+
+       rc = ldap_initialize( &ld, url );
+
+       slapi_ch_free_string( &url );
+
+       return ( rc == LDAP_SUCCESS ) ? ld : NULL;
+#else
+       return NULL;
+#endif /* LDAP_SLAPI */
+}
+
+void slapi_ldap_unbind( LDAP *ld )
+{
+#ifdef LDAP_SLAPI
+       ldap_unbind( ld );
+#endif /* LDAP_SLAPI */
+}
+